
Predicate Pushdown vs. Partition Pruning in Spark
Partition pruning skips whole partitions. Predicate pushdown lets the Parquet reader skip data within selected files. In Spark, filter the partition column directly - for example, event_date = DATE '2026-11-30' - and use supported data-column filters, such as amount >= 100, to apply both.
The difference is where Spark can avoid work. Neither optimization guarantees faster queries, and a pushed filter can still leave Spark with rows to check.
Quick Comparison
| Check | Partition pruning | Predicate pushdown |
|---|---|---|
| Filter target | Partition column, such as event_date |
Data column, such as amount |
| Scope | Entire partitions and their files | Row groups or supported pages within files |
| Metadata | Partition paths and values | Parquet statistics |
| Plan field | PartitionFilters |
PushedFilters |
| Common limits | Missing partition discovery or no partition-column filter | Unsupported expressions or weak statistics |
Compare 3 query patterns: partition-only, pushdown-only, and combined. Read from the dataset root, check column types, and inspect explain("formatted"). Then compare files read, actual bytes read, and scan time under the same test conditions - plan fields show filters, not measured savings.
Spark Partition Pruning vs. Predicate Pushdown
Predicate Pushdown & Column Pruning in Spark Explained | PySpark Performance Tuning #10
sbb-itb-61a6e59
Predicate Pushdown vs. Partition Pruning: How They Work
Use the scan plan to tell directory skips apart from skips within Parquet files.
| Aspect | Predicate pushdown | Partition pruning |
|---|---|---|
| Execution layer | Parquet reader | Spark file index and partition discovery |
| Unit skipped | Row groups or pages within candidate files | Entire partition directories and their files |
| Prerequisites | Supported data-column expression and usable file statistics | Discoverable partitioned paths and a filter on the actual partition column |
| Metadata used | Parquet min/max statistics and null counts | Directory names and partition values |
| Plan fields | DataFilters and PushedFilters |
PartitionFilters |
| Limits | Unsupported expressions, UDFs, casts, or weak statistics may prevent effective skipping | Spark may still need to list files and discover partitions |
Neither technique guarantees a speedup. Mastering these optimizations is a core part of data engineering training. Metadata reads and small-file overhead can offset the savings.
Predicate Pushdown: Skip Data Within Parquet Files
events.filter(F.col("customer_id") == 42).explain("formatted")
customer_id = 42 should appear in PushedFilters, while PartitionFilters should stay empty.
DataFilters does not prove pushdown happened. It shows the full condition Spark still tracks. PushedFilters lists the predicates Spark passes to the Parquet reader. Simple equality and range comparisons are good candidates, but UDFs, unsupported functions, and some type conversions can leave Spark to evaluate the conditions itself.
Because customer_id is not a partition column, this filter targets data inside Parquet files - not directories. For customer_id = 42, Parquet can skip a row group if its statistics rule out a match. If the row group’s value range still overlaps the predicate, it remains a candidate.
Candidate files may still require footer reads, and missing or broad statistics limit the savings. Row-group elimination and optional record-level filtering are separate steps. A pushed predicate alone does not prove that either happened.
Partition Pruning: Skip Date Partitions
events.filter( F.col("event_date") == F.lit("2026-11-30").cast("date") ).explain("formatted")
event_date = 2026-11-30 should appear in PartitionFilters; PushedFilters may stay empty.
Spark uses partition metadata to resolve this filter rather than passing it to the Parquet reader. That lets Spark skip other dates’ directories before the reader reads their files.
Filter event_date directly, not just event_timestamp. A timestamp condition does not necessarily tell Spark which event_date directories it can skip. When the date restriction fits the query, add it directly to event_date and keep any timestamp condition needed to select the precise rows.
The next section compares partition-only, pushdown-only, and combined filters.
Compare Partition-Only, Pushdown-Only, and Combined Filters
Compare all three filter types on the same scan. Use the same DataFrame and projection in every query so filtering is the only change. Plan fields can vary by Spark version, source, statistics, and reader settings.
| Query condition | Expected PartitionFilters |
Expected PushedFilters |
Skipped scope | Expected impact |
|---|---|---|---|---|
Partition-only: event_date = DATE '2026-11-30' |
Predicate on event_date |
Usually none, except null checks or engine-specific filters | Other date partitions | More savings across many dates; November 30 files remain eligible |
Customer-only: customer_id = 42 |
Empty or absent | EqualTo(customer_id,42) when supported |
Data within files; no partition pruning | Savings depend on selectivity and statistics |
Combined: both conditions with AND |
Predicate on event_date, often with isnotnull |
EqualTo(customer_id,42), possibly with IsNotNull |
Partitions and data within surviving files | Greatest reduction when both predicates are selective and supported |
Run the 3 Filter Queries
Reuse events from the setup, reading from the partitioned dataset root. Keep the projection identical across all three queries.
from pyspark.sql import functions as F selected = ["event_date", "customer_id", "event_timestamp", "amount"] partition_only = ( events .filter(F.col("event_date") == F.lit("2026-11-30").cast("date")) .select(*selected) ) partition_only.explain("formatted") customer_only = ( events .filter(F.col("customer_id") == 42) .select(*selected) ) customer_only.explain("formatted") combined = ( events .filter( (F.col("event_date") == F.lit("2026-11-30").cast("date")) & (F.col("customer_id") == 42) ) .select(*selected) ) combined.explain("formatted")
These calls inspect plans only. They don’t measure runtime savings.
Read the Scan Plan Fields
The combined query should show directory pruning and pushed predicates in the same scan. This example shows what to look for - not exact output to match character for character.
FileScan parquet PartitionFilters: [isnotnull(event_date), (event_date = 2026-11-30)] PushedFilters: [IsNotNull(customer_id), EqualTo(customer_id,42)] DataFilters: [isnotnull(customer_id), (customer_id = 42)] ReadSchema: struct<customer_id:bigint,event_timestamp:timestamp,amount:decimal(12,2)>
Check PartitionFilters for the date predicate and PushedFilters for the customer predicate. DataFilters alone doesn’t prove pushdown, and a residual Filter can remain.
ReadSchema leaves out event_date because Spark gets its value from the directory path. These fields show what Spark plans to do, not how much work it skips. Check bytes read in the execution metrics to verify savings.
Verify Scan Savings and Find Missing Filters
After checking the scan plan, confirm whether Spark reduced file reads and scan time. The plan shows what Spark intends to read; the Spark SQL UI shows what it actually read. Save each query’s complete explain("formatted") output. Location shows the scan source, not runtime file savings.
Check Partition Discovery and Predicate Support
The partition-only query should prune partitions. The customer-only query should push down its filter. The combined query should do both.
If PartitionFilters is missing, check the read path, partition discovery, and event_date type. A leaf-path read may need basePath. Compare a direct event_date condition with to_date(event_timestamp) for the same target date: Spark may not infer the partition predicate from the timestamp expression.
If PushedFilters is missing, check UDFs, complex comparisons, casts, and null handling. Test an equivalent simple comparison with matching types. Not every function or cast blocks optimization. Also check spark.sql.parquet.filterPushdown, whose documented default is true. Enabling it doesn’t make every predicate supported, so keep the original filtering logic for correctness.
Use these checks to explain why a query lost partition pruning or pushdown before comparing runtime metrics.
Measure Files, Bytes, and Scan Time
Use the Spark SQL UI and stage details to measure each scan. Keep the input snapshot, projection, cluster settings, cache conditions, and data-consuming action identical. Choose a read that materializes the selected columns; count() can understate scan cost. If you write to a temporary output path, elapsed time also includes that output work. Record unavailable metrics as N/A rather than inferring values.
Record the following metrics for partition-only, pushdown-only, and combined scans.
| Query variant | Selected partitions/files | Planned file size | Actual bytes read | Scan time | Elapsed time | Notes |
|---|---|---|---|---|---|---|
| Partition-only | ||||||
| Pushdown-only | ||||||
| Combined |
Planned file size is not actual bytes read. Weak Parquet statistics can limit skipping even when pushdown succeeds. Repeat runs on a larger controlled dataset, then compare medians across cold- and warm-cache runs.
Conclusion: Use Both and Check the Results
Partition pruning skips entire directories before Spark opens files. Predicate pushdown sends supported filters into the remaining Parquet files.
Test the same three query patterns: partition-only, pushdown-only, and combined. Save each EXPLAIN FORMATTED output and check PartitionFilters, PushedFilters, and ReadSchema. Record files read, bytes read, and scan time to show which optimization applied and what it changed.
FAQs
How should I choose partition columns for my queries?
Start with frequent query patterns: look at common filters and join keys. For partitioning, choose low-cardinality columns - such as dates, regions, or categories - that often appear in WHERE clauses. This lets partition pruning skip data the query doesn’t need.
Reference partition columns directly:
WHERE date_col >= '2026-01-01'
Avoid wrapping those columns in functions that prevent pruning. Keep partition sizes balanced, typically between 1 GB and 100 GB.
Does sorting Parquet data improve predicate pushdown?
Yes. Sorting Parquet data improves performance by helping readers skip more data. Partition pruning skips entire folders, while sorting within partitions narrows the minimum and maximum values recorded in metadata. These tighter ranges help the storage reader skip data blocks it doesn’t need. That means less data scanned and less I/O, especially for range and inequality predicates.
Can OR conditions prevent partition pruning or pushdown?
Yes, query conditions can prevent partition pruning. Wrapping a partition column in a function - for example, WHERE YEAR(date_col) = 2024 - can stop the optimizer from recognizing the partition key. That can lead to a full table scan.
Use direct comparisons against partition columns, such as WHERE date_col >= '2024-01-01', to help Spark skip partitions it doesn’t need to read.