v.22.1Improvement

Added Support for Subqueries as SQL User Defined Functions

Added support for specifying subquery as SQL user defined function. Example: CREATE FUNCTION test AS () -> (SELECT 1). Closes #30755. #32758 (Maksim Kita).
Added support for defining SQL user-defined functions (UDFs) using subqueries in ClickHouse, allowing users to create functions that return the results of a SELECT statement.

Why it matters

This feature enables users to encapsulate complex subqueries as reusable functions, improving query readability and maintainability by avoiding repetitive query patterns. It solves the problem of lacking native support for subquery-based UDFs in ClickHouse.

How to use it

Users can create a subquery-based user-defined function using the syntax:

CREATE FUNCTION function_name AS () -> (SELECT ...)


For example:

CREATE FUNCTION test AS () -> (SELECT 1)


After creation, the function can be used in queries to invoke the defined subquery logic.