Open History to see the last commit
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 }
78}
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 }
123}
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 return output
153 }
154}
155
156/**
157 * Joins two input streams
158 * @param other - The other stream to join with
159 */
160export function antiJoin<
161 K,
162 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
163 V2,
164 T,
165>(
166 other: IStreamBuilder<KeyValue<K, V2>>
167): PipedOperator<T, KeyValue<K, [V1, null]>> {
168 return (
169 stream: IStreamBuilder<T>
170 ): IStreamBuilder<KeyValue<K, [V1, null]>> => {
171 const matchedLeft = stream.pipe(
172 innerJoin(other),
173 map(([key, [valueLeft, _valueRight]]) => [key, valueLeft])
174 )
175 const anti = stream.pipe(
176 concat(matchedLeft.pipe(negate())),
177 // @ts-ignore TODO: fix this
178 map(([key, value]) => [key, [value, null]])
179 )
180 return anti as IStreamBuilder<KeyValue<K, [V1, null]>>
181 }
182}
183
184/**
185 * Joins two input streams
186 * @param other - The other stream to join with
187 */
188export function leftJoin<
189 K,
190 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
191 V2,
192 T,
193>(
194 other: IStreamBuilder<KeyValue<K, V2>>
195): PipedOperator<T, KeyValue<K, [V1, V2 | null]>> {
196 return (
197 stream: IStreamBuilder<T>
198 ): IStreamBuilder<KeyValue<K, [V1, V2 | null]>> => {
199 const left = stream
200 const right = other
201 const inner = left.pipe(innerJoin(right))
202 const anti = left.pipe(antiJoin(right))
203 return inner.pipe(concat(anti)) as IStreamBuilder<
204 KeyValue<K, [V1, V2 | null]>
205 >
206 }
207}
208
209/**
210 * Joins two input streams
211 * @param other - The other stream to join with
212 */
213export function rightJoin<
214 K,
215 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
216 V2,
217 T,
218>(
219 other: IStreamBuilder<KeyValue<K, V2>>
220): PipedOperator<T, KeyValue<K, [V1 | null, V2]>> {
221 return (
222 stream: IStreamBuilder<T>
223 ): IStreamBuilder<KeyValue<K, [V1 | null, V2]>> => {
224 const left = stream as IStreamBuilder<KeyValue<K, V1>>
225 const right = other
226 const inner = left.pipe(innerJoin(right))
227 const anti = right.pipe(
228 antiJoin(left),
229 map(([key, [a, b]]) => [key, [b, a]])
230 )
231 return inner.pipe(concat(anti)) as IStreamBuilder<
232 KeyValue<K, [V1 | null, V2]>
233 >
234 }
235}
236
237/**
238 * Joins two input streams
239 * @param other - The other stream to join with
240 */
241export function fullJoin<
242 K,
243 V1 extends T extends KeyValue<infer _KT, infer VT> ? VT : never,
244 V2,
245 T,
246>(
247 other: IStreamBuilder<KeyValue<K, V2>>
248): PipedOperator<T, KeyValue<K, [V1 | null, V2 | null]>> {
249 return (
250 stream: IStreamBuilder<T>
251 ): IStreamBuilder<KeyValue<K, [V1 | null, V2 | null]>> => {
252 const left = stream as IStreamBuilder<KeyValue<K, V1>>
253 const right = other
254 const inner = left.pipe(innerJoin(right))
255 const antiLeft = left.pipe(antiJoin(right))
256 const antiRight = right.pipe(
257 antiJoin(left),
258 map(([key, [a, b]]) => [key, [b, a]])
259 )
260 return inner.pipe(concat(antiLeft), concat(antiRight)) as IStreamBuilder<
261 KeyValue<K, [V1 | null, V2 | null]>
262 >
263 }
264}