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 { distinct } from "../../src/operators/distinct.js"
5import { output } from "../../src/operators/output.js"
6import { MessageTracker, assertResults } from "../test-utils.js"
7import { hash } from "../../src/hashing/index.js"
8
9describe(`Operators`, () => {
10 describe(`Efficient distinct operation`, () => {
11 testDistinct()
12 })
13})
14
15function testDistinct() {
16 test(`basic distinct operation`, () => {
17 const graph = new D2()
18 const input = graph.newInput<[number, string]>()
19 const messages: Array<MultiSet<[number, string]>> = []
20
21 input.pipe(
22 distinct(),
23 output((message) => {
24 messages.push(message)
25 })
26 )
27
28 graph.finalize()
29
30 input.sendData(
31 new MultiSet([
32 [[1, `a`], 2],
33 [[2, `b`], 1],
34 [[2, `c`], 2],
35 ])
36 )
37 graph.run()
38
39 const data = messages.map((m) => m.getInner())
40
41 expect(data).toEqual([
42 [
43 [[hash([1, `a`]), `a`], 1],
44 [[hash([2, `b`]), `b`], 1],
45 [[hash([2, `c`]), `c`], 1],
46 ],
47 ])
48 })
49
50 test(`distinct by certain property`, () => {
51 const graph = new D2()
52 const input = graph.newInput<[number, { name: string; country: string }]>()
53 const messages: Array<
54 MultiSet<[number, { name: string; country: string }]>
55 > = []
56
57 input.pipe(
58 distinct(([_, value]) => value.country),
59 output((message) => {
60 messages.push(message)
61 })
62 )
63
64 graph.finalize()
65
66 input.sendData(
67 new MultiSet([
68 [[1, { name: `Valter`, country: `Portugal` }], 1],
69 [[2, { name: `Sam`, country: `UK` }], 1],
70 [[2, { name: `Kevin`, country: `Belgium` }], 1],
71 [[3, { name: `Garry`, country: `UK` }], 1],
72 [[4, { name: `Kyle`, country: `USA` }], 1],
73 ])
74 )
75
76 graph.run()
77
78 const data = messages.map((m) => m.getInner())[0]!
79 const countries = data
80 .map(([[_, value], multiplicity]) => [value.country, multiplicity])
81 .sort()
82
83 expect(countries).toEqual(
84 [
85 [`Belgium`, 1],
86 [`Portugal`, 1],
87 [`UK`, 1],
88 [`USA`, 1],
89 ].sort()
90 )
91 })
92
93 test(`distinct with updates`, () => {
94 const graph = new D2()
95 const input = graph.newInput<[number, string]>()
96 const tracker = new MessageTracker<[number, string]>()
97
98 input.pipe(
99 distinct(),
100 output((message) => {
101 tracker.addMessage(message)
102 })
103 )
104
105 graph.finalize()
106
107 // Initial batch
108 input.sendData(
109 new MultiSet([
110 [[1, `a`], 1],
111 [[1, `b`], 1],
112 [[1, `a`], 1], // Duplicate, should only result in 1
113 ])
114 )
115 graph.run()
116
117 const initialResult = tracker.getResult()
118 assertResults(
119 `distinct with updates - initial`,
120 initialResult,
121 [
122 [hash([1, `a`]), `a`],
123 [hash([1, `b`]), `b`],
124 ], // Should have both distinct values
125 4 // Max expected messages
126 )
127
128 tracker.reset()
129
130 // Second batch - remove some, add new
131 input.sendData(
132 new MultiSet([
133 [[1, `b`], -1], // Remove 'b'
134 [[1, `c`], 2], // Add 'c' (multiplicity should be capped at 1)
135 [[1, `a`], -1], // Remove 'a'
136 ])
137 )
138 graph.run()
139
140 const secondResult = tracker.getResult()
141 assertResults(
142 `distinct with updates - second batch`,
143 secondResult,
144 [[hash([1, `c`]), `c`]], // Should only have 'c' remaining
145 4 // Max expected messages
146 )
147
148 tracker.reset()
149
150 // Third batch - remove remaining
151 input.sendData(new MultiSet([[[1, `c`], -2]]))
152 graph.run()
153
154 const thirdResult = tracker.getResult()
155 assertResults(
156 `distinct with updates - third batch`,
157 thirdResult,
158 [], // Should have no remaining distinct values
159 2 // Max expected messages
160 )
161 })
162
163 test(`distinct with multiple batches of same key`, () => {
164 const graph = new D2()
165 const input = graph.newInput<[string, number]>()
166 const messages: Array<MultiSet<[string, number]>> = []
167
168 input.pipe(
169 distinct(),
170 output((message) => {
171 messages.push(message)
172 })
173 )
174
175 graph.finalize()
176
177 input.sendData(
178 new MultiSet([
179 [[`key1`, 1], 2],
180 [[`key1`, 2], 3],
181 [[`key2`, 1], 1],
182 ])
183 )
184 graph.run()
185
186 const data = messages.map((m) => m.getInner())
187
188 expect(data).toEqual([
189 [
190 [[hash([`key1`, 1]), 1], 1],
191 [[hash([`key1`, 2]), 2], 1],
192 [[hash([`key2`, 1]), 1], 1],
193 ],
194 ])
195 })
196
197 test(`distinct with multiple batches of same key that cancel out`, () => {
198 const graph = new D2()
199 const input = graph.newInput<[string, number]>()
200 const tracker = new MessageTracker<[string, number]>()
201
202 input.pipe(
203 distinct(),
204 output((message) => {
205 tracker.addMessage(message)
206 })
207 )
208
209 graph.finalize()
210
211 input.sendData(
212 new MultiSet([
213 [[`key1`, 1], 2], // Add ['key1', 1] with multiplicity 2 -> should become 1 (distinct)
214 [[`key1`, 2], 2], // Add ['key1', 2] with multiplicity 2 -> should become 1 (distinct)
215 [[`key1`, 2], 1], // Add more ['key1', 2] with multiplicity 1 -> total 3, still 1 in distinct
216 [[`key2`, 1], 1], // Add ['key2', 1] with multiplicity 1 -> should become 1 (distinct)
217 [[`key1`, 2], -3], // Remove all ['key1', 2] (total was 3) -> should be removed from distinct
218 [[`key2`, 1], 1], // Add more ['key2', 1] -> still 1 in distinct
219 ])
220 )
221 graph.run()
222
223 const result = tracker.getResult()
224 assertResults(
225 `distinct with multiple batches that cancel out`,
226 result,
227 [
228 [hash([`key1`, 1]), 1], // Should remain (multiplicity 2 -> 1 in distinct)
229 [hash([`key2`, 1]), 1], // Should remain (multiplicity 2 -> 1 in distinct)
230 ],
231 6 // Max expected messages (generous upper bound)
232 )
233 })
234}