DuckDB lambda functions (a.k.a. ‘lambdas’ or ‘lambda expressions’) are expressions that define an inline anonymous function.
Lambdas are passed as argument to certain list functions, which evaluate the passed lambda expression against its elements.
The value resulting from the lambda’s evaluation is then processed further by the list function to create its return value.
Lambdas may also be used in the COLUMNS clause.
In that context, the lambda function acts as a filter: if it evaluates to true, the column is retained, and discarded otherwise.
COLUMNS-clause lambdas have a mandatory parameter that receives the column name. Optionally, a second parameter may be declared that receives the (1-based) column index.
The syntax for a lambda expression is:
lambda ⟨parameters⟩ : ⟨expression⟩
⟨parameters⟩{:.language-sql .highlight} is a comma-separated list of parameter names. Parameters may be given any name, and their name may be referenced in the ⟨expression⟩{:.language-sql .highlight}.
The number of allowed and required parameters depends on the list function to which it is passed.
There is always a mandatory parameter that is used by the list function to pass the current element of the list. This is typically the first parameter.
There is always an optional parameter used by the list function to pass the (1-based) index of the current element. This is always the last parameter.
The expression must be a scalar expression. Additionally, it cannot contain a subquery, table function, and the like.
The expression may contain macro invocations. However, only scalar macros are allowed, and only if they do not rely on a subquery.
For example, the following are all valid lambda functions:
Constructs a list from those elements of the input list for which the lambda function returns true. DuckDB must be able to cast the lambda function’s return type to BOOL. The return type of list_filter is the same as the input list’s. See list_filter examples.
Reduces all elements of the input list into a single scalar value by executing the lambda function on a running result and the next list element. The lambda function has an optional initial_value argument. See list_reduce examples.
Returns a list that is the result of applying the lambda function to each element of the input list. The return type is defined by the return type of the lambda function. See list_transform examples.
| Description | Constructs a list from those elements of the input list for which the lambda function returns true. DuckDB must be able to cast the lambda function’s return type to BOOL. The return type of list_filter is the same as the input list’s. See list_filter examples. |
| Example | list_filter([3, 4, 5], lambda x : x > 4) |
| Result | [5] |
| Aliases | array_filter, filter |
list_reduce(list, lambda(x,y)[, initial_value])
| Description | Reduces all elements of the input list into a single scalar value by executing the lambda function on a running result and the next list element. The lambda function has an optional initial_value argument. See list_reduce examples. |
| Example | list_reduce([1, 2, 3], lambda x, y : x + y) |
| Result | 6 |
| Aliases | array_reduce, reduce |
list_transform(list, lambda(x))
| Description | Returns a list that is the result of applying the lambda function to each element of the input list. The return type is defined by the return type of the lambda function. See list_transform examples. |
| Example | list_transform([1, 2, 3], lambda x : x + 1) |
| Result | [2, 3, 4] |
| Aliases | apply, array_apply, array_transform, list_apply |
Nesting Lambda Functions
All scalar functions can be arbitrarily nested. For example, nested lambda functions to get all squares of even list elements:
Nested lambda function to add each element of the first list to the sum of the second list:
SELECT list_transform(
[1, 2, 3],
lambda x :
list_reduce([4, 5, 6], lambda a, b: a + b) + x
);
[16, 17, 18]
Scoping
Lambda functions conform to scoping rules in the following order:
inner lambda parameters
outer lambda parameters
column names
macro parameters
CREATETABLEtbl (x INTEGER);
INSERT INTO tbl VALUES (10);
SELECT list_apply(
[1, 2],
lambda x: list_apply([4], lambda x: x +tbl.x)[1] + x
)
FROM tbl;
[15, 16]
Indexes as Parameters
All lambda functions accept an optional extra parameter that represents the index of the current element.
This is always the last parameter of the lambda function (e.g., i in (x, i)), and is 1-based (i.e., the first element has index 1).
Get all elements that are larger than their index:
subqueries in lambda expressions are not supported
The expression can contain macro calls, but only if the macro is a scalar macro. In addition, the macro must not contain a subquery or rely on another macro that contains a subquery.
lambda functions must be defined inline as argument to the list function. You cannot return a lambda function from a macro (but you can invoke a macro in the lambda’s expression).
Deprecated Syntax
Deprecated DuckDB v1.3 deprecated the old lambda single arrow syntax (x -> x + 1)
in favor of the Python-style syntax (lambda x : x + 1).
DuckDB v1.3 also introduces a new setting to configure the lambda syntax.
SET lambda_syntax ='DEFAULT';
SET lambda_syntax ='ENABLE_SINGLE_ARROW';
SET lambda_syntax ='DISABLE_SINGLE_ARROW';
Currently, DEFAULT enables both syntax styles, i.e.,
the old single arrow syntax and the Python-style syntax.
DuckDB v1.5 is the last release supporting the single arrow syntax without explicitly enabling it.
DuckDB v2.0 will disable the single arrow syntax by default.
DuckDB v2.1 will remove the lambda_syntax flag and fully deprecates the single arrow syntax,
so the old behavior will no longer be possible.
This is an unofficial website and is not affiliated with DuckDB. Official site:duckdb.org.duckdb.ubitools.com · Translated and built with Astro and daisyUI