Real-World Patterns
Concepts covered: Array Operations
Nested data in production arrives in volume, in loose formats and with holes. Three habits cover most of the trouble: answer inside the row before reaching for UNNEST, parse JSON into typed arrays defensively, and decide in advance what empty and NULL collections should produce.
Row-local first, explode second
- playlists (storage, a row per playlist)
- CROSS JOIN UNNEST (transform, a row per track)
- GROUP BY playlist_id (transform, shuffle by key); parallelism: 200 partitions
- filter + cardinality (transform, row-local lambdas)
- long tracks per playlist (consumer, dashboard)
- playlists -> CROSS JOIN UNNEST
- CROSS JOIN UNNEST -> GROUP BY playlist_id
- GROUP BY playlist_id -> long tracks per playlist
- playlists -> filter + cardinality
- filter + cardinality -> long tracks per playlist
Both paths count, per playlist, the tracks longer than 180 seconds. The UNNEST path multiplies rows by the array length, then GROUP BY sends every row with the same playlist_id to the same worker, which in a distributed engine means writing those rows to the network and reading them back. The lambda path reads each playlist once and returns it once.
| playlist_id | durations_sec |
|---|---|
| 101 | [210, 170, 240] |
| 102 | [195, null, 260] |
| 103 | [] |
lambdas
Each playlist row is answered in place, and playlist 103's empty array returns 0.
SELECT
playlist_id,
cardinality(filter(durations_sec, x -> x > 180)) AS long_tracks
FROM playlists
ORDER BY playlist_id| playlist_id | long_tracks |
|---|---|
| 101 | 2 |
| 102 | 2 |
| 103 | 0 |
unnest
Rows are grouped back by playlist, and playlist 103 has no rows to group, so it is missing.
SELECT
p.playlist_id,
count_if(t.sec > 180) AS long_tracks
FROM playlists AS p
CROSS JOIN UNNEST(p.durations_sec) AS t(sec)
GROUP BY p.playlist_id
ORDER BY p.playlist_id| playlist_id | long_tracks |
|---|---|
| 101 | 2 |
| 102 | 2 |
Playlists 101 and 102 show 2 long tracks in both tabs. The row that vanishes from the second tab is the dangerous part: any average, ratio or join built on it excludes every playlist with an empty array, and nothing in the output says so.
A dashboard averages long_tracks over the rows it receives. What does it show when it reads the unnest tab's result instead of the lambda tab's?
- 1.33, because both versions count 4 long tracks over 3 playlists
- NULL, because playlist 102's NULL duration makes its count NULL
- 2.0, because playlist 103 has no row and 4 is divided by 2
- 1.0, because the NULL duration adds a fourth row counted as 0
Parsing JSON payloads into typed arrays
Event pipelines often deliver arrays inside a JSON string. TRY_CAST(json_extract(payload, '$.skips') AS ARRAY(INTEGER)) turns the JSON array into a typed array, and returns NULL instead of failing the whole query when a producer sends something else. Once typed, the arrays work with every lambda in this lesson. Below, zip_with pairs each track with its skip flag and keeps the track id when the flag is 1, and filter removes the empty slots.
SELECT
event_id,
filter(
zip_with(
TRY_CAST(json_extract(payload, '$.track_ids') AS ARRAY(VARCHAR)),
TRY_CAST(json_extract(payload, '$.skips') AS ARRAY(INTEGER)),
(t, s) -> IF(s = 1, t)
),
t -> t IS NOT NULL
) AS skipped_tracks
FROM eventsEvent 1 returns ['t4', 't7']. Event 2's producer sent the string none where an array belonged, so the cast gives NULL, zip_with over a NULL array gives NULL, and the event's result is NULL where a plain CAST would have stopped the job. Count those NULLs on a schedule with COUNT_IF(skipped_tracks IS NULL); a sudden rise means a producer changed its format.
Habits that prevent incidents
- Do
- Answer per-row questions with filter, transform and reduce before reaching for UNNEST
- Add WITH ORDINALITY whenever element order carries meaning
- Compare the cardinality of parallel arrays before unnesting them together
- Use TRY_CAST when turning JSON into arrays or maps
- Avoid
- Unnest 2 arrays in separate CROSS JOINs
- Let a NULL element turn a reduce total into NULL
- Trust ARRAY_AGG order without an ORDER BY inside the call
- Forget that CROSS JOIN UNNEST drops empty and NULL arrays
Capstone: long tracks by id
The final query combines the lesson: 2 parallel arrays, a pairing lambda, a filter and no UNNEST. For each playlist it returns the ids of tracks longer than 180 seconds as an array; on playlists 101 and 102 that is ['t9', 't7'] and ['t2', 't8'].
The playlists table has playlist_id, track_ids and durations_sec, with matching positions in the 2 arrays. Build a query that returns a row per playlist with an array of the track ids whose duration exceeds 180 seconds.
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.