Lambda functions
Use a lambda function with these functions to change each element of a list, keep the elements that meet a condition, or combine the elements into one value. Write a lambda function with the lambda keyword, as in lambda x: x * 2, because the arrow form x -> x * 2 fails.
invoke function
Calls lambda_function with the arguments that follow it, and returns the result. The lambda function must have one parameter for each argument that you pass, and it receives the arguments in order. For example, invoke(lambda x, y: x * y, 6, 7) computes 6 * 7. For more information about lambda functions, see Lambda functions.
invoke passes NULL arguments to the lambda function like any other value, so the result for a NULL argument depends on the lambda expression. For example, lambda x: x + 1 returns NULL when x is NULL.
Syntax
invoke(lambda_function, argument [, ...])
Arguments
The invoke function takes the following arguments:
lambda_function-
A lambda function, written as
lambda.parameter[, ...]:expression argument-
One or more values of any type, one for each parameter of
lambda_function, in order.
Return type
The type of the lambda expression, for example INTEGER for lambda x: x + 1 with an INTEGER argument.
Examples
The following SQL query calls a lambda function that adds 1 to its argument, 5.
SELECT invoke(lambda x: x + 1, 5) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query calls a lambda function that has two parameters and returns their product.
SELECT invoke(lambda x, y: x * y, 6, 7) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes NULL to two lambda functions. The first returns NULL, and the second uses coalesce to treat NULL as 0.
SELECT invoke(lambda x: x + 1, NULL) AS plain, invoke( lambda x: coalesce(x, 0) + 1, NULL ) AS with_default
The query returns the following result.
| plain | with_default |
|---|---|
|
|
The following SQL query applies a lambda function to a column, once for each row.
SELECT v, invoke(lambda x: x * x, v) AS squared FROM (VALUES (1), (2), (3)) AS t(v) ORDER BY v
The query returns the following results.
| v | squared |
|---|---|
|
|
|
|
|
|
Considerations
The number of lambda parameters must equal the number of arguments after the lambda function. A mismatch makes the query fail.
list_filter function
Applies a lambda function to each element of list and returns a list of the elements for which the function returns true, in their original order. Elements for which it returns false or NULL are left out. If no element passes, the result is an empty list. If list is NULL, the result is NULL.
The first parameter of the lambda function stands for each element in turn, as x does in lambda x: x > 4. If you name a second parameter, as in lambda x, i: i % 2 = 0, it holds the position of the element, starting at 1. The condition can also use columns of the query. For more information about lambda functions, see Lambda functions.
array_filter and filter are aliases of list_filter.
Syntax
list_filter(list, lambda x: condition)
list_filter(list, lambda x, i: condition)
Arguments
The list_filter function takes the following arguments:
list-
The list to filter.
x-
A name that you choose for the element that the condition tests.
i-
A name that you choose for the position of the element in
list, as aBIGINTthat starts at1. This argument is optional. condition-
An expression that returns
truefor the elements to keep. It can usex,i, and the columns of the query.
Return type
A list of the same type as list, such as INTEGER[] for an INTEGER[] list.
Examples
The following SQL query keeps the elements that are greater than 4.
SELECT list_filter([3, 4, 5], lambda x: x > 4) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query keeps the positive elements. The NULL element is left out because the condition returns NULL for it.
SELECT list_filter( [5, -6, NULL, 7], lambda x: x > 0 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses a second lambda parameter, the position of each element, to keep the elements at even positions.
SELECT list_filter( [10, 20, 30, 40], lambda x, i: i % 2 = 0 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the filter alias on a list column. When no element passes, the result is an empty list.
SELECT order_id, filter( quantities, lambda q: q > 1 ) AS large_quantities FROM (VALUES (1, [1, 2, 3]), (2, [1]) ) AS t(order_id, quantities) ORDER BY order_id
The query returns the following results.
| order_id | large_quantities |
|---|---|
|
|
|
|
Considerations
If the condition does not return a BOOLEAN value, its result is cast to BOOLEAN. For example, a number keeps the element when it is not 0.
Differences from Athena engine version 3
In Athena engine version 3, you write a lambda function with an arrow, as in filter(. DuckDB does not accept the arrow form. Write array, x -> x > 0)list_filter( instead. The name array, lambda x: x > 0)filter also works in DuckDB.
list_reduce function
Applies lambda to the elements of list from first to last, and passes the result of each call to the next call. Without initial_value, the first call receives the first and second elements, and each later call receives the result so far and the next element. With initial_value, the first call receives initial_value and the first element. The function returns the result of the last call.
The lambda function takes two or three parameters, as in lambda acc, x: acc + x. The first parameter holds the result so far, and the second holds the current element. The optional third parameter holds the 1-based position of the current element in the list. For more information about lambda functions, see Lambda functions.
If list is NULL, the result is NULL. array_reduce and reduce are aliases of list_reduce.
Syntax
list_reduce(list, lambda [, initial_value ])
Arguments
The list_reduce function takes the following arguments:
list-
The list to combine, of any element type.
lambda-
The lambda function that combines the elements, of the form
lambdaoracc,x:expressionlambda. In it,acc,x,i:expressionaccis the result so far,xis the current element, andiis the position ofxin the list. initial_value-
The value to start from. When you pass it, the function returns it for an empty list. This argument is optional.
Return type
The common type of the value that lambda returns and the type of the starting value. The starting value is initial_value when you pass it, and the first element of list when you do not. For example, the result is INTEGER when lambda adds INTEGER elements. The position parameter is a BIGINT, so a lambda function that does arithmetic with it can return BIGINT.
Examples
The following SQL query adds the elements of a list. The first call adds 1 and 2, and the second call adds that result to 3.
SELECT list_reduce( [1, 2, 3], lambda acc, x: acc + x ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the reduce alias to join strings with a hyphen.
SELECT reduce( ['a', 'b', 'c'], lambda acc, s: acc || '-' || s ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses the position parameter i and an initial value of 0 to compute 10 * 1 + 20 * 2 + 30 * 3.
SELECT list_reduce( [10, 20, 30], lambda acc, x, i: acc + x * i, 0 ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query passes an empty list with an initial value, so the function returns the initial value.
SELECT list_reduce( CAST([] AS INTEGER[]), lambda acc, x: acc + x, 0 ) AS result
The query returns the following result.
| result |
|---|
|
Considerations
If list is empty and you do not pass initial_value, the query fails. Pass an initial_value when the list can be empty.
NULL elements are passed to lambda like other elements. With an expression such as acc + x, one NULL element makes the result NULL. To skip NULL elements, remove them first, as in list_reduce(list_filter(.list, lambda x: x IS NOT NULL), lambda acc, x: acc + x)
Without initial_value, the first element of list is the starting value, so the value that lambda returns must have a common type with the list elements. For example, list_reduce(['a', 'bb'], lambda acc, x: length(acc) + length(x)) fails, because the lambda function returns a number and the elements are strings. To return a value of another type, pass an initial_value of that type, as in list_reduce(['a', 'bb'], lambda acc, x: acc + length(x), 0).
Differences from Athena engine version 3
In Athena engine version 3, you write a lambda function with an arrow, as in (s, x) -> s + x. DuckDB does not accept the arrow form. Write lambda s, x: s + x instead.
In Athena engine version 3, reduce takes the initial state as its second argument, the combining function third, and an output function fourth. In DuckDB, reduce is an alias of list_reduce, which takes the lambda second and an optional initial value third, and has no output function. For example, rewrite reduce(ARRAY[5, 20, 50], 0, (s, x) -> s + x, s -> s) as list_reduce([5, 20, 50], lambda s, x: s + x, 0).
list_transform function
Returns a list with one element for each element of list, in the same order. Each element of the result is the value of expression for the matching element of list. If list is NULL, the result is NULL. A NULL element is passed to the lambda function like any other value, so its result is whatever expression returns for NULL, which is NULL for most expressions.
The first parameter of the lambda function stands for each element in turn, as x does in lambda x: x + 1. To also use the position of each element, add a second parameter, as in lambda x, i: x * i. The second parameter is the 1-based index of the element, as a BIGINT value. The expression can also refer to the columns of the query. For more information about lambda functions, see Lambda functions.
apply, array_apply, array_transform, and list_apply are aliases of list_transform.
Syntax
list_transform(list, lambda element: expression)
list_transform(list, lambda element, index: expression)
Arguments
The list_transform function takes the following arguments:
list-
The list to transform, as a
LISTvalue. element-
The name that
expressionuses for the current element oflist. index-
The name that
expressionuses for the position of the current element inlist, counting from 1, as aBIGINTvalue. This argument is optional. expression-
The expression to compute for each element. Its type is the element type of the result.
Return type
A LIST whose element type is the type of expression. For example, the result is INTEGER[] when expression returns INTEGER values.
Examples
The following SQL query adds 1 to each element of a list.
SELECT list_transform([1, 2, 3], lambda x: x + 1) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query uses a second lambda parameter, i, which is the position of each element, to append the position to each string.
SELECT list_transform( ['a', 'b', 'c'], lambda x, i: x || CAST(i AS VARCHAR) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query replaces a NULL element with 0.
SELECT list_transform( [1, NULL, 3], lambda x: coalesce(x, 0) ) AS result
The query returns the following result.
| result |
|---|
|
The following SQL query multiplies each element by the value of the factor column, to show that the lambda function can refer to columns of the query.
SELECT list_transform( [1, 2, 3], lambda x: x * factor ) AS result FROM (VALUES (10)) AS v(factor)
The query returns the following result.
| result |
|---|
|
Differences from Athena engine version 3
Athena engine version 3 names this function transform and writes lambda functions with an arrow, as in transform(. Because DuckDB has no array, x -> x + 1)transform function and does not accept the arrow form, write list_transform( instead.array, lambda x: x + 1)