Bind produces scopes and expressions; execution needs a plan tree. The conversion in between decides whether a query merely runs or runs fast.
This is part two of the plan series: how the plan tree is built, and the two optimizations that matter most.
1. The minimal plan node
type LogicalOperator struct {
Typ LOT // scan / filter / project / join / agg / order / limit
Children []*LogicalOperator
// node-specific extra fields
}
Generation follows the bind order:
from → where → agg/group by → having → project → order by → limit
Each step attaches bind’s output to the right node: from builds scans/joins, where builds filters, group by builds aggregates, and so on.
2. Filter pushdown: filter as early as possible
One sentence: the closer a filter is to the scan, the less data is read.
plan’s signature is “tree in + filters to push, new tree + filters that didn’t push out”:
func (b *Builder) pushdownFilters(root *LogicalOperator, filters []*Expr) (*LogicalOperator, []*Expr)
Rules differ per node:
| Node | Rule |
|---|---|
| scan | keep filters referencing this table only; return the rest |
| join | split into left-only / right-only / both; both-reference may become a join condition |
| agg | filters referencing aggregates cannot push down; keep them on the agg |
| project | substitute indirect references first, then try to push |
| filter | merge with filters from the parent; drop the node if everything pushed |
| limit | never push |
The easy mistake is outer joins: pushing a left-only filter into the left subtree can change “filter then join” into “join then filter”. plan only allows left-table filters to push through a left join, and avoids rewriting both-reference filters.
3. Join ordering: greedy over a graph
Different join orders can differ by orders of magnitude in intermediate size. plan builds a graph from filters and solves greedily:
- Extract base relations (
SingleJoinRelation); - Collect join conditions from filters;
- Build undirected edges from each filter’s left/right expressions; nodes are relation-id sets;
- Create a leaf node per relation and estimate cardinality;
- Solve greedily into a complete plan;
- If the graph is disconnected, add cross products and re-solve.
The key: the graph holds relation ids, not table names. “Which tables must be joined together” becomes a pure graph problem, independent of the tables themselves.
4. Column pruning and reference counting
Neither changes results; both reduce data movement:
- Column pruning: keep only columns actually referenced by the plan;
- Reference counting: how many times each column is referenced, which decides whether it can be modified in place.
A quick check: run a query that touches two columns and look at how many columns the scan emits.
5. A verifiable checklist
| Check | Expectation |
|---|---|
Does where a.x = 1 appear above the scan? | yes (pushed down) |
| Is a right-table filter pushed into the left subtree of a left join? | no |
| A 2-column query: does the scan emit 2 columns? | yes |
| Does a 3-table join produce a cross product? | normally no |
If the second is violated, the result can be wrong, not just slow — that’s what makes pushdown dangerous.
6. Next
With a plan, execution comes next: vectors, chunks and operators — and where performance comes from once correctness is settled.
Series: Database kernel · Project page
Get the next post
New posts on database kernels and engineering.