Skip to content
Silver 4 min read · Arrays 3 practice problems ↓

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

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_iditems
1[pen, ink]
2[paper]
3[]
4null

explode(items)

order_iditem
1pen
1ink
2paper

Orders 3 and 4 vanish: an empty array has no elements to emit, and neither does null.

Run the example

PySparkSpark SQL

Input: orders

order_iditems
1[pen, ink]
2[paper]
3[]
4null
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

FunctionOutputEmpty / null array
explodeOne row per elementRow dropped
explode_outerOne row per elementOne row with null
posexplodePosition and elementRow dropped
posexplode_outerPosition and elementOne row with nulls
inlineOne 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 columnsRow 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."

explode the items and group by item. Then point out that explode drops orders with empty arrays, which only matters if the question also wants order counts; for those use explode_outer or count orders separately. Mention selecting only order_id and items before the explode to keep the row size small.

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?"
Two columns, 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."
Zip them first with 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."
No. Spark allows one generator per select clause. Explode in separate steps (which multiplies rows) or zip the arrays first.

Check yourself

3 questions

A 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 read
posexplode
Explode an array and keep each element position, plus the _outer variant that keeps empty arrays.

Related lessons

Previous: percentile_approx

Primary sources: functions.explode · LATERAL VIEW