

# 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
<a name="athena-duckdb-functions-list-invoke"></a>

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](duckdb-expressions.md#athena-duckdb-expressions-lambdas).

`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
<a name="athena-duckdb-functions-list-invoke-syntax"></a>

```sql
invoke({{lambda_function}}, {{argument}} [, ...])
```

### Arguments
<a name="athena-duckdb-functions-list-invoke-arguments"></a>

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
<a name="athena-duckdb-functions-list-invoke-return-type"></a>

The type of the lambda expression, for example `INTEGER` for `lambda x: x + 1` with an `INTEGER` argument.

### Examples
<a name="athena-duckdb-functions-list-invoke-examples"></a>

The following SQL query calls a lambda function that adds `1` to its argument, `5`.

```sql
SELECT invoke(lambda x: x + 1, 5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `6` | 

The following SQL query calls a lambda function that has two parameters and returns their product.

```sql
SELECT invoke(lambda x, y: x * y, 6, 7) AS result
```

The query returns the following result.


| result | 
| --- | 
| `42` | 

The following SQL query passes `NULL` to two lambda functions. The first returns `NULL`, and the second uses `coalesce` to treat `NULL` as `0`.

```sql
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 | 
| --- | --- | 
| `NULL` | `1` | 

The following SQL query applies a lambda function to a column, once for each row.

```sql
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 | 
| --- | --- | 
| `1` | `1` | 
| `2` | `4` | 
| `3` | `9` | 

### Considerations
<a name="athena-duckdb-functions-list-invoke-considerations"></a>

The number of lambda parameters must equal the number of arguments after the lambda function. A mismatch makes the query fail.

## `list_filter` function
<a name="athena-duckdb-functions-list-list-filter"></a>

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](duckdb-expressions.md#athena-duckdb-expressions-lambdas).

`array_filter` and `filter` are aliases of `list_filter`.

### Syntax
<a name="athena-duckdb-functions-list-list-filter-syntax"></a>

```sql
list_filter({{list}}, lambda {{x}}: {{condition}})
list_filter({{list}}, lambda {{x}}, {{i}}: {{condition}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-filter-arguments"></a>

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 a `BIGINT` that starts at `1`. This argument is optional.

**{{condition}}**  
An expression that returns `true` for the elements to keep. It can use `x`, `i`, and the columns of the query.

### Return type
<a name="athena-duckdb-functions-list-list-filter-return-type"></a>

A list of the same type as `list`, such as `INTEGER[]` for an `INTEGER[]` list.

### Examples
<a name="athena-duckdb-functions-list-list-filter-examples"></a>

The following SQL query keeps the elements that are greater than 4.

```sql
SELECT list_filter([3, 4, 5], lambda x: x > 4) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[5]` | 

The following SQL query keeps the positive elements. The `NULL` element is left out because the condition returns `NULL` for it.

```sql
SELECT
    list_filter(
        [5, -6, NULL, 7], lambda x: x > 0
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[5, 7]` | 

The following SQL query uses a second lambda parameter, the position of each element, to keep the elements at even positions.

```sql
SELECT
    list_filter(
        [10, 20, 30, 40], lambda x, i: i % 2 = 0
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[20, 40]` | 

The following SQL query uses the `filter` alias on a list column. When no element passes, the result is an empty list.

```sql
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 | 
| --- | --- | 
| `1` | `[2, 3]` | 
| `2` | `[]` | 

### Considerations
<a name="athena-duckdb-functions-list-list-filter-considerations"></a>

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
<a name="athena-duckdb-functions-list-list-filter-differences-from-engine-version-3"></a>

In Athena engine version 3, you write a lambda function with an arrow, as in `filter({{array}}, x -> x > 0)`. DuckDB does not accept the arrow form. Write `list_filter({{array}}, lambda x: x > 0)` instead. The name `filter` also works in DuckDB.

## `list_reduce` function
<a name="athena-duckdb-functions-list-list-reduce"></a>

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](duckdb-expressions.md#athena-duckdb-expressions-lambdas).

If `list` is `NULL`, the result is `NULL`. `array_reduce` and `reduce` are aliases of `list_reduce`.

### Syntax
<a name="athena-duckdb-functions-list-list-reduce-syntax"></a>

```sql
list_reduce({{list}}, {{lambda}} [, {{initial_value}} ])
```

### Arguments
<a name="athena-duckdb-functions-list-list-reduce-arguments"></a>

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 `lambda {{acc}}, {{x}}: {{expression}}` or `lambda {{acc}}, {{x}}, {{i}}: {{expression}}`. In it, `acc` is the result so far, `x` is the current element, and `i` is the position of `x` in 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
<a name="athena-duckdb-functions-list-list-reduce-return-type"></a>

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
<a name="athena-duckdb-functions-list-list-reduce-examples"></a>

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`.

```sql
SELECT
    list_reduce(
        [1, 2, 3], lambda acc, x: acc + x
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `6` | 

The following SQL query uses the `reduce` alias to join strings with a hyphen.

```sql
SELECT
    reduce(
        ['a', 'b', 'c'], lambda acc, s: acc || '-' || s
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `a-b-c` | 

The following SQL query uses the position parameter `i` and an initial value of `0` to compute `10 * 1 + 20 * 2 + 30 * 3`.

```sql
SELECT
    list_reduce(
        [10, 20, 30], lambda acc, x, i: acc + x * i, 0
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `140` | 

The following SQL query passes an empty list with an initial value, so the function returns the initial value.

```sql
SELECT
    list_reduce(
        CAST([] AS INTEGER[]), lambda acc, x: acc + x, 0
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `0` | 

### Considerations
<a name="athena-duckdb-functions-list-list-reduce-considerations"></a>

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
<a name="athena-duckdb-functions-list-list-reduce-differences-from-engine-version-3"></a>

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
<a name="athena-duckdb-functions-list-list-transform"></a>

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](duckdb-expressions.md#athena-duckdb-expressions-lambdas).

`apply`, `array_apply`, `array_transform`, and `list_apply` are aliases of `list_transform`.

### Syntax
<a name="athena-duckdb-functions-list-list-transform-syntax"></a>

```sql
list_transform({{list}}, lambda {{element}}: {{expression}})
list_transform({{list}}, lambda {{element}}, {{index}}: {{expression}})
```

### Arguments
<a name="athena-duckdb-functions-list-list-transform-arguments"></a>

The `list_transform` function takes the following arguments:

**{{list}}**  
The list to transform, as a `LIST` value.

**{{element}}**  
The name that `expression` uses for the current element of `list`.

**{{index}}**  
The name that `expression` uses for the position of the current element in `list`, counting from 1, as a `BIGINT` value. This argument is optional.

**{{expression}}**  
The expression to compute for each element. Its type is the element type of the result.

### Return type
<a name="athena-duckdb-functions-list-list-transform-return-type"></a>

A `LIST` whose element type is the type of `expression`. For example, the result is `INTEGER[]` when `expression` returns `INTEGER` values.

### Examples
<a name="athena-duckdb-functions-list-list-transform-examples"></a>

The following SQL query adds 1 to each element of a list.

```sql
SELECT
    list_transform([1, 2, 3], lambda x: x + 1) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[2, 3, 4]` | 

The following SQL query uses a second lambda parameter, `i`, which is the position of each element, to append the position to each string.

```sql
SELECT
    list_transform(
        ['a', 'b', 'c'], lambda x,
        i: x || CAST(i AS VARCHAR)
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[a1, b2, c3]` | 

The following SQL query replaces a `NULL` element with `0`.

```sql
SELECT
    list_transform(
        [1, NULL, 3], lambda x: coalesce(x, 0)
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1, 0, 3]` | 

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.

```sql
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 | 
| --- | 
| `[10, 20, 30]` | 

### Differences from Athena engine version 3
<a name="athena-duckdb-functions-list-list-transform-differences-from-engine-version-3"></a>

Athena engine version 3 names this function `transform` and writes lambda functions with an arrow, as in `transform({{array}}, x -> x + 1)`. Because DuckDB has no `transform` function and does not accept the arrow form, write `list_transform({{array}}, lambda x: x + 1)` instead.