The previous two posts covered the port and the acceptance work. This one is only about traps — six real ones from the COLUMBIA + TPC-H project. What they have in common: everything looks fine until it isn’t.

Trap 1: matching results, wrong plan

The plan for ORDER BY ... LIMIT was once P_TOP at the root with QSORT below it — “truncate, then sort” — semantically wrong.

It slipped past every automated check: at execution time DuckDB normalizes ORDER BY after LIMIT, so the results of this plan were byte-identical to the correct plan’s.

How to catch it: inspect the plan’s shape (the nesting order of operators) on its own; never trust execution results alone. We added a shape-error verdict for this.

Lesson: execution results prove the data is right, not that the plan is right. Semantic verification must be independent of result verification.

Trap 2: a blind cast inside promise()

EQ_TO_MERGE::promise contained a forced cast to EQJOIN, but any expression can actually arrive there. ASAN caught a heap out-of-bounds read immediately.

The fix wasn’t clever, just a guard:

if (op_arg == NULL || op_arg->GetName() != "EQJOIN") return 0;

Lesson: running the whole corpus under ASAN beats reading the code ten times.

Trap 3: ledger drift — enum added, table forgotten

When adding the ORDER_BY operator I appended C_ORDER_BY to the enum in defs.h but forgot the matching row in global.h’s CLASS_STAT ClassStat[] table. The two must correspond positionally (the table length is slotsof(ClassStat)).

The result wasn’t a compile error but a runtime ASAN global-buffer-overflow, 28 bytes past the end of the global array.

How to catch it: script the invariants — check_ledgers.py asserts that the enum count, table count, and rule-registration count agree (currently 70/70, 29/29/29).

Lesson: any “aligned by position” ledger must be machine-checked, not remembered.

Trap 4: one rule explodes the search space to 46k groups

With PROJECT_THRU_SELECT enabled unconditionally, q11’s search space jumped from a few dozen groups to 46,000 with 120,000 rule firings — it simply never finished.

Root cause: KEYS_SET::Merge appends, so repeated firings keep creating new equivalent expressions.

Two-layer fix:

  1. Add a condition() to the rule: fire only when top ∪ freevars ≠ inner projection;
  2. Deduplicate with AddKey.

After the fix, q11 was back to 135 groups / 94 firings.

Lesson: transformation rules need preconditions; without them every rule is a fuse for exponential blow-up.

Trap 5: scheduling order decides survival

Once subquery bodies (derived tables, CTEs, EXISTS bodies) enter the memo, the order in which tasks are scheduled matters:

SchedulingPlans produced
All groups scheduled together0/22
Subquery groups scheduled after the root task12/22
Subquery groups scheduled before the root task22/22

If subquery groups are scheduled after the root, the root task runs before they produce winners, and the whole group ends up with no usable plan.

Lesson: scheduling order is strategy, not implementation detail. Design and test it as a first-class concern.

Trap 6: a 32-bit rule bitmap

The rule vector is an unsigned int bitmap (BIT_VECTOR), 32 bits wide. NUMOFRULES must simultaneously match three places: the enum tail, the rule-registration count, and the effective line count of the rule-set file. Any mismatch manifests as “rule silently not applied” or “array out of bounds” — not a clean error.

Lesson: design bitmap-style configuration to be extensible (64-bit or a dynamic bitset), and put the three-way consistency behind a gate.

Wrap-up

Six traps, six reusable lessons:

  1. Run the whole corpus under ASAN; don’t eyeball it;
  2. Matching results ≠ a correct plan — verify shape separately;
  3. Script-check positionally aligned ledgers;
  4. Rules need condition() and deduplication;
  5. Scheduling order must be designed and tested explicitly;
  6. Leave room to grow configuration bitmaps.

The hard part of engineering an optimizer isn’t writing rules — it’s making “correct” verifiable.

Get the next essay

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