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:

Elementx > 180Kept?
195TRUEYes
NULLNULLNo
260TRUEYes

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.

Stepxs befores after
startnonenone0
12100210
2170210380
3240380620
finishnone620620

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 durations

Playlist 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 durations

Playlist 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

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.