-
Notifications
You must be signed in to change notification settings - Fork 290
/
Copy pathjoin_partitioned_sorted.go
186 lines (170 loc) · 5.46 KB
/
join_partitioned_sorted.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
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
package instruction
import (
"io"
"github.com/chrislusf/gleam/pb"
"github.com/chrislusf/gleam/util"
)
func init() {
InstructionRunner.Register(func(m *pb.Instruction) Instruction {
if m.GetJoinPartitionedSorted() != nil {
return NewJoinPartitionedSorted(
m.GetJoinPartitionedSorted().GetIsLeftOuterJoin(),
m.GetJoinPartitionedSorted().GetIsRightOuterJoin(),
toInts(m.GetJoinPartitionedSorted().GetIndexes()),
)
}
return nil
})
}
type JoinPartitionedSorted struct {
isLeftOuterJoin bool
isRightOuterJoin bool
indexes []int
}
func NewJoinPartitionedSorted(isLeftOuterJoin bool, isRightOuterJoin bool, indexes []int) *JoinPartitionedSorted {
return &JoinPartitionedSorted{isLeftOuterJoin, isRightOuterJoin, indexes}
}
func (b *JoinPartitionedSorted) Name(prefix string) string {
return prefix + ".JoinPartitionedSorted"
}
func (b *JoinPartitionedSorted) Function() func(readers []io.Reader, writers []io.Writer, stats *pb.InstructionStat) error {
return func(readers []io.Reader, writers []io.Writer, stats *pb.InstructionStat) error {
return DoJoinPartitionedSorted(readers[0], readers[1], writers[0], b.indexes, b.isLeftOuterJoin, b.isRightOuterJoin, stats)
}
}
func (b *JoinPartitionedSorted) SerializeToCommand() *pb.Instruction {
return &pb.Instruction{
JoinPartitionedSorted: &pb.Instruction_JoinPartitionedSorted{
IsLeftOuterJoin: (b.isLeftOuterJoin),
IsRightOuterJoin: (b.isRightOuterJoin),
Indexes: getIndexes(b.indexes),
},
}
}
func (b *JoinPartitionedSorted) GetMemoryCostInMB(partitionSize int64) int64 {
return 5
}
func DoJoinPartitionedSorted(leftRawChan, rightRawChan io.Reader, writer io.Writer, indexes []int,
isLeftOuterJoin, isRightOuterJoin bool, stats *pb.InstructionStat) error {
leftChan := newChannelOfValuesWithSameKey("left", leftRawChan, indexes)
rightChan := newChannelOfValuesWithSameKey("right", rightRawChan, indexes)
// get first value from both channels
leftValuesWithSameKey, leftHasValue := <-leftChan
rightValuesWithSameKey, rightHasValue := <-rightChan
var leftValueLength, rightValueLength int
if leftHasValue {
leftValueLength = len(leftValuesWithSameKey.V[0].([]interface{}))
}
if rightHasValue {
rightValueLength = len(rightValuesWithSameKey.V[0].([]interface{}))
}
for leftHasValue && rightHasValue {
x := util.Compare(leftValuesWithSameKey.K, rightValuesWithSameKey.K)
ts := max(leftValuesWithSameKey.T, rightValuesWithSameKey.T)
switch {
case x == 0:
// left and right cartician join
for _, a := range leftValuesWithSameKey.V {
for _, b := range rightValuesWithSameKey.V {
util.NewRow(ts).AppendKey(
leftValuesWithSameKey.K...).AppendValue(
a.([]interface{})...).AppendValue(
b.([]interface{})...).WriteTo(writer)
stats.OutputCounter++
}
}
leftValuesWithSameKey, leftHasValue = <-leftChan
rightValuesWithSameKey, rightHasValue = <-rightChan
stats.InputCounter += 2
case x < 0:
if isLeftOuterJoin {
for _, leftValue := range leftValuesWithSameKey.V {
var t []interface{}
t = append(t, leftValue.([]interface{})...)
t = addNils(t, rightValueLength)
util.NewRow(ts).AppendKey(
leftValuesWithSameKey.K...).AppendValue(
t...).WriteTo(writer)
stats.OutputCounter++
}
}
leftValuesWithSameKey, leftHasValue = <-leftChan
stats.InputCounter++
case x > 0:
if isRightOuterJoin {
for _, rightValue := range rightValuesWithSameKey.V {
var t []interface{}
t = addNils(t, leftValueLength)
t = append(t, rightValue.([]interface{})...)
util.NewRow(ts).AppendKey(
rightValuesWithSameKey.K...).AppendValue(
t...).WriteTo(writer)
stats.OutputCounter++
}
}
rightValuesWithSameKey, rightHasValue = <-rightChan
stats.InputCounter++
}
}
if leftHasValue {
if isLeftOuterJoin {
for _, leftValue := range leftValuesWithSameKey.V {
var t []interface{}
t = append(t, leftValue.([]interface{})...)
t = addNils(t, rightValueLength)
util.NewRow(leftValuesWithSameKey.T).AppendKey(
leftValuesWithSameKey.K...).AppendValue(
t...).WriteTo(writer)
stats.OutputCounter++
}
}
}
for leftValuesWithSameKey = range leftChan {
stats.InputCounter++
if isLeftOuterJoin {
for _, leftValue := range leftValuesWithSameKey.V {
var t []interface{}
t = append(t, leftValue.([]interface{})...)
t = addNils(t, rightValueLength)
util.NewRow(leftValuesWithSameKey.T).AppendKey(
leftValuesWithSameKey.K...).AppendValue(
t...).WriteTo(writer)
stats.OutputCounter++
}
}
}
if rightHasValue {
if isRightOuterJoin {
for _, rightValue := range rightValuesWithSameKey.V {
var t []interface{}
t = addNils(t, leftValueLength)
t = append(t, rightValue.([]interface{})...)
util.NewRow(rightValuesWithSameKey.T).AppendKey(
rightValuesWithSameKey.K...).AppendValue(
t...).WriteTo(writer)
stats.OutputCounter++
}
}
}
for rightValuesWithSameKey = range rightChan {
stats.InputCounter++
if isRightOuterJoin {
for _, rightValue := range rightValuesWithSameKey.V {
var t []interface{}
t = addNils(t, leftValueLength)
t = append(t, rightValue.([]interface{})...)
util.NewRow(rightValuesWithSameKey.T).AppendKey(
rightValuesWithSameKey.K...).AppendValue(
t...).WriteTo(writer)
stats.OutputCounter++
}
}
}
return nil
}
func addNils(target []interface{}, nilCount int) []interface{} {
for i := 0; i < nilCount; i++ {
target = append(target, nil)
}
return target
}