@rocicorp/zero 1.13.0-canary.3 → 1.13.0-canary.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (80) hide show
  1. package/out/z2s/src/compiler.d.ts.map +1 -1
  2. package/out/z2s/src/compiler.js +6 -6
  3. package/out/z2s/src/compiler.js.map +1 -1
  4. package/out/zero/package.js +1 -1
  5. package/out/zero/package.js.map +1 -1
  6. package/out/zero-cache/src/server/replicator.js +3 -3
  7. package/out/zero-cache/src/server/replicator.js.map +1 -1
  8. package/out/zero-cache/src/services/replicator/write-worker-client.js +17 -3
  9. package/out/zero-cache/src/services/replicator/write-worker-client.js.map +1 -1
  10. package/out/zero-cache/src/services/replicator/write-worker.js +11 -2
  11. package/out/zero-cache/src/services/replicator/write-worker.js.map +1 -1
  12. package/out/zero-cache/src/services/view-syncer/view-syncer.js +13 -15
  13. package/out/zero-cache/src/services/view-syncer/view-syncer.js.map +1 -1
  14. package/out/zero-client/src/client/custom.js +1 -1
  15. package/out/zero-client/src/client/mutation-tracker.js +1 -1
  16. package/out/zero-client/src/client/version.js +1 -1
  17. package/out/zero-client/src/client/zero.js +1 -1
  18. package/out/zero-protocol/src/ast.d.ts +2 -2
  19. package/out/zero-protocol/src/ast.js +2 -2
  20. package/out/zero-protocol/src/ast.js.map +1 -1
  21. package/out/zero-protocol/src/down.d.ts +27 -27
  22. package/out/zero-protocol/src/inspect-down.d.ts +81 -81
  23. package/out/zql/src/ivm/aggregate.js +5 -4
  24. package/out/zql/src/ivm/aggregate.js.map +1 -1
  25. package/out/zql/src/ivm/cap.d.ts.map +1 -1
  26. package/out/zql/src/ivm/cap.js +116 -63
  27. package/out/zql/src/ivm/cap.js.map +1 -1
  28. package/out/zql/src/ivm/deferred-input.js +1 -1
  29. package/out/zql/src/ivm/exists.d.ts.map +1 -1
  30. package/out/zql/src/ivm/exists.js +136 -73
  31. package/out/zql/src/ivm/exists.js.map +1 -1
  32. package/out/zql/src/ivm/fan-in.d.ts.map +1 -1
  33. package/out/zql/src/ivm/fan-in.js +3 -3
  34. package/out/zql/src/ivm/fan-in.js.map +1 -1
  35. package/out/zql/src/ivm/fan-out.d.ts +2 -2
  36. package/out/zql/src/ivm/fan-out.d.ts.map +1 -1
  37. package/out/zql/src/ivm/fan-out.js +47 -12
  38. package/out/zql/src/ivm/fan-out.js.map +1 -1
  39. package/out/zql/src/ivm/filter-operators.d.ts.map +1 -1
  40. package/out/zql/src/ivm/filter-operators.js +98 -13
  41. package/out/zql/src/ivm/filter-operators.js.map +1 -1
  42. package/out/zql/src/ivm/filter-push.d.ts.map +1 -1
  43. package/out/zql/src/ivm/filter-push.js +5 -13
  44. package/out/zql/src/ivm/filter-push.js.map +1 -1
  45. package/out/zql/src/ivm/filter.d.ts.map +1 -1
  46. package/out/zql/src/ivm/filter.js +3 -2
  47. package/out/zql/src/ivm/filter.js.map +1 -1
  48. package/out/zql/src/ivm/flipped-join.js +1 -0
  49. package/out/zql/src/ivm/flipped-join.js.map +1 -1
  50. package/out/zql/src/ivm/group-by.js +1 -1
  51. package/out/zql/src/ivm/join-index-tap.d.ts +1 -1
  52. package/out/zql/src/ivm/join-index-tap.d.ts.map +1 -1
  53. package/out/zql/src/ivm/join-index-tap.js +9 -7
  54. package/out/zql/src/ivm/join-index-tap.js.map +1 -1
  55. package/out/zql/src/ivm/join.d.ts.map +1 -1
  56. package/out/zql/src/ivm/join.js +72 -67
  57. package/out/zql/src/ivm/join.js.map +1 -1
  58. package/out/zql/src/ivm/memory-source.d.ts.map +1 -1
  59. package/out/zql/src/ivm/memory-source.js +163 -57
  60. package/out/zql/src/ivm/memory-source.js.map +1 -1
  61. package/out/zql/src/ivm/push-accumulated.d.ts +1 -1
  62. package/out/zql/src/ivm/push-accumulated.d.ts.map +1 -1
  63. package/out/zql/src/ivm/push-accumulated.js +23 -28
  64. package/out/zql/src/ivm/push-accumulated.js.map +1 -1
  65. package/out/zql/src/ivm/skip.d.ts.map +1 -1
  66. package/out/zql/src/ivm/skip.js +6 -9
  67. package/out/zql/src/ivm/skip.js.map +1 -1
  68. package/out/zql/src/ivm/stream.d.ts +58 -0
  69. package/out/zql/src/ivm/stream.d.ts.map +1 -1
  70. package/out/zql/src/ivm/stream.js +256 -1
  71. package/out/zql/src/ivm/stream.js.map +1 -1
  72. package/out/zql/src/ivm/take-gate.d.ts +1 -1
  73. package/out/zql/src/ivm/take-gate.d.ts.map +1 -1
  74. package/out/zql/src/ivm/take-gate.js +11 -21
  75. package/out/zql/src/ivm/take-gate.js.map +1 -1
  76. package/out/zql/src/ivm/take.d.ts.map +1 -1
  77. package/out/zql/src/ivm/take.js +243 -217
  78. package/out/zql/src/ivm/take.js.map +1 -1
  79. package/out/zql/src/query/query.d.ts +4 -4
  80. package/package.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"flipped-join.js","names":[],"sources":["../../../../../zql/src/ivm/flipped-join.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {binarySearch} from '../../../shared/src/binary-search.ts';\nimport {assignProperty} from '../../../shared/src/objects.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 {constraintsAreCompatible, type Constraint} from './constraint.ts';\nimport type {Node} from './data.ts';\nimport {JoinIndex} from './join-index.ts';\nimport {\n buildJoinConstraint,\n canonicalKey,\n canonicalKeyForTest,\n generateWithOverlayNoYield,\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 MultiConstraint,\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\n/**\n * Maximum number of entries sent in a single batched `parent.fetch`\n * call. Larger child-node sets are split into multiple fetches whose\n * sorted streams are merged in JS.\n *\n * Why bound this:\n * - **Bounded fetch on early termination.** `mergeSortedStreams` primes\n * one row from every chunk before yielding the first output, so all\n * chunks open their cursors up front. Smaller chunks cap the\n * worst-case overfetch when downstream `Take` consumes only a few\n * rows — at chunk N, we may waste up to ~N index seeks before\n * `.return()` propagates.\n * - **Parameter limit.** Well under SQLite's default\n * `SQLITE_MAX_VARIABLE_NUMBER` (32766). Compound keys multiply the\n * parameter count by key length, so we leave headroom.\n *\n * Tested 64/128/256; 256 had the best worst-case across planner suites\n * and perf tests.\n *\n * We should, however, start doing shadow tests of the planner\n * against cloudzero queries.\n */\nconst MULTI_CONSTRAINT_CHUNK_SIZE = 256;\n\n// Mutable test seam — production code reads this via the getter.\nlet multiConstraintChunkSize: number = MULTI_CONSTRAINT_CHUNK_SIZE;\n\nexport function getMultiConstraintChunkSize(): number {\n return multiConstraintChunkSize;\n}\n\n/** Test only. Returns a restore function. */\nexport function setMultiConstraintChunkSizeForTest(size: number): () => void {\n const prev = multiConstraintChunkSize;\n multiConstraintChunkSize = size;\n return () => {\n multiConstraintChunkSize = prev;\n };\n}\n\ntype Args = {\n parent: Input;\n child: Input;\n // The nth key in childKey corresponds to the nth key in parentKey.\n parentKey: CompoundKey;\n childKey: CompoundKey;\n\n relationshipName: string;\n hidden: boolean;\n system: System;\n parentPartitionKey?: CompoundKey | undefined;\n boundProvider?: TakeBoundProvider | undefined;\n parentIndex?: JoinIndex | undefined;\n};\n\n/**\n * An *inner* join which fetches nodes from its child input first and then\n * fetches their related nodes from its parent input. Output nodes are the\n * nodes from parent input (in parent input order), which have at least one\n * related child. These output nodes have a new relationship added to them,\n * which has the name `relationshipName`. The value of the relationship is a\n * stream of related nodes from the child input (in child input order).\n */\nexport class FlippedJoin 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 #boundProvider: TakeBoundProvider | undefined;\n /**\n * The parents this join has output: those with at least one related\n * child.\n */\n readonly #parentIndex: JoinIndex;\n readonly #indexesParents: boolean;\n\n #output: Output = throwOutput;\n\n #inprogressChildChange: Change | undefined;\n #inprogressChildChangePosition: Row | 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 parentIndex,\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 this.#parent = parent;\n this.#child = child;\n this.#parentKey = parentKey;\n this.#childKey = childKey;\n this.#relationshipName = relationshipName;\n this.#boundProvider = boundProvider;\n if (parentIndex) {\n this.#parentIndex = parentIndex;\n this.#indexesParents = false;\n } else {\n this.#parentIndex = new JoinIndex(\n parentKey,\n parent.getSchema().primaryKey,\n parentPartitionKey,\n );\n this.#indexesParents = true;\n }\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.#child.destroy();\n this.#parent.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 // Translate constraints for the parent on parts of the join key to\n // constraints for the child.\n const childConstraint: Record<string, Value> = {};\n let hasChildConstraint = false;\n if (req.constraint) {\n for (const [key, value] of Object.entries(req.constraint)) {\n const index = this.#parentKey.indexOf(key);\n if (index !== -1) {\n hasChildConstraint = true;\n assignProperty(childConstraint, this.#childKey[index], value);\n }\n }\n }\n\n const childNodes: Node[] = [];\n for (const node of this.#child.fetch(\n hasChildConstraint ? {constraint: childConstraint} : {},\n )) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n childNodes.push(node);\n }\n\n // FlippedJoin's split-push change overlay logic is largely\n // the same as Join's with the exception of remove. For remove,\n // the change is undone here, and then re-applied to parents with order\n // less than or equal to change.position below. This is necessary\n // because if the removed node was the last related child, the\n // related parents with position greater than change.position\n // (which should not yet have the node removed), would not even\n // be fetched here, and would be absent from the output all together.\n if (this.#inprogressChildChange?.[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n const removedNode = this.#inprogressChildChange[ChangeIndex.NODE];\n const compare = this.#child.getSchema().compareRows;\n const insertPos = binarySearch(childNodes.length, i =>\n compare(removedNode.row, childNodes[i].row),\n );\n childNodes.splice(insertPos, 0, removedNode);\n }\n\n yield* this.#fetchBatched(req, childNodes);\n }\n\n /**\n * Fetches parents for `childNodes` in batched calls, using\n * `multiConstraint` so the source can issue one query per chunk (e.g.\n * SQL `IN` with index-aware seek) instead of N per-child cursors.\n *\n * Multi-constraint values are split into chunks of `CHUNK_SIZE`, so\n * SQL `IN` lists stay bounded — predictable plans, statement-cache\n * hits across calls of the same chunk size, well below SQLite's\n * parameter limit.\n *\n * Within each chunk, the source returns parents in `compareRows` order.\n * Across chunks, we merge with `mergeSortedStreams` so the overall\n * stream is also in order. Note: the merge primes one row from every\n * chunk before yielding the first output, so all chunks open their\n * cursors up front. Early termination downstream then prevents any\n * further work on un-advanced chunks (cursors get `.return()`'d via\n * `mergeSortedStreams`'s finally block).\n *\n * Replaces the previous split between `#fetchMergeSort` and\n * `#fetchQuicksort`. The unique-vs-not distinction is no longer needed:\n * the source handles cardinality (single index seek for each value) and\n * ordering (SQL `ORDER BY` / index walk).\n */\n *#fetchBatched(\n req: FetchRequest,\n childNodes: Node[],\n ): Stream<Node | 'yield'> {\n const parentReqConstraint = req.constraint;\n const parentKey = this.#parentKey;\n const childKey = this.#childKey;\n\n // Build (deduped) multi-constraint and a key→child-indexes map. Same\n // parent-key value across multiple children groups them together.\n const computedMulti: Constraint[] = [];\n const childIndexesByKey = new Map<string, number[]>();\n for (let i = 0; i < childNodes.length; i++) {\n const constraintFromChild = buildJoinConstraint(\n childNodes[i].row,\n childKey,\n parentKey,\n );\n if (\n !constraintFromChild ||\n (parentReqConstraint &&\n !constraintsAreCompatible(constraintFromChild, parentReqConstraint))\n ) {\n continue;\n }\n const key = canonicalKey(constraintFromChild, parentKey);\n const existing = childIndexesByKey.get(key);\n if (existing === undefined) {\n childIndexesByKey.set(key, [i]);\n computedMulti.push(constraintFromChild);\n } else {\n existing.push(i);\n }\n }\n\n if (computedMulti.length === 0) {\n return;\n }\n\n // Source returns parents in compareRows order within each chunk.\n // Merge across chunks to yield a globally ordered stream.\n const compareRows = this.#schema.compareRows;\n const compare: (a: Node, b: Node) => number = req.reverse\n ? (a, b) => compareRows(b.row, a.row)\n : (a, b) => compareRows(a.row, b.row);\n\n // Append our computed multi to whatever req.multiConstraints already\n // contained — chained FlippedJoins each contribute one entry, so the\n // source ANDs them all (e.g. `assigneeID IN (…) AND creatorID IN (…)`).\n const incoming = req.multiConstraints ?? [];\n const parentStream =\n computedMulti.length <= multiConstraintChunkSize\n ? this.#parent.fetch({\n ...req,\n multiConstraints: [...incoming, computedMulti],\n })\n : this.#fetchChunked(req, incoming, computedMulti, compare);\n\n for (const node of parentStream) {\n if (node === 'yield') {\n yield 'yield';\n continue;\n }\n const key = canonicalKey(node.row, parentKey);\n const idxs = childIndexesByKey.get(key);\n if (idxs === undefined) {\n // This row's parent-key doesn't match any of our computed\n // multi-constraint entries. Happens when our parent is an\n // intermediate operator (e.g. a chained FlippedJoin) that passes\n // multiConstraints through unchanged instead of filtering — see\n // FetchRequest.multiConstraints contract. The lookup miss here\n // performs the required filter, so just skip the row.\n continue;\n }\n // Children retain their original input order within the group\n // because we appended to `idxs` in iteration order.\n const relatedChildNodes: Node[] = idxs.map(i => childNodes[i]);\n yield* this.#yieldParentWithOverlay(node, relatedChildNodes);\n }\n }\n\n #fetchChunked(\n req: FetchRequest,\n incomingMultis: readonly MultiConstraint[],\n computedMulti: MultiConstraint,\n compare: (a: Node, b: Node) => number,\n ): Stream<Node | 'yield'> {\n const chunkStreams: Stream<Node | 'yield'>[] = [];\n for (let i = 0; i < computedMulti.length; i += multiConstraintChunkSize) {\n chunkStreams.push(\n this.#parent.fetch({\n ...req,\n multiConstraints: [\n ...incomingMultis,\n computedMulti.slice(i, i + multiConstraintChunkSize),\n ],\n }),\n );\n }\n return mergeSortedStreams(chunkStreams, compare);\n }\n\n *#yieldParentWithOverlay(\n minParentNode: Node,\n relatedChildNodes: Node[],\n ): Stream<Node> {\n let overlaidRelatedChildNodes = relatedChildNodes;\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 {compareRows} = this.#parent.getSchema();\n const isParentInPushQueue =\n this.#inprogressChildChangePosition !== undefined &&\n compareRows(minParentNode.row, this.#inprogressChildChangePosition) > 0 &&\n (this.#inprogressParentFetchBounds === undefined ||\n isInParentFetch(\n this.#inprogressParentFetchBounds,\n minParentNode.row,\n compareRows,\n ));\n\n if (\n this.#inprogressChildChange &&\n this.#inprogressChildChangePosition &&\n isJoinMatch(\n this.#inprogressChildChange[ChangeIndex.NODE].row,\n this.#childKey,\n minParentNode.row,\n this.#parentKey,\n )\n ) {\n if (this.#inprogressChildChange[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n if (!isParentInPushQueue) {\n // Remove from relatedChildNodes since the removed child\n // was inserted into childNodes above.\n overlaidRelatedChildNodes = relatedChildNodes.filter(\n n => n !== this.#inprogressChildChange?.[ChangeIndex.NODE],\n );\n }\n } else if (isParentInPushQueue) {\n overlaidRelatedChildNodes = [\n ...generateWithOverlayNoYield(\n relatedChildNodes,\n this.#inprogressChildChange,\n this.#child.getSchema(),\n ),\n ];\n }\n }\n\n // yield node if after the overlay it still has relationship nodes\n if (overlaidRelatedChildNodes.length > 0) {\n this.#indexParentRow(minParentNode.row);\n yield {\n ...minParentNode,\n relationships: {\n ...minParentNode.relationships,\n [this.#relationshipName]: () => overlaidRelatedChildNodes,\n },\n };\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);\n break;\n case ChangeType.EDIT: {\n assert(\n rowEqualsForCompoundKey(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n this.#childKey,\n ),\n `Child edit must not change relationship.`,\n );\n yield* this.#pushChildChange(change, true);\n break;\n }\n case ChangeType.CHILD:\n yield* this.#pushChildChange(change, true);\n break;\n }\n }\n\n *#pushChildChange(change: Change, exists?: boolean): Stream<'yield'> {\n this.#inprogressChildChange = change;\n this.#inprogressChildChangePosition = undefined;\n try {\n const constraint = buildJoinConstraint(\n change[ChangeIndex.NODE].row,\n this.#childKey,\n this.#parentKey,\n );\n if (!constraint) {\n return;\n }\n const childRow = change[ChangeIndex.NODE].row;\n const changeType = change[ChangeIndex.TYPE];\n\n const partitions = this.#parentIndex.lookup(childRow, this.#childKey);\n if (!partitions) {\n // If no matching parent is resident in the view, REMOVE and EDIT\n // cannot affect any view-resident parent. (ADD and CHILD can qualify\n // previously absent parents).\n if (changeType !== ChangeType.ADD && changeType !== ChangeType.CHILD) {\n return;\n }\n }\n\n let fetchConstraints: Constraint[];\n if (changeType !== ChangeType.ADD && changeType !== ChangeType.CHILD) {\n assert(\n partitions,\n 'Matching entries must exist for non-add child change',\n );\n fetchConstraints = partitions.map(partitionConstraint =>\n partitionConstraint\n ? {...constraint, ...partitionConstraint}\n : constraint,\n );\n } else {\n fetchConstraints = [constraint];\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 'yield';\n continue;\n }\n this.#inprogressChildChange = change;\n this.#inprogressChildChangePosition = parentNode.row;\n const childNodeStream = () => {\n const constraint = buildJoinConstraint(\n parentNode.row,\n this.#parentKey,\n this.#childKey,\n );\n return constraint ? this.#child.fetch({constraint}) : [];\n };\n let parentExists = exists;\n if (!parentExists) {\n for (const childNode of childNodeStream()) {\n if (childNode === 'yield') {\n yield 'yield';\n continue;\n }\n if (\n this.#child\n .getSchema()\n .compareRows(childNode.row, change[ChangeIndex.NODE].row) !== 0\n ) {\n parentExists = true;\n break;\n }\n }\n }\n if (parentExists) {\n yield* this.#output.push(\n makeChildChange(\n {\n ...parentNode,\n relationships: {\n ...parentNode.relationships,\n [this.#relationshipName]: childNodeStream,\n },\n },\n {\n relationshipName: this.#relationshipName,\n change,\n },\n ),\n this,\n );\n } else {\n const newNode = {\n ...parentNode,\n relationships: {\n ...parentNode.relationships,\n [this.#relationshipName]: () => [change[ChangeIndex.NODE]],\n },\n };\n if (change[ChangeIndex.TYPE] === ChangeType.ADD) {\n this.#indexParentRow(parentNode.row);\n yield* this.#output.push(makeAddChange(newNode), this);\n } else {\n this.#unindexParentRow(parentNode.row);\n yield* this.#output.push(makeRemoveChange(newNode), this);\n }\n }\n }\n } finally {\n this.#inprogressChildChange = undefined;\n this.#inprogressChildChangePosition = undefined;\n this.#inprogressParentFetchBounds = undefined;\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 *#pushParent(change: Change): Stream<'yield'> {\n const childNodeStream = (node: Node) => () => {\n const constraint = buildJoinConstraint(\n node.row,\n this.#parentKey,\n this.#childKey,\n );\n return constraint ? this.#child.fetch({constraint}) : [];\n };\n\n const flip = (node: Node) => ({\n ...node,\n relationships: {\n ...node.relationships,\n [this.#relationshipName]: childNodeStream(node),\n },\n });\n\n // If no related child don't push as this is an inner join.\n let hasRelatedChild = false;\n for (const node of childNodeStream(change[ChangeIndex.NODE])()) {\n if (node === 'yield') {\n yield 'yield';\n continue;\n } else {\n hasRelatedChild = true;\n break;\n }\n }\n if (!hasRelatedChild) {\n return;\n }\n\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n this.#indexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeAddChange(flip(change[ChangeIndex.NODE])),\n this,\n );\n break;\n case ChangeType.REMOVE:\n this.#unindexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeRemoveChange(flip(change[ChangeIndex.NODE])),\n this,\n );\n break;\n case ChangeType.CHILD: {\n yield* this.#output.push(\n makeChildChange(\n flip(change[ChangeIndex.NODE]),\n change[ChangeIndex.CHILD_DATA],\n ),\n this,\n );\n break;\n }\n case ChangeType.EDIT: {\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 flip(change[ChangeIndex.NODE]),\n flip(change[ChangeIndex.OLD_NODE]),\n ),\n this,\n );\n break;\n }\n default:\n unreachable(change);\n }\n }\n}\n\nexport {canonicalKeyForTest};\n"],"mappings":";;;;;;;;;;;AAmEA,IAAI,2BAAmC;AAEvC,SAAgB,8BAAsC;CACpD,OAAO;AACT;;;;;;;;;AAkCA,IAAa,cAAb,MAA0C;CACxC;CACA;CACA;CACA;CACA;CACA;CACA;;;;;CAKA;CACA;CAEA,UAAkB;CAElB;CACA;CACA;CAEA,YAAY,EACV,QACA,OACA,WACA,UACA,kBACA,QACA,QACA,oBACA,eACA,eACO;EACP,OAAO,WAAW,OAAO,8CAA8C;EACvE,OACE,UAAU,WAAW,SAAS,QAC9B,uDACF;EACA,KAAK,UAAU;EACf,KAAK,SAAS;EACd,KAAK,aAAa;EAClB,KAAK,YAAY;EACjB,KAAK,oBAAoB;EACzB,KAAK,iBAAiB;EACtB,IAAI,aAAa;GACf,KAAK,eAAe;GACpB,KAAK,kBAAkB;EACzB,OAAO;GACL,KAAK,eAAe,IAAI,UACtB,WACA,OAAO,UAAU,CAAC,CAAC,YACnB,kBACF;GACA,KAAK,kBAAkB;EACzB;EAEA,MAAM,eAAe,OAAO,UAAU;EACtC,MAAM,cAAc,MAAM,UAAU;EACpC,KAAK,UAAU;GACb,GAAG;GACH,eAAe;IACb,GAAG,aAAa;KACf,mBAAmB;KAClB,GAAG;KACH,UAAU;KACV;IACF;GACF;EACF;EAEA,OAAO,UAAU;GACf,OAAO,WAAmB,KAAK,YAAY,MAAM;GACjD,iBAAiB,KAAK,WAAW;EACnC,CAAC;EACD,MAAM,UAAU;GACd,OAAO,WAAmB,KAAK,WAAW,MAAM;GAChD,iBAAiB,KAAK,WAAW;EACnC,CAAC;CACH;CAEA,CAAC,aAA8B;EAC7B,IAAI,KAAK,QAAQ,WACf,OAAO,KAAK,QAAQ,UAAU,IAAI;CAEtC;CAEA,UAAgB;EACd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AiB,CAAC,CAAC,GAC3D,IAAI,SAAS,SAAS;GACpB,MAAM;GACN;EACF,OAAO;GACL,kBAAkB;GAClB;EACF;EAEF,IAAI,CAAC,iBACH;EAGF,QAAQ,OAAO,IAAf;GACE,KAAK;IACH,KAAK,gBAAgB,OAAO,EAAiB,CAAC,GAAG;IACjD,OAAO,KAAK,QAAQ,KAClB,cAAc,KAAK,OAAO,EAAiB,CAAC,GAC5C,IACF;IACA;GACF,KAAK;IACH,KAAK,kBAAkB,OAAO,EAAiB,CAAC,GAAG;IACnD,OAAO,KAAK,QAAQ,KAClB,iBAAiB,KAAK,OAAO,EAAiB,CAAC,GAC/C,IACF;IACA;GACF,KAAK;IACH,OAAO,KAAK,QAAQ,KAClB,gBACE,KAAK,OAAO,EAAiB,GAC7B,OAAO,EACT,GACA,IACF;IACA;GAEF,KAAK;IACH,OACE,wBACE,OAAO,EAAqB,CAAC,KAC7B,OAAO,EAAiB,CAAC,KACzB,KAAK,UACP,GACA,2CACF;IACA,KAAK,kBAAkB,OAAO,EAAqB,CAAC,GAAG;IACvD,KAAK,gBAAgB,OAAO,EAAiB,CAAC,GAAG;IACjD,OAAO,KAAK,QAAQ,KAClB,eACE,KAAK,OAAO,EAAiB,GAC7B,KAAK,OAAO,EAAqB,CACnC,GACA,IACF;IACA;GAEF,SACE,YAAY,MAAM;EACtB;CACF;AACF"}
1
+ {"version":3,"file":"flipped-join.js","names":[],"sources":["../../../../../zql/src/ivm/flipped-join.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {binarySearch} from '../../../shared/src/binary-search.ts';\nimport {assignProperty} from '../../../shared/src/objects.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 {constraintsAreCompatible, type Constraint} from './constraint.ts';\nimport type {Node} from './data.ts';\nimport {JoinIndex} from './join-index.ts';\nimport {\n buildJoinConstraint,\n canonicalKey,\n canonicalKeyForTest,\n generateWithOverlayNoYield,\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 MultiConstraint,\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\n/**\n * Maximum number of entries sent in a single batched `parent.fetch`\n * call. Larger child-node sets are split into multiple fetches whose\n * sorted streams are merged in JS.\n *\n * Why bound this:\n * - **Bounded fetch on early termination.** `mergeSortedStreams` primes\n * one row from every chunk before yielding the first output, so all\n * chunks open their cursors up front. Smaller chunks cap the\n * worst-case overfetch when downstream `Take` consumes only a few\n * rows — at chunk N, we may waste up to ~N index seeks before\n * `.return()` propagates.\n * - **Parameter limit.** Well under SQLite's default\n * `SQLITE_MAX_VARIABLE_NUMBER` (32766). Compound keys multiply the\n * parameter count by key length, so we leave headroom.\n *\n * Tested 64/128/256; 256 had the best worst-case across planner suites\n * and perf tests.\n *\n * We should, however, start doing shadow tests of the planner\n * against cloudzero queries.\n */\nconst MULTI_CONSTRAINT_CHUNK_SIZE = 256;\n\n// Mutable test seam — production code reads this via the getter.\nlet multiConstraintChunkSize: number = MULTI_CONSTRAINT_CHUNK_SIZE;\n\nexport function getMultiConstraintChunkSize(): number {\n return multiConstraintChunkSize;\n}\n\n/** Test only. Returns a restore function. */\nexport function setMultiConstraintChunkSizeForTest(size: number): () => void {\n const prev = multiConstraintChunkSize;\n multiConstraintChunkSize = size;\n return () => {\n multiConstraintChunkSize = prev;\n };\n}\n\ntype Args = {\n parent: Input;\n child: Input;\n // The nth key in childKey corresponds to the nth key in parentKey.\n parentKey: CompoundKey;\n childKey: CompoundKey;\n\n relationshipName: string;\n hidden: boolean;\n system: System;\n parentPartitionKey?: CompoundKey | undefined;\n boundProvider?: TakeBoundProvider | undefined;\n parentIndex?: JoinIndex | undefined;\n};\n\n/**\n * An *inner* join which fetches nodes from its child input first and then\n * fetches their related nodes from its parent input. Output nodes are the\n * nodes from parent input (in parent input order), which have at least one\n * related child. These output nodes have a new relationship added to them,\n * which has the name `relationshipName`. The value of the relationship is a\n * stream of related nodes from the child input (in child input order).\n */\nexport class FlippedJoin 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 #boundProvider: TakeBoundProvider | undefined;\n /**\n * The parents this join has output: those with at least one related\n * child.\n */\n readonly #parentIndex: JoinIndex;\n readonly #indexesParents: boolean;\n\n #output: Output = throwOutput;\n\n #inprogressChildChange: Change | undefined;\n #inprogressChildChangePosition: Row | 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 parentIndex,\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 this.#parent = parent;\n this.#child = child;\n this.#parentKey = parentKey;\n this.#childKey = childKey;\n this.#relationshipName = relationshipName;\n this.#boundProvider = boundProvider;\n if (parentIndex) {\n this.#parentIndex = parentIndex;\n this.#indexesParents = false;\n } else {\n this.#parentIndex = new JoinIndex(\n parentKey,\n parent.getSchema().primaryKey,\n parentPartitionKey,\n );\n this.#indexesParents = true;\n }\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.#child.destroy();\n this.#parent.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 // Translate constraints for the parent on parts of the join key to\n // constraints for the child.\n const childConstraint: Record<string, Value> = {};\n let hasChildConstraint = false;\n if (req.constraint) {\n for (const [key, value] of Object.entries(req.constraint)) {\n const index = this.#parentKey.indexOf(key);\n if (index !== -1) {\n hasChildConstraint = true;\n assignProperty(childConstraint, this.#childKey[index], value);\n }\n }\n }\n\n const childNodes: Node[] = [];\n for (const node of this.#child.fetch(\n hasChildConstraint ? {constraint: childConstraint} : {},\n )) {\n if (node === 'yield') {\n yield node;\n continue;\n }\n childNodes.push(node);\n }\n\n // FlippedJoin's split-push change overlay logic is largely\n // the same as Join's with the exception of remove. For remove,\n // the change is undone here, and then re-applied to parents with order\n // less than or equal to change.position below. This is necessary\n // because if the removed node was the last related child, the\n // related parents with position greater than change.position\n // (which should not yet have the node removed), would not even\n // be fetched here, and would be absent from the output all together.\n if (this.#inprogressChildChange?.[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n const removedNode = this.#inprogressChildChange[ChangeIndex.NODE];\n const compare = this.#child.getSchema().compareRows;\n const insertPos = binarySearch(childNodes.length, i =>\n compare(removedNode.row, childNodes[i].row),\n );\n childNodes.splice(insertPos, 0, removedNode);\n }\n\n yield* this.#fetchBatched(req, childNodes);\n }\n\n /**\n * Fetches parents for `childNodes` in batched calls, using\n * `multiConstraint` so the source can issue one query per chunk (e.g.\n * SQL `IN` with index-aware seek) instead of N per-child cursors.\n *\n * Multi-constraint values are split into chunks of `CHUNK_SIZE`, so\n * SQL `IN` lists stay bounded — predictable plans, statement-cache\n * hits across calls of the same chunk size, well below SQLite's\n * parameter limit.\n *\n * Within each chunk, the source returns parents in `compareRows` order.\n * Across chunks, we merge with `mergeSortedStreams` so the overall\n * stream is also in order. Note: the merge primes one row from every\n * chunk before yielding the first output, so all chunks open their\n * cursors up front. Early termination downstream then prevents any\n * further work on un-advanced chunks (cursors get `.return()`'d via\n * `mergeSortedStreams`'s finally block).\n *\n * Replaces the previous split between `#fetchMergeSort` and\n * `#fetchQuicksort`. The unique-vs-not distinction is no longer needed:\n * the source handles cardinality (single index seek for each value) and\n * ordering (SQL `ORDER BY` / index walk).\n */\n *#fetchBatched(\n req: FetchRequest,\n childNodes: Node[],\n ): Stream<Node | 'yield'> {\n const parentReqConstraint = req.constraint;\n const parentKey = this.#parentKey;\n const childKey = this.#childKey;\n\n // Build (deduped) multi-constraint and a key→child-indexes map. Same\n // parent-key value across multiple children groups them together.\n const computedMulti: Constraint[] = [];\n const childIndexesByKey = new Map<string, number[]>();\n for (let i = 0; i < childNodes.length; i++) {\n const constraintFromChild = buildJoinConstraint(\n childNodes[i].row,\n childKey,\n parentKey,\n );\n if (\n !constraintFromChild ||\n (parentReqConstraint &&\n !constraintsAreCompatible(constraintFromChild, parentReqConstraint))\n ) {\n continue;\n }\n const key = canonicalKey(constraintFromChild, parentKey);\n const existing = childIndexesByKey.get(key);\n if (existing === undefined) {\n childIndexesByKey.set(key, [i]);\n computedMulti.push(constraintFromChild);\n } else {\n existing.push(i);\n }\n }\n\n if (computedMulti.length === 0) {\n return;\n }\n\n // Source returns parents in compareRows order within each chunk.\n // Merge across chunks to yield a globally ordered stream.\n const compareRows = this.#schema.compareRows;\n const compare: (a: Node, b: Node) => number = req.reverse\n ? (a, b) => compareRows(b.row, a.row)\n : (a, b) => compareRows(a.row, b.row);\n\n // Append our computed multi to whatever req.multiConstraints already\n // contained — chained FlippedJoins each contribute one entry, so the\n // source ANDs them all (e.g. `assigneeID IN (…) AND creatorID IN (…)`).\n const incoming = req.multiConstraints ?? [];\n const parentStream =\n computedMulti.length <= multiConstraintChunkSize\n ? this.#parent.fetch({\n ...req,\n multiConstraints: [...incoming, computedMulti],\n })\n : this.#fetchChunked(req, incoming, computedMulti, compare);\n\n for (const node of parentStream) {\n if (node === 'yield') {\n yield 'yield';\n continue;\n }\n const key = canonicalKey(node.row, parentKey);\n const idxs = childIndexesByKey.get(key);\n if (idxs === undefined) {\n // This row's parent-key doesn't match any of our computed\n // multi-constraint entries. Happens when our parent is an\n // intermediate operator (e.g. a chained FlippedJoin) that passes\n // multiConstraints through unchanged instead of filtering — see\n // FetchRequest.multiConstraints contract. The lookup miss here\n // performs the required filter, so just skip the row.\n continue;\n }\n // Children retain their original input order within the group\n // because we appended to `idxs` in iteration order.\n const relatedChildNodes: Node[] = idxs.map(i => childNodes[i]);\n yield* this.#yieldParentWithOverlay(node, relatedChildNodes);\n }\n }\n\n #fetchChunked(\n req: FetchRequest,\n incomingMultis: readonly MultiConstraint[],\n computedMulti: MultiConstraint,\n compare: (a: Node, b: Node) => number,\n ): Stream<Node | 'yield'> {\n const chunkStreams: Stream<Node | 'yield'>[] = [];\n for (let i = 0; i < computedMulti.length; i += multiConstraintChunkSize) {\n chunkStreams.push(\n this.#parent.fetch({\n ...req,\n multiConstraints: [\n ...incomingMultis,\n computedMulti.slice(i, i + multiConstraintChunkSize),\n ],\n }),\n );\n }\n return mergeSortedStreams(chunkStreams, compare);\n }\n\n *#yieldParentWithOverlay(\n minParentNode: Node,\n relatedChildNodes: Node[],\n ): Stream<Node> {\n let overlaidRelatedChildNodes = relatedChildNodes;\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 {compareRows} = this.#parent.getSchema();\n const isParentInPushQueue =\n this.#inprogressChildChangePosition !== undefined &&\n compareRows(minParentNode.row, this.#inprogressChildChangePosition) > 0 &&\n (this.#inprogressParentFetchBounds === undefined ||\n isInParentFetch(\n this.#inprogressParentFetchBounds,\n minParentNode.row,\n compareRows,\n ));\n\n if (\n this.#inprogressChildChange &&\n this.#inprogressChildChangePosition &&\n isJoinMatch(\n this.#inprogressChildChange[ChangeIndex.NODE].row,\n this.#childKey,\n minParentNode.row,\n this.#parentKey,\n )\n ) {\n if (this.#inprogressChildChange[ChangeIndex.TYPE] === ChangeType.REMOVE) {\n if (!isParentInPushQueue) {\n // Remove from relatedChildNodes since the removed child\n // was inserted into childNodes above.\n overlaidRelatedChildNodes = relatedChildNodes.filter(\n n => n !== this.#inprogressChildChange?.[ChangeIndex.NODE],\n );\n }\n } else if (isParentInPushQueue) {\n overlaidRelatedChildNodes = [\n ...generateWithOverlayNoYield(\n relatedChildNodes,\n this.#inprogressChildChange,\n this.#child.getSchema(),\n ),\n ];\n }\n }\n\n // yield node if after the overlay it still has relationship nodes\n if (overlaidRelatedChildNodes.length > 0) {\n this.#indexParentRow(minParentNode.row);\n yield {\n ...minParentNode,\n relationships: {\n ...minParentNode.relationships,\n [this.#relationshipName]: () => overlaidRelatedChildNodes,\n },\n };\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);\n break;\n case ChangeType.EDIT: {\n assert(\n rowEqualsForCompoundKey(\n change[ChangeIndex.OLD_NODE].row,\n change[ChangeIndex.NODE].row,\n this.#childKey,\n ),\n `Child edit must not change relationship.`,\n );\n yield* this.#pushChildChange(change, true);\n break;\n }\n case ChangeType.CHILD:\n yield* this.#pushChildChange(change, true);\n break;\n }\n }\n\n *#pushChildChange(change: Change, exists?: boolean): Stream<'yield'> {\n this.#inprogressChildChange = change;\n this.#inprogressChildChangePosition = undefined;\n try {\n const constraint = buildJoinConstraint(\n change[ChangeIndex.NODE].row,\n this.#childKey,\n this.#parentKey,\n );\n if (!constraint) {\n return;\n }\n const childRow = change[ChangeIndex.NODE].row;\n const changeType = change[ChangeIndex.TYPE];\n\n const partitions = this.#parentIndex.lookup(childRow, this.#childKey);\n if (!partitions) {\n // If no matching parent is resident in the view, REMOVE and EDIT\n // cannot affect any view-resident parent. (ADD and CHILD can qualify\n // previously absent parents).\n if (changeType !== ChangeType.ADD && changeType !== ChangeType.CHILD) {\n return;\n }\n }\n\n let fetchConstraints: Constraint[];\n if (changeType !== ChangeType.ADD && changeType !== ChangeType.CHILD) {\n assert(\n partitions,\n 'Matching entries must exist for non-add child change',\n );\n fetchConstraints = partitions.map(partitionConstraint =>\n partitionConstraint\n ? {...constraint, ...partitionConstraint}\n : constraint,\n );\n } else {\n fetchConstraints = [constraint];\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 'yield';\n continue;\n }\n this.#inprogressChildChange = change;\n this.#inprogressChildChangePosition = parentNode.row;\n const childNodeStream = () => {\n const constraint = buildJoinConstraint(\n parentNode.row,\n this.#parentKey,\n this.#childKey,\n );\n return constraint ? this.#child.fetch({constraint}) : [];\n };\n let parentExists = exists;\n if (!parentExists) {\n for (const childNode of childNodeStream()) {\n if (childNode === 'yield') {\n yield 'yield';\n continue;\n }\n if (\n this.#child\n .getSchema()\n .compareRows(childNode.row, change[ChangeIndex.NODE].row) !== 0\n ) {\n parentExists = true;\n break;\n }\n }\n }\n if (parentExists) {\n yield* this.#output.push(\n makeChildChange(\n {\n ...parentNode,\n relationships: {\n ...parentNode.relationships,\n [this.#relationshipName]: childNodeStream,\n },\n },\n {\n relationshipName: this.#relationshipName,\n change,\n },\n ),\n this,\n );\n } else {\n const newNode = {\n ...parentNode,\n relationships: {\n ...parentNode.relationships,\n [this.#relationshipName]: () => [change[ChangeIndex.NODE]],\n },\n };\n if (change[ChangeIndex.TYPE] === ChangeType.ADD) {\n this.#indexParentRow(parentNode.row);\n yield* this.#output.push(makeAddChange(newNode), this);\n } else {\n this.#unindexParentRow(parentNode.row);\n yield* this.#output.push(makeRemoveChange(newNode), this);\n }\n }\n }\n } finally {\n this.#inprogressChildChange = undefined;\n this.#inprogressChildChangePosition = undefined;\n this.#inprogressParentFetchBounds = undefined;\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 *#pushParent(change: Change): Stream<'yield'> {\n const childNodeStream = (node: Node) => () => {\n const constraint = buildJoinConstraint(\n node.row,\n this.#parentKey,\n this.#childKey,\n );\n return constraint ? this.#child.fetch({constraint}) : [];\n };\n\n const flip = (node: Node) => ({\n ...node,\n relationships: {\n ...node.relationships,\n [this.#relationshipName]: childNodeStream(node),\n },\n });\n\n // If no related child don't push as this is an inner join.\n let hasRelatedChild = false;\n for (const node of childNodeStream(change[ChangeIndex.NODE])()) {\n if (node === 'yield') {\n yield 'yield';\n continue;\n } else {\n hasRelatedChild = true;\n break;\n }\n }\n if (!hasRelatedChild) {\n return;\n }\n\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n this.#indexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeAddChange(flip(change[ChangeIndex.NODE])),\n this,\n );\n break;\n case ChangeType.REMOVE:\n this.#unindexParentRow(change[ChangeIndex.NODE].row);\n yield* this.#output.push(\n makeRemoveChange(flip(change[ChangeIndex.NODE])),\n this,\n );\n break;\n case ChangeType.CHILD: {\n yield* this.#output.push(\n makeChildChange(\n flip(change[ChangeIndex.NODE]),\n change[ChangeIndex.CHILD_DATA],\n ),\n this,\n );\n break;\n }\n case ChangeType.EDIT: {\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 flip(change[ChangeIndex.NODE]),\n flip(change[ChangeIndex.OLD_NODE]),\n ),\n this,\n );\n break;\n }\n default:\n unreachable(change);\n }\n }\n}\n\nexport {canonicalKeyForTest};\n"],"mappings":";;;;;;;;;;;;AAmEA,IAAI,2BAAmC;AAEvC,SAAgB,8BAAsC;CACpD,OAAO;AACT;;;;;;;;;AAkCA,IAAa,cAAb,MAA0C;CACxC;CACA;CACA;CACA;CACA;CACA;CACA;;;;;CAKA;CACA;CAEA,UAAkB;CAElB;CACA;CACA;CAEA,YAAY,EACV,QACA,OACA,WACA,UACA,kBACA,QACA,QACA,oBACA,eACA,eACO;EACP,OAAO,WAAW,OAAO,8CAA8C;EACvE,OACE,UAAU,WAAW,SAAS,QAC9B,uDACF;EACA,KAAK,UAAU;EACf,KAAK,SAAS;EACd,KAAK,aAAa;EAClB,KAAK,YAAY;EACjB,KAAK,oBAAoB;EACzB,KAAK,iBAAiB;EACtB,IAAI,aAAa;GACf,KAAK,eAAe;GACpB,KAAK,kBAAkB;EACzB,OAAO;GACL,KAAK,eAAe,IAAI,UACtB,WACA,OAAO,UAAU,CAAC,CAAC,YACnB,kBACF;GACA,KAAK,kBAAkB;EACzB;EAEA,MAAM,eAAe,OAAO,UAAU;EACtC,MAAM,cAAc,MAAM,UAAU;EACpC,KAAK,UAAU;GACb,GAAG;GACH,eAAe;IACb,GAAG,aAAa;KACf,mBA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4
4
  import { makeAddChange, makeEditChange, makeRemoveChange } from "./change.js";
5
5
  import { makeComparator, normalizeUndefined, valueToKey } from "./data.js";
6
6
  import { throwOutput } from "./operator.js";
7
- import { AGGREGATE_KEY_COLUMN, AGGREGATE_ROWS_COLUMN, AGGREGATE_VALUE_COLUMN, aggregateSourceSchema, carriesComponents, isInvertible } from "./aggregate.js";
8
7
  import { emptyArray } from "../../../shared/src/sentinels.js";
8
+ import { AGGREGATE_KEY_COLUMN, AGGREGATE_ROWS_COLUMN, AGGREGATE_VALUE_COLUMN, aggregateSourceSchema, carriesComponents, isInvertible } from "./aggregate.js";
9
9
  import { rowEqualsForCompoundKey } from "./join-utils.js";
10
10
  //#region ../zql/src/ivm/group-by.ts
11
11
  /**
@@ -3,7 +3,7 @@ import type { Node } from './data.ts';
3
3
  import type { JoinIndex } from './join-index.ts';
4
4
  import { type FetchRequest, type Input, type InputBase, type Operator, type Output } from './operator.ts';
5
5
  import type { SourceSchema } from './schema.ts';
6
- import type { Stream } from './stream.ts';
6
+ import { type Stream } from './stream.ts';
7
7
  /**
8
8
  * Keeps the parent indexes of a pipeline's EXISTS joins equal to the rows
9
9
  * the pipeline emits: rows it yields on fetch or adds on push are indexed,
@@ -1 +1 @@
1
- {"version":3,"file":"join-index-tap.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/join-index-tap.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAC,MAAM,EAAC,MAAM,aAAa,CAAC;AACxC,OAAO,KAAK,EAAC,IAAI,EAAC,MAAM,WAAW,CAAC;AACpC,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAEL,KAAK,YAAY,EACjB,KAAK,KAAK,EACV,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,MAAM,EACZ,MAAM,eAAe,CAAC;AACvB,OAAO,KAAK,EAAC,YAAY,EAAC,MAAM,aAAa,CAAC;AAC9C,OAAO,KAAK,EAAC,MAAM,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,IAAI,CAEd;IAED,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAE/C;IAQD,IAAI,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,OAAO,CAAC,CAwBpC;IAED,SAAS,CAAC,OAAO,EAAE,SAAS,GAAG,MAAM,CAAC,OAAO,CAAC,CAE7C;CACF"}
@@ -1,5 +1,7 @@
1
1
  import { unreachable } from "../../../shared/src/asserts.js";
2
2
  import { throwOutput } from "./operator.js";
3
+ import { emptyArray } from "../../../shared/src/sentinels.js";
4
+ import { tapNodes } from "./stream.js";
3
5
  //#region ../zql/src/ivm/join-index-tap.ts
4
6
  /**
5
7
  * Keeps the parent indexes of a pipeline's EXISTS joins equal to the rows
@@ -35,12 +37,12 @@ var JoinIndexTap = class {
35
37
  destroy() {
36
38
  this.#input.destroy();
37
39
  }
38
- *fetch(req) {
39
- for (const node of this.#input.fetch(req)) {
40
- if (node !== "yield") for (const index of this.#indexes) index.add(node.row);
41
- yield node;
42
- }
40
+ fetch(req) {
41
+ return tapNodes(this.#input.fetch(req), this.#indexNode);
43
42
  }
43
+ #indexNode = (node) => {
44
+ for (const index of this.#indexes) index.add(node.row);
45
+ };
44
46
  push(change) {
45
47
  switch (change[0]) {
46
48
  case 0:
@@ -60,8 +62,8 @@ var JoinIndexTap = class {
60
62
  }
61
63
  return this.#output.push(change, this);
62
64
  }
63
- *reconcile(_pusher) {
64
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
65
+ reconcile(_pusher) {
66
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
65
67
  }
66
68
  };
67
69
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"join-index-tap.js","names":[],"sources":["../../../../../zql/src/ivm/join-index-tap.ts"],"sourcesContent":["import {unreachable} from '../../../shared/src/asserts.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport type {Node} from './data.ts';\nimport type {JoinIndex} from './join-index.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport type {Stream} from './stream.ts';\n\n/**\n * Keeps the parent indexes of a pipeline's EXISTS joins equal to the rows\n * the pipeline emits: rows it yields on fetch or adds on push are indexed,\n * and rows it removes are dropped.\n *\n * An EXISTS join sits upstream of the conditions and the limit, so it sees\n * every parent the pipeline scans, including those a condition rejects or the\n * limit never keeps. Indexing them there would grow with the scan (the whole\n * table for an unlimited or selective EXISTS). This operator is placed after\n * the limit instead, where a row is only seen once it is part of the result,\n * and a row the limit evicts arrives as a remove.\n *\n * It cannot live in the Exists filter: an OR does not evaluate the branches\n * after the first that passes, but every branch's relationship is still\n * synced for the emitted row.\n */\nexport class JoinIndexTap implements Operator {\n readonly #input: Input;\n readonly #indexes: readonly JoinIndex[];\n\n #output: Output = throwOutput;\n\n constructor(input: Input, indexes: readonly JoinIndex[]) {\n this.#input = input;\n this.#indexes = indexes;\n input.setOutput(this);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n *fetch(req: FetchRequest): Stream<Node | 'yield'> {\n for (const node of this.#input.fetch(req)) {\n if (node !== 'yield') {\n for (const index of this.#indexes) {\n index.add(node.row);\n }\n }\n yield node;\n }\n }\n\n push(change: Change): Stream<'yield'> {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n for (const index of this.#indexes) {\n index.add(change[ChangeIndex.NODE].row);\n }\n break;\n case ChangeType.REMOVE:\n for (const index of this.#indexes) {\n index.remove(change[ChangeIndex.NODE].row);\n }\n break;\n case ChangeType.EDIT:\n for (const index of this.#indexes) {\n index.remove(change[ChangeIndex.OLD_NODE].row);\n index.add(change[ChangeIndex.NODE].row);\n }\n break;\n case ChangeType.CHILD:\n break;\n default:\n unreachable(change);\n }\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"],"mappings":";;;;;;;;;;;;;;;;;;;AAiCA,IAAa,eAAb,MAA8C;CAC5C;CACA;CAEA,UAAkB;CAElB,YAAY,OAAc,SAA+B;EACvD,KAAK,SAAS;EACd,KAAK,WAAW;EAChB,MAAM,UAAU,IAAI;CACtB;CAEA,UAAU,QAAsB;EAC9B,KAAK,UAAU;CACjB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,CAAC,MAAM,KAA2C;EAChD,KAAK,MAAM,QAAQ,KAAK,OAAO,MAAM,GAAG,GAAG;GACzC,IAAI,SAAS,SACX,KAAK,MAAM,SAAS,KAAK,UACvB,MAAM,IAAI,KAAK,GAAG;GAGtB,MAAM;EACR;CACF;CAEA,KAAK,QAAiC;EACpC,QAAQ,OAAO,IAAf;GACE,KAAK;IACH,KAAK,MAAM,SAAS,KAAK,UACvB,MAAM,IAAI,OAAO,EAAiB,CAAC,GAAG;IAExC;GACF,KAAK;IACH,KAAK,MAAM,SAAS,KAAK,UACvB,MAAM,OAAO,OAAO,EAAiB,CAAC,GAAG;IAE3C;GACF,KAAK;IACH,KAAK,MAAM,SAAS,KAAK,UAAU;KACjC,MAAM,OAAO,OAAO,EAAqB,CAAC,GAAG;KAC7C,MAAM,IAAI,OAAO,EAAiB,CAAC,GAAG;IACxC;IACA;GACF,KAAK,GACH;GACF,SACE,YAAY,MAAM;EACtB;EACA,OAAO,KAAK,QAAQ,KAAK,QAAQ,IAAI;CACvC;CAEA,CAAC,UAAU,SAAqC;EAC9C,IAAI,KAAK,QAAQ,WACf,OAAO,KAAK,QAAQ,UAAU,IAAI;CAEtC;AACF"}
1
+ {"version":3,"file":"join-index-tap.js","names":[],"sources":["../../../../../zql/src/ivm/join-index-tap.ts"],"sourcesContent":["import {unreachable} from '../../../shared/src/asserts.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.ts';\nimport {ChangeIndex} from './change-index.ts';\nimport {ChangeType} from './change-type.ts';\nimport type {Change} from './change.ts';\nimport type {Node} from './data.ts';\nimport type {JoinIndex} from './join-index.ts';\nimport {\n throwOutput,\n type FetchRequest,\n type Input,\n type InputBase,\n type Operator,\n type Output,\n} from './operator.ts';\nimport type {SourceSchema} from './schema.ts';\nimport {type Stream, tapNodes} from './stream.ts';\n\n/**\n * Keeps the parent indexes of a pipeline's EXISTS joins equal to the rows\n * the pipeline emits: rows it yields on fetch or adds on push are indexed,\n * and rows it removes are dropped.\n *\n * An EXISTS join sits upstream of the conditions and the limit, so it sees\n * every parent the pipeline scans, including those a condition rejects or the\n * limit never keeps. Indexing them there would grow with the scan (the whole\n * table for an unlimited or selective EXISTS). This operator is placed after\n * the limit instead, where a row is only seen once it is part of the result,\n * and a row the limit evicts arrives as a remove.\n *\n * It cannot live in the Exists filter: an OR does not evaluate the branches\n * after the first that passes, but every branch's relationship is still\n * synced for the emitted row.\n */\nexport class JoinIndexTap implements Operator {\n readonly #input: Input;\n readonly #indexes: readonly JoinIndex[];\n\n #output: Output = throwOutput;\n\n constructor(input: Input, indexes: readonly JoinIndex[]) {\n this.#input = input;\n this.#indexes = indexes;\n input.setOutput(this);\n }\n\n setOutput(output: Output): void {\n this.#output = output;\n }\n\n getSchema(): SourceSchema {\n return this.#input.getSchema();\n }\n\n destroy(): void {\n this.#input.destroy();\n }\n\n fetch(req: FetchRequest): Stream<Node | 'yield'> {\n return tapNodes(this.#input.fetch(req), this.#indexNode);\n }\n\n readonly #indexNode = (node: Node): void => {\n for (const index of this.#indexes) {\n index.add(node.row);\n }\n };\n\n push(change: Change): Stream<'yield'> {\n switch (change[ChangeIndex.TYPE]) {\n case ChangeType.ADD:\n for (const index of this.#indexes) {\n index.add(change[ChangeIndex.NODE].row);\n }\n break;\n case ChangeType.REMOVE:\n for (const index of this.#indexes) {\n index.remove(change[ChangeIndex.NODE].row);\n }\n break;\n case ChangeType.EDIT:\n for (const index of this.#indexes) {\n index.remove(change[ChangeIndex.OLD_NODE].row);\n index.add(change[ChangeIndex.NODE].row);\n }\n break;\n case ChangeType.CHILD:\n break;\n default:\n unreachable(change);\n }\n return this.#output.push(change, this);\n }\n\n reconcile(_pusher: InputBase): Stream<'yield'> {\n return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAkCA,IAAa,eAAb,MAA8C;CAC5C;CACA;CAEA,UAAkB;CAElB,YAAY,OAAc,SAA+B;EACvD,KAAK,SAAS;EACd,KAAK,WAAW;EAChB,MAAM,UAAU,IAAI;CACtB;CAEA,UAAU,QAAsB;EAC9B,KAAK,UAAU;CACjB;CAEA,YAA0B;EACxB,OAAO,KAAK,OAAO,UAAU;CAC/B;CAEA,UAAgB;EACd,KAAK,OAAO,QAAQ;CACtB;CAEA,MAAM,KAA2C;EAC/C,OAAO,SAAS,KAAK,OAAO,MAAM,GAAG,GAAG,KAAK,UAAU;CACzD;CAEA,cAAuB,SAAqB;EAC1C,KAAK,MAAM,SAAS,KAAK,UACvB,MAAM,IAAI,KAAK,GAAG;CAEtB;CAEA,KAAK,QAAiC;EACpC,QAAQ,OAAO,IAAf;GACE,KAAK;IACH,KAAK,MAAM,SAAS,KAAK,UACvB,MAAM,IAAI,OAAO,EAAiB,CAAC,GAAG;IAExC;GACF,KAAK;IACH,KAAK,MAAM,SAAS,KAAK,UACvB,MAAM,OAAO,OAAO,EAAiB,CAAC,GAAG;IAE3C;GACF,KAAK;IACH,KAAK,MAAM,SAAS,KAAK,UAAU;KACjC,MAAM,OAAO,OAAO,EAAqB,CAAC,GAAG;KAC7C,MAAM,IAAI,OAAO,EAAiB,CAAC,GAAG;IACxC;IACA;GACF,KAAK,GACH;GACF,SACE,YAAY,MAAM;EACtB;EACA,OAAO,KAAK,Q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aAAa,CAAC,EAAE,iBAAiB,GAAG,SAAS,CAAC;IAC9C;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,UAAU,GAAG,SAAS,CAAC;CACjC,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IACvB,QAAQ,CAAC,EAAE,EAAE,QAAQ,GAAG,YAAY,CAAC;IACrC,QAAQ,CAAC,WAAW,EAAE,SAAS,CAAC;CACjC,CAAC;AAEF;;;;;;;;;GASG;AACH,qBAAa,IAAK,YAAW,KAAK;;IAkChC,YAAY,EACV,MAAM,EACN,KAAK,EACL,SAAS,EACT,QAAQ,EACR,gBAAgB,EAChB,MAAM,EACN,MAAM,EACN,kBAAkB,EAClB,aAAa,EACb,MAAM,GACP,EAAE,IAAI,EAkDN;IAMD,OAAO,IAAI,IAAI,CAGd;IAED,IAAI,kBAAkB,IAAI,SAAS,CAElC;IAED,SAAS,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,CAE9B;IAED,SAAS,IAAI,YAAY,CAExB;IAED,KAAK,CAAC,GAAG,EAAE,YAAY,GAAG,MAAM,CAAC,IAAI,GAAG,OAAO,CAAC,CAE/C;CAySF"}
@@ -1,6 +1,8 @@
1
1
  import { assert, unreachable } from "../../../shared/src/asserts.js";
2
2
  import { makeAddChange, makeChildChange, makeEditChange, makeRemoveChange } from "./change.js";
3
3
  import { throwOutput } from "./operator.js";
4
+ import { emptyArray } from "../../../shared/src/sentinels.js";
5
+ import { andFinally, eachStream, mapNodes, yieldOnce } from "./stream.js";
4
6
  import { buildJoinConstraint, canonicalKey, generateWithOverlay, generateWithOverlayUnordered, isJoinMatch, rowEqualsForCompoundKey } from "./join-utils.js";
5
7
  import { JoinIndex } from "./join-index.js";
6
8
  import { mergeSortedStreams } from "./memory-source.js";
@@ -88,8 +90,8 @@ var Join = class {
88
90
  reconcile: () => this.#reconcile()
89
91
  });
90
92
  }
91
- *#reconcile() {
92
- if (this.#output.reconcile) yield* this.#output.reconcile(this);
93
+ #reconcile() {
94
+ return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;
93
95
  }
94
96
  destroy() {
95
97
  this.#parent.destroy();
@@ -104,100 +106,103 @@ var Join = class {
104
106
  getSchema() {
105
107
  return this.#schema;
106
108
  }
107
- *fetch(req) {
108
- for (const parentNode of this.#parent.fetch(req)) {
109
- if (parentNode === "yield") {
110
- yield parentNode;
111
- continue;
112
- }
113
- this.#indexParentRow(parentNode.row);
114
- yield this.#processParentNode(parentNode.row, parentNode.relationships);
115
- }
109
+ fetch(req) {
110
+ return mapNodes(this.#parent.fetch(req), this.#fetchParentNode);
116
111
  }
117
- *#pushParent(change) {
112
+ #fetchParentNode = (parentNode) => {
113
+ this.#indexParentRow(parentNode.row);
114
+ return this.#processParentNode(parentNode.row, parentNode.relationships);
115
+ };
116
+ #pushParent(change) {
118
117
  switch (change[0]) {
119
118
  case 0:
120
119
  this.#indexParentRow(change[1].row);
121
- yield* this.#output.push(makeAddChange(this.#processParentNode(change[1].row, change[1].relationships)), this);
122
- break;
120
+ return this.#output.push(makeAddChange(this.#processParentNode(change[1].row, change[1].relationships)), this);
123
121
  case 1:
124
122
  this.#unindexParentRow(change[1].row);
125
- yield* this.#output.push(makeRemoveChange(this.#processParentNode(change[1].row, change[1].relationships)), this);
126
- break;
127
- case 3:
128
- yield* this.#output.push(makeChildChange(this.#processParentNode(change[1].row, change[1].relationships), change[2]), this);
129
- break;
123
+ return this.#output.push(makeRemoveChange(this.#processParentNode(change[1].row, change[1].relationships)), this);
124
+ case 3: return this.#output.push(makeChildChange(this.#processParentNode(change[1].row, change[1].relationships), change[2]), this);
130
125
  case 2:
131
126
  assert(rowEqualsForCompoundKey(change[2].row, change[1].row, this.#parentKey), `Parent edit must not change relationship.`);
132
127
  this.#unindexParentRow(change[2].row);
133
128
  this.#indexParentRow(change[1].row);
134
- yield* this.#output.push(makeEditChange(this.#processParentNode(change[1].row, change[1].relationships), this.#processParentNode(change[2].row, change[2].relationships)), this);
135
- break;
129
+ return this.#output.push(makeEditChange(this.#processParentNode(change[1].row, change[1].relationships), this.#processParentNode(change[2].row, change[2].relationships)), this);
136
130
  default: unreachable(change);
137
131
  }
138
132
  }
139
- *#pushChild(change) {
133
+ #pushChild(change) {
140
134
  switch (change[0]) {
141
135
  case 0:
142
136
  case 1:
143
- yield* this.#pushChildChange(change[1].row, change);
144
- break;
145
- case 3:
146
- yield* this.#pushChildChange(change[1].row, change);
147
- break;
137
+ case 3: return this.#pushChildChange(change[1].row, change);
148
138
  case 2: {
149
139
  const childRow = change[1].row;
150
140
  const oldChildRow = change[2].row;
151
141
  assert(rowEqualsForCompoundKey(oldChildRow, childRow, this.#childKey), "Child edit must not change relationship.");
152
- yield* this.#pushChildChange(childRow, change);
153
- break;
142
+ return this.#pushChildChange(childRow, change);
154
143
  }
155
144
  default: unreachable(change);
156
145
  }
157
146
  }
158
- *#pushChildChange(childRow, change) {
147
+ #pushChildChange(childRow, change) {
159
148
  this.#inprogressChildChange = change;
160
149
  this.#inprogressChildChangePosition = void 0;
161
150
  this.#inprogressReachedParents = this.#parent.getSchema().sort === void 0 ? /* @__PURE__ */ new Set() : void 0;
151
+ let s;
162
152
  try {
163
- const constraint = buildJoinConstraint(childRow, this.#childKey, this.#parentKey);
164
- if (constraint) {
165
- const partitions = this.#parentIndex.lookup(childRow, this.#childKey);
166
- let fetchConstraints;
167
- if (partitions) fetchConstraints = partitions.map((partitionConstraint) => partitionConstraint ? {
168
- ...constraint,
169
- ...partitionConstraint
170
- } : constraint);
171
- else if (change[0] === this.#qualifyingChildChange) fetchConstraints = [constraint];
172
- else return;
173
- if (this.#boundProvider) this.#inprogressParentFetchBounds = readParentFetchBounds(this.#boundProvider, fetchConstraints);
174
- let parentNodeStream;
175
- if (fetchConstraints.length === 1) parentNodeStream = this.#parent.fetch({ constraint: fetchConstraints[0] });
176
- else {
177
- const streams = fetchConstraints.map((c) => this.#parent.fetch({ constraint: c }));
178
- const compare = (a, b) => this.#schema.compareRows(a.row, b.row);
179
- parentNodeStream = mergeSortedStreams(streams, compare);
180
- }
181
- for (const parentNode of parentNodeStream) {
182
- if (parentNode === "yield") {
183
- yield parentNode;
184
- continue;
185
- }
186
- this.#inprogressChildChangePosition = parentNode.row;
187
- this.#inprogressReachedParents?.add(canonicalKey(parentNode.row, this.#schema.primaryKey));
188
- const childChange = makeChildChange(this.#processParentNode(parentNode.row, parentNode.relationships), {
189
- relationshipName: this.#relationshipName,
190
- change
191
- });
192
- yield* this.#output.push(childChange, this);
193
- }
194
- }
195
- } finally {
196
- this.#inprogressChildChange = void 0;
197
- this.#inprogressChildChangePosition = void 0;
198
- this.#inprogressReachedParents = void 0;
199
- this.#inprogressParentFetchBounds = void 0;
153
+ s = this.#pushChildChangeToParents(childRow, change);
154
+ } catch (e) {
155
+ this.#endChildChange();
156
+ throw e;
200
157
  }
158
+ return andFinally(s, this.#endChildChange);
159
+ }
160
+ #endChildChange = () => {
161
+ this.#inprogressChildChange = void 0;
162
+ this.#inprogressChildChangePosition = void 0;
163
+ this.#inprogressReachedParents = void 0;
164
+ this.#inprogressParentFetchBounds = void 0;
165
+ };
166
+ #pushChildChangeToParents(childRow, change) {
167
+ const constraint = buildJoinConstraint(childRow, this.#childKey, this.#parentKey);
168
+ if (!constraint) return emptyArray;
169
+ const partitions = this.#parentIndex.lookup(childRow, this.#childKey);
170
+ let fetchConstraints;
171
+ if (partitions) fetchConstraints = partitions.map((partitionConstraint) => partitionConstraint ? {
172
+ ...constraint,
173
+ ...partitionConstraint
174
+ } : constraint);
175
+ else if (change[0] === this.#qualifyingChildChange) fetchConstraints = [constraint];
176
+ else return emptyArray;
177
+ if (this.#boundProvider) this.#inprogressParentFetchBounds = readParentFetchBounds(this.#boundProvider, fetchConstraints);
178
+ let parentNodeStream;
179
+ if (fetchConstraints.length === 1) parentNodeStream = this.#parent.fetch({ constraint: fetchConstraints[0] });
180
+ else {
181
+ const streams = fetchConstraints.map((c) => this.#parent.fetch({ constraint: c }));
182
+ const compare = (a, b) => this.#schema.compareRows(a.row, b.row);
183
+ parentNodeStream = mergeSortedStreams(streams, compare);
184
+ }
185
+ return this.#pushChildChangeFrom(parentNodeStream[Symbol.iterator](), change);
186
+ }
187
+ /**
188
+ * Pushes the child change to each parent in `it`. Closes `it` if it stops
189
+ * before the end: on an error, or when the stream returned is not read to
190
+ * its end.
191
+ */
192
+ #pushChildChangeFrom(it, change) {
193
+ return eachStream(() => {
194
+ const r = it.next();
195
+ if (r.done) return;
196
+ const parentNode = r.value;
197
+ if (parentNode === "yield") return yieldOnce;
198
+ this.#inprogressChildChangePosition = parentNode.row;
199
+ this.#inprogressReachedParents?.add(canonicalKey(parentNode.row, this.#schema.primaryKey));
200
+ const childChange = makeChildChange(this.#processParentNode(parentNode.row, parentNode.relationships), {
201
+ relationshipName: this.#relationshipName,
202
+ change
203
+ });
204
+ return this.#output.push(childChange, this);
205
+ }, it);
201
206
  }
202
207
  /**
203
208
  * Whether the in-progress child change has yet to reach `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 = (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,eAAe;IACb,GAAG,aAAa;KACf,mBAAmB;KAClB,GAAG;KACH,UAAU;KACV;IACF;GACF;EACF;EAEA,OAAO,UAAU;GACf,OAAO,WAAmB,KAAK,YAAY,MAAM;GACjD,iBAAiB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a,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"}
1
+ {"version":3,"file":"join.js","names":[],"sources":["../../../../../zql/src/ivm/join.ts"],"sourcesContent":["import {assert, unreachable} from '../../../shared/src/asserts.ts';\nimport {emptyArray} from '../../../shared/src/sentinels.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 {\n andFinally,\n eachStream,\n mapNodes,\n type Stream,\n yieldOnce,\n} 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 return this.#output.reconcile ? this.#output.reconcile(this) : emptyArray;\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 return mapNodes(this.#parent.fetch(req), this.#fetchParentNode);\n }\n\n readonly #fetchParentNode = (parentNode: Node): Node => {\n this.#indexParentRow(parentNode.row);\n return this.#processParentNode(parentNode.row, parentNode.relationships);\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 return this.#output.push(\n makeAddChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n ),\n this,\n );\n case ChangeType.REMOVE:\n this.#unindexParentRow(change[ChangeIndex.NODE].row);\n return this.#output.push(\n makeRemoveChange(\n this.#processParentNode(\n change[ChangeIndex.NODE].row,\n change[ChangeIndex.NODE].relationships,\n ),\n ),\n this,\n );\n case ChangeType.CHILD:\n return 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 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 return 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 }\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 case ChangeType.CHILD:\n return this.#pushChildChange(change[ChangeIndex.NODE].row, change);\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 return this.#pushChildChange(childRow, change);\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 let s: Stream<'yield'>;\n try {\n s = this.#pushChildChangeToParents(childRow, change);\n } catch (e) {\n this.#endChildChange();\n throw e;\n }\n return andFinally(s, this.#endChildChange);\n }\n\n readonly #endChildChange = (): void => {\n this.#inprogressChildChange = undefined;\n this.#inprogressChildChangePosition = undefined;\n this.#inprogressReachedParents = undefined;\n this.#inprogressParentFetchBounds = undefined;\n };\n\n #pushChildChangeToParents(childRow: Row, change: Change): Stream<'yield'> {\n const constraint = buildJoinConstraint(\n childRow,\n this.#childKey,\n this.#parentKey,\n );\n if (!constraint) {\n return emptyArray;\n }\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 emptyArray;\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 return this.#pushChildChangeFrom(\n parentNodeStream[Symbol.iterator](),\n change,\n );\n }\n\n /**\n * Pushes the child change to each parent in `it`. Closes `it` if it stops\n * before the end: on an error, or when the stream returned is not read to\n * its end.\n */\n #pushChildChangeFrom(\n it: Iterator<Node | 'yield'>,\n change: Change,\n ): Stream<'yield'> {\n return eachStream(() => {\n const r = it.next();\n if (r.done) {\n return undefined;\n }\n const parentNode = r.value;\n if (parentNode === 'yield') {\n return yieldOnce;\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 return this.#output.push(childChange, this);\n }, it);\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":";;;;;;;;;;;;;;;;;;;;AAkFA,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;EAC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AAS,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"}
@@ -1 +1 @@
1
- {"version":3,"file":"memory-source.d.ts","sourceRoot":"","sources":["../../../../../zql/src/ivm/memory-source.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,QAAQ,EAAC,MAAM,kCAAkC,CAAC;AAS1D,OAAO,KAAK,EACV,SAAS,EACT,QAAQ,EAET,MAAM,mCAAmC,CAAC;AAC3C,OAAO,KAAK,EAAC,GAAG,EAAE,KAAK,EAAC,MAAM,oCAAoC,CAAC;AACnE,OAAO,KAAK,EAAC,UAAU,EAAC,MAAM,2CAA2C,CAAC;AAC1E,OAAO,KAAK,EAAC,WAAW,EAAC,MAAM,yCAAyC,CAAC;AACzE,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAGL,KAAK,mBAAmB,EACzB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EAIL,KAAK,UAAU,EAChB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAIL,KAAK,UAAU,EACf,KAAK,IAAI,EACV,MAAM,WAAW,CAAC;AAEnB,OAAO,EAGL,KAAK,KAAK,EACV,KAAK,eAAe,EACpB,KAAK,MAAM,EACX,KAAK,KAAK,EACX,MAAM,eAAe,CAAC;AAGvB,OAAO,KAAK,EACV,MAAM,EACN,YAAY,EAIZ,WAAW,EACZ,MAAM,aAAa,CAAC;AAErB,OAAO,EAAU,KAAK,MAAM,EAAC,MAAM,aAAa,CAAC;AAEjD,MAAM,MAAM,OAAO,GAAG;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,MA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