Null-Key Skew
Concepts covered: Data Skew
In section 0's ranking of follows, the second-hottest value was NULL, with 12M rows. Missing keys pile up because a single cause creates them in bulk: follows of deleted accounts, events from logged-out visitors, orders written before the customer id was captured. Every null hashes to the same value, so a shuffle on that column sends all of them to a single partition.
Inner joins drop them, left joins keep them
A null key can never satisfy an equality, so an inner join has nothing to find for it. Catalyst knows this: during logical optimization it adds an isnotnull filter on each join key, and null-keyed rows are discarded before the shuffle. A left join cannot discard them. It must return every left row, matched or not, so its null-keyed rows travel through the exchange and pile onto a single reducer that will match none of them.
The animation runs a left join of 7 orders to 3 customers with broadcasting off and 2 shuffle partitions. Four of the 7 orders have no customer_id. Watch the 4 null rows: they leave their input tasks for the same reducer, and each comes out with a null name.
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1)
orders.join(customers, "customer_id", "left").collect()| order_id | customer_id |
|---|---|
| 1 | c1 |
| 2 | NULL |
| 3 | c2 |
| 4 | NULL |
| 5 | NULL |
| 6 | c3 |
| 7 | NULL |
| customer_id | name |
|---|---|
| c1 | Ana |
| c2 | Ben |
| c3 | Chen |
At scale that reducer holds the 12M null follows plus its share of real keys, and the work is pure overhead: each output row is the input row with nulls appended. A groupBy on the same column also builds a null group, but a combining aggregate such as count keeps it to a partial per map task, so the left join is where null skew costs the most.
Send the nulls around the join
from pyspark.sql import functions as F
has_key = orders.filter(F.col("customer_id").isNotNull())
no_key = orders.filter(F.col("customer_id").isNull())
joined = has_key.join(customers, "customer_id", "left")
result = joined.unionByName(no_key, allowMissingColumns=True)The null rows skip the join altogether. unionByName with allowMissingColumns=True, available since Spark 3.1, fills the customer columns with nulls for them, which is what the left join would have produced. Only keyed rows can enter an exchange, and the reducer that used to collect every null now gets an ordinary share.
Sentinel values such as '', 'unknown', 0 or -1 behave like a real key. Catalyst infers only isnotnull, so an inner join shuffles every sentinel row to the same task whether it finds a partner, and a matching placeholder row in the dimension also multiplies the output on that task. Count them like any hot key and isolate them. The isolation filter from section 2 also drops nulls: ~F.col("followee_id").isin(hot_keys) evaluates to null on a null key, and filter discards null. In an inner join that loses nothing; in a left join the rest path needs F.col("followee_id").isNull() | ~F.col("followee_id").isin(hot_keys) to keep those rows.
A job left-joins page_views to users on user_id with 400 shuffle partitions, and 40% of page views have a null user_id. Which rewrite returns the same rows while shuffling none of the null-keyed page views?
- Raise spark.sql.shuffle.partitions from 400 to 4,000 so the null rows spread out
- Switch the left join to an inner join, which filters null keys before the shuffle
- Salt both sides with N = 40 so the null keys hash to 40 different partitions
- Join only the non-null rows, then unionByName the null rows with allowMissingColumns=True
Before choosing a fix, measure how much of the key is missing. The cell computes it as a single global aggregate: 1 row holding the total, the null count and the null share, with no row per region and no separate count() job to divide by.
About this lesson
This section is part of the Data Skew: Intermediate 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.