The first two posts covered the framework mechanics and the scheduler. This one goes into the heart of the memo: the dynamic-programming cell.

1. Correcting an intuition

Many people assume the optimizer’s job is to “find one optimal plan per group.” But gporca’s problem definition is:

For each group, under the given requirements, find an optimal plan.

Because the best implementation for the same group differs completely under different requirements:

  • If the parent demands “ordered by l_returnflag,” the best may be a sort operator — or an ordered index scan;
  • If the parent demands “hash distributed” (MPP), the best may be a Motion.

So “requirements” must become part of the optimization problem’s coordinate. That’s what COptimizationContext is:

cell key = (group, required plan properties, search stage)

It turns “find the optimum on a group, under some requirements, in search stage N” into an object that is addressable, deduplicatable, state-bearing, and result-bearing. Hashing uses only CReqdPropPlan; equality also compares group and stage.

2. Why requirements can’t live in CGroup

CandidateWhy it fails
Requirements in CGroupA group is optimized under multiple requirements; state and winners would overwrite each other
Requirements in the jobJobs are one-shot and recycled; the cell must survive across jobs and suspensions
Requirements in CGroupExpressionThe optimization dimension is (expression × cell): the same physical expression must be costed separately per cell

This also explains a detail: CGroupExpression has an Implemented state but no Optimized — because it doesn’t hold “the optimal solution.”

3. Required side and derived side: a mirrored pair

ORCA’s property system is a duality of required and derived:

Derived (what an operator can give)Required (what a parent demands)If unmet
COrderSpecCEnfdOrder → COrderSpecInsert CPhysicalSort
CDistributionSpecCEnfdDistributionInsert a Motion (droppable in single-node)
CRewindabilitySpecCEnfdRewindabilityInsert Materialize
CCTEMapCCTEReq → CCTEMapCheck CTE availability

The abstract root CReqdProp does exactly three things:

  1. Typology: EptRelational (columns/predicates — correctness and statistics) vs EptPlan (execution shape);
  2. Propagation protocol: Compute(mp, exprhdl, prpInput, child_index, pdrgpdpCtxt, ulOptReq) — note the signature: it needs “requirements from the parent + capabilities already derived by the children + which child + which opt request” to compute “what this child should be required to satisfy.” Requirements can’t be static data; they must be computed while looking at the children’s actual capabilities.
  3. Pool-agnostic: allocation comes in as a parameter, so the object can live long-term inside a DP key.

4. CGroup is really five things in one class

CGroup looks dependency-heavy because it is simultaneously:

#ContentWorth studying?
1The 7-state machine✅ Top priority
2Member list + duplicate list✅
3The DP table (optimization-context hashtable, 100 buckets)✅
4Domain data: stats, cost lower bounds, join keys, partitions/CTEs❌ Treat as opaque
5Two job queues (group-level dedup)✅

Measured: CGroup.cpp is 2,107 lines across 55 functions — framework core ~500 lines (24%), statistics derivation ~700 (33%), plan enumeration ~470 (22%), duplicate-group algorithms the rest.

You only need to read 1, 2, 3, and 5.

5. CGroup and COptimizationContext are duals, not a pipeline

A common mistake is “fully understand one before reading the other.” Measured: CGroup.h only forward-declares COptimizationContext; only 16 of 55 functions touch it (~33% of the code).

The right approach is “read the shared contract as a pair”: first grasp the cell’s three facts (identity triple, ownership on insert, lookup by stage), then read those 16 functions.

6. Implications for porting

The dependency closure of COptimizationContext: 89 files for the header alone; 361 files, 54 layers, 374 classes, 1,365 include edges with the .cpp.

But a member-by-member audit shows fewer than 10 are truly essential:

  • CReqdPropPlan (cell identity) — truly essential;
  • CGroup (part of identity) — downgradable to a group id;
  • CJobQueue (embedded by value) — structurally essential;
  • Most other includes exist only for a typedef, an inline function, or an optional strategy method.

So when porting, don’t move classes along the dependency closure — rebuild around the framework mechanisms. This is also why vendoring + an adapter layer beats hand-copying.

7. Summary

  • A DP cell’s key is (group, required properties, stage), not the group;
  • Requirements and derived properties are mirrored duals; the Compute protocol is “compute requirements while looking at children”;
  • CGroup is a five-in-one object; reading its 24% is enough;
  • The closure is huge, but the truly essential core is small — the general lesson for understanding a large optimizer.

Get the next essay

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