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