Returns a single value that is the result of applying the lambda function to each element of the input list. See the Lambda Functions page for more details. While list functions use a 1-based indexing, list_reduce uses a 0-based indexing. This is a known issue.
Zips kLISTs to a new LIST whose length will be that of the longest list. Its elements are structs of k elements from each list list_1, …, list_k, missing elements are replaced with NULL. If truncate is set, all lists are truncated to the smallest list length.
Returns a single value that is the result of applying the lambda function to each element of the input list. See the Lambda Functions page for more details. While list functions use a 1-based indexing, list_reduce uses a 0-based indexing. This is a known issue.
Zips kLISTs to a new LIST whose length will be that of the longest list. Its elements are structs of k elements from each list list_1, …, list_k, missing elements are replaced with NULL. If truncate is set, all lists are truncated to the smallest list length.
List comprehensions can also use the position of the list elements by adding a second variable.
In the following example, we use x, i, where x is the value and i is the position:
SELECT[4,5,6]ASl,[xFORx,iINlIFi!=2]filtered;
l
filtered
[4, 5, 6]
[4, 6]
Under the hood, [f(x) FOR x IN y IF g(x)] is translated to list_transform(list_filter(y, x -> f(x)), x -> f(x)).
DuckDB offers two range functions, range(start, stop, step) and generate_series(start, stop, step), and their variants with default arguments for stop and step. The two functions' behavior is different regarding their stop argument. This is documented below.
The range function creates a list of values in the range between start and stop.
The start parameter is inclusive, while the stop parameter is exclusive.
The default value of start is 0 and the default value of step is 1.
Based on the number of arguments, the following variants of range exist.
The generate_series function creates a list of values in the range between start and stop.
Both the start and the stop parameters are inclusive.
The default value of start is 0 and the default value of step is 1.
Based on the number of arguments, the following variants of generate_series exist.
Date ranges are also supported for TIMESTAMP and TIMESTAMP WITH TIME ZONE values.
Note that for these types, the stop and step arguments have to be specified explicitly (a default value is not provided).
The function list_aggregate allows the execution of arbitrary existing aggregate functions on the elements of a list. Its first argument is the list (column), its second argument is the aggregate function name, e.g., min, histogram or sum.
list_aggregate accepts additional arguments after the aggregate function name. These extra arguments are passed directly to the aggregate function, which serves as the second argument of list_aggregate.
The following is a list of existing rewrites. Rewrites simplify the use of the list aggregate function by only taking the list (column) as their argument. list_avg, list_var_samp, list_var_pop, list_stddev_pop, list_stddev_samp, list_sem, list_approx_count_distinct, list_bit_xor, list_bit_or, list_bit_and, list_bool_and, list_bool_or, list_count, list_entropy, list_last, list_first, list_kurtosis, list_kurtosis_pop, list_min, list_max, list_product, list_skewness, list_sum, list_string_agg, list_mode, list_median, list_mad and list_histogram.
The function list_sort sorts the elements of a list either in ascending or descending order.
In addition, it allows to provide whether NULL values should be moved to the beginning or to the end of the list.
It has the same sorting behavior as DuckDB's ORDER BY clause.
Therefore, (nested) values compare the same in list_sort as in ORDER BY.
By default, if no modifiers are provided, DuckDB sorts ASC NULLS FIRST.
I.e., the values are sorted in ascending order and NULL values are placed first.
This is identical to the default sort order of SQLite.
The default sort order can be changed using PRAGMA statements..
list_sort leaves it open to the user whether they want to use the default sort order or a custom order.
list_sort takes up to two additional optional parameters.
The second parameter provides the sort order and can be either ASC or DESC.
The third parameter provides the NULL order and can be either NULLS FIRST or NULLS LAST.
This query uses the default sort order and the default NULL order.
SELECTlist_sort([1,3,NULL,5,NULL,-5]);
[NULL,NULL,-5,1,3,5]
This query provides the sort order.
The NULL order uses the configurable default value.
SELECTlist_sort([1,3,NULL,2],'ASC');
[NULL,1,2,3]
This query provides both the sort order and the NULL order.
The flatten function is a scalar function that converts a list of lists into a single list by concatenating each sub-list together.
Note that this only flattens one level at a time, not all levels of sub-lists.
Convert a list of lists into a single list:
SELECTflatten([[1,2],[3,4]]);
[1, 2, 3, 4]
If the list has multiple levels of lists, only the first level of sub-lists is concatenated into a single list:
SELECTflatten([[[1,2],[3,4],],[[5,6],[7,8],]]);
[[1, 2], [3, 4], [5, 6], [7, 8]]
In general, the input to the flatten function should be a list of lists (not a single level list).
However, the behavior of the flatten function has specific behavior when handling empty lists and NULL values.
If the input list is empty, return an empty list:
SELECTflatten([]);
[]
If the entire input to flatten is NULL, return NULL:
SELECTflatten(NULL);
NULL
If a list whose only entry is NULL is flattened, return an empty list:
SELECTflatten([NULL]);
[]
If the sub-list in a list of lists only contains NULL, do not modify the sub-list:
-- (Note the extra set of parentheses vs. the prior example)SELECTflatten([[NULL]]);
[NULL]
Even if the only contents of each sub-list is NULL, still concatenate them together. Note that no de-duplication occurs when flattening. See list_distinct function for de-duplication:
There are also aggregate functionslist and histogram that produces lists and lists of structs.
The unnest function is used to unnest a list by one level.