If you want to hand-write a query optimizer, the cheapest starting point isn’t inventing one from scratch — it’s understanding a real Cascades implementation first, then getting it to run on your own machine. My sparring partner was COLUMBIA — the master’s thesis implementation by Yongwen Xu at Portland State University (1998–2000, advised by Shapiro and Maier), a textbook-grade Cascades/Volcano optimizer in 19,787 lines across 17 .cpp and 17 .h files.

The problem: it was born in the Visual C++ 6.0 and MFC era. Bringing it back to life required a small port.

1. Understand the structure first

COLUMBIA’s core model has exactly three layers:

  • SSP (search space) — the whole memo;
  • GROUP — an equivalence class: a set of semantically equivalent expressions;
  • M_EXPR — the compact expression inside a group: an operator pointer plus an array of input group IDs (GRP_ID *Inputs).

Search is driven by a task engine with five task kinds: O_GROUP (find the cheapest plan in a group satisfying a context), E_GROUP (explore a group, firing transformation rules), O_EXPR (fire all relevant rules for one expression), O_INPUTS (check whether an expression can satisfy the current context), and APPLY_RULE (apply a single rule to a single expression).

One easy-to-miss detail: the task queue is LIFO (PTASKS), so the task order printed in the trace is the reverse of actual execution.

2. Port strategy: a thin adapter layer, not a rewrite

I considered another path: port gporca (Greenplum’s optimizer). But it’s 935 compilation units and 373,104 lines, and 70.5% of libnaucrates is generated DXL code. Hand-copying it is not realistic.

The COLUMBIA port took the opposite strategy:

  1. Keep the sources as untouched as possible — make only platform-required edits, documented one by one;
  2. Write an MFC compatibility layer (port/compat.h): CString, CArray<T,ARG>, CFile, CWcolView, TRACE, BOOL reimplemented in modern C++;
  3. Replace the MFC application object’s entry point with int main in port/entry.cpp, feeding command-line arguments into the globals the optimizer expects.

3. Four real porting traps

TrapSymptomRoot cause and fix
Pointer truncationRandom crashes on 64-bitint(MExpr) truncated a pointer to 32 bits → (int)(intptr_t)
MSVC in-class qualificationclang refuses to compileMSVC allows Class::member qualification inside a class body; remove the qualifier
for-scope leakageCArray::SetSize asked for 1.8 billion elementsAn auto-fixer hoisted int i out of loops but left the inner int, creating two variables — uninitialized garbage → GetCollId returned nonsense
CRLF dataParser SIGSEGV, collection-type parse failures259 of 260 data files were CRLF; the parser treated \r as content → normalize to LF and make the parser tolerate \r

The third one is the most instructive: the automated fixer introduced its own bug, and the symptom (array length explosion) was very far from the cause (uninitialized loop variable).

4. Accept the port with a reproducible corpus

The upstream project left the “query × catalog × rule-set” pairing implicit in an options file. I wrote tools/run_all_cases.py to reconstruct the matrix: 120 cases × 15 rule vectors = 1,800 runs in 813 seconds:

VerdictCountMeaning
OK112At least one rule set produced a physical plan
TIMEOUT610-way join chains; the upstream search itself explodes
CRASH(-6)2Upstream corpus has unbalanced parentheses (318 vs 317)
No matching catalog31TPC-D family schemas were never shipped

26 of 31 “failures” were data problems, not port problems. Without this matrix you cannot tell “my port is broken” from “this corpus never ran.”

5. Debugging

VS Code + CodeLLDB is enough: set a breakpoint on Optimizer() (src/main.cpp:44) and the stack walks back to main in port/entry.cpp. For engine-side memory issues, use an ASAN build (cmake --build build-asan) — much faster than waiting for a SIGSEGV under lldb.

6. Wrap-up

Bringing old code back to life needs three things, all of them:

  1. A thin adapter layer — confine platform differences to one port/ directory;
  2. A reproducible corpus — a one-command regression matrix with classified verdicts;
  3. A measurement habit — every change measured before and after (group counts, task counts, wall time).

Next: how to make this optimizer actually run all 22 TPC-H queries.

Get the next essay

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