Lists & arrays
Scalar
list_reduce()
Returns a single value that is the result of applying the lambda function to each element of the input list, starting with the first element and then repeatedly applying the lambda function to the result of the previous application and the next element of the list. When an initial value is provided, it is used as the first argument to the lambda function
Arguments & returns
2 overloadslist_reduce(list, lambda[)
Scalar
| Overload link | list | lambda[ |
Returns |
|---|---|---|---|
| #01 | ANY[] | LAMBDA |
ANY |
list_reduce(list, lambda[, initial])
Scalar
| Overload link | list | lambda[ | initial] |
Returns |
|---|---|---|---|---|
| #02 | ANY[] | LAMBDA | ANY |
ANY |
Example
Haybarn WASM 1.5.5-rc3 · In your browser
Engine example · Run to view results.
Catalog examples
SELECT
list_reduce([1, 2, 3], (x, y) -> x + y);
In other engines
Used in larger rewrites 1
These examples use list_reduce as one part of a larger SQL translation.
Snowflakebitmap_construct_agg()
SELECT BITMAP_CONSTRUCT_AGG(v) FROM t
SELECT (SELECT CASE WHEN l IS NULL OR LENGTH(l) = 0 THEN NULL WHEN LENGTH(l) <> LENGTH(LIST_FILTER(l, __v -> __v BETWEEN 0 AND 32767)) THEN NULL WHEN LENGTH(l) < 5 THEN UNHEX(PRINTF('%04X', LENGTH(l)) || h || REPEAT('00', GREATEST(0, 4 - LENGTH(l)) * 2)) ELSE UNHEX('08000000000000000000' || h) END FROM (SELECT l, COALESCE(LIST_REDUCE(LIST_TRANSFORM(l, __x -> PRINTF('%02X%02X', CAST(__x AS INT) & 255, (CAST(__x AS INT) >> 8) & 255)), (__a, __b) -> __a || __b, ''), '') AS h FROM (SELECT LIST_SORT(LIST_DISTINCT(LIST(v) FILTER(WHERE NOT v IS NULL))) AS l))) FROM t