-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjoin.go
69 lines (60 loc) · 1.52 KB
/
join.go
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
package main
type JoinOperator struct {
left Operator
currentLeft Tuple
right Operator
expression BinaryJoinExpression
current Tuple
}
func NewJoinOperator(left Operator, right Operator, expression BinaryJoinExpression) *JoinOperator {
joinOperator := &JoinOperator{
left: left,
currentLeft: Tuple{}, // we have to save the state of current tuple
// since Operator only defines execute(), which moves the iterator
right: right,
expression: expression,
current: Tuple{},
}
getCurrent(joinOperator)
return joinOperator
}
func (jo *JoinOperator) Next() bool {
return jo.current.Values != nil
}
func getCurrent(jo *JoinOperator) {
var current Tuple = Tuple{}
var currentRight Tuple
if jo.currentLeft.Values == nil && jo.left.Next() {
// this is the first invocation of the function,
// we need to save the tuple on the outer operator
// for re-use on next invocations
jo.currentLeft = jo.left.Execute()
}
out:
for jo.currentLeft.Values != nil {
for jo.right.Next() {
currentRight = jo.right.Execute()
if jo.expression.Execute(jo.currentLeft, currentRight) {
current = Tuple{
Values: append(jo.currentLeft.Values, currentRight.Values...),
}
break out
}
}
jo.right.Reset()
if jo.left.Next() {
jo.currentLeft = jo.left.Execute()
} else {
jo.currentLeft = Tuple{}
}
}
jo.current = current
}
func (jo *JoinOperator) Execute() Tuple {
result := jo.current
getCurrent(jo)
return result
}
func (jo *JoinOperator) Reset() {
// not implemented
}