@rocicorp/zero 1.12.0-canary.10 → 1.12.0-canary.12

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.
Files changed (231) hide show
  1. package/out/analyze-query/src/zql-chain.js +9 -1
  2. package/out/analyze-query/src/zql-chain.js.map +1 -1
  3. package/out/ast-to-zql/src/ast-to-zql.d.ts.map +1 -1
  4. package/out/ast-to-zql/src/ast-to-zql.js +61 -20
  5. package/out/ast-to-zql/src/ast-to-zql.js.map +1 -1
  6. package/out/z2s/src/compiler.d.ts +1 -1
  7. package/out/z2s/src/compiler.d.ts.map +1 -1
  8. package/out/z2s/src/compiler.js +339 -2
  9. package/out/z2s/src/compiler.js.map +1 -1
  10. package/out/z2s/src/sql.d.ts +8 -0
  11. package/out/z2s/src/sql.d.ts.map +1 -1
  12. package/out/z2s/src/sql.js +58 -11
  13. package/out/z2s/src/sql.js.map +1 -1
  14. package/out/zero/package.js +1 -1
  15. package/out/zero/package.js.map +1 -1
  16. package/out/zero/src/bindings.js +4 -4
  17. package/out/zero-cache/src/auth/write-authorizer.js +1 -1
  18. package/out/zero-cache/src/config/zero-config.d.ts +4 -0
  19. package/out/zero-cache/src/config/zero-config.d.ts.map +1 -1
  20. package/out/zero-cache/src/config/zero-config.js +13 -0
  21. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  22. package/out/zero-cache/src/db/create.js +1 -1
  23. package/out/zero-cache/src/scripts/decommission.js +1 -1
  24. package/out/zero-cache/src/server/replicator.js +4 -2
  25. package/out/zero-cache/src/server/replicator.js.map +1 -1
  26. package/out/zero-cache/src/server/runner/run-worker.d.ts.map +1 -1
  27. package/out/zero-cache/src/server/runner/run-worker.js +8 -5
  28. package/out/zero-cache/src/server/runner/run-worker.js.map +1 -1
  29. package/out/zero-cache/src/server/serving-checkpointer.js +41 -0
  30. package/out/zero-cache/src/server/serving-checkpointer.js.map +1 -0
  31. package/out/zero-cache/src/server/worker-urls.d.ts +1 -0
  32. package/out/zero-cache/src/server/worker-urls.d.ts.map +1 -1
  33. package/out/zero-cache/src/server/worker-urls.js +2 -1
  34. package/out/zero-cache/src/server/worker-urls.js.map +1 -1
  35. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +1 -1
  36. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +1 -1
  37. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +1 -1
  38. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +1 -1
  39. package/out/zero-cache/src/services/change-source/pg/schema/init.js +1 -1
  40. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +1 -1
  41. package/out/zero-cache/src/services/http-service.d.ts +6 -0
  42. package/out/zero-cache/src/services/http-service.d.ts.map +1 -1
  43. package/out/zero-cache/src/services/http-service.js +13 -1
  44. package/out/zero-cache/src/services/http-service.js.map +1 -1
  45. package/out/zero-cache/src/services/life-cycle.d.ts +19 -0
  46. package/out/zero-cache/src/services/life-cycle.d.ts.map +1 -1
  47. package/out/zero-cache/src/services/life-cycle.js +53 -1
  48. package/out/zero-cache/src/services/life-cycle.js.map +1 -1
  49. package/out/zero-cache/src/services/replicator/change-processor.js +1 -1
  50. package/out/zero-cache/src/services/replicator/serving-checkpointer.js +85 -0
  51. package/out/zero-cache/src/services/replicator/serving-checkpointer.js.map +1 -0
  52. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +101 -13
  53. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js.map +1 -1
  54. package/out/zero-cache/src/services/view-syncer/query-covering.js +2 -2
  55. package/out/zero-cache/src/services/view-syncer/query-covering.js.map +1 -1
  56. package/out/zero-cache/src/services/view-syncer/snapshotter.js +1 -1
  57. package/out/zero-cache/src/services/view-syncer/view-syncer.js +1 -1
  58. package/out/zero-cache/src/types/processes.js +4 -4
  59. package/out/zero-cache/src/types/processes.js.map +1 -1
  60. package/out/zero-cache/src/types/row-key.js +8 -1
  61. package/out/zero-cache/src/types/row-key.js.map +1 -1
  62. package/out/zero-cache/src/workers/connection.js +2 -2
  63. package/out/zero-cache/src/workers/replicator.js +9 -4
  64. package/out/zero-cache/src/workers/replicator.js.map +1 -1
  65. package/out/zero-client/src/client/bindings.d.ts +1 -1
  66. package/out/zero-client/src/client/bindings.d.ts.map +1 -1
  67. package/out/zero-client/src/client/bindings.js +4 -4
  68. package/out/zero-client/src/client/context.d.ts +7 -1
  69. package/out/zero-client/src/client/context.d.ts.map +1 -1
  70. package/out/zero-client/src/client/context.js +7 -0
  71. package/out/zero-client/src/client/context.js.map +1 -1
  72. package/out/zero-client/src/client/crud-impl.d.ts.map +1 -1
  73. package/out/zero-client/src/client/crud-impl.js +171 -2
  74. package/out/zero-client/src/client/crud-impl.js.map +1 -1
  75. package/out/zero-client/src/client/custom.d.ts.map +1 -1
  76. package/out/zero-client/src/client/custom.js +42 -3
  77. package/out/zero-client/src/client/custom.js.map +1 -1
  78. package/out/zero-client/src/client/ivm-branch.d.ts +62 -2
  79. package/out/zero-client/src/client/ivm-branch.d.ts.map +1 -1
  80. package/out/zero-client/src/client/ivm-branch.js +152 -31
  81. package/out/zero-client/src/client/ivm-branch.js.map +1 -1
  82. package/out/zero-client/src/client/keys.d.ts +33 -0
  83. package/out/zero-client/src/client/keys.d.ts.map +1 -1
  84. package/out/zero-client/src/client/keys.js +43 -4
  85. package/out/zero-client/src/client/keys.js.map +1 -1
  86. package/out/zero-client/src/client/version.js +1 -1
  87. package/out/zero-client/src/client/zero-poke-handler.d.ts.map +1 -1
  88. package/out/zero-client/src/client/zero-poke-handler.js +29 -1
  89. package/out/zero-client/src/client/zero-poke-handler.js.map +1 -1
  90. package/out/zero-client/src/client/zero-rep.d.ts +1 -1
  91. package/out/zero-client/src/client/zero-rep.d.ts.map +1 -1
  92. package/out/zero-client/src/client/zero-rep.js +2 -15
  93. package/out/zero-client/src/client/zero-rep.js.map +1 -1
  94. package/out/zero-client/src/client/zero.js +1 -1
  95. package/out/zero-client/src/mod.d.ts +3 -3
  96. package/out/zero-client/src/mod.d.ts.map +1 -1
  97. package/out/zero-protocol/src/aggregate-table.d.ts +15 -0
  98. package/out/zero-protocol/src/aggregate-table.d.ts.map +1 -0
  99. package/out/zero-protocol/src/aggregate-table.js +5 -0
  100. package/out/zero-protocol/src/aggregate-table.js.map +1 -0
  101. package/out/zero-protocol/src/ast.d.ts +141 -5
  102. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  103. package/out/zero-protocol/src/ast.js +244 -18
  104. package/out/zero-protocol/src/ast.js.map +1 -1
  105. package/out/zero-protocol/src/down.d.ts +29 -29
  106. package/out/zero-protocol/src/inspect-down.d.ts +81 -81
  107. package/out/zero-protocol/src/protocol-version.d.ts +1 -1
  108. package/out/zero-protocol/src/protocol-version.d.ts.map +1 -1
  109. package/out/zero-protocol/src/protocol-version.js.map +1 -1
  110. package/out/zero-protocol/src/query-hash-visitor.d.ts.map +1 -1
  111. package/out/zero-protocol/src/query-hash-visitor.js +38 -0
  112. package/out/zero-protocol/src/query-hash-visitor.js.map +1 -1
  113. package/out/zero-react/src/bindings.js +4 -4
  114. package/out/zero-react/src/use-query.d.ts +3 -1
  115. package/out/zero-react/src/use-query.d.ts.map +1 -1
  116. package/out/zero-react/src/use-query.js +4 -2
  117. package/out/zero-react/src/use-query.js.map +1 -1
  118. package/out/zero-schema/src/builder/schema-builder.d.ts.map +1 -1
  119. package/out/zero-schema/src/builder/schema-builder.js +2 -0
  120. package/out/zero-schema/src/builder/schema-builder.js.map +1 -1
  121. package/out/zero-schema/src/name-mapper.d.ts.map +1 -1
  122. package/out/zero-schema/src/name-mapper.js +7 -4
  123. package/out/zero-schema/src/name-mapper.js.map +1 -1
  124. package/out/zero-server/src/custom.js +1 -1
  125. package/out/zero-server/src/schema.d.ts.map +1 -1
  126. package/out/zero-server/src/schema.js +13 -2
  127. package/out/zero-server/src/schema.js.map +1 -1
  128. package/out/zero-solid/src/bindings.js +4 -4
  129. package/out/zero-solid/src/solid-view.d.ts.map +1 -1
  130. package/out/zero-solid/src/solid-view.js +3 -3
  131. package/out/zero-solid/src/solid-view.js.map +1 -1
  132. package/out/zero-types/src/format.d.ts +13 -0
  133. package/out/zero-types/src/format.d.ts.map +1 -1
  134. package/out/zero-types/src/format.js.map +1 -1
  135. package/out/zero-types/src/name-mapper.d.ts +14 -0
  136. package/out/zero-types/src/name-mapper.d.ts.map +1 -1
  137. package/out/zero-types/src/name-mapper.js +12 -0
  138. package/out/zero-types/src/name-mapper.js.map +1 -1
  139. package/out/zero-types/src/schema-value.d.ts +2 -0
  140. package/out/zero-types/src/schema-value.d.ts.map +1 -1
  141. package/out/zero-types/src/schema-value.js +15 -0
  142. package/out/zero-types/src/schema-value.js.map +1 -0
  143. package/out/zero-types/src/server-schema.d.ts +18 -0
  144. package/out/zero-types/src/server-schema.d.ts.map +1 -1
  145. package/out/zql/src/builder/builder.d.ts +126 -1
  146. package/out/zql/src/builder/builder.d.ts.map +1 -1
  147. package/out/zql/src/builder/builder.js +281 -11
  148. package/out/zql/src/builder/builder.js.map +1 -1
  149. package/out/zql/src/builder/filter.d.ts +1 -1
  150. package/out/zql/src/builder/filter.d.ts.map +1 -1
  151. package/out/zql/src/builder/filter.js +41 -3
  152. package/out/zql/src/builder/filter.js.map +1 -1
  153. package/out/zql/src/ivm/aggregate.d.ts +188 -0
  154. package/out/zql/src/ivm/aggregate.d.ts.map +1 -0
  155. package/out/zql/src/ivm/aggregate.js +470 -0
  156. package/out/zql/src/ivm/aggregate.js.map +1 -0
  157. package/out/zql/src/ivm/array-view.d.ts +1 -1
  158. package/out/zql/src/ivm/array-view.d.ts.map +1 -1
  159. package/out/zql/src/ivm/array-view.js +3 -3
  160. package/out/zql/src/ivm/array-view.js.map +1 -1
  161. package/out/zql/src/ivm/cap.js +2 -2
  162. package/out/zql/src/ivm/constraint.d.ts.map +1 -1
  163. package/out/zql/src/ivm/constraint.js +2 -1
  164. package/out/zql/src/ivm/constraint.js.map +1 -1
  165. package/out/zql/src/ivm/deferred-input.js +1 -1
  166. package/out/zql/src/ivm/fan-in.js +1 -1
  167. package/out/zql/src/ivm/flipped-join.d.ts.map +1 -1
  168. package/out/zql/src/ivm/flipped-join.js +3 -2
  169. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  170. package/out/zql/src/ivm/group-by.d.ts +98 -0
  171. package/out/zql/src/ivm/group-by.d.ts.map +1 -0
  172. package/out/zql/src/ivm/group-by.js +384 -0
  173. package/out/zql/src/ivm/group-by.js.map +1 -0
  174. package/out/zql/src/ivm/join-index.d.ts.map +1 -1
  175. package/out/zql/src/ivm/join-index.js +2 -1
  176. package/out/zql/src/ivm/join-index.js.map +1 -1
  177. package/out/zql/src/ivm/join-utils.d.ts.map +1 -1
  178. package/out/zql/src/ivm/join-utils.js +2 -1
  179. package/out/zql/src/ivm/join-utils.js.map +1 -1
  180. package/out/zql/src/ivm/join.js +1 -1
  181. package/out/zql/src/ivm/lift-field.d.ts +54 -0
  182. package/out/zql/src/ivm/lift-field.d.ts.map +1 -0
  183. package/out/zql/src/ivm/lift-field.js +168 -0
  184. package/out/zql/src/ivm/lift-field.js.map +1 -0
  185. package/out/zql/src/ivm/maybe-split-and-push-edit-change.js +1 -1
  186. package/out/zql/src/ivm/memory-source.d.ts +2 -0
  187. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  188. package/out/zql/src/ivm/memory-source.js +9 -4
  189. package/out/zql/src/ivm/memory-source.js.map +1 -1
  190. package/out/zql/src/ivm/push-accumulated.js +1 -1
  191. package/out/zql/src/ivm/schema.d.ts +1 -0
  192. package/out/zql/src/ivm/schema.d.ts.map +1 -1
  193. package/out/zql/src/ivm/source.d.ts +21 -0
  194. package/out/zql/src/ivm/source.d.ts.map +1 -1
  195. package/out/zql/src/ivm/source.js.map +1 -1
  196. package/out/zql/src/ivm/take.js +1 -1
  197. package/out/zql/src/ivm/union-fan-in.js +1 -1
  198. package/out/zql/src/ivm/view-apply-change.d.ts +5 -0
  199. package/out/zql/src/ivm/view-apply-change.d.ts.map +1 -1
  200. package/out/zql/src/ivm/view-apply-change.js +23 -3
  201. package/out/zql/src/ivm/view-apply-change.js.map +1 -1
  202. package/out/zql/src/planner/planner-debug.d.ts +1 -1
  203. package/out/zql/src/planner/planner-debug.d.ts.map +1 -1
  204. package/out/zql/src/planner/planner-debug.js +4 -0
  205. package/out/zql/src/planner/planner-debug.js.map +1 -1
  206. package/out/zql/src/query/expression.d.ts +50 -3
  207. package/out/zql/src/query/expression.d.ts.map +1 -1
  208. package/out/zql/src/query/expression.js +62 -6
  209. package/out/zql/src/query/expression.js.map +1 -1
  210. package/out/zql/src/query/measure-push-operator.js +1 -1
  211. package/out/zql/src/query/query-delegate-base.d.ts +8 -0
  212. package/out/zql/src/query/query-delegate-base.d.ts.map +1 -1
  213. package/out/zql/src/query/query-delegate-base.js +13 -5
  214. package/out/zql/src/query/query-delegate-base.js.map +1 -1
  215. package/out/zql/src/query/query-impl.d.ts +17 -3
  216. package/out/zql/src/query/query-impl.d.ts.map +1 -1
  217. package/out/zql/src/query/query-impl.js +239 -7
  218. package/out/zql/src/query/query-impl.js.map +1 -1
  219. package/out/zql/src/query/query.d.ts +319 -7
  220. package/out/zql/src/query/query.d.ts.map +1 -1
  221. package/out/zqlite/src/pending-delta.d.ts.map +1 -1
  222. package/out/zqlite/src/pending-delta.js +4 -3
  223. package/out/zqlite/src/pending-delta.js.map +1 -1
  224. package/out/zqlite/src/query-builder.d.ts +12 -1
  225. package/out/zqlite/src/query-builder.d.ts.map +1 -1
  226. package/out/zqlite/src/query-builder.js +114 -13
  227. package/out/zqlite/src/query-builder.js.map +1 -1
  228. package/out/zqlite/src/table-source.d.ts.map +1 -1
  229. package/out/zqlite/src/table-source.js +37 -3
  230. package/out/zqlite/src/table-source.js.map +1 -1
  231. package/package.json +1 -1
@@ -0,0 +1,384 @@
1
+ import { assert, unreachable } from "../../../shared/src/asserts.js";
2
+ import { must } from "../../../shared/src/must.js";
3
+ import { assignProperty } from "../../../shared/src/objects.js";
4
+ import { makeAddChange, makeEditChange, makeRemoveChange } from "./change.js";
5
+ import { makeComparator, normalizeUndefined, valueToKey } from "./data.js";
6
+ import { throwOutput } from "./operator.js";
7
+ import { AGGREGATE_KEY_COLUMN, AGGREGATE_ROWS_COLUMN, AGGREGATE_VALUE_COLUMN, aggregateSourceSchema, carriesComponents, isInvertible } from "./aggregate.js";
8
+ import { emptyArray } from "../../../shared/src/sentinels.js";
9
+ import { rowEqualsForCompoundKey } from "./join-utils.js";
10
+ //#region ../zql/src/ivm/group-by.ts
11
+ /**
12
+ * The synthetic schema (columns, primary key and sort) of the rows
13
+ * {@link GroupBy} emits: the group columns, the key of the group (`_0_key`)
14
+ * and the aggregate's payload (`_0_value`, and for sum/avg its components and
15
+ * the group's number of rows, `_0_rows`).
16
+ * Shared by the operator (compute mode) and the client query builder
17
+ * (`aggregatesFromSource` mode) so both agree on the synthetic source's shape.
18
+ *
19
+ * The key is the group's values encoded as one string, rather than the
20
+ * columns themselves, because a synced row key can't hold a `null`, and `null`
21
+ * is a value like any other.
22
+ */
23
+ function groupSourceSchema(inputColumns, ordering, reducer) {
24
+ const { columns } = aggregateSourceSchema(inputColumns, ordering.map(([col]) => col), reducer.fn, reducer.field, reducer.path);
25
+ columns[AGGREGATE_KEY_COLUMN] = { type: "string" };
26
+ if (carriesComponents(reducer.fn)) columns[AGGREGATE_ROWS_COLUMN] = { type: "number" };
27
+ return {
28
+ columns,
29
+ primaryKey: [AGGREGATE_KEY_COLUMN],
30
+ sort: [...ordering, [AGGREGATE_KEY_COLUMN, "asc"]]
31
+ };
32
+ }
33
+ /**
34
+ * The `_0_key` of a group, which also keys its state in the operator's
35
+ * storage: as for Take and Cap, `valueToKey` of the value when there is one
36
+ * column, and otherwise the values in column order, JSON encoded. One query
37
+ * always has the same number of columns, so the two forms never meet.
38
+ */
39
+ function groupKey(values) {
40
+ return values.length === 1 ? valueToKey(values[0]) : JSON.stringify(values);
41
+ }
42
+ /** A row's values of the group `columns`, in their order. */
43
+ function groupValues(row, columns) {
44
+ return columns.map((col) => normalizeUndefined(row[col]));
45
+ }
46
+ /**
47
+ * GroupBy groups its input by `columns` and reduces each group with an
48
+ * aggregate function, as `GROUP BY` does: one row per combination of the
49
+ * columns' values, `{...columns, _0_key, _0_value}`, plus the sum and count
50
+ * components for sum/avg. `null` is a value like any other.
51
+ *
52
+ * A distinct is a GroupBy with a count, whose value a view doesn't show.
53
+ *
54
+ * It sits at the end of a top-level pipeline whose rows are sorted by the
55
+ * group columns first, so the rows of a group are adjacent and a fetch reads
56
+ * them in one pass. It keeps the state of every group, so a push adds a row
57
+ * when a group gets its first input row, removes it when it loses its last,
58
+ * and otherwise edits it when its value changes. Like {@link Aggregate}, a
59
+ * min/max whose extreme a push removes is re-read from the input in
60
+ * {@link reconcile}.
61
+ */
62
+ var GroupBy = class {
63
+ #input;
64
+ #storage;
65
+ #columns;
66
+ #reducer;
67
+ #carriesRows;
68
+ #schema;
69
+ #groupSource;
70
+ #hydrated = false;
71
+ /**
72
+ * min/max groups whose extreme a push removed, by storage key, with a row
73
+ * of the group, to re-read in {@link reconcile}. Until then their storage
74
+ * keeps the value last sent downstream, with the current count.
75
+ */
76
+ #dirtyGroups = /* @__PURE__ */ new Map();
77
+ #output = throwOutput;
78
+ /**
79
+ * @param ordering The order of the groups, which names each group column
80
+ * once. The input must be sorted by it first.
81
+ */
82
+ constructor(input, storage, ordering, tableName, reducer, groupSource) {
83
+ assert(ordering.length > 0, "GroupBy: needs at least one column");
84
+ this.#input = input;
85
+ this.#storage = storage;
86
+ this.#columns = ordering.map(([col]) => col);
87
+ this.#reducer = reducer;
88
+ this.#carriesRows = carriesComponents(reducer.fn);
89
+ this.#groupSource = reducer.fn === "count" ? groupSource : void 0;
90
+ input.setOutput(this);
91
+ const inputSchema = input.getSchema();
92
+ const inputSort = inputSchema.sort ?? [];
93
+ assert(ordering.every(([col, dir], i) => inputSort[i]?.[0] === col && inputSort[i][1] === dir), "GroupBy: the input must be sorted by the group columns first");
94
+ const { columns, primaryKey, sort } = groupSourceSchema(inputSchema.columns, ordering, reducer);
95
+ this.#schema = {
96
+ tableName,
97
+ columns,
98
+ primaryKey,
99
+ relationships: {},
100
+ isHidden: false,
101
+ isAggregate: true,
102
+ system: inputSchema.system,
103
+ sort,
104
+ compareRows: makeComparator(sort)
105
+ };
106
+ }
107
+ setOutput(output) {
108
+ this.#output = output;
109
+ }
110
+ getSchema() {
111
+ return this.#schema;
112
+ }
113
+ destroy() {
114
+ this.#input.destroy();
115
+ this.#storage.destroy();
116
+ }
117
+ fetch(req) {
118
+ assert(req.constraint === void 0 && req.start === void 0 && !req.reverse, "GroupBy: fetch takes no constraint, start or reverse");
119
+ this.#hydrated = true;
120
+ const groups = this.#groupSource?.groupCounts?.(this.#columns);
121
+ if (groups === void 0) return this.#fetchGroups();
122
+ const nodes = groups.map(({ row, count }) => this.#store(this.#groupValues(row), {
123
+ ...this.#reducer.empty(),
124
+ count
125
+ }));
126
+ nodes.sort((a, b) => this.#schema.compareRows(a.row, b.row));
127
+ return nodes;
128
+ }
129
+ /** Reads the input and folds each run of a group into its row. */
130
+ *#fetchGroups() {
131
+ let first;
132
+ let state;
133
+ for (const node of this.#input.fetch({})) {
134
+ if (node === "yield") {
135
+ yield node;
136
+ continue;
137
+ }
138
+ if (first === void 0 || !rowEqualsForCompoundKey(first, node.row, this.#columns)) {
139
+ if (first !== void 0) yield this.#store(this.#groupValues(first), must(state));
140
+ first = node.row;
141
+ state = this.#reducer.empty();
142
+ }
143
+ this.#reducer.contribute(must(state), node.row);
144
+ }
145
+ if (first !== void 0) yield this.#store(this.#groupValues(first), must(state));
146
+ }
147
+ push(change) {
148
+ if (!this.#hydrated) return emptyArray;
149
+ switch (change[0]) {
150
+ case 0: return this.#add(change[1].row);
151
+ case 1: return this.#remove(change[1].row);
152
+ case 2: {
153
+ const oldRow = change[2].row;
154
+ const row = change[1].row;
155
+ return rowEqualsForCompoundKey(oldRow, row, this.#columns) ? this.#update(oldRow, row) : this.#move(oldRow, row);
156
+ }
157
+ case 3: return emptyArray;
158
+ default: unreachable(change);
159
+ }
160
+ }
161
+ /**
162
+ * Phase 2 of the two-phase push: re-read the min/max groups whose extreme
163
+ * Phase 1 removed, now that everything upstream has settled, then forward
164
+ * the signal for operators downstream.
165
+ */
166
+ reconcile(_pusher) {
167
+ if (this.#dirtyGroups.size > 0) return this.#reconcileDirty();
168
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
169
+ }
170
+ *#reconcileDirty() {
171
+ const dirty = [...this.#dirtyGroups];
172
+ this.#dirtyGroups.clear();
173
+ for (const [key, groupRow] of dirty) {
174
+ const stored = this.#state(key);
175
+ if (stored === void 0) continue;
176
+ const state = this.#reducer.empty();
177
+ for (const node of this.#input.fetch({ constraint: this.#constraint(groupRow) })) {
178
+ if (node === "yield") {
179
+ yield node;
180
+ continue;
181
+ }
182
+ if (rowEqualsForCompoundKey(node.row, groupRow, this.#columns)) this.#reducer.contribute(state, node.row);
183
+ }
184
+ yield* this.#emitIfChanged(this.#groupValues(groupRow), stored, state);
185
+ }
186
+ if (this.#output.reconcile) yield* this.#output.reconcile(this);
187
+ }
188
+ /** An edit that moves its row from one group to another. */
189
+ *#move(oldRow, row) {
190
+ yield* this.#remove(oldRow);
191
+ yield* this.#add(row);
192
+ }
193
+ #add(row) {
194
+ const values = this.#groupValues(row);
195
+ const key = groupKey(values);
196
+ const stored = this.#state(key);
197
+ if (stored === void 0) {
198
+ const state = this.#reducer.empty();
199
+ this.#reducer.contribute(state, row);
200
+ return this.#output.push(makeAddChange(this.#store(values, state)), this);
201
+ }
202
+ const state = { ...stored };
203
+ if (this.#dirtyGroups.has(key)) {
204
+ state.count++;
205
+ this.#storage.set(key, state);
206
+ return emptyArray;
207
+ }
208
+ this.#reducer.contribute(state, row);
209
+ return this.#emitIfChanged(values, stored, state);
210
+ }
211
+ #remove(row) {
212
+ const values = this.#groupValues(row);
213
+ const key = groupKey(values);
214
+ const stored = must(this.#state(key), "GroupBy: removed a row of a group it lacks");
215
+ if (stored.count === 1) {
216
+ this.#storage.del(key);
217
+ this.#dirtyGroups.delete(key);
218
+ return this.#output.push(makeRemoveChange(this.#makeNode(values, stored)), this);
219
+ }
220
+ const state = { ...stored };
221
+ if (isInvertible(this.#reducer.fn)) this.#reducer.applyInvertible(state, row, -1);
222
+ else if (this.#dirtyGroups.has(key) || !this.#reducer.pushExtreme(state, makeRemoveChange({
223
+ row,
224
+ relationships: {}
225
+ }))) {
226
+ this.#dirtyGroups.set(key, row);
227
+ this.#storage.set(key, {
228
+ ...stored,
229
+ count: stored.count - 1
230
+ });
231
+ return emptyArray;
232
+ }
233
+ return this.#emitIfChanged(values, stored, state);
234
+ }
235
+ /** An edit within a group. */
236
+ #update(oldRow, row) {
237
+ const values = this.#groupValues(row);
238
+ const key = groupKey(values);
239
+ const stored = this.#state(key);
240
+ if (stored === void 0 || this.#dirtyGroups.has(key) || this.#reducer.sameValue(oldRow, row)) return emptyArray;
241
+ const state = { ...stored };
242
+ if (!isInvertible(this.#reducer.fn)) {
243
+ this.#dirtyGroups.set(key, row);
244
+ return emptyArray;
245
+ }
246
+ this.#reducer.applyInvertible(state, oldRow, -1, true);
247
+ this.#reducer.applyInvertible(state, row, 1, true);
248
+ return this.#emitIfChanged(values, stored, state);
249
+ }
250
+ /** Stores `state` and sends the move from `stored` downstream, if any. */
251
+ #emitIfChanged(values, stored, state) {
252
+ this.#storage.set(groupKey(values), state);
253
+ if (!this.#reducer.changed(stored, state) && !(this.#carriesRows && stored.count !== state.count)) return emptyArray;
254
+ return this.#output.push(makeEditChange(this.#makeNode(values, state), this.#makeNode(values, stored)), this);
255
+ }
256
+ #state(key) {
257
+ return this.#storage.get(key);
258
+ }
259
+ /** Records `state` for the group and returns its row. */
260
+ #store(values, state) {
261
+ this.#storage.set(groupKey(values), state);
262
+ return this.#makeNode(values, state);
263
+ }
264
+ #makeNode(values, state) {
265
+ const row = {};
266
+ for (let i = 0; i < this.#columns.length; i++) assignProperty(row, this.#columns[i], values[i]);
267
+ row[AGGREGATE_KEY_COLUMN] = groupKey(values);
268
+ this.#reducer.writePayload(row, state);
269
+ if (this.#carriesRows) row[AGGREGATE_ROWS_COLUMN] = state.count;
270
+ return {
271
+ row,
272
+ relationships: {}
273
+ };
274
+ }
275
+ /**
276
+ * A constraint that reads the rows of `row`'s group. It leaves out the
277
+ * `null` values, which a constraint never matches, so it can read rows of
278
+ * other groups too, which the caller skips.
279
+ */
280
+ #constraint(row) {
281
+ let constraint;
282
+ for (const col of this.#columns) {
283
+ const value = normalizeUndefined(row[col]);
284
+ if (value !== null) assignProperty(constraint ??= {}, col, value);
285
+ }
286
+ return constraint;
287
+ }
288
+ #groupValues(row) {
289
+ return groupValues(row, this.#columns);
290
+ }
291
+ };
292
+ /**
293
+ * Turns the rows of a {@link GroupBy}, or of the synthetic source a synced
294
+ * client reads them from, into the rows a view of the query shows: the group
295
+ * columns under the query's column names, and, for a `groupBy()` with an
296
+ * aggregate, its value under `valueName` (`count`, `sum`, …). A `distinct()`
297
+ * shows no value, so an edit, which only moves its count, doesn't reach the
298
+ * view.
299
+ */
300
+ var GroupProjection = class {
301
+ #input;
302
+ #columns;
303
+ #valueName;
304
+ #schema;
305
+ #output = throwOutput;
306
+ constructor(input, columns, valueName) {
307
+ this.#input = input;
308
+ this.#columns = columns;
309
+ this.#valueName = valueName;
310
+ assert(valueName === void 0 || columns.every(([out]) => out !== valueName), () => `groupBy(): the column "${valueName}" has the same name as the ${valueName}() value`);
311
+ input.setOutput(this);
312
+ const inputSchema = input.getSchema();
313
+ const viewName = new Map(columns.map(([out, from]) => [from, out]));
314
+ const sort = must(inputSchema.sort).filter(([col]) => viewName.has(col)).map(([col, dir]) => [must(viewName.get(col)), dir]);
315
+ const viewColumns = {};
316
+ for (const [out, from] of columns) assignProperty(viewColumns, out, must(inputSchema.columns[from]));
317
+ if (valueName !== void 0) assignProperty(viewColumns, valueName, must(inputSchema.columns[AGGREGATE_VALUE_COLUMN]));
318
+ this.#schema = {
319
+ tableName: inputSchema.tableName,
320
+ columns: viewColumns,
321
+ primaryKey: columns.map(([out]) => out),
322
+ relationships: {},
323
+ isHidden: false,
324
+ system: inputSchema.system,
325
+ sort,
326
+ compareRows: makeComparator(sort)
327
+ };
328
+ }
329
+ setOutput(output) {
330
+ this.#output = output;
331
+ }
332
+ getSchema() {
333
+ return this.#schema;
334
+ }
335
+ destroy() {
336
+ this.#input.destroy();
337
+ }
338
+ *fetch(req) {
339
+ for (const node of this.#input.fetch(req)) yield node === "yield" ? node : this.#project(node);
340
+ }
341
+ push(change) {
342
+ switch (change[0]) {
343
+ case 0: return this.#output.push(makeAddChange(this.#project(change[1])), this);
344
+ case 1: return this.#output.push(makeRemoveChange(this.#project(change[1])), this);
345
+ case 2: {
346
+ const node = change[1];
347
+ const old = change[2];
348
+ if (this.#valueName === void 0 || node.row["_0_value"] === old.row["_0_value"]) return emptyArray;
349
+ return this.#output.push(makeEditChange(this.#project(node), this.#project(old)), this);
350
+ }
351
+ case 3: return emptyArray;
352
+ default: unreachable(change);
353
+ }
354
+ }
355
+ reconcile(_pusher) {
356
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
357
+ }
358
+ #project(node) {
359
+ const row = {};
360
+ for (const [out, from] of this.#columns) assignProperty(row, out, node.row[from]);
361
+ if (this.#valueName !== void 0) assignProperty(row, this.#valueName, node.row[AGGREGATE_VALUE_COLUMN]);
362
+ return {
363
+ row,
364
+ relationships: {}
365
+ };
366
+ }
367
+ };
368
+ /**
369
+ * The order of a grouped query's rows (its `groupBy`, or the columns a count
370
+ * of distinct combinations groups by): its `orderBy`, which names only those
371
+ * columns, then the rest of them ascending. Other columns, such as the
372
+ * primary key a completed ordering ends with, are left out.
373
+ */
374
+ function groupOrdering(ast) {
375
+ const columns = must(ast.groupBy ?? ast.aggregate?.distinct);
376
+ const ordering = [];
377
+ for (const [col, dir] of ast.orderBy ?? []) if (columns.includes(col) && !ordering.some(([c]) => c === col)) ordering.push([col, dir]);
378
+ for (const col of columns) if (!ordering.some(([c]) => c === col)) ordering.push([col, "asc"]);
379
+ return ordering;
380
+ }
381
+ //#endregion
382
+ export { GroupBy, GroupProjection, groupKey, groupOrdering, groupSourceSchema, groupValues };
383
+
384
+ //# sourceMappingURL=group-by.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"group-by.js","names":[],"sources":["../../../../../zql/src/ivm/group-by.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 {emptyArray} from '../../../shared/src/sentinels.ts';\nimport type {\n AST,\n CompoundKey,\n 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 {\n AGGREGATE_KEY_COLUMN,\n AGGREGATE_ROWS_COLUMN,\n AGGREGATE_VALUE_COLUMN,\n aggregateSourceSchema,\n carriesComponents,\n isInvertible,\n type AggState,\n type AggStorage,\n type Reducer,\n} from './aggregate.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n makeAddChange,\n makeEditChange,\n makeRemoveChange,\n type Change,\n} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {\n makeComparator,\n normalizeUndefined,\n valueToKey,\n type Node,\n type NormalizedValue,\n} from './data.ts';\nimport {rowEqualsForCompoundKey} from './join-utils.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 synthetic schema (columns, primary key and sort) of the rows\n * {@link GroupBy} emits: the group columns, the key of the group (`_0_key`)\n * and the aggregate's payload (`_0_value`, and for sum/avg its components and\n * the group's number of rows, `_0_rows`).\n * Shared by the operator (compute mode) and the client query builder\n * (`aggregatesFromSource` mode) so both agree on the synthetic source's shape.\n *\n * The key is the group's values encoded as one string, rather than the\n * columns themselves, because a synced row key can't hold a `null`, and `null`\n * is a value like any other.\n */\nexport function groupSourceSchema(\n inputColumns: Record<string, SchemaValue>,\n ordering: Ordering,\n reducer: Pick<Reducer, 'fn' | 'field' | 'path'>,\n): {\n columns: Record<string, SchemaValue>;\n primaryKey: PrimaryKey;\n sort: Ordering;\n} {\n const {columns} = aggregateSourceSchema(\n inputColumns,\n ordering.map(([col]) => col),\n reducer.fn,\n reducer.field,\n reducer.path,\n );\n columns[AGGREGATE_KEY_COLUMN] = {type: 'string'};\n if (carriesComponents(reducer.fn)) {\n columns[AGGREGATE_ROWS_COLUMN] = {type: 'number'};\n }\n return {\n columns,\n primaryKey: [AGGREGATE_KEY_COLUMN],\n // The key is unique and determined by the columns, so it only makes the\n // order total.\n sort: [...ordering, [AGGREGATE_KEY_COLUMN, 'asc']],\n };\n}\n\n/**\n * The `_0_key` of a group, which also keys its state in the operator's\n * storage: as for Take and Cap, `valueToKey` of the value when there is one\n * column, and otherwise the values in column order, JSON encoded. One query\n * always has the same number of columns, so the two forms never meet.\n */\nexport function groupKey(values: readonly NormalizedValue[]): string {\n return values.length === 1 ? valueToKey(values[0]) : JSON.stringify(values);\n}\n\n/** A row's values of the group `columns`, in their order. */\nexport function groupValues(\n row: Row,\n columns: readonly string[],\n): NormalizedValue[] {\n return columns.map(col => normalizeUndefined(row[col]));\n}\n\n/**\n * GroupBy groups its input by `columns` and reduces each group with an\n * aggregate function, as `GROUP BY` does: one row per combination of the\n * columns' values, `{...columns, _0_key, _0_value}`, plus the sum and count\n * components for sum/avg. `null` is a value like any other.\n *\n * A distinct is a GroupBy with a count, whose value a view doesn't show.\n *\n * It sits at the end of a top-level pipeline whose rows are sorted by the\n * group columns first, so the rows of a group are adjacent and a fetch reads\n * them in one pass. It keeps the state of every group, so a push adds a row\n * when a group gets its first input row, removes it when it loses its last,\n * and otherwise edits it when its value changes. Like {@link Aggregate}, a\n * min/max whose extreme a push removes is re-read from the input in\n * {@link reconcile}.\n */\nexport class GroupBy implements Operator {\n readonly #input: Input;\n readonly #storage: AggStorage;\n readonly #columns: CompoundKey;\n readonly #reducer: Reducer;\n // A sum/avg row also carries its group's row count (`_0_rows`).\n readonly #carriesRows: boolean;\n readonly #schema: SourceSchema;\n // The source connection `input` reads straight from, when nothing between\n // them drops rows. It can count the groups without the rows being read\n // (see fetch).\n readonly #groupSource: SourceInput | undefined;\n // Set by the first fetch. Until then nothing downstream has the rows, so a\n // push has nothing to update.\n #hydrated = false;\n\n /**\n * min/max groups whose extreme a push removed, by storage key, with a row\n * of the group, to re-read in {@link reconcile}. Until then their storage\n * keeps the value last sent downstream, with the current count.\n */\n readonly #dirtyGroups = new Map<string, Row>();\n\n #output: Output = throwOutput;\n\n /**\n * @param ordering The order of the groups, which names each group column\n * once. The input must be sorted by it first.\n */\n constructor(\n input: Input,\n storage: Storage,\n ordering: Ordering,\n tableName: string,\n reducer: Reducer,\n groupSource?: SourceInput | undefined,\n ) {\n assert(ordering.length > 0, 'GroupBy: needs at least one column');\n this.#input = input;\n this.#storage = storage as AggStorage;\n this.#columns = ordering.map(([col]) => col) as unknown as CompoundKey;\n this.#reducer = reducer;\n this.#carriesRows = carriesComponents(reducer.fn);\n this.#groupSource = reducer.fn === 'count' ? groupSource : undefined;\n input.setOutput(this);\n\n const inputSchema = input.getSchema();\n const inputSort = inputSchema.sort ?? [];\n assert(\n ordering.every(\n ([col, dir], i) => inputSort[i]?.[0] === col && inputSort[i][1] === dir,\n ),\n 'GroupBy: the input must be sorted by the group columns first',\n );\n const {columns, primaryKey, sort} = groupSourceSchema(\n inputSchema.columns,\n ordering,\n reducer,\n );\n this.#schema = {\n // The rows are routed (e.g. by the view-syncer streamer) to this\n // synthetic table, apart from the input table's rows.\n tableName,\n columns,\n 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 fetch(req: FetchRequest): Stream<Node | 'yield'> {\n // A top-level query without a bound, so the view reads every row once.\n assert(\n req.constraint === undefined && req.start === undefined && !req.reverse,\n 'GroupBy: fetch takes no constraint, start or reverse',\n );\n this.#hydrated = true;\n const groups = this.#groupSource?.groupCounts?.(this.#columns);\n if (groups === undefined) {\n return this.#fetchGroups();\n }\n const nodes = groups.map(({row, count}) =>\n this.#store(this.#groupValues(row), {...this.#reducer.empty(), count}),\n );\n nodes.sort((a, b) => this.#schema.compareRows(a.row, b.row));\n return nodes;\n }\n\n /** Reads the input and folds each run of a group into its row. */\n *#fetchGroups(): Stream<Node | 'yield'> {\n // The first row of the current run, and its group's state.\n let first: Row | undefined;\n let state: AggState | undefined;\n for (const node of this.#input.fetch({})) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (\n first === undefined ||\n !rowEqualsForCompoundKey(first, node.row, this.#columns)\n ) {\n if (first !== undefined) {\n yield this.#store(this.#groupValues(first), must(state));\n }\n first = node.row;\n state = this.#reducer.empty();\n }\n this.#reducer.contribute(must(state), node.row);\n }\n if (first !== undefined) {\n yield this.#store(this.#groupValues(first), must(state));\n }\n }\n\n push(change: Change): Stream<'yield'> {\n if (!this.#hydrated) {\n return emptyArray;\n }\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return this.#add(change[ChangeIndex.NODE].row);\n case ChangeType.REMOVE:\n return this.#remove(change[ChangeIndex.NODE].row);\n case ChangeType.EDIT: {\n const oldRow = change[ChangeIndex.OLD_NODE].row;\n const row = change[ChangeIndex.NODE].row;\n return rowEqualsForCompoundKey(oldRow, row, this.#columns)\n ? this.#update(oldRow, row)\n : this.#move(oldRow, row);\n }\n case ChangeType.CHILD:\n // The input has no relationships the group rows show.\n return emptyArray;\n default:\n unreachable(change);\n }\n }\n\n /**\n * Phase 2 of the two-phase push: re-read the min/max groups whose extreme\n * Phase 1 removed, now that everything upstream has settled, then forward\n * the signal for operators downstream.\n */\n reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#dirtyGroups.size > 0) {\n return this.#reconcileDirty();\n }\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n\n *#reconcileDirty(): Stream<'yield'> {\n const dirty = [...this.#dirtyGroups];\n this.#dirtyGroups.clear();\n for (const [key, groupRow] of dirty) {\n const stored = this.#state(key);\n if (stored === undefined) {\n continue; // its last row went in the meantime\n }\n const state = this.#reducer.empty();\n for (const node of this.#input.fetch({\n constraint: this.#constraint(groupRow),\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (rowEqualsForCompoundKey(node.row, groupRow, this.#columns)) {\n this.#reducer.contribute(state, node.row);\n }\n }\n yield* this.#emitIfChanged(this.#groupValues(groupRow), stored, state);\n }\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n /** An edit that moves its row from one group to another. */\n *#move(oldRow: Row, row: Row): Stream<'yield'> {\n yield* this.#remove(oldRow);\n yield* this.#add(row);\n }\n\n #add(row: Row): Stream<'yield'> {\n const values = this.#groupValues(row);\n const key = groupKey(values);\n const stored = this.#state(key);\n if (stored === undefined) {\n const state = this.#reducer.empty();\n this.#reducer.contribute(state, row);\n return this.#output.push(makeAddChange(this.#store(values, state)), this);\n }\n const state = {...stored};\n if (this.#dirtyGroups.has(key)) {\n // Re-read in reconcile; only the count is kept up to date.\n state.count++;\n this.#storage.set(key, state);\n return emptyArray;\n }\n this.#reducer.contribute(state, row);\n return this.#emitIfChanged(values, stored, state);\n }\n\n #remove(row: Row): Stream<'yield'> {\n const values = this.#groupValues(row);\n const key = groupKey(values);\n const stored = must(\n this.#state(key),\n 'GroupBy: removed a row of a group it lacks',\n );\n if (stored.count === 1) {\n this.#storage.del(key);\n this.#dirtyGroups.delete(key);\n return this.#output.push(\n makeRemoveChange(this.#makeNode(values, stored)),\n this,\n );\n }\n const state = {...stored};\n if (isInvertible(this.#reducer.fn)) {\n this.#reducer.applyInvertible(state, row, -1);\n } else if (\n this.#dirtyGroups.has(key) ||\n !this.#reducer.pushExtreme(\n state,\n makeRemoveChange({row, relationships: {}}),\n )\n ) {\n // Keep what was last sent downstream, with the count down by one.\n this.#dirtyGroups.set(key, row);\n this.#storage.set(key, {...stored, count: stored.count - 1});\n return emptyArray;\n }\n return this.#emitIfChanged(values, stored, state);\n }\n\n /** An edit within a group. */\n #update(oldRow: Row, row: Row): Stream<'yield'> {\n const values = this.#groupValues(row);\n const key = groupKey(values);\n const stored = this.#state(key);\n if (\n stored === undefined ||\n this.#dirtyGroups.has(key) ||\n this.#reducer.sameValue(oldRow, row)\n ) {\n return emptyArray;\n }\n const state = {...stored};\n if (!isInvertible(this.#reducer.fn)) {\n // The extreme may have moved in either direction.\n this.#dirtyGroups.set(key, row);\n return emptyArray;\n }\n this.#reducer.applyInvertible(state, oldRow, -1, true);\n this.#reducer.applyInvertible(state, row, 1, true);\n return this.#emitIfChanged(values, stored, state);\n }\n\n /** Stores `state` and sends the move from `stored` downstream, if any. */\n #emitIfChanged(\n values: readonly NormalizedValue[],\n stored: AggState,\n state: AggState,\n ): Stream<'yield'> {\n this.#storage.set(groupKey(values), state);\n if (\n !this.#reducer.changed(stored, state) &&\n // A row whose field is null moves only a sum/avg group's row count.\n !(this.#carriesRows && stored.count !== state.count)\n ) {\n return emptyArray;\n }\n return this.#output.push(\n makeEditChange(\n this.#makeNode(values, state),\n this.#makeNode(values, stored),\n ),\n this,\n );\n }\n\n #state(key: string): AggState | undefined {\n return this.#storage.get(key);\n }\n\n /** Records `state` for the group and returns its row. */\n #store(values: readonly NormalizedValue[], state: AggState): Node {\n this.#storage.set(groupKey(values), state);\n return this.#makeNode(values, state);\n }\n\n #makeNode(values: readonly NormalizedValue[], state: AggState): Node {\n const row: Record<string, Value> = {};\n for (let i = 0; i < this.#columns.length; i++) {\n assignProperty(row, this.#columns[i], values[i]);\n }\n row[AGGREGATE_KEY_COLUMN] = groupKey(values);\n this.#reducer.writePayload(row, state);\n if (this.#carriesRows) {\n row[AGGREGATE_ROWS_COLUMN] = state.count;\n }\n return {row, relationships: {}};\n }\n\n /**\n * A constraint that reads the rows of `row`'s group. It leaves out the\n * `null` values, which a constraint never matches, so it can read rows of\n * other groups too, which the caller skips.\n */\n #constraint(row: Row): Constraint | undefined {\n let constraint: Record<string, Value> | undefined;\n for (const col of this.#columns) {\n const value = normalizeUndefined(row[col]);\n if (value !== null) {\n assignProperty((constraint ??= {}), col, value);\n }\n }\n return constraint;\n }\n\n #groupValues(row: Row): NormalizedValue[] {\n return groupValues(row, this.#columns);\n }\n}\n\n/**\n * Turns the rows of a {@link GroupBy}, or of the synthetic source a synced\n * client reads them from, into the rows a view of the query shows: the group\n * columns under the query's column names, and, for a `groupBy()` with an\n * aggregate, its value under `valueName` (`count`, `sum`, …). A `distinct()`\n * shows no value, so an edit, which only moves its count, doesn't reach the\n * view.\n */\nexport class GroupProjection implements Operator {\n readonly #input: Input;\n // [the view's name, the input's name] of each group column.\n readonly #columns: readonly (readonly [string, string])[];\n readonly #valueName: string | undefined;\n readonly #schema: SourceSchema;\n\n #output: Output = throwOutput;\n\n constructor(\n input: Input,\n columns: readonly (readonly [string, string])[],\n valueName?: string | undefined,\n ) {\n this.#input = input;\n this.#columns = columns;\n this.#valueName = valueName;\n // The value would overwrite the group column. The query builder rejects\n // such a query; a hand-built AST can still name one.\n assert(\n valueName === undefined || columns.every(([out]) => out !== valueName),\n () =>\n `groupBy(): the column \"${valueName}\" has the same name as the ${valueName}() value`,\n );\n input.setOutput(this);\n\n const inputSchema = input.getSchema();\n const viewName = new Map(columns.map(([out, from]) => [from, out]));\n const sort: Ordering = must(inputSchema.sort)\n .filter(([col]) => viewName.has(col))\n .map(([col, dir]) => [must(viewName.get(col)), dir] as const);\n const viewColumns: Record<string, SchemaValue> = {};\n for (const [out, from] of columns) {\n assignProperty(viewColumns, out, must(inputSchema.columns[from]));\n }\n if (valueName !== undefined) {\n assignProperty(\n viewColumns,\n valueName,\n must(inputSchema.columns[AGGREGATE_VALUE_COLUMN]),\n );\n }\n this.#schema = {\n tableName: inputSchema.tableName,\n columns: viewColumns,\n primaryKey: columns.map(([out]) => out) as unknown as PrimaryKey,\n relationships: {},\n isHidden: false,\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 }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n for (const node of this.#input.fetch(req)) {\n yield node === 'yield' ? node : this.#project(node);\n }\n }\n\n push(change: Change): Stream<'yield'> {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n return this.#output.push(\n makeAddChange(this.#project(change[ChangeIndex.NODE])),\n this,\n );\n case ChangeType.REMOVE:\n return this.#output.push(\n makeRemoveChange(this.#project(change[ChangeIndex.NODE])),\n this,\n );\n case ChangeType.EDIT: {\n // A row's key is its group's values, so an edit only moves its\n // value, which a distinct doesn't show. Nothing is copied unless\n // the value changed.\n const node = change[ChangeIndex.NODE];\n const old = change[ChangeIndex.OLD_NODE];\n if (\n this.#valueName === undefined ||\n node.row[AGGREGATE_VALUE_COLUMN] === old.row[AGGREGATE_VALUE_COLUMN]\n ) {\n return emptyArray;\n }\n return this.#output.push(\n makeEditChange(this.#project(node), this.#project(old)),\n this,\n );\n }\n case ChangeType.CHILD:\n return emptyArray;\n default:\n unreachable(change);\n }\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n\n #project(node: Node): Node {\n const row: Record<string, Value> = {};\n for (const [out, from] of this.#columns) {\n assignProperty(row, out, node.row[from]);\n }\n if (this.#valueName !== undefined) {\n assignProperty(row, this.#valueName, node.row[AGGREGATE_VALUE_COLUMN]);\n }\n return {row, relationships: {}};\n }\n}\n\n/**\n * The order of a grouped query's rows (its `groupBy`, or the columns a count\n * of distinct combinations groups by): its `orderBy`, which names only those\n * columns, then the rest of them ascending. Other columns, such as the\n * primary key a completed ordering ends with, are left out.\n */\nexport function groupOrdering(ast: AST): Ordering {\n const columns = must(ast.groupBy ?? ast.aggregate?.distinct);\n const ordering: [string, 'asc' | 'desc'][] = [];\n for (const [col, dir] of ast.orderBy ?? []) {\n if (columns.includes(col) && !ordering.some(([c]) => c === col)) {\n ordering.push([col, dir]);\n }\n }\n for (const col of columns) {\n if (!ordering.some(([c]) => c === col)) {\n ordering.push([col, 'asc']);\n }\n }\n return ordering;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAiEA,SAAgB,kBACd,cACA,UACA,SAKA;CACA,MAAM,EAAC,YAAW,sBAChB,cACA,SAAS,KAAK,CAAC,SAAS,GAAG,GAC3B,QAAQ,IACR,QAAQ,OACR,QAAQ,IACV;CACA,QAAQ,wBAAwB,EAAC,MAAM,SAAQ;CAC/C,IAAI,kBAAkB,QAAQ,EAAE,GAC9B,QAAQ,yBAAyB,EAAC,MAAM,SAAQ;CAElD,OAAO;EACL;EACA,YAAY,CAAC,oBAAoB;EAGjC,MAAM,CAAC,GAAG,UAAU,CAAC,sBAAsB,KAAK,CAAC;CACnD;AACF;;;;;;;AAQA,SAAgB,SAAS,QAA4C;CACnE,OAAO,OAAO,WAAW,IAAI,WAAW,OAAO,EAAE,IAAI,KAAK,UAAU,MAAM;AAC5E;;AAGA,SAAgB,YACd,KACA,SACmB;CACnB,OAAO,QAAQ,KAAI,QAAO,mBAAmB,IAAI,IAAI,CAAC;AACxD;;;;;;;;;;;;;;;;;AAkBA,IAAa,UAAb,MAAyC;CACvC;CACA;CACA;CACA;CAEA;CACA;CAIA;CAGA,YAAY;;;;;;CAOZ,+BAAwB,IAAI,IAAiB;CAE7C,UAAkB;;;;;CAMlB,YACE,OACA,SACA,UAC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;IAED,8CAA8C;IAC9C,IAAI,IAAI,IAAI,MAAM,CAEjB;IAED,GAAG,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAuClB;IAED,MAAM,CAAC,GAAG,EAAE,GAAG,GAAG,IAAI,CAuCrB;IAED;;;OAGG;IACH,MAAM,CAAC,QAAQ,EAAE,GAAG,EAAE,QAAQ,EAAE,WAAW,GAAG,gBAAgB,GAAG,SAAS,CAazE;IAED;;;OAGG;IACH,cAAc,IAAI,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAiBlC;CAKF"}
@@ -1,4 +1,5 @@
1
1
  import { must } from "../../../shared/src/must.js";
2
+ import { assignProperty } from "../../../shared/src/objects.js";
2
3
  import { canonicalKey } from "./join-utils.js";
3
4
  //#region ../zql/src/ivm/join-index.ts
4
5
  var UNPARTITIONED = [void 0];
@@ -56,7 +57,7 @@ var JoinIndex = class {
56
57
  let partition = partitions.get(partKey);
57
58
  if (partition === void 0) {
58
59
  const constraint = {};
59
- for (const key of partitionKey) constraint[key] = row[key];
60
+ for (const key of partitionKey) assignProperty(constraint, key, row[key]);
60
61
  partition = {
61
62
  constraint,
62
63
  pks: /* @__PURE__ */ new Set()
@@ -1 +1 @@
1
- {"version":3,"file":"join-index.js","names":[],"sources":["../../../../../zql/src/ivm/join-index.ts"],"sourcesContent":["import {must} from '../../../shared/src/must.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport {canonicalKey} from './join-utils.ts';\n\n/**\n * The partitions of the indexed parents that share a join key, as fetch\n * constraints. An unpartitioned index reports a single `undefined`.\n */\nexport type ParentPartitions = readonly (Record<string, Value> | undefined)[];\n\nconst UNPARTITIONED: ParentPartitions = [undefined];\n\ntype Partition = {\n readonly constraint: Record<string, Value>;\n readonly pks: Set<string>;\n};\n\n/**\n * An in-memory index from a join key to the parent rows that have it. Join\n * and FlippedJoin look up a child change's join key here to skip the parent\n * fetch when no indexed parent can see the change.\n *\n * Parents are tracked by primary key so that adding or removing a row twice\n * is harmless. Lookups only report whether any parent has the key and, when\n * the index is partitioned, in which partitions.\n */\nexport class JoinIndex {\n readonly #parentKey: CompoundKey;\n readonly #primaryKey: CompoundKey;\n /** The join key is the primary key, so one string serves as both. */\n readonly #joinKeyIsPK: boolean;\n readonly #partitionKey: CompoundKey | undefined;\n readonly #unpartitioned = new Map<string, Set<string>>();\n readonly #partitioned = new Map<string, Map<string, Partition>>();\n #size = 0;\n\n constructor(\n parentKey: CompoundKey,\n primaryKey: CompoundKey,\n partitionKey?: CompoundKey | undefined,\n ) {\n this.#parentKey = parentKey;\n this.#primaryKey = primaryKey;\n this.#joinKeyIsPK =\n parentKey.length === primaryKey.length &&\n parentKey.every((k, i) => k === primaryKey[i]);\n this.#partitionKey = partitionKey;\n }\n\n /** The number of parent rows in the index. */\n get size(): number {\n return this.#size;\n }\n\n add(row: Row): void {\n const joinKey = joinKeyOf(row, this.#parentKey);\n if (joinKey === undefined) {\n return;\n }\n const pk = this.#pkOf(row, joinKey);\n const partitionKey = this.#partitionKey;\n if (partitionKey === undefined) {\n let pks = this.#unpartitioned.get(joinKey);\n if (pks === undefined) {\n pks = new Set();\n this.#unpartitioned.set(joinKey, pks);\n }\n if (!pks.has(pk)) {\n pks.add(pk);\n this.#size++;\n }\n return;\n }\n\n const partKey = canonicalKey(row, partitionKey);\n let partitions = this.#partitioned.get(joinKey);\n if (partitions === undefined) {\n partitions = new Map();\n this.#partitioned.set(joinKey, partitions);\n }\n let partition = partitions.get(partKey);\n if (partition === undefined) {\n const constraint: Record<string, Value> = {};\n for (const key of partitionKey) {\n constraint[key] = row[key];\n }\n partition = {constraint, pks: new Set()};\n partitions.set(partKey, partition);\n }\n if (!partition.pks.has(pk)) {\n partition.pks.add(pk);\n this.#size++;\n }\n }\n\n remove(row: Row): void {\n const joinKey = joinKeyOf(row, this.#parentKey);\n if (joinKey === undefined) {\n return;\n }\n const pk = this.#pkOf(row, joinKey);\n const partitionKey = this.#partitionKey;\n if (partitionKey === undefined) {\n const pks = this.#unpartitioned.get(joinKey);\n if (pks === undefined) {\n return;\n }\n if (pks.delete(pk)) {\n this.#size--;\n if (pks.size === 0) {\n this.#unpartitioned.delete(joinKey);\n }\n }\n return;\n }\n\n const partitions = this.#partitioned.get(joinKey);\n if (partitions === undefined) {\n return;\n }\n const partKey = canonicalKey(row, partitionKey);\n const partition = partitions.get(partKey);\n if (partition === undefined) {\n return;\n }\n if (partition.pks.delete(pk)) {\n this.#size--;\n if (partition.pks.size === 0) {\n partitions.delete(partKey);\n if (partitions.size === 0) {\n this.#partitioned.delete(joinKey);\n }\n }\n }\n }\n\n /**\n * The partitions of the indexed parents whose join key matches `childRow`\n * over `childKey`, or `undefined` if there are none.\n */\n lookup(childRow: Row, childKey: CompoundKey): ParentPartitions | undefined {\n const joinKey = joinKeyOf(childRow, childKey);\n if (joinKey === undefined) {\n return undefined;\n }\n if (this.#partitionKey === undefined) {\n return this.#unpartitioned.has(joinKey) ? UNPARTITIONED : undefined;\n }\n const partitions = this.#partitioned.get(joinKey);\n if (partitions === undefined) {\n return undefined;\n }\n return Array.from(partitions.values(), p => p.constraint);\n }\n\n /**\n * The index as the operator storage entries it replaced, keyed\n * `j\\0<joinKey>\\0[<partitionKey>\\0]<primaryKey>`. For tests.\n */\n entriesForTest(): Record<string, 1> {\n const entries: Record<string, 1> = {};\n for (const [joinKey, pks] of this.#unpartitioned) {\n for (const pk of pks) {\n entries[`j\\x00${joinKey}\\x00${pk}`] = 1;\n }\n }\n for (const [joinKey, partitions] of this.#partitioned) {\n const partitionKey = must(this.#partitionKey);\n for (const {constraint, pks} of partitions.values()) {\n const partKey = canonicalKey(constraint, partitionKey);\n for (const pk of pks) {\n entries[`j\\x00${joinKey}\\x00${partKey}\\x00${pk}`] = 1;\n }\n }\n }\n return entries;\n }\n\n #pkOf(row: Row, joinKey: string): string {\n return this.#joinKeyIsPK ? joinKey : canonicalKey(row, this.#primaryKey);\n }\n}\n\n/**\n * The canonical join key of `row`, or `undefined` if any part of it is null:\n * a null key cannot match anything.\n */\nfunction joinKeyOf(row: Row, key: CompoundKey): string | undefined {\n for (const k of key) {\n if (row[k] === null) {\n return undefined;\n }\n }\n return canonicalKey(row, key);\n}\n"],"mappings":";;;AAWA,IAAM,gBAAkC,CAAC,KAAA,CAAS;;;;;;;;;;AAgBlD,IAAa,YAAb,MAAuB;CACrB;CACA;;CAEA;CACA;CACA,iCAA0B,IAAI,IAAyB;CACvD,+BAAwB,IAAI,IAAoC;CAChE,QAAQ;CAER,YACE,WACA,YACA,cACA;EACA,KAAK,aAAa;EAClB,KAAK,cAAc;EACnB,KAAK,eACH,UAAU,WAAW,WAAW,UAChC,UAAU,OAAO,GAAG,MAAM,MAAM,WAAW,EAAE;EAC/C,KAAK,gBAAgB;CACvB;;CAGA,IAAI,OAAe;EACjB,OAAO,KAAK;CACd;CAEA,IAAI,KAAgB;EAClB,MAAM,UAAU,UAAU,KAAK,KAAK,UAAU;EAC9C,IAAI,YAAY,KAAA,GACd;EAEF,MAAM,KAAK,KAAK,MAAM,KAAK,OAAO;EAClC,MAAM,eAAe,KAAK;EAC1B,IAAI,iBAAiB,KAAA,GAAW;GAC9B,IAAI,MAAM,KAAK,eAAe,IAAI,OAAO;GACzC,IAAI,QAAQ,KAAA,GAAW;IACrB,sBAAM,IAAI,IAAI;IACd,KAAK,eAAe,IAAI,SAAS,GAAG;GACtC;GACA,IAAI,CAAC,IAAI,IAAI,EAAE,GAAG;IAChB,IAAI,IAAI,EAAE;IACV,KAAK;GACP;GACA;EACF;EAEA,MAAM,UAAU,aAAa,KAAK,YAAY;EAC9C,IAAI,aAAa,KAAK,aAAa,IAAI,OAAO;EAC9C,IAAI,eAAe,KAAA,GAAW;GAC5B,6BAAa,IAAI,IAAI;GACrB,KAAK,aAAa,IAAI,SAAS,UAAU;EAC3C;EACA,IAAI,YAAY,WAAW,IAAI,OAAO;EACtC,IAAI,cAAc,KAAA,GAAW;GAC3B,MAA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AAI,eAAe,KAAA,GACjB;EAEF,OAAO,MAAM,KAAK,WAAW,OAAO,IAAG,MAAK,EAAE,UAAU;CAC1D;;;;;CAMA,iBAAoC;EAClC,MAAM,UAA6B,CAAC;EACpC,KAAK,MAAM,CAAC,SAAS,QAAQ,KAAK,gBAChC,KAAK,MAAM,MAAM,KACf,QAAQ,QAAQ,QAAQ,MAAM,QAAQ;EAG1C,KAAK,MAAM,CAAC,SAAS,eAAe,KAAK,cAAc;GACrD,MAAM,eAAe,KAAK,KAAK,aAAa;GAC5C,KAAK,MAAM,EAAC,YAAY,SAAQ,WAAW,OAAO,GAAG;IACnD,MAAM,UAAU,aAAa,YAAY,YAAY;IACrD,KAAK,MAAM,MAAM,KACf,QAAQ,QAAQ,QAAQ,MAAM,QAAQ,MAAM,QAAQ;GAExD;EACF;EACA,OAAO;CACT;CAEA,MAAM,KAAU,SAAyB;EACvC,OAAO,KAAK,eAAe,UAAU,aAAa,KAAK,KAAK,WAAW;CACzE;AACF;;;;;AAMA,SAAS,UAAU,KAAU,KAAsC;CACjE,KAAK,MAAM,KAAK,KACd,IAAI,IAAI,OAAO,MACb;CAGJ,OAAO,aAAa,KAAK,GAAG;AAC9B"}
1
+ {"version":3,"file":"join-index.js","names":[],"sources":["../../../../../zql/src/ivm/join-index.ts"],"sourcesContent":["import {must} from '../../../shared/src/must.ts';\nimport {assignProperty} from '../../../shared/src/objects.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport {canonicalKey} from './join-utils.ts';\n\n/**\n * The partitions of the indexed parents that share a join key, as fetch\n * constraints. An unpartitioned index reports a single `undefined`.\n */\nexport type ParentPartitions = readonly (Record<string, Value> | undefined)[];\n\nconst UNPARTITIONED: ParentPartitions = [undefined];\n\ntype Partition = {\n readonly constraint: Record<string, Value>;\n readonly pks: Set<string>;\n};\n\n/**\n * An in-memory index from a join key to the parent rows that have it. Join\n * and FlippedJoin look up a child change's join key here to skip the parent\n * fetch when no indexed parent can see the change.\n *\n * Parents are tracked by primary key so that adding or removing a row twice\n * is harmless. Lookups only report whether any parent has the key and, when\n * the index is partitioned, in which partitions.\n */\nexport class JoinIndex {\n readonly #parentKey: CompoundKey;\n readonly #primaryKey: CompoundKey;\n /** The join key is the primary key, so one string serves as both. */\n readonly #joinKeyIsPK: boolean;\n readonly #partitionKey: CompoundKey | undefined;\n readonly #unpartitioned = new Map<string, Set<string>>();\n readonly #partitioned = new Map<string, Map<string, Partition>>();\n #size = 0;\n\n constructor(\n parentKey: CompoundKey,\n primaryKey: CompoundKey,\n partitionKey?: CompoundKey | undefined,\n ) {\n this.#parentKey = parentKey;\n this.#primaryKey = primaryKey;\n this.#joinKeyIsPK =\n parentKey.length === primaryKey.length &&\n parentKey.every((k, i) => k === primaryKey[i]);\n this.#partitionKey = partitionKey;\n }\n\n /** The number of parent rows in the index. */\n get size(): number {\n return this.#size;\n }\n\n add(row: Row): void {\n const joinKey = joinKeyOf(row, this.#parentKey);\n if (joinKey === undefined) {\n return;\n }\n const pk = this.#pkOf(row, joinKey);\n const partitionKey = this.#partitionKey;\n if (partitionKey === undefined) {\n let pks = this.#unpartitioned.get(joinKey);\n if (pks === undefined) {\n pks = new Set();\n this.#unpartitioned.set(joinKey, pks);\n }\n if (!pks.has(pk)) {\n pks.add(pk);\n this.#size++;\n }\n return;\n }\n\n const partKey = canonicalKey(row, partitionKey);\n let partitions = this.#partitioned.get(joinKey);\n if (partitions === undefined) {\n partitions = new Map();\n this.#partitioned.set(joinKey, partitions);\n }\n let partition = partitions.get(partKey);\n if (partition === undefined) {\n const constraint: Record<string, Value> = {};\n for (const key of partitionKey) {\n assignProperty(constraint, key, row[key]);\n }\n partition = {constraint, pks: new Set()};\n partitions.set(partKey, partition);\n }\n if (!partition.pks.has(pk)) {\n partition.pks.add(pk);\n this.#size++;\n }\n }\n\n remove(row: Row): void {\n const joinKey = joinKeyOf(row, this.#parentKey);\n if (joinKey === undefined) {\n return;\n }\n const pk = this.#pkOf(row, joinKey);\n const partitionKey = this.#partitionKey;\n if (partitionKey === undefined) {\n const pks = this.#unpartitioned.get(joinKey);\n if (pks === undefined) {\n return;\n }\n if (pks.delete(pk)) {\n this.#size--;\n if (pks.size === 0) {\n this.#unpartitioned.delete(joinKey);\n }\n }\n return;\n }\n\n const partitions = this.#partitioned.get(joinKey);\n if (partitions === undefined) {\n return;\n }\n const partKey = canonicalKey(row, partitionKey);\n const partition = partitions.get(partKey);\n if (partition === undefined) {\n return;\n }\n if (partition.pks.delete(pk)) {\n this.#size--;\n if (partition.pks.size === 0) {\n partitions.delete(partKey);\n if (partitions.size === 0) {\n this.#partitioned.delete(joinKey);\n }\n }\n }\n }\n\n /**\n * The partitions of the indexed parents whose join key matches `childRow`\n * over `childKey`, or `undefined` if there are none.\n */\n lookup(childRow: Row, childKey: CompoundKey): ParentPartitions | undefined {\n const joinKey = joinKeyOf(childRow, childKey);\n if (joinKey === undefined) {\n return undefined;\n }\n if (this.#partitionKey === undefined) {\n return this.#unpartitioned.has(joinKey) ? UNPARTITIONED : undefined;\n }\n const partitions = this.#partitioned.get(joinKey);\n if (partitions === undefined) {\n return undefined;\n }\n return Array.from(partitions.values(), p => p.constraint);\n }\n\n /**\n * The index as the operator storage entries it replaced, keyed\n * `j\\0<joinKey>\\0[<partitionKey>\\0]<primaryKey>`. For tests.\n */\n entriesForTest(): Record<string, 1> {\n const entries: Record<string, 1> = {};\n for (const [joinKey, pks] of this.#unpartitioned) {\n for (const pk of pks) {\n entries[`j\\x00${joinKey}\\x00${pk}`] = 1;\n }\n }\n for (const [joinKey, partitions] of this.#partitioned) {\n const partitionKey = must(this.#partitionKey);\n for (const {constraint, pks} of partitions.values()) {\n const partKey = canonicalKey(constraint, partitionKey);\n for (const pk of pks) {\n entries[`j\\x00${joinKey}\\x00${partKey}\\x00${pk}`] = 1;\n }\n }\n }\n return entries;\n }\n\n #pkOf(row: Row, joinKey: string): string {\n return this.#joinKeyIsPK ? joinKey : canonicalKey(row, this.#primaryKey);\n }\n}\n\n/**\n * The canonical join key of `row`, or `undefined` if any part of it is null:\n * a null key cannot match anything.\n */\nfunction joinKeyOf(row: Row, key: CompoundKey): string | undefined {\n for (const k of key) {\n if (row[k] === null) {\n return undefined;\n }\n }\n return canonicalKey(row, key);\n}\n"],"mappings":";;;;AAYA,IAAM,gBAAkC,CAAC,KAAA,CAAS;;;;;;;;;;AAgBlD,IAAa,YAAb,MAAuB;CACrB;CACA;;CAEA;CACA;CACA,iCAA0B,IAAI,IAAyB;CACvD,+BAAwB,IAAI,IAAoC;CAChE,QAAQ;CAER,YACE,WACA,YACA,cACA;EACA,KAAK,aAAa;EAClB,KAAK,cAAc;EACnB,KAAK,eACH,UAAU,WAAW,WAAW,UAChC,UAAU,OAAO,GAAG,MAAM,MAAM,WAAW,EAAE;EAC/C,KAAK,gBAAgB;CACvB;;CAGA,IAAI,OAAe;EACjB,OAAO,KAAK;CACd;CAEA,IAAI,KAAgB;EAClB,MAAM,UAAU,UAAU,KAAK,KAAK,UAAU;EAC9C,IAAI,YAAY,KAAA,GACd;EAEF,MAAM,KAAK,KAAK,MAAM,KAAK,OAAO;EAClC,MAAM,eAAe,KAAK;EAC1B,IAAI,iBAAiB,KAAA,GAAW;GAC9B,IAAI,MAAM,KAAK,eAAe,IAAI,OAAO;GACzC,IAAI,QAAQ,KAAA,GAAW;IACrB,sBAAM,IAAI,IAAI;IACd,KAAK,eAAe,IAAI,SAAS,GAAG;GACtC;GACA,IAAI,CAAC,IAAI,IAAI,EAAE,GAAG;IAChB,IAAI,IAAI,EAAE;IACV,KAAK;GACP;GACA;EACF;EAEA,MAAM,UAAU,aAAa,KAAK,YAAY;EAC9C,IAAI,aAAa,KAAK,aAAa,IAAI,OAAO;EAC9C,IAAI,eAAe,KAAA,GAAW;GAC5B,6BAAa,IAAI,IAAI;GACrB,KAAK,aAAa,IAAI,SAAS,UAAU;EAC3C;EACA,IAAI,YAAY,WAAW,IAAI,OAAO;EACtC,I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GAAG,EAAE,WAAW,GACf,OAAO,CAOT;AAED,wBAAgB,WAAW,CACzB,MAAM,EAAE,GAAG,EACX,SAAS,EAAE,WAAW,EACtB,KAAK,EAAE,GAAG,EACV,QAAQ,EAAE,WAAW,WAQtB;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,GAAG,EACd,SAAS,EAAE,WAAW,EACtB,SAAS,EAAE,WAAW,GACrB,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,GAAG,SAAS,CAUnC;AAED,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,SAAS,CAAC,EACzC,IAAI,EAAE,WAAW,GAChB,MAAM,CAER;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,GAAG,SAAS,CAAC,EACzC,IAAI,EAAE,WAAW,GAChB,MAAM,CAUR"}
@@ -1,4 +1,5 @@
1
1
  import { assert } from "../../../shared/src/asserts.js";
2
+ import { assignProperty } from "../../../shared/src/objects.js";
2
3
  import { compareValues, valuesEqual } from "./data.js";
3
4
  //#region ../zql/src/ivm/join-utils.ts
4
5
  function generateWithOverlayNoYield(stream, overlay, schema) {
@@ -118,7 +119,7 @@ function buildJoinConstraint(sourceRow, sourceKey, targetKey) {
118
119
  for (let i = 0; i < targetKey.length; i++) {
119
120
  const value = sourceRow[sourceKey[i]];
120
121
  if (value === null) return;
121
- constraint[targetKey[i]] = value;
122
+ assignProperty(constraint, targetKey[i], value);
122
123
  }
123
124
  return constraint;
124
125
  }
@@ -1 +1 @@
1
- {"version":3,"file":"join-utils.js","names":[],"sources":["../../../../../zql/src/ivm/join-utils.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport {compareValues, valuesEqual, type Node} from './data.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\nexport function generateWithOverlayNoYield(\n stream: Stream<Node>,\n overlay: Change,\n schema: SourceSchema,\n): Stream<Node> {\n return generateWithOverlay(stream, overlay, schema) as Stream<Node>;\n}\n\nexport function* generateWithOverlay(\n stream: Stream<Node | 'yield'>,\n overlay: Change,\n schema: SourceSchema,\n): Stream<Node | 'yield'> {\n let applied = false;\n let editOldApplied = false;\n let editNewApplied = false;\n for (const node of stream) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n let yieldNode = true;\n if (!applied) {\n switch (overlay[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n if (\n schema.compareRows(overlay[ChangeIndex.NODE].row, node.row) === 0\n ) {\n applied = true;\n yieldNode = false;\n }\n break;\n }\n case ChangeType.REMOVE: {\n if (schema.compareRows(overlay[ChangeIndex.NODE].row, node.row) < 0) {\n applied = true;\n yield overlay[ChangeIndex.NODE];\n }\n break;\n }\n case ChangeType.EDIT: {\n if (\n !editOldApplied &&\n schema.compareRows(overlay[ChangeIndex.OLD_NODE].row, node.row) < 0\n ) {\n editOldApplied = true;\n if (editNewApplied) {\n applied = true;\n }\n yield overlay[ChangeIndex.OLD_NODE];\n }\n if (\n !editNewApplied &&\n schema.compareRows(overlay[ChangeIndex.NODE].row, node.row) === 0\n ) {\n editNewApplied = true;\n if (editOldApplied) {\n applied = true;\n }\n yieldNode = false;\n }\n break;\n }\n case ChangeType.CHILD: {\n if (\n schema.compareRows(overlay[ChangeIndex.NODE].row, node.row) === 0\n ) {\n applied = true;\n yield {\n row: node.row,\n relationships: {\n ...node.relationships,\n [overlay[ChangeIndex.CHILD_DATA].relationshipName]: () =>\n generateWithOverlay(\n node.relationships[\n overlay[ChangeIndex.CHILD_DATA].relationshipName\n ](),\n overlay[ChangeIndex.CHILD_DATA].change,\n schema.relationships[\n overlay[ChangeIndex.CHILD_DATA].relationshipName\n ],\n ),\n },\n };\n yieldNode = false;\n }\n break;\n }\n }\n }\n if (yieldNode) {\n yield node;\n }\n }\n if (!applied) {\n if (overlay[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n applied = true;\n yield overlay[ChangeIndex.NODE];\n } else if (overlay[ChangeIndex.TYPE] === ChangeType.EDIT) {\n assert(\n editNewApplied,\n 'edit overlay: new node must be applied before old node',\n );\n editOldApplied = true;\n applied = true;\n yield overlay[ChangeIndex.OLD_NODE];\n }\n }\n\n assert(\n applied,\n 'overlayGenerator: overlay was never applied to any fetched node',\n );\n}\n\nexport function generateWithOverlayNoYieldUnordered(\n stream: Stream<Node>,\n overlay: Change,\n schema: SourceSchema,\n): Stream<Node> {\n return generateWithOverlayUnordered(stream, overlay, schema) as Stream<Node>;\n}\n\nexport function* generateWithOverlayUnordered(\n stream: Stream<Node | 'yield'>,\n overlay: Change,\n schema: SourceSchema,\n): Stream<Node | 'yield'> {\n // Eager inject\n if (overlay[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n yield overlay[ChangeIndex.NODE];\n } else if (overlay[ChangeIndex.TYPE] === ChangeType.EDIT) {\n yield overlay[ChangeIndex.OLD_NODE];\n }\n\n // Stream with inline suppress\n let suppressed = false;\n for (const node of stream) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (!suppressed) {\n if (\n overlay[ChangeIndex.TYPE] === ChangeType.ADD ||\n overlay[ChangeIndex.TYPE] === ChangeType.EDIT\n ) {\n if (\n rowEqualsForCompoundKey(\n overlay[ChangeIndex.NODE].row,\n node.row,\n schema.primaryKey,\n )\n ) {\n suppressed = true;\n continue;\n }\n }\n if (overlay[ChangeIndex.TYPE] === ChangeType.CHILD) {\n if (\n rowEqualsForCompoundKey(\n overlay[ChangeIndex.NODE].row,\n node.row,\n schema.primaryKey,\n )\n ) {\n suppressed = true;\n yield {\n row: node.row,\n relationships: {\n ...node.relationships,\n [overlay[ChangeIndex.CHILD_DATA].relationshipName]: () =>\n generateWithOverlay(\n node.relationships[\n overlay[ChangeIndex.CHILD_DATA].relationshipName\n ](),\n overlay[ChangeIndex.CHILD_DATA].change,\n schema.relationships[\n overlay[ChangeIndex.CHILD_DATA].relationshipName\n ],\n ),\n },\n };\n continue;\n }\n }\n }\n yield node;\n }\n assert(\n suppressed || overlay[ChangeIndex.TYPE] === ChangeType.REMOVE,\n 'overlayGenerator: overlay was never applied to any fetched node',\n );\n}\n\nexport function rowEqualsForCompoundKey(\n a: Row,\n b: Row,\n key: CompoundKey,\n): boolean {\n for (let i = 0; i < key.length; i++) {\n if (compareValues(a[key[i]], b[key[i]]) !== 0) {\n return false;\n }\n }\n return true;\n}\n\nexport function isJoinMatch(\n parent: Row,\n parentKey: CompoundKey,\n child: Row,\n childKey: CompoundKey,\n) {\n for (let i = 0; i < parentKey.length; i++) {\n if (!valuesEqual(parent[parentKey[i]], child[childKey[i]])) {\n return false;\n }\n }\n return true;\n}\n\n/**\n * Builds a constraint object by mapping values from `sourceRow` using `sourceKey`\n * to keys specified by `targetKey`. Returns `undefined` if any source value is `null`,\n * since null foreign keys cannot match any rows.\n */\nexport function buildJoinConstraint(\n sourceRow: Row,\n sourceKey: CompoundKey,\n targetKey: CompoundKey,\n): Record<string, Value> | undefined {\n const constraint: Record<string, Value> = {};\n for (let i = 0; i < targetKey.length; i++) {\n const value = sourceRow[sourceKey[i]];\n if (value === null) {\n return undefined;\n }\n constraint[targetKey[i]] = value;\n }\n return constraint;\n}\n\nexport function canonicalKeyForTest(\n record: Record<string, Value | undefined>,\n keys: CompoundKey,\n): string {\n return canonicalKey(record, keys);\n}\n\n/**\n * Canonical string key over `keys` of `record`. Tags values by type\n * so distinct types (e.g. 1 and \"1\") do not collide.\n */\nexport function canonicalKey(\n record: Record<string, Value | undefined>,\n keys: CompoundKey,\n): string {\n if (keys.length === 1) {\n return canonicalValue(record[keys[0]]);\n }\n let s = '';\n for (let i = 0; i < keys.length; i++) {\n if (i > 0) s += '\\x00';\n s += canonicalValue(record[keys[i]]);\n }\n return s;\n}\n\nfunction canonicalValue(v: Value): string {\n // Tag by type so we don't conflate e.g. `1` (number) with `\"1\"` (string).\n if (v === null || v === undefined) return 'n';\n const t = typeof v;\n if (t === 'string') return 's' + (v as string);\n if (t === 'number') return 'd' + (v as number);\n if (t === 'boolean') return v ? 't' : 'f';\n return 'j' + JSON.stringify(v);\n}\n"],"mappings":";;;AAUA,SAAgB,2BACd,QACA,SACA,QACc;CACd,OAAO,oBAAoB,QAAQ,SAAS,MAAM;AACpD;AAEA,UAAiB,oBACf,QACA,SACA,QACwB;CACxB,IAAI,UAAU;CACd,IAAI,iBAAiB;CACrB,IAAI,iBAAiB;CACrB,KAAK,MAAM,QAAQ,QAAQ;EACzB,IAAI,SAAS,SAAS;GACpB,MAAM;GACN;EACF;EACA,IAAI,YAAY;EAChB,IAAI,CAAC,SACH,QAAQ,QAAQ,IAAhB;GACE,KAAK;IACH,IACE,OAAO,YAAY,QAAQ,EAAiB,CAAC,KAAK,KAAK,GAAG,MAAM,GAChE;KACA,UAAU;KACV,YAAY;IACd;IACA;GAEF,KAAK;IACH,IAAI,OAAO,YAAY,QAAQ,EAAiB,CAAC,KAAK,KAAK,GAAG,IAAI,GAAG;KACnE,UAAU;KACV,MAAM,QAAQ;IAChB;IACA;GAEF,KAAK;IACH,IACE,CAAC,kBACD,OAAO,YAAY,QAAQ,EAAqB,CAAC,KAAK,KAAK,GAAG,IAAI,GAClE;KACA,iBAAiB;KACjB,IAAI,gBACF,UAAU;KAEZ,MAAM,QAAQ;IAChB;IACA,IACE,CAAC,kBACD,OAAO,YAAY,QAAQ,EAAiB,CAAC,KAAK,KAAK,GAAG,MAAM,GAChE;KACA,iBAAiB;KACjB,IAAI,gBACF,UAAU;KAEZ,YAAY;IACd;IACA;GAEF,KAAK,GACH,IACE,OAAO,YAAY,QAAQ,EAAiB,CAAC,KAAK,KAAK,GAAG,MAAM,GAChE;IACA,UAAU;IACV,MAAM;KACJ,KAAK,KAAK;KAC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ACE,cAAc,QAAQ,OAAsB,GAC5C,iEACF;AACF;AAEA,SAAgB,wBACd,GACA,GACA,KACS;CACT,KAAK,IAAI,IAAI,GAAG,IAAI,IAAI,QAAQ,KAC9B,IAAI,cAAc,EAAE,IAAI,KAAK,EAAE,IAAI,GAAG,MAAM,GAC1C,OAAO;CAGX,OAAO;AACT;AAEA,SAAgB,YACd,QACA,WACA,OACA,UACA;CACA,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KACpC,IAAI,CAAC,YAAY,OAAO,UAAU,KAAK,MAAM,SAAS,GAAG,GACvD,OAAO;CAGX,OAAO;AACT;;;;;;AAOA,SAAgB,oBACd,WACA,WACA,WACmC;CACnC,MAAM,aAAoC,CAAC;CAC3C,KAAK,IAAI,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;EACzC,MAAM,QAAQ,UAAU,UAAU;EAClC,IAAI,UAAU,MACZ;EAEF,WAAW,UAAU,MAAM;CAC7B;CACA,OAAO;AACT;;;;;AAaA,SAAgB,aACd,QACA,MACQ;CACR,IAAI,KAAK,WAAW,GAClB,OAAO,eAAe,OAAO,KAAK,GAAG;CAEvC,IAAI,IAAI;CACR,KAAK,IAAI,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAK;EACpC,IAAI,IAAI,GAAG,KAAK;EAChB,KAAK,eAAe,OAAO,KAAK,GAAG;CACrC;CACA,OAAO;AACT;AAEA,SAAS,eAAe,GAAkB;CAExC,IAAI,MAAM,QAAQ,MAAM,KAAA,GAAW,OAAO;CAC1C,MAAM,IAAI,OAAO;CACjB,IAAI,MAAM,UAAU,OAAO,MAAO;CAClC,IAAI,MAAM,UAAU,OAAO,MAAO;CAClC,IAAI,MAAM,WAAW,OAAO,IAAI,MAAM;CACtC,OAAO,MAAM,KAAK,UAAU,CAAC;AAC/B"}
1
+ {"version":3,"file":"join-utils.js","names":[],"sources":["../../../../../zql/src/ivm/join-utils.ts"],"sourcesContent":["import {assert} from '../../../shared/src/asserts.ts';\nimport {assignProperty} from '../../../shared/src/objects.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport {compareValues, valuesEqual, type Node} from './data.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\nexport function generateWithOverlayNoYield(\n stream: Stream<Node>,\n overlay: Change,\n schema: SourceSchema,\n): Stream<Node> {\n return generateWithOverlay(stream, overlay, schema) as Stream<Node>;\n}\n\nexport function* generateWithOverlay(\n stream: Stream<Node | 'yield'>,\n overlay: Change,\n schema: SourceSchema,\n): Stream<Node | 'yield'> {\n let applied = false;\n let editOldApplied = false;\n let editNewApplied = false;\n for (const node of stream) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n let yieldNode = true;\n if (!applied) {\n switch (overlay[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n if (\n schema.compareRows(overlay[ChangeIndex.NODE].row, node.row) === 0\n ) {\n applied = true;\n yieldNode = false;\n }\n break;\n }\n case ChangeType.REMOVE: {\n if (schema.compareRows(overlay[ChangeIndex.NODE].row, node.row) < 0) {\n applied = true;\n yield overlay[ChangeIndex.NODE];\n }\n break;\n }\n case ChangeType.EDIT: {\n if (\n !editOldApplied &&\n schema.compareRows(overlay[ChangeIndex.OLD_NODE].row, node.row) < 0\n ) {\n editOldApplied = true;\n if (editNewApplied) {\n applied = true;\n }\n yield overlay[ChangeIndex.OLD_NODE];\n }\n if (\n !editNewApplied &&\n schema.compareRows(overlay[ChangeIndex.NODE].row, node.row) === 0\n ) {\n editNewApplied = true;\n if (editOldApplied) {\n applied = true;\n }\n yieldNode = false;\n }\n break;\n }\n case ChangeType.CHILD: {\n if (\n schema.compareRows(overlay[ChangeIndex.NODE].row, node.row) === 0\n ) {\n applied = true;\n yield {\n row: node.row,\n relationships: {\n ...node.relationships,\n [overlay[ChangeIndex.CHILD_DATA].relationshipName]: () =>\n generateWithOverlay(\n node.relationships[\n overlay[ChangeIndex.CHILD_DATA].relationshipName\n ](),\n overlay[ChangeIndex.CHILD_DATA].change,\n schema.relationships[\n overlay[ChangeIndex.CHILD_DATA].relationshipName\n ],\n ),\n },\n };\n yieldNode = false;\n }\n break;\n }\n }\n }\n if (yieldNode) {\n yield node;\n }\n }\n if (!applied) {\n if (overlay[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n applied = true;\n yield overlay[ChangeIndex.NODE];\n } else if (overlay[ChangeIndex.TYPE] === ChangeType.EDIT) {\n assert(\n editNewApplied,\n 'edit overlay: new node must be applied before old node',\n );\n editOldApplied = true;\n applied = true;\n yield overlay[ChangeIndex.OLD_NODE];\n }\n }\n\n assert(\n applied,\n 'overlayGenerator: overlay was never applied to any fetched node',\n );\n}\n\nexport function generateWithOverlayNoYieldUnordered(\n stream: Stream<Node>,\n overlay: Change,\n schema: SourceSchema,\n): Stream<Node> {\n return generateWithOverlayUnordered(stream, overlay, schema) as Stream<Node>;\n}\n\nexport function* generateWithOverlayUnordered(\n stream: Stream<Node | 'yield'>,\n overlay: Change,\n schema: SourceSchema,\n): Stream<Node | 'yield'> {\n // Eager inject\n if (overlay[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n yield overlay[ChangeIndex.NODE];\n } else if (overlay[ChangeIndex.TYPE] === ChangeType.EDIT) {\n yield overlay[ChangeIndex.OLD_NODE];\n }\n\n // Stream with inline suppress\n let suppressed = false;\n for (const node of stream) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (!suppressed) {\n if (\n overlay[ChangeIndex.TYPE] === ChangeType.ADD ||\n overlay[ChangeIndex.TYPE] === ChangeType.EDIT\n ) {\n if (\n rowEqualsForCompoundKey(\n overlay[ChangeIndex.NODE].row,\n node.row,\n schema.primaryKey,\n )\n ) {\n suppressed = true;\n continue;\n }\n }\n if (overlay[ChangeIndex.TYPE] === ChangeType.CHILD) {\n if (\n rowEqualsForCompoundKey(\n overlay[ChangeIndex.NODE].row,\n node.row,\n schema.primaryKey,\n )\n ) {\n suppressed = true;\n yield {\n row: node.row,\n relationships: {\n ...node.relationships,\n [overlay[ChangeIndex.CHILD_DATA].relationshipName]: () =>\n generateWithOverlay(\n node.relationships[\n overlay[ChangeIndex.CHILD_DATA].relationshipName\n ](),\n overlay[ChangeIndex.CHILD_DATA].change,\n schema.relationships[\n overlay[ChangeIndex.CHILD_DATA].relationshipName\n ],\n ),\n },\n };\n continue;\n }\n }\n }\n yield node;\n }\n assert(\n suppressed || overlay[ChangeIndex.TYPE] === ChangeType.REMOVE,\n 'overlayGenerator: overlay was never applied to any fetched node',\n );\n}\n\nexport function rowEqualsForCompoundKey(\n a: Row,\n b: Row,\n key: CompoundKey,\n): boolean {\n for (let i = 0; i < key.length; i++) {\n if (compareValues(a[key[i]], b[key[i]]) !== 0) {\n return false;\n }\n }\n return true;\n}\n\nexport function isJoinMatch(\n parent: Row,\n parentKey: CompoundKey,\n child: Row,\n childKey: CompoundKey,\n) {\n for (let i = 0; i < parentKey.length; i++) {\n if (!valuesEqual(parent[parentKey[i]], child[childKey[i]])) {\n return false;\n }\n }\n return true;\n}\n\n/**\n * Builds a constraint object by mapping values from `sourceRow` using `sourceKey`\n * to keys specified by `targetKey`. Returns `undefined` if any source value is `null`,\n * since null foreign keys cannot match any rows.\n */\nexport function buildJoinConstraint(\n sourceRow: Row,\n sourceKey: CompoundKey,\n targetKey: CompoundKey,\n): Record<string, Value> | undefined {\n const constraint: Record<string, Value> = {};\n for (let i = 0; i < targetKey.length; i++) {\n const value = sourceRow[sourceKey[i]];\n if (value === null) {\n return undefined;\n }\n assignProperty(constraint, targetKey[i], value);\n }\n return constraint;\n}\n\nexport function canonicalKeyForTest(\n record: Record<string, Value | undefined>,\n keys: CompoundKey,\n): string {\n return canonicalKey(record, keys);\n}\n\n/**\n * Canonical string key over `keys` of `record`. Tags values by type\n * so distinct types (e.g. 1 and \"1\") do not collide.\n */\nexport function canonicalKey(\n record: Record<string, Value | undefined>,\n keys: CompoundKey,\n): string {\n if (keys.length === 1) {\n return canonicalValue(record[keys[0]]);\n }\n let s = '';\n for (let i = 0; i < keys.length; i++) {\n if (i > 0) s += '\\x00';\n s += canonicalValue(record[keys[i]]);\n }\n return s;\n}\n\nfunction canonicalValue(v: Value): string {\n // Tag by type so we don't conflate e.g. `1` (number) with `\"1\"` (string).\n if (v === null || v === undefined) return 'n';\n const t = typeof v;\n if (t === 'string') return 's' + (v as string);\n if (t === 'number') return 'd' + (v as number);\n if (t === 'boolean') return v ? 't' : 'f';\n return 'j' + JSON.stringify(v);\n}\n"],"mappings":";;;;AAWA,SAAgB,2BACd,QACA,SACA,QACc;CACd,OAAO,oBAAoB,QAAQ,SAAS,MAAM;AACpD;AAEA,UAAiB,oBACf,QACA,SACA,QACwB;CACxB,IAAI,UAAU;CACd,IAAI,iBAAiB;CACrB,IAAI,iBAAiB;CACrB,KAAK,MAAM,QAAQ,QAAQ;EACzB,IAAI,SAAS,SAAS;GACpB,MAAM;GACN;EACF;EACA,IAAI,YAAY;EAChB,IAAI,CAAC,SACH,QAAQ,QAAQ,IAAhB;GACE,KAAK;IACH,IACE,OAAO,YAAY,QAAQ,EAAiB,CAAC,KAAK,KAAK,GAAG,MAAM,GAChE;KACA,UAAU;KACV,YAAY;IACd;IACA;GAEF,KAAK;IACH,IAAI,OAAO,YAAY,QAAQ,EAAiB,CAAC,KAAK,KAAK,GAAG,IAAI,GAAG;KACnE,UAAU;KACV,MAAM,QAAQ;IAChB;IACA;GAEF,KAAK;IACH,IACE,CAAC,kBACD,OAAO,YAAY,QAAQ,EAAqB,CAAC,KAAK,KAAK,GAAG,IAAI,GAClE;KACA,iBAAiB;KACjB,IAAI,gBACF,UAAU;KAEZ,MAAM,QAAQ;IAChB;IACA,IACE,CAAC,kBACD,OAAO,YAAY,QAAQ,EAAiB,CAAC,KAAK,KAAK,GAAG,MAAM,GAChE;KACA,iBAAiB;KACjB,IAAI,gBACF,UA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O,IAAI,MAAM;CACtC,OAAO,MAAM,KAAK,UAAU,CAAC;AAC/B"}
@@ -1,6 +1,6 @@
1
1
  import { assert, unreachable } from "../../../shared/src/asserts.js";
2
- import { throwOutput } from "./operator.js";
3
2
  import { makeAddChange, makeChildChange, makeEditChange, makeRemoveChange } from "./change.js";
3
+ import { throwOutput } from "./operator.js";
4
4
  import { buildJoinConstraint, canonicalKey, generateWithOverlay, generateWithOverlayUnordered, isJoinMatch, rowEqualsForCompoundKey } from "./join-utils.js";
5
5
  import { JoinIndex } from "./join-index.js";
6
6
  import { mergeSortedStreams } from "./memory-source.js";