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[])
Topics
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. |
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. |
Returns the cosine distance between two fixed-size arrays of numbers, which is 1 minus their cosine similarity. |
|
Returns the cosine similarity of two fixed-size arrays of numbers. |
|
Returns the cross product of two arrays of three numbers. |
|
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. |
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. |
Returns the inner product, also called the dot product, of two fixed-size arrays of numbers. |
|
array_intersect |
Alias of list_intersect. |
Returns the number of elements in a list or an array. |
|
array_negative_dot_product |
Alias of array_negative_inner_product. |
Returns the negative of the inner product of two fixed-size arrays of numbers. |
|
Returns a list without its last element. |
|
Returns a list without its first element. |
|
array_position |
Alias of list_position. |
Adds an element to the start of a list, with the element as the first argument. |
|
Adds an element to the end of a list, like |
|
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. |
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. |
Converts a list or array to a JSON array. |
|
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. |
Creates a fixed-size |
|
array_where |
Alias of list_where. |
array_zip |
Alias of list_zip. |
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. |
Combines the inner lists of a list of lists into one list. |
|
Returns a list, or a set of rows, of the values from a start value through a stop value. |
|
Returns the positions of the elements of a list, one row for each position. |
|
grade_up |
Alias of list_grade_up. |
Calls a lambda function with the arguments that you pass. |
|
len |
Alias of length. |
Returns the number of characters in a string, elements in a list, or bits in a bitstring. |
|
list_aggr |
Alias of list_aggregate. |
Runs an aggregate function, chosen by name, over the elements of a list. |
|
Returns the first element of a list that is not |
|
Adds an element to the end of a list. |
|
list_apply |
Alias of list_transform. |
Returns an approximate count of the distinct elements of a list that are not |
|
Returns the average of the elements of a list. |
|
Returns the bitwise AND of the non-NULL elements of a list of integers. |
|
Returns the bitwise OR of the non-NULL elements of a list of integers. |
|
Returns the bitwise exclusive OR (XOR) of the non-NULL elements of a list of integers. |
|
Tests whether every non-NULL element of a list of Boolean values is |
|
Tests whether at least one non-NULL element of a list of Boolean values is |
|
list_cat |
Alias of list_concat. |
Concatenates two or more lists into one list. |
|
Tests whether a list contains a given value. |
|
Returns 1 minus the cosine similarity of two equal-length lists of numbers, which measures how different their directions are. |
|
Returns the cosine of the angle between two equal-length lists of numbers, treated as vectors. |
|
Returns the number of non-NULL elements in a list. |
|
Returns the straight-line (Euclidean) distance between two equal-length lists of numbers that represent points. |
|
Returns the distinct non- |
|
list_dot_product |
Alias of list_inner_product. |
list_element |
Alias of list_extract. |
Returns the Shannon entropy, in bits, of the values in a list. |
|
Returns the element at a given position in a list, or the character at a given position in a string. |
|
Keeps the list elements that meet a condition written as a lambda function. |
|
Returns the first element of a list, even when that element is |
|
Returns the positions of the elements of a list, in the order that sorts the elements. |
|
list_has |
Alias of list_contains. |
Tests whether a list contains every element of a second list. |
|
Tests whether two lists have at least one element in common. |
|
Counts how often each element occurs in a list and returns the counts as a map. |
|
list_indexof |
Alias of list_position. |
Returns the inner product (dot product) of two lists of numbers that have the same length. |
|
Returns the distinct elements that two lists have in common. |
|
Returns the sample excess kurtosis of the numbers in a list. |
|
Returns the population excess kurtosis of the numbers in a list. |
|
Returns the last element of a list, even when that element is |
|
Returns the median absolute deviation of the elements of a list. |
|
Returns the largest element of a list. |
|
Returns the middle value of the elements of a list. |
|
Returns the smallest element of a list. |
|
Returns the most frequent element of a list. |
|
list_negative_dot_product |
Alias of 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. |
Returns the position of the first occurrence of an element in a list. |
|
Adds an element to the start of a list. |
|
Multiplies the elements of a list and returns the product. |
|
Combines the elements of a list into one value by applying a lambda function to each element in turn. |
|
Changes the number of elements in a list by removing elements from the end or by adding elements to the end. |
|
Reverses the order of the elements in a list. |
|
Returns a list with its elements in descending order, from largest to smallest. |
|
Returns the elements of a list at the positions that you specify. |
|
Returns the standard error of the mean of the elements of a list. |
|
Returns the skewness of the elements of a list. |
|
Returns the part of a list or string between a start position and an end position. |
|
Returns a list with its elements in ascending or descending order. |
|
Returns the population standard deviation of the elements of a list. |
|
Returns the sample standard deviation of the elements of a list. |
|
Joins the elements of a list into one string, separated by commas. |
|
Returns the sum of the elements of a list. |
|
Applies a lambda function to each element of a list, and returns the list of results. |
|
Returns the number of distinct values in a list, not counting |
|
Returns a list that holds the values that you pass as arguments. |
|
Returns the population variance of the elements of a list. |
|
Returns the sample variance of the elements of a list. |
|
Returns the elements of a list at the positions where a mask list of |
|
Combines several lists, position by position, into one list of unnamed structs. |
|
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. |
Expands a list into rows, one row for each element. |