Engineering · 1 min read
Spark Array Functions & collect_list vs collect_set
Spark's higher-order array functions — transform, filter, exists, forall — plus NVL, array_contains, and the collect_list vs collect_set difference (keeps duplicates vs de-dupes).
Spark’s higher-order array functions, plus the collect_* family for rolling multiple rows into an array.
Common
NVL(x, default)— default value for nullarray_contains(arr, val)— array membership check, returns Boolcollect_list()/collect_set()— roll multiple rows into an arrayto_json()— struct → JSONtransform(arr, x -> x*2)— apply an expression to every elementfilter(arr, x -> x > 0)— keep elements matching a predicateexists(arr, x -> x > 0)— true if any element matches → equivalent to Python’sany()forall(arr, x -> ...)— true if all elements match → equivalent to Python’sall()
collect_list() vs collect_set()
Data:
| customer_id | item_bought |
|---|---|
| 101 | laptop |
| 101 | mouse |
| 101 | mouse |
| 102 | monitor |
| 102 | hdmi_cable |
SELECT
customer_id,
collect_list(item_bought) AS all_items,
collect_set(item_bought) AS unique_items
FROM store_sales
GROUP BY customer_id;
Result:
| customer_id | all_items (collect_list) | unique_items (collect_set) |
|---|---|---|
| 101 | ["laptop", "mouse", "mouse"] | ["laptop", "mouse"] |
| 102 | ["monitor", "hdmi_cable"] | ["monitor", "hdmi_cable"] |
The difference: collect_list keeps duplicates, collect_set de-dupes.
References
- Spark SQL Built-in Functions — https://spark.apache.org/docs/latest/api/sql/
Related: back to the Cross-Engine DB Query overview; see also Spark data types and the ClickHouse counterpart, ClickHouse array and conditional functions.