Course outline

Apache Spark / Intermediate: distribution and execution evidence / Lesson 14

Partition pruning and filter evidence

Distinguish a logical filter from observed partition and data-source read behavior.

About 3 min readContent reviewed
On this page

Lesson goals

  • Explain the goal of partition pruning and predicate pushdown.
  • Compare plans, input metrics and output correctness for a bounded query.

Start with a correct filter

A query’s logical predicate defines which rows should appear; its physical plan determines how the engine attempts to read and filter them. Partition pruning can avoid reading irrelevant partition directories when the layout and predicate permit it. Predicate pushdown can delegate supported filters to a data source. Neither optimization should be assumed from the SQL text alone, and exact behavior depends on the source and Spark version.

Inspect a synthetic layout

This fixture describes two date partitions and requests November 5. It is a data-layout sketch, not a Spark configuration or an executed query. Check the actual partition column type, file layout and reader configuration before comparing it with a plan. A timestamp converted in an incompatible timezone can select the wrong business interval even when pruning appears effective.

json Two synthetic date partitions
{"partition_column":"event_date","available_dates":["2026-11-04","2026-11-05"],"requested_date":"2026-11-05"}

The requested date identifies the intended partition under the fixture’s simple contract.

Separate expectation from observed plan

The second fixture records an expectation that the November 5 partition is selected. Inspect the actual physical plan and scan metrics in a disposable run rather than treating that expectation as proof. A pushed filter can still require a residual filter, and a plan can change with configuration or adaptive execution. Record selected files, bytes and output rows where the source exposes them.

json Expected candidate partition
{"candidate_partitions":["2026-11-05"],"note":"expected selection, not an observed Spark plan"}

The JSON names an expected selection and makes no claim that Spark executed it.

Compare the same workload

Before optimizing, capture the source snapshot, query, relevant settings, plan and representative input size. Change one bounded factor and compare scan metrics and runtime under comparable conditions. Independently reconcile output keys and aggregates with a known fixture. A faster query that omits boundary records or applies a different timezone does not satisfy the original data contract.

Test boundary and fallback cases

In an isolated Spark session, build a small partitioned fixture containing records immediately before and at the requested boundary. Compare the query result with an independently calculated expectation and inspect the plan and actual scan evidence. Test a predicate that the source cannot push down and observe the difference. Report the result only for the tested source, release and configuration.

Common errors and fixes

A filter in SQL is assumed to prove partition pruning.

Why it happens
Logical selection was confused with the physical scan.
How to fix it
Inspect the plan and source scan metrics.
Prevent it next time
Retain plan evidence for representative queries.

A faster filter drops events at a date boundary.

Why it happens
Timezone or endpoint semantics changed.
How to fix it
Restore the agreed interval and reconcile affected records.
Prevent it next time
Test records on both sides of the boundary.

A pushed filter is assumed to eliminate all later filtering.

Why it happens
Source and residual predicate behavior were not examined.
How to fix it
Inspect the complete plan and output.
Prevent it next time
Test supported and unsupported predicates separately.

2 practice exercises

1. The fixture requests November 5. Does the JSON prove Spark read only that partition?

Show worked solution

No. It states an expected candidate selection. Inspect the executed plan and scan metrics for the actual query and source, and verify the selected rows.

2. The optimized query is faster but misses a midnight event. Is it acceptable?

Show worked solution

No. Compare timezone and interval boundaries with the business contract, restore the missing record and remeasure only after correctness is established.

Key takeaways

  • Logical filters and physical scan optimizations are separate evidence.
  • Measure scan cost and verify output correctness under the same contract.

Official references