View a markdown version of this page

List search and set functions - Amazon Athena

List search and set functions

Use these functions to test whether a list contains a value or some or all elements of a second list, and to find the position of a value. You can also remove duplicate values, count distinct values, or find the elements that two lists share.

&& (overlap) operator

Returns true if list1 and list2 share at least one element, and false if they do not. Elements are compared by value, so the order of the elements and the number of times that each one appears do not matter. You can also use the operator with two ARRAY values, and the two values do not need to have the same number of elements.

NULL elements never match, not even a NULL element in the other list. An empty list has no element in common with any list, so the result is false. If either list is NULL, the result is NULL.

list1 && list2 returns the same result as list_has_any(list1, list2).

Syntax

list1 && list2

Arguments

The && (overlap) operator takes the following arguments:

list1

The first list, or an ARRAY value.

list2

The second list, or an ARRAY value. Its elements must have the same type as the elements of list1, or a type that DuckDB can convert to a common type with them.

Return type

BOOLEAN

Examples

The following SQL query returns true because both lists contain 3.

SELECT [1, 2, 3] && [3, 4] AS result

The query returns the following result.

result

true

The following SQL query returns false twice. The first pair of lists has no element in common. In the second pair, b and B differ in case, so they do not match.

SELECT [1, 2] && [3, 4] AS no_shared_element, ['a', 'b'] && ['B', 'C'] AS different_case

The query returns the following result.

no_shared_element different_case

false

false

The following SQL query shows that NULL elements do not match. Both results are false, even though both lists in the second pair contain NULL.

SELECT [1, NULL] && [2] AS no_shared_value, [1, NULL] && [NULL, 3] AS only_null_shared

The query returns the following result.

no_shared_value only_null_shared

false

false

The following SQL query uses && in a WHERE clause to return the rows whose colors list contains blue or black.

SELECT id FROM (VALUES (1, ['red', 'blue']), (2, ['green']), (3, ['blue', 'white']) ) AS t(id, colors) WHERE colors && ['blue', 'black'] ORDER BY id

The query returns the following results.

id

1

3

Considerations

If one operand is a list or an ARRAY value, the other operand must be one too, so [1, 2] && 1 fails. To test whether a list contains one value, use the list_contains function.

If the element types cannot be converted to a common type, as in [1, 2] && ['a'], the query fails.

String elements match only when they have the same case, so ['a', 'b'] && ['B', 'C'] returns false.

Differences from Athena engine version 3

Athena engine version 3 uses the arrays_overlap function for this test, and DuckDB has no arrays_overlap function. Replace arrays_overlap(array1, array2) with array1 && array2 or list_has_any(array1, array2).

In Athena engine version 3, arrays_overlap returns NULL when the two arrays have no element in common other than NULL and either array contains a NULL element. In DuckDB, && returns false in that case, so [1, NULL] && [2] returns false.

list_contains function

Returns true if at least one element of list equals element, and false if none does. Elements are compared with =, so string comparisons are case-sensitive. A NULL element never matches, so list_contains([1, NULL], 3) returns false, not NULL.

If list or element is NULL, the result is NULL. For this reason, you cannot use list_contains to find a NULL element. To test whether a list holds a NULL element, use list_count(list) < len(list), which returns true when at least one element is NULL.

array_contains, array_has, and list_has are aliases of list_contains.

Syntax

list_contains(list, element)

Arguments

The list_contains function takes the following arguments:

list

The list to search.

element

The value to look for. Its type must be comparable with the element type of list.

Return type

BOOLEAN

Examples

The following SQL query finds the value 1 in a list that also holds a NULL element.

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

The query returns the following result.

result

true

The following SQL query shows the two NULL cases. A search with no match returns false even though the list holds a NULL element, and a search for NULL returns NULL.

SELECT list_contains([1, NULL], 3) AS no_match, list_contains([1, NULL], NULL) AS search_for_null

The query returns the following result.

no_match search_for_null

false

NULL

The following SQL query returns the rows whose list of tags includes blue.

SELECT order_id FROM (VALUES (1, ['red', 'blue']), (2, ['green']), (3, ['blue']) ) AS t(order_id, tags) WHERE list_contains(tags, 'blue') ORDER BY order_id

The query returns the following results.

order_id

1

3

The following SQL query uses the array_has alias. The search is case-sensitive, so B does not match b.

SELECT array_has(['a', 'b'], 'B') AS result

The query returns the following result.

result

false

Differences from Athena engine version 3

In Athena engine version 3, contains(array, element) returns NULL when no element matches and the array holds a NULL element. In DuckDB, contains and list_contains return false in that case.

list_distinct function

Returns a list with one copy of each distinct non-NULL value in list. NULL elements are removed, so a list that holds only NULL elements returns an empty list. The order of the elements in the result is not defined and can differ from their order in list. If list is NULL, the result is NULL.

To get a predictable order, sort the result, as in list_sort(list_distinct(list)). To count the distinct values instead, use the list_unique function. array_distinct is an alias of list_distinct.

Syntax

list_distinct(list)

Arguments

The list_distinct function takes the following argument:

list

The list whose distinct values you want, of any element type.

Return type

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

Examples

The following SQL query removes the duplicate values and the NULL element from a list, and sorts the result.

SELECT list_sort( list_distinct([1, 1, NULL, -3, 1, 5]) ) AS result

The query returns the following result.

result

[-3, 1, 5]

The following SQL query shows that a list that holds only NULL elements returns an empty list.

SELECT list_distinct( CAST([NULL, NULL] AS INTEGER[]) ) AS result

The query returns the following result.

result

[]

The following SQL query uses the array_distinct alias and counts the result. Strings are compared case-sensitively, so b and B are different values.

SELECT len(array_distinct(['b', 'a', 'b', 'B'])) AS result

The query returns the following result.

result

3

Differences from Athena engine version 3

In Athena engine version 3, array_distinct keeps one NULL element and keeps the elements in the order in which they first appear. In DuckDB, array_distinct and list_distinct remove NULL elements and do not keep the original order.

list_has_all function

Returns true if every element of list2 is also an element of list1, and false otherwise. The order of the elements and the number of times that each one occurs do not matter. NULL elements in either list are ignored, so if list2 is empty or holds only NULL elements, the result is true. If either list is NULL, the result is NULL.

You can also write the test with the @> (contains) and <@ (contained by) operators. The expression list1 @> list2 returns true when the list on the left contains every element of the list on the right. The expression list2 <@ list1 returns true when every element of the list on the left is in the list on the right. Both forms return the same result as list_has_all(list1, list2).

array_has_all is an alias of list_has_all. To test whether two lists share at least one element, use the list_has_any function.

Syntax

list_has_all(list1, list2) list1 @> list2 list2 <@ list1

Arguments

The list_has_all function takes the following arguments:

list1

The list to search, with elements of any type.

list2

The list of elements to look for. Its element type must be compatible with the element type of list1.

Return type

BOOLEAN

Examples

The following SQL query tests whether the first list contains every element of the second list.

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

The query returns the following result.

result

true

The following SQL query returns false when an element is missing, and shows that a NULL element in the second list is ignored.

SELECT list_has_all([1, 2, 3], [2, 4]) AS missing_element, list_has_all([1, 2], [1, NULL]) AS null_ignored

The query returns the following result.

missing_element null_ignored

false

true

The following SQL query uses the @> operator. The list on the left must contain every element of the list on the right, so swapping the lists changes the result.

SELECT [1, 2, 3] @> [1, 2] AS result, [1, 2] @> [1, 2, 3] AS reversed

The query returns the following result.

result reversed

true

false

The following SQL query uses the <@ operator. Every element of the list on the left must be in the list on the right.

SELECT [1, 2] <@ [1, 2, 3] AS result, [1, 2, 3] <@ [1, 2] AS reversed

The query returns the following result.

result reversed

true

false

Considerations

The two lists must have element types that can be compared. For example, list_has_all([1, 2], ['1']) fails, because an INTEGER list and a VARCHAR list have no common type. Cast one of the lists to the type of the other first.

list_has_any function

Returns true if at least one element of list1 is also an element of list2, and false otherwise. NULL elements are ignored, so two lists that share only NULL elements return false. An empty list has no element in common with any list. If either list is NULL, the result is NULL.

array_has_any is an alias of list_has_any. The && (overlap) operator runs the same test on two lists. To test whether one list contains every element of another, use the list_has_all function.

Syntax

list_has_any(list1, list2)

Arguments

The list_has_any function takes the following arguments:

list1

The first list to compare, with elements of any type.

list2

The second list to compare, whose element type is compatible with the element type of list1.

Return type

BOOLEAN

Examples

The following SQL query tests whether two lists share an element.

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

The query returns the following result.

result

true

The following SQL query returns false because the lists have no element in common.

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

The query returns the following result.

result

false

The following SQL query shows that NULL elements do not count as a match. The only element that the two lists share is NULL, so the result is false.

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

The query returns the following result.

result

false

Considerations

The two lists must have element types that can be compared. For example, list_has_any([1, 2], ['1']) fails, because an INTEGER list and a VARCHAR list have no common type. Cast one of the lists to the type of the other first.

Differences from Athena engine version 3

Athena engine version 3 uses arrays_overlap for this test. When the arrays share no element other than NULL, and either array has a NULL element, arrays_overlap returns NULL. In the same case, list_has_any returns false. DuckDB has no arrays_overlap function.

list_intersect function

Returns a list of the elements that occur in both list1 and list2. Each element appears once in the result, even when it occurs more than once in either list. NULL elements are ignored, so a NULL that is in both lists is not in the result. If the lists have no element in common, the result is an empty list.

array_intersect is an alias of list_intersect.

Syntax

list_intersect(list1, list2)

Arguments

The list_intersect function takes the following arguments:

list1

The first list, with elements of any type.

list2

The second list, whose element type is compatible with the element type of list1.

Return type

A list of the common element type of the two lists, for example INTEGER[] for two INTEGER[] lists.

Examples

The following SQL query returns the elements that two lists share. list_sort puts them in ascending order, because the order of the elements that list_intersect returns is not defined.

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

The query returns the following result.

result

[2, 3]

The following SQL query shows that each shared element appears once, even when it repeats. list_sort puts the result in a predictable order.

SELECT list_sort( list_intersect([5, 1, 5, 3, 1], [1, 5, 9]) ) AS result

The query returns the following result.

result

[1, 5]

The following SQL query shows that a NULL element in both lists is not part of the result.

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

The query returns the following result.

result

[2]

The following SQL query shows the different results for a NULL first list and a NULL second list. When the first list is NULL, the result is NULL, so IS NULL returns true. When only the second list is NULL, the result is an empty list, so len returns 0.

SELECT list_intersect( NULL, [1, 2] ) IS NULL AS first_is_null, len( list_intersect([1, 2], NULL) ) AS second_null_count

The query returns the following result.

first_is_null second_null_count

true

0

Considerations

The order of the elements in the result is not defined, and it can differ from the order in either input list. To get a predictable order, wrap the call in list_sort.

The two list arguments do not handle NULL the same way. If list1 is NULL, the result is NULL. If only list2 is NULL, the result is an empty list.

Differences from Athena engine version 3

In Athena engine version 3, array_intersect keeps a NULL element that is in both arrays. In DuckDB, list_intersect and its alias array_intersect leave NULL out of the result.

list_position function

Searches list for element and returns the 1-based position of the first element that equals it. If list does not contain element, the result is NULL.

A NULL value of element matches a NULL element in the list, so list_position([1, NULL, 3], NULL) returns 2. If list is NULL, the result is NULL.

array_indexof, array_position, and list_indexof are aliases of list_position. To test only whether a list contains an element, use the list_contains function.

Syntax

list_position(list, element)

Arguments

The list_position function takes the following arguments:

list

The list to search, of any element type.

element

The value to find, of a type that can be compared with the elements of list.

Return type

INTEGER

Examples

The following SQL query returns the position of the value 2 in a list.

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

The query returns the following result.

result

2

The following SQL query uses the list_indexof alias. The value 20 appears twice, and the function returns the position of the first one.

SELECT list_indexof([10, 20, 30, 20], 20) AS result

The query returns the following result.

result

2

The following SQL query uses the array_position alias to search for a value that the list does not contain, so the result is NULL.

SELECT array_position([5, 6, 7], 9) AS result

The query returns the following result.

result

NULL

The following SQL query searches for NULL, which matches the NULL element at position 2.

SELECT list_position([1, NULL, 3], NULL) AS result

The query returns the following result.

result

2

Considerations

String comparisons are case-sensitive. For example, list_position(['a', 'b'], 'B') returns NULL.

Differences from Athena engine version 3

In Athena engine version 3, array_position returns 0 when the array does not contain the element. In DuckDB, array_position is an alias of list_position and returns NULL. To get the Athena engine version 3 result, use coalesce(list_position(list, element), 0).

In Athena engine version 3, array_position returns NULL when the element to find is NULL. In DuckDB, list_position returns the position of the first NULL element in the list.

list_unique function

Returns how many different values list holds. A value that appears more than once is counted once, and NULL elements are not counted. For an empty list, or a list of only NULL elements, the result is 0. If list is NULL, the result is NULL.

The result equals len(list_distinct(list)). Strings are compared with case, so 'a' and 'A' count as two values. array_unique is an alias of list_unique.

Syntax

list_unique(list)

Arguments

The list_unique function takes the following argument:

list

The list whose distinct elements you want to count, as a LIST value.

Return type

UBIGINT

Examples

The following SQL query counts the distinct values in a list. The value 1 appears three times and is counted once, and the NULL element is not counted.

SELECT list_unique([1, 1, NULL, -3, 1, 5]) AS result

The query returns the following result.

result

3

The following SQL query shows that strings that differ only in case are counted as different values.

SELECT list_unique(['a', 'A', 'a']) AS result

The query returns the following result.

result

2

The following SQL query counts the distinct values in an empty list and in a list of only NULL elements. Both results are 0.

SELECT list_unique([]) AS empty_list, list_unique( [CAST(NULL AS INTEGER), NULL] ) AS only_nulls

The query returns the following result.

empty_list only_nulls

0

0

Considerations

Hive and Iceberg tables cannot store UBIGINT values. To write the result to a table, cast it, as in CAST(list_unique(list) AS BIGINT).

Differences from Athena engine version 3

In Athena engine version 3, you count the distinct elements of an array with cardinality(array_distinct(array)), which counts NULL as one distinct value. The list_unique function does not count NULL, so for a list that contains NULL, its result is 1 lower.