Non-Equi Joins
Concepts covered: Shuffle Optimization
A hash or sort-merge join works because equal keys can be routed to one task. Rows that might match meet there, and rows that cannot match are never compared. A range condition offers nothing to route by. Take t.start_ts >= w.from_ts AND t.start_ts < w.to_ts: a trip starting at 14:07 could fall in any window of the table, so no partitioning of the windows rules any of them out in advance. Spark has to test the condition on pairs.
What Spark plans with no equality
| Situation | Physical operator | How pairs are formed |
|---|---|---|
| One side under the broadcast threshold, or hinted | `BroadcastNestedLoopJoin` | The small side is copied to every executor; each task loops over its rows of the big side and, for each one, over the whole copy |
| Neither side small, inner join | `CartesianProduct` | One task per pair of partitions: 50 left and 40 right make 2,000 tasks, and each right partition is read once per left one |
| Neither side small, outer join | `BroadcastNestedLoopJoin`, forced | Spark broadcasts one side whatever its size, which can exhaust driver or executor memory |
The animation broadcasts 3 surge windows and runs 4 trips through them in 2 tasks, with start times in minutes. No shuffle happens. Each task tests each of its trips against all 3 windows: 12 comparisons to produce 4 rows.
in_window = (trips.start_min >= surge_windows.from_min) & (
trips.start_min < surge_windows.to_min
)
trips.join(F.broadcast(surge_windows), in_window).collect()| trip_id | start_min |
|---|---|
| t1 | 5 |
| t2 | 12 |
| t3 | 18 |
| t4 | 31 |
| window_id | from_min | to_min | multiplier |
|---|---|---|---|
| w1 | 0 | 10 | 1 |
| w2 | 10 | 20 | 1.8 |
| w3 | 20 | 40 | 1.2 |
Pricing a range join in pairs
A nested loop costs the number of pairs tested, whatever share of them match. Take a surge-pricing job: 48 million trips a day across 500 cities, with a surge window every 2 minutes in each city. That makes 720 windows per city per day and 360,000 in all. Each trip belongs to exactly one window of its own city. The range-only condition also matches it to the concurrent window in the other 499 cities, which returns about 24 billion rows, so city_id is needed for a correct answer as well as for speed. The 2 keyed versions below both return the right 48 million rows.
| Join condition | Windows tested per trip | Pairs tested per day |
|---|---|---|
| time range only | 360,000 | 17.28 trillion |
| equal `city_id`, plus the time range | 720 | 34.56 billion |
| equal `city_id` and hour, plus the time range | 30 | 1.44 billion |
Pairs tested equal trips times the windows each trip is checked against, and every equality term cuts the second factor. One equality is enough to change the operator. Spark takes the equal columns as join keys, plans a sort-merge or broadcast hash join on them, and evaluates the range as a filter on each pair within a key group. The plan then shows a keyed operator such as SortMergeJoin [city_id], [city_id], Inner, with the range condition after the join type.
- 12,000x
Binning a range into a key
Time has no natural equality, so you build one. Truncate each trip's start to the hour, and give each window one row for every hour it overlaps, so a window from 13:59 to 14:01 appears under both 13:00 and 14:00. Join on city and hour, and keep the range test in the condition to drop pairs that share an hour but not a window.
trips_h = trips.withColumn("hour", F.date_trunc("hour", "start_ts"))
windows_h = surge_windows.withColumn(
"hour",
F.explode(
F.sequence(
F.date_trunc("hour", "from_ts"),
F.date_trunc("hour", F.expr("to_ts - interval 1 microsecond")),
F.expr("interval 1 hour"),
)
),
)
priced = trips_h.join(
windows_h,
(trips_h.city_id == windows_h.city_id)
& (trips_h.hour == windows_h.hour)
& (trips_h.start_ts >= windows_h.from_ts)
& (trips_h.start_ts < windows_h.to_ts),
)The sequence ends at the hour of the window's last instant, one microsecond before to_ts, so a window ending exactly on the hour stays in one bin. Choose the bin from the window length. Bins much shorter than a window copy each window into many bins and inflate that side. Bins much longer leave many windows per bin and push the pair count back up. With 2-minute windows on even minutes, an hour bin copies nothing and leaves 30 windows per city-hour.
Spark joins 2 million sessions to a 3MB table of 50,000 campaign windows on s.ts BETWEEN c.start_ts AND c.end_ts, with default settings. Which operator runs, and how many pairs does it test?
- BroadcastNestedLoopJoin: the 3MB side is broadcast, and all 100 billion pairs are tested.
- BroadcastHashJoin: the windows are hashed, so each session costs one lookup, 2 million in all.
- CartesianProduct: every range join becomes one, testing all 100 billion pairs across tasks.
- BroadcastNestedLoopJoin: the 3MB side is broadcast, and each session is tested against its one window.
On the practice tables, the cell below pairs each product with every cheaper product in its category. Category equality is the join key, and the price comparison runs on pairs inside each category. Replace join(right, "category") with crossJoin(right) and run it again. The counts can only grow, and so does the work, since every product is now tested against every other.
About this lesson
This section is part of the Joins: Advanced 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.