KatInterview

Rewrite a Relational Plan: Sink Filters and Trim Columns

Trees & GraphsModel answer
HardModel answerTrees & Graphstree-rewritingquery-optimizationpredicate-pushdown

Last updated

Transform a tree of Scan, Filter, Project and Join operators by moving filters toward the leaves and removing unused columns, while keeping query results unchanged.

Rewriting a Relational Query Plan

A query engine stores a plan as a tree of operators. Four operator kinds exist:

  • Scan(table_name, columns): a leaf that reads a table and exposes the listed columns.
  • Filter(predicate, child): keeps only rows that satisfy the predicate.
  • Project(columns, child): keeps only the listed columns.
  • Join(condition, left, right): an inner join of two inputs. It is the only node with two children.

The node class is:

class Node:
    def __init__(self, type, children=None, **kwargs):
        self.type = type
        self.children = children or []
        self.kwargs = kwargs  # columns, predicate, condition, table_name, ...

Task

Write optimize_plan(root) that returns the root of an equivalent but cheaper plan, by applying two rewrites in order:

  1. Predicate pushdown. If a Filter sits above a Join and its predicate only mentions columns from one join input, move the filter into that input, and keep sinking it as deep as is legal.
  2. Projection pruning. Going from the root toward the leaves, drop every column that no ancestor outputs and no operator on the way (filter predicate, join condition) evaluates.

Example

Project([a, b],
  Filter(a > 10,
    Join(c == d,
      Scan(T1, [a, c]),
      Scan(T2, [b, d]))))

Column a exists only in T1, so after optimization the filter must be applied on the T1 side, below the join. Columns c and d are needed by the join condition, so they must survive down to the scans.

Assumptions

  • Column names are globally unique across tables.
  • A predicate or condition is a short string such as "a > 10"; the columns it references can be extracted by tokenizing identifiers.
  • The result must produce exactly the same rows and columns as the input plan.

What to deliver

Describe your approach, justify why each rewrite preserves results, give the complexity, list the edge cases you handle, and write the code.

Hints

Hint 1

Start with a helper that computes the output columns of any node: Scan and Project give their own list, Filter passes through its child's columns, Join concatenates left and right.

Hint 2

For pushdown, recurse into the children first, then, at each Filter, try to sink it: into the left input if its columns are a subset of the left schema, into the right input if a subset of the right schema, through another Filter freely, and through a Project only if the projection keeps every column the predicate needs. Otherwise leave it where it is.

Hint 3

For pruning, carry a 'required columns' set down from the root. Each Filter adds its predicate's columns, each Join adds its condition's columns, each Project narrows the set to its own list. At a Scan, keep only required columns. Run pushdown first so pruning sees filters in their final positions.