View a markdown version of this page

List length and element access functions - Amazon Athena

List length and element access functions

Use these functions to count the elements of a list, and to read one element, a slice, or the elements at the positions that you choose. Positions in a list start at 1. The cardinality function also counts the elements of a list.

array_extract function

Returns the element of value at position index when value is a list. Positions start at 1, and a negative index counts back from the end, so -1 is the last element. When index is out of range, the result is NULL. If value or index is NULL, the result is NULL. To read a list element, you can also use the list_extract function or a subscript, as in list[index].

For a struct with named fields, pass the field name as entry to get that field. For an unnamed struct, such as one built with row, pass the field position as index.

array_extract also accepts a VARCHAR string as value, and then returns the character at position index as a one-character VARCHAR. Positions in a string follow the same rules as in a list, but when index is out of range, the string form returns an empty string, not NULL.

Syntax

array_extract(value, index) array_extract(struct, entry)

Arguments

The array_extract function takes the following arguments:

value

The list, the unnamed STRUCT, or the VARCHAR string to read from.

index

The position to read, as a BIGINT value, starting at 1.

struct

The struct to read a field from, as a STRUCT value with named fields.

entry

The name of the field to return, as a VARCHAR value.

Return type

The type of the selected item: the element type for a list (for example, INTEGER for an INTEGER[] list), the field type for a struct, or VARCHAR for a string.

Examples

The following SQL query returns a list element, and shows that a position past the end of a list returns NULL.

SELECT array_extract([10, 20, 30], 2) AS second_element, array_extract([10, 20, 30], 5) AS out_of_range

The query returns the following result.

second_element out_of_range

20

NULL

The following SQL query returns a field of a struct by name.

SELECT array_extract( {'id': 1, 'note': 'shipped'}, 'note' ) AS result

The query returns the following result.

result

shipped

The following SQL query returns the second character of a string.

SELECT array_extract('DuckDB', 2) AS result

The query returns the following result.

result

u

The following SQL query returns the last character of a string, and shows that a position past the end returns an empty string.

SELECT array_extract('DuckDB', -1) AS last_char, array_extract( 'DuckDB', 10 ) = '' AS out_of_range_is_empty

The query returns the following result.

last_char out_of_range_is_empty

B

true

Considerations

For a struct with named fields, a field name that the struct does not have causes an error. It does not return NULL.

For an unnamed struct, index must be from 1 to the number of fields. Negative positions cause an error.

array_length function

Returns the number of elements in list. NULL elements are counted. For an empty list, the result is 0, and if list is NULL, the result is NULL.

For a list of lists, the result is the number of inner lists, not the total number of elements. With dimension, you can read the size of one level of a nested ARRAY value.

For a list, array_length returns the same result as len.

Syntax

array_length(list [, dimension ])

Arguments

The array_length function takes the following arguments:

list

The list or ARRAY value to measure.

dimension

The level to measure, as a BIGINT value, where 1 is the outermost level. For a nested ARRAY type such as INTEGER[3][2], which holds two arrays of three INTEGER values each, 1 returns 2 and 2 returns 3. The default is 1. This argument is optional.

Return type

BIGINT

Examples

The following SQL query returns the number of elements in a list.

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

The query returns the following result.

result

3

The following SQL query counts the elements of a list that has a NULL element, of an empty list, and of a list of two lists.

SELECT array_length([1, NULL, 3]) AS with_null, array_length(CAST([] AS INTEGER[])) AS empty_list, array_length([[1, 2], [3]]) AS nested_list

The query returns the following result.

with_null empty_list nested_list

3

0

2

The following SQL query measures both levels of an INTEGER[3][2] value, an ARRAY of two ARRAY values of three elements each.

SELECT array_length( [[1, 2, 3], [4, 5, 6]]::INTEGER[3][2], 1 ) AS outer_size, array_length( [[1, 2, 3], [4, 5, 6]]::INTEGER[3][2], 2 ) AS inner_size

The query returns the following result.

outer_size inner_size

2

3

Considerations

For a list, 1 is the only dimension that you can pass. Any other value makes the query fail, even for a list of lists. For a nested ARRAY, a dimension greater than the number of levels makes the query fail.

array_length does not accept a string. To get the number of characters in a string, use the length function.

length function

Returns the length of value. For a VARCHAR value, this is the number of characters (Unicode code points), not bytes. For a LIST or ARRAY, it is the number of top-level elements, including NULL elements. For a BIT value, it is the number of bits. If value is NULL, the result is NULL.

To count the bytes in a string, use the strlen function. To count the bytes in a BLOB, use octet_length. To count grapheme clusters, the characters as a reader sees them, use the length_grapheme function.

char_length, character_length, and len are aliases of length.

Syntax

length(value)

Arguments

The length function takes the following argument:

value

The value to measure, as a VARCHAR, LIST, ARRAY, or BIT value.

Return type

BIGINT

Examples

The following SQL query returns the number of elements in a list.

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

The query returns the following result.

result

3

The following SQL query returns the number of characters in a string, and uses the len alias to count the elements of a list. The NULL element is counted.

SELECT length('Hello') AS string_length, len([1, NULL, 3]) AS list_length

The query returns the following result.

string_length list_length

5

3

The following SQL query returns the number of bits in a bitstring. Casting the TINYINT value 42 to BIT produces 8 bits.

SELECT length(42::TINYINT::BIT) AS result

The query returns the following result.

result

8

The following SQL query shows that an empty string and an empty list have a length of 0, and that a NULL input returns NULL.

SELECT length('') AS empty_string, length([]) AS empty_list, length(NULL::VARCHAR) AS null_input

The query returns the following result.

empty_string empty_list null_input

0

0

NULL

Considerations

length does not accept BLOB or numeric values, and such a call fails. Use octet_length for a BLOB, and cast a number to VARCHAR to count its characters.

Differences from Athena engine version 3

To count the elements of an array, Athena engine version 3 queries use cardinality(array). In DuckDB, use len(list) or length(list).

In Athena engine version 3, length also accepts a VARBINARY value and returns its number of bytes. In DuckDB, length does not accept a BLOB value. Use octet_length instead.

list_any_value function

Applies the any_value aggregate function to the elements of list, which returns the first element that is not NULL. It is the same as list_aggregate(list, 'any_value').

If list is empty, is NULL, or has only NULL elements, the result is NULL.

Syntax

list_any_value(list)

Arguments

The list_any_value function takes the following argument:

list

The list to return an element from, as a LIST or ARRAY value of any element type.

Return type

The element type of list, for example INTEGER for an INTEGER[] list.

Examples

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

SELECT list_any_value([3, 3, 9]) AS result

The query returns the following result.

result

3

The following SQL query skips a leading NULL element and returns the first element that is not NULL.

SELECT list_any_value([NULL, 7, 9]) AS result

The query returns the following result.

result

7

The following SQL query passes a list whose elements are all NULL, so the result is NULL.

SELECT list_any_value([NULL, NULL]::INTEGER[]) AS result

The query returns the following result.

result

NULL

list_count function

Counts the elements of list that are not NULL. Duplicate values each count. If list is empty, the result is 0. If list is NULL, the result is NULL.

To count every element, including NULL elements, use len. list_count(list) returns the same result as list_aggregate(list, 'count').

Syntax

list_count(list)

Arguments

The list_count function takes the following argument:

list

The list whose elements you want to count, of any element type.

Return type

BIGINT

Examples

The following SQL query counts the elements of a list. Both copies of 3 are counted.

SELECT list_count([3, 3, 9]) AS result

The query returns the following result.

result

3

The following SQL query compares list_count and len on a list that holds a NULL element.

SELECT list_count([1, NULL, 3]) AS non_null_elements, len([1, NULL, 3]) AS all_elements

The query returns the following result.

non_null_elements all_elements

2

3

The following SQL query shows that an empty list returns 0.

SELECT list_count(CAST([] AS INTEGER[])) AS result

The query returns the following result.

result

0

Considerations

list_count does not count the occurrences of one value. To count the elements that equal a value, filter the list first, as in len(list_filter(list, lambda x: x = value)).

list_extract function

Returns the element of list at position index. Positions start at 1, and a negative index counts from the end, so -1 returns the last element. If index is 0 or points past either end of the list, the result is NULL. If list or index is NULL, the result is NULL.

When the first argument is a string, list_extract returns the character at position index as a VARCHAR value. For a position outside the string, the result is an empty string, not NULL.

list_element is an alias of list_extract.

Syntax

list_extract(list, index) list_extract(string, index)

Arguments

The list_extract function takes the following arguments:

list

The list to read an element from.

string

The string to read a character from, as a VARCHAR value.

index

The position of the element or character to return, as a BIGINT value. 1 is the first element, and -1 is the last.

Return type

The element type of list, such as INTEGER for an INTEGER[] list. For a string, the result is a VARCHAR value.

Examples

The following SQL query returns the third element of a list.

SELECT list_extract([4, 5, 6], 3) AS result

The query returns the following result.

result

6

The following SQL query uses a negative index to return the last element, and shows that index 0 and an index past the end return NULL.

SELECT list_extract([4, 5, 6], -1) AS last_element, list_extract([4, 5, 6], 0) AS index_zero, list_extract([4, 5, 6], 7) AS past_end

The query returns the following result.

last_element index_zero past_end

6

NULL

NULL

The following SQL query uses the list_element alias to return the second character of a string. It also shows that a position past the end of a string returns an empty string.

SELECT list_element('DuckDB', 2) AS second_character, list_extract('DuckDB', 10) = '' AS past_end_is_empty

The query returns the following result.

second_character past_end_is_empty

u

true

Considerations

index must be an integer. A value such as 1.7 causes an error and is not rounded.

Differences from Athena engine version 3

In Athena engine version 3, you get an array element with element_at(array, index). In DuckDB, element_at accepts only a map, so that call fails on an array. Use list_extract(array, index) or array[index] instead.

list_first function

Returns the first element of list. The function does not skip NULL elements, so if the first element is NULL, the result is NULL. If list is empty or NULL, the result is NULL.

list_first(list) returns the same result as list_aggregate(list, 'first'). To get the first element that is not NULL, use the list_any_value function. To get the last element, use the list_last function.

Syntax

list_first(list)

Arguments

The list_first function takes the following argument:

list

The list to read the first element from, with elements of any type.

Return type

The element type of list, for example INTEGER for an INTEGER[] list.

Examples

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

SELECT list_first([3, 3, 9]) AS result

The query returns the following result.

result

3

The following SQL query shows that list_first returns NULL when the first element is NULL. list_any_value skips the NULL element and returns the first element that is not NULL.

SELECT list_first([NULL, 1, 2]) AS first_element, list_any_value([NULL, 1, 2]) AS first_non_null

The query returns the following result.

first_element first_non_null

NULL

1

list_last function

Returns the last element of list. The function does not skip NULL elements, so if the last element is NULL, the result is NULL. If list is empty or NULL, the result is NULL.

list_last(list) returns the same result as list_aggregate(list, 'last'). To get the first element, use the list_first function.

Syntax

list_last(list)

Arguments

The list_last function takes the following argument:

list

The list to read the last element from, with elements of any type.

Return type

The element type of list, for example INTEGER for an INTEGER[] list.

Examples

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

SELECT list_last([3, 3, 9]) AS result

The query returns the following result.

result

9

The following SQL query returns NULL because the last element is NULL.

SELECT list_last([1, 2, NULL]) AS result

The query returns the following result.

result

NULL

The following SQL query returns the last element that is not NULL. The list_filter function first removes the NULL elements: the lambda function lambda x: x IS NOT NULL keeps each element x that is not NULL.

SELECT list_last( list_filter( [1, 2, NULL], lambda x: x IS NOT NULL ) ) AS result

The query returns the following result.

result

2

list_select function

Returns a list with one element for each position in index_list: the element of value_list at that 1-based position. Positions can repeat and can appear in any order, so you can use list_select to pick, reorder, or repeat elements.

A position that is 0, negative, or greater than the length of value_list gives a NULL element in the result. Negative positions do not count from the end of the list. If either list is NULL, the result is NULL.

array_select is an alias of list_select. To keep the elements that match a list of BOOLEAN values instead, use the list_where function.

Syntax

list_select(value_list, index_list)

Arguments

The list_select function takes the following arguments:

value_list

The list to take elements from, of any element type.

index_list

The positions of the elements to return, as a BIGINT[] list of integers. Lists of other integer types are converted. The list cannot contain NULL elements.

Return type

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

Examples

The following SQL query returns the first and fourth elements of a list.

SELECT list_select([10, 20, 30, 40], [1, 4]) AS result

The query returns the following result.

result

[10, 40]

The following SQL query reorders the elements and repeats one of them.

SELECT list_select(['a', 'b', 'c'], [3, 1, 1]) AS result

The query returns the following result.

result

[c, a, a]

The following SQL query uses the array_select alias with a position past the end of the list, which gives a NULL element.

SELECT array_select([10, 20, 30, 40], [2, 5]) AS result

The query returns the following result.

result

[20, NULL]

The following SQL query sorts one list by the values of another. list_grade_up([2, 3, 1]) returns [3, 1, 2], the positions that put [2, 3, 1] in ascending order.

SELECT list_select( ['b', 'c', 'a'], list_grade_up([2, 3, 1]) ) AS result

The query returns the following result.

result

[a, b, c]

Considerations

If index_list contains a NULL element, the query fails.

list_slice function

Returns the elements of list from position begin through position end. Positions are 1-based, and the result includes both begin and end. A negative position counts from the end of the list, so -1 is the last element. With step, the function returns every step-th element, starting at begin. A negative step moves backward from begin to end.

Positions outside the list are limited to the list, so the result can be shorter than you ask for, or empty. A begin of 0 works like 1. If begin or end is NULL, the result is NULL.

You can also pass a VARCHAR value to get a substring. The string form does not take step. array_slice is an alias of list_slice.

Syntax

list_slice(list, begin, end [, step ]) list_slice(string, begin, end)

Arguments

The list_slice function takes the following arguments:

list

The list to take elements from, of any element type.

string

The string to take characters from, as a VARCHAR value.

begin

The position of the first element or character to return, as a BIGINT value. A negative value counts from the end.

end

The position of the last element or character to return, as a BIGINT value. A negative value counts from the end. To slice to the end, pass -1.

step

The distance between the positions of the returned elements, as a BIGINT other than 0. The default is 1. You can pass it only for lists. This argument is optional.

Return type

The type of the first argument: a list of the same type as list, such as INTEGER[], or VARCHAR for a string.

Examples

The following SQL query returns the elements from position 2 through position 3.

SELECT list_slice([4, 5, 6], 2, 3) AS result

The query returns the following result.

result

[5, 6]

The following SQL query returns every second element from position 1 through position 3.

SELECT list_slice([4, 5, 6], 1, 3, 2) AS result

The query returns the following result.

result

[4, 6]

The following SQL query uses negative positions to return the last two elements.

SELECT list_slice([1, 2, 3, 4, 5], -2, -1) AS result

The query returns the following result.

result

[4, 5]

The following SQL query uses the array_slice alias to return the third and fourth characters of a string.

SELECT array_slice('DuckDB', 3, 4) AS result

The query returns the following result.

result

ck

Considerations

If begin or end is NULL, the result is NULL, not the rest of the list. To slice to the end, pass -1 as end.

A step of 0 makes the query fail, and so does a step with a string.

Differences from Athena engine version 3

In Athena engine version 3, the third argument of slice(array, start, length) is the number of elements to return. DuckDB has no slice function. In DuckDB, the third argument of list_slice is the position of the last element to return. To rewrite slice(array, start, length), use list_slice(array, start, start + length - 1). When start is negative and the slice reaches the end of the array, use -1 as the end position instead.

list_where function

Compares value_list and mask_list position by position, and returns, in order, the elements of value_list whose matching element of mask_list is true. Elements whose matching mask element is false are left out.

If mask_list is shorter than value_list, the elements of value_list past the end of the mask are left out. If mask_list is longer and is true at a position past the end of value_list, the result has a NULL element for that position. If either list is NULL, the result is NULL.

array_where is an alias of list_where. To keep the elements that meet a condition, use the list_filter function, or build the mask with list_transform.

Syntax

list_where(value_list, mask_list)

Arguments

The list_where function takes the following arguments:

value_list

The list to take elements from, as a LIST value.

mask_list

The mask, as a LIST of BOOLEAN values. Element n of mask_list decides whether element n of value_list is in the result.

Return type

A LIST of the same type as value_list, such as INTEGER[].

Examples

The following SQL query keeps the first and the last element of a list.

SELECT list_where( [10, 20, 30, 40], [true, false, false, true] ) AS result

The query returns the following result.

result

[10, 40]

The following SQL query builds the mask from a second list with list_transform, and keeps the letters whose matching number is greater than 3.

SELECT list_where( ['a', 'b', 'c'], list_transform([1, 5, 9], lambda x: x > 3) ) AS result

The query returns the following result.

result

[b, c]

The following SQL query uses a mask that is shorter than the list and a mask that is longer than the list. The shorter mask leaves out the last two elements, and the longer mask adds a NULL element.

SELECT list_where([10, 20, 30], [true]) AS short_mask, list_where( [10, 20], [true, true, true] ) AS long_mask

The query returns the following result.

short_mask long_mask

[10]

[10, 20, NULL]

The following SQL query replaces the NULL element of a mask with false before it calls list_where.

SELECT list_where( [10, 20, 30], list_transform( [true, NULL, true], lambda b: coalesce(b, false) ) ) AS result

The query returns the following result.

result

[10, 30]

Considerations

If mask_list contains a NULL element, the query fails. To treat NULL as false, replace it first, as in list_where(value_list, list_transform(mask_list, lambda b: coalesce(b, false))).

mask_list must be a list of BOOLEAN values. If you pass a list of the numbers 1 and 0, the query fails.