Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

join.ts

265 lines7.1 KB

1import { BinaryOperator, DifferenceStreamWriter } from "../graph.js"
2import { StreamBuilder } from "../d2.js"
3import { MultiSet } from "../multiset.js"
4import { Index } from "../indexes.js"
5import { negate } from "./negate.js"
6import { map } from "./map.js"
7import { concat } from "./concat.js"
8import type { DifferenceStreamReader } from "../graph.js"
9import type { IStreamBuilder, KeyValue, PipedOperator } from "../types.js"
10​
11/**
12 * Type of join to perform
13 */
14export type JoinType = `inner` | `left` | `right` | `full` | `anti`
15​
16/**
17 * Operator that joins two input streams
18 */
19export class JoinOperator<K, V1, V2> extends BinaryOperator<
20 [K, V1] | [K, V2] | [K, [V1, V2]]
21> {
22 #indexA = new Index<K, V1>()
23 #indexB = new Index<K, V2>()
24​
25 constructor(
26 id: number,
27 inputA: DifferenceStreamReader<[K, V1]>,
28 inputB: DifferenceStreamReader<[K, V2]>,
29 output: DifferenceStreamWriter<[K, [V1, V2]]>
30 ) {
31 super(id, inputA, inputB, output)
32 }
33​
34 run(): void {
35 const deltaA = new Index<K, V1>()
36 const deltaB = new Index<K, V2>()
37​
38 // Process input A - process ALL messages, not just the first one
39 const messagesA = this.inputAMessages()
40 for (const message of messagesA) {
41 const multiSetMessage = message as unknown as MultiSet<[K, V1]>
42 for (const [item, multiplicity] of multiSetMessage.getInner()) {
43 const [key, value] = item
44 deltaA.addValue(key, [value, multiplicity])
45 }
46 }
47​
48 // Process input B - process ALL messages, not just the first one
49 const messagesB = this.inputBMessages()
50 for (const message of messagesB) {
51 const multiSetMessage = message as unknown as MultiSet<[K, V2]>
52 for (const [item, multiplicity] of multiSetMessage.getInner()) {
53 const [key, value] = item
54 deltaB.addValue(key, [value, multiplicity])
55 }
56 }
57​
58 // Process results
59 const results = new MultiSet<[K, [V1, V2]]>()
60​
61 // Join deltaA with existing indexB
62 results.extend(deltaA.join(this.#indexB))
63​
64 // Append deltaA to indexA
65 this.#indexA.append(deltaA)
66​
67 // Join existing indexA with deltaB
68 results.extend(this.#indexA.join(deltaB))
69​
70 // Send results
71 if (results.getInner().length > 0) {
72 this.output.sendData(results)
73 }
74​
75 // Append deltaB to indexB
76 this.#indexB.append(deltaB)
77 }
79​
80/**
81 * Joins two input streams
82 * @param other - The other stream to join with
83 * @param type - The type of join to perform
84 */
85export function join<
86 K,
87 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
88 V2,
89 T,
90>(
91 other: IStreamBuilder<KeyValue<K, V2>>,
92 type: JoinType = `inner`
93): PipedOperator<T, KeyValue<K, [V1 | null, V2 | null]>> {
94 switch (type) {
95 case `inner`:
96 return innerJoin(other) as unknown as PipedOperator<
97 T,
98 KeyValue<K, [V1, V2]>
99 >
100 case `anti`:
101 return antiJoin(other) as unknown as PipedOperator<
102 T,
103 KeyValue<K, [V1, null]>
104 >
105 case `left`:
106 return leftJoin(other) as unknown as PipedOperator<
107 T,
108 KeyValue<K, [V1, V2 | null]>
109 >
110 case `right`:
111 return rightJoin(other) as unknown as PipedOperator<
112 T,
113 KeyValue<K, [V1 | null, V2]>
114 >
115 case `full`:
116 return fullJoin(other) as unknown as PipedOperator<
117 T,
118 KeyValue<K, [V1 | null, V2 | null]>
119 >
120 default:
121 throw new Error(`Join type ${type} is invalid`)
122 }
124​
125/**
126 * Joins two input streams
127 * @param other - The other stream to join with
128 */
129export function innerJoin<
130 K,
131 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
132 V2,
133 T,
134>(
135 other: IStreamBuilder<KeyValue<K, V2>>
136): PipedOperator<T, KeyValue<K, [V1, V2]>> {
137 return (stream: IStreamBuilder<T>): IStreamBuilder<KeyValue<K, [V1, V2]>> => {
138 if (stream.graph !== other.graph) {
139 throw new Error(`Cannot join streams from different graphs`)
140 }
141 const output = new StreamBuilder<KeyValue<K, [V1, V2]>>(
142 stream.graph,
143 new DifferenceStreamWriter<KeyValue<K, [V1, V2]>>()
144 )
145 const operator = new JoinOperator<K, V1, V2>(
146 stream.graph.getNextOperatorId(),
147 stream.connectReader() as DifferenceStreamReader<KeyValue<K, V1>>,
148 other.connectReader(),
149 output.writer
150 )
151 stream.graph.addOperator(operator)
152 stream.graph.addStream(output.connectReader())
153 return output
154 }
156​
157/**
158 * Joins two input streams
159 * @param other - The other stream to join with
160 */
161export function antiJoin<
162 K,
163 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
164 V2,
165 T,
166>(
167 other: IStreamBuilder<KeyValue<K, V2>>
168): PipedOperator<T, KeyValue<K, [V1, null]>> {
169 return (
170 stream: IStreamBuilder<T>
171 ): IStreamBuilder<KeyValue<K, [V1, null]>> => {
172 const matchedLeft = stream.pipe(
173 innerJoin(other),
174 map(([key, [valueLeft, _valueRight]]) => [key, valueLeft])
175 )
176 const anti = stream.pipe(
177 concat(matchedLeft.pipe(negate())),
178 // @ts-ignore TODO: fix this
179 map(([key, value]) => [key, [value, null]])
180 )
181 return anti as IStreamBuilder<KeyValue<K, [V1, null]>>
182 }
184​
185/**
186 * Joins two input streams
187 * @param other - The other stream to join with
188 */
189export function leftJoin<
190 K,
191 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
192 V2,
193 T,
194>(
195 other: IStreamBuilder<KeyValue<K, V2>>
196): PipedOperator<T, KeyValue<K, [V1, V2 | null]>> {
197 return (
198 stream: IStreamBuilder<T>
199 ): IStreamBuilder<KeyValue<K, [V1, V2 | null]>> => {
200 const left = stream
201 const right = other
202 const inner = left.pipe(innerJoin(right))
203 const anti = left.pipe(antiJoin(right))
204 return inner.pipe(concat(anti)) as IStreamBuilder<
205 KeyValue<K, [V1, V2 | null]>
206 >
207 }
209​
210/**
211 * Joins two input streams
212 * @param other - The other stream to join with
213 */
214export function rightJoin<
215 K,
216 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
217 V2,
218 T,
219>(
220 other: IStreamBuilder<KeyValue<K, V2>>
221): PipedOperator<T, KeyValue<K, [V1 | null, V2]>> {
222 return (
223 stream: IStreamBuilder<T>
224 ): IStreamBuilder<KeyValue<K, [V1 | null, V2]>> => {
225 const left = stream as IStreamBuilder<KeyValue<K, V1>>
226 const right = other
227 const inner = left.pipe(innerJoin(right))
228 const anti = right.pipe(
229 antiJoin(left),
230 map(([key, [a, b]]) => [key, [b, a]])
231 )
232 return inner.pipe(concat(anti)) as IStreamBuilder<
233 KeyValue<K, [V1 | null, V2]>
234 >
235 }
237​
238/**
239 * Joins two input streams
240 * @param other - The other stream to join with
241 */
242export function fullJoin<
243 K,
244 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
245 V2,
246 T,
247>(
248 other: IStreamBuilder<KeyValue<K, V2>>
249): PipedOperator<T, KeyValue<K, [V1 | null, V2 | null]>> {
250 return (
251 stream: IStreamBuilder<T>
252 ): IStreamBuilder<KeyValue<K, [V1 | null, V2 | null]>> => {
253 const left = stream as IStreamBuilder<KeyValue<K, V1>>
254 const right = other
255 const inner = left.pipe(innerJoin(right))
256 const antiLeft = left.pipe(antiJoin(right))
257 const antiRight = right.pipe(
258 antiJoin(left),
259 map(([key, [a, b]]) => [key, [b, a]])
260 )
261 return inner.pipe(concat(antiLeft), concat(antiRight)) as IStreamBuilder<
262 KeyValue<K, [V1 | null, V2 | null]>
263 >
264 }