Jump to content

Dynamic partition pruning

From Open Lakehouse Wiki
Dynamic partition pruning
Also known as DPP, runtime partition pruning
Field Query optimization
Key parts Partitioned fact table, filtered dimension table, equi-join key on a partition column, broadcast reuse or a pruning subquery
Open source implementations Apache Spark (since 3.0.0), Apache Iceberg and other Data Source V2 connectors through runtime filtering
Related Partition pruning, Runtime filter, Broadcast join, Predicate pushdown, Star schema
Last verified 23 November 2026 against the v4.3.0 tag of apache/spark (PartitionPruning.scala, PlanDynamicPruningFilters.scala, CleanupDynamicPruningFilters.scala, DynamicPruning.scala, joins.scala, predicates.scala, SQLConf.scala, SupportsRuntimeV2Filtering.java), Spark JIRA SPARK-11150, and the Apache Iceberg 1.12.0 Spark source.

Dynamic partition pruning (DPP) is a query optimization in Apache Spark that skips partitions of a table using filter values that are only known while the query runs, typically the join keys that survive a filter on the other side of a join. It targets star-schema queries, where a large fact table partitioned by a key is joined to a small, filtered dimension table. DPP was added in Spark 3.0.0 (SPARK-11150) and is on by default.[1][2]

Why is dynamic partition pruning needed?

Static partition pruning needs the filter on the partitioned table itself. In a star-schema query the filter is on the dimension table, and the fact table is constrained only through the join:

-- sales is partitioned by date_key; dim_date is small
SELECT d.calendar_month, SUM(s.amount)
FROM sales s
JOIN dim_date d ON s.date_key = d.date_key
WHERE d.is_holiday = true
GROUP BY d.calendar_month;

Without DPP, Spark reads every partition of sales and discards non-holiday rows at the join. With DPP, Spark first evaluates the filtered dim_date, collects its date_key values, and scans only the sales partitions with those keys.

How does dynamic partition pruning work in Apache Spark?

DPP is an optimizer rule, PartitionPruning. When a join has an equality condition between a partition column on one side and a column on the other side, the rule inserts a DynamicPruningSubquery filter on the partitioned side. The subquery holds the filtering side of the join, and Spark decides during physical planning how to run it.[3]

  1. If the join is planned as a broadcast hash join, Spark reuses the broadcast of the filtering side as the pruning filter, so the dimension table is read only once.
  2. Otherwise, if the estimated benefit outweighs the cost of running the filtering side twice, Spark keeps a separate subquery.
  3. Otherwise, Spark drops the pruning filter.[3][4]

With the defaults, step 2 does not happen. spark.sql.optimizer.dynamicPartitionPruning.reuseBroadcastOnly is true, so DPP applies only when a broadcast exchange can be reused.[2] In practice DPP depends on the dimension side being small enough to broadcast.

The benefit estimate compares the size of the partitioned side after pruning with the size of the filtering side. It uses column statistics when they exist (spark.sql.optimizer.dynamicPartitionPruning.useStats, default true) and otherwise a fallback filter ratio (spark.sql.optimizer.dynamicPartitionPruning.fallbackFilterRatio, default 0.5).[3][2]

When does dynamic partition pruning apply?

The rule at Spark 4.3.0 inserts a pruning filter only when all of the following are true:[3]

  • The join condition contains an equality (=) between the two sides.
  • The key on the pruned side traces back to a partition column of a file source or Hive table, or to an attribute that a Data Source V2 scan accepts for runtime filtering.
  • The join type allows pruning that side. Inner and left semi joins can prune either side; a left outer join can prune only its right side, and a right outer join only its left side.[5]
  • The filtering side is not a streaming plan, and it contains a predicate that is likely to be selective (a comparison, IN, LIKE, a string predicate and similar) or a small input that is already materialized.[3][6]

When does dynamic partition pruning not apply?

Common reasons a star join gets no DPP:

  • The fact table is not partitioned on the join key. DPP prunes partitions, not files or row groups. Joins on non-partition columns can use a runtime filter instead.
  • The dimension side has no selective filter. Without a filter, every key survives, so there is nothing to prune.
  • The dimension side is not broadcast. With reuseBroadcastOnly at its default (true), a sort-merge join gets no DPP.[2]
  • The join type is wrong for the side. For example, the preserved side of an outer join (the left side of a left outer join) cannot be pruned.[5]
  • The filtering side is non-deterministic. If the filtering plan is non-deterministic, Spark replaces the dynamic predicate with true before physical planning.[3]
  • DPP is turned off with spark.sql.optimizer.dynamicPartitionPruning.enabled=false.[2]

What does dynamic partition pruning look like in a plan?

In the physical plan, the scan of the partitioned table lists the dynamic filter in its PartitionFilters, wrapped in dynamicpruningexpression(...). With broadcast reuse, the filter refers to a SubqueryBroadcast node that reads the broadcast built for the join, and the reuse rule turns the duplicate broadcast exchange into a ReusedExchange node.[7][8][9] After the query runs, the scan's number of partitions read metric in the Spark UI's SQL tab shows how many partitions were read.[10]

Dynamic partition pruning with table formats

Data Source V2 tables take part in DPP through the SupportsRuntimeV2Filtering interface (since Spark 3.4.0), which lets a scan drop input partitions using the runtime predicate.[11] Apache Iceberg's Spark batch scan implements it, so DPP prunes Iceberg partitions, including hidden partitions derived from a source column.[12]

Dynamic partition pruning vs runtime filter

DPP and the Spark runtime filter both push information from one side of a join to the other at run time. DPP removes whole partitions at the scan and needs a partition column as the join key. The runtime filter, added in Spark 3.3.0, inserts a bloom filter on any join key to reduce the rows that go into a shuffle, and Spark does not add one where DPP already applies.[2][13]

See also

References

  1. ↑ SPARK-11150: Dynamic partition pruning. Apache Software Foundation.Retrieved 2026-11-23.
  2. ↑ 2.0 2.1 2.2 2.3 2.4 2.5 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  3. ↑ 3.0 3.1 3.2 3.3 3.4 3.5 PartitionPruning.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  4. ↑ PlanDynamicPruningFilters.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  5. ↑ 5.0 5.1 joins.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  6. ↑ predicates.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  7. ↑ ReuseExchangeAndSubquery.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  8. ↑ DynamicPruning.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  9. ↑ SubqueryBroadcastExec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  10. ↑ DataSourceScanExec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  11. ↑ SupportsRuntimeV2Filtering.java (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
  12. ↑ SparkRuntimeFilterableScan.java (apache-iceberg-1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
  13. ↑ InjectRuntimeFilter.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.