View a markdown version of this page

List element addition and removal functions - Amazon Athena

List element addition and removal functions

Use these functions to add an element to the start or the end of a list, to remove its first or last element, or to change its length.

array_append function

Returns a new list that holds the elements of list followed by element. The element is added even when it is NULL, so the result has one more element than list.

If list is NULL, the function treats it as an empty list and returns a list that holds only element. If you pass an ARRAY value, the result is a LIST.

array_append returns the same result as list_append and array_push_back. To add an element to the start of a list, use the array_prepend function or the array_push_front function.

Syntax

array_append(list, element)

Arguments

The array_append function takes the following arguments:

list

The list to add to.

element

The value to add. It must have the element type of list, or a type that DuckDB can convert to a common type with it.

Return type

A list of the element type of list, such as INTEGER[]. If element has a wider type, the result has the common type of both, for example a DECIMAL list when you add 2.5 to [1, 2].

Examples

The following SQL query adds 4 to the end of a list.

SELECT array_append([2, 3], 4) AS result

The query returns the following result.

result

[2, 3, 4]

The following SQL query adds an element to a NULL list, which returns a list with one element, and adds a NULL element to a list.

SELECT array_append( CAST(NULL AS INTEGER[]), 4 ) AS null_list, array_append([2, 3], NULL) AS null_element

The query returns the following result.

null_list null_element

[4]

[2, 3, NULL]

The following SQL query adds an element to an INTEGER[2] array and returns the type of the result, which is a list.

SELECT typeof( array_append([1, 2]::INTEGER[2], 3) ) AS result_type

The query returns the following result.

result_type

INTEGER[]

Considerations

If element cannot be converted to the element type of the list, as when you add a string to an INTEGER list, the query fails.

To add all the elements of another list, use the list_concat function or the || operator. If element is itself a list, array_append adds it as one element, which works only when list is a list of lists.

Differences from Athena engine version 3

In Athena engine version 3, the || operator also adds a single element to the end of an array, as in ARRAY[1, 2] || 3. In DuckDB, || joins two lists only, so [1, 2] || 3 fails. Use array_append([1, 2], 3), or put the element in a list, as in [1, 2] || [3].

array_pop_back function

Returns list without its last element. For a list with one element, or for an empty list, the result is an empty list. If list is NULL, the result is NULL.

Unlike a pop operation in a programming language, the function returns the shortened list, not the element that it removes. To get the last element, use list[-1]. If you pass an ARRAY value, the result is a LIST.

Syntax

array_pop_back(list)

Arguments

The array_pop_back function takes the following argument:

list

The list to remove the last element from.

Return type

A list of the element type of list, such as INTEGER[].

Examples

The following SQL query removes the last element of a list.

SELECT array_pop_back([4, 5, 6]) AS result

The query returns the following result.

result

[4, 5]

The following SQL query shows that a list with one element and an empty list both return an empty list.

SELECT array_pop_back([4]) AS one_element, array_pop_back(CAST([] AS INTEGER[])) AS empty_list

The query returns the following result.

one_element empty_list

[]

[]

The following SQL query returns the last element of a list and the other elements, in two columns.

SELECT [4, 5, 6][-1] AS last_element, array_pop_back([4, 5, 6]) AS other_elements

The query returns the following result.

last_element other_elements

6

[4, 5]

array_pop_front function

Returns list without its first element. For a list with one element, or for an empty list, the result is an empty list. If list is NULL, the result is NULL.

Unlike a pop operation in a programming language, the function returns the shortened list, not the element that it removes. To get the first element, use list[1]. If you pass an ARRAY value, the result is a LIST.

Syntax

array_pop_front(list)

Arguments

The array_pop_front function takes the following argument:

list

The list to remove the first element from.

Return type

A list of the element type of list, such as INTEGER[].

Examples

The following SQL query removes the first element of a list.

SELECT array_pop_front([4, 5, 6]) AS result

The query returns the following result.

result

[5, 6]

The following SQL query shows that a list with one element returns an empty list, and that a NULL list returns NULL.

SELECT array_pop_front([4]) AS one_element, array_pop_front( CAST(NULL AS INTEGER[]) ) AS null_list

The query returns the following result.

one_element null_list

[]

NULL

The following SQL query returns the first element of a list and the other elements, in two columns.

SELECT [4, 5, 6][1] AS first_element, array_pop_front([4, 5, 6]) AS other_elements

The query returns the following result.

first_element other_elements

4

[5, 6]

array_prepend function

Returns a new list that holds element followed by the elements of list. The element is added even when it is NULL. If list is NULL, the function treats it as an empty list and returns a list that holds only element. If you pass an ARRAY value, the result is a LIST.

The element comes first in the arguments, as it does in the result. The array_push_front function also adds an element to the start of a list, but it takes the list first. The array_prepend function returns the same result as list_prepend. To add an element to the end of a list, use the array_append function.

Syntax

array_prepend(element, list)

Arguments

The array_prepend function takes the following arguments:

element

The value to add. It must have the element type of list, or a type that DuckDB can convert to a common type with it.

list

The list to add to.

Return type

A list of the element type of list, such as INTEGER[]. If element has a wider type, the result has the common type of both.

Examples

The following SQL query adds 1 to the start of a list.

SELECT array_prepend(1, [2, 3]) AS result

The query returns the following result.

result

[1, 2, 3]

The following SQL query adds a NULL element to a list, and adds an element to a NULL list, which returns a list with one element.

SELECT array_prepend(NULL, [2, 3]) AS null_element, array_prepend( 1, CAST(NULL AS INTEGER[]) ) AS null_list

The query returns the following result.

null_element null_list

[NULL, 2, 3]

[1]

Considerations

If you pass the list first, as in array_prepend([2, 3], 1), the query fails. Put the element first, or use array_push_front([2, 3], 1).

Differences from Athena engine version 3

In Athena engine version 3, the || operator also adds a single element to the start of an array, as in 0 || ARRAY[1, 2]. In DuckDB, || joins two lists only, so 0 || [1, 2] fails. Use array_prepend(0, [1, 2]), or put the element in a list, as in [0] || [1, 2].

array_push_back function

Returns a new list that holds the elements of list followed by element. The element is added even when it is NULL. If list is NULL, the function treats it as an empty list and returns a list that holds only element. If you pass an ARRAY value, the result is a LIST.

array_push_back returns the same result as array_append and list_append. To add an element to the start of a list, use the array_push_front function.

Syntax

array_push_back(list, element)

Arguments

The array_push_back function takes the following arguments:

list

The list to add to.

element

The value to add. It must have the element type of list, or a type that DuckDB can convert to a common type with it.

Return type

A list of the element type of list, such as INTEGER[]. If element has a wider type, the result has the common type of both.

Examples

The following SQL query adds 4 to the end of a list.

SELECT array_push_back([2, 3], 4) AS result

The query returns the following result.

result

[2, 3, 4]

The following SQL query adds a NULL element to the end of a list of strings.

SELECT array_push_back(['a', 'b'], NULL) AS result

The query returns the following result.

result

[a, b, NULL]

Considerations

If element cannot be converted to the element type of the list, as when you add a string to an INTEGER list, the query fails.

array_push_front function

Returns a new list that holds element followed by the elements of list. The element is added even when it is NULL. If list is NULL, the function treats it as an empty list and returns a list that holds only element. If you pass an ARRAY value, the result is a LIST.

array_push_front takes the list first and the element second. array_prepend returns the same result, with the arguments in the other order. To add an element to the end of a list, use the array_push_back function.

Syntax

array_push_front(list, element)

Arguments

The array_push_front function takes the following arguments:

list

The list to add to.

element

The value to add. It must have the element type of list, or a type that DuckDB can convert to a common type with it.

Return type

A list of the element type of list, such as INTEGER[]. If element has a wider type, the result has the common type of both.

Examples

The following SQL query adds 1 to the start of a list.

SELECT array_push_front([2, 3], 1) AS result

The query returns the following result.

result

[1, 2, 3]

The following SQL query adds a NULL element to the start of a list, and adds an element to a NULL list, which returns a list with one element.

SELECT array_push_front(['b', 'c'], NULL) AS null_element, array_push_front( CAST(NULL AS VARCHAR[]), 'a' ) AS null_list

The query returns the following result.

null_element null_list

[NULL, b, c]

[a]

The following SQL query shows that array_push_front and array_prepend return the same list when you pass their arguments in opposite orders.

SELECT array_push_front([2, 3], 1) = array_prepend(1, [2, 3]) AS same_result

The query returns the following result.

same_result

true

Considerations

If you pass the element first, as in array_push_front(1, [2, 3]), the query fails. Put the list first, or use array_prepend(1, [2, 3]).

list_append function

Returns list with element added as its last element. The type of element must match the element type of list, or both types must convert to a common type. A NULL element is added like any other value.

If list is NULL, it is treated as an empty list, so the result is a list with only element. To add an element at the start of a list, use the list_prepend function. To join two lists, use the list_concat function.

Syntax

list_append(list, element)

Arguments

The list_append function takes the following arguments:

list

The list to add to, as a LIST value.

element

The value to add, of the element type of list.

Return type

A LIST of the common type of the list elements and element, for example INTEGER[].

Examples

The following SQL query adds 4 to the end of a list.

SELECT list_append([2, 3], 4) AS result

The query returns the following result.

result

[2, 3, 4]

The following SQL query adds an element to a NULL list, which is treated as empty, and adds a NULL element to a list.

SELECT list_append(NULL::INTEGER[], 4) AS null_list, list_append([2, 3], NULL) AS null_element

The query returns the following result.

null_list null_element

[4]

[2, 3, NULL]

The following SQL query adds a list as one element to a list of lists.

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

The query returns the following result.

result

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

Considerations

list_append adds one element. Passing a list as element to a list of single values, as in list_append([1, 2], [3, 4]), makes the query fail. To add the elements of a second list, use the list_concat function.

Differences from Athena engine version 3

In Athena engine version 3, array || element adds one element to the end of an array. In DuckDB, || joins two lists, and a list followed by a single value makes the query fail. Use list_append(list, element), or wrap the element in a list, as in list || [element].

list_prepend function

Returns a new list that has element as its first element, followed by the elements of list. Pass the element first and the list second. This is the reverse of the argument order of list_append.

If element is NULL, the result starts with a NULL element. If list is NULL, the function treats it as an empty list, so the result is a list with one element.

Syntax

list_prepend(element, list)

Arguments

The list_prepend function takes the following arguments:

element

The value to add, of a type that can be combined with the element type of list.

list

The list to add the element to.

Return type

A list whose element type is the common type of element and the elements of list, for example INTEGER[].

Examples

The following SQL query adds 1 to the start of a list.

SELECT list_prepend(1, [2, 3]) AS result

The query returns the following result.

result

[1, 2, 3]

The following SQL query adds a NULL element to the start of a list.

SELECT list_prepend(NULL, [2, 3]) AS result

The query returns the following result.

result

[NULL, 2, 3]

The following SQL query passes a NULL list. The function treats it as an empty list and returns a list with one element.

SELECT list_prepend(1, CAST(NULL AS INTEGER[])) AS result

The query returns the following result.

result

[1]

Considerations

If you pass the list first and the element second, as in list_prepend([2, 3], 1), the query fails. To add an element to the end of a list, use list_append(list, element).

list_resize function

Returns a list with exactly size elements. If list has more than size elements, the function keeps the first size elements. If it has fewer, the function adds elements at the end, each set to value, or to NULL when you omit value.

If list is NULL, the result is NULL. If size is NULL, the result is an empty list. array_resize is an alias of list_resize.

Syntax

list_resize(list, size [, value ])

Arguments

The list_resize function takes the following arguments:

list

The list to resize.

size

The number of elements in the result, as a non-negative integer.

value

The value of each added element. It is converted to the element type of list. The default is NULL. This argument is optional.

Return type

A list of the same type as list, for example INTEGER[].

Examples

The following SQL query extends a list to five elements and fills the new elements with 0.

SELECT list_resize([1, 2, 3], 5, 0) AS result

The query returns the following result.

result

[1, 2, 3, 0, 0]

The following SQL query uses the array_resize alias without value, so the new elements are NULL.

SELECT array_resize([1, 2, 3], 5) AS result

The query returns the following result.

result

[1, 2, 3, NULL, NULL]

The following SQL query shortens a list to its first two elements.

SELECT list_resize([1, 2, 3], 2) AS result

The query returns the following result.

result

[1, 2]

The following SQL query passes 0.5 as value for a list of INTEGER values. The value is converted to INTEGER, which rounds it to 1.

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

The query returns the following result.

result

[1, 2, 3, 1]

Considerations

A negative size makes the query fail.

Because value is converted to the element type of list, it can be rounded. For example, list_resize([1, 2, 3], 4, 0.5) returns [1, 2, 3, 1], because 0.5 becomes the INTEGER value 1. To keep the fraction, cast the list first, as in list_resize(CAST([1, 2, 3] AS DOUBLE[]), 4, 0.5).