Spark plans with guesses. AQE re-plans at every shuffle with real sizes, and DPP skips partitions a join rules out.
The optimizer plans the whole query before reading a byte, from estimates: file sizes plus whatever catalog statistics exist. Two guesses go wrong here. customers is 2 GB on disk and only 3 MB once filtered to New Zealand, but the optimizer doesn't know how selective the filter is, so the estimate stays far above 10 MB and Spark plans a sort-merge join that shuffles all the orders.
The final aggregation gets spark.sql.shuffle.partitions = 200 partitions for what turns out to be ~400 MB: 200 tasks of 2 MB, mostly scheduling overhead. A static plan can't know either fact; a plan that adapts while running can.
nz = customers.filter(F.col("country") == "NZ") # 2 GB -> ~3 MB
report = (orders.join(nz, "customer_id")
.groupBy("product_id")
.agg(F.sum("amount").alias("revenue")))
report.explain()
# AdaptiveSparkPlan isFinalPlan=false
# +- HashAggregate(keys=[product_id], functions=[sum(amount)])
# +- Exchange hashpartitioning(product_id, 200)
# +- ... SortMergeJoin [customer_id], [customer_id], InnerReach the last step for the run summary: the key takeaways from this lesson.