Query Plan Optimization: Column Pruning And Filter Pushdown
Asked of: Software Engineer
Last updated

What's being tested
Two core skills: implementing column pruning and filter pushdown on a query-plan tree, and writing correct, efficient tree rewrites. Interviewers check recursive AST traversal, tracking required columns and predicates across Scan, Project, Filter, and Join nodes, plus sound handling of expression lineage and semantics.
Patterns & templates
-
Bottom-up recursion: compute required columns for each node from its parent, revisit children with that set; linear time
O(nodes)for fixed-size expressions. -
Predicate classification: split predicates into pushable (refer only to one child) and residual; push pushables down early to reduce rows.
-
Column projection: at
Scanemit only needed physical columns; atProjectrewrite expressions to map child columns, prune unused projections. -
Join handling: push predicates that reference only one side into that side; predicates referencing both become join conditions. Handle outer joins conservatively.
-
Expression substitution: maintain a mapping from parent output to child expressions for safe rewrite; avoid re-evaluating complex expressions.
-
Immutability & CAS: return new nodes (functional updates) so rewrites are testable; memoize visited nodes to avoid exponential work.
Common pitfalls
Pitfall: Pushing a predicate that references columns produced by a
Projectwithout rewriting to the underlyingScannames — causes invalid plans.
Pitfall: Ignoring
NULLsemantics on outer joins and pushing predicates that change result cardinality.
Pitfall: Over-pruning columns used inside non-project side-effects (e.g.,
EXISTS,ORDER BYexpressions) — leads to incorrect answers.
Practice these
The practice cards below cover the canonical variants — solve all of them and time yourself.
Practice questions
Related concepts
- Distributed Query Execution, Shuffle, And Skew
- Apache Spark Execution And DataFrame Fundamentals
- SQL Analytical Querying And Data ModelingData Manipulation (SQL/Python)
- Dynamic Programming And Mutable Range QueriesCoding & Algorithms
- SQL Joins, Aggregations, And Deduplication
- SQL Product AnalyticsData Manipulation (SQL/Python)