explode
Some columns hold lists: the items in an order, the tags on a post. To count or join on the items, you need one row per item. explode does that. This lesson shows how, why rows with empty lists disappear, and how one innocent explode can multiply a table's size.
Step 1 · PySpark functions · Silver · Arrays and nested data, lesson 1 of 5
On this page
Show code in
Every code block on the page follows this.
Read first: select (skip if you know it)
In plain words first
An order slip lists three items. To count items, you rewrite it as three slips, one per item, each copied with the same order number and date. A slip with no items produces no new slips at all, so it quietly disappears from the pile.
That is explode: one row per element of an arrayarray: A column that holds a list of values in each row, such as the items of one order. Learn more →, the other columns copied. explode_outer keeps the empty slip as one row with a nullnull: A missing or unknown value. Any comparison with null gives null, so filters treat it as false. Learn more → item.
What it does
explode is a generator: one input row can produce zero, one or many output rows. Each output row holds one element of the array, alongside copies of the other selected columns.
Step by step
orders
| order_id | items |
|---|---|
| 1 | [pen, ink] |
| 2 | [paper] |
| 3 | [] |
| 4 | null |
explode(items)
| order_id | item |
|---|---|
| 1 | pen |
| 1 | ink |
| 2 | paper |
Orders 3 and 4 vanish: an empty array has no elements to emit, and neither does null.
Run the example
Input: orders
| order_id | items |
|---|---|
| 1 | [pen, ink] |
| 2 | [paper] |
| 3 | [] |
| 4 | null |
from pyspark.sql import functions as F result = orders.select("order_id", F.explode_outer("items").alias("item"))
SELECT order_id, item FROM orders LATERAL VIEW OUTER explode(items) t AS item
This runs explode_outer, so orders 3 and 4 stay with a null item. Change it to F.explode to see them disappear.
The explode family
| Function | Output | Empty / null array |
|---|---|---|
explode | One row per element | Row dropped |
explode_outer | One row per element | One row with null |
posexplode | Position and element | Row dropped |
posexplode_outer | Position and element | One row with nulls |
inline | One row per structstruct: A column that holds a small record with named fields, like an address with a city and a pin code. Learn more →, struct fields as columns | Row dropped |
On a map column, explode produces two columns, key and value.
Strings first: split
Comma-separated text is a string, not an array. F.split("tags", ",") turns it into one; trim the elements if the separator has spaces: F.explode(F.split("tags", r"\s*,\s*")). split takes a regular expressionregular expression: A short pattern that describes what some text looks like, used to find, check or extract parts of strings. Learn more →, so a separator like | must be escaped: "\\|".
In SQL
Spark SQL accepts explode directly in the SELECT list (SELECT order_id, explode(items) AS item) and the Hive-style LATERAL VIEW shown above. LATERAL VIEW OUTER is the explode_outer equivalent. Only one generator is allowed per SELECT list.
Under the hood: data size Optional deep dive
explode is a narrow transformation: no shuffleshuffle: Moving rows between machines so that all rows with the same key end up together. Needed by joins, groupBy and sorting, and usually the most expensive step of a job. Learn more →. But it multiplies rows. Exploding a column averaging 1,000 elements turns 1 million rows into 1 billion, each carrying a copy of every other column. Select only the columns you need before exploding, and if you will aggregateaggregate: A function that turns many rows into one value per group: count, sum, avg, min, max. Learn more → straight back, check whether a higher-order function such as aggregate, filter or transform can work on the array without exploding at all.
Common mistakes
- Losing rows with empty arrays — Counts drop silently after explode. Use explode_outer when every parent row must survive.
- Exploding two arrays in one select — Spark allows one generator per select. Explode one, then the other (which gives a cross product), or zip them first with
arrays_zip. - Splitting on a regex metacharacter —
split(col, "|")splits on every character, because|is regex alternation. Escape it.
Key takeaways
- explode makes one row per array element and copies the other columns.
- Empty and null arrays produce no rows; explode_outer keeps them.
- posexplode also returns each element's position.
- explode does not shuffle, but it multiplies rows: prune columns first.
Interview prep
THE QUESTION
"Each order has an array of items. Count how often each item is bought, including orders with no items."
Avoid: forgetting the rows explode drops. Empty and null arrays disappear unless you use explode_outer.
Follow-ups · answer out loud, then open
Follow-up"What does explode produce on a map column?"
key and value, one row per entry. posexplode on a map adds a pos column too.Scenario"Each row has two arrays, items and quantities, aligned by position. Produce one row per item with its quantity."
F.arrays_zip("items", "quantities"), explode the zipped array, then select the struct fields. Exploding twice would produce every combination. You cannot use two generators in one select either.Trap"I can call explode on two columns in the same select."
Check yourself
3 questionsA table has 3 rows with arrays of size 2, 0 and null. How many rows does explode return?
Show the answer
2. Only the first row has elements. The empty and null arrays produce nothing. explode_outer would return 4.
Which keeps a row whose array is empty?
Show the answer
explode_outer. The _outer variants emit one row with nulls for an empty or null array.
Does explode cause a shuffle?
Show the answer
No, it is a narrow transformation, but it multiplies rows. Each input row is expanded in place. The cost is the extra data, not network movement.
Practise this
Problems that use explode, graded on hidden tests
Keep going
Up next · Arrays and nested data · lesson 2 of 5 · 3 min readposexplode
Explode an array and keep each element position, plus the _outer variant that keeps empty arrays.
Related lessons
collect_list / collect_setSpark internals · 6 min read
Narrow vs wide transformations
Previous: percentile_approx
Primary sources: functions.explode · LATERAL VIEW