

# List creation and combination functions

Use these functions to build a `LIST` or a fixed-size `ARRAY` from separate values, or to combine several lists into one list. You can also join the elements of a list into one `VARCHAR` string or one JSON array.

## `array_to_json` function
<a name="athena-duckdb-functions-list-array-to-json"></a>

Returns `list` as a `JSON` array, with one JSON element for each element of the list, in order. Numbers and booleans become JSON numbers and booleans, strings become JSON strings, and a `NULL` element becomes JSON `null`. Nested lists become nested JSON arrays, and `STRUCT` and `MAP` elements become JSON objects.

If `list` is `NULL`, the result is `NULL`. To convert a value that is not a list, use the [`to_json` function](duckdb-functions-json-creation.md#athena-duckdb-functions-json-to-json).

### Syntax
<a name="athena-duckdb-functions-list-array-to-json-syntax"></a>

```sql
array_to_json({{list}})
```

### Arguments
<a name="athena-duckdb-functions-list-array-to-json-arguments"></a>

The `array_to_json` function takes the following argument:

**{{list}}**  
The list to convert, as a `LIST` or `ARRAY` value. The elements can be of any type.

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

`JSON`

### Examples
<a name="athena-duckdb-functions-list-array-to-json-examples"></a>

The following SQL query converts a list of integers to a JSON array.

```sql
SELECT array_to_json([1, 2]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1,2]` | 

The following SQL query converts a list of strings that has a `NULL` element. The strings are quoted, and the `NULL` element becomes JSON `null`.

```sql
SELECT array_to_json(['a', NULL, 'c']) AS result
```

The query returns the following result.


| result | 
| --- | 
| `["a",null,"c"]` | 

The following SQL query converts a list of `STRUCT` values to a JSON array of objects.

```sql
SELECT
    array_to_json(
        [{'id': 1, 'tag': 'x'}, {'id': 2, 'tag': 'y'}]
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[{"id":1,"tag":"x"},{"id":2,"tag":"y"}]` | 

### Considerations
<a name="athena-duckdb-functions-list-array-to-json-considerations"></a>

`array_to_json` takes exactly one argument, and that argument must be a `LIST` or an `ARRAY`. Any other value makes the query fail.

## `array_to_string` function
<a name="athena-duckdb-functions-list-array-to-string"></a>

Converts each element of `list` to `VARCHAR` and joins the results into one string, with `separator` between consecutive elements. `NULL` elements are skipped, so they add neither text nor a separator.

For an empty list, the result is an empty string. If every element is `NULL`, or if `list` is `NULL`, the result is `NULL`.

### Syntax
<a name="athena-duckdb-functions-list-array-to-string-syntax"></a>

```sql
array_to_string({{list}}, {{separator}})
```

### Arguments
<a name="athena-duckdb-functions-list-array-to-string-arguments"></a>

The `array_to_string` function takes the following arguments:

**{{list}}**  
The list to join, as a `LIST` or `ARRAY` value. Elements that are not `VARCHAR` values are cast to `VARCHAR` first.

**{{separator}}**  
The string to put between elements, as a `VARCHAR` value.

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

`VARCHAR`

### Examples
<a name="athena-duckdb-functions-list-array-to-string-examples"></a>

The following SQL query joins a list of integers with hyphens.

```sql
SELECT array_to_string([1, 2, 3], '-') AS result
```

The query returns the following result.


| result | 
| --- | 
| `1-2-3` | 

The following SQL query joins a list that has a `NULL` element. The `NULL` element is skipped, and no extra separator is added.

```sql
SELECT array_to_string(['a', NULL, 'c'], ',') AS result
```

The query returns the following result.


| result | 
| --- | 
| `a,c` | 

The following SQL query shows that an empty list returns an empty string, while a list whose elements are all `NULL` returns `NULL`.

```sql
SELECT
    array_to_string(
        []::VARCHAR[], ','
    ) = '' AS empty_list_is_empty,
    array_to_string(
        [NULL, NULL]::VARCHAR[], ','
    ) IS NULL AS all_null_is_null
```

The query returns the following result.


| empty\_list\_is\_empty | all\_null\_is\_null | 
| --- | --- | 
| `true` | `true` | 

The following SQL query replaces each `NULL` element with `?` before it joins the list. The lambda function `lambda x: coalesce(x, '?')` takes each element as `x` and returns `?` when `x` is `NULL`.

```sql
SELECT
    array_to_string(
        list_transform(
            ['a', NULL, 'c'], lambda x: coalesce(x, '?')
        ),
        ','
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `a,?,c` | 

### Considerations
<a name="athena-duckdb-functions-list-array-to-string-considerations"></a>

`array_to_string` requires `separator`. A call with only a list fails. To join the elements with commas, pass `','` as `separator`.

Elements are cast to `VARCHAR` before they are joined, so a nested list appears in its text form, such as `[1, 2]`.

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

Athena engine version 3 names this function `array_join`, and its optional third argument replaces `NULL` elements. In DuckDB, use `array_to_string({{list}}, {{separator}})`, which has no third argument. To replace `NULL` elements, change them before you join, for example `array_to_string(list_transform({{list}}, lambda x: coalesce(CAST(x AS VARCHAR), '?')), ',')`.

In Athena engine version 3, `array_join` returns an empty string when every element is `NULL`. In DuckDB, `array_to_string` returns `NULL` for such a list. To get an empty string instead, use `coalesce(array_to_string({{list}}, {{separator}}), '')`.

## `array_value` function
<a name="athena-duckdb-functions-list-array-value"></a>

Returns an `ARRAY` whose elements are the arguments, in order. The size of the array is part of its type, for example `INTEGER[3]` for three `INTEGER` arguments. A list literal such as `[1, 2, 3]` creates a `LIST` instead, whose type, `INTEGER[]`, has no fixed size.

The arguments are converted to one common type, so `INTEGER` and `DECIMAL` arguments produce a `DECIMAL` array. A `NULL` argument becomes a `NULL` element.

Use `array_value` to build input for the functions that need fixed-size arrays, such as `array_cosine_similarity` and `array_inner_product`.

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

```sql
array_value({{value}} [, ...])
```

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

The `array_value` function takes the following argument:

**{{value}}**  
One or more values that share a common type, such as numbers of different numeric types.

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

An `ARRAY` of the common type of the arguments, sized by the number of arguments, for example `FLOAT[3]` for three `FLOAT` arguments.

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

The following SQL query creates an array of three `FLOAT` values.

```sql
SELECT
    array_value(
        1.0::FLOAT, 2.0::FLOAT, 3.0::FLOAT
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1.0, 2.0, 3.0]` | 

The following SQL query compares the type of an array from `array_value` with the type of a list literal. Only the array type has a size.

```sql
SELECT
    typeof(array_value(1, 2, 3)) AS array_type,
    typeof([1, 2, 3]) AS list_type
```

The query returns the following result.


| array\_type | list\_type | 
| --- | --- | 
| `INTEGER[3]` | `INTEGER[]` | 

The following SQL query mixes an `INTEGER` argument with a `DECIMAL` argument. Both elements become `DECIMAL` values, so the integer is shown as `1.0`.

```sql
SELECT array_value(1, 2.5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1.0, 2.5]` | 

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

`array_value` needs at least one argument.

Arguments that have no common type, such as an `INTEGER` and a `VARCHAR`, make the query fail. Cast them to one type first.

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

Joins the inner lists of `nested_list`, in order, into one list. Only one level of nesting is removed, so a list of lists of lists becomes a list of lists.

Inner lists that are `NULL` are skipped, while `NULL` elements inside an inner list are kept. If `nested_list` is `NULL`, the result is `NULL`.

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

```sql
flatten({{nested_list}})
```

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

The `flatten` function takes the following argument:

**{{nested\_list}}**  
The list of lists to flatten, as a `LIST` whose elements are lists, such as an `INTEGER[][]` value.

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

A `LIST` of the inner element type, for example `INTEGER[]` for an `INTEGER[][]` argument.

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

The following SQL query flattens a list of two integer lists into one list.

```sql
SELECT flatten([[1, 2, 3], [4, 5]]) AS result
```

The query returns the following result.


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

The following SQL query shows how `NULL` values are treated. The `NULL` inner list and the empty list add nothing, and the `NULL` element of the first inner list is kept.

```sql
SELECT flatten([[1, NULL], NULL, [], [3]]) AS result
```

The query returns the following result.


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

The following SQL query flattens a list that is nested three levels deep. Only the outer level is removed.

```sql
SELECT flatten([[[1, 2], [3]], [[4]]]) AS result
```

The query returns the following result.


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

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

The argument must be a list of lists. A list of single values, such as `[1, 2]`, makes the query fail.

## `list_concat` function
<a name="athena-duckdb-functions-list-list-concat"></a>

Returns one list that holds the elements of each argument, in argument order. A `NULL` argument adds no elements, so `list_concat([1, 2], NULL)` returns `[1, 2]`. `NULL` elements inside a list are kept.

`array_cat`, `array_concat`, and `list_cat` are aliases of `list_concat`. You can also concatenate two lists with the `||` operator, but unlike `list_concat`, `||` returns `NULL` when either operand is `NULL`.

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

```sql
list_concat({{list1}}, {{list2}} [, ...])
```

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

The `list_concat` function takes the following arguments:

**{{list1}}**  
The first list.

**{{list2}}**  
The list to append to `list1`. You can pass more lists as further arguments.

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

A list whose element type is the common type of the element types of the arguments. For example, two `INTEGER[]` lists return an `INTEGER[]` list, and an `INTEGER[]` list with a list of decimals returns a list of `DECIMAL` values.

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

The following SQL query concatenates three lists.

```sql
SELECT list_concat([2, 3], [4, 5, 6], [7]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[2, 3, 4, 5, 6, 7]` | 

The following SQL query compares `list_concat` and the `||` operator when one list is `NULL`. `list_concat` skips the `NULL` list, and `||` returns `NULL`.

```sql
SELECT
    list_concat(
        [1, 2], CAST(NULL AS INTEGER[])
    ) AS function_result,
    [1, 2] || CAST(NULL AS INTEGER[]) AS operator_result
```

The query returns the following result.


| function\_result | operator\_result | 
| --- | --- | 
| `[1, 2]` | `NULL` | 

The following SQL query shows that when every argument is a `NULL` list, the result is an empty list.

```sql
SELECT
    list_concat(
        CAST(NULL AS INTEGER[]), CAST(NULL AS INTEGER[])
    ) AS result
```

The query returns the following result.


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

The following SQL query uses the `array_cat` alias to concatenate two lists of strings.

```sql
SELECT array_cat(['a'], ['b', 'c']) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[a, b, c]` | 

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

The element types of the lists must have a common type. For example, concatenating an `INTEGER[]` list and a `VARCHAR[]` list causes an error. Cast one of the lists first, for example `CAST({{list}} AS VARCHAR[])`.

When every argument is a `NULL` list, the result is an empty list, not `NULL`.

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

In Athena engine version 3, `concat` on arrays returns `NULL` when any argument is `NULL`. In DuckDB, `concat` and `list_concat` skip `NULL` arguments instead. A query that you move from Athena engine version 3 can therefore return a list where it returned `NULL` before. To keep the Athena engine version 3 result, use the `||` operator, which returns `NULL` for a `NULL` operand.

## `list_value` function
<a name="athena-duckdb-functions-list-list-value"></a>

Returns a `LIST` whose elements are the arguments, in the order that you pass them. `list_value(1, 2, 3)` returns the same list as the list literal `[1, 2, 3]`. A `NULL` argument becomes a `NULL` element. With no arguments, the result is an empty list.

The arguments are converted to one common type, which becomes the element type of the list. For example, `list_value(1, 2.5)` converts `1` to a `DECIMAL` value. `list_pack` is an alias of `list_value`. To create a fixed-size `ARRAY` value instead of a `LIST`, use the [`array_value` function](#athena-duckdb-functions-list-array-value).

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

```sql
list_value([ {{value}} [, ...] ])
```

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

The `list_value` function takes the following argument:

**{{value}}**  
A value to put in the list, of any type. All the values must have a common type that each of them can be converted to.

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

A `LIST` of the common type of the arguments, such as `INTEGER[]` for `INTEGER` arguments.

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

The following SQL query creates a list of three integers.

```sql
SELECT list_value(4, 5, 6) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[4, 5, 6]` | 

The following SQL query passes `NULL` as one of the arguments, which becomes a `NULL` element.

```sql
SELECT list_value(1, NULL, 3) AS result
```

The query returns the following result.


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

The following SQL query builds a list from two columns and an expression, for each row.

```sql
SELECT x, list_value(x, y, x + y) AS result
FROM (VALUES (1, 2), (3, 4)) AS v(x, y)
ORDER BY x
```

The query returns the following results.


| x | result | 
| --- | --- | 
| `1` | `[1, 2, 3]` | 
| `3` | `[3, 4, 7]` | 

The following SQL query passes an integer and a decimal number. The integer is converted to the common type, `DECIMAL`.

```sql
SELECT list_value(1, 2.5) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[1.0, 2.5]` | 

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

When you mix a string with values of another type, the query fails unless the string can be converted to that type. For example, `list_value(1, 'a')` fails because `'a'` is not a number, and a `VARCHAR` column next to an `INTEGER` column fails too. Cast the values to one type first, as in `list_value(CAST({{x}} AS VARCHAR), {{s}})`.

## `list_zip` function
<a name="athena-duckdb-functions-list-list-zip"></a>

Returns a list whose element `n` is an unnamed `STRUCT` value that holds element `n` of each input list, in the order of the arguments. The `typeof` function shows an unnamed `STRUCT` type as `TUPLE`, for example `TUPLE(INTEGER, VARCHAR)`. To read a field, use its position, as in `{{element}}[1]`.

By default, the result is as long as the longest input list, and the fields for positions past the end of a shorter list are `NULL`. If you pass `true` as the last argument, the result is as long as the shortest input list instead. A `NULL` list argument is treated as an empty list, so its fields are `NULL`.

`array_zip` is an alias of `list_zip`.

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

```sql
list_zip({{list1}} [, {{list2}} [, ...] ] [, {{truncate}} ])
```

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

The `list_zip` function takes the following arguments:

**{{list1}}**  
The first list to combine, as a `LIST` value.

**{{list2}}**  
The other lists to combine, as `LIST` values. The lists can have different element types. This argument is optional.

**{{truncate}}**  
A flag that controls the length of the result, as a `BOOLEAN` value that follows the lists. If it is `true`, the result is as long as the shortest list. The default is `false`. This argument is optional.

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

A `LIST` of unnamed `STRUCT` values with one field for each input list. The `typeof` function shows the type as `TUPLE` followed by the element types, such as `TUPLE(INTEGER, INTEGER, INTEGER)[]` for three lists of `INTEGER` values.

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

The following SQL query combines three lists of the same length. Each element of the result is an unnamed struct, shown in parentheses, that holds the elements at one position of the three lists.

```sql
SELECT list_zip([1, 2], [3, 4], [5, 6]) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[(1, 3, 5), (2, 4, 6)]` | 

The following SQL query combines lists of different lengths, first with the default behavior and then with `true` as the last argument. The default behavior pads the shorter lists with `NULL`. With `true`, the result stops at the end of the shortest list.

```sql
SELECT
    list_zip([1, 2], [3, 4], [5, 6, 7]) AS padded,
    list_zip(
        [1, 2], [3, 4], [5, 6, 7], true
    ) AS truncated
```

The query returns the following result.


| padded | truncated | 
| --- | --- | 
| `[(1, 3, 5), (2, 4, 6), (NULL, NULL, 7)]` | `[(1, 3, 5), (2, 4, 6)]` | 

The following SQL query passes a `NULL` list as the second argument. The `list_zip` result is not `NULL`. The second field of each struct is `NULL`.

```sql
SELECT
    list_zip([1, 2], CAST(NULL AS VARCHAR[])) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[(1, NULL), (2, NULL)]` | 

The following SQL query adds two lists element by element. It combines the lists with `list_zip`, and then adds the two fields of each struct, which it reads by position.

```sql
SELECT
    list_transform(
        list_zip([1, 2], [10, 20]),
        lambda p: p[1] + p[2]
    ) AS result
```

The query returns the following result.


| result | 
| --- | 
| `[11, 22]` | 

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

The fields of the structs have no names, so you cannot refer to a field by name. Use its position, or cast the result to a list of named structs, as in `CAST(list_zip({{a}}, {{b}}) AS STRUCT(num INTEGER, letter VARCHAR)[])`.

An Iceberg table cannot store unnamed `STRUCT` (`TUPLE`) values. Before you write the result to an Iceberg table, cast it to a list of named structs.

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

Athena engine version 3 names this function `zip`. DuckDB has no `zip` function, so replace `zip({{array1}}, {{array2}})` with `list_zip({{array1}}, {{array2}})`. Both fill the positions past the end of a shorter array with `NULL`.