Skip to content

Give every aggregate the same init, step and result interface #2660

Open

Description

@mathiasrw

What happens today

  • compileGroup (src/select/groupby.js) writes code for each built-in aggregate twice, for the first row of a group (lines 132-217) and for every next row (lines 220-508).
  • A user aggregate is alasql.aggr.NAME(value, accumulator, stage) with stage 1, 2 and 3 (src/select/queryfn.js, lines 192-210). With several arguments the accumulator moves to position n + 1 (Enable multi-column user-defined aggregate functions #2396).
  • GROUP_CONCAT is a user aggregate with special handling. getGroupConcatParams (lines 19-30) passes the separator and the sort on every row.
  • DISTINCT inside an aggregate is a $$_VALUES_ wrapper written around each aggregate's code.
  • With no rows and no GROUP BY, queryfn3 (lines 174-190) builds one group by hand.

What to change

  1. Add src/select/aggregates.js with one {init(options), step(state, ...args), result(state)} per built-in. options holds the clauses of the call (DISTINCT, SEPARATOR, the sort of Sort GROUP_CONCAT values by the expressions of its ORDER BY #3166) and is given once when the group is created. Each built-in reproduces today's code exactly, including the type lists of MIN and MAX, SUM and AVG turning a date into NULL, bigints, and the 0 or undefined for no rows.
  2. Wrap every alasql.aggr function with fromStages(fn, argCount). step calls stage 1 on the first value and stage 2 after, result calls stage 3, with the same arguments in the same positions as today.
  3. GROUP_CONCAT becomes a built-in that reads separator and sort from options. alasql.aggr.GROUP_CONCAT stays callable with its old arguments. Remove getGroupConcatParams.
  4. DISTINCT is one generic wrapper around step that skips a value already seen in the group, with today's key (distinctRowKey for COUNT(DISTINCT *)).
  5. compileGroup calls init when it creates a group, step for every row including the first, and result when the groups are read. States live in g.aggs[j] from Match GROUP BY keys by structure and keep each group's state in slots #2647. A built-in may give stepJS text that the generator inlines. The object form is the reference.

Window aggregates in src/select/select.js stay as they are.

What stays the same

  • Every aggregate result for every built-in and for user aggregates with one or several arguments. This includes MIN and MAX of text (missing), MAX of dates, SUM over no rows, COUNT of NULL, AVG with bigints, and GROUP_CONCAT with SEPARATOR, ORDER BY and DISTINCT.
  • The calls a user aggregate receives.
  • No result changes.

Done when

  • A new test file named after this issue runs every built-in aggregate over a value matrix (numbers, 0, bigints, text, dates, NULL, undefined, mixed, no rows, one row), with and without DISTINCT, against results recorded on main before the change. It also records the calls a user aggregate with one and with two arguments receives.
  • bun test/performance/bench.js --group=group-by shows no slowdown beyond the noise described in test/performance/README.md.
  • The full test suite and the CI checks (dialect test, parity check, run-state check, binding check) pass, and the pull request lists every parity difference.

Depends on

#2647

Activity

  1. added this to the v4.x Focus milestone on Nov 26, 2026
  2. changed the title [-]Review #2418 for code improvements: GROUP_CONCAT special-cased in the generic aggregate code[/-] [+]Give every aggregate the same init, step and result interface[/+] on Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions