Craticula

Aggregations

A count counts the rows where the named field has a value, never all rows. The rules also say what the result column is called and how grouping keys are matched. An aggregation over a group with no values has a defined answer, and a known difference in the placement of null groups on OpenSearch is recorded.

enumeration of SEM-1..SEM-7; pinned here anyway because the conformance fixture suite, #1630, needed answers that AR-8/BM-2 don't give and no other document owns)

AGG-1. count aggregates non-null occurrences of the named field — never a row count. AR-8's aggregation is (field, func); for func: "count", the field is not decorative — count means "how many rows have a non-null value for field", identically to SQL's COUNT(field) (which already skips NULLs) and Elasticsearch's value_count (which already skips missing). MongoDB's naive { "$sum": 1 } counts every document in the group regardless of the field's value and MUST NOT be used — it silently answers a different question the moment the aggregated field can be null. The correct, portable Mongo form is a conditional sum:

{ "$sum": { "$cond": [ { "$eq": ["$amount", null] }, 0, 1 ] } }

If a future host genuinely wants "count of rows in the group" regardless of nullability, that is a count on the grid's own stableKey (never null by SEM-6/BM-2's own tiebreaker guarantee), not a different meaning smuggled into count on an arbitrary field.

AGG-2. aggregation.alias is set from the AG Grid ColumnVO.id whenever it differs from ColumnVO.field; otherwise it is absent (compiler default applies). AG Grid's valueCols entries carry both an id (the column's identity in the grid) and a field (the data field being aggregated) — when a host configures a distinct id for an aggregated column (a common pattern for disambiguating two aggregations on the same field, e.g. sum(amount) and avg(amount) both keyed off field: "amount"), that id is the label the grid UI already displays for the column and is the only signal available for what the output key should be. The adapter therefore sets aggregation.alias := ColumnVO.id if and only if ColumnVO.id !== ColumnVO.field; when they are equal (the common case — AG Grid defaults id to field unless the host overrides it), alias is left absent and the compiler's own default naming applies. This is a one-line, deterministic rule — "is id distinct from field" — precisely so that every fixture/port computes the identical answer from the identical ColumnVO shape, rather than each guessing when alias-setting is warranted.

AGG-3. A groupKeys entry is a typed value (AR-7), compiled into an equality predicate against its group column. It is a value like any other filter value and is typed from the group column's declared schema type before the IR is built, never left as a raw wire string.

Superseded in part by GRP-2 (#1640). This rule previously also said that the drill-down match "follows the group column's own case-sensitivity setting (SEM-1) — it is not automatically exact-match". That is now wrong: the grouping addendum makes the drill-down, the GROUP BY and the grouped ORDER BY exact together, because they must agree with one another or a child level stops summing to its parent's aggregate, and because a collated GROUP BY key is not valid SQL. The sentence is recorded here rather than deleted silently, since the affected artifacts (group-aggregation/002-012, 015 on all three backends) were authored against it.

AGG-4. ES/OpenSearch terms aggregation ordering (BM-3 set-filter values, and any composite group aggregation) is pinned to ascending by key value, not the engine default. A bare terms aggregation defaults to doc-count-descending, which would return set-filter values (and, for composite grouping, bucket order) in a different order than Postgres's ORDER BY or Mongo's $sort on the same logical request — a user-visible list-order divergence for an operation (BM-3) whose whole purpose is presenting a sorted list of distinct values. Compilers MUST set "order": {"_key": "asc"} explicitly on any terms aggregation backing a set-filter-values or grouped-drill-down request.

AGG-5. Known, accepted divergence: composite-aggregation null-bucket placement on ES/OpenSearch. ES/OpenSearch's composite aggregation missing_bucket: true (the mechanism that keeps a null group key from being silently dropped, matching AR-8/BM-2's intent) has no missing_order control in the pinned 7.10-era subset — the null bucket sorts first regardless of the sort direction requested, while Postgres (NULLS LAST/NULLS FIRST per SEM-3) and Mongo (the null-bucket emulation) both honor the requested direction. This is documented here as an accepted, host-visible divergence (per the TRD §15 risk register's own "documented-divergence register" precedent) rather than worked around with a client-side reorder, which would defeat server-side paging's whole purpose for the grouped case. Fixtures exercising composite null-bucket ordering mark this explicitly rather than silently pinning three-way-identical output that ES/OpenSearch cannot actually produce.

AGG-6. An aggregate over a group with no non-null values: sum is 0; avg, min and max are null. "No non-null values" covers both shapes a host can hit — a group whose every row carries NULL in the aggregated field, and (on the backends where an aggregation runs over an empty input at all) a group with no rows. count is not part of this rule and needs no clause: AGG-1 already makes it a count of non-null occurrences, which is 0 on all four backends by construction.

Where the four backends stand today, and who has to move. Three of them already answer this way; one does not:

sum, no non-null valuesavg / min / max
SQL compilerSUM(x) → NULL — the outlierbare call → null
EF Core (AR-4, since #1887)provider emits COALESCE(SUM(x), 0.0) → 0bare call → null
MongoDB{"$sum": ref} → 0$avg/$min/$max → null
ES/OpenSearchsum metric → 0avg/min/max → null

So compilers targeting SQL MUST wrap sum, and only sum, in a two-argument null-coalesce with a literal zero — COALESCE(SUM(x), 0), which is ANSI and spelled identically by all five v1 dialects, so it is composed in the builder rather than behind BM-1's dialect seam. They MUST NOT wrap avg, min or max: those three already agree with the other backends, and a wrap there would turn "this group has nothing to average" into a fabricated 0. Mongo and Search compilers change nothing; in particular they MUST NOT grow a companion count and a conditional in order to recover a NULL.

Why 0 and not NULL, which was the incumbent SQL answer. NULL is not recoverable on the two document stores. $sum and the sum metric return 0 for an empty set, and a genuine sum of zero is indistinguishable from an empty one, so a NULL rule would oblige every sum on both backends to carry a companion count plus a conditional on top of it. 0 costs one COALESCE on one backend. It is therefore the only one of the two answers implementable on all four, which is what G3 (one logical answer per request, whatever the backend) asks for — and it is the answer EF Core's provider already generates unprompted.

What this rests on, stated plainly — because no tier in this repository grades it. An artifact compares emitted text; it cannot see what a number comes back as. The four rows above are therefore evidence of four different strengths, and collapsing them into one confident table would be the dishonest move:

Confirming all four against real servers is the T-1b execution tier's (#1624), and the natural first case is precisely an all-null group. Until that runs, this rule is a decision backed by documentation, not an observation — a narrower claim than the other AGG-* rules make, recorded rather than smoothed over.

The ordering consequence is part of the rule, not a side effect. GRP-4 makes a grouped level's ORDER BY repeat the aggregate expression, so the wrap appears there too, and ORDER BY COALESCE(SUM(x), 0) places a group with no non-null values among the zeroes where ORDER BY SUM(x) placed it with the NULLs — i.e. wherever AR-9/SEM-3's nullsPosition had sent it. Row order for such a group legitimately changes, on every SQL dialect at once. The nullsPosition machinery is still emitted around the wrapped expression (MySQL's leading ISNULL(…) key, SQL Server's CASE WHEN … IS NULL, the others' NULLS FIRST/LAST) even though it can no longer fire: SEM-6's "no schema state licenses omitting the marker" is unconditional, and suppressing a sort key on a nullability inference is a semantic special case no rule grants. On an avg/min/max ordering key the same machinery is fully live, which is the difference the fixtures pin.

Pinned by every grouped SQL artifact carrying a sum — group-aggregation/001-004, 007, 012-015, 020, 021, 023 on all five dialects, and ag-grid-majors/004 on Postgres only — and discriminated in the ORDER BY by dialect-parity/024, which orders a grouped level by an avg while a wrapped sum sits beside it in the same projection. No other fixture orders a grouped level by anything but a sum or a group column, so 024 is the only case that fails when the wrap is missing from a sum's ORDER BY and when it is over-applied to avg's. It does not follow that over-applying the wrap goes otherwise undetected: AVG/MIN/MAX appear bare in the projection of 008-010, 012, 013 as well, and those fail exactly the same way 024 does if the wrap reaches their SELECT list.