@rocicorp/zero 1.13.0-canary.2 → 1.13.0-canary.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/out/z2s/src/compiler.d.ts.map +1 -1
- package/out/z2s/src/compiler.js +6 -6
- package/out/z2s/src/compiler.js.map +1 -1
- package/out/zero/package.js +1 -1
- package/out/zero/package.js.map +1 -1
- package/out/zero-cache/src/server/replicator.js +3 -3
- package/out/zero-cache/src/server/replicator.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/change-source-init.js +14 -4
- package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts +9 -0
- package/out/zero-cache/src/services/change-source/pg/schema/shard.d.ts.map +1 -1
- package/out/zero-cache/src/services/change-source/pg/schema/shard.js +23 -1
- package/out/zero-cache/src/services/change-source/pg/schema/shard.js.map +1 -1
- package/out/zero-cache/src/services/replicator/write-worker-client.js +17 -3
- package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
- package/out/zero-cache/src/services/replicator/write-worker.js +11 -2
- package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
- package/out/zero-cache/src/services/view-syncer/view-syncer.js +13 -15
- package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
- package/out/zero-client/src/client/custom.js +1 -1
- package/out/zero-client/src/client/mutation-tracker.js +1 -1
- package/out/zero-client/src/client/version.js +1 -1
- package/out/zero-client/src/client/zero.js +1 -1
- package/out/zero-protocol/src/ast.d.ts +2 -2
- package/out/zero-protocol/src/ast.js +2 -2
- package/out/zero-protocol/src/ast.js.map +1 -1
- package/out/zero-protocol/src/down.d.ts +27 -27
- package/out/zero-protocol/src/inspect-down.d.ts +81 -81
- package/out/zql/src/ivm/aggregate.js +5 -4
- package/out/zql/src/ivm/aggregate.js.map +1 -1
- package/out/zql/src/ivm/cap.d.ts.map +1 -1
- package/out/zql/src/ivm/cap.js +116 -63
- package/out/zql/src/ivm/cap.js.map +1 -1
- package/out/zql/src/ivm/deferred-input.js +1 -1
- package/out/zql/src/ivm/exists.d.ts.map +1 -1
- package/out/zql/src/ivm/exists.js +136 -73
- package/out/zql/src/ivm/exists.js.map +1 -1
- package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
- package/out/zql/src/ivm/fan-in.js +3 -3
- package/out/zql/src/ivm/fan-in.js.map +1 -1
- package/out/zql/src/ivm/fan-out.d.ts +2 -2
- package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
- package/out/zql/src/ivm/fan-out.js +47 -12
- package/out/zql/src/ivm/fan-out.js.map +1 -1
- package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
- package/out/zql/src/ivm/filter-operators.js +98 -13
- package/out/zql/src/ivm/filter-operators.js.map +1 -1
- package/out/zql/src/ivm/filter-push.d.ts.map +1 -1
- package/out/zql/src/ivm/filter-push.js +5 -13
- package/out/zql/src/ivm/filter-push.js.map +1 -1
- package/out/zql/src/ivm/filter.d.ts.map +1 -1
- package/out/zql/src/ivm/filter.js +3 -2
- package/out/zql/src/ivm/filter.js.map +1 -1
- package/out/zql/src/ivm/flipped-join.js +1 -0
- package/out/zql/src/ivm/flipped-join.js.map +1 -1
- package/out/zql/src/ivm/group-by.js +1 -1
- package/out/zql/src/ivm/join-index-tap.d.ts +1 -1
- package/out/zql/src/ivm/join-index-tap.d.ts.map +1 -1
- package/out/zql/src/ivm/join-index-tap.js +9 -7
- package/out/zql/src/ivm/join-index-tap.js.map +1 -1
- package/out/zql/src/ivm/join.d.ts.map +1 -1
- package/out/zql/src/ivm/join.js +72 -67
- package/out/zql/src/ivm/join.js.map +1 -1
- package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
- package/out/zql/src/ivm/memory-source.js +163 -57
- package/out/zql/src/ivm/memory-source.js.map +1 -1
- package/out/zql/src/ivm/push-accumulated.d.ts +1 -1
- package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -1
- package/out/zql/src/ivm/push-accumulated.js +23 -28
- package/out/zql/src/ivm/push-accumulated.js.map +1 -1
- package/out/zql/src/ivm/skip.d.ts.map +1 -1
- package/out/zql/src/ivm/skip.js +6 -9
- package/out/zql/src/ivm/skip.js.map +1 -1
- package/out/zql/src/ivm/stream.d.ts +58 -0
- package/out/zql/src/ivm/stream.d.ts.map +1 -1
- package/out/zql/src/ivm/stream.js +256 -1
- package/out/zql/src/ivm/stream.js.map +1 -1
- package/out/zql/src/ivm/take-gate.d.ts +1 -1
- package/out/zql/src/ivm/take-gate.d.ts.map +1 -1
- package/out/zql/src/ivm/take-gate.js +11 -21
- package/out/zql/src/ivm/take-gate.js.map +1 -1
- package/out/zql/src/ivm/take.d.ts.map +1 -1
- package/out/zql/src/ivm/take.js +243 -217
- package/out/zql/src/ivm/take.js.map +1 -1
- package/out/zql/src/query/query.d.ts +4 -4
- package/package.json +1 -1
|
@@ -99,33 +99,6 @@ export declare const inspectAnalyzeQueryDownSchema: v.ObjectType<Omit<{
|
|
|
99
99
|
type: "attempt-start";
|
|
100
100
|
attemptNumber: number;
|
|
101
101
|
totalAttempts: number;
|
|
102
|
-
} | {
|
|
103
|
-
type: "connection-selected";
|
|
104
|
-
attemptNumber: number;
|
|
105
|
-
connection: string;
|
|
106
|
-
cost: number;
|
|
107
|
-
isRoot: boolean;
|
|
108
|
-
} | {
|
|
109
|
-
type: "plan-failed";
|
|
110
|
-
attemptNumber: number;
|
|
111
|
-
reason: string;
|
|
112
|
-
} | {
|
|
113
|
-
type: "best-plan-selected";
|
|
114
|
-
bestAttemptNumber: number;
|
|
115
|
-
totalCost: number;
|
|
116
|
-
flipPattern: number;
|
|
117
|
-
joinStates: {
|
|
118
|
-
join: string;
|
|
119
|
-
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
120
|
-
}[];
|
|
121
|
-
} | {
|
|
122
|
-
type: "node-constraint";
|
|
123
|
-
attemptNumber?: number | undefined;
|
|
124
|
-
nodeType: "connection" | "fan-in" | "fan-out" | "filter" | "join" | "terminus" | v.Unrecognized<string>;
|
|
125
|
-
node: string;
|
|
126
|
-
branchPattern: number[];
|
|
127
|
-
constraint?: Record<string, unknown> | null | undefined;
|
|
128
|
-
from: string;
|
|
129
102
|
} | {
|
|
130
103
|
type: "connection-costs";
|
|
131
104
|
attemptNumber: number;
|
|
@@ -151,6 +124,12 @@ export declare const inspectAnalyzeQueryDownSchema: v.ObjectType<Omit<{
|
|
|
151
124
|
limit?: number | undefined;
|
|
152
125
|
}>;
|
|
153
126
|
}[];
|
|
127
|
+
} | {
|
|
128
|
+
type: "connection-selected";
|
|
129
|
+
attemptNumber: number;
|
|
130
|
+
connection: string;
|
|
131
|
+
cost: number;
|
|
132
|
+
isRoot: boolean;
|
|
154
133
|
} | {
|
|
155
134
|
type: "constraints-propagated";
|
|
156
135
|
attemptNumber: number;
|
|
@@ -175,6 +154,19 @@ export declare const inspectAnalyzeQueryDownSchema: v.ObjectType<Omit<{
|
|
|
175
154
|
join: string;
|
|
176
155
|
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
177
156
|
}[];
|
|
157
|
+
} | {
|
|
158
|
+
type: "plan-failed";
|
|
159
|
+
attemptNumber: number;
|
|
160
|
+
reason: string;
|
|
161
|
+
} | {
|
|
162
|
+
type: "best-plan-selected";
|
|
163
|
+
bestAttemptNumber: number;
|
|
164
|
+
totalCost: number;
|
|
165
|
+
flipPattern: number;
|
|
166
|
+
joinStates: {
|
|
167
|
+
join: string;
|
|
168
|
+
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
169
|
+
}[];
|
|
178
170
|
} | {
|
|
179
171
|
type: "node-cost";
|
|
180
172
|
attemptNumber?: number | undefined;
|
|
@@ -193,6 +185,14 @@ export declare const inspectAnalyzeQueryDownSchema: v.ObjectType<Omit<{
|
|
|
193
185
|
filters?: import("./ast.ts").Condition | undefined;
|
|
194
186
|
ordering?: readonly (readonly [string, "asc" | "desc"])[] | undefined;
|
|
195
187
|
joinType?: "flipped" | "semi" | "unflippable" | v.Unrecognized<string> | undefined;
|
|
188
|
+
} | {
|
|
189
|
+
type: "node-constraint";
|
|
190
|
+
attemptNumber?: number | undefined;
|
|
191
|
+
nodeType: "connection" | "fan-in" | "fan-out" | "filter" | "join" | "terminus" | v.Unrecognized<string>;
|
|
192
|
+
node: string;
|
|
193
|
+
branchPattern: number[];
|
|
194
|
+
constraint?: Record<string, unknown> | null | undefined;
|
|
195
|
+
from: string;
|
|
196
196
|
})[]>;
|
|
197
197
|
}, undefined>;
|
|
198
198
|
}, undefined>;
|
|
@@ -264,33 +264,6 @@ declare const inspectDownBodySchema: v.UnionType<[v.ObjectType<Omit<{
|
|
|
264
264
|
type: "attempt-start";
|
|
265
265
|
attemptNumber: number;
|
|
266
266
|
totalAttempts: number;
|
|
267
|
-
} | {
|
|
268
|
-
type: "connection-selected";
|
|
269
|
-
attemptNumber: number;
|
|
270
|
-
connection: string;
|
|
271
|
-
cost: number;
|
|
272
|
-
isRoot: boolean;
|
|
273
|
-
} | {
|
|
274
|
-
type: "plan-failed";
|
|
275
|
-
attemptNumber: number;
|
|
276
|
-
reason: string;
|
|
277
|
-
} | {
|
|
278
|
-
type: "best-plan-selected";
|
|
279
|
-
bestAttemptNumber: number;
|
|
280
|
-
totalCost: number;
|
|
281
|
-
flipPattern: number;
|
|
282
|
-
joinStates: {
|
|
283
|
-
join: string;
|
|
284
|
-
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
285
|
-
}[];
|
|
286
|
-
} | {
|
|
287
|
-
type: "node-constraint";
|
|
288
|
-
attemptNumber?: number | undefined;
|
|
289
|
-
nodeType: "connection" | "fan-in" | "fan-out" | "filter" | "join" | "terminus" | v.Unrecognized<string>;
|
|
290
|
-
node: string;
|
|
291
|
-
branchPattern: number[];
|
|
292
|
-
constraint?: Record<string, unknown> | null | undefined;
|
|
293
|
-
from: string;
|
|
294
267
|
} | {
|
|
295
268
|
type: "connection-costs";
|
|
296
269
|
attemptNumber: number;
|
|
@@ -316,6 +289,12 @@ declare const inspectDownBodySchema: v.UnionType<[v.ObjectType<Omit<{
|
|
|
316
289
|
limit?: number | undefined;
|
|
317
290
|
}>;
|
|
318
291
|
}[];
|
|
292
|
+
} | {
|
|
293
|
+
type: "connection-selected";
|
|
294
|
+
attemptNumber: number;
|
|
295
|
+
connection: string;
|
|
296
|
+
cost: number;
|
|
297
|
+
isRoot: boolean;
|
|
319
298
|
} | {
|
|
320
299
|
type: "constraints-propagated";
|
|
321
300
|
attemptNumber: number;
|
|
@@ -340,6 +319,19 @@ declare const inspectDownBodySchema: v.UnionType<[v.ObjectType<Omit<{
|
|
|
340
319
|
join: string;
|
|
341
320
|
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
342
321
|
}[];
|
|
322
|
+
} | {
|
|
323
|
+
type: "plan-failed";
|
|
324
|
+
attemptNumber: number;
|
|
325
|
+
reason: string;
|
|
326
|
+
} | {
|
|
327
|
+
type: "best-plan-selected";
|
|
328
|
+
bestAttemptNumber: number;
|
|
329
|
+
totalCost: number;
|
|
330
|
+
flipPattern: number;
|
|
331
|
+
joinStates: {
|
|
332
|
+
join: string;
|
|
333
|
+
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
334
|
+
}[];
|
|
343
335
|
} | {
|
|
344
336
|
type: "node-cost";
|
|
345
337
|
attemptNumber?: number | undefined;
|
|
@@ -358,6 +350,14 @@ declare const inspectDownBodySchema: v.UnionType<[v.ObjectType<Omit<{
|
|
|
358
350
|
filters?: import("./ast.ts").Condition | undefined;
|
|
359
351
|
ordering?: readonly (readonly [string, "asc" | "desc"])[] | undefined;
|
|
360
352
|
joinType?: "flipped" | "semi" | "unflippable" | v.Unrecognized<string> | undefined;
|
|
353
|
+
} | {
|
|
354
|
+
type: "node-constraint";
|
|
355
|
+
attemptNumber?: number | undefined;
|
|
356
|
+
nodeType: "connection" | "fan-in" | "fan-out" | "filter" | "join" | "terminus" | v.Unrecognized<string>;
|
|
357
|
+
node: string;
|
|
358
|
+
branchPattern: number[];
|
|
359
|
+
constraint?: Record<string, unknown> | null | undefined;
|
|
360
|
+
from: string;
|
|
361
361
|
})[]>;
|
|
362
362
|
}, undefined>;
|
|
363
363
|
}, undefined>, v.ObjectType<Omit<{
|
|
@@ -427,33 +427,6 @@ export declare const inspectDownMessageSchema: v.TupleType<[v.Type<"inspect">, v
|
|
|
427
427
|
type: "attempt-start";
|
|
428
428
|
attemptNumber: number;
|
|
429
429
|
totalAttempts: number;
|
|
430
|
-
} | {
|
|
431
|
-
type: "connection-selected";
|
|
432
|
-
attemptNumber: number;
|
|
433
|
-
connection: string;
|
|
434
|
-
cost: number;
|
|
435
|
-
isRoot: boolean;
|
|
436
|
-
} | {
|
|
437
|
-
type: "plan-failed";
|
|
438
|
-
attemptNumber: number;
|
|
439
|
-
reason: string;
|
|
440
|
-
} | {
|
|
441
|
-
type: "best-plan-selected";
|
|
442
|
-
bestAttemptNumber: number;
|
|
443
|
-
totalCost: number;
|
|
444
|
-
flipPattern: number;
|
|
445
|
-
joinStates: {
|
|
446
|
-
join: string;
|
|
447
|
-
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
448
|
-
}[];
|
|
449
|
-
} | {
|
|
450
|
-
type: "node-constraint";
|
|
451
|
-
attemptNumber?: number | undefined;
|
|
452
|
-
nodeType: "connection" | "fan-in" | "fan-out" | "filter" | "join" | "terminus" | v.Unrecognized<string>;
|
|
453
|
-
node: string;
|
|
454
|
-
branchPattern: number[];
|
|
455
|
-
constraint?: Record<string, unknown> | null | undefined;
|
|
456
|
-
from: string;
|
|
457
430
|
} | {
|
|
458
431
|
type: "connection-costs";
|
|
459
432
|
attemptNumber: number;
|
|
@@ -479,6 +452,12 @@ export declare const inspectDownMessageSchema: v.TupleType<[v.Type<"inspect">, v
|
|
|
479
452
|
limit?: number | undefined;
|
|
480
453
|
}>;
|
|
481
454
|
}[];
|
|
455
|
+
} | {
|
|
456
|
+
type: "connection-selected";
|
|
457
|
+
attemptNumber: number;
|
|
458
|
+
connection: string;
|
|
459
|
+
cost: number;
|
|
460
|
+
isRoot: boolean;
|
|
482
461
|
} | {
|
|
483
462
|
type: "constraints-propagated";
|
|
484
463
|
attemptNumber: number;
|
|
@@ -503,6 +482,19 @@ export declare const inspectDownMessageSchema: v.TupleType<[v.Type<"inspect">, v
|
|
|
503
482
|
join: string;
|
|
504
483
|
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
505
484
|
}[];
|
|
485
|
+
} | {
|
|
486
|
+
type: "plan-failed";
|
|
487
|
+
attemptNumber: number;
|
|
488
|
+
reason: string;
|
|
489
|
+
} | {
|
|
490
|
+
type: "best-plan-selected";
|
|
491
|
+
bestAttemptNumber: number;
|
|
492
|
+
totalCost: number;
|
|
493
|
+
flipPattern: number;
|
|
494
|
+
joinStates: {
|
|
495
|
+
join: string;
|
|
496
|
+
type: "flipped" | "semi" | "unflippable" | v.Unrecognized<string>;
|
|
497
|
+
}[];
|
|
506
498
|
} | {
|
|
507
499
|
type: "node-cost";
|
|
508
500
|
attemptNumber?: number | undefined;
|
|
@@ -521,6 +513,14 @@ export declare const inspectDownMessageSchema: v.TupleType<[v.Type<"inspect">, v
|
|
|
521
513
|
filters?: import("./ast.ts").Condition | undefined;
|
|
522
514
|
ordering?: readonly (readonly [string, "asc" | "desc"])[] | undefined;
|
|
523
515
|
joinType?: "flipped" | "semi" | "unflippable" | v.Unrecognized<string> | undefined;
|
|
516
|
+
} | {
|
|
517
|
+
type: "node-constraint";
|
|
518
|
+
attemptNumber?: number | undefined;
|
|
519
|
+
nodeType: "connection" | "fan-in" | "fan-out" | "filter" | "join" | "terminus" | v.Unrecognized<string>;
|
|
520
|
+
node: string;
|
|
521
|
+
branchPattern: number[];
|
|
522
|
+
constraint?: Record<string, unknown> | null | undefined;
|
|
523
|
+
from: string;
|
|
524
524
|
})[]>;
|
|
525
525
|
}, undefined>;
|
|
526
526
|
}, undefined>, v.ObjectType<Omit<{
|
|
@@ -5,6 +5,7 @@ import { aggregateValue } from "../../../zero-protocol/src/ast.js";
|
|
|
5
5
|
import { makeEditChange } from "./change.js";
|
|
6
6
|
import { compareValues, makeComparator, normalizeUndefined, valueToKey } from "./data.js";
|
|
7
7
|
import { throwOutput } from "./operator.js";
|
|
8
|
+
import "./stream.js";
|
|
8
9
|
//#region ../zql/src/ivm/aggregate.ts
|
|
9
10
|
/**
|
|
10
11
|
* The name of the synthetic column that holds the aggregate result in the rows
|
|
@@ -448,12 +449,12 @@ var Aggregate = class {
|
|
|
448
449
|
};
|
|
449
450
|
/**
|
|
450
451
|
* Orders the values a min/max reduces. A JSON path's leaves can be numbers or
|
|
451
|
-
* strings, decided per value;
|
|
452
|
-
*
|
|
453
|
-
*
|
|
452
|
+
* strings, decided per value; strings order before numbers, as in Postgres's
|
|
453
|
+
* jsonb ordering and in rindle (z2s compiles the same rule). Otherwise it is
|
|
454
|
+
* {@link compareValues}.
|
|
454
455
|
*/
|
|
455
456
|
function compareAggregateValues(a, b) {
|
|
456
|
-
if (a !== null && b !== null && typeof a !== typeof b && (typeof a === "number" || typeof a === "string") && (typeof b === "number" || typeof b === "string")) return typeof a === "
|
|
457
|
+
if (a !== null && b !== null && typeof a !== typeof b && (typeof a === "number" || typeof a === "string") && (typeof b === "number" || typeof b === "string")) return typeof a === "string" ? -1 : 1;
|
|
457
458
|
return compareValues(a, b);
|
|
458
459
|
}
|
|
459
460
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregate.js","names":[],"sources":["../../../../../zql/src/ivm/aggregate.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {assignProperty} from '../../../shared/src/objects.ts';\nimport {\n aggregateValue,\n type AggregateFunction,\n type Ordering,\n} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport type {SchemaValue} from '../../../zero-types/src/schema-value.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeEditChange, type Change} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {\n compareValues,\n makeComparator,\n normalizeUndefined,\n valueToKey,\n type Node,\n type NormalizedValue,\n} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {SourceInput} from './source.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The name of the synthetic column that holds the aggregate result in the rows\n * this operator emits. The value is projected to a bare scalar in the view, so\n * this name is internal.\n *\n * The synthetic columns share the emitted row with the group key, whose names\n * come from the user's schema, so they use the reserved `_0_` prefix (as\n * `_0_version` does) rather than plain words a schema could also use.\n */\nexport const AGGREGATE_VALUE_COLUMN = '_0_value';\n\n/**\n * A top-level (ungrouped) aggregate has no group key, but the synced path keys\n * every row in the CVR and forbids empty row keys (see zero-cache\n * `types/row-key.ts`). Since there is no group key, the single global row is\n * given one synthetic, constant-valued key column so it still has a (non-empty)\n * primary key. The materialized-view path ignores it (it projects only\n * `AGGREGATE_VALUE_COLUMN`), so this name is internal.\n *\n * The row holds nothing but this column and the payload columns, so any name\n * other than theirs (and `_0_version`) is collision-free. It must not be empty:\n * a SQLite-backed source cannot declare a zero-length column name.\n */\nexport const AGGREGATE_KEY_COLUMN = '_0_key';\n\n/** The constant value of {@link AGGREGATE_KEY_COLUMN} (one global row). */\nconst AGGREGATE_KEY_VALUE = 0;\n\n/**\n * `sum` and `avg` additionally carry their running components so the synced\n * client can adjust them optimistically: you can't move an average from the\n * final value alone, and a sum's value alone can't tell an empty group (`null`)\n * from one summing to 0. The value column stays the result (for display); these are the\n * sum of non-null field values and the count of non-null contributors.\n */\nexport const AGGREGATE_SUM_COLUMN = '_0_sum';\nexport const AGGREGATE_COUNT_COLUMN = '_0_count';\n\n/**\n * A grouped `sum`/`avg` row also carries its group's number of rows, nulls\n * included, which neither component gives: the synced client adds a group with\n * its first row and removes it with its last, as it does a count's.\n */\nexport const AGGREGATE_ROWS_COLUMN = '_0_rows';\n\n/**\n * Whether `col` is the name of a synthetic column of an aggregate or grouped\n * row: `_0_key` or a payload column.\n */\nexport function isSyntheticColumn(col: string): boolean {\n return col === AGGREGATE_KEY_COLUMN || AGGREGATE_PAYLOAD_COLUMNS.has(col);\n}\n\n/**\n * Asserts that the group key column `col` of an aggregate, or a column of a\n * grouped query, is not a synthetic column. The synthetic columns are written over\n * the group's columns in the emitted row, the payload is stripped from it by\n * the client's row key and `_0_key` alone keys a row that has it, so a\n * collision would silently merge or corrupt the groups. Their reserved `_0_`\n * prefix keeps real schemas clear of this; the check guards the invariant.\n */\nexport function assertNotSyntheticColumn(col: string): void {\n assert(\n !isSyntheticColumn(col),\n () =>\n `Aggregate: group key column \"${col}\" collides with the synthetic ` +\n `aggregate column of the same name`,\n );\n}\n\n/** Whether `fn`'s rows carry the sum and count components. */\nexport function carriesComponents(fn: AggregateFunction): boolean {\n return fn === 'sum' || fn === 'avg';\n}\n\n/** The synthetic payload columns (not part of an aggregate row's key). */\nexport const AGGREGATE_PAYLOAD_COLUMNS: ReadonlySet<string> = new Set([\n AGGREGATE_VALUE_COLUMN,\n AGGREGATE_SUM_COLUMN,\n AGGREGATE_COUNT_COLUMN,\n AGGREGATE_ROWS_COLUMN,\n]);\n\n/**\n * The type of the synthetic value column for an aggregate result: `count` is a\n * non-null number; `sum`/`avg` are nullable numbers; `min`/`max` take the\n * field's own type (`json` for a JSON path, whose leaf can be a number or a\n * string) and are nullable (empty group).\n */\nfunction aggregateValueColumn(\n fn: AggregateFunction,\n field: string | undefined,\n inputColumns: Record<string, SchemaValue>,\n path: readonly (string | number)[] | undefined,\n): SchemaValue {\n switch (fn) {\n case 'count':\n return {type: 'number'};\n case 'sum':\n case 'avg':\n return {type: 'number', optional: true};\n case 'min':\n case 'max': {\n if (path !== undefined) {\n // A JSON leaf: a number or a string, decided per value.\n return {type: 'json', optional: true};\n }\n const fieldSchema = inputColumns[must(field)];\n assert(fieldSchema, () => `Aggregate: ${fn} field \"${field}\" missing`);\n return {...fieldSchema, optional: true};\n }\n default:\n unreachable(fn);\n }\n}\n\n/**\n * The synthetic schema (columns + primary key) an aggregate produces, derived\n * from the *input* columns (the rows being aggregated), the group key, and the\n * fn/field. Shared by the {@link Aggregate} operator (compute mode) and the\n * client query builder (`aggregatesFromSource` mode) so both agree on the\n * synthetic source's shape. For a top-level (empty group key) aggregate the\n * single synthetic key column is added; otherwise the group key is the key.\n */\nexport function aggregateSourceSchema(\n inputColumns: Record<string, SchemaValue>,\n groupKey: readonly string[],\n fn: AggregateFunction,\n field: string | undefined,\n path?: readonly (string | number)[] | undefined,\n): {columns: Record<string, SchemaValue>; primaryKey: readonly string[]} {\n const columns: Record<string, SchemaValue> = {};\n for (const col of groupKey) {\n assertNotSyntheticColumn(col);\n const colSchema = inputColumns[col];\n assert(colSchema, () => `Aggregate: group key column \"${col}\" missing`);\n assignProperty(columns, col, colSchema);\n }\n const keyColumns = groupKey.length > 0 ? groupKey : [AGGREGATE_KEY_COLUMN];\n if (groupKey.length === 0) {\n columns[AGGREGATE_KEY_COLUMN] = {type: 'number'};\n }\n columns[AGGREGATE_VALUE_COLUMN] = aggregateValueColumn(\n fn,\n field,\n inputColumns,\n path,\n );\n if (carriesComponents(fn)) {\n // Components for optimistic adjustment on the synced client.\n columns[AGGREGATE_SUM_COLUMN] = {type: 'number'};\n columns[AGGREGATE_COUNT_COLUMN] = {type: 'number'};\n }\n return {columns, primaryKey: keyColumns};\n}\n\n/**\n * Per-group running state. We keep enough to derive any supported function:\n * - `count` → count(*) (all rows)\n * - `sum` → sum of non-null (NULL when nonNull === 0)\n * - `avg` → sum / nonNull (NULL when nonNull === 0)\n * - `min`/`max`→ extreme of non-null (NULL when nonNull === 0)\n */\nexport type AggState = {\n count: number;\n sum: number;\n nonNull: number;\n /** Current min/max of the non-null field values (null when nonNull === 0). */\n extreme: NormalizedValue;\n};\n\n/**\n * Typed view of the operator's {@link Storage} (mirrors Take): a group's state\n * by its key. Shared with `GroupBy`.\n */\nexport interface AggStorage {\n get(key: string): AggState | undefined;\n set(key: string, value: AggState): void;\n del(key: string): void;\n destroy(): void;\n}\n\n/**\n * Whether a change to one input row maps to a fixed delta of the aggregate\n * (`count`/`sum`/`avg`). Shared with the synced client, which only applies\n * optimistic deltas to these functions.\n */\nexport const isInvertible = (fn: AggregateFunction) =>\n fn === 'count' || fn === 'sum' || fn === 'avg';\n\n/**\n * How an aggregate function reduces a group's rows into an {@link AggState},\n * and reads its result back. Shared by {@link Aggregate}, which keeps a state\n * per parent of a relationship, and `GroupBy`, which keeps one per group.\n */\nexport class Reducer {\n readonly fn: AggregateFunction;\n readonly field: string | undefined;\n /** A JSON path into `field` (see `Aggregate.path`). */\n readonly path: readonly (string | number)[] | undefined;\n\n constructor(\n fn: AggregateFunction,\n field: string | undefined,\n path: readonly (string | number)[] | undefined,\n ) {\n assert(\n fn === 'count' ? field === undefined : field !== undefined,\n () =>\n `Aggregate: ${fn} ${fn === 'count' ? 'takes no' : 'requires a'} field`,\n );\n assert(\n path === undefined || field !== undefined,\n 'Aggregate: a JSON path requires a field',\n );\n this.fn = fn;\n this.field = field;\n this.path = path;\n }\n\n /** The state of a group with no rows. */\n empty(): AggState {\n return {count: 0, sum: 0, nonNull: 0, extreme: null};\n }\n\n /** The scalar result for a state, per the aggregate function. */\n value(s: AggState): Value {\n switch (this.fn) {\n case 'count':\n return s.count;\n case 'sum':\n return s.nonNull === 0 ? null : s.sum;\n case 'avg':\n return s.nonNull === 0 ? null : s.sum / s.nonNull;\n case 'min':\n case 'max':\n return s.extreme; // null when nonNull === 0\n default:\n unreachable(this.fn);\n }\n }\n\n /** Whether `v` should replace the current extreme for a min/max. */\n #beats(v: NormalizedValue, current: NormalizedValue): boolean {\n const c = compareAggregateValues(v, current);\n return this.fn === 'max' ? c > 0 : c < 0;\n }\n\n /**\n * The value `row` contributes: its field, or the JSON leaf at the path (see\n * `aggregateValue`), `null` when it contributes nothing.\n */\n #read(row: Row): NormalizedValue {\n const field = must(this.field);\n return normalizeUndefined(\n this.path === undefined\n ? row[field]\n : (aggregateValue({fn: this.fn, field, path: this.path}, row) as Value),\n );\n }\n\n /** Whether an edit from `oldRow` to `newRow` leaves the value it reduces. */\n sameValue(oldRow: Row, newRow: Row): boolean {\n return (\n this.field === undefined ||\n compareAggregateValues(this.#read(oldRow), this.#read(newRow)) === 0\n );\n }\n\n /** Add one row's contribution to a (freshly accumulating) state. */\n contribute(s: AggState, row: Row): void {\n s.count++;\n if (this.field === undefined) {\n return;\n }\n const v = this.#read(row);\n if (v === null) {\n return;\n }\n s.nonNull++;\n switch (this.fn) {\n case 'sum':\n case 'avg':\n assert(\n typeof v === 'number',\n () => `Aggregate: ${this.fn} needs a number`,\n );\n s.sum += v;\n break;\n case 'min':\n case 'max':\n if (s.extreme === null || this.#beats(v, s.extreme)) {\n s.extreme = v;\n }\n break;\n default:\n break;\n }\n }\n\n /** Invertible delta for count/sum/avg. */\n applyInvertible(\n state: AggState,\n row: Row,\n sign: 1 | -1,\n fieldOnly = false,\n ): void {\n if (!fieldOnly) {\n state.count += sign;\n }\n if (this.field !== undefined) {\n const v = this.#read(row);\n if (v !== null) {\n assert(\n typeof v === 'number',\n () => `Aggregate: ${this.fn}(${this.field}) requires a number`,\n );\n state.sum += sign * v;\n state.nonNull += sign;\n }\n }\n }\n\n /**\n * min/max maintenance, applied to `state` in place. Cheap for adds and for\n * removing a non-extreme value. Returns false when the current extreme is\n * removed or edited away: the new extreme is not recoverable from running\n * state, so the group has to be re-fetched.\n */\n pushExtreme(state: AggState, change: Change): boolean {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n // Adding can only extend the extreme outward — O(1).\n this.contribute(state, change[ChangeIndex.NODE].row);\n return true;\n case ChangeType.REMOVE: {\n const v = this.#read(change[ChangeIndex.NODE].row);\n state.count--;\n if (v === null) {\n return true; // null never contributed to the extreme\n }\n if (\n state.extreme !== null &&\n compareAggregateValues(v, state.extreme) === 0\n ) {\n return false; // removed (one of) the extreme\n }\n state.nonNull--; // a non-extreme value left; extreme unchanged\n return true;\n }\n case ChangeType.EDIT:\n // When the field moved, the extreme may have changed in either\n // direction.\n return this.sameValue(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n );\n case ChangeType.CHILD:\n return true;\n default:\n unreachable(change);\n }\n }\n\n /**\n * Whether a group's row changes from `stored` to `state`: its value, or, for\n * sum/avg, the components even when the value doesn't (e.g. adding a 0 to a\n * sum, or a row equal to the average), so the synced client's\n * optimistic-delta base stays in sync.\n */\n changed(stored: AggState, state: AggState): boolean {\n return (\n this.value(stored) !== this.value(state) ||\n (carriesComponents(this.fn) &&\n (stored.sum !== state.sum || stored.nonNull !== state.nonNull))\n );\n }\n\n /** Writes the result, and for sum/avg its components, into `row`. */\n writePayload(row: Record<string, Value>, state: AggState): void {\n row[AGGREGATE_VALUE_COLUMN] = this.value(state);\n if (carriesComponents(this.fn)) {\n // Carry the running components so the synced client can adjust them.\n row[AGGREGATE_SUM_COLUMN] = state.sum;\n row[AGGREGATE_COUNT_COLUMN] = state.nonNull;\n }\n }\n}\n\n/**\n * Aggregate is a reducing operator for `count(*)` / `sum` / `avg` / `min` /\n * `max` over a correlated relationship.\n *\n * It sits at the end of a related-subquery pipeline (where a normal `related`\n * would emit child rows) and collapses the child rows for each parent into a\n * *single* synthetic row `{ ...groupKey, value }`. Carrying the group key keeps\n * the {@link Join} unchanged — it routes child changes by childField.\n *\n * Two maintenance strategies share the same fetch/emit machinery:\n *\n * - **Invertible** (count/sum/avg): add/remove/edit adjust running numbers in\n * O(1); no re-fetch.\n * - **Non-invertible** (min/max): add and removing a non-extreme value are\n * O(1), but removing (or editing away) the current extreme is not\n * invertible — the new extreme is unknown. In that case the group is marked\n * dirty and re-fetched from the source in {@link reconcile}, as {@link Take}\n * refills a partition. Re-fetching during the push itself could read\n * operators upstream (e.g. an `EXISTS` under an `OR`) before they have\n * settled the rest of the change.\n *\n * Null/empty semantics match SQL: every function except `count(*)` ignores null\n * field values and is NULL for an empty (or all-null) group; `count(*)` counts\n * all rows (0 when empty). An EDIT is emitted only when the derived value\n * actually changes.\n */\nexport class Aggregate implements Operator {\n readonly #input: Input;\n readonly #storage: AggStorage;\n // Empty for a top-level (ungrouped) aggregate — a single global group.\n readonly #groupKey: readonly string[];\n readonly #reducer: Reducer;\n readonly #schema: SourceSchema;\n // The source connection `input` reads straight from, when nothing between\n // them drops rows. A `count` asks it for the number instead of reading every\n // row (see #compute).\n readonly #countSource: SourceInput | undefined;\n\n #output: Output = throwOutput;\n\n /**\n * min/max groups whose extreme a push removed, by storage key, to recompute\n * in {@link reconcile}. Until then their storage keeps the state last sent\n * downstream, which a fetch keeps returning.\n */\n readonly #dirtyGroups = new Map<string, readonly NormalizedValue[]>();\n\n constructor(\n input: Input,\n storage: Storage,\n groupKey: readonly string[],\n fn: AggregateFunction,\n field?: string,\n tableName?: string,\n countSource?: SourceInput,\n path?: readonly (string | number)[],\n ) {\n this.#reducer = new Reducer(fn, field, path);\n this.#input = input;\n this.#storage = storage as AggStorage;\n this.#groupKey = groupKey;\n this.#countSource = fn === 'count' ? countSource : undefined;\n input.setOutput(this);\n\n const inputSchema = input.getSchema();\n // Top-level (ungrouped) aggregate synthesizes a single constant-valued key\n // column (so the one global row has a non-empty primary key, required by the\n // synced/CVR path); grouped/relationship aggregates use the group key. The\n // same derivation runs on the client (aggregatesFromSource), so the two\n // sides agree on the synthetic source shape.\n const {columns, primaryKey: keyColumns} = aggregateSourceSchema(\n inputSchema.columns,\n groupKey,\n fn,\n field,\n path,\n );\n\n // Exactly one row per key, so the key is the primary key and a valid\n // (stable) sort.\n const sort: Ordering = keyColumns.map(col => [col, 'asc'] as const);\n this.#schema = {\n // The synthetic rows are routed (e.g. by the view-syncer streamer) to a\n // synthetic table, distinct from the child table, so they are never\n // confused with real child rows.\n tableName: tableName ?? inputSchema.tableName,\n columns,\n // The group key (relationship aggregate) or the synthetic singleton key\n // (top-level aggregate) — one row per key in either case.\n primaryKey: keyColumns as unknown as PrimaryKey,\n relationships: {},\n isHidden: false,\n isAggregate: true,\n system: inputSchema.system,\n sort,\n compareRows: makeComparator(sort),\n };\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#schema;\n }\n\n destroy(): void {\n this.#input.destroy();\n this.#storage.destroy();\n }\n\n /** Recompute the whole group state from the source (already up to date). */\n *#compute(constraint: Constraint | undefined): Generator<'yield', AggState> {\n const s = this.#reducer.empty();\n const count = this.#countSource?.count?.(constraint);\n if (count !== undefined) {\n s.count = count;\n return s;\n }\n for (const node of this.#input.fetch({constraint})) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n this.#reducer.contribute(s, node.row);\n }\n return s;\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n const groupValues = this.#groupValuesFromConstraint(req.constraint);\n const key = stateKey(groupValues);\n\n let state = this.#storage.get(key);\n if (state === undefined) {\n state = yield* this.#compute(req.constraint);\n this.#storage.set(key, state);\n }\n\n yield this.#makeNode(groupValues, state);\n }\n\n /**\n * Phase 2 of the two-phase push: recompute the min/max groups whose extreme\n * Phase 1 removed, now that everything upstream has settled, then forward the\n * signal for bounded operators downstream.\n */\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#dirtyGroups.size > 0) {\n const dirty = [...this.#dirtyGroups];\n this.#dirtyGroups.clear();\n for (const [key, groupValues] of dirty) {\n const stored = must(this.#storage.get(key));\n const state = yield* this.#compute(\n this.#constraintFromGroup(groupValues),\n );\n this.#storage.set(key, state);\n yield* this.#emitIfChanged(groupValues, stored, state);\n }\n }\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *push(change: Change): Stream<'yield'> {\n const groupValues = this.#groupValuesFromRow(change[ChangeIndex.NODE].row);\n const key = stateKey(groupValues);\n const stored = this.#storage.get(key);\n\n // If we have no state for this group, nothing downstream has materialized\n // it, so there is nothing to update. When it is later fetched, fetch()\n // computes the true state from the source (which has already applied this\n // change). Mirrors Take's no-state no-op.\n if (stored === undefined) {\n return;\n }\n\n const state: AggState = {...stored};\n\n const reducer = this.#reducer;\n if (isInvertible(reducer.fn)) {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n reducer.applyInvertible(state, change[ChangeIndex.NODE].row, 1);\n break;\n case ChangeType.REMOVE:\n reducer.applyInvertible(state, change[ChangeIndex.NODE].row, -1);\n break;\n case ChangeType.EDIT: {\n // Group key unchanged (Join guarantees it); only the field moves the\n // value. count(*) is unaffected, and so is a sum/avg whose field kept\n // its value: subtracting and re-adding it would only add rounding\n // error.\n if (\n reducer.sameValue(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n )\n ) {\n return;\n }\n // Remove the old contribution, add the new.\n reducer.applyInvertible(\n state,\n change[ChangeIndex.OLD_NODE].row,\n -1,\n true,\n );\n reducer.applyInvertible(state, change[ChangeIndex.NODE].row, 1, true);\n break;\n }\n case ChangeType.CHILD:\n return;\n default:\n unreachable(change);\n }\n } else {\n // min/max — non-invertible. A dirty group is recomputed from the source\n // in reconcile, which covers this change too.\n if (this.#dirtyGroups.has(key) || !reducer.pushExtreme(state, change)) {\n this.#dirtyGroups.set(key, groupValues);\n return;\n }\n }\n\n assert(state.count >= 0, 'Aggregate: removed a row from an empty group');\n this.#storage.set(key, state);\n yield* this.#emitIfChanged(groupValues, stored, state);\n }\n\n /** Sends downstream the move of a group from `stored` to `state`, if any. */\n *#emitIfChanged(\n groupValues: readonly NormalizedValue[],\n stored: AggState,\n state: AggState,\n ): Stream<'yield'> {\n if (this.#reducer.changed(stored, state)) {\n yield* this.#output.push(\n makeEditChange(\n this.#makeNode(groupValues, state),\n this.#makeNode(groupValues, stored),\n ),\n this,\n );\n }\n }\n\n #makeNode(groupValues: readonly NormalizedValue[], state: AggState): Node {\n const row: Record<string, Value> = {};\n for (let i = 0; i < this.#groupKey.length; i++) {\n assignProperty(row, this.#groupKey[i], groupValues[i]);\n }\n if (this.#groupKey.length === 0) {\n // Top-level (ungrouped): the single row carries the synthetic key column.\n row[AGGREGATE_KEY_COLUMN] = AGGREGATE_KEY_VALUE;\n }\n this.#reducer.writePayload(row, state);\n return {row, relationships: {}};\n }\n\n #constraintFromGroup(groupValues: readonly NormalizedValue[]): Constraint {\n const constraint: Record<string, Value> = {};\n for (let i = 0; i < this.#groupKey.length; i++) {\n assignProperty(constraint, this.#groupKey[i], groupValues[i]);\n }\n return constraint;\n }\n\n #groupValuesFromConstraint(\n constraint: Constraint | undefined,\n ): NormalizedValue[] {\n if (this.#groupKey.length === 0) {\n return []; // top-level (ungrouped) — one global group, no constraint\n }\n assert(\n constraint,\n 'Aggregate requires a constraint on the group key (relationship aggregate only)',\n );\n return this.#groupKey.map(col => {\n assert(\n Object.hasOwn(constraint, col),\n () => `Aggregate: constraint missing group key column \"${col}\"`,\n );\n return normalizeUndefined(constraint[col]);\n });\n }\n\n #groupValuesFromRow(row: Row): NormalizedValue[] {\n return this.#groupKey.map(col => normalizeUndefined(row[col]));\n }\n}\n\n/**\n * Orders the values a min/max reduces. A JSON path's leaves can be numbers or\n * strings, decided per value; numbers order before strings, as in JS and\n * SQLite (z2s compiles the same rule rather than Postgres's jsonb ordering,\n * which puts strings first). Otherwise it is {@link compareValues}.\n */\nfunction compareAggregateValues(\n a: NormalizedValue,\n b: NormalizedValue,\n): number {\n if (\n a !== null &&\n b !== null &&\n typeof a !== typeof b &&\n (typeof a === 'number' || typeof a === 'string') &&\n (typeof b === 'number' || typeof b === 'string')\n ) {\n return typeof a === 'number' ? -1 : 1;\n }\n return compareValues(a, b);\n}\n\n/**\n * The storage key of a group's state. A relationship is usually correlated on\n * one column, which gets the cheaper single-value encoding Take and Cap use;\n * an operator's group key has a fixed length, so the two forms never meet.\n */\nfunction stateKey(groupValues: readonly NormalizedValue[]): string {\n return groupValues.length === 1\n ? valueToKey(groupValues[0])\n : JSON.stringify(groupValues);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AA6CA,IAAa,yBAAyB;;;;;;;;;;;;;AActC,IAAa,uBAAuB;;AAGpC,IAAM,sBAAsB;;;;;;;;AAS5B,IAAa,uBAAuB;AACpC,IAAa,yBAAyB;;;;;;AAOtC,IAAa,wBAAwB;;;;;AAMrC,SAAgB,kBAAkB,KAAsB;CACtD,OAAO,QAAA,YAAgC,0BAA0B,IAAI,GAAG;AAC1E;;;;;;;;;AAUA,SAAgB,yBAAyB,KAAmB;CAC1D,OACE,CAAC,kBAAkB,GAAG,SAEpB,gCAAgC,IAAI,gEAExC;AACF;;AAGA,SAAgB,kBAAkB,IAAgC;CAChE,OAAO,OAAO,SAAS,OAAO;AAChC;;AAGA,IAAa,4CAAiD,IAAI,IAAI;CACpE;CACA;CACA;CACA;AACF,CAAC;;;;;;;AAQD,SAAS,qBACP,IACA,OACA,cACA,MACa;CACb,QAAQ,IAAR;EACE,KAAK,SACH,OAAO,EAAC,MAAM,SAAQ;EACxB,KAAK;EACL,KAAK,OACH,OAAO;GAAC,MAAM;GAAU,UAAU;EAAI;EACxC,KAAK;EACL,KAAK,OAAO;GACV,IAAI,SAAS,KAAA,GAEX,OAAO;IAAC,MAAM;IAAQ,UAAU;GAAI;GAEtC,MAAM,cAAc,aAAa,KAAK,KAAK;GAC3C,OAAO,mBAAmB,cAAc,GAAG,UAAU,MAAM,UAAU;GACrE,OAAO;IAAC,GAAG;IAAa,UAAU;GAAI;EACxC;EACA,SACE,YAAY,EAAE;CAClB;AACF;;;;;;;;;AAUA,SAAgB,sBACd,cACA,UACA,IACA,OACA,MACuE;CACvE,MAAM,UAAuC,CAAC;CAC9C,KAAK,MAAM,OAAO,UAAU;EAC1B,yBAAyB,GAAG;EAC5B,MAAM,YAAY,aAAa;EAC/B,OAAO,iBAAiB,gCAAgC,IAAI,UAAU;EAC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AyC,OAAuB;EACxE,MAAM,MAA6B,CAAC;EACpC,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,UAAU,QAAQ,KACzC,eAAe,KAAK,KAAK,UAAU,IAAI,YAAY,EAAE;EAEvD,IAAI,KAAK,UAAU,WAAW,GAE5B,IAAI,wBAAwB;EAE9B,KAAK,SAAS,aAAa,KAAK,KAAK;EACrC,OAAO;GAAC;GAAK,eAAe,CAAC;EAAC;CAChC;CAEA,qBAAqB,aAAqD;EACxE,MAAM,aAAoC,CAAC;EAC3C,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,UAAU,QAAQ,KACzC,eAAe,YAAY,KAAK,UAAU,IAAI,YAAY,EAAE;EAE9D,OAAO;CACT;CAEA,2BACE,YACmB;EACnB,IAAI,KAAK,UAAU,WAAW,GAC5B,OAAO,CAAC;EAEV,OACE,YACA,gFACF;EACA,OAAO,KAAK,UAAU,KAAI,QAAO;GAC/B,OACE,OAAO,OAAO,YAAY,GAAG,SACvB,mDAAmD,IAAI,EAC/D;GACA,OAAO,mBAAmB,WAAW,IAAI;EAC3C,CAAC;CACH;CAEA,oBAAoB,KAA6B;EAC/C,OAAO,KAAK,UAAU,KAAI,QAAO,mBAAmB,IAAI,IAAI,CAAC;CAC/D;AACF;;;;;;;AAQA,SAAS,uBACP,GACA,GACQ;CACR,IACE,MAAM,QACN,MAAM,QACN,OAAO,MAAM,OAAO,MACnB,OAAO,MAAM,YAAY,OAAO,MAAM,cACtC,OAAO,MAAM,YAAY,OAAO,MAAM,WAEvC,OAAO,OAAO,MAAM,WAAW,KAAK;CAEtC,OAAO,cAAc,GAAG,CAAC;AAC3B;;;;;;AAOA,SAAS,SAAS,aAAiD;CACjE,OAAO,YAAY,WAAW,IAC1B,WAAW,YAAY,EAAE,IACzB,KAAK,UAAU,WAAW;AAChC"}
|
|
1
|
+
{"version":3,"file":"aggregate.js","names":[],"sources":["../../../../../zql/src/ivm/aggregate.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {must} from '../../../shared/src/must.ts';\nimport {assignProperty} from '../../../shared/src/objects.ts';\nimport {\n aggregateValue,\n type AggregateFunction,\n type Ordering,\n} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport type {SchemaValue} from '../../../zero-types/src/schema-value.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeEditChange, type Change} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {\n compareValues,\n makeComparator,\n normalizeUndefined,\n valueToKey,\n type Node,\n type NormalizedValue,\n} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {SourceInput} from './source.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The name of the synthetic column that holds the aggregate result in the rows\n * this operator emits. The value is projected to a bare scalar in the view, so\n * this name is internal.\n *\n * The synthetic columns share the emitted row with the group key, whose names\n * come from the user's schema, so they use the reserved `_0_` prefix (as\n * `_0_version` does) rather than plain words a schema could also use.\n */\nexport const AGGREGATE_VALUE_COLUMN = '_0_value';\n\n/**\n * A top-level (ungrouped) aggregate has no group key, but the synced path keys\n * every row in the CVR and forbids empty row keys (see zero-cache\n * `types/row-key.ts`). Since there is no group key, the single global row is\n * given one synthetic, constant-valued key column so it still has a (non-empty)\n * primary key. The materialized-view path ignores it (it projects only\n * `AGGREGATE_VALUE_COLUMN`), so this name is internal.\n *\n * The row holds nothing but this column and the payload columns, so any name\n * other than theirs (and `_0_version`) is collision-free. It must not be empty:\n * a SQLite-backed source cannot declare a zero-length column name.\n */\nexport const AGGREGATE_KEY_COLUMN = '_0_key';\n\n/** The constant value of {@link AGGREGATE_KEY_COLUMN} (one global row). */\nconst AGGREGATE_KEY_VALUE = 0;\n\n/**\n * `sum` and `avg` additionally carry their running components so the synced\n * client can adjust them optimistically: you can't move an average from the\n * final value alone, and a sum's value alone can't tell an empty group (`null`)\n * from one summing to 0. The value column stays the result (for display); these are the\n * sum of non-null field values and the count of non-null contributors.\n */\nexport const AGGREGATE_SUM_COLUMN = '_0_sum';\nexport const AGGREGATE_COUNT_COLUMN = '_0_count';\n\n/**\n * A grouped `sum`/`avg` row also carries its group's number of rows, nulls\n * included, which neither component gives: the synced client adds a group with\n * its first row and removes it with its last, as it does a count's.\n */\nexport const AGGREGATE_ROWS_COLUMN = '_0_rows';\n\n/**\n * Whether `col` is the name of a synthetic column of an aggregate or grouped\n * row: `_0_key` or a payload column.\n */\nexport function isSyntheticColumn(col: string): boolean {\n return col === AGGREGATE_KEY_COLUMN || AGGREGATE_PAYLOAD_COLUMNS.has(col);\n}\n\n/**\n * Asserts that the group key column `col` of an aggregate, or a column of a\n * grouped query, is not a synthetic column. The synthetic columns are written over\n * the group's columns in the emitted row, the payload is stripped from it by\n * the client's row key and `_0_key` alone keys a row that has it, so a\n * collision would silently merge or corrupt the groups. Their reserved `_0_`\n * prefix keeps real schemas clear of this; the check guards the invariant.\n */\nexport function assertNotSyntheticColumn(col: string): void {\n assert(\n !isSyntheticColumn(col),\n () =>\n `Aggregate: group key column \"${col}\" collides with the synthetic ` +\n `aggregate column of the same name`,\n );\n}\n\n/** Whether `fn`'s rows carry the sum and count components. */\nexport function carriesComponents(fn: AggregateFunction): boolean {\n return fn === 'sum' || fn === 'avg';\n}\n\n/** The synthetic payload columns (not part of an aggregate row's key). */\nexport const AGGREGATE_PAYLOAD_COLUMNS: ReadonlySet<string> = new Set([\n AGGREGATE_VALUE_COLUMN,\n AGGREGATE_SUM_COLUMN,\n AGGREGATE_COUNT_COLUMN,\n AGGREGATE_ROWS_COLUMN,\n]);\n\n/**\n * The type of the synthetic value column for an aggregate result: `count` is a\n * non-null number; `sum`/`avg` are nullable numbers; `min`/`max` take the\n * field's own type (`json` for a JSON path, whose leaf can be a number or a\n * string) and are nullable (empty group).\n */\nfunction aggregateValueColumn(\n fn: AggregateFunction,\n field: string | undefined,\n inputColumns: Record<string, SchemaValue>,\n path: readonly (string | number)[] | undefined,\n): SchemaValue {\n switch (fn) {\n case 'count':\n return {type: 'number'};\n case 'sum':\n case 'avg':\n return {type: 'number', optional: true};\n case 'min':\n case 'max': {\n if (path !== undefined) {\n // A JSON leaf: a number or a string, decided per value.\n return {type: 'json', optional: true};\n }\n const fieldSchema = inputColumns[must(field)];\n assert(fieldSchema, () => `Aggregate: ${fn} field \"${field}\" missing`);\n return {...fieldSchema, optional: true};\n }\n default:\n unreachable(fn);\n }\n}\n\n/**\n * The synthetic schema (columns + primary key) an aggregate produces, derived\n * from the *input* columns (the rows being aggregated), the group key, and the\n * fn/field. Shared by the {@link Aggregate} operator (compute mode) and the\n * client query builder (`aggregatesFromSource` mode) so both agree on the\n * synthetic source's shape. For a top-level (empty group key) aggregate the\n * single synthetic key column is added; otherwise the group key is the key.\n */\nexport function aggregateSourceSchema(\n inputColumns: Record<string, SchemaValue>,\n groupKey: readonly string[],\n fn: AggregateFunction,\n field: string | undefined,\n path?: readonly (string | number)[] | undefined,\n): {columns: Record<string, SchemaValue>; primaryKey: readonly string[]} {\n const columns: Record<string, SchemaValue> = {};\n for (const col of groupKey) {\n assertNotSyntheticColumn(col);\n const colSchema = inputColumns[col];\n assert(colSchema, () => `Aggregate: group key column \"${col}\" missing`);\n assignProperty(columns, col, colSchema);\n }\n const keyColumns = groupKey.length > 0 ? groupKey : [AGGREGATE_KEY_COLUMN];\n if (groupKey.length === 0) {\n columns[AGGREGATE_KEY_COLUMN] = {type: 'number'};\n }\n columns[AGGREGATE_VALUE_COLUMN] = aggregateValueColumn(\n fn,\n field,\n inputColumns,\n path,\n );\n if (carriesComponents(fn)) {\n // Components for optimistic adjustment on the synced client.\n columns[AGGREGATE_SUM_COLUMN] = {type: 'number'};\n columns[AGGREGATE_COUNT_COLUMN] = {type: 'number'};\n }\n return {columns, primaryKey: keyColumns};\n}\n\n/**\n * Per-group running state. We keep enough to derive any supported function:\n * - `count` → count(*) (all rows)\n * - `sum` → sum of non-null (NULL when nonNull === 0)\n * - `avg` → sum / nonNull (NULL when nonNull === 0)\n * - `min`/`max`→ extreme of non-null (NULL when nonNull === 0)\n */\nexport type AggState = {\n count: number;\n sum: number;\n nonNull: number;\n /** Current min/max of the non-null field values (null when nonNull === 0). */\n extreme: NormalizedValue;\n};\n\n/**\n * Typed view of the operator's {@link Storage} (mirrors Take): a group's state\n * by its key. Shared with `GroupBy`.\n */\nexport interface AggStorage {\n get(key: string): AggState | undefined;\n set(key: string, value: AggState): void;\n del(key: string): void;\n destroy(): void;\n}\n\n/**\n * Whether a change to one input row maps to a fixed delta of the aggregate\n * (`count`/`sum`/`avg`). Shared with the synced client, which only applies\n * optimistic deltas to these functions.\n */\nexport const isInvertible = (fn: AggregateFunction) =>\n fn === 'count' || fn === 'sum' || fn === 'avg';\n\n/**\n * How an aggregate function reduces a group's rows into an {@link AggState},\n * and reads its result back. Shared by {@link Aggregate}, which keeps a state\n * per parent of a relationship, and `GroupBy`, which keeps one per group.\n */\nexport class Reducer {\n readonly fn: AggregateFunction;\n readonly field: string | undefined;\n /** A JSON path into `field` (see `Aggregate.path`). */\n readonly path: readonly (string | number)[] | undefined;\n\n constructor(\n fn: AggregateFunction,\n field: string | undefined,\n path: readonly (string | number)[] | undefined,\n ) {\n assert(\n fn === 'count' ? field === undefined : field !== undefined,\n () =>\n `Aggregate: ${fn} ${fn === 'count' ? 'takes no' : 'requires a'} field`,\n );\n assert(\n path === undefined || field !== undefined,\n 'Aggregate: a JSON path requires a field',\n );\n this.fn = fn;\n this.field = field;\n this.path = path;\n }\n\n /** The state of a group with no rows. */\n empty(): AggState {\n return {count: 0, sum: 0, nonNull: 0, extreme: null};\n }\n\n /** The scalar result for a state, per the aggregate function. */\n value(s: AggState): Value {\n switch (this.fn) {\n case 'count':\n return s.count;\n case 'sum':\n return s.nonNull === 0 ? null : s.sum;\n case 'avg':\n return s.nonNull === 0 ? null : s.sum / s.nonNull;\n case 'min':\n case 'max':\n return s.extreme; // null when nonNull === 0\n default:\n unreachable(this.fn);\n }\n }\n\n /** Whether `v` should replace the current extreme for a min/max. */\n #beats(v: NormalizedValue, current: NormalizedValue): boolean {\n const c = compareAggregateValues(v, current);\n return this.fn === 'max' ? c > 0 : c < 0;\n }\n\n /**\n * The value `row` contributes: its field, or the JSON leaf at the path (see\n * `aggregateValue`), `null` when it contributes nothing.\n */\n #read(row: Row): NormalizedValue {\n const field = must(this.field);\n return normalizeUndefined(\n this.path === undefined\n ? row[field]\n : (aggregateValue({fn: this.fn, field, path: this.path}, row) as Value),\n );\n }\n\n /** Whether an edit from `oldRow` to `newRow` leaves the value it reduces. */\n sameValue(oldRow: Row, newRow: Row): boolean {\n return (\n this.field === undefined ||\n compareAggregateValues(this.#read(oldRow), this.#read(newRow)) === 0\n );\n }\n\n /** Add one row's contribution to a (freshly accumulating) state. */\n contribute(s: AggState, row: Row): void {\n s.count++;\n if (this.field === undefined) {\n return;\n }\n const v = this.#read(row);\n if (v === null) {\n return;\n }\n s.nonNull++;\n switch (this.fn) {\n case 'sum':\n case 'avg':\n assert(\n typeof v === 'number',\n () => `Aggregate: ${this.fn} needs a number`,\n );\n s.sum += v;\n break;\n case 'min':\n case 'max':\n if (s.extreme === null || this.#beats(v, s.extreme)) {\n s.extreme = v;\n }\n break;\n default:\n break;\n }\n }\n\n /** Invertible delta for count/sum/avg. */\n applyInvertible(\n state: AggState,\n row: Row,\n sign: 1 | -1,\n fieldOnly = false,\n ): void {\n if (!fieldOnly) {\n state.count += sign;\n }\n if (this.field !== undefined) {\n const v = this.#read(row);\n if (v !== null) {\n assert(\n typeof v === 'number',\n () => `Aggregate: ${this.fn}(${this.field}) requires a number`,\n );\n state.sum += sign * v;\n state.nonNull += sign;\n }\n }\n }\n\n /**\n * min/max maintenance, applied to `state` in place. Cheap for adds and for\n * removing a non-extreme value. Returns false when the current extreme is\n * removed or edited away: the new extreme is not recoverable from running\n * state, so the group has to be re-fetched.\n */\n pushExtreme(state: AggState, change: Change): boolean {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n // Adding can only extend the extreme outward — O(1).\n this.contribute(state, change[ChangeIndex.NODE].row);\n return true;\n case ChangeType.REMOVE: {\n const v = this.#read(change[ChangeIndex.NODE].row);\n state.count--;\n if (v === null) {\n return true; // null never contributed to the extreme\n }\n if (\n state.extreme !== null &&\n compareAggregateValues(v, state.extreme) === 0\n ) {\n return false; // removed (one of) the extreme\n }\n state.nonNull--; // a non-extreme value left; extreme unchanged\n return true;\n }\n case ChangeType.EDIT:\n // When the field moved, the extreme may have changed in either\n // direction.\n return this.sameValue(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n );\n case ChangeType.CHILD:\n return true;\n default:\n unreachable(change);\n }\n }\n\n /**\n * Whether a group's row changes from `stored` to `state`: its value, or, for\n * sum/avg, the components even when the value doesn't (e.g. adding a 0 to a\n * sum, or a row equal to the average), so the synced client's\n * optimistic-delta base stays in sync.\n */\n changed(stored: AggState, state: AggState): boolean {\n return (\n this.value(stored) !== this.value(state) ||\n (carriesComponents(this.fn) &&\n (stored.sum !== state.sum || stored.nonNull !== state.nonNull))\n );\n }\n\n /** Writes the result, and for sum/avg its components, into `row`. */\n writePayload(row: Record<string, Value>, state: AggState): void {\n row[AGGREGATE_VALUE_COLUMN] = this.value(state);\n if (carriesComponents(this.fn)) {\n // Carry the running components so the synced client can adjust them.\n row[AGGREGATE_SUM_COLUMN] = state.sum;\n row[AGGREGATE_COUNT_COLUMN] = state.nonNull;\n }\n }\n}\n\n/**\n * Aggregate is a reducing operator for `count(*)` / `sum` / `avg` / `min` /\n * `max` over a correlated relationship.\n *\n * It sits at the end of a related-subquery pipeline (where a normal `related`\n * would emit child rows) and collapses the child rows for each parent into a\n * *single* synthetic row `{ ...groupKey, value }`. Carrying the group key keeps\n * the {@link Join} unchanged — it routes child changes by childField.\n *\n * Two maintenance strategies share the same fetch/emit machinery:\n *\n * - **Invertible** (count/sum/avg): add/remove/edit adjust running numbers in\n * O(1); no re-fetch.\n * - **Non-invertible** (min/max): add and removing a non-extreme value are\n * O(1), but removing (or editing away) the current extreme is not\n * invertible — the new extreme is unknown. In that case the group is marked\n * dirty and re-fetched from the source in {@link reconcile}, as {@link Take}\n * refills a partition. Re-fetching during the push itself could read\n * operators upstream (e.g. an `EXISTS` under an `OR`) before they have\n * settled the rest of the change.\n *\n * Null/empty semantics match SQL: every function except `count(*)` ignores null\n * field values and is NULL for an empty (or all-null) group; `count(*)` counts\n * all rows (0 when empty). An EDIT is emitted only when the derived value\n * actually changes.\n */\nexport class Aggregate implements Operator {\n readonly #input: Input;\n readonly #storage: AggStorage;\n // Empty for a top-level (ungrouped) aggregate — a single global group.\n readonly #groupKey: readonly string[];\n readonly #reducer: Reducer;\n readonly #schema: SourceSchema;\n // The source connection `input` reads straight from, when nothing between\n // them drops rows. A `count` asks it for the number instead of reading every\n // row (see #compute).\n readonly #countSource: SourceInput | undefined;\n\n #output: Output = throwOutput;\n\n /**\n * min/max groups whose extreme a push removed, by storage key, to recompute\n * in {@link reconcile}. Until then their storage keeps the state last sent\n * downstream, which a fetch keeps returning.\n */\n readonly #dirtyGroups = new Map<string, readonly NormalizedValue[]>();\n\n constructor(\n input: Input,\n storage: Storage,\n groupKey: readonly string[],\n fn: AggregateFunction,\n field?: string,\n tableName?: string,\n countSource?: SourceInput,\n path?: readonly (string | number)[],\n ) {\n this.#reducer = new Reducer(fn, field, path);\n this.#input = input;\n this.#storage = storage as AggStorage;\n this.#groupKey = groupKey;\n this.#countSource = fn === 'count' ? countSource : undefined;\n input.setOutput(this);\n\n const inputSchema = input.getSchema();\n // Top-level (ungrouped) aggregate synthesizes a single constant-valued key\n // column (so the one global row has a non-empty primary key, required by the\n // synced/CVR path); grouped/relationship aggregates use the group key. The\n // same derivation runs on the client (aggregatesFromSource), so the two\n // sides agree on the synthetic source shape.\n const {columns, primaryKey: keyColumns} = aggregateSourceSchema(\n inputSchema.columns,\n groupKey,\n fn,\n field,\n path,\n );\n\n // Exactly one row per key, so the key is the primary key and a valid\n // (stable) sort.\n const sort: Ordering = keyColumns.map(col => [col, 'asc'] as const);\n this.#schema = {\n // The synthetic rows are routed (e.g. by the view-syncer streamer) to a\n // synthetic table, distinct from the child table, so they are never\n // confused with real child rows.\n tableName: tableName ?? inputSchema.tableName,\n columns,\n // The group key (relationship aggregate) or the synthetic singleton key\n // (top-level aggregate) — one row per key in either case.\n primaryKey: keyColumns as unknown as PrimaryKey,\n relationships: {},\n isHidden: false,\n isAggregate: true,\n system: inputSchema.system,\n sort,\n compareRows: makeComparator(sort),\n };\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#schema;\n }\n\n destroy(): void {\n this.#input.destroy();\n this.#storage.destroy();\n }\n\n /** Recompute the whole group state from the source (already up to date). */\n *#compute(constraint: Constraint | undefined): Generator<'yield', AggState> {\n const s = this.#reducer.empty();\n const count = this.#countSource?.count?.(constraint);\n if (count !== undefined) {\n s.count = count;\n return s;\n }\n for (const node of this.#input.fetch({constraint})) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n this.#reducer.contribute(s, node.row);\n }\n return s;\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n const groupValues = this.#groupValuesFromConstraint(req.constraint);\n const key = stateKey(groupValues);\n\n let state = this.#storage.get(key);\n if (state === undefined) {\n state = yield* this.#compute(req.constraint);\n this.#storage.set(key, state);\n }\n\n yield this.#makeNode(groupValues, state);\n }\n\n /**\n * Phase 2 of the two-phase push: recompute the min/max groups whose extreme\n * Phase 1 removed, now that everything upstream has settled, then forward the\n * signal for bounded operators downstream.\n */\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#dirtyGroups.size > 0) {\n const dirty = [...this.#dirtyGroups];\n this.#dirtyGroups.clear();\n for (const [key, groupValues] of dirty) {\n const stored = must(this.#storage.get(key));\n const state = yield* this.#compute(\n this.#constraintFromGroup(groupValues),\n );\n this.#storage.set(key, state);\n yield* this.#emitIfChanged(groupValues, stored, state);\n }\n }\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *push(change: Change): Stream<'yield'> {\n const groupValues = this.#groupValuesFromRow(change[ChangeIndex.NODE].row);\n const key = stateKey(groupValues);\n const stored = this.#storage.get(key);\n\n // If we have no state for this group, nothing downstream has materialized\n // it, so there is nothing to update. When it is later fetched, fetch()\n // computes the true state from the source (which has already applied this\n // change). Mirrors Take's no-state no-op.\n if (stored === undefined) {\n return;\n }\n\n const state: AggState = {...stored};\n\n const reducer = this.#reducer;\n if (isInvertible(reducer.fn)) {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n reducer.applyInvertible(state, change[ChangeIndex.NODE].row, 1);\n break;\n case ChangeType.REMOVE:\n reducer.applyInvertible(state, change[ChangeIndex.NODE].row, -1);\n break;\n case ChangeType.EDIT: {\n // Group key unchanged (Join guarantees it); only the field moves the\n // value. count(*) is unaffected, and so is a sum/avg whose field kept\n // its value: subtracting and re-adding it would only add rounding\n // error.\n if (\n reducer.sameValue(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n )\n ) {\n return;\n }\n // Remove the old contribution, add the new.\n reducer.applyInvertible(\n state,\n change[ChangeIndex.OLD_NODE].row,\n -1,\n true,\n );\n reducer.applyInvertible(state, change[ChangeIndex.NODE].row, 1, true);\n break;\n }\n case ChangeType.CHILD:\n return;\n default:\n unreachable(change);\n }\n } else {\n // min/max — non-invertible. A dirty group is recomputed from the source\n // in reconcile, which covers this change too.\n if (this.#dirtyGroups.has(key) || !reducer.pushExtreme(state, change)) {\n this.#dirtyGroups.set(key, groupValues);\n return;\n }\n }\n\n assert(state.count >= 0, 'Aggregate: removed a row from an empty group');\n this.#storage.set(key, state);\n yield* this.#emitIfChanged(groupValues, stored, state);\n }\n\n /** Sends downstream the move of a group from `stored` to `state`, if any. */\n *#emitIfChanged(\n groupValues: readonly NormalizedValue[],\n stored: AggState,\n state: AggState,\n ): Stream<'yield'> {\n if (this.#reducer.changed(stored, state)) {\n yield* this.#output.push(\n makeEditChange(\n this.#makeNode(groupValues, state),\n this.#makeNode(groupValues, stored),\n ),\n this,\n );\n }\n }\n\n #makeNode(groupValues: readonly NormalizedValue[], state: AggState): Node {\n const row: Record<string, Value> = {};\n for (let i = 0; i < this.#groupKey.length; i++) {\n assignProperty(row, this.#groupKey[i], groupValues[i]);\n }\n if (this.#groupKey.length === 0) {\n // Top-level (ungrouped): the single row carries the synthetic key column.\n row[AGGREGATE_KEY_COLUMN] = AGGREGATE_KEY_VALUE;\n }\n this.#reducer.writePayload(row, state);\n return {row, relationships: {}};\n }\n\n #constraintFromGroup(groupValues: readonly NormalizedValue[]): Constraint {\n const constraint: Record<string, Value> = {};\n for (let i = 0; i < this.#groupKey.length; i++) {\n assignProperty(constraint, this.#groupKey[i], groupValues[i]);\n }\n return constraint;\n }\n\n #groupValuesFromConstraint(\n constraint: Constraint | undefined,\n ): NormalizedValue[] {\n if (this.#groupKey.length === 0) {\n return []; // top-level (ungrouped) — one global group, no constraint\n }\n assert(\n constraint,\n 'Aggregate requires a constraint on the group key (relationship aggregate only)',\n );\n return this.#groupKey.map(col => {\n assert(\n Object.hasOwn(constraint, col),\n () => `Aggregate: constraint missing group key column \"${col}\"`,\n );\n return normalizeUndefined(constraint[col]);\n });\n }\n\n #groupValuesFromRow(row: Row): NormalizedValue[] {\n return this.#groupKey.map(col => normalizeUndefined(row[col]));\n }\n}\n\n/**\n * Orders the values a min/max reduces. A JSON path's leaves can be numbers or\n * strings, decided per value; strings order before numbers, as in Postgres's\n * jsonb ordering and in rindle (z2s compiles the same rule). Otherwise it is\n * {@link compareValues}.\n */\nfunction compareAggregateValues(\n a: NormalizedValue,\n b: NormalizedValue,\n): number {\n if (\n a !== null &&\n b !== null &&\n typeof a !== typeof b &&\n (typeof a === 'number' || typeof a === 'string') &&\n (typeof b === 'number' || typeof b === 'string')\n ) {\n return typeof a === 'string' ? -1 : 1;\n }\n return compareValues(a, b);\n}\n\n/**\n * The storage key of a group's state. A relationship is usually correlated on\n * one column, which gets the cheaper single-value encoding Take and Cap use;\n * an operator's group key has a fixed length, so the two forms never meet.\n */\nfunction stateKey(groupValues: readonly NormalizedValue[]): string {\n return groupValues.length === 1\n ? valueToKey(groupValues[0])\n : JSON.stringify(groupValues);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA6CA,IAAa,yBAAyB;;;;;;;;;;;;;AActC,IAAa,uBAAuB;;AAGpC,IAAM,sBAAsB;;;;;;;;AAS5B,IAAa,uBAAuB;AACpC,IAAa,yBAAyB;;;;;;AAOtC,IAAa,wBAAwB;;;;;AAMrC,SAAgB,kBAAkB,KAAsB;CACtD,OAAO,QAAA,YAAgC,0BAA0B,IAAI,GAAG;AAC1E;;;;;;;;;AAUA,SAAgB,yBAAyB,KAAmB;CAC1D,OACE,CAAC,kBAAkB,GAAG,SAEpB,gCAAgC,IAAI,gEAExC;AACF;;AAGA,SAAgB,kBAAkB,IAAgC;CAChE,OAAO,OAAO,SAAS,OAAO;AAChC;;AAGA,IAAa,4CAAiD,IAAI,IAAI;CACpE;CACA;CACA;CACA;AACF,CAAC;;;;;;;AAQD,SAAS,qBACP,IACA,OACA,cACA,MACa;CACb,QAAQ,IAAR;EACE,KAAK,SACH,OAAO,EAAC,MAAM,SAAQ;EACxB,KAAK;EACL,KAAK,OACH,OAAO;GAAC,MAAM;GAAU,UAAU;EAAI;EACxC,KAAK;EACL,KAAK,OAAO;GACV,IAAI,SAAS,KAAA,GAEX,OAAO;IAAC,MAAM;IAAQ,UAAU;GAAI;GAEtC,MAAM,cAAc,aAAa,KAAK,KAAK;GAC3C,OAAO,mBAAmB,cAAc,GAAG,UAAU,MAAM,UAAU;GACrE,OAAO;IAAC,GAAG;IAAa,UAAU;GAAI;EACxC;EACA,SACE,YAAY,EAAE;CAClB;AACF;;;;;;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AAS,aAAiD;CACjE,OAAO,YAAY,WAAW,IAC1B,WAAW,YAAY,EAAE,IACzB,KAAK,UAAU,WAAW;AAChC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cap.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/cap.ts"],"names":[],"mappings":"AAMA,OAAO,EAAgB,KAAK,MAAM,EAAkB,MAAM,aAAa,CAAC;AAExE,OAAO,EAA8B,KAAK,IAAI,EAAC,MAAM,WAAW,CAAC;AACjE,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,MAAM,EACX,KAAK,OAAO,EACb,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,
|
|
1
|
+
{"version":3,"file":"cap.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/cap.ts"],"names":[],"mappings":"AAMA,OAAO,EAAgB,KAAK,MAAM,EAAkB,MAAM,aAAa,CAAC;AAExE,OAAO,EAA8B,KAAK,IAAI,EAAC,MAAM,WAAW,CAAC;AACjE,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,MAAM,EACX,KAAK,OAAO,EACb,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAsB,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AAC7D,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,WAAW,CAAC;AAcnB;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,GAAI,YAAW,QAAQ;;IAUlC,YACE,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,MAAM,EACb,YAAY,CAAC,EAAE,YAAY,EAW5B;IAED,SAAS,CAAC,MAAM,EAA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