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

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.