Dynamic Partition Pruning

Static partition pruning, which you have used, needs a predicate on the partition column itself. With store_sales written by partitionBy("store_id"), a filter store_id = 17 skips every other directory at planning time. Star-schema queries filter a dimension instead: they ask for stores where region = 'West', and at planning time Spark has no idea which store ids those are. Dynamic partition pruning, DPP, closes that gap. It turns the dimension's filtered keys into a filter on the fact table's partition column, evaluated at runtime, before the fact's directories are read.

Schema
  • store_sales: sale_id BIGINT PK, store_id INT, partition column FK, product_id INT, sale_date DATE, revenue DOUBLE
  • stores: store_id INT PK, store_name STRING, region STRING
  • store_sales.store_id references stores.store_id (N:1)

The sequence at runtime

The same broadcast feeds the join and the scan's partition filter, so the scan waits for the 12 store ids and then opens only their directories.
  • stores (source, 400 rows)
  • Filter region = West (transform, 12 stores)
  • Broadcast (transform, 12 store ids)
  • store_sales directories (storage, 400 partitions, 2TB)
  • Pruned scan (transform, 12 partitions read)
  • Broadcast hash join (transform, probe on store_id)
  • Revenue by store (consumer, 12 rows)
  • stores -> Filter region = West
  • Filter region = West -> Broadcast
  • Broadcast -> Pruned scan
  • store_sales directories -> Pruned scan
  • Broadcast -> Broadcast hash join
  • Pruned scan -> Broadcast hash join
  • Broadcast hash join -> Revenue by store

The fact table holds 2TB in 400 directories, and 12 stores sit in the West. The scan opens those 12 directories, about 61GB, and never touches the other 388. Without DPP the scan reads all 2TB, and the join discards 97% of the rows after paying to read and probe them.

from pyspark.sql import functions as F

sales = spark.table("store_sales")
stores = spark.table("stores")

west = (
    sales.join(stores.filter(F.col("region") == "West"), "store_id")
    .groupBy("store_name")
    .agg(F.sum("revenue").alias("revenue"))
)

One shuffle: the filtered stores is far under 10MB, so the join is a broadcast, and only the groupBy exchanges rows. That broadcast is what DPP reuses.

Where to confirm DPP ran

What DPP needs

Where DPP meets AQE

The optimizer places DPP in the plan before the job starts, as a placeholder that resolves when the broadcast arrives, and since Spark 3.2 it runs alongside AQE. DPP and AQE decide broadcasts at different moments. DPP looks for a broadcast in the plan as compiled, from estimates. If the filtered stores was estimated above the threshold and planned as a sort-merge join, the pruning filter is dropped, and a later AQE conversion to broadcast does not bring it back. Accurate table statistics, or F.broadcast() on the filtered dimension, put the broadcast into the compiled plan where DPP can reuse it. This is how a runtime value reaches a scan that AQE itself never touches.

Treat store_sales_raw as if it were written with partitionBy("store_id"), so each store id is a directory of its own. The cell joins it to stores and, for each region, counts the stores a filter on that region would keep and the sales rows it would read. Add the rows across regions for the full table, and each region's share is what DPP would read.

Read the output as a pruning ratio: a region's rows_read over the sum of rows_read across all regions is the share of the fact table its scan would open. A region holding most of the rows saves little even when it keeps few stores, because DPP prunes directories, and each skipped directory saves only the rows inside it. A filter that keeps every store saves nothing at all.

events is written with partitionBy("event_day"), and calendar is a 2KB table with columns event_day and is_holiday. Which query lets Spark prune events partitions at runtime?

About this lesson

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