-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathjoin.ts
58 lines (53 loc) · 1.57 KB
/
join.ts
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
/*!
* Copyright (c) Microsoft. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project.
*/
import type { JoinArgs } from '@datashaper/schema'
import { JoinStrategy } from '@datashaper/schema'
import type { TableContainer } from '@datashaper/tables'
import { container } from '@datashaper/tables'
import type ColumnTable from 'arquero/dist/types/table/column-table'
import { BaseNode, NodeInput } from '../dataflow/index.js'
class JoinNode extends BaseNode<TableContainer, JoinArgs> {
constructor(id: string) {
super([NodeInput.Other])
this.id = id
}
protected doRecalculate(): void | Promise<void> {
const left = this.inputValue()
const right = this.inputValue(NodeInput.Other)
if (left?.table != null && right?.table != null && this.config != null) {
this.emit(
container(this.id, doJoin(left.table, right.table, this.config)),
)
} else {
this.emit(undefined)
}
}
}
export function join(id: string): JoinNode {
return new JoinNode(id)
}
function doJoin(
input: ColumnTable,
other: ColumnTable,
{ on, strategy = JoinStrategy.Inner }: JoinArgs,
): ColumnTable {
switch (strategy) {
case JoinStrategy.SemiJoin:
return input.semijoin(other, on)
case JoinStrategy.AntiJoin:
return input.antijoin(other, on)
case JoinStrategy.Cross:
return input.cross(other)
default:
return input.join(other, on, undefined, {
left:
strategy === JoinStrategy.LeftOuter ||
strategy === JoinStrategy.FullOuter,
right:
strategy === JoinStrategy.RightOuter ||
strategy === JoinStrategy.FullOuter,
})
}
}