60. Pick the Join Strategy
Difficulty: Medium · Topics: Spark Performance, Joins, Conditional Logic
tables lists table sizes (name, size_mb). joins lists planned joins: join_id, left_table, right_table, join_type ('inner', 'left', 'right' or 'full').
Decide the strategy Spark would pick with a broadcast threshold of 10 MB:
'broadcast right'if the right table is at most 10 MB and the join type isinnerorleft, and, for inner joins, the right table is not larger than the left- otherwise
'broadcast left'if the left table is at most 10 MB and the join type isinnerorright - otherwise
'sort-merge'
Return join_id and strategy, sorted by join_id.
Row order matters for this problem. Your code is graded on 3 test cases, including hidden edge cases.
Sample data
tables
| name | size_mb |
|---|---|
| orders | 50000 |
| customers | 8000 |
| countries | 1 |
| currencies | 0.5 |
| promos | 9 |
joins
| join_id | left_table | right_table | join_type |
|---|---|---|---|
| 1 | orders | countries | inner |
| 2 | orders | customers | left |
| 3 | countries | orders | left |
| 4 | promos | orders | right |
| 5 | orders | currencies | full |
| 6 | currencies | promos | inner |
Expected output
| join_id | strategy |
|---|---|
| 1 | broadcast right |
| 2 | sort-merge |
| 3 | sort-merge |
| 4 | broadcast left |
| 5 | sort-merge |
| 6 | broadcast left |
Hints
Hint 1
Jointables twice, once for each side; rename its columns first so the two copies do not clash.Hint 2
The rule for outer joins: the side whose unmatched rows must be kept cannot be broadcast. A full outer join cannot broadcast either side.Hint 3
Build the result with awhen chain in the order of the rules.Learn the concepts
- Broadcast joins · 6 min read. Ship a small table to every executor and skip the shuffle, and when broadcasting backfires.
- Join strategies · 8 min read. Broadcast hash, shuffle hash, sort-merge and nested loop joins: how Spark picks one, and how to steer it.
- Adaptive Query Execution · 7 min read. Spark re-plans a running query from real statistics: coalescing, join switching and skew splitting.
PySpark functions you'll practise
- join
- when / otherwise
- select
- orderBy
- isin
Related problems
- Find Hot Keys and Plan Salting · Hard · Spark Performance, Joins, Aggregations
- How Much Does Each Query Read? · Medium · Data Lake, Joins, Aggregations
- Split Train and Test With No User in Both · Medium · Data Prep for AI, Joins, Aggregations
- Parse UTM Parameters · Medium · Strings, Conditional Logic, Filtering & Selection
- Customers Without Orders · Medium · Joins, Filtering & Selection
Browse
Topics: Window Functions · Joins · Aggregations · Pivot, Unpivot & Rollup · Arrays · Null Handling · Conditional Logic · Dates · Filtering & Selection · Strings · Data Lake · Lakehouse · Streaming · Data Prep for AI · Spark Performance
Difficulty: Easy · Medium · Hard · Learn · All problems · dbt practice