View a markdown version of this page

List creation and combination functions - Amazon Athena

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

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.

Syntax

array_to_json(list)

Arguments

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

JSON

Examples

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

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.

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.

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

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

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

array_to_string(list, separator)

Arguments

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

VARCHAR

Examples

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

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.

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.

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.

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

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

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

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

array_value(value [, ...])

Arguments

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

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

Examples

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

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.

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.

SELECT array_value(1, 2.5) AS result

The query returns the following result.

result

[1.0, 2.5]

Considerations

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

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

flatten(nested_list)

Arguments

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 LIST of the inner element type, for example INTEGER[] for an INTEGER[][] argument.

Examples

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

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.

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.

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

The query returns the following result.

result

[[1, 2], [3], [4]]

Considerations

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

list_concat function

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

list_concat(list1, list2 [, ...])

Arguments

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

The following SQL query concatenates three lists.

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.

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.

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.

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

The query returns the following result.

result

[a, b, c]

Considerations

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

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

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.

Syntax

list_value([ value [, ...] ])

Arguments

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 LIST of the common type of the arguments, such as INTEGER[] for INTEGER arguments.

Examples

The following SQL query creates a list of three integers.

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.

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.

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.

SELECT list_value(1, 2.5) AS result

The query returns the following result.

result

[1.0, 2.5]

Considerations

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

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

list_zip(list1 [, list2 [, ...] ] [, truncate ])

Arguments

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

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.

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.

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.

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.

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

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

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.