Skip to main content

Module aggregate

Module aggregate 

Source
Expand description

Aggregate executor.

Handles SELECT … <aggs> … [GROUP BY …] queries. The planning strategy is straightforward:

  1. Walk the SELECT (and ORDER BY) expressions to find every aggregate function call. Dedupe by AST equality and assign each __agg_<i>.
  2. Same for every GROUP BY expression: assign __grp_<j>.
  3. Stream the WHERE-filtered rows, group by the tuple of GROUP BY values, and update per-group aggregate state.
  4. Materialise a synthetic per-group row containing [__grp_0..__grp_K, __agg_0..__agg_N] and rewrite the user’s SELECT / ORDER BY expressions to reference those synthetic columns instead of the originals.
  5. Evaluate the rewritten expressions against the synthetic schema and emit results.

v1.8 implements count(*), count(expr), sum, min, max, avg. NULL semantics follow PG: aggregates skip NULL inputs (except count(*), which counts rows). sum(int) widens to BigInt; avg(int|bigint) returns Float.

Structs§

AggResult
Output of running the aggregate path. Schema describes one row per group; rows are not yet ORDER BY-sorted (caller does it).

Statics§

AGGREGATE_ARRAY_AGG_ORDER_BY_FIRE
AGG_PER_ROW_COMPILED_HIT
AGG_PER_ROW_COMPILED_MISS
AGG_PER_ROW_COUNT_STAR_SENTINEL
AGG_PER_ROW_EVAL_FALLBACK
AGG_PER_ROW_FAST_POS
v7.37.9 Phase 1A-ext — per-row spec dispatch branches in accumulate_groups’s hot loop. Verifies the Phase 1A decomposition agent’s S06 assumption (“14 specs × eval_expr per row”). Sum should equal n_specs × n_input_rows. Branch distribution tells which attack target ROI is highest: FAST_POS many = baseline OK; COMPILED_MISS many = Step-VM is hot path; EVAL_FALLBACK > 0 = uncompilable specs walking the eval_expr tree per row × Cow row materialise.
DISTA_LITERAL_ARG2_CACHE_FIRE
v7.37.9 Phase 0 diagnostic counters — see .claude/notes/v7.37.9-class-a-c-cascade-closure-plan.md. These are read-only telemetry, do not gate any code path. Used by xtests/dogfood_replay/src/bin/counter_dump.rs to verify whether the DISTA A-3 + array_agg-ordered fast paths actually fire on the mailrs Class A SQL shape.

Functions§

contains_aggregate
is_aggregate_name
is_hypothetical_set_name
v7.32 (round-29) — hypothetical-set aggregates: rank(args) WITHIN GROUP (ORDER BY …) and friends compute the rank the hypothetical row would have. Like ordered-set, the value stream comes from the sort spec and the in-parens args are direct (the hypothetical row).
is_ordered_set_name
v7.32 (round-29) — ordered-set aggregates: the value to aggregate comes from the WITHIN GROUP (ORDER BY …) sort spec, and any in-parens arguments are direct arguments (the percentile fraction). mode() takes no direct argument.
is_within_group_name
v7.32 (round-29) — every aggregate that takes its value stream from a WITHIN GROUP (ORDER BY …) clause (ordered-set + hypothetical-set).
uses_aggregate
True if this statement should go through the aggregate path.

Type Aliases§

CorrelatedEval
Execute aggregate logic against an already-WHERE-filtered iterator of rows. table_alias is the alias accepted by column resolution. v7.25.2 (round-19 A) — caller-injected evaluator for synth-row expressions that still carry subquery nodes after the rewrite (correlated subqueries in the select list / HAVING / aggregate ORDER BY of a GROUP BY query). The engine passes its correlated-aware evaluator; pure-library callers pass None and surviving subqueries keep erroring loudly.