Query optimizers are the component everyone uses and few people write. You can recite Join Order, subquery unnesting and cost models in an interview — but actually writing an optimizer that survives all 22 TPC-H queries teaches you that the hard part isn’t the vocabulary. It’s the way the details constrain each other.
This series is a record of one such attempt: from porting a textbook Cascades implementation, to making it digest TPC-H, to admitting the traps it fell into.
1. Why COLUMBIA
There are two ways to learn optimizers:
- Read an industrial one (gporca, DuckDB) and understand its architecture;
- Find a small but complete implementation, run it, break it, and fix it.
I did both: first three posts taking gporca’s framework apart (task engine, memo, DP cells), then COLUMBIA — the 1998–2000 master’s thesis implementation from Portland State University, a textbook-grade Cascades/Volcano codebase of 19,787 lines across 17 .cpp and 17 .h files.
It is small enough to read the core in a week, and complete enough to have real search, rules, costing and properties.
2. What an optimizer is made of
Strip away the implementation details and every Cascades-style optimizer needs four things:
| Component | Question it answers |
|---|---|
| Search space (memo) | Which plans are equivalent? How to represent them compactly? |
| Transformation rules | How to move from one plan shape to another? |
| Cost model | Which plan is cheaper? How is cost computed? |
| Property system | What does the parent require (order, distribution, columns)? How to satisfy it? |
COLUMBIA puts these in SSP/GROUP/M_EXPR, RULE, CM, and LOG_PROP/PHYS_PROP. Understand those four and you understand 80%.
3. Reading order for this series
The series is organized as “get it running, reconcile it, then reflect”:
- Porting a 1999 Cascades optimizer to modern C++ — compiling, debugging and running the corpus on a modern toolchain;
- Making an optimizer run TPC-H — using DuckDB as the reference frame to turn “it runs” into “it is provably correct”;
- Six real traps in an optimizer — matching results with wrong plans, ledger drift, rule explosions.
If you care more about industrial architecture, start with Reading gporca, Part 1 and come back to the port.
4. What you get
Not a production optimizer — far from it. What you get is a reproducible method:
- How to define “correct” with an external reference (DuckDB);
- How to reconcile with ASTs instead of text;
- How to prove a fix with a revert check (removing it must fail a named test);
- How to locate which rule and which firing caused a search explosion.
5. An honest expectation
Porting and acceptance are just the beginning. The hard parts are cost accuracy, statistics, rule coverage, and semantic problems like “matching results, wrong plan”. This series records those honestly rather than showing only the wins.
Get the next essay
New essays on products, business and tech — plus what the experiment's numbers actually say.