Spark skipped the broadcast join you expected for a small filtered table — why, and what does forcing it risk?
Spark broadcasts a side only if its size estimate is under spark.sql.autoBroadcastJoinThreshold (10 MB). Without CBO statistics a filter keeps the input's estimate, so a filtered 2 GB table still looks 2 GB. AQE may switch at run time; a hint forces it, risking OOM.
- Assuming the threshold is checked against the real post-filter size
- Forcing
broadcast()on a side that later grows past executor or driver memory - Raising the threshold globally instead of fixing statistics or hinting one join
- How do
ANALYZE TABLEcolumn statistics change the planner's size estimate? - Why can't Spark broadcast the left side of a left outer join?
The planner compares the threshold with the plan's size estimate, not the real size. For a file-based table that is the file size; a Filter without CBO enabled (spark.sql.cbo.enabled, off by default) and without column statistics does not shrink that estimate. So a 2 GB table filtered down to a thousand rows still looks like 2 GB at planning time, and Spark picks a sort-merge join.
from pyspark.sql import functions as F
small = dims.filter(F.col("country") == "PT") # really ~1k rows, estimated at 2 GB
orders.join(small, "dim_id") # plan: SortMergeJoin
orders.join(F.broadcast(small), "dim_id") # plan: BroadcastHashJoin
spark.conf.set("spark.sql.autoBroadcastJoinThreshold", -1) # auto-broadcast offAQE (on by default since 3.2) sees the side's real size after the shuffle and can replace the sort-merge join with a broadcast-hash join at run time, but that costs more than a broadcast planned up front: part of the shuffle has already run. A hint bypasses the threshold entirely: if the side is actually large, the broadcast exhausts the memory of the driver that collects the table or of the executors that each hold a copy.