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

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

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.

aba = ba <=> b
11truetrue
12falsefalse
1nullnullfalse
nullnullnulltrue

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

countryregiontarget
INNorth100
INnull40
USnull70

equality join

countryregiontargetactual
INNorth10090

Two of the three rows disappear because null = null is not true. A null-safe join keeps all three.

Run the example

PySparkSpark SQL

Input: left

countryregiontarget
INNorth100
INnull40
USnull70

Input: right

countryregionactual
INNorth90
INnull45
USnull65
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.

Tip: often the better fix is upstream: replace the null with an explicit value such as "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?"

The change condition used !=, 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."
Join on the key, then OR together ~(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."
Every null key now matches every other null key, and all of them go to one partition. Many null keys means skew and a fan-out.

Check yourself

3 questions

What 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 read
pivot
Turn row values into columns, and why passing the values list up front makes it faster.

Related lessons

Previous: left_semi / left_anti

Primary sources: Column.eqNullSafe · NULL semantics