FILTER and REDUCE
Concepts covered: Array Operations
filter(array, x -> condition) returns the elements whose condition is TRUE, in their original order. An element whose condition is FALSE is dropped, and so is an element whose condition is NULL: filter keeps an element only on a definite TRUE. The output can hold anywhere from 0 elements to all of them.
Here is the trace for playlist 102's durations under the condition x > 180:
| Element | x > 180 | Kept? |
|---|---|---|
| 195 | TRUE | Yes |
| NULL | NULL | No |
| 260 | TRUE | Yes |
The result is [195, 260]. Playlist 102's missing duration disappears with no explicit NULL check, because NULL > 180 is NULL and only TRUE keeps an element. That suits most filters, and it is the opposite of what transform did with the same element, where the NULL kept its slot.
Yes-or-no questions about an array
When the question is whether any element qualifies, any_match(arr, x -> condition) returns a BOOLEAN directly, and all_match and none_match cover the other 2 shapes. All 3 follow three-valued logic: all_match returns NULL when no element fails the condition but at least one element makes it NULL. cardinality(filter(arr, x -> condition)) answers a neighbouring question, how many elements qualify, and returns a number for any non-NULL array.
REDUCE carries a state through the array
reduce(array, initial_state, (s, x) -> next_state, s -> result) takes 4 arguments. The engine sets the state s to the initial value, then calls the middle lambda once per element, in order, with the current state and that element; whatever it returns becomes the new state. After the last element, the final lambda runs once and turns the state into the answer. The trace below is reduce(ARRAY[210, 170, 240], 0, (s, x) -> s + x, s -> s), playlist 101's durations.
| Step | x | s before | s after |
|---|---|---|---|
| start | none | none | 0 |
| 1 | 210 | 0 | 210 |
| 2 | 170 | 210 | 380 |
| 3 | 240 | 380 | 620 |
| finish | none | 620 | 620 |
The initial value fixes the state's type. Starting from 0 makes the state an integer, and every value the middle lambda returns must have that same type, which is why a running total of DOUBLE values starts from CAST(0 AS DOUBLE).
A single NULL poisons a running total
SELECT
playlist_id,
reduce(secs, 0, (s, x) -> s + x, s -> s) AS naive_total,
reduce(secs, 0, (s, x) -> s + coalesce(x, 0), s -> s) AS safe_total
FROM durationsPlaylist 101 totals 620 both ways. Playlist 102's naive total is NULL: at step 2 the state becomes 195 + NULL, which is NULL, and every later step adds to NULL. Applying coalesce(x, 0) inside the middle lambda keeps the state a number, and the safe total comes out at 455.
FILTER feeds REDUCE
The functions compose. Filter first to choose which elements count, reduce what is left, and let the final lambda format the answer. Because the filter already removes the NULL, this reduce needs no coalesce.
SELECT
playlist_id,
reduce(filter(secs, x -> x > 180), 0, (s, x) -> s + x, s -> round(s / 60.0, 1)) AS long_track_min
FROM durationsPlaylist 101 keeps 210 and 240, 450 seconds, which the final lambda turns into 7.5 minutes. Playlist 102 keeps 195 and 260, 455 seconds, which rounds to 7.6 minutes.
What does this query return in all_long?
SELECT
all_match(ARRAY[210, NULL, 240], x -> x > 180) AS all_long- TRUE, because the NULL element is skipped as filter skips it
- FALSE, because a NULL element cannot be greater than 180
- An error, because all_match rejects arrays holding NULL
- NULL, because no element fails and one element is unknown
About this lesson
This section is part of the Complex Data: Advanced lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.