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:

RepresentationUse caseStores
FLATdefaultcontiguous array, same as logical
CONSTANTwhole column identicalone value
DICTIONARYlow-cardinality stringsindices + dictionary
SEQUENCEincreasing sequencebase + increment
UNIFIEDgeneric algorithmscontiguous data + selection

Why so many? Because many columns during execution are constant or repetitive:

  • a constant column (1 in WHERE x = 1) as CONSTANT avoids writing 2048 copies;
  • low-cardinality columns (status, country) as DICTIONARY let comparisons work on indices.

2. Flatten and Unified Format

Two conversions with different purposes:

  • Flatten expands CONSTANT and friends into a contiguous array, for algorithms that must write element by element;
  • ToUnifiedFormat doesn’t expand; it attaches a selection pointer, so sel[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:

  1. initializes the output chunk;
  2. dispatches by operator type (scan/project/join/agg/filter/order/limit);
  3. 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;
  • filter usually 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

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