@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
@@ -1,5 +1,10 @@
1
1
  import { assert, unreachable } from "../../../shared/src/asserts.js";
2
+ import { parse } from "../../../shared/src/valita.js";
2
3
  import { must } from "../../../shared/src/must.js";
4
+ import { formatJsonPathReference, simpleConditionSchema } from "../../../zero-protocol/src/ast.js";
5
+ import { assignProperty } from "../../../shared/src/objects.js";
6
+ import { AGGREGATE_KEY_COLUMN, Aggregate, Reducer, aggregateSourceSchema, assertNotSyntheticColumn, isInvertible } from "../ivm/aggregate.js";
7
+ import { AGGREGATE_TABLE_PREFIX } from "../../../zero-protocol/src/aggregate-table.js";
3
8
  import { completeOrdering } from "../query/complete-ordering.js";
4
9
  import { Take } from "../ivm/take.js";
5
10
  import { Cap } from "../ivm/cap.js";
@@ -12,8 +17,10 @@ import { JoinIndex } from "../ivm/join-index.js";
12
17
  import { createPredicate, transformFilters } from "./filter.js";
13
18
  import { TakeGate } from "../ivm/take-gate.js";
14
19
  import { FlippedJoin } from "../ivm/flipped-join.js";
20
+ import { GroupBy, GroupProjection, groupOrdering, groupSourceSchema } from "../ivm/group-by.js";
15
21
  import { JoinIndexTap } from "../ivm/join-index-tap.js";
16
22
  import { Join } from "../ivm/join.js";
23
+ import { LIFTED_FIELD_COLUMN, LiftField } from "../ivm/lift-field.js";
17
24
  import { Skip } from "../ivm/skip.js";
18
25
  import { UnionFanIn } from "../ivm/union-fan-in.js";
19
26
  import { UnionFanOut } from "../ivm/union-fan-out.js";
@@ -46,6 +53,12 @@ import { pushDownCorrelatedPredicates } from "./correlated-predicate-pushdown.js
46
53
  */
47
54
  function buildPipeline(ast, delegate, queryID, costModel, lc, planDebugger) {
48
55
  ast = delegate.mapAst ? delegate.mapAst(ast) : ast;
56
+ assert(ast.groupBy === void 0 || ast.aggregate?.distinct === void 0, "a count of distinct values per group is not supported yet");
57
+ if ((ast.aggregate || ast.groupBy) && delegate.aggregatesFromSource) return buildPipelineInternal(ast, delegate, queryID, "");
58
+ if (ast.groupBy || ast.aggregate?.distinct) ast = {
59
+ ...ast,
60
+ orderBy: groupOrdering(ast)
61
+ };
49
62
  ast = completeOrdering(ast, (tableName) => must(delegate.getSource(tableName)).tableSchema.primaryKey);
50
63
  const columnsOf = (tableName) => must(delegate.getSource(tableName)).tableSchema.columns;
51
64
  const { planWarnings } = delegate;
@@ -71,11 +84,15 @@ function bindStaticParameters(ast, staticQueryParameters) {
71
84
  }))
72
85
  });
73
86
  function bindCondition(condition) {
74
- if (condition.type === "simple") return {
75
- ...condition,
76
- left: bindValue(condition.left),
77
- right: bindValue(condition.right)
78
- };
87
+ if (condition.type === "simple") {
88
+ const bound = {
89
+ ...condition,
90
+ left: bindValue(condition.left),
91
+ right: bindValue(condition.right)
92
+ };
93
+ if (bound.left.type === "json" && condition.right.type === "static") parse(bound, simpleConditionSchema);
94
+ return bound;
95
+ }
79
96
  if (condition.type === "correlatedSubquery") return {
80
97
  ...condition,
81
98
  related: {
@@ -129,6 +146,15 @@ function assertNoNotExists(condition) {
129
146
  }
130
147
  }
131
148
  function buildPipelineInternal(ast, delegate, queryID, name, partitionKey, isNonFlippedExistsChild) {
149
+ assert(ast.groupBy === void 0 || name === "", "groupBy is only supported at the top level");
150
+ if (ast.aggregate && !ast.groupBy && delegate.aggregatesFromSource) {
151
+ const aggTable = topLevelAggregateTableName(queryID);
152
+ const aggSource = delegate.getSource(aggTable);
153
+ if (!aggSource) throw new Error(`Aggregate source not found: ${aggTable}`);
154
+ const conn = aggSource.connect([[AGGREGATE_KEY_COLUMN, "asc"]]);
155
+ return delegate.decorateInput(delegate.decorateSourceInput(conn, queryID), `${name}:aggregateSource`);
156
+ }
157
+ if (ast.groupBy && delegate.aggregatesFromSource) return groupFromSource(ast, delegate, queryID, name);
132
158
  const source = delegate.getSource(ast.table);
133
159
  if (!source) throw new Error(`Source not found: ${ast.table}`);
134
160
  ast = uniquifyCorrelatedSubqueryConditionAliases(ast);
@@ -199,6 +225,30 @@ function buildPipelineInternal(ast, delegate, queryID, name, partitionKey, isNon
199
225
  delegate.addEdge(end, tap);
200
226
  end = delegate.decorateInput(tap, tapName);
201
227
  }
228
+ if (ast.groupBy) {
229
+ assert(ast.start === void 0 && ast.limit === void 0 && ast.related === void 0, "groupBy does not support start, limit or related");
230
+ const sourceAppliesAll = csqConditions.length === 0 && (!ast.where || fullyAppliedFilters && !delegate.applyFiltersAnyway);
231
+ const groupName = `${name}:groupBy`;
232
+ const groupBy = new GroupBy(end, delegate.createStorage(groupName), groupOrdering(ast), groupTableName(queryID), reducerOf(ast), sourceAppliesAll ? conn : void 0);
233
+ delegate.addEdge(end, groupBy);
234
+ return delegate.decorateInput(groupBy, groupName);
235
+ }
236
+ if (ast.aggregate) {
237
+ const sourceAppliesAll = ast.start === void 0 && ast.limit === void 0 && csqConditions.length === 0 && (!ast.where || fullyAppliedFilters && !delegate.applyFiltersAnyway);
238
+ let countSource = conn;
239
+ if (ast.aggregate.distinct) {
240
+ assert(ast.start === void 0 && ast.limit === void 0, "a count of distinct values does not support start or limit");
241
+ const groupName = `${name}:groupBy`;
242
+ const groupBy = new GroupBy(end, delegate.createStorage(groupName), groupOrdering(ast), groupTableName(queryID), new Reducer("count", void 0, void 0), sourceAppliesAll ? conn : void 0);
243
+ delegate.addEdge(end, groupBy);
244
+ end = delegate.decorateInput(groupBy, groupName);
245
+ countSource = void 0;
246
+ }
247
+ const aggName = `${name}:aggregate`;
248
+ const aggregate = new Aggregate(end, delegate.createStorage(aggName), [], ast.aggregate.fn, ast.aggregate.field, topLevelAggregateTableName(queryID), sourceAppliesAll ? countSource : void 0, ast.aggregate.path);
249
+ delegate.addEdge(end, aggregate);
250
+ return delegate.decorateInput(aggregate, aggName);
251
+ }
202
252
  if (ast.related) {
203
253
  const byAlias = /* @__PURE__ */ new Map();
204
254
  for (const csq of ast.related) byAlias.set(csq.subquery.alias ?? "", csq);
@@ -249,7 +299,7 @@ function applyFilterWithFlips(input, condition, delegate, name, parentPartitionK
249
299
  }
250
300
  case "correlatedSubquery": {
251
301
  const sq = condition.related;
252
- const child = buildPipelineInternal(sq.subquery, delegate, "", `${name}.${sq.subquery.alias}`, sq.correlation.childField, false);
302
+ const child = buildPipelineInternal(sq.subquery, delegate, "", childPipelineName(name, sq), sq.correlation.childField, false);
253
303
  let parentIndex;
254
304
  if (existsParentIndexes) {
255
305
  parentIndex = new JoinIndex(sq.correlation.parentField, end.getSchema().primaryKey, parentPartitionKey);
@@ -336,18 +386,86 @@ function valuePosName(left) {
336
386
  case "static": return left.field;
337
387
  case "literal": return left.value;
338
388
  case "column": return left.name;
389
+ case "json": return formatJsonPathReference(left);
390
+ default: unreachable(left);
339
391
  }
340
392
  }
341
393
  function applyCorrelatedSubQuery(sq, delegate, queryID, end, name, fromCondition, parentPartitionKey, boundProvider, exists) {
342
394
  if (sq.subquery.limit === 0 && fromCondition) return end;
343
395
  assert(sq.subquery.alias, "Subquery must have an alias");
344
- const child = buildPipelineInternal(sq.subquery, delegate, queryID, `${name}.${sq.subquery.alias}`, sq.correlation.childField, fromCondition);
396
+ let childInput;
397
+ let childKey = sq.correlation.childField;
398
+ if (sq.aggregate) {
399
+ assert(sq.aggregate.distinct === void 0, "a count of distinct values is not supported in a relationship yet");
400
+ const aggTable = aggregateTableName(queryID, sq, name);
401
+ if (delegate.aggregatesFromSource) {
402
+ childKey = delegate.aggregateKeyColumns?.(sq.subquery.table, sq.correlation.childField) ?? sq.correlation.childField;
403
+ for (const col of childKey) assertNotSyntheticColumn(col);
404
+ let source;
405
+ if (delegate.getAggregateSource) {
406
+ const childSource = delegate.getSource(sq.subquery.table);
407
+ if (!childSource) throw new Error(`Source not found: ${sq.subquery.table}`);
408
+ let inputColumns = childSource.tableSchema.columns;
409
+ const isJunction = !!(sq.subquery.related && sq.subquery.related.length);
410
+ if (isJunction) {
411
+ const destTable = must(sq.subquery.related)[0].subquery.table;
412
+ const destSource = delegate.getSource(destTable);
413
+ if (!destSource) throw new Error(`Source not found: ${destTable}`);
414
+ inputColumns = {
415
+ ...childSource.tableSchema.columns,
416
+ ...destSource.tableSchema.columns
417
+ };
418
+ }
419
+ const { columns: clientColumns } = aggregateSourceSchema(inputColumns, sq.correlation.childField, sq.aggregate.fn, sq.aggregate.field, sq.aggregate.path);
420
+ const columns = renameColumns(clientColumns, sq.correlation.childField, childKey);
421
+ const { fn } = sq.aggregate;
422
+ const invertible = isInvertible(fn);
423
+ let whereEvaluable = true;
424
+ let wherePredicate;
425
+ if (!isJunction && invertible && sq.subquery.where !== void 0) {
426
+ const { filters, conditionsRemoved } = transformFilters(sq.subquery.where);
427
+ if (conditionsRemoved || filters === void 0) whereEvaluable = false;
428
+ else wherePredicate = createPredicate(filters);
429
+ }
430
+ source = delegate.getAggregateSource(aggTable, columns, childKey, !isJunction && invertible && whereEvaluable && sq.subquery.limit === void 0 && sq.subquery.start === void 0 ? {
431
+ table: sq.subquery.table,
432
+ childField: sq.correlation.childField,
433
+ keyField: childKey,
434
+ fn,
435
+ field: sq.aggregate.field,
436
+ path: sq.aggregate.path,
437
+ predicate: wherePredicate
438
+ } : void 0);
439
+ } else source = delegate.getSource(aggTable);
440
+ if (!source) throw new Error(`Aggregate source not found: ${aggTable}`);
441
+ const conn = source.connect(childKey.map((f) => [f, "asc"]));
442
+ childInput = delegate.decorateSourceInput(conn, queryID);
443
+ childInput = delegate.decorateInput(childInput, `${name}:aggregateSource(${sq.subquery.alias})`);
444
+ } else {
445
+ const child = buildPipelineInternal(sq.subquery, delegate, queryID, childPipelineName(name, sq), sq.correlation.childField, fromCondition);
446
+ const aggName = `${name}:aggregate(${sq.subquery.alias})`;
447
+ let aggInput = child;
448
+ let aggField = sq.aggregate.field;
449
+ let aggPath = sq.aggregate.path;
450
+ if (sq.subquery.related && sq.subquery.related.length > 0) {
451
+ const destAlias = must(sq.subquery.related[0].subquery.alias);
452
+ const lift = new LiftField(child, destAlias, must(sq.aggregate.field), aggPath === void 0 ? void 0 : sq.aggregate);
453
+ delegate.addEdge(child, lift);
454
+ aggInput = lift;
455
+ aggField = LIFTED_FIELD_COLUMN;
456
+ aggPath = void 0;
457
+ }
458
+ const aggregate = new Aggregate(aggInput, delegate.createStorage(aggName), sq.correlation.childField, sq.aggregate.fn, aggField, aggTable, void 0, aggPath);
459
+ delegate.addEdge(aggInput, aggregate);
460
+ childInput = delegate.decorateInput(aggregate, aggName);
461
+ }
462
+ } else childInput = buildPipelineInternal(sq.subquery, delegate, queryID, childPipelineName(name, sq), sq.correlation.childField, fromCondition);
345
463
  const joinName = `${name}:join(${sq.subquery.alias})`;
346
464
  const join = new Join({
347
465
  parent: end,
348
- child,
466
+ child: childInput,
349
467
  parentKey: sq.correlation.parentField,
350
- childKey: sq.correlation.childField,
468
+ childKey,
351
469
  relationshipName: sq.subquery.alias,
352
470
  hidden: sq.hidden ?? false,
353
471
  system: sq.system ?? "client",
@@ -356,7 +474,7 @@ function applyCorrelatedSubQuery(sq, delegate, queryID, end, name, fromCondition
356
474
  exists
357
475
  });
358
476
  delegate.addEdge(end, join);
359
- delegate.addEdge(child, join);
477
+ delegate.addEdge(childInput, join);
360
478
  return delegate.decorateInput(join, joinName);
361
479
  }
362
480
  function applyCorrelatedSubqueryCondition(input, condition, delegate, name) {
@@ -391,6 +509,158 @@ function gatherCorrelatedSubqueryQueryConditions(condition) {
391
509
  if (condition) gather(condition);
392
510
  return csqs;
393
511
  }
512
+ /** `columns` with each of `from` renamed to the name at its index in `to`. */
513
+ function renameColumns(columns, from, to) {
514
+ const renamed = {};
515
+ for (const [name, schema] of Object.entries(columns)) {
516
+ const i = from.indexOf(name);
517
+ assignProperty(renamed, i === -1 ? name : to[i], schema);
518
+ }
519
+ return renamed;
520
+ }
521
+ /**
522
+ * Deterministic synthetic table name for an `aggregate` relationship. Must
523
+ * agree between the server (which produces the rows) and the synced client
524
+ * (which reads them). It folds in the `queryID` — the client query hash, which
525
+ * both sides share and which already distinguishes differently-filtered queries
526
+ * — and the relationship's alias path from the root, so two aggregates never
527
+ * collide, even ones with the same alias at different depths.
528
+ *
529
+ * @param parentName The pipeline name of the parent, which the builder forms
530
+ * from the aliases of the relationships leading to it (`''` at the
531
+ * root, `'.comments'` one level down). It is the same on both sides.
532
+ */
533
+ function aggregateTableName(queryID, sq, parentName = "") {
534
+ const path = childPipelineName(parentName, sq).slice(1);
535
+ return `${AGGREGATE_TABLE_PREFIX}${queryID}:${path}`;
536
+ }
537
+ /**
538
+ * The pipeline name of the child `sq` under the pipeline `name`: the parent's
539
+ * name, a `.`, and the escaped alias. A relationship name can be any string, so
540
+ * the alias is escaped to keep the `.`-joined path unambiguous and free of the
541
+ * `/` and `:` that {@link aggregateTableName} must not contain.
542
+ */
543
+ function childPipelineName(name, sq) {
544
+ return `${name}.${encodeURIComponent(sq.subquery.alias ?? "").replaceAll(".", "%2E")}`;
545
+ }
546
+ /**
547
+ * Deterministic synthetic table name for a *top-level* (ungrouped) aggregate
548
+ * query (e.g. `z.query.issue.count()`). There is no relationship alias, so the
549
+ * name is just the prefix + `queryID` (`aggregate:<queryID>`). Distinct from
550
+ * relationship-aggregate names (`aggregate:<queryID>:<alias>`), which always
551
+ * carry an alias segment — which is how the client tells the two apart by name.
552
+ */
553
+ function topLevelAggregateTableName(queryID) {
554
+ return `${AGGREGATE_TABLE_PREFIX}${queryID}`;
555
+ }
556
+ /**
557
+ * The synthetic table of a grouped query's rows (a `distinct()` or a
558
+ * `groupBy()`): `aggregate:<queryID>:` and a `#`, which an escaped
559
+ * relationship alias never contains, so it can't be a relationship
560
+ * aggregate's, and isn't a top-level aggregate's either, whose shape the
561
+ * client derives from the name.
562
+ */
563
+ function groupTableName(queryID) {
564
+ return `${AGGREGATE_TABLE_PREFIX}${queryID}${GROUP_TABLE_SUFFIX}`;
565
+ }
566
+ var GROUP_TABLE_SUFFIX = `:#group`;
567
+ /** What a grouped query reduces each group to: its aggregate, or a count. */
568
+ function reducerOf(ast) {
569
+ const { aggregate } = ast;
570
+ return aggregate ? new Reducer(aggregate.fn, aggregate.field, aggregate.path) : new Reducer("count", void 0, void 0);
571
+ }
572
+ /**
573
+ * The names a grouped query's input rows have `columns` under: the query's
574
+ * own, or, for a synced client reading them from the synthetic source, the
575
+ * server's.
576
+ */
577
+ function inputNames(ast, delegate, columns) {
578
+ return delegate.aggregatesFromSource && delegate.aggregateKeyColumns ? delegate.aggregateKeyColumns(ast.table, columns) : columns;
579
+ }
580
+ /**
581
+ * A synced client reads a grouped query's rows from the synthetic source the
582
+ * server streams them to; the table's rows are not synced for it.
583
+ */
584
+ function groupFromSource(ast, delegate, queryID, name) {
585
+ const tableSource = must(delegate.getSource(ast.table), `Source not found: ${ast.table}`);
586
+ const reducer = reducerOf(ast);
587
+ const columns = [...must(ast.groupBy)];
588
+ if (reducer.field !== void 0 && !columns.includes(reducer.field)) columns.push(reducer.field);
589
+ const names = inputNames(ast, delegate, columns);
590
+ const inputName = (col) => names[columns.indexOf(col)];
591
+ const inputColumns = {};
592
+ for (const col of columns) assignProperty(inputColumns, inputName(col), tableSource.tableSchema.columns[col]);
593
+ const { columns: sourceColumns, primaryKey, sort } = groupSourceSchema(inputColumns, groupOrdering(ast).map(([col, dir]) => [inputName(col), dir]), {
594
+ fn: reducer.fn,
595
+ field: reducer.field === void 0 ? void 0 : inputName(reducer.field),
596
+ path: reducer.path
597
+ });
598
+ const table = groupTableName(queryID);
599
+ const source = delegate.getAggregateSource ? delegate.getAggregateSource(table, sourceColumns, primaryKey, groupOptimisticDelta(ast, reducer, inputName)) : delegate.getSource(table);
600
+ if (!source) throw new Error(`Group source not found: ${table}`);
601
+ return delegate.decorateInput(delegate.decorateSourceInput(source.connect(sort), queryID), `${name}:groupSource`);
602
+ }
603
+ /**
604
+ * What the synced client needs to move a grouped query's rows when it mutates
605
+ * a row of its table, so that a mutator reads its own writes and a view shows
606
+ * them before the server answers. As for a relationship aggregate, only for
607
+ * the invertible functions, and only when the `where` can be judged from the
608
+ * row alone: otherwise the rows are server-authoritative.
609
+ */
610
+ function groupOptimisticDelta(ast, reducer, inputName) {
611
+ if (!isInvertible(reducer.fn)) return;
612
+ let predicate;
613
+ if (ast.where !== void 0) {
614
+ const { filters, conditionsRemoved } = transformFilters(ast.where);
615
+ if (conditionsRemoved || filters === void 0) return;
616
+ predicate = createPredicate(filters);
617
+ }
618
+ const columns = groupOrdering(ast).map(([col]) => col);
619
+ return {
620
+ table: ast.table,
621
+ childField: columns,
622
+ keyField: columns.map(inputName),
623
+ fn: reducer.fn,
624
+ field: reducer.field,
625
+ path: ast.aggregate?.path,
626
+ predicate,
627
+ grouped: true
628
+ };
629
+ }
630
+ /**
631
+ * The input of a view of a grouped query: its rows reduced to the group
632
+ * columns, and for a `groupBy()` its value under the aggregate function's
633
+ * name, under the names the view shows a row's columns by, those of the
634
+ * pipeline's AST (see {@link GroupProjection}).
635
+ */
636
+ function groupViewInput(input, ast, delegate) {
637
+ const viewNames = must((delegate.mapAst ? delegate.mapAst(ast) : ast).groupBy);
638
+ const names = delegate.aggregatesFromSource ? inputNames(ast, delegate, must(ast.groupBy)) : viewNames;
639
+ return new GroupProjection(input, viewNames.map((col, i) => [col, names[i]]), ast.aggregate?.fn);
640
+ }
641
+ /** True if `name` is a synthetic aggregate table (any kind). */
642
+ function isAggregateTableName(name) {
643
+ return name.startsWith(AGGREGATE_TABLE_PREFIX);
644
+ }
645
+ /**
646
+ * True if `name` is a *top-level* aggregate table (`aggregate:<queryID>`), as
647
+ * opposed to a relationship one (`aggregate:<queryID>:<alias>`). Top-level names
648
+ * have no further separator after the prefix. The client uses this to recognize
649
+ * a synthetic source it can provision from the name alone (fixed key + value
650
+ * shape), which relationship aggregates — needing the correlation key — cannot.
651
+ */
652
+ function isTopLevelAggregateTableName(name) {
653
+ return name.startsWith("aggregate:") && !name.slice(10).includes(":");
654
+ }
655
+ /**
656
+ * True if the rows of the synthetic table `name` are keyed by `_0_key`
657
+ * alone: a top-level aggregate's ({@link topLevelAggregateTableName}) or a
658
+ * grouped query's ({@link groupTableName}). A relationship aggregate's rows
659
+ * are keyed by their correlation columns, whatever they are named.
660
+ */
661
+ function isKeyedAggregateTableName(name) {
662
+ return isTopLevelAggregateTableName(name) || name.startsWith("aggregate:") && name.endsWith(GROUP_TABLE_SUFFIX);
663
+ }
394
664
  function uniquifyCorrelatedSubqueryConditionAliases(ast) {
395
665
  if (!ast.where) return ast;
396
666
  const { where } = ast;
@@ -434,6 +704,6 @@ function partitionBranches(conditions, predicate) {
434
704
  return [matched, notMatched];
435
705
  }
436
706
  //#endregion
437
- export { bindStaticParameters, buildPipeline, conditionIncludesFlippedSubqueryAtAnyLevel, groupSubqueryConditions, partitionBranches };
707
+ export { aggregateTableName, bindStaticParameters, buildPipeline, conditionIncludesFlippedSubqueryAtAnyLevel, groupSubqueryConditions, groupTableName, groupViewInput, isAggregateTableName, isKeyedAggregateTableName, isTopLevelAggregateTableName, partitionBranches, topLevelAggregateTableName };
438
708
 
439
709
  //# sourceMappingURL=builder.js.map