@rocicorp/zero 1.12.0-canary.4 → 1.12.0-canary.6

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 (164) hide show
  1. package/out/analyze-query/src/analyze-cli.d.ts.map +1 -1
  2. package/out/analyze-query/src/analyze-cli.js +2 -1
  3. package/out/analyze-query/src/analyze-cli.js.map +1 -1
  4. package/out/analyze-query/src/bin-transform.js +1 -1
  5. package/out/analyze-query/src/zql-chain.d.ts +23 -0
  6. package/out/analyze-query/src/zql-chain.d.ts.map +1 -0
  7. package/out/analyze-query/src/zql-chain.js +162 -0
  8. package/out/analyze-query/src/zql-chain.js.map +1 -0
  9. package/out/ast-to-zql/src/bin.js +1 -1
  10. package/out/replicache/src/btree/node.js +1 -1
  11. package/out/replicache/src/deleted-clients.d.ts.map +1 -1
  12. package/out/replicache/src/deleted-clients.js +2 -1
  13. package/out/replicache/src/deleted-clients.js.map +1 -1
  14. package/out/replicache/src/kv/sqlite-store.d.ts.map +1 -1
  15. package/out/replicache/src/kv/sqlite-store.js +7 -3
  16. package/out/replicache/src/kv/sqlite-store.js.map +1 -1
  17. package/out/replicache/src/replicache-impl.d.ts.map +1 -1
  18. package/out/replicache/src/replicache-impl.js +17 -14
  19. package/out/replicache/src/replicache-impl.js.map +1 -1
  20. package/out/z2s/src/compiler.js +3 -3
  21. package/out/z2s/src/compiler.js.map +1 -1
  22. package/out/zero/package.js +22 -23
  23. package/out/zero/package.js.map +1 -1
  24. package/out/zero-cache/src/auth/load-permissions.js +1 -1
  25. package/out/zero-cache/src/auth/write-authorizer.js +1 -1
  26. package/out/zero-cache/src/config/zero-config.js +2 -2
  27. package/out/zero-cache/src/config/zero-config.js.map +1 -1
  28. package/out/zero-cache/src/scripts/decommission.js +1 -1
  29. package/out/zero-cache/src/scripts/deploy-permissions.js +1 -1
  30. package/out/zero-cache/src/server/change-streamer.d.ts.map +1 -1
  31. package/out/zero-cache/src/server/change-streamer.js +33 -10
  32. package/out/zero-cache/src/server/change-streamer.js.map +1 -1
  33. package/out/zero-cache/src/server/replicator.js +1 -1
  34. package/out/zero-cache/src/server/syncer.js +3 -3
  35. package/out/zero-cache/src/services/analyze.js +2 -2
  36. package/out/zero-cache/src/services/change-source/change-source.d.ts +7 -0
  37. package/out/zero-cache/src/services/change-source/change-source.d.ts.map +1 -1
  38. package/out/zero-cache/src/services/change-source/common/backfill-manager.js +1 -1
  39. package/out/zero-cache/src/services/change-source/common/replica-restore.d.ts +25 -0
  40. package/out/zero-cache/src/services/change-source/common/replica-restore.d.ts.map +1 -1
  41. package/out/zero-cache/src/services/change-source/common/replica-restore.js.map +1 -1
  42. package/out/zero-cache/src/services/change-source/common/replica-schema.js +1 -1
  43. package/out/zero-cache/src/services/change-source/custom/change-source.js +1 -1
  44. package/out/zero-cache/src/services/change-source/pg/backfill-stream.js +1 -1
  45. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts +8 -1
  46. package/out/zero-cache/src/services/change-source/pg/change-source-init.d.ts.map +1 -1
  47. package/out/zero-cache/src/services/change-source/pg/change-source-init.js +35 -18
  48. package/out/zero-cache/src/services/change-source/pg/change-source-init.js.map +1 -1
  49. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts +12 -0
  50. package/out/zero-cache/src/services/change-source/pg/change-source.d.ts.map +1 -1
  51. package/out/zero-cache/src/services/change-source/pg/change-source.js +66 -9
  52. package/out/zero-cache/src/services/change-source/pg/change-source.js.map +1 -1
  53. package/out/zero-cache/src/services/change-source/pg/fork-request.d.ts +57 -0
  54. package/out/zero-cache/src/services/change-source/pg/fork-request.d.ts.map +1 -0
  55. package/out/zero-cache/src/services/change-source/pg/fork-request.js +54 -0
  56. package/out/zero-cache/src/services/change-source/pg/fork-request.js.map +1 -0
  57. package/out/zero-cache/src/services/change-source/pg/initial-sync.js +4 -4
  58. package/out/zero-cache/src/services/change-source/pg/logical-replication/stream.js +2 -2
  59. package/out/zero-cache/src/services/change-source/pg/replication-slots.d.ts.map +1 -1
  60. package/out/zero-cache/src/services/change-source/pg/replication-slots.js +6 -0
  61. package/out/zero-cache/src/services/change-source/pg/replication-slots.js.map +1 -1
  62. package/out/zero-cache/src/services/change-source/pg/schema/ddl.js +1 -1
  63. package/out/zero-cache/src/services/change-source/pg/schema/init.js +1 -1
  64. package/out/zero-cache/src/services/change-source/pg/schema/published.js +1 -1
  65. package/out/zero-cache/src/services/change-source/pg/schema/shard.js +1 -1
  66. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts +1 -0
  67. package/out/zero-cache/src/services/change-source/protocol/current/downstream.d.ts.map +1 -1
  68. package/out/zero-cache/src/services/change-source/protocol/current/status.d.ts +3 -0
  69. package/out/zero-cache/src/services/change-source/protocol/current/status.d.ts.map +1 -1
  70. package/out/zero-cache/src/services/change-source/protocol/current/status.js +2 -1
  71. package/out/zero-cache/src/services/change-source/protocol/current/status.js.map +1 -1
  72. package/out/zero-cache/src/services/change-source/protocol/current/upstream.d.ts +1 -0
  73. package/out/zero-cache/src/services/change-source/protocol/current/upstream.d.ts.map +1 -1
  74. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.d.ts +1 -3
  75. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.d.ts.map +1 -1
  76. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js +6 -6
  77. package/out/zero-cache/src/services/change-streamer/backup-cleanup-monitor-factory.js.map +1 -1
  78. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts +41 -2
  79. package/out/zero-cache/src/services/change-streamer/backup-monitor.d.ts.map +1 -1
  80. package/out/zero-cache/src/services/change-streamer/backup-monitor.js +55 -20
  81. package/out/zero-cache/src/services/change-streamer/backup-monitor.js.map +1 -1
  82. package/out/zero-cache/src/services/change-streamer/change-log-initializer.js +1 -1
  83. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts +9 -0
  84. package/out/zero-cache/src/services/change-streamer/change-streamer-service.d.ts.map +1 -1
  85. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js +27 -8
  86. package/out/zero-cache/src/services/change-streamer/change-streamer-service.js.map +1 -1
  87. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts +1 -12
  88. package/out/zero-cache/src/services/change-streamer/change-streamer.d.ts.map +1 -1
  89. package/out/zero-cache/src/services/change-streamer/change-streamer.js.map +1 -1
  90. package/out/zero-cache/src/services/change-streamer/litestream-sync-requester.d.ts +29 -2
  91. package/out/zero-cache/src/services/change-streamer/litestream-sync-requester.d.ts.map +1 -1
  92. package/out/zero-cache/src/services/change-streamer/litestream-sync-requester.js +80 -11
  93. package/out/zero-cache/src/services/change-streamer/litestream-sync-requester.js.map +1 -1
  94. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.d.ts +2 -2
  95. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.d.ts.map +1 -1
  96. package/out/zero-cache/src/services/change-streamer/litestream3-prometheus-poller.js.map +1 -1
  97. package/out/zero-cache/src/services/change-streamer/replica-poller.d.ts +2 -2
  98. package/out/zero-cache/src/services/change-streamer/replica-poller.d.ts.map +1 -1
  99. package/out/zero-cache/src/services/change-streamer/replica-poller.js +1 -1
  100. package/out/zero-cache/src/services/change-streamer/replica-poller.js.map +1 -1
  101. package/out/zero-cache/src/services/change-streamer/server-start-metrics.d.ts +33 -0
  102. package/out/zero-cache/src/services/change-streamer/server-start-metrics.d.ts.map +1 -0
  103. package/out/zero-cache/src/services/change-streamer/server-start-metrics.js +42 -0
  104. package/out/zero-cache/src/services/change-streamer/server-start-metrics.js.map +1 -0
  105. package/out/zero-cache/src/services/change-streamer/sqlite-change-log-writer.js +2 -2
  106. package/out/zero-cache/src/services/change-streamer/storer.js +1 -1
  107. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts +17 -6
  108. package/out/zero-cache/src/services/change-streamer/subscriber.d.ts.map +1 -1
  109. package/out/zero-cache/src/services/change-streamer/subscriber.js +5 -4
  110. package/out/zero-cache/src/services/change-streamer/subscriber.js.map +1 -1
  111. package/out/zero-cache/src/services/life-cycle.js +1 -1
  112. package/out/zero-cache/src/services/litestream/commands.js +2 -2
  113. package/out/zero-cache/src/services/plannerz.js +2 -2
  114. package/out/zero-cache/src/services/replicator/change-log-db.js +1 -1
  115. package/out/zero-cache/src/services/replicator/incremental-sync.js +3 -3
  116. package/out/zero-cache/src/services/replicator/schema/backfilling.js +1 -1
  117. package/out/zero-cache/src/services/replicator/write-worker.js +3 -3
  118. package/out/zero-cache/src/services/run-ast.js +1 -1
  119. package/out/zero-cache/src/services/statz.js +2 -2
  120. package/out/zero-cache/src/services/view-syncer/cvr-store.js +1 -1
  121. package/out/zero-cache/src/services/view-syncer/inspect-handler.js +1 -1
  122. package/out/zero-cache/src/services/view-syncer/pipeline-driver.js +5 -5
  123. package/out/zero-cache/src/services/view-syncer/row-record-cache.js +1 -1
  124. package/out/zero-cache/src/services/view-syncer/snapshotter.js +1 -1
  125. package/out/zero-cache/src/services/view-syncer/view-syncer.js +6 -6
  126. package/out/zero-cache/src/types/profiler.js +1 -1
  127. package/out/zero-client/src/client/version.js +1 -1
  128. package/out/zero-protocol/src/ast.d.ts.map +1 -1
  129. package/out/zero-protocol/src/ast.js +2 -1
  130. package/out/zero-protocol/src/ast.js.map +1 -1
  131. package/out/zero-protocol/src/down.d.ts +27 -27
  132. package/out/zql/src/ivm/cap.d.ts +1 -1
  133. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  134. package/out/zql/src/ivm/cap.js +10 -0
  135. package/out/zql/src/ivm/cap.js.map +1 -1
  136. package/out/zql/src/ivm/data.d.ts +15 -1
  137. package/out/zql/src/ivm/data.d.ts.map +1 -1
  138. package/out/zql/src/ivm/data.js +15 -1
  139. package/out/zql/src/ivm/data.js.map +1 -1
  140. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  141. package/out/zql/src/ivm/exists.js +12 -4
  142. package/out/zql/src/ivm/exists.js.map +1 -1
  143. package/out/zql/src/ivm/filter-operators.d.ts +5 -0
  144. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  145. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  146. package/out/zql/src/ivm/join.d.ts.map +1 -1
  147. package/out/zql/src/ivm/join.js +6 -3
  148. package/out/zql/src/ivm/join.js.map +1 -1
  149. package/out/zql/src/ivm/memory-source.js +1 -1
  150. package/out/zql/src/ivm/operator.d.ts +6 -0
  151. package/out/zql/src/ivm/operator.d.ts.map +1 -1
  152. package/out/zql/src/ivm/operator.js.map +1 -1
  153. package/out/zql/src/ivm/take.d.ts.map +1 -1
  154. package/out/zql/src/ivm/take.js +4 -3
  155. package/out/zql/src/ivm/take.js.map +1 -1
  156. package/out/zqlite/src/table-source.js +1 -1
  157. package/package.json +22 -23
  158. package/out/analyze-query/src/bin-analyze.d.ts +0 -2
  159. package/out/analyze-query/src/bin-analyze.d.ts.map +0 -1
  160. package/out/analyze-query/src/bin-analyze.js +0 -285
  161. package/out/analyze-query/src/bin-analyze.js.map +0 -1
  162. package/out/zero/src/analyze-query.d.ts +0 -3
  163. package/out/zero/src/analyze-query.d.ts.map +0 -1
  164. package/out/zero/src/analyze-query.js +0 -3
@@ -1 +1 @@
1
- {"version":3,"file":"exists.js","names":[],"sources":["../../../../../zql/src/ivm/exists.ts"],"sourcesContent":["import {areEqual} from '../../../shared/src/arrays.ts';\nimport {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeAddChange, makeRemoveChange, type Change} from './change.ts';\nimport {normalizeUndefined, type Node, type NormalizedValue} from './data.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport type {InputBase} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The Exists operator filters data based on whether or not a relationship is\n * non-empty.\n */\nexport class Exists implements FilterOperator {\n readonly #input: FilterInput;\n readonly #relationshipName: string;\n readonly #not: boolean;\n readonly #parentJoinKey: CompoundKey;\n readonly #noSizeReuse: boolean;\n #cache: Map<string, boolean>;\n #cacheHitCountsForTesting: Map<string, number> | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n /**\n * This instance variable is `true` when this operator is processing a `push`,\n * and is used to disable reuse of cached sizes across rows with the\n * same parent join key value.\n * This is necessary because during a push relationships can be inconsistent\n * due to push communicating changes (which may change multiple Nodes) one\n * Node at a time.\n */\n #inPush = false;\n\n constructor(\n input: FilterInput,\n relationshipName: string,\n parentJoinKey: CompoundKey,\n type: 'EXISTS' | 'NOT EXISTS',\n cacheHitCountsForTesting?: Map<string, number>,\n ) {\n this.#input = input;\n this.#relationshipName = relationshipName;\n this.#input.setFilterOutput(this);\n this.#cache = new Map();\n this.#cacheHitCountsForTesting = cacheHitCountsForTesting;\n assert(\n this.#input.getSchema().relationships[relationshipName],\n // log-leak-ignore -- relationship name is schema, allowed in errors\n `Input schema missing ${relationshipName}`,\n );\n this.#not = type === 'NOT EXISTS';\n this.#parentJoinKey = parentJoinKey;\n\n // If the parentJoinKey is the primary key, no sense in trying to reuse.\n this.#noSizeReuse = areEqual(\n parentJoinKey,\n this.#input.getSchema().primaryKey,\n );\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#output = output;\n }\n\n beginFilter() {\n this.#output.beginFilter();\n }\n\n endFilter() {\n this.#cache = new Map();\n this.#output.endFilter();\n }\n\n *filter(node: Node): IterableIterator<'yield', boolean> {\n let exists: boolean | undefined;\n if (!this.#noSizeReuse && !this.#inPush) {\n const key = this.#getCacheKey(node, this.#parentJoinKey);\n exists = this.#cache.get(key);\n if (exists === undefined) {\n exists = yield* this.#fetchExists(node);\n this.#cache.set(key, exists);\n } else if (this.#cacheHitCountsForTesting) {\n this.#cacheHitCountsForTesting.set(\n key,\n (this.#cacheHitCountsForTesting.get(key) ?? 0) + 1,\n );\n }\n }\n\n const result =\n (yield* this.#filter(node, exists)) && (yield* this.#output.filter(node));\n return result;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *push(change: Change): Stream<'yield'> {\n assert(!this.#inPush, 'Unexpected re-entrancy');\n this.#inPush = true;\n try {\n switch (change[ChangeIndex.TYPE]) {\n // add, remove and edit cannot change the size of the\n // this.#relationshipName relationship, so simply #pushWithFilter\n case ChangeType.ADD:\n case ChangeType.EDIT:\n case ChangeType.REMOVE: {\n yield* this.#pushWithFilter(change);\n return;\n }\n case ChangeType.CHILD:\n // Only add and remove child changes for the\n // this.#relationshipName relationship, can change the size\n // of the this.#relationshipName relationship, for other\n // child changes simply #pushWithFilter\n if (\n change[ChangeIndex.CHILD_DATA].relationshipName !==\n this.#relationshipName ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.EDIT ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.CHILD\n ) {\n yield* this.#pushWithFilter(change);\n return;\n }\n switch (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 1) {\n if (this.#not) {\n // Since the add child change currently being processed is not\n // pushed to output, the added child needs to be excluded from\n // the remove being pushed to output (since the child has\n // never been added to the output).\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [],\n },\n }),\n this,\n );\n } else {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n case ChangeType.REMOVE: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 0) {\n if (this.#not) {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n } else {\n // Since the remove child change currently being processed is\n // not pushed to output, the removed child needs to be added to\n // the remove being pushed to output.\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [\n change[ChangeIndex.CHILD_DATA].change[\n ChangeIndex.NODE\n ],\n ],\n },\n }),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n }\n return;\n default:\n unreachable(change);\n }\n } finally {\n this.#inPush = false;\n }\n }\n\n /**\n * Returns whether or not the node's this.#relationshipName\n * relationship passes the exist/not exists filter condition.\n * If the optional `size` is passed it is used.\n * Otherwise, if there is a stored size for the row it is used.\n * Otherwise the size is computed by streaming the node's\n * relationship with this.#relationshipName (this computed size is also\n * stored).\n */\n *#filter(node: Node, exists?: boolean): IterableIterator<'yield', boolean> {\n exists = exists ?? (yield* this.#fetchExists(node));\n return this.#not ? !exists : exists;\n }\n\n #getCacheKey(node: Node, def: CompoundKey): string {\n const values: NormalizedValue[] = [];\n for (const key of def) {\n values.push(normalizeUndefined(node.row[key]));\n }\n return JSON.stringify(values);\n }\n\n /**\n * Pushes a change if this.#filter is true for its row.\n */\n *#pushWithFilter(change: Change, exists?: boolean): Stream<'yield'> {\n if (yield* this.#filter(change[ChangeIndex.NODE], exists)) {\n yield* this.#output.push(change, this);\n }\n }\n\n *#fetchExists(node: Node): IterableIterator<'yield', boolean> {\n // While it seems like this should be able to fetch just 1 node\n // to check for exists, we can't because Take does not support\n // early return during initial fetch.\n return (yield* this.#fetchSize(node)) > 0;\n }\n\n *#fetchSize(node: Node): IterableIterator<'yield', number> {\n const relationship = node.relationships[this.#relationshipName];\n assert(\n relationship,\n () =>\n `Exists: relationship \"${this.#relationshipName}\" not found on node`,\n );\n let size = 0;\n for (const n of relation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AA0B,CAC9B,OAAO,EAAuB,CAAC,OAC7B,EAEJ;SACF;QACF,CAAC,GACD,IACF;OAEJ,OACE,OAAO,KAAK,gBAAgB,QAAQ,OAAO,CAAC;OAE9C;MACF;KACF;KACA;IACF,SACE,YAAY,MAAM;GACtB;EACF,UAAU;GACR,KAAK,UAAU;EACjB;CACF;;;;;;;;;;CAWA,CAAC,QAAQ,MAAY,QAAsD;EACzE,SAAS,WAAW,OAAO,KAAK,aAAa,IAAI;EACjD,OAAO,KAAK,OAAO,CAAC,SAAS;CAC/B;CAEA,aAAa,MAAY,KAA0B;EACjD,MAAM,SAA4B,CAAC;EACnC,KAAK,MAAM,OAAO,KAChB,OAAO,KAAK,mBAAmB,KAAK,IAAI,IAAI,CAAC;EAE/C,OAAO,KAAK,UAAU,MAAM;CAC9B;;;;CAKA,CAAC,gBAAgB,QAAgB,QAAmC;EAClE,IAAI,OAAO,KAAK,QAAQ,OAAO,IAAmB,MAAM,GACtD,OAAO,KAAK,QAAQ,KAAK,QAAQ,IAAI;CAEzC;CAEA,CAAC,aAAa,MAAgD;EAI5D,QAAQ,OAAO,KAAK,WAAW,IAAI,KAAK;CAC1C;CAEA,CAAC,WAAW,MAA+C;EACzD,MAAM,eAAe,KAAK,cAAc,KAAK;EAC7C,OACE,oBAEE,yBAAyB,KAAK,kBAAkB,oBACpD;EACA,IAAI,OAAO;EACX,KAAK,MAAM,KAAK,aAAa,GAC3B,IAAI,MAAM,SACR,MAAM;OAEN;EAGJ,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"exists.js","names":[],"sources":["../../../../../zql/src/ivm/exists.ts"],"sourcesContent":["import {areEqual} from '../../../shared/src/arrays.ts';\nimport {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport type {CompoundKey} from '../../../zero-protocol/src/ast.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {makeAddChange, makeRemoveChange, type Change} from './change.ts';\nimport {\n normalizeUndefined,\n valueToKey,\n type Node,\n type NormalizedValue,\n} from './data.ts';\nimport {\n throwFilterOutput,\n type FilterInput,\n type FilterOperator,\n type FilterOutput,\n} from './filter-operators.ts';\nimport type {InputBase} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The Exists operator filters data based on whether or not a relationship is\n * non-empty.\n */\nexport class Exists implements FilterOperator {\n readonly #input: FilterInput;\n readonly #relationshipName: string;\n readonly #not: boolean;\n readonly #parentJoinKey: CompoundKey;\n readonly #noSizeReuse: boolean;\n #cache: Map<string, boolean>;\n #cacheHitCountsForTesting: Map<string, number> | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n /**\n * This instance variable is `true` when this operator is processing a `push`,\n * and is used to disable reuse of cached sizes across rows with the\n * same parent join key value.\n * This is necessary because during a push relationships can be inconsistent\n * due to push communicating changes (which may change multiple Nodes) one\n * Node at a time.\n */\n #inPush = false;\n\n constructor(\n input: FilterInput,\n relationshipName: string,\n parentJoinKey: CompoundKey,\n type: 'EXISTS' | 'NOT EXISTS',\n cacheHitCountsForTesting?: Map<string, number>,\n ) {\n this.#input = input;\n this.#relationshipName = relationshipName;\n this.#input.setFilterOutput(this);\n this.#cache = new Map();\n this.#cacheHitCountsForTesting = cacheHitCountsForTesting;\n assert(\n this.#input.getSchema().relationships[relationshipName],\n // log-leak-ignore -- relationship name is schema, allowed in errors\n `Input schema missing ${relationshipName}`,\n );\n this.#not = type === 'NOT EXISTS';\n this.#parentJoinKey = parentJoinKey;\n\n // If the parentJoinKey is the primary key, no sense in trying to reuse.\n this.#noSizeReuse = areEqual(\n parentJoinKey,\n this.#input.getSchema().primaryKey,\n );\n }\n\n setFilterOutput(output: FilterOutput): void {\n this.#output = output;\n }\n\n beginFilter() {\n this.#output.beginFilter();\n }\n\n endFilter() {\n this.#cache = new Map();\n this.#output.endFilter();\n }\n\n *filter(node: Node): IterableIterator<'yield', boolean> {\n let exists: boolean | undefined;\n if (!this.#noSizeReuse && !this.#inPush) {\n const key = this.#getCacheKey(node, this.#parentJoinKey);\n exists = this.#cache.get(key);\n if (exists === undefined) {\n exists = yield* this.#fetchExists(node);\n this.#cache.set(key, exists);\n } else if (this.#cacheHitCountsForTesting) {\n this.#cacheHitCountsForTesting.set(\n key,\n (this.#cacheHitCountsForTesting.get(key) ?? 0) + 1,\n );\n }\n }\n\n const result =\n (yield* this.#filter(node, exists)) && (yield* this.#output.filter(node));\n return result;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *push(change: Change): Stream<'yield'> {\n assert(!this.#inPush, 'Unexpected re-entrancy');\n this.#inPush = true;\n try {\n switch (change[ChangeIndex.TYPE]) {\n // add, remove and edit cannot change the size of the\n // this.#relationshipName relationship, so simply #pushWithFilter\n case ChangeType.ADD:\n case ChangeType.EDIT:\n case ChangeType.REMOVE: {\n yield* this.#pushWithFilter(change);\n return;\n }\n case ChangeType.CHILD:\n // Only add and remove child changes for the\n // this.#relationshipName relationship, can change the size\n // of the this.#relationshipName relationship, for other\n // child changes simply #pushWithFilter\n if (\n change[ChangeIndex.CHILD_DATA].relationshipName !==\n this.#relationshipName ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.EDIT ||\n change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE] ===\n ChangeType.CHILD\n ) {\n yield* this.#pushWithFilter(change);\n return;\n }\n switch (change[ChangeIndex.CHILD_DATA].change[ChangeIndex.TYPE]) {\n case ChangeType.ADD: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 1) {\n if (this.#not) {\n // Since the add child change currently being processed is not\n // pushed to output, the added child needs to be excluded from\n // the remove being pushed to output (since the child has\n // never been added to the output).\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [],\n },\n }),\n this,\n );\n } else {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n case ChangeType.REMOVE: {\n const size = yield* this.#fetchSize(change[ChangeIndex.NODE]);\n if (size === 0) {\n if (this.#not) {\n yield* this.#output.push(\n makeAddChange(change[ChangeIndex.NODE]),\n this,\n );\n } else {\n // Since the remove child change currently being processed is\n // not pushed to output, the removed child needs to be added to\n // the remove being pushed to output.\n yield* this.#output.push(\n makeRemoveChange({\n row: change[ChangeIndex.NODE].row,\n relationships: {\n ...change[ChangeIndex.NODE].relationships,\n [this.#relationshipName]: () => [\n change[ChangeIndex.CHILD_DATA].change[\n ChangeIndex.NODE\n ],\n ],\n },\n }),\n this,\n );\n }\n } else {\n yield* this.#pushWithFilter(change, size > 0);\n }\n return;\n }\n }\n return;\n default:\n unreachable(change);\n }\n } finally {\n this.#inPush = false;\n }\n }\n\n /**\n * Returns whether or not the node's this.#relationshipName\n * relationship passes the exist/not exists filter condition.\n * If the optional `size` is passed it is used.\n * Otherwise, if there is a stored size for the row it is used.\n * Otherwise the size is computed by streaming the node's\n * relationship with this.#relationshipName (this computed size is also\n * stored).\n */\n *#filter(node: Node, exists?: boolean): IterableIterator<'yield', boolean> {\n exists = exists ?? (yield* this.#fetchExists(node));\n return this.#not ? !exists : exists;\n }\n\n #getCacheKey(node: Node, def: CompoundKey): string {\n if (def.length === 1) {\n return valueToKey(node.row[def[0]]);\n }\n const values: NormalizedValue[] = [];\n for (const key of def) {\n values.push(normalizeUndefined(node.row[key]));\n }\n return JSON.stringify(values);\n }\n\n /**\n * Pushes a change if this.#filter is true for its row.\n */\n *#pushWithFilter(change: Change, exists?: boolean): Stream<'yield'> {\n if (yield* this.#filter(change[ChangeIndex.NODE], exists)) {\n yield* this.#output.push(change, this);\n }\n }\n\n *#fetchExists(node: Node): IterableIterator<'yield', boolean> {\n // While it seems like this should be able to fetch just 1 node\n // to check for exists, we can't because Take does not support\n // early return during initial fetch.\n return (yield* this.#fetchSize(node)) > 0;\n }\n\n *#fetchSize(node: Node): IterableIterator<'yield', number> {\n const relationship = node.relationships[this.#relationshipName];\n assert(\n relationship,\n () =>\n `Exists: relationship \"${this.#relationshipName}\" not found on node`,\n );\n if (this.#inPush) {\n // Only the size is needed, which Cap can answer from its state.\n let size = 0;\n for (const n of relationship(true)) {\n if (n === 'yield') {\n yield 'yield';\n } else {\n size++;\n }\n }\n return size;\n }\n // During a fetch, keep the children read to count them and make the\n // node's relationship return them, so that the consumer of the node does\n // not fetch them again. The exists subquery is limited, so this holds at\n // most a few nodes. `node.relationships` is created per node by the Join.\n const children: Node[] = [];\n for (const n of relationship()) {\n if (n === 'yield') {\n yield 'yield';\n } else {\n children.push(n);\n }\n }\n node.relationships[this.#relationshipName] = () => children;\n return children.length;\n }\n}\n"],"mappings":";;;;;;;;;;AA0BA,IAAa,SAAb,MAA8C;CAC5C;CACA;CACA;CACA;CACA;CACA;CACA;CACA,UAAwB;;;;;;;;;CAUxB,UAAU;CAEV,YACE,OACA,kBACA,eACA,MACA,0BACA;EACA,KAAK,SAAS;EACd,KAAK,oBAAoB;EACzB,KAAK,OAAO,gBAAgB,IAAI;EAChC,KAAK,yBAAS,IAAI,IAAI;EACtB,KAAK,4BAA4B;EACjC,OACE,KAAK,OAAO,UAAU,CAAC,CAAC,cAAc,mBAEtC,wBAAwB,kBAC1B;EACA,KAAK,OAAO,SAAS;EACrB,KAAK,iBAAiB;EAGtB,KAAK,eAAe,SAClB,eACA,KAAK,OAAO,UAAU,CAAC,CAAC,UAC1B;CACF;CAEA,gBAAgB,QAA4B;EAC1C,KAAK,UAAU;CACjB;CAEA,cAAc;EACZ,KAAK,QAAQ,YAAY;CAC3B;CAEA,YAAY;EACV,KAAK,yBAAS,IAAI,IAAI;EACtB,KAAK,QAAQ,UAAU;CACzB;CAEA,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IAAI;CAEzC;CAEA,CAAC,aAAa,MAAgD;EAI5D,QAAQ,OAAO,KAAK,WAAW,IAAI,KAAK;CAC1C;CAEA,CAAC,WAAW,MAA+C;EACzD,MAAM,eAAe,KAAK,cAAc,KAAK;EAC7C,OACE,oBAEE,yBAAyB,KAAK,kBAAkB,oBACpD;EACA,IAAI,KAAK,SAAS;GAEhB,IAAI,OAAO;GACX,KAAK,MAAM,KAAK,aAAa,IAAI,GAC/B,IAAI,MAAM,SACR,MAAM;QAEN;GAGJ,OAAO;EACT;EAKA,MAAM,WAAmB,CAAC;EAC1B,KAAK,MAAM,KAAK,aAAa,GAC3B,IAAI,MAAM,SACR,MAAM;OAEN,SAAS,KAAK,CAAC;EAGnB,KAAK,cAAc,KAAK,2BAA2B;EACnD,OAAO,SAAS;CAClB;AACF"}
@@ -18,6 +18,11 @@ import { type Stream } from './stream.ts';
18
18
  * `Node` `row`s or `relationship`s, but they shouldn't, they should just
19
19
  * filter.
20
20
  *
21
+ * One exception: during a fetch, `Exists` replaces the relationship it read
22
+ * with a function returning the children it read, so that the consumer does
23
+ * not fetch them again. The relationship yields the same nodes, so other
24
+ * branches of a `FanOut`, which see the same node object, are not affected.
25
+ *
21
26
  * This `FilterOperator` abstraction enables much more efficient processing of
22
27
  * `fetch` for `where` clauses containing OR conditions.
23
28
  *
@@ -1 +1 @@
1
- {"version":3,"file":"filter-operators.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/filter-operators.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,eAAe,EAAC,MAAM,uBAAuB,CAAC;AAC3D,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,sBAAsB,CAAC;AAC9D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,EAAC,KAAK,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAC,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,EAAC,MAAM,eAAe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C,EAAE,mBAAmB,GAC9B,KAAK,CAQP"}
@@ -1 +1 @@
1
- {"version":3,"file":"filter-operators.js","names":[],"sources":["../../../../../zql/src/ivm/filter-operators.ts"],"sourcesContent":["import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts';\nimport type {BuilderDelegate} from '../builder/builder.ts';\nimport type {NoSubqueryCondition} from '../builder/filter.ts';\nimport type {Change} from './change.ts';\nimport {type Node} from './data.ts';\nimport type {FetchRequest, Input, InputBase, Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The `where` clause of a ZQL query is implemented using a sub-graph of\n * `FilterOperators`. This sub-graph starts with a `FilterStart` operator,\n * that adapts from the normal `Operator` `Output`, to the\n * `FilterOperator` `FilterInput`, and ends with a `FilterEnd` operator that\n * adapts from a `FilterOperator` `FilterOutput` to a normal `Operator` `Input`.\n * `FilterOperator`'s do not have `fetch` instead they have a\n * `filter(node: Node): boolean` method.\n * They also have `push` which is just like normal `Operator` push.\n * Not having a `fetch` means these `FilterOperator`'s cannot modify\n * `Node` `row`s or `relationship`s, but they shouldn't, they should just\n * filter.\n *\n * This `FilterOperator` abstraction enables much more efficient processing of\n * `fetch` for `where` clauses containing OR conditions.\n *\n * See https://github.com/rocicorp/mono/pull/4339\n */\n\nexport interface FilterInput extends InputBase {\n /** Tell the input where to send its output. */\n setFilterOutput(output: FilterOutput): void;\n}\n\nexport interface FilterOutput extends Output {\n // Lets the operator know that we're in a loop of filtering\n // nodes. E.g., so the operator can cache results for the\n // duration of the loop.\n beginFilter(): void;\n filter(node: Node): IterableIterator<'yield', boolean>;\n endFilter(): void;\n}\n\nexport interface FilterOperator extends FilterInput, FilterOutput {}\n\n/**\n * An implementation of FilterOutput that throws if push or filter is called.\n * It is used as the initial value for for an operator's output before it is\n * set.\n */\nexport const throwFilterOutput: FilterOutput = {\n // oxlint-disable-next-line require-yield\n *push(_change: Change): Stream<'yield'> {\n throw new Error('Output not set');\n },\n\n // oxlint-disable-next-line require-yield\n *filter(_node: Node): IterableIterator<'yield', boolean> {\n throw new Error('Output not set');\n },\n\n reconcile(): Stream<'yield'> {\n throw new Error('Output not set');\n },\n\n beginFilter() {},\n endFilter() {},\n};\n\nexport class FilterStart implements FilterInput, Output {\n readonly #input: Input;\n readonly #condition: NoSubqueryCondition | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n constructor(input: Input, condition?: NoSubqueryCondition) {\n this.#input = input;\n this.#condition = condition;\n input.setOutput(this);\n }\n\n setFilterOutput(output: FilterOutput) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return this.#output.push(change, this);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n const mergedFilter = mergeFilters(req.filter, this.#condition);\n const childReq =\n mergedFilter === req.filter ? req : {...req, filter: mergedFilter};\n this.#output.beginFilter();\n try {\n for (const node of this.#input.fetch(childReq)) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (yield* this.#output.filter(node)) {\n yield node;\n }\n }\n } finally {\n // finally is important if an exception is thrown or\n // if the stream is not fully consumed.\n this.#output.endFilter();\n }\n }\n}\n\nfunction mergeFilters(\n reqFilter: NoSubqueryCondition | undefined,\n ownCondition: NoSubqueryCondition | undefined,\n): NoSubqueryCondition | undefined {\n if (!ownCondition) {\n return reqFilter;\n }\n if (!reqFilter) {\n return ownCondition;\n }\n return {type: 'and', conditions: [reqFilter, ownCondition]};\n}\n\nexport class FilterEnd implements Input, FilterOutput {\n readonly #start: FilterStart;\n readonly #input: FilterInput;\n\n #output: Output = throwFilterOutput;\n\n constructor(start: FilterStart, input: FilterInput) {\n this.#start = start;\n this.#input = input;\n input.setFilterOutput(this);\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n return this.#start.fetch(req);\n }\n\n beginFilter() {}\n endFilter() {}\n\n filter(_node: Node) {\n return returnTrueEmptyIterator;\n }\n\n setOutput(output: Output) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return this.#output.push(change, this);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n}\n\nconst returnTrueEmptyIterator = makeEmptyIteratorWithReturn(true);\n\nexport function buildFilterPipeline(\n input: Input,\n delegate: BuilderDelegate,\n pipeline: (filterInput: FilterInput) => FilterInput,\n condition?: NoSubqueryCondition,\n): Input {\n const filterStart = new FilterStart(input, condition);\n delegate.addEdge(input, filterStart);\n const middle = pipeline(filterStart);\n delegate.addEdge(filterStart, middle);\n const filterEnd = new FilterEnd(filterStart, middle);\n delegate.addEdge(middle, filterEnd);\n return filterEnd;\n}\n"],"mappings":";;;;;;;;AAiDA,IAAa,oBAAkC;CAE7C,CAAC,KAAK,SAAkC;EACtC,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAGA,CAAC,OAAO,OAAiD;EACvD,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAEA,YAA6B;EAC3B,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAEA,cAAc,CAAC;CACf,YAAY,CAAC;AACf;AAEA,IAAa,cAAb,MAAwD;CACtD;CACA;CACA,UAAwB;CAExB,YAAY,OAAc,WAAiC;EACzD,KAAK,SAAS;EACd,KAAK,aAAa;EAClB,MAAM,UAAU,IAAI;CACtB;CAEA,gBAAgB,QAAsB;EACpC,KAAK,UAAU;CACjB;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,KAAK,QAAgB;EACnB,OAAO,KAAK,QAAQ,KAAK,QAAQ,IAAI;CACvC;CA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EA,IAAM,0BAA0B,4BAA4B,IAAI;AAEhE,SAAgB,oBACd,OACA,UACA,UACA,WACO;CACP,MAAM,cAAc,IAAI,YAAY,OAAO,SAAS;CACpD,SAAS,QAAQ,OAAO,WAAW;CACnC,MAAM,SAAS,SAAS,WAAW;CACnC,SAAS,QAAQ,aAAa,MAAM;CACpC,MAAM,YAAY,IAAI,UAAU,aAAa,MAAM;CACnD,SAAS,QAAQ,QAAQ,SAAS;CAClC,OAAO;AACT"}
1
+ {"version":3,"file":"filter-operators.js","names":[],"sources":["../../../../../zql/src/ivm/filter-operators.ts"],"sourcesContent":["import {makeEmptyIteratorWithReturn} from '../../../shared/src/iterables.ts';\nimport type {BuilderDelegate} from '../builder/builder.ts';\nimport type {NoSubqueryCondition} from '../builder/filter.ts';\nimport type {Change} from './change.ts';\nimport {type Node} from './data.ts';\nimport type {FetchRequest, Input, InputBase, Output} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\n\n/**\n * The `where` clause of a ZQL query is implemented using a sub-graph of\n * `FilterOperators`. This sub-graph starts with a `FilterStart` operator,\n * that adapts from the normal `Operator` `Output`, to the\n * `FilterOperator` `FilterInput`, and ends with a `FilterEnd` operator that\n * adapts from a `FilterOperator` `FilterOutput` to a normal `Operator` `Input`.\n * `FilterOperator`'s do not have `fetch` instead they have a\n * `filter(node: Node): boolean` method.\n * They also have `push` which is just like normal `Operator` push.\n * Not having a `fetch` means these `FilterOperator`'s cannot modify\n * `Node` `row`s or `relationship`s, but they shouldn't, they should just\n * filter.\n *\n * One exception: during a fetch, `Exists` replaces the relationship it read\n * with a function returning the children it read, so that the consumer does\n * not fetch them again. The relationship yields the same nodes, so other\n * branches of a `FanOut`, which see the same node object, are not affected.\n *\n * This `FilterOperator` abstraction enables much more efficient processing of\n * `fetch` for `where` clauses containing OR conditions.\n *\n * See https://github.com/rocicorp/mono/pull/4339\n */\n\nexport interface FilterInput extends InputBase {\n /** Tell the input where to send its output. */\n setFilterOutput(output: FilterOutput): void;\n}\n\nexport interface FilterOutput extends Output {\n // Lets the operator know that we're in a loop of filtering\n // nodes. E.g., so the operator can cache results for the\n // duration of the loop.\n beginFilter(): void;\n filter(node: Node): IterableIterator<'yield', boolean>;\n endFilter(): void;\n}\n\nexport interface FilterOperator extends FilterInput, FilterOutput {}\n\n/**\n * An implementation of FilterOutput that throws if push or filter is called.\n * It is used as the initial value for for an operator's output before it is\n * set.\n */\nexport const throwFilterOutput: FilterOutput = {\n // oxlint-disable-next-line require-yield\n *push(_change: Change): Stream<'yield'> {\n throw new Error('Output not set');\n },\n\n // oxlint-disable-next-line require-yield\n *filter(_node: Node): IterableIterator<'yield', boolean> {\n throw new Error('Output not set');\n },\n\n reconcile(): Stream<'yield'> {\n throw new Error('Output not set');\n },\n\n beginFilter() {},\n endFilter() {},\n};\n\nexport class FilterStart implements FilterInput, Output {\n readonly #input: Input;\n readonly #condition: NoSubqueryCondition | undefined;\n #output: FilterOutput = throwFilterOutput;\n\n constructor(input: Input, condition?: NoSubqueryCondition) {\n this.#input = input;\n this.#condition = condition;\n input.setOutput(this);\n }\n\n setFilterOutput(output: FilterOutput) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return this.#output.push(change, this);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n const mergedFilter = mergeFilters(req.filter, this.#condition);\n const childReq =\n mergedFilter === req.filter ? req : {...req, filter: mergedFilter};\n this.#output.beginFilter();\n try {\n for (const node of this.#input.fetch(childReq)) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n if (yield* this.#output.filter(node)) {\n yield node;\n }\n }\n } finally {\n // finally is important if an exception is thrown or\n // if the stream is not fully consumed.\n this.#output.endFilter();\n }\n }\n}\n\nfunction mergeFilters(\n reqFilter: NoSubqueryCondition | undefined,\n ownCondition: NoSubqueryCondition | undefined,\n): NoSubqueryCondition | undefined {\n if (!ownCondition) {\n return reqFilter;\n }\n if (!reqFilter) {\n return ownCondition;\n }\n return {type: 'and', conditions: [reqFilter, ownCondition]};\n}\n\nexport class FilterEnd implements Input, FilterOutput {\n readonly #start: FilterStart;\n readonly #input: FilterInput;\n\n #output: Output = throwFilterOutput;\n\n constructor(start: FilterStart, input: FilterInput) {\n this.#start = start;\n this.#input = input;\n input.setFilterOutput(this);\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n return this.#start.fetch(req);\n }\n\n beginFilter() {}\n endFilter() {}\n\n filter(_node: Node) {\n return returnTrueEmptyIterator;\n }\n\n setOutput(output: Output) {\n this.#output = output;\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n push(change: Change) {\n return this.#output.push(change, this);\n }\n\n *reconcile(_pusher: InputBase): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n}\n\nconst returnTrueEmptyIterator = makeEmptyIteratorWithReturn(true);\n\nexport function buildFilterPipeline(\n input: Input,\n delegate: BuilderDelegate,\n pipeline: (filterInput: FilterInput) => FilterInput,\n condition?: NoSubqueryCondition,\n): Input {\n const filterStart = new FilterStart(input, condition);\n delegate.addEdge(input, filterStart);\n const middle = pipeline(filterStart);\n delegate.addEdge(filterStart, middle);\n const filterEnd = new FilterEnd(filterStart, middle);\n delegate.addEdge(middle, filterEnd);\n return filterEnd;\n}\n"],"mappings":";;;;;;;;AAsDA,IAAa,oBAAkC;CAE7C,CAAC,KAAK,SAAkC;EACtC,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAGA,CAAC,OAAO,OAAiD;EACvD,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAEA,YAA6B;EAC3B,MAAM,IAAI,MAAM,gBAAgB;CAClC;CAEA,cAAc,CAAC;CACf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CAAC,CAShD;CA6QF"}
@@ -216,16 +216,19 @@ var Join = class {
216
216
  if (this.#indexesParents) this.#parentIndex.remove(row);
217
217
  }
218
218
  #processParentNode(parentNodeRow, parentNodeRelations) {
219
- const childStream = () => {
219
+ const childStream = (countOnly) => {
220
220
  const constraint = buildJoinConstraint(parentNodeRow, this.#parentKey, this.#childKey);
221
- const stream = constraint ? this.#child.fetch({ constraint }) : [];
222
221
  const inPushQueue = this.#isAfterInprogressPosition(parentNodeRow) && (this.#inprogressParentFetchBounds === void 0 || isInParentFetch(this.#inprogressParentFetchBounds, parentNodeRow, this.#schema.compareRows));
223
222
  if (this.#inprogressChildChange && isJoinMatch(parentNodeRow, this.#parentKey, this.#inprogressChildChange[1].row, this.#childKey) && inPushQueue) {
223
+ const stream = constraint ? this.#child.fetch({ constraint }) : [];
224
224
  const childSchema = this.#child.getSchema();
225
225
  if (childSchema.sort === void 0) return generateWithOverlayUnordered(stream, this.#inprogressChildChange, childSchema);
226
226
  return generateWithOverlay(stream, this.#inprogressChildChange, childSchema);
227
227
  }
228
- return stream;
228
+ return constraint ? this.#child.fetch(countOnly ? {
229
+ constraint,
230
+ countOnly
231
+ } : { constraint }) : [];
229
232
  };
230
233
  return {
231
234
  row: parentNodeRow,
@@ -1 +1 @@
1
- {"version":3,"file":"join.js","names":[],"sources":["../../../../../zql/src/ivm/join.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport type {CompoundKey, System} 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 {\n makeAddChange,\n makeChildChange,\n makeEditChange,\n makeRemoveChange,\n type Change,\n} from './change.ts';\nimport type {Node} from './data.ts';\nimport {JoinIndex} from './join-index.ts';\nimport {\n buildJoinConstraint,\n canonicalKey,\n generateWithOverlay,\n generateWithOverlayUnordered,\n isJoinMatch,\n rowEqualsForCompoundKey,\n} from './join-utils.ts';\nimport {mergeSortedStreams} from './memory-source.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type Output,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\nimport {\n isInParentFetch,\n readParentFetchBounds,\n type ParentFetchBound,\n type TakeBoundProvider,\n} from './take-gate.ts';\n\ntype Args = {\n parent: Input;\n child: Input;\n // The nth key in parentKey corresponds to the nth key in childKey.\n parentKey: CompoundKey;\n childKey: CompoundKey;\n relationshipName: string;\n hidden: boolean;\n system: System;\n parentPartitionKey?: CompoundKey | undefined;\n boundProvider?: TakeBoundProvider | undefined;\n /**\n * Set when this join feeds an EXISTS or NOT EXISTS condition. Its parent\n * index is then kept by a JoinIndexTap at the end of the pipeline, so it\n * only holds the parents the pipeline emits, not every parent this join\n * sees. A child change that misses the index is dropped unless it could\n * make a parent pass the condition: an add for EXISTS, a remove for NOT\n * EXISTS. Those still fetch the parents.\n */\n exists?: ExistsJoin | undefined;\n};\n\nexport type ExistsJoin = {\n readonly op: 'EXISTS' | 'NOT EXISTS';\n readonly parentIndex: JoinIndex;\n};\n\n/**\n * The Join operator joins the output from two upstream inputs. Zero's join\n * is a little different from SQL's join in that we output hierarchical data,\n * not a flat table. This makes it a lot more useful for UI programming and\n * avoids duplicating tons of data like left join would.\n *\n * The Nodes output from Join have a new relationship added to them, which has\n * the name #relationshipName. The value of the relationship is a stream of\n * child nodes which are the corresponding values from the child source.\n */\nexport class Join implements Input {\n readonly #parent: Input;\n readonly #child: Input;\n readonly #parentKey: CompoundKey;\n readonly #childKey: CompoundKey;\n readonly #relationshipName: string;\n readonly #schema: SourceSchema;\n readonly #parentIndex: JoinIndex;\n /**\n * Whether this join adds and removes parents in #parentIndex itself. Not\n * so for EXISTS joins, see Args.exists.\n */\n readonly #indexesParents: boolean;\n /**\n * The child change type that is still pushed to the parents when no\n * indexed parent has its join key. See Args.exists.\n */\n readonly #qualifyingChildChange: ChangeType | undefined;\n readonly #boundProvider: TakeBoundProvider | undefined;\n\n #output: Output = throwOutput;\n\n #inprogressChildChange: Change | undefined;\n #inprogressChildChangePosition: Row | undefined;\n /**\n * Primary keys of the parents #inprogressChildChange has reached so far,\n * kept only when the parent input is unordered. An unordered stream is not\n * in `compareRows` order (SQLite returns it in whatever order its plan\n * visits, e.g. rowid order), so whether a parent is still in the push queue\n * cannot be decided by comparing it to #inprogressChildChangePosition.\n */\n #inprogressReachedParents: Set<string> | undefined;\n #inprogressParentFetchBounds: ParentFetchBound[] | undefined;\n\n constructor({\n parent,\n child,\n parentKey,\n childKey,\n relationshipName,\n hidden,\n system,\n parentPartitionKey,\n boundProvider,\n exists,\n }: Args) {\n assert(parent !== child, 'Parent and child must be different operators');\n assert(\n parentKey.length === childKey.length,\n 'The parentKey and childKey keys must have same length',\n );\n assert(!exists || !parentPartitionKey, 'EXISTS joins are not partitioned');\n this.#parent = parent;\n this.#child = child;\n this.#parentKey = parentKey;\n this.#childKey = childKey;\n this.#relationshipName = relationshipName;\n if (exists) {\n this.#parentIndex = exists.parentIndex;\n this.#indexesParents = false;\n this.#qualifyingChildChange =\n exists.op === 'EXISTS' ? ChangeType.ADD : ChangeType.REMOVE;\n } else {\n this.#parentIndex = new JoinIndex(\n parentKey,\n parent.getSchema().primaryKey,\n parentPartitionKey,\n );\n this.#indexesParents = true;\n this.#qualifyingChildChange = undefined;\n }\n this.#boundProvider = boundProvider;\n\n const parentSchema = parent.getSchema();\n const childSchema = child.getSchema();\n this.#schema = {\n ...parentSchema,\n relationships: {\n ...parentSchema.relationships,\n [relationshipName]: {\n ...childSchema,\n isHidden: hidden,\n system,\n },\n },\n };\n\n parent.setOutput({\n push: (change: Change) => this.#pushParent(change),\n reconcile: () => this.#reconcile(),\n });\n child.setOutput({\n push: (change: Change) => this.#pushChild(change),\n reconcile: () => this.#reconcile(),\n });\n }\n\n *#reconcile(): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n destroy(): void {\n this.#parent.destroy();\n this.#child.destroy();\n }\n\n get parentIndexForTest(): JoinIndex {\n return this.#parentIndex;\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#schema;\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n for (const parentNode of this.#parent.fetch(req)) {\n if (parentNode === 'yield') {\n yield parentNode;\n continue;\n }\n this.#indexParentRow(parentNode.row);\n yield this.#processParentNode(parentNode.row, parentNode.relationships);\n }\n }\n\n *#pushParent(change: Change): Stream<'yield'> {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n this.#indexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeAddChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n ),\n this,\n );\n break;\n case ChangeType.REMOVE:\n this.#unindexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeRemoveChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n ),\n this,\n );\n break;\n case ChangeType.CHILD:\n yield* this.#output.push(\n makeChildChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n change[ChangeIndex.CHILD_DATA],\n ),\n this,\n );\n break;\n case ChangeType.EDIT: {\n // Assert the edit could not change the relationship.\n assert(\n rowEqualsForCompoundKey(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n this.#parentKey,\n ),\n `Parent edit must not change relationship.`,\n );\n this.#unindexParentRow(change[ChangeIndex.OLD_NODE].row);\n this.#indexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeEditChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n this.#processParentNode(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.OLD_NODE].relationships,\n ),\n ),\n this,\n );\n break;\n }\n default:\n unreachable(change);\n }\n }\n\n *#pushChild(change: Change): Stream<'yield'> {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n case ChangeType.REMOVE:\n yield* this.#pushChildChange(change[ChangeIndex.NODE].row, change);\n break;\n case ChangeType.CHILD:\n yield* this.#pushChildChange(change[ChangeIndex.NODE].row, change);\n break;\n case ChangeType.EDIT: {\n const childRow = change[ChangeIndex.NODE].row;\n const oldChildRow = change[ChangeIndex.OLD_NODE].row;\n // Assert the edit could not change the relationship.\n assert(\n rowEqualsForCompoundKey(oldChildRow, childRow, this.#childKey),\n 'Child edit must not change relationship.',\n );\n yield* this.#pushChildChange(childRow, change);\n break;\n }\n\n default:\n unreachable(change);\n }\n }\n\n *#pushChildChange(childRow: Row, change: Change): Stream<'yield'> {\n this.#inprogressChildChange = change;\n this.#inprogressChildChangePosition = undefined;\n this.#inprogressReachedParents =\n this.#parent.getSchema().sort === undefined ? new Set() : undefined;\n try {\n const constraint = buildJoinConstraint(\n childRow,\n this.#childKey,\n this.#parentKey,\n );\n if (constraint) {\n const partitions = this.#parentIndex.lookup(childRow, this.#childKey);\n let fetchConstraints: Record<string, Value>[];\n if (partitions) {\n fetchConstraints = partitions.map(partitionConstraint =>\n partitionConstraint\n ? {...constraint, ...partitionConstraint}\n : constraint,\n );\n } else if (change[ChangeIndex.TYPE] === this.#qualifyingChildChange) {\n fetchConstraints = [constraint];\n } else {\n return;\n }\n\n if (this.#boundProvider) {\n this.#inprogressParentFetchBounds = readParentFetchBounds(\n this.#boundProvider,\n fetchConstraints,\n );\n }\n\n let parentNodeStream: Stream<Node | 'yield'>;\n if (fetchConstraints.length === 1) {\n parentNodeStream = this.#parent.fetch({\n constraint: fetchConstraints[0],\n });\n } else {\n const streams = fetchConstraints.map(c =>\n this.#parent.fetch({constraint: c}),\n );\n const compare = (a: Node, b: Node) =>\n this.#schema.compareRows(a.row, b.row);\n parentNodeStream = mergeSortedStreams(streams, compare);\n }\n\n for (const parentNode of parentNodeStream) {\n if (parentNode === 'yield') {\n yield parentNode;\n continue;\n }\n this.#inprogressChildChangePosition = parentNode.row;\n this.#inprogressReachedParents?.add(\n canonicalKey(parentNode.row, this.#schema.primaryKey),\n );\n const childChange = makeChildChange(\n this.#processParentNode(parentNode.row, parentNode.relationships),\n {\n relationshipName: this.#relationshipName,\n change,\n },\n );\n yield* this.#output.push(childChange, this);\n }\n }\n } finally {\n this.#inprogressChildChange = undefined;\n this.#inprogressChildChangePosition = undefined;\n this.#inprogressReachedParents = undefined;\n this.#inprogressParentFetchBounds = undefined;\n }\n }\n\n /**\n * Whether the in-progress child change has yet to reach `parentNodeRow`,\n * i.e. the row comes after #inprogressChildChangePosition in the parent\n * stream.\n */\n #isAfterInprogressPosition(parentNodeRow: Row): boolean {\n if (this.#inprogressChildChangePosition === undefined) {\n return false;\n }\n if (this.#inprogressReachedParents) {\n return !this.#inprogressReachedParents.has(\n canonicalKey(parentNodeRow, this.#schema.primaryKey),\n );\n }\n return (\n this.#schema.compareRows(\n parentNodeRow,\n this.#inprogressChildChangePosition,\n ) > 0\n );\n }\n\n #indexParentRow(row: Row): void {\n if (this.#indexesParents) {\n this.#parentIndex.add(row);\n }\n }\n\n #unindexParentRow(row: Row): void {\n if (this.#indexesParents) {\n this.#parentIndex.remove(row);\n }\n }\n\n #processParentNode(\n parentNodeRow: Row,\n parentNodeRelations: Record<string, () => Stream<Node | 'yield'>>,\n ): Node {\n const childStream = () => {\n const constraint = buildJoinConstraint(\n parentNodeRow,\n this.#parentKey,\n this.#childKey,\n );\n const stream = constraint ? this.#child.fetch({constraint}) : [];\n\n // The parent has yet to get the in-progress child change if it comes\n // after the current position and a parent fetch of the push yields it.\n // With a TakeGate the fetches are capped at the bounds read when they\n // started.\n const inPushQueue =\n this.#isAfterInprogressPosition(parentNodeRow) &&\n (this.#inprogressParentFetchBounds === undefined ||\n isInParentFetch(\n this.#inprogressParentFetchBounds,\n parentNodeRow,\n this.#schema.compareRows,\n ));\n\n if (\n this.#inprogressChildChange &&\n isJoinMatch(\n parentNodeRow,\n this.#parentKey,\n this.#inprogressChildChange[ChangeIndex.NODE].row,\n this.#childKey,\n ) &&\n inPushQueue\n ) {\n const childSchema = this.#child.getSchema();\n if (childSchema.sort === undefined) {\n return generateWithOverlayUnordered(\n stream,\n this.#inprogressChildChange,\n childSchema,\n );\n }\n return generateWithOverlay(\n stream,\n this.#inprogressChildChange,\n childSchema,\n );\n }\n return stream;\n };\n\n return {\n row: parentNodeRow,\n relationships: {\n ...parentNodeRelations,\n [this.#relationshipName]: childStream,\n },\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2EA,IAAa,OAAb,MAAmC;CACjC;CACA;CACA;CACA;CACA;CACA;CACA;;;;;CAKA;;;;;CAKA;CACA;CAEA,UAAkB;CAElB;CACA;;;;;;;;CAQA;CACA;CAEA,YAAY,EACV,QACA,OACA,WACA,UACA,kBACA,QACA,QACA,oBACA,eACA,UACO;EACP,OAAO,WAAW,OAAO,8CAA8C;EACvE,OACE,UAAU,WAAW,SAAS,QAC9B,uDACF;EACA,OAAO,CAAC,UAAU,CAAC,oBAAoB,kCAAkC;EACzE,KAAK,UAAU;EACf,KAAK,SAAS;EACd,KAAK,aAAa;EAClB,KAAK,YAAY;EACjB,KAAK,oBAAoB;EACzB,IAAI,QAAQ;GACV,KAAK,eAAe,OAAO;GAC3B,KAAK,kBAAkB;GACvB,KAAK,yBACH,OAAO,OAAO,WAAW,IAAiB;EAC9C,OAAO;GACL,KAAK,eAAe,IAAI,UACtB,WACA,OAAO,UAAU,CAAC,CAAC,YACnB,kBACF;GACA,KAAK,kBAAkB;GACvB,KAAK,yBAAyB,KAAA;EAChC;EACA,KAAK,iBAAiB;EAEtB,MAAM,eAAe,OAAO,UAAU;EACtC,MAAM,cAAc,MAAM,UAAU;EACpC,KAAK,UAAU;GACb,GAAG;GACH,eAAe;IACb,GAAG,aAAa;KACf,mBAAmB;KAClB,GAAG;KACH,UA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ACP,KAAK,aAAa,IAAI,GAAG;CAE7B;CAEA,kBAAkB,KAAgB;EAChC,IAAI,KAAK,iBACP,KAAK,aAAa,OAAO,GAAG;CAEhC;CAEA,mBACE,eACA,qBACM;EACN,MAAM,oBAAoB;GACxB,MAAM,aAAa,oBACjB,eACA,KAAK,YACL,KAAK,SACP;GACA,MAAM,SAAS,aAAa,KAAK,OAAO,MAAM,EAAC,WAAU,CAAC,IAAI,CAAC;GAM/D,MAAM,cACJ,KAAK,2BAA2B,aAAa,MAC5C,KAAK,iCAAiC,KAAA,KACrC,gBACE,KAAK,8BACL,eACA,KAAK,QAAQ,WACf;GAEJ,IACE,KAAK,0BACL,YACE,eACA,KAAK,YACL,KAAK,uBAAuB,EAAiB,CAAC,KAC9C,KAAK,SACP,KACA,aACA;IACA,MAAM,cAAc,KAAK,OAAO,UAAU;IAC1C,IAAI,YAAY,SAAS,KAAA,GACvB,OAAO,6BACL,QACA,KAAK,wBACL,WACF;IAEF,OAAO,oBACL,QACA,KAAK,wBACL,WACF;GACF;GACA,OAAO;EACT;EAEA,OAAO;GACL,KAAK;GACL,eAAe;IACb,GAAG;KACF,KAAK,oBAAoB;GAC5B;EACF;CACF;AACF"}
1
+ {"version":3,"file":"join.js","names":[],"sources":["../../../../../zql/src/ivm/join.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport type {CompoundKey, System} 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 {\n makeAddChange,\n makeChildChange,\n makeEditChange,\n makeRemoveChange,\n type Change,\n} from './change.ts';\nimport type {Node} from './data.ts';\nimport {JoinIndex} from './join-index.ts';\nimport {\n buildJoinConstraint,\n canonicalKey,\n generateWithOverlay,\n generateWithOverlayUnordered,\n isJoinMatch,\n rowEqualsForCompoundKey,\n} from './join-utils.ts';\nimport {mergeSortedStreams} from './memory-source.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type Output,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\nimport {\n isInParentFetch,\n readParentFetchBounds,\n type ParentFetchBound,\n type TakeBoundProvider,\n} from './take-gate.ts';\n\ntype Args = {\n parent: Input;\n child: Input;\n // The nth key in parentKey corresponds to the nth key in childKey.\n parentKey: CompoundKey;\n childKey: CompoundKey;\n relationshipName: string;\n hidden: boolean;\n system: System;\n parentPartitionKey?: CompoundKey | undefined;\n boundProvider?: TakeBoundProvider | undefined;\n /**\n * Set when this join feeds an EXISTS or NOT EXISTS condition. Its parent\n * index is then kept by a JoinIndexTap at the end of the pipeline, so it\n * only holds the parents the pipeline emits, not every parent this join\n * sees. A child change that misses the index is dropped unless it could\n * make a parent pass the condition: an add for EXISTS, a remove for NOT\n * EXISTS. Those still fetch the parents.\n */\n exists?: ExistsJoin | undefined;\n};\n\nexport type ExistsJoin = {\n readonly op: 'EXISTS' | 'NOT EXISTS';\n readonly parentIndex: JoinIndex;\n};\n\n/**\n * The Join operator joins the output from two upstream inputs. Zero's join\n * is a little different from SQL's join in that we output hierarchical data,\n * not a flat table. This makes it a lot more useful for UI programming and\n * avoids duplicating tons of data like left join would.\n *\n * The Nodes output from Join have a new relationship added to them, which has\n * the name #relationshipName. The value of the relationship is a stream of\n * child nodes which are the corresponding values from the child source.\n */\nexport class Join implements Input {\n readonly #parent: Input;\n readonly #child: Input;\n readonly #parentKey: CompoundKey;\n readonly #childKey: CompoundKey;\n readonly #relationshipName: string;\n readonly #schema: SourceSchema;\n readonly #parentIndex: JoinIndex;\n /**\n * Whether this join adds and removes parents in #parentIndex itself. Not\n * so for EXISTS joins, see Args.exists.\n */\n readonly #indexesParents: boolean;\n /**\n * The child change type that is still pushed to the parents when no\n * indexed parent has its join key. See Args.exists.\n */\n readonly #qualifyingChildChange: ChangeType | undefined;\n readonly #boundProvider: TakeBoundProvider | undefined;\n\n #output: Output = throwOutput;\n\n #inprogressChildChange: Change | undefined;\n #inprogressChildChangePosition: Row | undefined;\n /**\n * Primary keys of the parents #inprogressChildChange has reached so far,\n * kept only when the parent input is unordered. An unordered stream is not\n * in `compareRows` order (SQLite returns it in whatever order its plan\n * visits, e.g. rowid order), so whether a parent is still in the push queue\n * cannot be decided by comparing it to #inprogressChildChangePosition.\n */\n #inprogressReachedParents: Set<string> | undefined;\n #inprogressParentFetchBounds: ParentFetchBound[] | undefined;\n\n constructor({\n parent,\n child,\n parentKey,\n childKey,\n relationshipName,\n hidden,\n system,\n parentPartitionKey,\n boundProvider,\n exists,\n }: Args) {\n assert(parent !== child, 'Parent and child must be different operators');\n assert(\n parentKey.length === childKey.length,\n 'The parentKey and childKey keys must have same length',\n );\n assert(!exists || !parentPartitionKey, 'EXISTS joins are not partitioned');\n this.#parent = parent;\n this.#child = child;\n this.#parentKey = parentKey;\n this.#childKey = childKey;\n this.#relationshipName = relationshipName;\n if (exists) {\n this.#parentIndex = exists.parentIndex;\n this.#indexesParents = false;\n this.#qualifyingChildChange =\n exists.op === 'EXISTS' ? ChangeType.ADD : ChangeType.REMOVE;\n } else {\n this.#parentIndex = new JoinIndex(\n parentKey,\n parent.getSchema().primaryKey,\n parentPartitionKey,\n );\n this.#indexesParents = true;\n this.#qualifyingChildChange = undefined;\n }\n this.#boundProvider = boundProvider;\n\n const parentSchema = parent.getSchema();\n const childSchema = child.getSchema();\n this.#schema = {\n ...parentSchema,\n relationships: {\n ...parentSchema.relationships,\n [relationshipName]: {\n ...childSchema,\n isHidden: hidden,\n system,\n },\n },\n };\n\n parent.setOutput({\n push: (change: Change) => this.#pushParent(change),\n reconcile: () => this.#reconcile(),\n });\n child.setOutput({\n push: (change: Change) => this.#pushChild(change),\n reconcile: () => this.#reconcile(),\n });\n }\n\n *#reconcile(): Stream<'yield'> {\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n\n destroy(): void {\n this.#parent.destroy();\n this.#child.destroy();\n }\n\n get parentIndexForTest(): JoinIndex {\n return this.#parentIndex;\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#schema;\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n for (const parentNode of this.#parent.fetch(req)) {\n if (parentNode === 'yield') {\n yield parentNode;\n continue;\n }\n this.#indexParentRow(parentNode.row);\n yield this.#processParentNode(parentNode.row, parentNode.relationships);\n }\n }\n\n *#pushParent(change: Change): Stream<'yield'> {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n this.#indexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeAddChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n ),\n this,\n );\n break;\n case ChangeType.REMOVE:\n this.#unindexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeRemoveChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n ),\n this,\n );\n break;\n case ChangeType.CHILD:\n yield* this.#output.push(\n makeChildChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n change[ChangeIndex.CHILD_DATA],\n ),\n this,\n );\n break;\n case ChangeType.EDIT: {\n // Assert the edit could not change the relationship.\n assert(\n rowEqualsForCompoundKey(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n this.#parentKey,\n ),\n `Parent edit must not change relationship.`,\n );\n this.#unindexParentRow(change[ChangeIndex.OLD_NODE].row);\n this.#indexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeEditChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n this.#processParentNode(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.OLD_NODE].relationships,\n ),\n ),\n this,\n );\n break;\n }\n default:\n unreachable(change);\n }\n }\n\n *#pushChild(change: Change): Stream<'yield'> {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n case ChangeType.REMOVE:\n yield* this.#pushChildChange(change[ChangeIndex.NODE].row, change);\n break;\n case ChangeType.CHILD:\n yield* this.#pushChildChange(change[ChangeIndex.NODE].row, change);\n break;\n case ChangeType.EDIT: {\n const childRow = change[ChangeIndex.NODE].row;\n const oldChildRow = change[ChangeIndex.OLD_NODE].row;\n // Assert the edit could not change the relationship.\n assert(\n rowEqualsForCompoundKey(oldChildRow, childRow, this.#childKey),\n 'Child edit must not change relationship.',\n );\n yield* this.#pushChildChange(childRow, change);\n break;\n }\n\n default:\n unreachable(change);\n }\n }\n\n *#pushChildChange(childRow: Row, change: Change): Stream<'yield'> {\n this.#inprogressChildChange = change;\n this.#inprogressChildChangePosition = undefined;\n this.#inprogressReachedParents =\n this.#parent.getSchema().sort === undefined ? new Set() : undefined;\n try {\n const constraint = buildJoinConstraint(\n childRow,\n this.#childKey,\n this.#parentKey,\n );\n if (constraint) {\n const partitions = this.#parentIndex.lookup(childRow, this.#childKey);\n let fetchConstraints: Record<string, Value>[];\n if (partitions) {\n fetchConstraints = partitions.map(partitionConstraint =>\n partitionConstraint\n ? {...constraint, ...partitionConstraint}\n : constraint,\n );\n } else if (change[ChangeIndex.TYPE] === this.#qualifyingChildChange) {\n fetchConstraints = [constraint];\n } else {\n return;\n }\n\n if (this.#boundProvider) {\n this.#inprogressParentFetchBounds = readParentFetchBounds(\n this.#boundProvider,\n fetchConstraints,\n );\n }\n\n let parentNodeStream: Stream<Node | 'yield'>;\n if (fetchConstraints.length === 1) {\n parentNodeStream = this.#parent.fetch({\n constraint: fetchConstraints[0],\n });\n } else {\n const streams = fetchConstraints.map(c =>\n this.#parent.fetch({constraint: c}),\n );\n const compare = (a: Node, b: Node) =>\n this.#schema.compareRows(a.row, b.row);\n parentNodeStream = mergeSortedStreams(streams, compare);\n }\n\n for (const parentNode of parentNodeStream) {\n if (parentNode === 'yield') {\n yield parentNode;\n continue;\n }\n this.#inprogressChildChangePosition = parentNode.row;\n this.#inprogressReachedParents?.add(\n canonicalKey(parentNode.row, this.#schema.primaryKey),\n );\n const childChange = makeChildChange(\n this.#processParentNode(parentNode.row, parentNode.relationships),\n {\n relationshipName: this.#relationshipName,\n change,\n },\n );\n yield* this.#output.push(childChange, this);\n }\n }\n } finally {\n this.#inprogressChildChange = undefined;\n this.#inprogressChildChangePosition = undefined;\n this.#inprogressReachedParents = undefined;\n this.#inprogressParentFetchBounds = undefined;\n }\n }\n\n /**\n * Whether the in-progress child change has yet to reach `parentNodeRow`,\n * i.e. the row comes after #inprogressChildChangePosition in the parent\n * stream.\n */\n #isAfterInprogressPosition(parentNodeRow: Row): boolean {\n if (this.#inprogressChildChangePosition === undefined) {\n return false;\n }\n if (this.#inprogressReachedParents) {\n return !this.#inprogressReachedParents.has(\n canonicalKey(parentNodeRow, this.#schema.primaryKey),\n );\n }\n return (\n this.#schema.compareRows(\n parentNodeRow,\n this.#inprogressChildChangePosition,\n ) > 0\n );\n }\n\n #indexParentRow(row: Row): void {\n if (this.#indexesParents) {\n this.#parentIndex.add(row);\n }\n }\n\n #unindexParentRow(row: Row): void {\n if (this.#indexesParents) {\n this.#parentIndex.remove(row);\n }\n }\n\n #processParentNode(\n parentNodeRow: Row,\n parentNodeRelations: Record<string, () => Stream<Node | 'yield'>>,\n ): Node {\n const childStream = (countOnly?: boolean | undefined) => {\n const constraint = buildJoinConstraint(\n parentNodeRow,\n this.#parentKey,\n this.#childKey,\n );\n\n // The parent has yet to get the in-progress child change if it comes\n // after the current position and a parent fetch of the push yields it.\n // With a TakeGate the fetches are capped at the bounds read when they\n // started.\n const inPushQueue =\n this.#isAfterInprogressPosition(parentNodeRow) &&\n (this.#inprogressParentFetchBounds === undefined ||\n isInParentFetch(\n this.#inprogressParentFetchBounds,\n parentNodeRow,\n this.#schema.compareRows,\n ));\n\n if (\n this.#inprogressChildChange &&\n isJoinMatch(\n parentNodeRow,\n this.#parentKey,\n this.#inprogressChildChange[ChangeIndex.NODE].row,\n this.#childKey,\n ) &&\n inPushQueue\n ) {\n // The overlay needs the child rows, so countOnly is not forwarded.\n const stream = constraint ? this.#child.fetch({constraint}) : [];\n const childSchema = this.#child.getSchema();\n if (childSchema.sort === undefined) {\n return generateWithOverlayUnordered(\n stream,\n this.#inprogressChildChange,\n childSchema,\n );\n }\n return generateWithOverlay(\n stream,\n this.#inprogressChildChange,\n childSchema,\n );\n }\n return constraint\n ? this.#child.fetch(countOnly ? {constraint, countOnly} : {constraint})\n : [];\n };\n\n return {\n row: parentNodeRow,\n relationships: {\n ...parentNodeRelations,\n [this.#relationshipName]: childStream,\n },\n };\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AA2EA,IAAa,OAAb,MAAmC;CACjC;CACA;CACA;CACA;CACA;CACA;CACA;;;;;CAKA;;;;;CAKA;CACA;CAEA,UAAkB;CAElB;CACA;;;;;;;;CAQA;CACA;CAEA,YAAY,EACV,QACA,OACA,WACA,UACA,kBACA,QACA,QACA,oBACA,eACA,UACO;EACP,OAAO,WAAW,OAAO,8CAA8C;EACvE,OACE,UAAU,WAAW,SAAS,QAC9B,uDACF;EACA,OAAO,CAAC,UAAU,CAAC,oBAAoB,kCAAkC;EACzE,KAAK,UAAU;EACf,KAAK,SAAS;EACd,KAAK,aAAa;EAClB,KAAK,YAAY;EACjB,KAAK,oBAAoB;EACzB,IAAI,QAAQ;GACV,KAAK,eAAe,OAAO;GAC3B,KAAK,kBAAkB;GACvB,KAAK,yBACH,OAAO,OAAO,WAAW,IAAiB;EAC9C,OAAO;GACL,KAAK,eAAe,IAAI,UACtB,WACA,OAAO,UAAU,CAAC,CAAC,YACnB,kBACF;GACA,KAAK,kBAAkB;GACvB,KAAK,yBAAyB,KAAA;EAChC;EACA,KAAK,iBAAiB;EAEtB,MAAM,eAAe,OAAO,UAAU;EACtC,MAAM,cAAc,MAAM,UAAU;EACpC,KAAK,UAAU;GACb,GAAG;GACH,eAA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AI;CAER;CAEA,gBAAgB,KAAgB;EAC9B,IAAI,KAAK,iBACP,KAAK,aAAa,IAAI,GAAG;CAE7B;CAEA,kBAAkB,KAAgB;EAChC,IAAI,KAAK,iBACP,KAAK,aAAa,OAAO,GAAG;CAEhC;CAEA,mBACE,eACA,qBACM;EACN,MAAM,eAAe,cAAoC;GACvD,MAAM,aAAa,oBACjB,eACA,KAAK,YACL,KAAK,SACP;GAMA,MAAM,cACJ,KAAK,2BAA2B,aAAa,MAC5C,KAAK,iCAAiC,KAAA,KACrC,gBACE,KAAK,8BACL,eACA,KAAK,QAAQ,WACf;GAEJ,IACE,KAAK,0BACL,YACE,eACA,KAAK,YACL,KAAK,uBAAuB,EAAiB,CAAC,KAC9C,KAAK,SACP,KACA,aACA;IAEA,MAAM,SAAS,aAAa,KAAK,OAAO,MAAM,EAAC,WAAU,CAAC,IAAI,CAAC;IAC/D,MAAM,cAAc,KAAK,OAAO,UAAU;IAC1C,IAAI,YAAY,SAAS,KAAA,GACvB,OAAO,6BACL,QACA,KAAK,wBACL,WACF;IAEF,OAAO,oBACL,QACA,KAAK,wBACL,WACF;GACF;GACA,OAAO,aACH,KAAK,OAAO,MAAM,YAAY;IAAC;IAAY;GAAS,IAAI,EAAC,WAAU,CAAC,IACpE,CAAC;EACP;EAEA,OAAO;GACL,KAAK;GACL,eAAe;IACb,GAAG;KACF,KAAK,oBAAoB;GAC5B;EACF;CACF;AACF"}
@@ -4,9 +4,9 @@ import { once, toSorted } from "../../../shared/src/iterables.js";
4
4
  import { must } from "../../../shared/src/must.js";
5
5
  import { skipYields } from "./skip-yields.js";
6
6
  import { makeAddChange, makeEditChange, makeRemoveChange } from "./change.js";
7
+ import { compareValues, makeComparator, valuesEqual } from "./data.js";
7
8
  import { consume } from "./stream.js";
8
9
  import { assertOrderingIncludesPK } from "../query/complete-ordering.js";
9
- import { compareValues, makeComparator, valuesEqual } from "./data.js";
10
10
  import { filterPush } from "./filter-push.js";
11
11
  import { constraintMatchesPrimaryKey, constraintMatchesRow, primaryKeyConstraintFromFilters } from "./constraint.js";
12
12
  import { BTreeSet } from "../../../shared/src/btree-set.js";
@@ -72,6 +72,12 @@ export type FetchRequest = {
72
72
  readonly start?: Start | undefined;
73
73
  /** Whether to fetch in reverse order of the SourceSchema's sort. */
74
74
  readonly reverse?: boolean | undefined;
75
+ /**
76
+ * The caller only counts the nodes. An operator that knows the count may
77
+ * then yield that many placeholder nodes instead of fetching the rows.
78
+ * `Cap` does this from its state.
79
+ */
80
+ readonly countOnly?: boolean | undefined;
75
81
  /**
76
82
  * Additional predicate the consumer wants the source/intermediate operators
77
83
  * to apply to the fetched rows. The receiver must AND this with whatever
@@ -1 +1 @@
1
- {"version":3,"file":"operator.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/operator.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,6BAA6B,CAAC;AAC3D,OAAO,KAAK,EAAC,GAAG,EAAC,MAAM,oCAAoC,CAAC;AAC5D,OAAO,KAAK,EAAC,mBAAmB,EAAC,MAAM,sBAAsB,CAAC;AAC9D,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,iBAAiB,CAAC;AAChD,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AAExC,OAAO,EAAC,UAAU,EAAC,MAAM,kBAAkB,CAAC;AAE5C;;GAEG;AACH,MAAM,WAAW,SAAS;IACxB,iDAAiD;IACjD,SAAS,IAAI,YAAY,CAAC;IAE1B;;;;OAIG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB;AAED,MAAM,WAAW,KAAM,SAAQ,SAAS;IACtC,+CAA+C;IAC/C,SAAS,CAAC,MAAM,EAAE,MAAM,GA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AS,CAAC,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;CAChD;AAED;;;GAGG;AACH,eAAO,MAAM,WAAW,EAAE,MAOzB,CAAC;AAEF;;;;;GAKG;AACH,MAAM,WAAW,QAAS,SAAQ,KAAK,EAAE,MAAM;IAC7C;;;;OAIG;IACH,SAAS,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAAC;CAC/C;AAED;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IACzC,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,SAAS,GAAG,SAAS,GAAG,SAAS,CAAC;IACzD;;OAEG;IACH,IAAI,CAAC,OAAO,CAAC,EAAE;QAAC,MAAM,EAAE,MAAM,CAAA;KAAC,GAAG,MAAM,CAAC,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC,CAAC;IAC9D,GAAG,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB;;;;OAIG;IACH,OAAO,IAAI,IAAI,CAAC;CACjB"}
@@ -1 +1 @@
1
- {"version":3,"file":"operator.js","names":[],"sources":["../../../../../zql/src/ivm/operator.ts"],"sourcesContent":["import type {JSONValue} from '../../../shared/src/json.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport type {NoSubqueryCondition} from '../builder/filter.ts';\nimport type {Change} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport type {Node} from './data.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\nexport {skipYields} from './skip-yields.ts';\n\n/**\n * Input to an operator.\n */\nexport interface InputBase {\n /** The schema of the data this input returns. */\n getSchema(): SourceSchema;\n\n /**\n * Completely destroy the input. Destroying an input\n * causes it to call destroy on its upstreams, fully\n * cleaning up a pipeline.\n */\n destroy(): void;\n}\n\nexport interface Input extends InputBase {\n /** Tell the input where to send its output. */\n setOutput(output: Output): void;\n\n /**\n * Fetch data. May modify the data in place.\n * Returns nodes sorted in order of `SourceSchema.compareRows`.\n *\n * The stream may contain 'yield' to yield control to the caller for purposes\n * of responsiveness.\n *\n * Contract:\n * - During fetch: If an input yields 'yield', 'yield' must be yielded to the\n * caller of fetch immediately.\n * - During push: If a fetch to an input consumed by the push logic yields\n * 'yield', it must be yielded to the caller of push immediately.\n * - During reconcile: If a fetch to an input consumed by reconcile logic\n * yields 'yield', it must be yielded to the caller of reconcile immediately.\n */\n fetch(req: FetchRequest): Stream<Node | 'yield'>;\n}\n\n/**\n * A single multi-row IN clause: a non-empty list of Constraints all\n * sharing the same column shape. Sources treat it as\n * `(col_a, col_b, …) IN VALUES (…)`.\n *\n * Caller invariants (sources rely on these — they are not re-checked):\n * - **Unique entries.** TableSource gets set semantics for free from SQL\n * `IN`; MemorySource fans sub-fetches out per entry, so duplicates\n * would yield the same row N times. FlippedJoin groups by canonical\n * parent-key (`flipped-join.ts:#fetchBatched`) before emitting.\n * - **Key-compatible with `req.constraint`.** Entries that contradict\n * `constraint` should be dropped upstream. FlippedJoin filters\n * incompatible children via `constraintsAreCompatible` before adding\n * them to a batch.\n */\nexport type MultiConstraint = readonly Constraint[];\n\nexport type FetchRequest = {\n readonly constraint?: Constraint | undefined;\n\n /**\n * List of multi-row IN clauses, all ANDed together (and ANDed with\n * `constraint` if both are provided). Each entry is a `MultiConstraint`\n * over its own set of columns.\n *\n * Used by FlippedJoin to push child→parent fetches into a single\n * batched statement, and to AND together constraints contributed by\n * chained FlippedJoins (e.g. `assigneeID IN (…) AND creatorID IN (…)`).\n */\n readonly multiConstraints?: readonly MultiConstraint[] | undefined;\n\n /** If supplied, `start.row` must have previously been output by fetch or push. */\n readonly start?: Start | undefined;\n\n /** Whether to fetch in reverse order of the SourceSchema's sort. */\n readonly reverse?: boolean | undefined;\n\n /**\n * Additional predicate the consumer wants the source/intermediate operators\n * to apply to the fetched rows. The receiver must AND this with whatever\n * filtering it already applies. Sources may use this to drive index\n * selection (e.g. PK lookup) the same way they use the connection-time\n * filter.\n *\n * Always a `NoSubqueryCondition` — anything passed via `req.filter` must\n * already have had correlated subqueries stripped (see `transformFilters`).\n *\n * Contract:\n * - The only operator that *introduces* `req.filter` is `FilterStart`. It\n * AND-merges its static condition with whatever `req.filter` it receives\n * and forwards the merged value upstream. No other operator should\n * construct a `req.filter`.\n * - \"Pass-through\" fetch operators (`Skip`, `Take`, `Join`, `FlippedJoin`,\n * `UnionFanIn`, `UnionFanOut`) MUST preserve `req.filter` when forwarding\n * to their input — typically via `{...req, ...overrides}` spread. Dropping\n * it silently regresses predicate pushdown for queries below the\n * forwarding operator.\n * - Operators that *initiate* internal fetches (e.g., `Join.#pushChildChange`,\n * `FlippedJoin.#pushChildChange`, `UnionFanIn.#pushInternalChange`,\n * `Take`'s push-time bound recomputation) construct fresh\n * `FetchRequest`s without `req.filter`, and that is correct: there is no\n * inbound consumer-side filter at those call sites. Any `FilterStart`\n * that sits between such an internal fetch and the source will still\n * apply its own condition on the way through, so the source still gets\n * the right WHERE.\n * - `Cap` is *not* a pass-through: it intentionally builds fresh\n * `{constraint}` requests when replaying tracked PKs and drops any\n * incoming `req.filter`. This is sound only because `Cap`'s sole\n * consumer in production is a `Join`'s child fetch (see\n * `Join.#processParentNode` in `join.ts`), which itself never carries\n * `req.filter` — Join's child fetches are constructed with just\n * `{constraint}` derived from join keys. If `Cap` is ever wired below\n * something that does push a `req.filter`, this carve-out becomes a\n * correctness bug.\n */\n readonly filter?: NoSubqueryCondition | undefined;\n};\n\nexport type Start = {\n readonly row: Row;\n readonly basis: 'at' | 'after';\n};\n\n/**\n * An output for an operator. Typically another Operator in an IVM pipeline,\n * but can also be a terminal sink (such as a View or test consumer).\n */\nexport interface Output {\n /**\n * Phase 1 of two-phase push: pushes incremental changes to data previously\n * received with fetch(). Consumers must apply all pushed changes or the\n * incremental result will be incorrect.\n *\n * Callers must maintain some invariants for correct operation:\n * - Only add rows which do not already exist (by deep equality).\n * - Only remove rows which do exist (by deep equality).\n *\n * Implementations can yield 'yield' to yield control to the caller for\n * purposes of responsiveness.\n *\n * Yield contract:\n * - During a push: If a push call to an output yields 'yield', it must be\n * yielded to the caller of push immediately.\n */\n push(change: Change, pusher: InputBase): Stream<'yield'>;\n\n /**\n * Phase 2 of two-phase push: invoked after Phase 1 changes have propagated.\n * Bounded operators (such as Take or Cap) use this signal to refill deficits,\n * emit refilled rows downstream via push(), and update their bounds.\n *\n * Implementations can yield 'yield' to yield control to the caller for\n * purposes of responsiveness.\n *\n * Yield contract:\n * - During reconcile: If an internal fetch, push, or downstream reconcile call\n * yields 'yield', it must be yielded to the caller of reconcile immediately.\n *\n * This method is optional on Output to allow terminal sinks (such as UI views\n * or custom third-party sinks) that only consume changes to omit it without\n * breaking compatibility. Intermediate operators must check\n * `if (this.#output.reconcile)` before forwarding.\n */\n reconcile?(pusher: InputBase): Stream<'yield'>;\n}\n\n/**\n * An implementation of Output that throws if pushed to. It is used as the\n * initial value for an operator's output before it is set.\n */\nexport const throwOutput: Output = {\n push(_change: Change): Stream<'yield'> {\n throw new Error('Output not set');\n },\n reconcile(): Stream<'yield'> {\n throw new Error('Output not set');\n },\n};\n\n/**\n * Operators are arranged into IVM pipelines.\n * They are stateful.\n * Each operator is an input to the next operator in the chain and an output\n * to the previous.\n */\nexport interface Operator extends Input, Output {\n /**\n * Intermediate pipeline operators MUST implement reconcile so that Phase 2\n * reconciliation reaches all bounded operators in the pipeline and is never\n * accidentally swallowed.\n */\n reconcile(pusher: InputBase): Stream<'yield'>;\n}\n\n/**\n * Operators get access to storage that they can store their internal\n * state in.\n */\nexport interface Storage {\n set(key: string, value: JSONValue): void;\n get(key: string, def?: JSONValue): JSONValue | undefined;\n /**\n * If options is not specified, defaults to scanning all entries.\n */\n scan(options?: {prefix: string}): Stream<[string, JSONValue]>;\n del(key: string): void;\n /**\n * Called by the operator that owns the storage when the operator is\n * destroyed. Releases anything the storage holds outside of the operator\n * (e.g. rows in a shared database). The storage is not used afterwards.\n */\n destroy(): void;\n}\n"],"mappings":";;;;;;AAkLA,IAAa,cAAsB;CACjC,KAAK,SAAkC;EACrC,MAAM,IAAI,MAAM,gBAAgB;CAClC;CACA,YAA6B;EAC3B,MAAM,IAAI,MAAM,gBAAgB;CAClC;AACF"}
1
+ {"version":3,"file":"operator.js","names":[],"sources":["../../../../../zql/src/ivm/operator.ts"],"sourcesContent":["import type {JSONValue} from '../../../shared/src/json.ts';\nimport type {Row} from '../../../zero-protocol/src/data.ts';\nimport type {NoSubqueryCondition} from '../builder/filter.ts';\nimport type {Change} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport type {Node} from './data.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\nexport {skipYields} from './skip-yields.ts';\n\n/**\n * Input to an operator.\n */\nexport interface InputBase {\n /** The schema of the data this input returns. */\n getSchema(): SourceSchema;\n\n /**\n * Completely destroy the input. Destroying an input\n * causes it to call destroy on its upstreams, fully\n * cleaning up a pipeline.\n */\n destroy(): void;\n}\n\nexport interface Input extends InputBase {\n /** Tell the input where to send its output. */\n setOutput(output: Output): void;\n\n /**\n * Fetch data. May modify the data in place.\n * Returns nodes sorted in order of `SourceSchema.compareRows`.\n *\n * The stream may contain 'yield' to yield control to the caller for purposes\n * of responsiveness.\n *\n * Contract:\n * - During fetch: If an input yields 'yield', 'yield' must be yielded to the\n * caller of fetch immediately.\n * - During push: If a fetch to an input consumed by the push logic yields\n * 'yield', it must be yielded to the caller of push immediately.\n * - During reconcile: If a fetch to an input consumed by reconcile logic\n * yields 'yield', it must be yielded to the caller of reconcile immediately.\n */\n fetch(req: FetchRequest): Stream<Node | 'yield'>;\n}\n\n/**\n * A single multi-row IN clause: a non-empty list of Constraints all\n * sharing the same column shape. Sources treat it as\n * `(col_a, col_b, …) IN VALUES (…)`.\n *\n * Caller invariants (sources rely on these — they are not re-checked):\n * - **Unique entries.** TableSource gets set semantics for free from SQL\n * `IN`; MemorySource fans sub-fetches out per entry, so duplicates\n * would yield the same row N times. FlippedJoin groups by canonical\n * parent-key (`flipped-join.ts:#fetchBatched`) before emitting.\n * - **Key-compatible with `req.constraint`.** Entries that contradict\n * `constraint` should be dropped upstream. FlippedJoin filters\n * incompatible children via `constraintsAreCompatible` before adding\n * them to a batch.\n */\nexport type MultiConstraint = readonly Constraint[];\n\nexport type FetchRequest = {\n readonly constraint?: Constraint | undefined;\n\n /**\n * List of multi-row IN clauses, all ANDed together (and ANDed with\n * `constraint` if both are provided). Each entry is a `MultiConstraint`\n * over its own set of columns.\n *\n * Used by FlippedJoin to push child→parent fetches into a single\n * batched statement, and to AND together constraints contributed by\n * chained FlippedJoins (e.g. `assigneeID IN (…) AND creatorID IN (…)`).\n */\n readonly multiConstraints?: readonly MultiConstraint[] | undefined;\n\n /** If supplied, `start.row` must have previously been output by fetch or push. */\n readonly start?: Start | undefined;\n\n /** Whether to fetch in reverse order of the SourceSchema's sort. */\n readonly reverse?: boolean | undefined;\n\n /**\n * The caller only counts the nodes. An operator that knows the count may\n * then yield that many placeholder nodes instead of fetching the rows.\n * `Cap` does this from its state.\n */\n readonly countOnly?: boolean | undefined;\n\n /**\n * Additional predicate the consumer wants the source/intermediate operators\n * to apply to the fetched rows. The receiver must AND this with whatever\n * filtering it already applies. Sources may use this to drive index\n * selection (e.g. PK lookup) the same way they use the connection-time\n * filter.\n *\n * Always a `NoSubqueryCondition` — anything passed via `req.filter` must\n * already have had correlated subqueries stripped (see `transformFilters`).\n *\n * Contract:\n * - The only operator that *introduces* `req.filter` is `FilterStart`. It\n * AND-merges its static condition with whatever `req.filter` it receives\n * and forwards the merged value upstream. No other operator should\n * construct a `req.filter`.\n * - \"Pass-through\" fetch operators (`Skip`, `Take`, `Join`, `FlippedJoin`,\n * `UnionFanIn`, `UnionFanOut`) MUST preserve `req.filter` when forwarding\n * to their input — typically via `{...req, ...overrides}` spread. Dropping\n * it silently regresses predicate pushdown for queries below the\n * forwarding operator.\n * - Operators that *initiate* internal fetches (e.g., `Join.#pushChildChange`,\n * `FlippedJoin.#pushChildChange`, `UnionFanIn.#pushInternalChange`,\n * `Take`'s push-time bound recomputation) construct fresh\n * `FetchRequest`s without `req.filter`, and that is correct: there is no\n * inbound consumer-side filter at those call sites. Any `FilterStart`\n * that sits between such an internal fetch and the source will still\n * apply its own condition on the way through, so the source still gets\n * the right WHERE.\n * - `Cap` is *not* a pass-through: it intentionally builds fresh\n * `{constraint}` requests when replaying tracked PKs and drops any\n * incoming `req.filter`. This is sound only because `Cap`'s sole\n * consumer in production is a `Join`'s child fetch (see\n * `Join.#processParentNode` in `join.ts`), which itself never carries\n * `req.filter` — Join's child fetches are constructed with just\n * `{constraint}` derived from join keys. If `Cap` is ever wired below\n * something that does push a `req.filter`, this carve-out becomes a\n * correctness bug.\n */\n readonly filter?: NoSubqueryCondition | undefined;\n};\n\nexport type Start = {\n readonly row: Row;\n readonly basis: 'at' | 'after';\n};\n\n/**\n * An output for an operator. Typically another Operator in an IVM pipeline,\n * but can also be a terminal sink (such as a View or test consumer).\n */\nexport interface Output {\n /**\n * Phase 1 of two-phase push: pushes incremental changes to data previously\n * received with fetch(). Consumers must apply all pushed changes or the\n * incremental result will be incorrect.\n *\n * Callers must maintain some invariants for correct operation:\n * - Only add rows which do not already exist (by deep equality).\n * - Only remove rows which do exist (by deep equality).\n *\n * Implementations can yield 'yield' to yield control to the caller for\n * purposes of responsiveness.\n *\n * Yield contract:\n * - During a push: If a push call to an output yields 'yield', it must be\n * yielded to the caller of push immediately.\n */\n push(change: Change, pusher: InputBase): Stream<'yield'>;\n\n /**\n * Phase 2 of two-phase push: invoked after Phase 1 changes have propagated.\n * Bounded operators (such as Take or Cap) use this signal to refill deficits,\n * emit refilled rows downstream via push(), and update their bounds.\n *\n * Implementations can yield 'yield' to yield control to the caller for\n * purposes of responsiveness.\n *\n * Yield contract:\n * - During reconcile: If an internal fetch, push, or downstream reconcile call\n * yields 'yield', it must be yielded to the caller of reconcile immediately.\n *\n * This method is optional on Output to allow terminal sinks (such as UI views\n * or custom third-party sinks) that only consume changes to omit it without\n * breaking compatibility. Intermediate operators must check\n * `if (this.#output.reconcile)` before forwarding.\n */\n reconcile?(pusher: InputBase): Stream<'yield'>;\n}\n\n/**\n * An implementation of Output that throws if pushed to. It is used as the\n * initial value for an operator's output before it is set.\n */\nexport const throwOutput: Output = {\n push(_change: Change): Stream<'yield'> {\n throw new Error('Output not set');\n },\n reconcile(): Stream<'yield'> {\n throw new Error('Output not set');\n },\n};\n\n/**\n * Operators are arranged into IVM pipelines.\n * They are stateful.\n * Each operator is an input to the next operator in the chain and an output\n * to the previous.\n */\nexport interface Operator extends Input, Output {\n /**\n * Intermediate pipeline operators MUST implement reconcile so that Phase 2\n * reconciliation reaches all bounded operators in the pipeline and is never\n * accidentally swallowed.\n */\n reconcile(pusher: InputBase): Stream<'yield'>;\n}\n\n/**\n * Operators get access to storage that they can store their internal\n * state in.\n */\nexport interface Storage {\n set(key: string, value: JSONValue): void;\n get(key: string, def?: JSONValue): JSONValue | undefined;\n /**\n * If options is not specified, defaults to scanning all entries.\n */\n scan(options?: {prefix: string}): Stream<[string, JSONValue]>;\n del(key: string): void;\n /**\n * Called by the operator that owns the storage when the operator is\n * destroyed. Releases anything the storage holds outside of the operator\n * (e.g. rows in a shared database). The storage is not used afterwards.\n */\n destroy(): void;\n}\n"],"mappings":";;;;;;AAyLA,IAAa,cAAsB;CACjC,KAAK,SAAkC;EACrC,MAAM,IAAI,MAAM,gBAAgB;CAClC;CACA,YAA6B;EAC3B,MAAM,IAAI,MAAM,gBAAgB;CAClC;AACF"}
@@ -1 +1 @@
1
- {"version":3,"file":"take.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/take.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAC,GAAG,EAAQ,MAAM,oCAAoC,CAAC;AACnE,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,2CAA2C,CA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K,EAAE,MAAM,EACb,YAAY,CAAC,EAAE,YAAY,EAa5B;IAED,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9B;IAED,SAAS,IAAI,YAAY,CAExB;IAED,QAAQ,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,GAAG,GAAG,SAAS,CAmBjD;IAEA,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CA2ChD;IAkFA,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAsKrC;IA4PD,OAAO,IAAI,IAAI,CAGd;IAEA,SAAS,CAAC,OAAO,CAAC,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAqD/C;CACF;AAsBD,wBAAgB,6BAA6B,CAC3C,UAAU,EAAE,UAAU,GAAG,SAAS,EAClC,YAAY,EAAE,YAAY,GAAG,SAAS,GACrC,OAAO,CAaT;AAED,wBAAgB,8BAA8B,CAC5C,UAAU,EAAE,UAAU,GAAG,SAAS,EAClC,YAAY,EAAE,YAAY,GAAG,SAAS,GACrC,OAAO,CAUT;AAED,wBAAgB,0BAA0B,CACxC,YAAY,EAAE,YAAY,GACzB,UAAU,CAUZ"}
@@ -2,8 +2,8 @@ import { assert, unreachable } from "../../../shared/src/asserts.js";
2
2
  import { hasOwn } from "../../../shared/src/has-own.js";
3
3
  import { throwOutput } from "./operator.js";
4
4
  import { makeAddChange, makeRemoveChange } from "./change.js";
5
+ import { compareValues, valueToKey } from "./data.js";
5
6
  import { assertOrderingIncludesPK } from "../query/complete-ordering.js";
6
- import { compareValues } from "./data.js";
7
7
  //#region ../zql/src/ivm/take.ts
8
8
  /**
9
9
  * The Take operator is for implementing limit queries. It takes the first n
@@ -27,8 +27,8 @@ var Take = class {
27
27
  /**
28
28
  * Partitions that incurred removals in Phase 1 and require deficit refills in
29
29
  * Phase 2 (reconcile). Keyed by `takeStateKey` (see `getTakeStateKey`), matching
30
- * `#storage` keys: `'["take"]'` when unpartitioned, or `'["take", ...partitionValues]'`
31
- * when partitioned.
30
+ * `#storage` keys: `'["take"]'` when unpartitioned, `'take:' + valueToKey(value)` for one
31
+ * partition column, or `'["take", ...partitionValues]'` for more.
32
32
  */
33
33
  #dirtyPartitions = /* @__PURE__ */ new Map();
34
34
  #output = throwOutput;
@@ -414,6 +414,7 @@ var Take = class {
414
414
  }
415
415
  };
416
416
  function getTakeStateKey(partitionKey, rowOrConstraint) {
417
+ if (partitionKey?.length === 1 && rowOrConstraint) return "take:" + valueToKey(rowOrConstraint[partitionKey[0]]);
417
418
  const partitionValues = [];
418
419
  if (partitionKey && rowOrConstraint) for (const key of partitionKey) partitionValues.push(rowOrConstraint[key]);
419
420
  return JSON.stringify(["take", ...partitionValues]);
@@ -1 +1 @@
1
- {"version":3,"file":"take.js","names":[],"sources":["../../../../../zql/src/ivm/take.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {hasOwn} from '../../../shared/src/has-own.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport {assertOrderingIncludesPK} from '../query/complete-ordering.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n makeAddChange,\n makeRemoveChange,\n type Change,\n type EditChange,\n} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {compareValues, type Comparator, type Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\nimport type {TakeBoundProvider, TakeGate} from './take-gate.ts';\n\ntype TakeState = {\n size: number;\n bound: Row | undefined;\n};\n\ninterface TakeStorage {\n get(key: string): TakeState | undefined;\n set(key: string, value: TakeState): void;\n del(key: string): void;\n destroy(): void;\n}\n\nexport type PartitionKey = PrimaryKey;\n\ntype DirtyPartitionState = {\n constraint: Constraint | undefined;\n /**\n * The bound to report to upstream TakeGate via getBound() until Phase 2\n * reconciliation completes.\n *\n * When removals reduce partition size below limit in Phase 1, normal getBound()\n * would return undefined (unbounded), which would cause upstream TakeGate to open\n * and allow unbounded parent pushes through. Preserving gateBound keeps TakeGate\n * capped at the pre-removal boundary until Phase 2 refills the deficit.\n */\n gateBound: Row | undefined;\n};\n\n/**\n * The Take operator is for implementing limit queries. It takes the first n\n * nodes of its input as determined by the input’s comparator. It then keeps\n * a *bound* of the last item it has accepted so that it can evaluate whether\n * new incoming pushes should be accepted or rejected.\n *\n * Take can count rows globally or by unique value of some field.\n *\n * Maintains the invariant that its output size is always <= limit, even\n * mid processing of a push.\n */\nexport class Take implements Operator, TakeBoundProvider {\n readonly #input: Input;\n readonly #storage: TakeStorage;\n readonly #limit: number;\n readonly #partitionKey: PartitionKey | undefined;\n readonly #partitionKeyComparator: Comparator | undefined;\n // Fetch overlay needed for some split push cases.\n #rowHiddenFromFetch: Row | undefined;\n\n #takeGate: TakeGate | undefined;\n /**\n * Partitions that incurred removals in Phase 1 and require deficit refills in\n * Phase 2 (reconcile). Keyed by `takeStateKey` (see `getTakeStateKey`), matching\n * `#storage` keys: `'[\"take\"]'` when unpartitioned, or `'[\"take\", ...partitionValues]'`\n * when partitioned.\n */\n readonly #dirtyPartitions = new Map<string, DirtyPartitionState>();\n\n #output: Output = throwOutput;\n\n setTakeGate(gate: TakeGate): void {\n this.#takeGate = gate;\n }\n\n constructor(\n input: Input,\n storage: Storage,\n limit: number,\n partitionKey?: PartitionKey,\n ) {\n assert(limit >= 0, 'Limit must be non-negative');\n const {sort} = input.getSchema();\n assert(sort !== undefined, 'Take requires sorted input');\n assertOrderingIncludesPK(sort, input.getSchema().primaryKey);\n input.setOutput(this);\n this.#input = input;\n this.#storage = storage as TakeStorage;\n this.#limit = limit;\n this.#partitionKey = partitionKey;\n this.#partitionKeyComparator =\n partitionKey && makePartitionKeyComparator(partitionKey);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n getBound(constraint?: Constraint): Row | undefined {\n if (\n this.#partitionKey &&\n !constraintContainsPartitionKey(constraint, this.#partitionKey)\n ) {\n return undefined;\n }\n const takeStateKey = getTakeStateKey(this.#partitionKey, constraint);\n const dirty = this.#dirtyPartitions.get(takeStateKey);\n if (dirty) {\n // While dirty in Phase 1, return gateBound (the bound prior to removals) so\n // upstream operators (e.g. TakeGate) remain capped rather than unbounding during push.\n return dirty.gateBound;\n }\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState || takeState.size < this.#limit) {\n return undefined;\n }\n return takeState.bound;\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n assert(\n !this.#partitionKey ||\n (req.constraint !== undefined &&\n constraintContainsPartitionKey(req.constraint, this.#partitionKey)),\n 'Partitioned take does not allow unpartitioned fetches',\n );\n\n const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState) {\n if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) {\n yield* this.#initialFetch(req, takeStateKey);\n }\n return;\n }\n if (takeState.bound === undefined) {\n return;\n }\n let count = 0;\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n if (this.getSchema().compareRows(takeState.bound, inputNode.row) < 0) {\n return;\n }\n if (\n this.#rowHiddenFromFetch &&\n this.getSchema().compareRows(\n this.#rowHiddenFromFetch,\n inputNode.row,\n ) === 0\n ) {\n continue;\n }\n yield inputNode;\n count++;\n if (count >= this.#limit) {\n return;\n }\n }\n }\n\n *#initialFetch(\n req: FetchRequest,\n takeStateKey: string,\n ): Stream<Node | 'yield'> {\n assert(req.start === undefined, 'Start should be undefined');\n assert(!req.reverse, 'Reverse should be false');\n\n if (this.#limit === 0) {\n return;\n }\n\n assert(\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey),\n 'Constraint should match partition key',\n );\n\n // The caller (fetch) has already checked that there is no state for\n // takeStateKey.\n\n let size = 0;\n let bound: Row | undefined;\n let downstreamEarlyReturn = true;\n let exceptionThrown = false;\n try {\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield 'yield';\n continue;\n }\n yield inputNode;\n bound = inputNode.row;\n size++;\n if (size === this.#limit) {\n break;\n }\n }\n downstreamEarlyReturn = false;\n } catch (e) {\n exceptionThrown = true;\n throw e;\n } finally {\n if (!exceptionThrown) {\n this.#setTakeState(takeStateKey, size, bound);\n // If it becomes necessary to support downstream early return, this\n // assert should be removed, and replaced with code that consumes\n // the input stream until limit is reached or the input stream is\n // exhausted so that takeState is properly hydrated.\n assert(\n !downstreamEarlyReturn,\n 'Unexpected early return prevented full hydration',\n );\n }\n }\n }\n\n #getStateAndConstraint(row: Row) {\n const takeStateKey = getTakeStateKey(this.#partitionKey, row);\n const takeState = this.#storage.get(takeStateKey);\n let constraint: Constraint | undefined;\n if (takeState) {\n constraint =\n this.#partitionKey &&\n Object.fromEntries(\n this.#partitionKey.map(key => [key, row[key]] as const),\n );\n }\n\n return {takeState, takeStateKey, constraint} as\n | {\n takeState: undefined;\n takeStateKey: string;\n constraint: undefined;\n }\n | {\n takeState: TakeState;\n takeStateKey: string;\n constraint: Constraint | undefined;\n };\n }\n\n *push(change: Change): Stream<'yield'> {\n if (change[ChangeIndex.TYPE] === ChangeType.EDIT) {\n yield* this.#pushEditChange(change);\n return;\n }\n\n const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint(\n change[ChangeIndex.NODE].row,\n );\n if (!takeState) {\n return;\n }\n\n const {compareRows} = this.getSchema();\n\n if (change[ChangeIndex.TYPE] === ChangeType.ADD) {\n if (takeState.size < this.#limit) {\n if (this.#dirtyPartitions.has(takeStateKey)) {\n // While dirty with an unresolved deficit in Phase 1, only admit rows that\n // sort strictly before the current window boundary. We refuse to expand the\n // bound forward during Phase 1 so that Phase 2 can backfill earlier candidate\n // rows from storage in sort order.\n if (\n takeState.bound === undefined ||\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0\n ) {\n return;\n }\n }\n const nextSize = takeState.size + 1;\n if (nextSize === this.#limit) {\n this.#dirtyPartitions.delete(takeStateKey);\n }\n this.#setTakeState(\n takeStateKey,\n nextSize,\n takeState.bound === undefined ||\n compareRows(takeState.bound, change[ChangeIndex.NODE].row) < 0\n ? change[ChangeIndex.NODE].row\n : takeState.bound,\n );\n yield* this.#output.push(change, this);\n return;\n }\n // size === limit\n if (\n takeState.bound === undefined ||\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0\n ) {\n return;\n }\n // added row < bound\n let beforeBoundNode: Node | undefined;\n let boundNode: Node | undefined;\n if (this.#limit === 1) {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n boundNode = node;\n break;\n }\n } else {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n } else if (boundNode === undefined) {\n boundNode = node;\n } else {\n beforeBoundNode = node;\n break;\n }\n }\n }\n assert(\n boundNode !== undefined,\n 'Take: boundNode must be found during fetch',\n );\n const removeChange = makeRemoveChange(boundNode);\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode === undefined ||\n compareRows(change[ChangeIndex.NODE].row, beforeBoundNode.row) > 0\n ? change[ChangeIndex.NODE].row\n : beforeBoundNode.row,\n );\n yield* this.#pushWithRowHiddenFromFetch(\n change[ChangeIndex.NODE].row,\n removeChange,\n );\n yield* this.#output.push(change, this);\n } else if (change[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n if (takeState.bound === undefined) {\n // change is after bound\n return;\n }\n const compToBound = compareRows(\n change[ChangeIndex.NODE].row,\n takeState.bound,\n );\n if (compToBound > 0 || (compToBound < 0 && this.#limit === 1)) {\n // change is not in window\n return;\n }\n let beforeBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n beforeBoundNode = node;\n break;\n }\n\n const finalBound =\n takeState.size - 1 === 0\n ? undefined\n : compToBound < 0\n ? takeState.bound\n : beforeBoundNode?.row;\n if (!this.#dirtyPartitions.has(takeStateKey)) {\n // Snapshot the pre-removal bound as the gate bound on the first removal that\n // dirties this partition. Subsequent getBound() calls throughout Phase 1 will return\n // this bound to keep upstream TakeGate bounded until Phase 2 refills deficits.\n this.#dirtyPartitions.set(takeStateKey, {\n constraint,\n gateBound: takeState.bound,\n });\n }\n this.#setTakeState(takeStateKey, takeState.size - 1, finalBound);\n yield* this.#output.push(change, this);\n } else if (change[ChangeIndex.TYPE] === ChangeType.CHILD) {\n // A 'child' change should be pushed to output if its row\n // is <= bound.\n if (\n takeState.bound &&\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) <= 0\n ) {\n yield* this.#output.push(change, this);\n }\n }\n }\n\n *#pushEditChange(change: EditChange): Stream<'yield'> {\n assert(\n !this.#partitionKeyComparator ||\n this.#partitionKeyComparator(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n ) === 0,\n 'Unexpected change of partition key',\n );\n\n const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint(\n change[ChangeIndex.OLD_NODE].row,\n );\n if (!takeState) {\n return;\n }\n\n assert(takeState.bound, 'Bound should be set');\n const {compareRows} = this.getSchema();\n const oldCmp = compareRows(\n change[ChangeIndex.OLD_NODE].row,\n takeState.bound,\n );\n const newCmp = compareRows(change[ChangeIndex.NODE].row, takeState.bound);\n\n const replaceBoundAndForwardChange = () => {\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n change[ChangeIndex.NODE].row,\n );\n return this.#output.push(change, this);\n };\n\n // The bounds row was changed.\n if (oldCmp === 0) {\n // The new row is the new bound.\n if (newCmp === 0) {\n // no need to update the state since we are keeping the bounds\n yield* this.#output.push(change, this);\n return;\n }\n\n if (newCmp < 0) {\n if (this.#limit === 1) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n // New row will be in the result but it might not be the bounds any\n // more. We need to find the row before the bounds to determine the new\n // bounds.\n\n let beforeBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n beforeBoundNode = node;\n break;\n }\n assert(\n beforeBoundNode !== undefined,\n 'Take: beforeBoundNode must be found during fetch',\n );\n\n this.#setTakeState(takeStateKey, takeState.size, beforeBoundNode.row);\n yield* this.#output.push(change, this);\n return;\n }\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n // Find the first item at the old bounds. This will be the new bounds.\n let newBoundNode: Node | undefined;\n this.#takeGate?.open();\n try {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n newBoundNode = node;\n break;\n }\n } finally {\n this.#takeGate?.close();\n }\n assert(\n newBoundNode !== undefined,\n 'Take: newBoundNode must be found during fetch',\n );\n\n // The next row is the new row. We can replace the bounds and keep the\n // edit change.\n if (compareRows(newBoundNode.row, change[ChangeIndex.NODE].row) === 0) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n // The new row is now outside the bounds, so we need to remove the old\n // row and add the new bounds row.\n this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);\n yield* this.#pushWithRowHiddenFromFetch(\n newBoundNode.row,\n makeRemoveChange(change[ChangeIndex.OLD_NODE]),\n );\n yield* this.#output.push(makeAddChange(newBoundNode), this);\n return;\n }\n\n if (oldCmp > 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new outside of bounds\n if (newCmp > 0) {\n return;\n }\n\n // old was outside, new is inside. Pushing out the old bounds\n assert(newCmp < 0, 'New comparison must be less than 0');\n\n let oldBoundNode: Node | undefined;\n let newBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n } else if (oldBoundNode === undefined) {\n oldBoundNode = node;\n } else {\n newBoundNode = node;\n break;\n }\n }\n assert(\n oldBoundNode !== undefined,\n 'Take: oldBoundNode must be found during fetch',\n );\n assert(\n newBoundNode !== undefined,\n 'Take: newBoundNode must be found during fetch',\n );\n\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);\n yield* this.#pushWithRowHiddenFromFetch(\n change[ChangeIndex.NODE].row,\n makeRemoveChange(oldBoundNode),\n );\n yield* this.#output.push(makeAddChange(change[ChangeIndex.NODE]), this);\n\n return;\n }\n\n if (oldCmp < 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new inside of bounds\n if (newCmp < 0) {\n yield* this.#output.push(change, this);\n return;\n }\n\n // old was inside, new is larger than old bound\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n\n // at this point we need to find the row after the bound and use that or\n // the newRow as the new bound.\n let afterBoundNode: Node | undefined;\n this.#takeGate?.open();\n try {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n afterBoundNode = node;\n break;\n }\n } finally {\n this.#takeGate?.close();\n }\n assert(\n afterBoundNode !== undefined,\n 'Take: afterBoundNode must be found during fetch',\n );\n\n // The new row is the new bound. Use an edit change.\n if (compareRows(afterBoundNode.row, change[ChangeIndex.NODE].row) === 0) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n yield* this.#output.push(\n makeRemoveChange(change[ChangeIndex.OLD_NODE]),\n this,\n );\n this.#setTakeState(takeStateKey, takeState.size, afterBoundNode.row);\n yield* this.#output.push(makeAddChange(afterBoundNode), this);\n return;\n }\n\n unreachable();\n }\n\n *#pushWithRowHiddenFromFetch(row: Row, change: Change) {\n this.#rowHiddenFromFetch = row;\n try {\n yield* this.#output.push(change, this);\n } finally {\n this.#rowHiddenFromFetch = undefined;\n }\n }\n\n #setTakeState(takeStateKey: string, size: number, bound: Row | undefined) {\n this.#storage.set(takeStateKey, {\n size,\n bound,\n });\n }\n\n destroy(): void {\n this.#input.destroy();\n this.#storage.destroy();\n }\n\n *reconcile(_pusher?: InputBase): Stream<'yield'> {\n if (this.#dirtyPartitions.size > 0) {\n const dirty = [...this.#dirtyPartitions.entries()];\n this.#dirtyPartitions.clear();\n\n for (const [takeStateKey, {constraint}] of dirty) {\n const takeState = this.#storage.get(takeStateKey);\n assert(\n takeState !== undefined,\n 'Take: dirty partition must exist in storage',\n );\n const deficit = this.#limit - takeState.size;\n assert(deficit > 0, 'Take: dirty partition must have a deficit');\n\n const toPush: Node[] = [];\n this.#takeGate?.open();\n try {\n const stream = this.#input.fetch({\n start: takeState.bound\n ? {\n row: takeState.bound,\n basis: 'after',\n }\n : undefined,\n constraint,\n });\n\n for (const node of stream) {\n if (node === 'yield') {\n yield 'yield';\n continue;\n }\n toPush.push(node);\n if (toPush.length === deficit) {\n break;\n }\n }\n } finally {\n this.#takeGate?.close();\n }\n\n let currentSize = takeState.size;\n for (const node of toPush) {\n currentSize++;\n this.#setTakeState(takeStateKey, currentSize, node.row);\n yield* this.#output.push(makeAddChange(node), this);\n }\n }\n }\n\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n}\n\nfunction getTakeStateKey(\n partitionKey: PartitionKey | undefined,\n rowOrConstraint: Row | Constraint | undefined,\n): string {\n // The order must be consistent. We always use the order as defined by the\n // partition key.\n const partitionValues: Value[] = [];\n\n if (partitionKey && rowOrConstraint) {\n for (const key of partitionKey) {\n partitionValues.push(rowOrConstraint[key]);\n }\n }\n\n return JSON.stringify(['take', ...partitionValues]);\n}\n\nexport function constraintMatchesPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return constraint === partitionKey;\n }\n if (partitionKey.length !== Object.keys(constraint).length) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nexport function constraintContainsPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nexport function makePartitionKeyComparator(\n partitionKey: PartitionKey,\n): Comparator {\n return (a, b) => {\n for (const key of partitionKey) {\n const cmp = compareValues(a[key], b[key]);\n if (cmp !== 0) {\n return cmp;\n }\n }\n return 0;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmEA,IAAa,OAAb,MAAyD;CACvD;CACA;CACA;CACA;CACA;CAEA;CAEA;;;;;;;CAOA,mCAA4B,IAAI,IAAiC;CAEjE,UAAkB;CAElB,YAAY,MAAsB;EAChC,KAAK,YAAY;CACnB;CAEA,YACE,OACA,SACA,OACA,cACA;EACA,OAAO,SAAS,GAAG,4BAA4B;EAC/C,MAAM,EAAC,SAAQ,MAAM,UAAU;EAC/B,OAAO,SAAS,KAAA,GAAW,4BAA4B;EACvD,yBAAyB,MAAM,MAAM,UAAU,CAAC,CAAC,UAAU;EAC3D,MAAM,UAAU,IAAI;EACpB,KAAK,SAAS;EACd,KAAK,WAAW;EAChB,KAAK,SAAS;EACd,KAAK,gBAAgB;EACrB,KAAK,0BACH,gBAAgB,2BAA2B,YAAY;CAC3D;CAEA,UAAU,QAAsB;EAC9B,KAAK,UAAU;CACjB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,U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AAI,GAAG,IAAI;IACpD;GACF;EACF;EAEA,IAAI,KAAK,QAAQ,WACf,OAAO,KAAK,QAAQ,UAAU,IAAI;CAEtC;AACF;AAEA,SAAS,gBACP,cACA,iBACQ;CAGR,MAAM,kBAA2B,CAAC;CAElC,IAAI,gBAAgB,iBAClB,KAAK,MAAM,OAAO,cAChB,gBAAgB,KAAK,gBAAgB,IAAI;CAI7C,OAAO,KAAK,UAAU,CAAC,QAAQ,GAAG,eAAe,CAAC;AACpD;AAEA,SAAgB,8BACd,YACA,cACS;CACT,IAAI,eAAe,KAAA,KAAa,iBAAiB,KAAA,GAC/C,OAAO,eAAe;CAExB,IAAI,aAAa,WAAW,OAAO,KAAK,UAAU,CAAC,CAAC,QAClD,OAAO;CAET,KAAK,MAAM,OAAO,cAChB,IAAI,CAAC,OAAO,YAAY,GAAG,GACzB,OAAO;CAGX,OAAO;AACT;AAEA,SAAgB,+BACd,YACA,cACS;CACT,IAAI,eAAe,KAAA,KAAa,iBAAiB,KAAA,GAC/C,OAAO;CAET,KAAK,MAAM,OAAO,cAChB,IAAI,CAAC,OAAO,YAAY,GAAG,GACzB,OAAO;CAGX,OAAO;AACT;AAEA,SAAgB,2BACd,cACY;CACZ,QAAQ,GAAG,MAAM;EACf,KAAK,MAAM,OAAO,cAAc;GAC9B,MAAM,MAAM,cAAc,EAAE,MAAM,EAAE,IAAI;GACxC,IAAI,QAAQ,GACV,OAAO;EAEX;EACA,OAAO;CACT;AACF"}
1
+ {"version":3,"file":"take.js","names":[],"sources":["../../../../../zql/src/ivm/take.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {hasOwn} from '../../../shared/src/has-own.ts';\nimport type {Row, Value} from '../../../zero-protocol/src/data.ts';\nimport type {PrimaryKey} from '../../../zero-protocol/src/primary-key.ts';\nimport {assertOrderingIncludesPK} from '../query/complete-ordering.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport {\n makeAddChange,\n makeRemoveChange,\n type Change,\n type EditChange,\n} from './change.ts';\nimport type {Constraint} from './constraint.ts';\nimport {compareValues, valueToKey, type Comparator, type Node} from './data.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n type Storage,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream} from './stream.ts';\nimport type {TakeBoundProvider, TakeGate} from './take-gate.ts';\n\ntype TakeState = {\n size: number;\n bound: Row | undefined;\n};\n\ninterface TakeStorage {\n get(key: string): TakeState | undefined;\n set(key: string, value: TakeState): void;\n del(key: string): void;\n destroy(): void;\n}\n\nexport type PartitionKey = PrimaryKey;\n\ntype DirtyPartitionState = {\n constraint: Constraint | undefined;\n /**\n * The bound to report to upstream TakeGate via getBound() until Phase 2\n * reconciliation completes.\n *\n * When removals reduce partition size below limit in Phase 1, normal getBound()\n * would return undefined (unbounded), which would cause upstream TakeGate to open\n * and allow unbounded parent pushes through. Preserving gateBound keeps TakeGate\n * capped at the pre-removal boundary until Phase 2 refills the deficit.\n */\n gateBound: Row | undefined;\n};\n\n/**\n * The Take operator is for implementing limit queries. It takes the first n\n * nodes of its input as determined by the input’s comparator. It then keeps\n * a *bound* of the last item it has accepted so that it can evaluate whether\n * new incoming pushes should be accepted or rejected.\n *\n * Take can count rows globally or by unique value of some field.\n *\n * Maintains the invariant that its output size is always <= limit, even\n * mid processing of a push.\n */\nexport class Take implements Operator, TakeBoundProvider {\n readonly #input: Input;\n readonly #storage: TakeStorage;\n readonly #limit: number;\n readonly #partitionKey: PartitionKey | undefined;\n readonly #partitionKeyComparator: Comparator | undefined;\n // Fetch overlay needed for some split push cases.\n #rowHiddenFromFetch: Row | undefined;\n\n #takeGate: TakeGate | undefined;\n /**\n * Partitions that incurred removals in Phase 1 and require deficit refills in\n * Phase 2 (reconcile). Keyed by `takeStateKey` (see `getTakeStateKey`), matching\n * `#storage` keys: `'[\"take\"]'` when unpartitioned, `'take:' + valueToKey(value)` for one\n * partition column, or `'[\"take\", ...partitionValues]'` for more.\n */\n readonly #dirtyPartitions = new Map<string, DirtyPartitionState>();\n\n #output: Output = throwOutput;\n\n setTakeGate(gate: TakeGate): void {\n this.#takeGate = gate;\n }\n\n constructor(\n input: Input,\n storage: Storage,\n limit: number,\n partitionKey?: PartitionKey,\n ) {\n assert(limit >= 0, 'Limit must be non-negative');\n const {sort} = input.getSchema();\n assert(sort !== undefined, 'Take requires sorted input');\n assertOrderingIncludesPK(sort, input.getSchema().primaryKey);\n input.setOutput(this);\n this.#input = input;\n this.#storage = storage as TakeStorage;\n this.#limit = limit;\n this.#partitionKey = partitionKey;\n this.#partitionKeyComparator =\n partitionKey && makePartitionKeyComparator(partitionKey);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n getBound(constraint?: Constraint): Row | undefined {\n if (\n this.#partitionKey &&\n !constraintContainsPartitionKey(constraint, this.#partitionKey)\n ) {\n return undefined;\n }\n const takeStateKey = getTakeStateKey(this.#partitionKey, constraint);\n const dirty = this.#dirtyPartitions.get(takeStateKey);\n if (dirty) {\n // While dirty in Phase 1, return gateBound (the bound prior to removals) so\n // upstream operators (e.g. TakeGate) remain capped rather than unbounding during push.\n return dirty.gateBound;\n }\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState || takeState.size < this.#limit) {\n return undefined;\n }\n return takeState.bound;\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n assert(\n !this.#partitionKey ||\n (req.constraint !== undefined &&\n constraintContainsPartitionKey(req.constraint, this.#partitionKey)),\n 'Partitioned take does not allow unpartitioned fetches',\n );\n\n const takeStateKey = getTakeStateKey(this.#partitionKey, req.constraint);\n const takeState = this.#storage.get(takeStateKey);\n if (!takeState) {\n if (constraintMatchesPartitionKey(req.constraint, this.#partitionKey)) {\n yield* this.#initialFetch(req, takeStateKey);\n }\n return;\n }\n if (takeState.bound === undefined) {\n return;\n }\n let count = 0;\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield inputNode;\n continue;\n }\n if (this.getSchema().compareRows(takeState.bound, inputNode.row) < 0) {\n return;\n }\n if (\n this.#rowHiddenFromFetch &&\n this.getSchema().compareRows(\n this.#rowHiddenFromFetch,\n inputNode.row,\n ) === 0\n ) {\n continue;\n }\n yield inputNode;\n count++;\n if (count >= this.#limit) {\n return;\n }\n }\n }\n\n *#initialFetch(\n req: FetchRequest,\n takeStateKey: string,\n ): Stream<Node | 'yield'> {\n assert(req.start === undefined, 'Start should be undefined');\n assert(!req.reverse, 'Reverse should be false');\n\n if (this.#limit === 0) {\n return;\n }\n\n assert(\n constraintMatchesPartitionKey(req.constraint, this.#partitionKey),\n 'Constraint should match partition key',\n );\n\n // The caller (fetch) has already checked that there is no state for\n // takeStateKey.\n\n let size = 0;\n let bound: Row | undefined;\n let downstreamEarlyReturn = true;\n let exceptionThrown = false;\n try {\n for (const inputNode of this.#input.fetch(req)) {\n if (inputNode === 'yield') {\n yield 'yield';\n continue;\n }\n yield inputNode;\n bound = inputNode.row;\n size++;\n if (size === this.#limit) {\n break;\n }\n }\n downstreamEarlyReturn = false;\n } catch (e) {\n exceptionThrown = true;\n throw e;\n } finally {\n if (!exceptionThrown) {\n this.#setTakeState(takeStateKey, size, bound);\n // If it becomes necessary to support downstream early return, this\n // assert should be removed, and replaced with code that consumes\n // the input stream until limit is reached or the input stream is\n // exhausted so that takeState is properly hydrated.\n assert(\n !downstreamEarlyReturn,\n 'Unexpected early return prevented full hydration',\n );\n }\n }\n }\n\n #getStateAndConstraint(row: Row) {\n const takeStateKey = getTakeStateKey(this.#partitionKey, row);\n const takeState = this.#storage.get(takeStateKey);\n let constraint: Constraint | undefined;\n if (takeState) {\n constraint =\n this.#partitionKey &&\n Object.fromEntries(\n this.#partitionKey.map(key => [key, row[key]] as const),\n );\n }\n\n return {takeState, takeStateKey, constraint} as\n | {\n takeState: undefined;\n takeStateKey: string;\n constraint: undefined;\n }\n | {\n takeState: TakeState;\n takeStateKey: string;\n constraint: Constraint | undefined;\n };\n }\n\n *push(change: Change): Stream<'yield'> {\n if (change[ChangeIndex.TYPE] === ChangeType.EDIT) {\n yield* this.#pushEditChange(change);\n return;\n }\n\n const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint(\n change[ChangeIndex.NODE].row,\n );\n if (!takeState) {\n return;\n }\n\n const {compareRows} = this.getSchema();\n\n if (change[ChangeIndex.TYPE] === ChangeType.ADD) {\n if (takeState.size < this.#limit) {\n if (this.#dirtyPartitions.has(takeStateKey)) {\n // While dirty with an unresolved deficit in Phase 1, only admit rows that\n // sort strictly before the current window boundary. We refuse to expand the\n // bound forward during Phase 1 so that Phase 2 can backfill earlier candidate\n // rows from storage in sort order.\n if (\n takeState.bound === undefined ||\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0\n ) {\n return;\n }\n }\n const nextSize = takeState.size + 1;\n if (nextSize === this.#limit) {\n this.#dirtyPartitions.delete(takeStateKey);\n }\n this.#setTakeState(\n takeStateKey,\n nextSize,\n takeState.bound === undefined ||\n compareRows(takeState.bound, change[ChangeIndex.NODE].row) < 0\n ? change[ChangeIndex.NODE].row\n : takeState.bound,\n );\n yield* this.#output.push(change, this);\n return;\n }\n // size === limit\n if (\n takeState.bound === undefined ||\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) >= 0\n ) {\n return;\n }\n // added row < bound\n let beforeBoundNode: Node | undefined;\n let boundNode: Node | undefined;\n if (this.#limit === 1) {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n boundNode = node;\n break;\n }\n } else {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n } else if (boundNode === undefined) {\n boundNode = node;\n } else {\n beforeBoundNode = node;\n break;\n }\n }\n }\n assert(\n boundNode !== undefined,\n 'Take: boundNode must be found during fetch',\n );\n const removeChange = makeRemoveChange(boundNode);\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n beforeBoundNode === undefined ||\n compareRows(change[ChangeIndex.NODE].row, beforeBoundNode.row) > 0\n ? change[ChangeIndex.NODE].row\n : beforeBoundNode.row,\n );\n yield* this.#pushWithRowHiddenFromFetch(\n change[ChangeIndex.NODE].row,\n removeChange,\n );\n yield* this.#output.push(change, this);\n } else if (change[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n if (takeState.bound === undefined) {\n // change is after bound\n return;\n }\n const compToBound = compareRows(\n change[ChangeIndex.NODE].row,\n takeState.bound,\n );\n if (compToBound > 0 || (compToBound < 0 && this.#limit === 1)) {\n // change is not in window\n return;\n }\n let beforeBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n beforeBoundNode = node;\n break;\n }\n\n const finalBound =\n takeState.size - 1 === 0\n ? undefined\n : compToBound < 0\n ? takeState.bound\n : beforeBoundNode?.row;\n if (!this.#dirtyPartitions.has(takeStateKey)) {\n // Snapshot the pre-removal bound as the gate bound on the first removal that\n // dirties this partition. Subsequent getBound() calls throughout Phase 1 will return\n // this bound to keep upstream TakeGate bounded until Phase 2 refills deficits.\n this.#dirtyPartitions.set(takeStateKey, {\n constraint,\n gateBound: takeState.bound,\n });\n }\n this.#setTakeState(takeStateKey, takeState.size - 1, finalBound);\n yield* this.#output.push(change, this);\n } else if (change[ChangeIndex.TYPE] === ChangeType.CHILD) {\n // A 'child' change should be pushed to output if its row\n // is <= bound.\n if (\n takeState.bound &&\n compareRows(change[ChangeIndex.NODE].row, takeState.bound) <= 0\n ) {\n yield* this.#output.push(change, this);\n }\n }\n }\n\n *#pushEditChange(change: EditChange): Stream<'yield'> {\n assert(\n !this.#partitionKeyComparator ||\n this.#partitionKeyComparator(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n ) === 0,\n 'Unexpected change of partition key',\n );\n\n const {takeState, takeStateKey, constraint} = this.#getStateAndConstraint(\n change[ChangeIndex.OLD_NODE].row,\n );\n if (!takeState) {\n return;\n }\n\n assert(takeState.bound, 'Bound should be set');\n const {compareRows} = this.getSchema();\n const oldCmp = compareRows(\n change[ChangeIndex.OLD_NODE].row,\n takeState.bound,\n );\n const newCmp = compareRows(change[ChangeIndex.NODE].row, takeState.bound);\n\n const replaceBoundAndForwardChange = () => {\n this.#setTakeState(\n takeStateKey,\n takeState.size,\n change[ChangeIndex.NODE].row,\n );\n return this.#output.push(change, this);\n };\n\n // The bounds row was changed.\n if (oldCmp === 0) {\n // The new row is the new bound.\n if (newCmp === 0) {\n // no need to update the state since we are keeping the bounds\n yield* this.#output.push(change, this);\n return;\n }\n\n if (newCmp < 0) {\n if (this.#limit === 1) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n // New row will be in the result but it might not be the bounds any\n // more. We need to find the row before the bounds to determine the new\n // bounds.\n\n let beforeBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n beforeBoundNode = node;\n break;\n }\n assert(\n beforeBoundNode !== undefined,\n 'Take: beforeBoundNode must be found during fetch',\n );\n\n this.#setTakeState(takeStateKey, takeState.size, beforeBoundNode.row);\n yield* this.#output.push(change, this);\n return;\n }\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n // Find the first item at the old bounds. This will be the new bounds.\n let newBoundNode: Node | undefined;\n this.#takeGate?.open();\n try {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n newBoundNode = node;\n break;\n }\n } finally {\n this.#takeGate?.close();\n }\n assert(\n newBoundNode !== undefined,\n 'Take: newBoundNode must be found during fetch',\n );\n\n // The next row is the new row. We can replace the bounds and keep the\n // edit change.\n if (compareRows(newBoundNode.row, change[ChangeIndex.NODE].row) === 0) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n // The new row is now outside the bounds, so we need to remove the old\n // row and add the new bounds row.\n this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);\n yield* this.#pushWithRowHiddenFromFetch(\n newBoundNode.row,\n makeRemoveChange(change[ChangeIndex.OLD_NODE]),\n );\n yield* this.#output.push(makeAddChange(newBoundNode), this);\n return;\n }\n\n if (oldCmp > 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new outside of bounds\n if (newCmp > 0) {\n return;\n }\n\n // old was outside, new is inside. Pushing out the old bounds\n assert(newCmp < 0, 'New comparison must be less than 0');\n\n let oldBoundNode: Node | undefined;\n let newBoundNode: Node | undefined;\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'at',\n },\n constraint,\n reverse: true,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n } else if (oldBoundNode === undefined) {\n oldBoundNode = node;\n } else {\n newBoundNode = node;\n break;\n }\n }\n assert(\n oldBoundNode !== undefined,\n 'Take: oldBoundNode must be found during fetch',\n );\n assert(\n newBoundNode !== undefined,\n 'Take: newBoundNode must be found during fetch',\n );\n\n // Remove before add to maintain invariant that\n // output size <= limit.\n this.#setTakeState(takeStateKey, takeState.size, newBoundNode.row);\n yield* this.#pushWithRowHiddenFromFetch(\n change[ChangeIndex.NODE].row,\n makeRemoveChange(oldBoundNode),\n );\n yield* this.#output.push(makeAddChange(change[ChangeIndex.NODE]), this);\n\n return;\n }\n\n if (oldCmp < 0) {\n assert(newCmp !== 0, 'Invalid state. Row has duplicate primary key');\n\n // Both old and new inside of bounds\n if (newCmp < 0) {\n yield* this.#output.push(change, this);\n return;\n }\n\n // old was inside, new is larger than old bound\n\n assert(newCmp > 0, 'New comparison must be greater than 0');\n\n // at this point we need to find the row after the bound and use that or\n // the newRow as the new bound.\n let afterBoundNode: Node | undefined;\n this.#takeGate?.open();\n try {\n for (const node of this.#input.fetch({\n start: {\n row: takeState.bound,\n basis: 'after',\n },\n constraint,\n })) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n afterBoundNode = node;\n break;\n }\n } finally {\n this.#takeGate?.close();\n }\n assert(\n afterBoundNode !== undefined,\n 'Take: afterBoundNode must be found during fetch',\n );\n\n // The new row is the new bound. Use an edit change.\n if (compareRows(afterBoundNode.row, change[ChangeIndex.NODE].row) === 0) {\n yield* replaceBoundAndForwardChange();\n return;\n }\n\n yield* this.#output.push(\n makeRemoveChange(change[ChangeIndex.OLD_NODE]),\n this,\n );\n this.#setTakeState(takeStateKey, takeState.size, afterBoundNode.row);\n yield* this.#output.push(makeAddChange(afterBoundNode), this);\n return;\n }\n\n unreachable();\n }\n\n *#pushWithRowHiddenFromFetch(row: Row, change: Change) {\n this.#rowHiddenFromFetch = row;\n try {\n yield* this.#output.push(change, this);\n } finally {\n this.#rowHiddenFromFetch = undefined;\n }\n }\n\n #setTakeState(takeStateKey: string, size: number, bound: Row | undefined) {\n this.#storage.set(takeStateKey, {\n size,\n bound,\n });\n }\n\n destroy(): void {\n this.#input.destroy();\n this.#storage.destroy();\n }\n\n *reconcile(_pusher?: InputBase): Stream<'yield'> {\n if (this.#dirtyPartitions.size > 0) {\n const dirty = [...this.#dirtyPartitions.entries()];\n this.#dirtyPartitions.clear();\n\n for (const [takeStateKey, {constraint}] of dirty) {\n const takeState = this.#storage.get(takeStateKey);\n assert(\n takeState !== undefined,\n 'Take: dirty partition must exist in storage',\n );\n const deficit = this.#limit - takeState.size;\n assert(deficit > 0, 'Take: dirty partition must have a deficit');\n\n const toPush: Node[] = [];\n this.#takeGate?.open();\n try {\n const stream = this.#input.fetch({\n start: takeState.bound\n ? {\n row: takeState.bound,\n basis: 'after',\n }\n : undefined,\n constraint,\n });\n\n for (const node of stream) {\n if (node === 'yield') {\n yield 'yield';\n continue;\n }\n toPush.push(node);\n if (toPush.length === deficit) {\n break;\n }\n }\n } finally {\n this.#takeGate?.close();\n }\n\n let currentSize = takeState.size;\n for (const node of toPush) {\n currentSize++;\n this.#setTakeState(takeStateKey, currentSize, node.row);\n yield* this.#output.push(makeAddChange(node), this);\n }\n }\n }\n\n if (this.#output.reconcile) {\n yield* this.#output.reconcile(this);\n }\n }\n}\n\nfunction getTakeStateKey(\n partitionKey: PartitionKey | undefined,\n rowOrConstraint: Row | Constraint | undefined,\n): string {\n if (partitionKey?.length === 1 && rowOrConstraint) {\n return 'take:' + valueToKey(rowOrConstraint[partitionKey[0]]);\n }\n // The order must be consistent. We always use the order as defined by the\n // partition key.\n const partitionValues: Value[] = [];\n\n if (partitionKey && rowOrConstraint) {\n for (const key of partitionKey) {\n partitionValues.push(rowOrConstraint[key]);\n }\n }\n\n return JSON.stringify(['take', ...partitionValues]);\n}\n\nexport function constraintMatchesPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return constraint === partitionKey;\n }\n if (partitionKey.length !== Object.keys(constraint).length) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nexport function constraintContainsPartitionKey(\n constraint: Constraint | undefined,\n partitionKey: PartitionKey | undefined,\n): boolean {\n if (constraint === undefined || partitionKey === undefined) {\n return false;\n }\n for (const key of partitionKey) {\n if (!hasOwn(constraint, key)) {\n return false;\n }\n }\n return true;\n}\n\nexport function makePartitionKeyComparator(\n partitionKey: PartitionKey,\n): Comparator {\n return (a, b) => {\n for (const key of partitionKey) {\n const cmp = compareValues(a[key], b[key]);\n if (cmp !== 0) {\n return cmp;\n }\n }\n return 0;\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAmEA,IAAa,OAAb,MAAyD;CACvD;CACA;CACA;CACA;CACA;CAEA;CAEA;;;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MAAM,OAAO,cAChB,IAAI,CAAC,OAAO,YAAY,GAAG,GACzB,OAAO;CAGX,OAAO;AACT;AAEA,SAAgB,2BACd,cACY;CACZ,QAAQ,GAAG,MAAM;EACf,KAAK,MAAM,OAAO,cAAc;GAC9B,MAAM,MAAM,cAAc,EAAE,MAAM,EAAE,IAAI;GACxC,IAAI,QAAQ,GACV,OAAO;EAEX;EACA,OAAO;CACT;AACF"}
@@ -2,8 +2,8 @@ import { assert, unreachable } from "../../shared/src/asserts.js";
2
2
  import { getOrInsertComputed } from "../../shared/src/map.js";
3
3
  import { must } from "../../shared/src/must.js";
4
4
  import { assignProperty } from "../../shared/src/objects.js";
5
- import { assertOrderingIncludesPK } from "../../zql/src/query/complete-ordering.js";
6
5
  import { makeComparator } from "../../zql/src/ivm/data.js";
6
+ import { assertOrderingIncludesPK } from "../../zql/src/query/complete-ordering.js";
7
7
  import { createPredicate, transformFilters } from "../../zql/src/builder/filter.js";
8
8
  import { genPushAndWriteWithSplitEdit, generateWithOverlay, generateWithOverlayUnordered, generateWithStart } from "../../zql/src/ivm/memory-source.js";
9
9
  import { compile, format, sql } from "./internal/sql.js";