An optimizer is worth using when it can digest all 22 TPC-H queries — not when it can run a couple of toy examples. This post records how we took COLUMBIA (see the previous post) from “runs Chain2” to “handles the whole TPC-H suite.”
1. Principle: convert as-is, never rewrite for the optimizer
The first step wasn’t code, it was rules:
- As-is conversion: no restructuring of TPC-H SQL to make the optimizer’s life easier;
- Record what’s unsupported: syntax the optimizer can’t express yet goes on a TODO list, never gets deleted from the query;
- Reconcilable: the conversion must be provably equivalent to the original.
The value of this discipline only shows up later: it turns “capability gaps” into a quantified list instead of a vague pile of failures.
2. Use DuckDB as the reference frame
The front end uses DuckDB’s tpch extension:
CALL dbgen(sf=1); -- generate SF=1 data
SELECT * FROM tpch_queries(); -- the 22 standard queries
SELECT json_serialize_sql('...'); -- SQL → structured AST
Scale factor 1 row counts: lineitem 6,001,215; orders 1,500,000; partsupp 800,000; part 200,000; customer 150,000; supplier 10,000; nation 25; region 5. From this we generated the catalog and cost model the optimizer needs.
3. Lossless reconciliation: AST vs AST
The first verification idea — convert back to SQL text and compare strings — failed quickly. Alias lists like AS t (c1,c2), parenthesization of IN predicates, and literal typing all shift on round trips.
We switched to AST vs AST: convert the optimizer’s query language back into DuckDB’s JSON AST and compare node by node (ignoring query_location).
Result: 22/22 identical.
What this buys over string comparison is that it compares semantic structure, not formatting.
4. Getting plans: 13/22 → 22/22
Once conversion passed, the real fight began: every query had to produce a complete plan. Five work packages got us there:
| Package | Content | Effect |
|---|---|---|
| W1 | Derived tables / CTE binding: DERIVED output-column positional mapping, real log props, P_DERIVED pass-through | unlocks q7/q8/q9/q22 |
| W2 | Log props and physical operators for OUTER_JOIN / THETA_JOIN | unlocks q13 |
| W3 | Subqueries into the memo: SUBQUERY_OP::SubGrpID + M_EXPR copy hook | “not in memo” 8 → 0 |
| W4 | Semi/anti join: operators, rules, ledgers, parser, tools | unlocks EXISTS-style predicates |
| W5 | Convergence: identified PROJECT_THRU_SELECT as the search explosion, added condition() | 46k groups → 135 |
Plus a few bare-knuckle fixes: TOP had no physical operator (q2/q3/q10/q18/q21 all blocked), ORDER BY was lifted into a context property and put the sort operator in the wrong place (next section), negative-cost asserts, and missing projection columns.
5. A semantic trap: QSORT above P_TOP
The plan for ORDER BY ... LIMIT started out as:
P_TOP(10)
└─ QSORT(L.l_returnflag) ← sort first, then LIMIT?
Looks plausible; semantically it means sort after the limit. Plans print root-first, so P_TOP at the root means the limit happens before the sort. For ORDER BY x LIMIT 10 that’s wrong: you must sort by x, then take the top 10 — not take the top 10, then sort.
The nastier part: DuckDB executed this plan and the results still matched — because DuckDB normalizes ORDER BY after LIMIT at the SQL layer, masking the difference. We therefore added a shape check: the plan’s operator nesting must match the query’s semantics; execution results alone are not enough.
The fix was to operator-ize: make ORDER BY a real logical operator, add an ORDER_BY→QSORT rule, and fuse TOP(ORDER_BY(x)) into P_TOPN (a heap-based Top-N rather than “full sort then truncate”).
6. Final acceptance: reconcile execution results
The acceptance criterion was defined explicitly:
The optimizer’s plan must convert to JSON, run in DuckDB, and produce the same results as the original query.
Flow: plan text → query language → DuckDB JSON AST → json_execute_serialized_sql(...) → row-by-row comparison with the original query.
Final state: 22/22 plans, 22/22 identical results, 0 mismatches, with all five gates green (ledgers 70/70, AST 22/22, syntax 23/23, TPC-H execution, upstream A/B).
7. Takeaways
- Fix the “as-is” rule before converting; don’t let adaptation silently restructure queries;
- Use a mature execution engine (DuckDB) as the reference frame instead of inventing your own correctness standard;
- Reconcile with ASTs, not text;
- Matching results ≠ a correct plan — check semantic shape separately;
- Make the regression one command (
sh tools/run_gates.sh), otherwise every change is a gamble.
Related
- Porting a 1999 Cascades Optimizer to Modern C++: The COLUMBIA Story
- Six Real Traps in an Optimizer: Matching Results Don’t Mean a Correct Plan
- Reading gporca (Part 3): The Key of a DP Cell — (Group, Required Properties, Stage)
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.