Database Kernel (2): From Syntax Tree to Plan Tree — Filter Pushdown and Join Ordering

Bind produces scopes and expressions; execution needs a plan tree. The conversion in between decides whether a query merely runs or runs fast. This is part two of the plan series: how the plan tree is built, and the two optimizations that matter most. 1. The minimal plan node type LogicalOperator struct { Typ LOT // scan / filter / project / join / agg / order / limit Children []*LogicalOperator // node-specific extra fields } Generation follows the bind order: ...

October 22, 2026 · 3 min · daviszhen

SignalBrief #1: Five Signals on Writing an Optimizer

SignalBrief is a weekly bilingual brief: one or two picks each from products, business and tech, with one line on why each matters. This is issue #1. Subscribe at the bottom. 1. A 1999 optimizer that still compiles today Porting a 1999 Cascades optimizer to modern C++ Why it matters: it proves a textbook implementation isn’t a museum piece. 19,787 lines, MFC-era, 17 .cpp files — it runs on a modern toolchain in a weekend, and then you can change it, break it and fix it. The fastest way to learn optimizers isn’t reading papers; it’s making a real one run on your machine. ...

October 20, 2026 · 2 min · daviszhen

gporca's Rule System: How to Read 119 Rules

The previous post covered gporca’s DP cells. This one covers rules: xforms/ contains 119 files, roughly 44K lines. At first it looks like required reading — it isn’t. Rules are highly homogeneous; understanding three of them teaches you a whole class. 1. There are only three kinds of rules Kind Role Examples Transformation Produce equivalent logical expressions inside the same group Join commutativity, associativity, predicate pushdown Implementation Turn a logical expression into an executable physical operator HashJoin, IndexScan, Sort Enforcer Insert operators to satisfy parent requirements Sort (order), Motion (distribution) Search works like this: transformations grow groups, implementations propose candidates, DP picks the cheapest per group. ...

October 20, 2026 · 3 min · daviszhen

The Optimizer's Cost Model: From Formulas to Calibrated Numbers

The search space decides how many plans you can think of; the rules decide what shapes are reachable; and the cost model decides who wins. Whether an optimizer is usable depends heavily on whether its cost model “makes sense”. This post takes COLUMBIA’s cost model apart, plus the traps we hit when calibrating it on TPC-H. 1. What COLUMBIA’s cost model looks like Minimal: a cost model is a set of constants plus a local-cost formula per physical operator. ...

October 20, 2026 · 3 min · daviszhen

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

Reading gporca (Part 3): The Key of a DP Cell — (Group, Required Properties, Stage)

The first two posts covered the framework mechanics and the scheduler. This one goes into the heart of the memo: the dynamic-programming cell. 1. Correcting an intuition Many people assume the optimizer’s job is to “find one optimal plan per group.” But gporca’s problem definition is: For each group, under the given requirements, find an optimal plan. Because the best implementation for the same group differs completely under different requirements: ...

October 20, 2026 · 4 min · daviszhen

Reading gporca (Part 2): The Reference-Count Gate That Turns Recursive Search into a Pausable Task Network

Query-optimizer search is inherently recursive: explore a group, derive plans, recurse into each input. The naive implementation is plain depth-first recursion. gporca doesn’t do that — it flattens the recursion into a task network, driven by a ready queue and a single reference counter. This post takes apart the most subtle piece: the m_ulpRefs reference-count gate. 1. m_ulpRefs is not a lifetime counter CJob::m_ulpRefs has three sources that must be read together: ...

October 20, 2026 · 4 min · daviszhen

Reading gporca (Part 1): Only ~11K of Its 400K Lines Are Actually Framework

gporca is Greenplum’s query optimizer: about 401K lines across 935 compilation units. Opening it for the first time is intimidating. But if you slice it by “is this a framework design decision?”, the part that is actually framework is under 10K lines. 1. Four libraries, four roles Many people assume libgpopt is the framework and the other three are “foundational components.” That’s imprecise. They are four different kinds of thing: ...

October 20, 2026 · 4 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

Porting a 1999 Cascades Optimizer to Modern C++: The COLUMBIA Story

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

October 20, 2026 · 4 min · daviszhen