Repository navigation
Open
Labels
Milestone
Description
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 positionn + 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
- Add
src/select/aggregates.jswith one{init(options), step(state, ...args), result(state)}per built-in.optionsholds 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 orundefinedfor no rows. - Wrap every
alasql.aggrfunction withfromStages(fn, argCount).stepcalls stage 1 on the first value and stage 2 after,resultcalls stage 3, with the same arguments in the same positions as today. - GROUP_CONCAT becomes a built-in that reads separator and sort from
options.alasql.aggr.GROUP_CONCATstays callable with its old arguments. RemovegetGroupConcatParams. - DISTINCT is one generic wrapper around
stepthat skips a value already seen in the group, with today's key (distinctRowKeyforCOUNT(DISTINCT *)). compileGroupcallsinitwhen it creates a group,stepfor every row including the first, andresultwhen the groups are read. States live ing.aggs[j]from Match GROUP BY keys by structure and keep each group's state in slots #2647. A built-in may givestepJStext 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-byshows no slowdown beyond the noise described intest/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.
Activity
[-]Review #2418 for code improvements: GROUP_CONCAT special-cased in the generic aggregate code[/-][+]Give every aggregate the same init, step and result interface[/+]