Open History to see the last commit
1import { describe, expect, test } from 'vitest'
2import { D2 } from '../../src/d2.js'
3import { MultiSet } from '../../src/multiset.js'
4import { join } from '../../src/operators/join.js'
5import { output } from '../../src/operators/output.js'
6import {
7 KeyedMessageTracker,
8 assertKeyedResults,
9 assertOnlyKeysAffected,
10} from '../test-utils.js'
11
12describe(`Operators`, () => {
13 describe(`Join operation`, () => {
14 testJoin()
15 })
16})
17
18function testJoin() {
19 test(`basic join operation`, () => {
20 const graph = new D2()
21 const inputA = graph.newInput<[number, string]>()
22 const inputB = graph.newInput<[number, string]>()
23 const tracker = new KeyedMessageTracker<number, [string, string]>()
24
25 inputA.pipe(
26 join(inputB),
27 output((message) => {
28 tracker.addMessage(message as MultiSet<[number, [string, string]]>)
29 }),
30 )
31
32 graph.finalize()
33
34 inputA.sendData(
35 new MultiSet([
36 [[1, `a`], 1],
37 [[2, `b`], 1],
38 ]),
39 )
40
41 inputB.sendData(
42 new MultiSet([
43 [[1, `x`], 1],
44 [[2, `y`], 1],
45 [[3, `z`], 1], // key 3 only exists in B, so no join output expected
46 ]),
47 )
48
49 graph.run()
50
51 const result = tracker.getResult()
52
53 // Assert only keys that can actually join (1, 2) are affected, not key 3
54 assertOnlyKeysAffected(
55 `basic join operation`,
56 result.weightedDeltas,
57 [1, 2],
58 )
59
60 // Assert the final materialized results are correct
61 assertKeyedResults(
62 `basic join operation`,
63 result,
64 [
65 [1, [`a`, `x`]],
66 [2, [`b`, `y`]],
67 ],
68 4, // Expected weighted delta count
69 )
70 })
71
72 test(`join with late arriving data`, () => {
73 const graph = new D2()
74 const inputA = graph.newInput<[number, string]>()
75 const inputB = graph.newInput<[number, string]>()
76 const tracker = new KeyedMessageTracker<number, [string, string]>()
77
78 inputA.pipe(
79 join(inputB),
80 output((message) => {
81 tracker.addMessage(message as MultiSet<[number, [string, string]]>)
82 }),
83 )
84
85 graph.finalize()
86
87 inputA.sendData(
88 new MultiSet([
89 [[1, `a`], 1],
90 [[2, `b`], 1],
91 ]),
92 )
93
94 graph.run()
95
96 inputB.sendData(
97 new MultiSet([
98 [[1, `x`], 1],
99 [[2, `y`], 1],
100 ]),
101 )
102
103 graph.run()
104
105 const result = tracker.getResult()
106
107 // Assert only expected keys (1, 2) are affected in the join output
108 assertOnlyKeysAffected(
109 `join with late arriving data`,
110 result.weightedDeltas,
111 [1, 2],
112 )
113
114 // Assert the final materialized results are correct
115 assertKeyedResults(
116 `join with late arriving data`,
117 result,
118 [
119 [1, [`a`, `x`]],
120 [2, [`b`, `y`]],
121 ],
122 4, // Expected weighted delta count
123 )
124 })
125
126 test(`join with negative multiplicities`, () => {
127 const graph = new D2()
128 const inputA = graph.newInput<[number, string]>()
129 const inputB = graph.newInput<[number, string]>()
130 const tracker = new KeyedMessageTracker<number, [string, string]>()
131
132 inputA.pipe(
133 join(inputB),
134 output((message) => {
135 tracker.addMessage(message as MultiSet<[number, [string, string]]>)
136 }),
137 )
138
139 graph.finalize()
140
141 inputA.sendData(
142 new MultiSet([
143 [[1, `a`], 1],
144 [[2, `b`], -1], // Negative multiplicity
145 ]),
146 )
147 inputB.sendData(
148 new MultiSet([
149 [[1, `x`], 1],
150 [[2, `y`], 1],
151 ]),
152 )
153
154 graph.run()
155
156 const result = tracker.getResult()
157
158 // Assert only keys that participate in join (1, 2) are affected
159 assertOnlyKeysAffected(
160 `join with negative multiplicities`,
161 result.weightedDeltas,
162 [1, 2],
163 )
164
165 // Verify that key 2 produces a message but with negative multiplicity
166 const key2Deltas = result.weightedDeltas.filter(
167 ([[key, _value], _mult]) => key === 2,
168 )
169 expect(key2Deltas.length).toBeGreaterThan(0) // Key 2 should produce weighted deltas
170 expect(key2Deltas[0]![1]).toBeLessThan(0) // But with negative multiplicity
171
172 // Assert the final materialized results (only positive multiplicities remain)
173 assertKeyedResults(
174 `join with negative multiplicities`,
175 result,
176 [
177 [1, [`a`, `x`]], // Only key 1 should remain in final results
178 ],
179 4, // Expected weighted delta count
180 )
181 })
182
183 test(`join with multiple batches sent before running - regression test for data loss bug`, () => {
184 const graph = new D2()
185 const inputA = graph.newInput<[string, string]>()
186 const inputB = graph.newInput<[string, string]>()
187 const tracker = new KeyedMessageTracker<string, [string, string]>()
188
189 inputA.pipe(
190 join(inputB),
191 output((message) => {
192 tracker.addMessage(message as MultiSet<[string, [string, string]]>)
193 }),
194 )
195
196 graph.finalize()
197
198 // Send multiple batches to inputA before running
199 inputA.sendData(
200 new MultiSet([
201 [[`key1`, `batch1_a`], 1],
202 [[`key2`, `batch1_b`], 1],
203 ]),
204 )
205
206 inputA.sendData(
207 new MultiSet([
208 [[`key3`, `batch2_a`], 1],
209 [[`key4`, `batch2_b`], 1],
210 ]),
211 )
212
213 inputA.sendData(new MultiSet([[[`key5`, `batch3_a`], 1]]))
214
215 // Send corresponding data to inputB
216 inputB.sendData(
217 new MultiSet([
218 [[`key1`, `x1`], 1],
219 [[`key2`, `x2`], 1],
220 [[`key3`, `x3`], 1],
221 [[`key4`, `x4`], 1],
222 [[`key5`, `x5`], 1],
223 ]),
224 )
225
226 // Run the graph - should process all batches
227 graph.run()
228
229 const result = tracker.getResult()
230
231 // Assert only expected keys are affected - all keys that can join
232 const expectedKeys = [`key1`, `key2`, `key3`, `key4`, `key5`]
233 assertOnlyKeysAffected(
234 `join multiple batches`,
235 result.weightedDeltas,
236 expectedKeys,
237 )
238
239 // Assert the final materialized results are correct
240 assertKeyedResults(
241 `join multiple batches`,
242 result,
243 [
244 [`key1`, [`batch1_a`, `x1`]],
245 [`key2`, [`batch1_b`, `x2`]],
246 [`key3`, [`batch2_a`, `x3`]],
247 [`key4`, [`batch2_b`, `x4`]],
248 [`key5`, [`batch3_a`, `x5`]],
249 ],
250 10, // Expected weighted delta count
251 )
252 })
253
254 test(`join comparison: step-by-step vs batch processing should give same results`, () => {
255 // Step-by-step processing
256 const graph1 = new D2()
257 const inputA1 = graph1.newInput<[string, string]>()
258 const inputB1 = graph1.newInput<[string, string]>()
259 const stepTracker = new KeyedMessageTracker<string, [string, string]>()
260
261 inputA1.pipe(
262 join(inputB1),
263 output((message) => {
264 stepTracker.addMessage(message as MultiSet<[string, [string, string]]>)
265 }),
266 )
267
268 graph1.finalize()
269
270 // Set up inputB data first
271 inputB1.sendData(
272 new MultiSet([
273 [[`item1`, `x1`], 1],
274 [[`item2`, `x2`], 1],
275 [[`item3`, `x3`], 1],
276 ]),
277 )
278
279 // Send and process inputA one batch at a time
280 inputA1.sendData(new MultiSet([[[`item1`, `a1`], 1]]))
281 graph1.run()
282
283 inputA1.sendData(new MultiSet([[[`item2`, `a2`], 1]]))
284 graph1.run()
285
286 inputA1.sendData(new MultiSet([[[`item3`, `a3`], 1]]))
287 graph1.run()
288
289 // Batch processing
290 const graph2 = new D2()
291 const inputA2 = graph2.newInput<[string, string]>()
292 const inputB2 = graph2.newInput<[string, string]>()
293 const batchTracker = new KeyedMessageTracker<string, [string, string]>()
294
295 inputA2.pipe(
296 join(inputB2),
297 output((message) => {
298 batchTracker.addMessage(message as MultiSet<[string, [string, string]]>)
299 }),
300 )
301
302 graph2.finalize()
303
304 // Set up inputB data
305 inputB2.sendData(
306 new MultiSet([
307 [[`item1`, `x1`], 1],
308 [[`item2`, `x2`], 1],
309 [[`item3`, `x3`], 1],
310 ]),
311 )
312
313 // Send all inputA batches then run once
314 inputA2.sendData(new MultiSet([[[`item1`, `a1`], 1]]))
315 inputA2.sendData(new MultiSet([[[`item2`, `a2`], 1]]))
316 inputA2.sendData(new MultiSet([[[`item3`, `a3`], 1]]))
317 graph2.run()
318
319 const stepResult = stepTracker.getResult()
320 const batchResult = batchTracker.getResult()
321
322 // Both approaches should affect exactly the same keys
323 const expectedKeys = [`item1`, `item2`, `item3`]
324 assertOnlyKeysAffected(
325 `join step-by-step`,
326 stepResult.weightedDeltas,
327 expectedKeys,
328 )
329 assertOnlyKeysAffected(
330 `join batch processing`,
331 batchResult.weightedDeltas,
332 expectedKeys,
333 )
334
335 // Both approaches should produce the same final materialized results
336 expect(stepResult.sortedResults).toEqual(batchResult.sortedResults)
337
338 // Both should have the expected final results
339 const expectedResults: Array<[string, [string, string]]> = [
340 [`item1`, [`a1`, `x1`]],
341 [`item2`, [`a2`, `x2`]],
342 [`item3`, [`a3`, `x3`]],
343 ]
344
345 assertKeyedResults(`join step-by-step`, stepResult, expectedResults, 6)
346 assertKeyedResults(`join batch processing`, batchResult, expectedResults, 6)
347 })
348}