Skip to content

Commit 70f469d

Browse files
ampagenttmm
andcommitted
feat(zero-swift): aggregates, distinct and groupBy in the IVM engine
Ports the JS client's compute mode: the Aggregate, GroupBy, GroupProjection and LiftField operators, the builder's paths for a top-level aggregate, a grouping, a count of distinct combinations and a relationship aggregate (a junction one through LiftField), and the view's aggregate values (ViewValue.value, 0 or null before any row). The untyped builder gets count/sum/avg/min/max (of a column or a JSON path), distinct and groupBy, and rejects what the JS builder throws for, with its messages. New vectors from JS: ivm-features gets curated aggregate, distinct, groupBy and relationship-aggregate cases (and the builder's errors), recording the pipeline's own rows as the server streams them; ivm-features-fuzz adds an aggregate or grouping to seeded random programs. Co-authored-by: tmm <tmm@tmm.dev> Amp-Thread-ID: https://ampcode.com/threads/T-01a19a37-5aa9-7256-bff5-cdc47c01a1fe
1 parent 680af92 commit 70f469d

12 files changed

Lines changed: 2030 additions & 41 deletions

File tree

‎.changeset/swift-ivm-aggregates.md‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'zero-swift': minor
3+
---
4+
5+
Queries can compute aggregates, `distinct()` and `groupBy()` on the client, with the IVM operators of the JS client (`Aggregate`, `GroupBy`, `GroupProjection` and `LiftField`), kept up to date as rows change. The untyped builder has `count()`, `sum`, `avg`, `min` and `max` (of a column, or of a value at a JSON path: `sum(json: "metadata", "points")`), `distinct(...)`, and `groupBy(...)` followed by an aggregate, with the JS builder's checks. On a query the view holds the value; in `related(...)` the relationship does, for a junction relationship too.
6+
7+
Breaking: `ViewValue` has a new case, `.value`, an aggregate's value; an exhaustive `switch` over it needs one too.

‎packages/zero-conformance/src/ivm/harness.ts‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
import type {JSONValue} from '../../../shared/src/json.ts';
2-
import type {AST, SimpleOperator} from '../../../zero-protocol/src/ast.ts';
2+
import type {
3+
AggregateFunction,
4+
AST,
5+
SimpleOperator,
6+
} from '../../../zero-protocol/src/ast.ts';
37
import type {Row} from '../../../zero-protocol/src/data.ts';
48
import type {Format} from '../../../zero-types/src/format.ts';
59
import type {Schema} from '../../../zero-types/src/schema.ts';
6-
import {buildPipeline} from '../../../zql/src/builder/builder.ts';
10+
import {
11+
buildPipeline,
12+
groupViewInput,
13+
} from '../../../zql/src/builder/builder.ts';
714
import {TestBuilderDelegate} from '../../../zql/src/builder/test-builder-delegate.ts';
815
import {ArrayView} from '../../../zql/src/ivm/array-view.ts';
916
import type {Change} from '../../../zql/src/ivm/change.ts';
@@ -44,7 +51,24 @@ export type Op =
4451
| {op: 'orderBy'; field: string; dir: 'asc' | 'desc'}
4552
| {op: 'limit'; n: number}
4653
| {op: 'start'; row: Row; inclusive?: boolean | undefined}
47-
| {op: 'one'};
54+
| {op: 'one'}
55+
| {op: 'count'}
56+
| {op: 'sum' | 'avg' | 'min' | 'max'; field: string; path?: JSONPath}
57+
| {op: 'distinct'; columns: string[]}
58+
| {
59+
op: 'groupBy';
60+
columns: string[];
61+
fn: AggregateFunction;
62+
field?: string | undefined;
63+
path?: JSONPath | undefined;
64+
};
65+
66+
type JSONPath = (string | number)[];
67+
68+
/** An aggregate's argument: a column, or `json(field, ...path)`. */
69+
function selector(field: string | undefined, path: JSONPath | undefined) {
70+
return path === undefined ? field : ({json}: AnyEB) => json(field, ...path);
71+
}
4872

4973
export type Expr =
5074
| {cmp: [field: string, op: SimpleOperator, value: JSONValue]}
@@ -128,6 +152,21 @@ function applyOp(q: AnyQuery, op: Op): AnyQuery {
128152
: query.start(op.row, {inclusive: op.inclusive});
129153
case 'one':
130154
return query.one();
155+
case 'count':
156+
return query.count();
157+
case 'sum':
158+
case 'avg':
159+
case 'min':
160+
case 'max':
161+
return query[op.op](selector(op.field, op.path));
162+
case 'distinct':
163+
return query.distinct(...op.columns);
164+
case 'groupBy': {
165+
const grouped = query.groupBy(...op.columns);
166+
return op.fn === 'count'
167+
? grouped.count()
168+
: grouped[op.fn](selector(op.field, op.path));
169+
}
131170
}
132171
}
133172

@@ -307,8 +346,13 @@ export function runQuery(
307346
let view: ArrayView<never>;
308347
try {
309348
const input = buildPipeline(ast, delegate, 'q');
349+
// Records the pipeline's own rows, as the server streams them; a view of
350+
// a grouped query shows only their columns and value.
310351
recorder = new Recorder(input);
311-
view = new ArrayView(recorder, format, true, () => {});
352+
const viewInput = ast.groupBy
353+
? groupViewInput(recorder, ast, delegate)
354+
: recorder;
355+
view = new ArrayView(viewInput, format, true, () => {});
312356
} catch (e) {
313357
return {error: String(e instanceof Error ? e.message : e)};
314358
}

0 commit comments

Comments
 (0)