The plan tree settles what to do; execution settles how to do it batch by batch. The key here isn’t the algorithm — it’s memory layout: the same data arranged differently can be an order of magnitude faster.
Part three of the plan series.
1. vector: logically an array, physically maybe not
A vector is logically a fixed-size array, but it has several physical representations:
| Representation | Use case | Stores |
|---|---|---|
FLAT | default | contiguous array, same as logical |
CONSTANT | whole column identical | one value |
DICTIONARY | low-cardinality strings | indices + dictionary |
SEQUENCE | increasing sequence | base + increment |
UNIFIED | generic algorithms | contiguous data + selection |
Why so many? Because many columns during execution are constant or repetitive:
- a constant column (
1inWHERE x = 1) asCONSTANTavoids writing 2048 copies; - low-cardinality columns (status, country) as
DICTIONARYlet comparisons work on indices.
2. Flatten and Unified Format
Two conversions with different purposes:
FlattenexpandsCONSTANTand friends into a contiguous array, for algorithms that must write element by element;ToUnifiedFormatdoesn’t expand; it attaches a selection pointer, sosel[i]gives the real position.
The latter is what makes generic algorithms possible: one implementation, no special case per representation.
The cost is an extra indirection per access — worth it only where the operator benefits.
3. chunk: a batch of vectors
A chunk is a set of equal-length vectors (columns) representing a batch of rows. Execution processes batches, not rows.
Batch size matters:
- too small: call and branch overhead dominates;
- too large: doesn’t fit in cache, gets slower.
plan uses util.DefaultVectorSize, and operators produce output chunks on demand in Execute.
4. The vectorized volcano model
Each operator implements three methods:
type OperatorExec interface {
Init() error
Execute(input, output *chunk.Chunk, state *OperatorState) (OperatorResult, error)
Close() error
}
Runner.Init recursively initializes children; Execute:
- initializes the output chunk;
- dispatches by operator type (scan/project/join/agg/filter/order/limit);
- returns one batch.
The difference from a classic volcano model: each Execute returns a batch, not a row. The number of next() calls is divided by the batch size, which is friendlier to branch prediction and cache.
5. The expression executor
Expressions (a + b > 10) are interpreted over vectors rather than compiled to machine code:
- inputs are vectors plus a selection;
- intermediate results are vectors too;
filterusually doesn’t rebuild data; it emits a selection of passing rows.
That explains a common observation: with many WHERE conditions, the cost may not be the computation but whether the selection gets materialized downstream.
6. Three verifiable checks
| Check | Expectation |
|---|---|
Does a constant predicate use CONSTANT? | yes (no 2048 copies) |
| Does filter pass only a selection? | yes (unless downstream must flatten) |
| Is batch size stable across operators? | yes |
If the second fails, something materializes unnecessarily — often a bigger cost than the algorithm itself.
7. Next
Above execution sits storage: pages, indexes, transactions and checkpoints — where data lives and how consistency is kept.
Series: Database kernel · Project page
Get the next post
New posts on database kernels and engineering.