eqNullSafe (<=>)
Comparing columns that can be missing is trickier than it looks: in Spark, null equals null is not true. Joins on nullable keys lose rows, and change detection misses changes. This lesson shows why, and the null-safe comparison that fixes both.
Step 1 · PySpark functions · Silver · Reshape and refine, lesson 3 of 7
On this page
Show code in
Every code block on the page follows this.
Read first: join · filter / where (skip if you know them)
In plain words first
Two forms both have the "middle name" box empty. Are the middle names the same? You cannot say: both are unknown. That is how Spark reasons with =, so two missing values never count as equal.
Sometimes you mean "both empty counts as the same", for example when matching rows or checking whether a record changed. The null-safe comparison <=> (eqNullSafe) says exactly that.
The problem
In SQL logic, null means "unknown". Is one unknown equal to another unknown? The answer is unknown, so null = null is null, and filters and joins treat null as false.
| a | b | a = b | a <=> b |
|---|---|---|---|
| 1 | 1 | true | true |
| 1 | 2 | false | false |
| 1 | null | null | false |
| null | null | null | true |
Step by step: a join on a nullable key
Targets and actuals are keyed by country and region, and region is null for country-level rows. A normal join on both columns loses those rows:
left joined to right on country and region
| country | region | target |
|---|---|---|
| IN | North | 100 |
| IN | null | 40 |
| US | null | 70 |
equality join
| country | region | target | actual |
|---|---|---|---|
| IN | North | 100 | 90 |
Two of the three rows disappear because null = null is not true. A null-safe join keeps all three.
Run the example
Input: left
| country | region | target |
|---|---|---|
| IN | North | 100 |
| IN | null | 40 |
| US | null | 70 |
Input: right
| country | region | actual |
|---|---|---|
| IN | North | 90 |
| IN | null | 45 |
| US | null | 65 |
cond = (left["country"] == right["country"]) & ( left["region"].eqNullSafe(right["region"]) ) result = left.join(right, cond).select( left["country"], left["region"], "target", "actual")
SELECT l.country, l.region, l.target, r.actual FROM `left` l JOIN `right` r ON l.country = r.country AND l.region <=> r.region
Detecting changed rows
When comparing yesterday's and today's version of a record, old.email != new.email is null when either side is null, so a change from null to a value is missed. The null-safe "not equal" is ~old.email.eqNullSafe(new.email), or in SQL NOT (old.email <=> new.email) / old.email IS DISTINCT FROM new.email.
Under the hood Optional deep dive
Spark can still use a hash or sort-merge joinsort-merge join: Spark's default join for two large tables: both sides are shuffled and sorted by the key, then matched in one pass. Learn more → with a null-safe key: it rewrites a <=> b into an equi-join on coalesce(a, default) plus an isnull(a) flag. That has a cost: every null key now lands in the same partitionpartition: A chunk of a DataFrame's rows. Spark processes each partition as one task, so partitions decide how much work runs in parallel. Learn more →. If most keys are null, one tasktask: The work for one partition in one stage, run on one CPU core. Learn more → gets most of the data. Check the null share of a column before joining null-safely on it.
"ALL" at ingestion, so the key means something and plain equality works.Common mistakes
- Expecting null keys to match in a join — They never do with ==. Use eqNullSafe or replace nulls with a sentinel.
- Detecting changes with != — Changes from or to null are missed. Use the null-safe comparison.
- Null-safe joins on mostly-null keys — All nulls hash to one partition, creating skewskew: When one key has far more rows than the others, so one task does most of the work while the rest wait. Learn more →.
Key takeaways
- null = null is null, which filters and joins treat as false.
- eqNullSafe / <=> treats two nulls as equal.
- Use the null-safe comparison to detect changes involving nulls.
- Null-safe joins send all null keys to one partition.
Interview prep
THE QUESTION
"A MERGE that updates changed rows misses rows where a column went from null to a value. Why?"
!=, which is null, not true, when one side is null. Use NOT (a <=> b) / IS DISTINCT FROM. A strong answer also mentions null-safe join keys and their skew risk.Avoid: "compare with coalesce(col, '')". It works until a real value equals the placeholder.
Follow-ups · answer out loud, then open
Follow-up"What does null <=> null return, and null = null?"
null <=> null is true, null = null is null. 1 <=> null is false. In SQL, IS NOT DISTINCT FROM is the standard spelling.Scenario"Detect changed rows between two snapshots of a table with 40 nullable columns."
~(a.c <=> b.c) for each column, built with a comprehension and functools.reduce. Or compare a hash of each row computed with a null-aware approach, such as xxhash64 over all columns.Trap"Null-safe join keys have no downside."
Check yourself
3 questionsWhat is NULL <=> NULL?
Show the answer
true. The null-safe operator treats two nulls as equal.
What is 1 <=> NULL?
Show the answer
false. It never returns null: one null and one value are not equal.
Which detects a change from null to "a@x.com"?
Show the answer
NOT (old <=> new). old != new is null in that case. The null-safe form is false for equality, so NOT gives true.
Practise this
Problems that use eqNullSafe (<=>), graded on hidden tests
Keep going
Up next · Reshape and refine · lesson 4 of 7 · 5 min readpivot
Turn row values into columns, and why passing the values list up front makes it faster.
Related lessons
joinPySpark functions · 5 min read
filter / whereData lake & lakehouse · 7 min read
MERGE INTO and upserts
Previous: left_semi / left_anti
Primary sources: Column.eqNullSafe · NULL semantics