-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathmerge_join.go
More file actions
292 lines (265 loc) · 6.39 KB
/
merge_join.go
File metadata and controls
292 lines (265 loc) · 6.39 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package executor
import (
"sort"
"sync"
"planner"
"github.com/pkg/errors"
"github.com/xelabs/go-mysqlstack/sqlparser"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
)
// sortMergeJoin used to join `lres` and `rres` to `res`.
func sortMergeJoin(lres, rres, res *sqltypes.Result, node *planner.JoinNode, maxrow int) error {
var wg sync.WaitGroup
sort := func(keys []planner.JoinKey, res *sqltypes.Result) {
defer wg.Done()
sort.Slice(res.Rows, func(i, j int) bool {
for _, key := range keys {
cmp := sqltypes.NullsafeCompare(res.Rows[i][key.Index], res.Rows[j][key.Index])
if cmp == 0 {
continue
}
return cmp < 0
}
return true
})
}
wg.Add(1)
go sort(node.LeftKeys, lres)
wg.Add(1)
go sort(node.RightKeys, rres)
wg.Wait()
return mergeJoin(lres, rres, res, node, maxrow)
}
// mergeJoin used to join the sorted results.
func mergeJoin(lres, rres, res *sqltypes.Result, node *planner.JoinNode, maxrow int) error {
var err error
lrows, lidx := fetchSameKeyRows(lres.Rows, node.LeftKeys, 0)
rrows, ridx := fetchSameKeyRows(rres.Rows, node.RightKeys, 0)
for lrows != nil {
if rrows == nil {
err = concatLeftAndNil(lres.Rows[lidx-len(lrows):], node, res, maxrow)
break
}
cmp := 0
isNull := false
for k, key := range node.LeftKeys {
cmp = sqltypes.NullsafeCompare(lrows[0][key.Index], rrows[0][node.RightKeys[k].Index])
if cmp != 0 {
break
}
if lrows[0][key.Index].IsNull() {
isNull = true
break
}
}
if cmp == 0 {
if isNull {
err = concatLeftAndNil(lrows, node, res, maxrow)
} else {
err = concatLeftAndRight(lrows, rrows, node, res, maxrow)
}
lrows, lidx = fetchSameKeyRows(lres.Rows, node.LeftKeys, lidx)
rrows, ridx = fetchSameKeyRows(rres.Rows, node.RightKeys, ridx)
} else if cmp > 0 {
rrows, ridx = fetchSameKeyRows(rres.Rows, node.RightKeys, ridx)
} else {
err = concatLeftAndNil(lrows, node, res, maxrow)
lrows, lidx = fetchSameKeyRows(lres.Rows, node.LeftKeys, lidx)
}
if err != nil {
return err
}
}
return err
}
// fetchSameKeyRows used to fetch the same joinkey values' rows.
func fetchSameKeyRows(rows [][]sqltypes.Value, joins []planner.JoinKey, index int) ([][]sqltypes.Value, int) {
var chunk [][]sqltypes.Value
if index >= len(rows) {
return nil, index
}
if len(joins) == 0 {
return rows, len(rows)
}
current := rows[index]
chunk = append(chunk, current)
index++
for index < len(rows) {
equal := keysEqual(current, rows[index], joins)
if !equal {
break
}
chunk = append(chunk, rows[index])
index++
}
return chunk, index
}
func keysEqual(row1, row2 []sqltypes.Value, joins []planner.JoinKey) bool {
for _, join := range joins {
cmp := sqltypes.NullsafeCompare(row1[join.Index], row2[join.Index])
if cmp != 0 {
return false
}
}
return true
}
// concatLeftAndRight used to concat thle left and right results, handle otherJoinOn|rightNull|OtherFilter.
func concatLeftAndRight(lrows, rrows [][]sqltypes.Value, node *planner.JoinNode, res *sqltypes.Result, maxrow int) error {
var err error
var mu sync.Mutex
p := newCalcPool(joinWorkers)
mathOps := func(lrow []sqltypes.Value) {
defer p.done()
if err != nil {
return
}
blend := true
matchCnt := 0
for _, idx := range node.LeftTmpCols {
vn := lrow[idx].ToNative()
if vn == nil || vn.(int64) == 0 {
blend = false
break
}
}
if blend {
for _, rrow := range rrows {
match := true
for _, filter := range node.CmpFilter {
v1, v2 := lrow[filter.Left], rrow[filter.Right]
if filter.Exchange {
v1, v2 = v2, v1
}
cmp := sqltypes.NullsafeCompare(v1, v2)
switch filter.Operator {
case sqlparser.EqualStr:
if cmp != 0 {
match = false
}
case sqlparser.LessThanStr:
if cmp != -1 {
match = false
}
case sqlparser.GreaterThanStr:
if cmp != 1 {
match = false
}
case sqlparser.LessEqualStr:
if cmp == 1 {
match = false
}
case sqlparser.GreaterEqualStr:
if cmp == -1 {
match = false
}
case sqlparser.NotEqualStr:
if cmp == 0 {
match = false
}
case sqlparser.NullSafeEqualStr:
if cmp != 0 {
match = false
}
}
if !match {
break
}
// null value cannot match.
if filter.Operator != sqlparser.NullSafeEqualStr && (lrow[filter.Left].IsNull() || rrow[filter.Right].IsNull()) {
match = false
break
}
}
if match {
matchCnt++
ok := true
for _, idx := range node.RightTmpCols {
if !rrow[idx].IsNull() {
ok = false
break
}
}
if ok {
mu.Lock()
if err == nil {
res.Rows = append(res.Rows, joinRows(lrow, rrow, node.Cols))
res.RowsAffected++
if len(res.Rows) > maxrow {
err = errors.Errorf("unsupported: join.row.count.exceeded.allowed.limit.of.'%d'", maxrow)
mu.Unlock()
break
}
}
mu.Unlock()
}
}
}
}
if matchCnt == 0 && node.IsLeftJoin && !node.HasRightFilter {
mu.Lock()
if err == nil {
res.Rows = append(res.Rows, joinRows(lrow, nil, node.Cols))
res.RowsAffected++
if len(res.Rows) > maxrow {
err = errors.Errorf("unsupported: join.row.count.exceeded.allowed.limit.of.'%d'", maxrow)
}
}
mu.Unlock()
}
}
for _, lrow := range lrows {
p.add(1)
go mathOps(lrow)
}
p.wait()
return err
}
func concatLeftAndNil(lrows [][]sqltypes.Value, node *planner.JoinNode, res *sqltypes.Result, maxrow int) error {
if node.IsLeftJoin && !node.HasRightFilter {
for _, row := range lrows {
res.Rows = append(res.Rows, joinRows(row, nil, node.Cols))
res.RowsAffected++
if len(res.Rows) > maxrow {
return errors.Errorf("unsupported: join.row.count.exceeded.allowed.limit.of.'%d'", maxrow)
}
}
}
return nil
}
// calcPool used to the merge join calc.
type calcPool struct {
queue chan int
wg *sync.WaitGroup
}
func newCalcPool(size int) *calcPool {
if size <= 0 {
size = 1
}
return &calcPool{
queue: make(chan int, size),
wg: &sync.WaitGroup{},
}
}
func (p *calcPool) add(delta int) {
for i := 0; i < delta; i++ {
p.queue <- 1
}
for i := 0; i > delta; i-- {
<-p.queue
}
p.wg.Add(delta)
}
func (p *calcPool) done() {
<-p.queue
p.wg.Done()
}
func (p *calcPool) wait() {
p.wg.Wait()
}