The previous post covered gporca’s DP cells. This one covers rules: xforms/ contains 119 files, roughly 44K lines. At first it looks like required reading — it isn’t. Rules are highly homogeneous; understanding three of them teaches you a whole class.

1. There are only three kinds of rules

KindRoleExamples
TransformationProduce equivalent logical expressions inside the same groupJoin commutativity, associativity, predicate pushdown
ImplementationTurn a logical expression into an executable physical operatorHashJoin, IndexScan, Sort
EnforcerInsert operators to satisfy parent requirementsSort (order), Motion (distribution)

Search works like this: transformations grow groups, implementations propose candidates, DP picks the cheapest per group.

2. Anatomy of a rule

Take join commutativity. A rule has exactly four parts:

pattern   : match (InnerJoin, two inputs)
condition : (optional) extra premise, e.g. neither input is nullable
substitute: produce (InnerJoin, right, left)
promise   : estimated number of new expressions (used for scheduling priority)

In code: CXform::Transform runs PatternMatch, then condition(), then next_substitute() builds a new expression and inserts it back into the memo via PgroupInsert.

Key detail: pattern matching only looks at operator type and arity, not arguments. Arguments are moved by next_substitute. That’s why rule code is mostly “take input i, construct the operator, copy arguments”.

3. How a rule changes the memo

After a transformation fires, three things happen:

  1. next_substitute builds a new EXPR;
  2. It is converted into an M_EXPR (children folded into group references) and inserted into the same group;
  3. Duplicates are discarded — otherwise the group grows without bound.

Implementation rules differ: they produce physical M_EXPRs, trigger costing, and may update the group’s winner.

4. Rule sets and search stages

gporca doesn’t “open all rules at once”. It uses search stages:

Stage 1: exploration rules only (join reordering, pushdown)   → widen the space
Stage 2: implementation rules only                            → produce physical plans
Stage 3: (optional) explore again with more transformations   → refine

Each stage has a CXformSet deciding which rules may fire. That’s why “turn everything on” is not automatically better: more rules mean a larger space, more cost, and possible explosions.

5. A practical reading order

Don’t start at line one of xforms/. Read in this order:

  1. Joins first: CXformExpandNAryJoin, CXformInnerJoinCommutativity, CXformInnerJoinAssociativity — understand why join reordering explodes the space;
  2. Then pushdown: CXformPushFilterThroughJoin, CXformPushJoinBelowJoin — how predicates reshape groups;
  3. Then implementations: CXformImplementHashJoin, CXformImplementIndexScan — where physical candidates come from;
  4. Finally enforcers: Sort / Motion injection.

For each file, read only PatternMatch, condition, next_substitute; the rest is boilerplate.

6. A real lesson

Rules are not free. On COLUMBIA, one rule (PROJECT_THRU_SELECT) had no precondition: q11’s search space went from a few dozen groups to 46,000, with 120,000 firings. It simply never finished.

The fix had two layers: add a condition() (fire only for specific shapes) and deduplicate generated expressions. q11 went back to 135 groups.

7. Summary

  • The 119 rules are homogeneous; three teach you a class;
  • Rules come in three kinds: transformation, implementation, enforcer;
  • Patterns match operator + arity; arguments are moved by substitution;
  • Rules must be staged and preconditioned, or they become fuses for search explosions.

Get the next essay

New essays on products, business and tech — plus what the experiment's numbers actually say.