Verifying with explain()

Concepts covered: Broadcast Join

Code shows what you asked Spark to do. The physical plan shows what it decided, and they can differ: a hint can be dropped, an estimate can fall on the other side of 10MB, and a join you expected to broadcast can arrive as a sort-merge. explain() prints the physical plan without running the query, so you can check before spending an hour of cluster time.

trips_with_drivers = trips.join(F.broadcast(city_drivers), "driver_id")
trips_with_drivers.explain()

Here is that plan, trimmed, beside the plan for the same join without the hint, where the estimate sent it to sort-merge. Read each tree from the bottom up: the scans are the leaves, :- marks a node's first child and +- its last.

4 checks on every join line

Inner after the keys is the join type. Each Sort under the sort-merge join orders one side within its partitions, after that side's exchange. The isnotnull(driver_id...) filters are ones Catalyst added: an inner join can never match a NULL key, so those rows are dropped at the scan.

The line above the join

A recent Spark prints one more line at the top of the plan, beginning with AdaptiveSparkPlan. It wraps the plan and is covered in a later lesson. The join operators beneath it read the same way.

Catching a dropped hint

report = F.broadcast(regions).join(orders, "region_id", "left")
report.explain()

The code asks for regions to be broadcast, but a left outer join preserves its left side, so Spark cannot build regions and discards the hint with a warning on the driver. The plan's join line reads SortMergeJoin [region_id#3], [region_id#9], LeftOuter, with an Exchange hashpartitioning under each side. To get every order with its region, write orders.join(F.broadcast(regions), "region_id", "left"): that join type can build the right side, and the plan shows BroadcastHashJoin ... LeftOuter, BuildRight. To keep every region, including those with no orders, the original join is correct and sort-merge is its strategy.

The plan that ran

explain() prints the plan Spark intends to run. After a query has run, the SQL tab of the Spark UI draws the plan it executed as a graph of the same operators, with metrics on each. The BroadcastExchange node reports the data size actually broadcast, the measured number the previous section asked for, along with the time spent collecting the rows and the time spent building and broadcasting the table. Each Exchange node under a sort-merge join reports the bytes its side wrote to the shuffle.

Running explain() on events.join(users, "user_id") prints this join line: BroadcastHashJoin [user_id#4], [user_id#19], Inner, BuildLeft, false. Which DataFrame was broadcast?

Make the plan the last check before a join goes to production. The operator and its build side tell you which strategy will run, and the count of exchanges tells you how many shuffles the join will pay for. The final section uses those counts to order several joins.

About this lesson

This section is part of the Joins: 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.