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:
- Keep the sources as untouched as possible — make only platform-required edits, documented one by one;
- Write an MFC compatibility layer (
port/compat.h):CString,CArray<T,ARG>,CFile,CWcolView,TRACE,BOOLreimplemented in modern C++; - Replace the MFC application object’s entry point with
int maininport/entry.cpp, feeding command-line arguments into the globals the optimizer expects.
3. Four real porting traps
| Trap | Symptom | Root cause and fix |
|---|---|---|
| Pointer truncation | Random crashes on 64-bit | int(MExpr) truncated a pointer to 32 bits → (int)(intptr_t) |
| MSVC in-class qualification | clang refuses to compile | MSVC allows Class::member qualification inside a class body; remove the qualifier |
for-scope leakage | CArray::SetSize asked for 1.8 billion elements | An auto-fixer hoisted int i out of loops but left the inner int, creating two variables — uninitialized garbage → GetCollId returned nonsense |
| CRLF data | Parser SIGSEGV, collection-type parse failures | 259 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:
| Verdict | Count | Meaning |
|---|---|---|
| OK | 112 | At least one rule set produced a physical plan |
| TIMEOUT | 6 | 10-way join chains; the upstream search itself explodes |
| CRASH(-6) | 2 | Upstream corpus has unbalanced parentheses (318 vs 317) |
| No matching catalog | 31 | TPC-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:
- A thin adapter layer — confine platform differences to one
port/directory; - A reproducible corpus — a one-command regression matrix with classified verdicts;
- 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.
Related
- Six Real Traps in an Optimizer: Matching Results Don’t Mean a Correct Plan
- Making an Optimizer Run TPC-H: Lossless Conversion and Execution Reconciliation for 22 Queries
- 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.