How rate walks a block and crosses its edge

promql-engine internals · companion to docs/engine.md and docs/series-source.md · illustrates docs/engine-blocks.md

Before you read

Block
The store's unit of time, such as a TSDB block, named on every row by block_start and block_end. Its samples include the reach-back needed to answer its steps independently. No raw-series fold crosses the edge; completed output and native aggregate state may outlive it.
Batch (RecordBatch)
The series the store sends together. The store may cut a block into batches anywhere, even between two chunks of one series. It is the unit of transport.
Series
One series' full raw labels, mandatory opaque series_id, and one chunk of its samples, with the two block columns, stored as one Arrow row. A series with several chunks arrives once per chunk, consecutive by ID and chronological within each block and partition. How many chunks a label set becomes in a block is the store's call: a TSDB-backed store sends one per chunk it keeps, a remote store may merge what it receives into one. It is the unit the engine folds.
Chunk
A run of one series' samples as the store keeps it, such as a compressed TSDB chunk. The store need not decode a chunk to trim it, so samples may reach past a block edge or overlap the next chunk of the same label set. It is the store's unit of storage.

One select yields blocks, a block arrives as batches, a batch holds series, and a series holds one chunk's samples.

How does rate reach across two chunks of one series that it never aligns? It copies what a later window reaches into the one current ID's window buffer. SeriesEvalExec emits a row when that ID closes, and an aggregate above sees only evaluated rows.

sum(rate(x[5m]))  ·  x{pod="a"}, x{pod="b"}  ·  step 30 s, 420 s → 600 s  ·  blocks 1 and 2  ·  batches C and D

The shape

labels       Struct<__name__: Utf8View, ...>
samples      List<Struct<timestamp: Timestamp(Millisecond), value: Float64>>
block_start  Timestamp(Millisecond)
block_end    Timestamp(Millisecond)
series_id    UInt64 | FixedSizeBinary(n>0)   mandatory at the source

One series is one Arrow row: full raw labels, an opaque ID, one chunk's samples, and the block it belongs to. The illustration assigns ID 41 to a and ID 7 to b; their order is not numeric ID order. Evaluation removes series_id from the canonical four-column output.

What the source promises

The source is trusted. These obligations are not per-row engine audits; a broken source can produce wrong results. No seen-ID map or provenance validator is kept.

source obligationwhy it matters
Each series_id maps one-to-one to full raw labels, stable across chunks, blocks and selectionsID equality identifies the current fold without comparing labels
Chunks of one ID are contiguous and chronological within each block, all in one partitiona different ID closes the current series permanently for this block
block_start and block_end stay constant within a block, with the same edges in every partitionthe first row establishes every step the block answers
Blocks ascend and do not overlapno fold needs to survive a block edge; the same ID may recur in a later block

A batch boundary is not among the events, because the store may cut a block anywhere, even between two chunks of one ID. The evaluator carries one current ID and fold per partition, plus budgeted completed output and a pending input cursor. Output budgets allow one indivisible oversized row. The fold closes on an ID change, block change or end of stream. Only block ordering and unknown partitioning are advertised to DataFusion, never labels or IDs ascending.

History follows the window's sample high water plus a bounded ingestion allowance. At most 256 samples enter before readiness and retirement are checked; unused future tails are not copied. Front trimming does not release vector capacity. The diagrams show sample reachability, not retained capacity or cooperative work budgets (docs/engine.md).

Watch one query cross a block edge

The store cuts blocks every 240 s here, so block 1 answers 420 s and 450 s and block 2 answers 480 s to 600 s. The engine selects once, from 120 s to 600 s with window_ms 300 s, and the store reaches back by that window at each block.

block  labels.pod  samples                     first / last sample timestamp
  1    "a"         300 330 … 420 450    (6)    300 / 450
  1    "b"         150 180 … 420 450   (11)    150 / 450
  2    "a"         300 330 … 570 600   (11)    300 / 600    reach-back 300–450
  2    "b"         180 210 … 570 600   (15)    180 / 600    reach-back 180–450

The same step, as Arrow holds it

The top half draws the two input RecordBatches, each with label, ID and block columns and a samples list of offsets over two flat child buffers. The batch edge falls inside block 2 on purpose. The bottom half draws copied history and logical sum partials and output. It omits executor output batching, retained capacities and native aggregate buffering.

Partials per step

Every value comes from the page's own fold and its port of extrapolatedRate. This schematic updates partials as steps become numerically final and emits at each logical block edge. The real executor first closes the ID and delivers its completed row in an output batch; native aggregation can retain groups from multiple blocks. The schematic shows arithmetic and prefix progression, not exact emission timing or a memory bound.

stepblockwindowrate arate bsum partialstate

sum never sees samples, only the step values rate emits, each keyed by its step, so a batch cannot arrive early or late.

With several partitions, Partial aggregates feed a Final aggregate through an order-preserving repartition, with block_start leading the group key. Completed prefixes can emit once every partition has passed them (docs/engine.md). This does not require the native aggregate to release one block before consuming any rows of the next.

Finalising

Each lane of x{pod="a"} goes through Prometheus's extrapolatedRate when rate answers it, threshold clamp first, then the counter zero clip.

result             = last_v - first_v + reset_sum = 6 - 11 + 16 = 11
sampled_interval   = 600 - 330                                  = 270 s
average_interval   = 270 / (10 - 1)                             = 30 s
threshold          = 1.1 * 30                                   = 33 s
duration_to_start  = 330 - 300 = 30 s   (< 33, kept as measured)
duration_to_zero   = 270 * (11 / 11) = 270 s   (30 < 270, zero clip does not bite)
duration_to_end    = 600 - 600 = 0 s    (< 33, kept)
factor             = (270 + 30 + 0) / 270                       = 10/9
increase           = 11 * 10/9                                  = 12.2222222
rate               = 12.2222222 / 300                           = 0.0407407407
lane tresultsampled intervalto_startfactorincreaserate
420 s4120 s15 s (clamped)135/1204.50.015
450 s5150 s15 s (clamped)165/1505.50.0183333333
480 s6180 s15 s (clamped)195/1806.50.0216666667
510 s9210 s15 s (clamped)225/2109.64285710.0321428571
540 s10240 s15 s (clamped)255/24010.6250.0354166667
570 s11270 s30 s300/27012.22222220.0407407407
600 s11270 s30 s300/27012.22222220.0407407407

Edge cases

Overlapping chunks. Adjacent chunks of a label set may overlap in time, so the engine skips every sample at or before the last timestamp it saw for the current identity and the first chunk wins. A stale marker claims its timestamp too (docs/engine-blocks.md).

Functions that need every sample. quantile_over_time and functions like it refold the lane's chronological slice of the same history at each step, with function-specific scratch as needed. The slice follows the window; retained buffer capacity can also depend on chunk size (docs/engine-blocks.md).

Notes on the numbers. The fold and extrapolatedRate run in this page, term for term after Prometheus's promql/functions.go. The engine checks itself against the same rates in the_engine_blocks_fixture in buffer.rs.