Writing a Query Optimizer: From COLUMBIA to TPC-H

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. ...

October 20, 2026 · 3 min · daviszhen

Six Real Traps in an Optimizer: Matching Results Don't Mean a Correct Plan

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. ...

October 20, 2026 · 4 min · daviszhen

Making an Optimizer Run TPC-H: Lossless Conversion and Execution Reconciliation for 22 Queries

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. ...

October 20, 2026 · 4 min · daviszhen