forked from pingcap/tidb
/
index_join.go
287 lines (267 loc) · 7.89 KB
/
index_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
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
// Copyright 2017 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package executor
import (
"bytes"
"sort"
"github.com/juju/errors"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/terror"
"github.com/pingcap/tidb/util/codec"
"github.com/pingcap/tidb/util/types"
)
type orderedRow struct {
key []byte
row Row
}
type orderedRows []orderedRow
// Len returns the number of rows.
func (e orderedRows) Len() int {
return len(e)
}
// Swap implements sort.Interface Swap interface.
func (e orderedRows) Swap(i, j int) {
e[i], e[j] = e[j], e[i]
}
// Less implements sort.Interface Less interface.
func (e orderedRows) Less(i, j int) bool {
return bytes.Compare(e[i].key, e[j].key) < 0
}
// IndexLookUpJoin fetches batches of data from outer executor and constructs ranges for inner executor.
type IndexLookUpJoin struct {
baseExecutor
innerExec DataReader
cursor int
resultRows []Row
outerRows orderedRows
innerRows orderedRows
innerDatums orderedRows // innerDatums are extracted by innerRows and innerJoinKeys
exhausted bool // exhausted means whether all data has been extracted
leftIsOuter bool
outerJoinKeys []*expression.Column
innerJoinKeys []*expression.Column
outerConditions expression.CNFExprs
innerConditions expression.CNFExprs
otherConditions expression.CNFExprs
defaultValues []types.Datum
outer bool
batchSize int
curBatchSize int
}
// Open implements the Executor Open interface.
func (e *IndexLookUpJoin) Open() error {
e.curBatchSize = 32
e.cursor = 0
e.exhausted = false
return errors.Trace(e.children[0].Open())
}
// Close implements the Executor Close interface.
func (e *IndexLookUpJoin) Close() error {
e.resultRows = nil
e.outerRows = nil
e.innerDatums = nil
return errors.Trace(e.children[0].Close())
}
// Next implements the Executor Next interface.
// We will fetch batches of row from outer executor, construct the inner datums and sort them.
// At the same time we will fetch the inner row by the inner datums and apply merge join.
func (e *IndexLookUpJoin) Next() (Row, error) {
for e.cursor == len(e.resultRows) {
if e.exhausted {
return nil, nil
}
e.curBatchSize *= 2
if e.curBatchSize > e.batchSize {
e.curBatchSize = e.batchSize
}
e.outerRows = e.outerRows[:0]
e.innerRows = e.innerRows[:0]
e.resultRows = e.resultRows[:0]
e.innerDatums = e.innerDatums[:0]
for i := 0; i < e.curBatchSize; i++ {
outerRow, err := e.children[0].Next()
if err != nil {
return nil, errors.Trace(err)
}
if outerRow == nil {
e.exhausted = true
break
}
match, err := expression.EvalBool(e.outerConditions, outerRow, e.ctx)
if err != nil {
return nil, errors.Trace(err)
}
if match {
joinDatums := make([]types.Datum, 0, len(e.outerJoinKeys))
for i, col := range e.outerJoinKeys {
datum, err := col.Eval(outerRow)
if err != nil {
return nil, errors.Trace(err)
}
innerDatum, err := datum.ConvertTo(e.ctx.GetSessionVars().StmtCtx, e.innerJoinKeys[i].GetType())
if err != nil {
return nil, errors.Trace(err)
}
joinDatums = append(joinDatums, innerDatum)
}
joinOuterEncodeKey, err := codec.EncodeKey(nil, joinDatums...)
if err != nil {
return nil, errors.Trace(err)
}
e.outerRows = append(e.outerRows, orderedRow{key: joinOuterEncodeKey, row: outerRow})
e.innerDatums = append(e.innerDatums, orderedRow{key: joinOuterEncodeKey, row: joinDatums})
} else if e.outer {
e.resultRows = append(e.resultRows, e.fillDefaultValues(outerRow))
}
}
sort.Sort(e.outerRows)
err := e.doJoin()
if err != nil {
return nil, errors.Trace(err)
}
e.cursor = 0
}
row := e.resultRows[e.cursor]
e.cursor++
return row, nil
}
func (e *IndexLookUpJoin) fillDefaultValues(row Row) Row {
if e.leftIsOuter {
row = append(row, e.defaultValues...)
} else {
row = append(e.defaultValues, row...)
}
return row
}
func getUniqueDatums(rows orderedRows) [][]types.Datum {
datums := make([][]types.Datum, 0, rows.Len())
sort.Sort(rows)
for i := range rows {
if i > 0 && bytes.Equal(rows[i-1].key, rows[i].key) {
continue
}
datums = append(datums, rows[i].row)
}
return datums
}
// doJoin will join the outer rows and inner rows and store them to resultRows.
func (e *IndexLookUpJoin) doJoin() error {
err := e.innerExec.doRequestForDatums(getUniqueDatums(e.innerDatums), e.ctx.GoCtx())
if err != nil {
return errors.Trace(err)
}
defer terror.Call(e.innerExec.Close)
for {
innerRow, err1 := e.innerExec.Next()
if err1 != nil {
return errors.Trace(err1)
}
if innerRow == nil {
break
}
match, err1 := expression.EvalBool(e.innerConditions, innerRow, e.ctx)
if err1 != nil {
return errors.Trace(err1)
}
if !match {
continue
}
joinDatums := make([]types.Datum, 0, len(e.innerJoinKeys))
for _, col := range e.innerJoinKeys {
datum, err2 := col.Eval(innerRow)
if err2 != nil {
return errors.Trace(err2)
}
joinDatums = append(joinDatums, datum)
}
joinKey, err1 := codec.EncodeKey(nil, joinDatums...)
if err1 != nil {
return errors.Trace(err1)
}
e.innerRows = append(e.innerRows, orderedRow{key: joinKey, row: innerRow})
}
sort.Sort(e.innerRows)
return e.doMergeJoin()
}
// getNextCursor will move cursor to the next datum that is different from the previous one and return it.
func getNextCursor(cursor int, rows orderedRows) int {
for {
cursor++
if cursor >= len(rows) {
break
}
c := bytes.Compare(rows[cursor].key, rows[cursor-1].key)
if c != 0 {
break
}
}
return cursor
}
// doMergeJoin joins the innerRows and outerRows which have been sorted before.
func (e *IndexLookUpJoin) doMergeJoin() error {
var outerCursor, innerCursor int
for outerCursor < len(e.outerRows) && innerCursor < len(e.innerRows) {
c := bytes.Compare(e.outerRows[outerCursor].key, e.innerRows[innerCursor].key)
if c == 0 {
outerBeginCursor := outerCursor
outerEndCursor := getNextCursor(outerCursor, e.outerRows)
innerBeginCursor := innerCursor
innerEndCursor := getNextCursor(innerCursor, e.innerRows)
for i := outerBeginCursor; i < outerEndCursor; i++ {
var outerMatch bool
outerRow := e.outerRows[i].row
for j := innerBeginCursor; j < innerEndCursor; j++ {
innerRow := e.innerRows[j].row
var joinedRow Row
if e.leftIsOuter {
joinedRow = makeJoinRow(outerRow, innerRow)
} else {
joinedRow = makeJoinRow(innerRow, outerRow)
}
match, err := expression.EvalBool(e.otherConditions, joinedRow, e.ctx)
if err != nil {
return errors.Trace(err)
}
if match {
outerMatch = true
e.resultRows = append(e.resultRows, joinedRow)
}
}
if e.outer && !outerMatch {
e.resultRows = append(e.resultRows, e.fillDefaultValues(outerRow))
}
}
outerCursor, innerCursor = outerEndCursor, innerEndCursor
} else if c < 0 {
// If outer smaller than inner, move and enlarge outer cursor
nextOuterCursor := getNextCursor(outerCursor, e.outerRows)
if !e.outer {
outerCursor = nextOuterCursor
} else {
for outerCursor < nextOuterCursor {
outerRow := e.outerRows[outerCursor].row
e.resultRows = append(e.resultRows, e.fillDefaultValues(outerRow))
outerCursor++
}
}
} else {
innerCursor = getNextCursor(innerCursor, e.outerRows)
}
}
for e.outer && outerCursor < len(e.outerRows) {
outerRow := e.outerRows[outerCursor].row
e.resultRows = append(e.resultRows, e.fillDefaultValues(outerRow))
outerCursor++
}
return nil
}