Partition pruning
| Also known as | Partition elimination, partition filtering |
|---|---|
| Field | Query optimization |
| Key parts | Partition columns or transforms, partition filters, catalog or table metadata, runtime filters |
| Open source implementations | Apache Spark, Apache Iceberg, Delta Lake, Apache Hive, Trino |
| Related | Partition, Dynamic partition pruning, Predicate pushdown, Column pruning, Bucketing |
Partition pruning is a query optimization in which an engine uses the filters of a query to skip whole partitions of a table, so that the files in partitions that cannot match are never listed or read. In Apache Spark it comes in two forms: static partition pruning, where the filter on a partition column is known when the query is planned, and dynamic partition pruning, where the filter values are only known at run time, usually from the other side of a join. Table formats such as Apache Iceberg and Delta Lake do the pruning from their metadata instead of from directory names.
What does partition pruning skip?
Partition pruning works at the coarsest level of data skipping. It removes whole table partitions before any file is opened, while predicate pushdown into the file format skips row groups and pages inside the files that remain. For built-in file sources, Spark sorts the filters of a scan into partition filters, which prune directories; bucket filters, which prune bucket files; data filters, which are pushed to the file reader; and filters that must be evaluated again after the scan.[1]
Static partition pruning
Static partition pruning applies when a query filters a partition column with a value known at planning time:
-- sales is partitioned by sale_date
SELECT store_id, SUM(amount)
FROM sales
WHERE sale_date >= DATE'2026-11-01'
GROUP BY store_id;
For a Hive-style table, each partition is a directory such as sale_date=2026-11-01/, and Spark lists only the directories whose values satisfy the filter. For tables stored in a Hive metastore, spark.sql.hive.metastorePartitionPruning (default true, since 1.5.0) pushes partition predicates into the metastore, so that Spark fetches only the matching partition metadata.[2]
Spark uses a filter for partition pruning when it is deterministic and refers only to partition columns; an expression over a partition column, such as year(sale_date) = 2026, still qualifies.[1] A filter on a different column does not, even when it is related: WHERE sale_ts >= TIMESTAMP '2026-11-01 00:00:00' on a table partitioned by a separate sale_date column prunes nothing, because Spark does not know how the two columns relate. Iceberg's hidden partitioning addresses this case (see below).
Dynamic partition pruning
Dynamic partition pruning (DPP) prunes partitions using values that Spark learns while the query runs. In a join of a partitioned fact table with a filtered dimension table, Spark uses the join keys that survive the dimension filter as a partition filter on the fact table. DPP was added in Spark 3.0.0 (SPARK-11150) and is on by default through spark.sql.optimizer.dynamicPartitionPruning.enabled.[2][3]
Data Source V2 connectors take part in runtime pruning through SupportsRuntimeFiltering (since Spark 3.2.0) and SupportsRuntimeV2Filtering (since 3.4.0), which let a scan drop planned input partitions using predicates Spark infers during execution.[4][5]
Static vs dynamic partition pruning
| Static partition pruning | Dynamic partition pruning | |
|---|---|---|
| When the filter is known | At planning time, from a literal or constant expression | At run time, from the result of another part of the query |
| Typical query | WHERE sale_date = '2026-11-01'
|
JOIN dim_date d ON f.sale_date = d.date WHERE d.is_holiday
|
| Shown in Spark plans as | A plain predicate in PartitionFilters
|
dynamicpruningexpression(...) in PartitionFilters, fed by a subquery or a reused broadcast
|
| Setting | Always on for partition columns | spark.sql.optimizer.dynamicPartitionPruning.enabled (default true)[2]
|
What to look for in a Spark plan
EXPLAIN FORMATTED prints the metadata of each scan. For a built-in file source, the scan node lists PartitionFilters, PushedFilters, DataFilters and ReadSchema.[6][7]
- Static pruning worked if the partition predicate appears in
PartitionFilters, for examplePartitionFilters: [isnotnull(sale_date#12), (sale_date#12 >= 2026-11-01)]. - Static pruning did not happen if
PartitionFiltersis empty and the predicate shows up only inDataFiltersor in aFilterabove the scan. Common causes are a filter on a column that is not a partition column, a non-deterministic filter, or a table that Spark does not see as partitioned. - Dynamic pruning was planned if
PartitionFilterscontainsdynamicpruningexpression(...). Spark represents a planned dynamic filter with theDynamicPruningExpressionmarker.[8]
After the query runs, the scan node in the Spark UI's SQL tab shows the metrics number of files read and, for partitioned tables, number of partitions read, which measure pruning directly.[6][9]
Partition pruning in table formats
Apache Iceberg prunes from metadata rather than directory names. Each data file records the partition values it belongs to, and partitions are derived from source columns by transforms such as day, bucket[N] or truncate[W].[10] A filter on the source column, such as WHERE sale_ts >= TIMESTAMP '2026-11-01 00:00:00' on a table partitioned by day(sale_ts), therefore prunes partitions without the query naming a partition column; this is called hidden partitioning. Iceberg's Spark batch scan also implements SupportsRuntimeV2Filtering, so it takes part in dynamic partition pruning.[11]
Delta Lake records the partition values of each data file in the partitionValues field of its add action in the transaction log, so a reader prunes partitions by filtering the log instead of listing directories.[12] The same add actions can carry per-file column statistics, which Delta uses to skip files inside a partition.[12]
See also
References
- ↑ 1.0 1.1 FileSourceStrategy.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 2.0 2.1 2.2 SQLConf.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SPARK-11150: Dynamic partition pruning. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SupportsRuntimeV2Filtering.java (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SPARK-35779: Support dynamic filtering for v2 tables. Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 6.0 6.1 DataSourceScanExec.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ FileScan.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ DynamicPruning.scala (v4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Web UI (Spark 4.3.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ Iceberg Table Spec (apache-iceberg-1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ SparkRuntimeFilterableScan.java (apache-iceberg-1.12.0). Apache Software Foundation.Retrieved 2026-11-23.
- ↑ 12.0 12.1 Delta Transaction Log Protocol (v4.4.1). Delta Lake.Retrieved 2026-11-23.