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