TRANSFORM for Array Mapping

Concepts covered: Array Transforms

transform(array, x -> expression) returns a new array with an output element for every input element, in the same order. The part written x -> expression is a lambda: a small unnamed function that the engine calls once per element, with x standing for the element being visited. The query's row count never changes, because the work happens inside each row's array and creates or removes no rows.

Anatomy of a lambda

PieceIn x -> round(x / 60.0, 2)What it does
Parameter`x`Names the current element. Any identifier works: `sec`, `t`, `d`.
Arrow`->`Separates the parameter from the body.
Body`round(x / 60.0, 2)`Computes the output element. Its type sets the output array's type.
Captured columnnone hereThe body may read other columns of the same row, such as `gap_sec`.

The body can also read other columns of the same row. transform(durations_sec, x -> x + gap_sec) adds that row's own gap to each of its tracks, so 2 playlists with different gap_sec values get different arrays from the same expression.

In the query below, durations arrive in seconds and the product team wants minutes. Playlist 102 has a track whose duration was never recorded, stored as NULL, so watch what happens to its slot.

SELECT
  playlist_id,
  durations_sec,
  transform(durations_sec, x -> round(x / 60.0, 2)) AS durations_min
FROM playlists
playlists
playlist_iddurations_sec
101[210, 170, 240]
102[195, null, 260]
Result
playlist_iddurations_secdurations_min
101[210, 170, 240][3.5,2.83,4]
102[195, null, 260][3.25,null,4.33]

Playlist 101 comes back as 3.5, 2.83 and 4.0 minutes. Playlist 102 keeps all 3 slots, and its middle slot is NULL: NULL / 60.0 is NULL, and transform stores whatever the body returns. The output array has exactly as many elements as the input array, every time.

Edge cases worth predicting

InputLambdaResult
`ARRAY[1, 2, 3]``x -> x * 10``[10, 20, 30]`
`ARRAY[1, NULL, 3]``x -> x * 10``[10, NULL, 30]`
`ARRAY[1, NULL, 3]``x -> coalesce(x, 0) * 10``[10, 0, 30]`
`ARRAY['a', 'b']``x -> upper(x)``['A', 'B']`
`ARRAY[1, 2]``x -> x > 1``[false, true]`
empty array`x -> x * 10``[]`
`NULL``x -> x * 10``NULL`

The last 2 rows separate an empty array from a NULL one. An empty array gives the lambda nothing to visit, so an empty array comes back. A NULL array also has nothing to visit, but the result is NULL, and the difference carries forward: cardinality returns 0 for the first and NULL for the second.

Using an element's position

transform gives the lambda the value and nothing about where that value sits. When the output depends on position, pair the array with sequence(1, cardinality(track_ids)), which builds the array 1, 2, 3 up to the array's length, and walk both arrays together with zip_with(a, b, (x, i) -> ...). It calls its lambda once per position with an element from each array; when the arrays differ in length, the shorter one is padded with NULL.

SELECT
  playlist_id,
  zip_with(track_ids, sequence(1, cardinality(track_ids)), (t, i) -> CAST(i AS VARCHAR) || '. ' || t) AS numbered
FROM playlists

The result is ['1. t9', '2. t4', '3. t7']: the numbers come from the sequence and the track ids from the array, matched slot by slot.

What does this query return in out?

SELECT
  transform(ARRAY[4, NULL, 6], x -> coalesce(x, 1) * x) AS out

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.