View a markdown version of this page

List and array functions and operators in DuckDB - Amazon Athena

List and array functions and operators in DuckDB

Use list and array functions and operators to build, change, search, and measure lists and fixed-size arrays, and to compute the distance or the similarity between two vectors of numbers.

DuckDB has two types for an ordered sequence of values that all have the same type. A LIST can hold any number of elements, so two values of the same list type can have different lengths. You write a list as a literal in square brackets, such as [1, 2, 3]. You write its type as the element type followed by empty brackets, such as INTEGER[].

An ARRAY holds a fixed number of elements. That number is part of its type, such as INTEGER[2] for exactly two INTEGER elements. To create an ARRAY, cast a list to an array type, as in [1, 2]::INTEGER[2] or CAST([1, 2] AS INTEGER[2]). The cast fails if the list does not have exactly that number of elements. You can also use the array_value function.

Query results show a LIST and an ARRAY the same way, for example [1, 2], and they show string elements without quotation marks. To see which type a value has, use the typeof function.

Most functions in this category take lists, including many functions whose names start with array_, such as array_append and array_reverse. When you pass an ARRAY to one of these functions, the result is a LIST.

The vector functions array_cosine_similarity, array_cosine_distance, array_distance, array_inner_product, and array_negative_inner_product work the other way. They need ARRAY arguments with FLOAT or DOUBLE elements. To compute the same values for lists, use list_cosine_similarity, list_cosine_distance, list_distance, list_inner_product, and list_negative_inner_product. The exception is array_cross_product, which takes ARRAY values of size 3 but also accepts a list of exactly three numbers and converts it for you.

Positions in a list start at 1, so [10, 20, 30][1] returns 10.

Several functions in this category, such as list_transform, list_filter, list_reduce, and invoke, take a lambda function as an argument. A lambda function is an expression with named parameters. Each of these functions calls its lambda function, for example once for each element of a list. Write it with the lambda keyword, the parameter names separated by commas, a colon, and the expression, as in lambda x: x + 1. For two parameters, write lambda x, y: x + y. DuckDB does not accept the arrow form, such as x -> x + 1, and the query fails. The page for each of these functions says what the parameters of its lambda function hold. For more information about lambda functions, see Lambda functions.

Apache Iceberg tables cannot store ARRAY values. To store one, cast it to a list first, as in CAST(vector AS DOUBLE[]).

The following table lists the list and array functions and operators, with a link to the description of each one.

Function or operator Description

&&

Tests whether two lists have at least one element in common.

<->

Alias of list_distance.

<=>

Alias of list_cosine_distance.

<@

Alias of list_has_all.

@>

Alias of list_has_all.

aggregate

Alias of list_aggregate.

apply

Alias of list_transform.

array_aggr

Alias of list_aggregate.

array_aggregate

Alias of list_aggregate.

array_append

Adds an element to the end of a list.

array_apply

Alias of list_transform.

array_cat

Alias of list_concat.

array_concat

Alias of list_concat.

array_contains

Alias of list_contains.

array_cosine_distance

Returns the cosine distance between two fixed-size arrays of numbers, which is 1 minus their cosine similarity.

array_cosine_similarity

Returns the cosine similarity of two fixed-size arrays of numbers.

array_cross_product

Returns the cross product of two arrays of three numbers.

array_distance

Returns the Euclidean distance between two fixed-size arrays of numbers.

array_distinct

Alias of list_distinct.

array_dot_product

Alias of array_inner_product.

array_extract

Returns one element of a list, one field of a struct, or one character of a string.

array_filter

Alias of list_filter.

array_grade_up

Alias of list_grade_up.

array_has

Alias of list_contains.

array_has_all

Alias of list_has_all.

array_has_any

Alias of list_has_any.

array_indexof

Alias of list_position.

array_inner_product

Returns the inner product, also called the dot product, of two fixed-size arrays of numbers.

array_intersect

Alias of list_intersect.

array_length

Returns the number of elements in a list or an array.

array_negative_dot_product

Alias of array_negative_inner_product.

array_negative_inner_product

Returns the negative of the inner product of two fixed-size arrays of numbers.

array_pop_back

Returns a list without its last element.

array_pop_front

Returns a list without its first element.

array_position

Alias of list_position.

array_prepend

Adds an element to the start of a list, with the element as the first argument.

array_push_back

Adds an element to the end of a list, like array_append.

array_push_front

Adds an element to the start of a list, with the list as the first argument.

array_reduce

Alias of list_reduce.

array_resize

Alias of list_resize.

array_reverse

Reverses the order of the elements in a list.

array_reverse_sort

Alias of list_reverse_sort.

array_select

Alias of list_select.

array_slice

Alias of list_slice.

array_sort

Alias of list_sort.

array_to_json

Converts a list or array to a JSON array.

array_to_string

Joins the elements of a list into one string, with a separator between them.

array_transform

Alias of list_transform.

array_unique

Alias of list_unique.

array_value

Creates a fixed-size ARRAY from its arguments.

array_where

Alias of list_where.

array_zip

Alias of list_zip.

cardinality

Returns the number of elements in a list. It also counts the entries of a map. The struct, map, and union functions topic describes it.

char_length

Alias of length.

character_length

Alias of length.

filter

Alias of list_filter.

flatten

Combines the inner lists of a list of lists into one list.

generate_series

Returns a list, or a set of rows, of the values from a start value through a stop value.

generate_subscripts

Returns the positions of the elements of a list, one row for each position.

grade_up

Alias of list_grade_up.

invoke

Calls a lambda function with the arguments that you pass.

len

Alias of length.

length

Returns the number of characters in a string, elements in a list, or bits in a bitstring.

list_aggr

Alias of list_aggregate.

list_aggregate

Runs an aggregate function, chosen by name, over the elements of a list.

list_any_value

Returns the first element of a list that is not NULL.

list_append

Adds an element to the end of a list.

list_apply

Alias of list_transform.

list_approx_count_distinct

Returns an approximate count of the distinct elements of a list that are not NULL.

list_avg

Returns the average of the elements of a list.

list_bit_and

Returns the bitwise AND of the non-NULL elements of a list of integers.

list_bit_or

Returns the bitwise OR of the non-NULL elements of a list of integers.

list_bit_xor

Returns the bitwise exclusive OR (XOR) of the non-NULL elements of a list of integers.

list_bool_and

Tests whether every non-NULL element of a list of Boolean values is true.

list_bool_or

Tests whether at least one non-NULL element of a list of Boolean values is true.

list_cat

Alias of list_concat.

list_concat

Concatenates two or more lists into one list.

list_contains

Tests whether a list contains a given value.

list_cosine_distance

Returns 1 minus the cosine similarity of two equal-length lists of numbers, which measures how different their directions are.

list_cosine_similarity

Returns the cosine of the angle between two equal-length lists of numbers, treated as vectors.

list_count

Returns the number of non-NULL elements in a list.

list_distance

Returns the straight-line (Euclidean) distance between two equal-length lists of numbers that represent points.

list_distinct

Returns the distinct non-NULL values of a list, in no defined order.

list_dot_product

Alias of list_inner_product.

list_element

Alias of list_extract.

list_entropy

Returns the Shannon entropy, in bits, of the values in a list.

list_extract

Returns the element at a given position in a list, or the character at a given position in a string.

list_filter

Keeps the list elements that meet a condition written as a lambda function.

list_first

Returns the first element of a list, even when that element is NULL.

list_grade_up

Returns the positions of the elements of a list, in the order that sorts the elements.

list_has

Alias of list_contains.

list_has_all

Tests whether a list contains every element of a second list.

list_has_any

Tests whether two lists have at least one element in common.

list_histogram

Counts how often each element occurs in a list and returns the counts as a map.

list_indexof

Alias of list_position.

list_inner_product

Returns the inner product (dot product) of two lists of numbers that have the same length.

list_intersect

Returns the distinct elements that two lists have in common.

list_kurtosis

Returns the sample excess kurtosis of the numbers in a list.

list_kurtosis_pop

Returns the population excess kurtosis of the numbers in a list.

list_last

Returns the last element of a list, even when that element is NULL.

list_mad

Returns the median absolute deviation of the elements of a list.

list_max

Returns the largest element of a list.

list_median

Returns the middle value of the elements of a list.

list_min

Returns the smallest element of a list.

list_mode

Returns the most frequent element of a list.

list_negative_dot_product

Alias of list_negative_inner_product.

list_negative_inner_product

Returns the negative of the inner product (dot product) of two lists of numbers that have the same length.

list_pack

Alias of list_value.

list_position

Returns the position of the first occurrence of an element in a list.

list_prepend

Adds an element to the start of a list.

list_product

Multiplies the elements of a list and returns the product.

list_reduce

Combines the elements of a list into one value by applying a lambda function to each element in turn.

list_resize

Changes the number of elements in a list by removing elements from the end or by adding elements to the end.

list_reverse

Reverses the order of the elements in a list.

list_reverse_sort

Returns a list with its elements in descending order, from largest to smallest.

list_select

Returns the elements of a list at the positions that you specify.

list_sem

Returns the standard error of the mean of the elements of a list.

list_skewness

Returns the skewness of the elements of a list.

list_slice

Returns the part of a list or string between a start position and an end position.

list_sort

Returns a list with its elements in ascending or descending order.

list_stddev_pop

Returns the population standard deviation of the elements of a list.

list_stddev_samp

Returns the sample standard deviation of the elements of a list.

list_string_agg

Joins the elements of a list into one string, separated by commas.

list_sum

Returns the sum of the elements of a list.

list_transform

Applies a lambda function to each element of a list, and returns the list of results.

list_unique

Returns the number of distinct values in a list, not counting NULL.

list_value

Returns a list that holds the values that you pass as arguments.

list_var_pop

Returns the population variance of the elements of a list.

list_var_samp

Returns the sample variance of the elements of a list.

list_where

Returns the elements of a list at the positions where a mask list of BOOLEAN values is true.

list_zip

Combines several lists, position by position, into one list of unnamed structs.

range

Returns a list, or a table, of evenly spaced numbers or timestamps from a start value up to, but not including, a stop value.

reduce

Alias of list_reduce.

unnest

Expands a list into rows, one row for each element.