-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathmerge_join.go
395 lines (341 loc) · 10.5 KB
/
merge_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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// 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 (
"context"
"fmt"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/disk"
"github.com/pingcap/tidb/util/memory"
"github.com/pingcap/tidb/util/stringutil"
)
// MergeJoinExec implements the merge join algorithm.
// This operator assumes that two iterators of both sides
// will provide required order on join condition:
// 1. For equal-join, one of the join key from each side
// matches the order given.
// 2. For other cases its preferred not to use SMJ and operator
// will throw error.
type MergeJoinExec struct {
baseExecutor
stmtCtx *stmtctx.StatementContext
compareFuncs []expression.CompareFunc
joiner joiner
isOuterJoin bool
desc bool
innerTable *mergeJoinTable
outerTable *mergeJoinTable
hasMatch bool
hasNull bool
memTracker *memory.Tracker
diskTracker *disk.Tracker
}
var (
innerTableLabel fmt.Stringer = stringutil.StringerStr("innerTable")
outerTableLabel fmt.Stringer = stringutil.StringerStr("outerTable")
)
type mergeJoinTable struct {
isInner bool
childIndex int
joinKeys []*expression.Column
filters []expression.Expression
executed bool
childChunk *chunk.Chunk
childChunkIter *chunk.Iterator4Chunk
groupChecker *vecGroupChecker
groupRowsSelected []int
groupRowsIter chunk.Iterator
// for inner table, an unbroken group may refer many chunks
rowContainer *chunk.RowContainer
// for outer table, save result of filters
filtersSelected []bool
memTracker *memory.Tracker
}
func (t *mergeJoinTable) init(exec *MergeJoinExec) {
child := exec.children[t.childIndex]
t.childChunk = newFirstChunk(child)
t.childChunkIter = chunk.NewIterator4Chunk(t.childChunk)
items := make([]expression.Expression, 0, len(t.joinKeys))
for _, col := range t.joinKeys {
items = append(items, col)
}
t.groupChecker = newVecGroupChecker(exec.ctx, items)
t.groupRowsIter = chunk.NewIterator4Chunk(t.childChunk)
if t.isInner {
t.rowContainer = chunk.NewRowContainer(child.base().retFieldTypes, t.childChunk.Capacity())
t.rowContainer.GetMemTracker().AttachTo(exec.memTracker)
t.rowContainer.GetMemTracker().SetLabel(innerTableLabel)
t.rowContainer.GetDiskTracker().AttachTo(exec.diskTracker)
t.rowContainer.GetDiskTracker().SetLabel(innerTableLabel)
if config.GetGlobalConfig().OOMUseTmpStorage {
actionSpill := t.rowContainer.ActionSpill()
exec.ctx.GetSessionVars().StmtCtx.MemTracker.SetActionOnExceed(actionSpill)
}
t.memTracker = memory.NewTracker(innerTableLabel, -1)
} else {
t.filtersSelected = make([]bool, 0, exec.maxChunkSize)
t.memTracker = memory.NewTracker(outerTableLabel, -1)
}
t.memTracker.AttachTo(exec.memTracker)
t.memTracker.Consume(t.childChunk.MemoryUsage())
}
func (t *mergeJoinTable) finish() error {
t.memTracker.Consume(-t.childChunk.MemoryUsage())
if t.isInner {
if err := t.rowContainer.Close(); err != nil {
return err
}
}
t.executed = false
t.childChunk = nil
t.childChunkIter = nil
t.groupChecker = nil
t.groupRowsSelected = nil
t.groupRowsIter = nil
t.rowContainer = nil
t.filtersSelected = nil
t.memTracker = nil
return nil
}
func (t *mergeJoinTable) selectNextGroup() {
t.groupRowsSelected = t.groupRowsSelected[:0]
begin, end := t.groupChecker.getNextGroup()
if t.isInner && t.hasNullInJoinKey(t.childChunk.GetRow(begin)) {
return
}
for i := begin; i < end; i++ {
t.groupRowsSelected = append(t.groupRowsSelected, i)
}
t.childChunk.SetSel(t.groupRowsSelected)
}
func (t *mergeJoinTable) fetchNextChunk(ctx context.Context, exec *MergeJoinExec) error {
oldMemUsage := t.childChunk.MemoryUsage()
err := Next(ctx, exec.children[t.childIndex], t.childChunk)
t.memTracker.Consume(t.childChunk.MemoryUsage() - oldMemUsage)
if err != nil {
return err
}
t.executed = t.childChunk.NumRows() == 0
return nil
}
func (t *mergeJoinTable) fetchNextInnerGroup(ctx context.Context, exec *MergeJoinExec) error {
t.childChunk.SetSel(nil)
if err := t.rowContainer.Reset(); err != nil {
return err
}
fetchNext:
if t.executed && t.groupChecker.isExhausted() {
// Ensure iter at the end, since sel of childChunk has been cleared.
t.groupRowsIter.ReachEnd()
return nil
}
isEmpty := true
// For inner table, rows have null in join keys should be skip by selectNextGroup.
for isEmpty && !t.groupChecker.isExhausted() {
t.selectNextGroup()
isEmpty = len(t.groupRowsSelected) == 0
}
// For inner table, all the rows have the same join keys should be put into one group.
for !t.executed && t.groupChecker.isExhausted() {
if !isEmpty {
// Group is not empty, hand over the management of childChunk to t.rowContainer.
if err := t.rowContainer.Add(t.childChunk); err != nil {
return err
}
t.memTracker.Consume(-t.childChunk.MemoryUsage())
t.groupRowsSelected = nil
t.childChunk = t.rowContainer.AllocChunk()
t.childChunkIter = chunk.NewIterator4Chunk(t.childChunk)
t.memTracker.Consume(t.childChunk.MemoryUsage())
}
if err := t.fetchNextChunk(ctx, exec); err != nil {
return err
}
if t.executed {
break
}
isFirstGroupSameAsPrev, err := t.groupChecker.splitIntoGroups(t.childChunk)
if err != nil {
return err
}
if isFirstGroupSameAsPrev && !isEmpty {
t.selectNextGroup()
}
}
if isEmpty {
goto fetchNext
}
// iterate all data in t.rowContainer and t.childChunk
var iter chunk.Iterator
if t.rowContainer.NumChunks() != 0 {
iter = chunk.NewIterator4RowContainer(t.rowContainer)
}
if len(t.groupRowsSelected) != 0 {
if iter != nil {
iter = chunk.NewMultiIterator(iter, t.childChunkIter)
} else {
iter = t.childChunkIter
}
}
t.groupRowsIter = iter
t.groupRowsIter.Begin()
return nil
}
func (t *mergeJoinTable) fetchNextOuterGroup(ctx context.Context, exec *MergeJoinExec, requiredRows int) error {
if t.executed && t.groupChecker.isExhausted() {
return nil
}
if !t.executed && t.groupChecker.isExhausted() {
// It's hard to calculate selectivity if there is any filter or it's inner join,
// so we just push the requiredRows down when it's outer join and has no filter.
if exec.isOuterJoin && len(t.filters) == 0 {
t.childChunk.SetRequiredRows(requiredRows, exec.maxChunkSize)
}
err := t.fetchNextChunk(ctx, exec)
if err != nil || t.executed {
return err
}
t.childChunkIter.Begin()
t.filtersSelected, err = expression.VectorizedFilter(exec.ctx, t.filters, t.childChunkIter, t.filtersSelected)
if err != nil {
return err
}
_, err = t.groupChecker.splitIntoGroups(t.childChunk)
if err != nil {
return err
}
}
t.selectNextGroup()
t.groupRowsIter.Begin()
return nil
}
func (t *mergeJoinTable) hasNullInJoinKey(row chunk.Row) bool {
for _, col := range t.joinKeys {
ordinal := col.Index
if row.IsNull(ordinal) {
return true
}
}
return false
}
// Close implements the Executor Close interface.
func (e *MergeJoinExec) Close() error {
if err := e.innerTable.finish(); err != nil {
return err
}
if err := e.outerTable.finish(); err != nil {
return err
}
e.hasMatch = false
e.hasNull = false
e.memTracker = nil
e.diskTracker = nil
return e.baseExecutor.Close()
}
// Open implements the Executor Open interface.
func (e *MergeJoinExec) Open(ctx context.Context) error {
if err := e.baseExecutor.Open(ctx); err != nil {
return err
}
e.memTracker = memory.NewTracker(e.id, e.ctx.GetSessionVars().MemQuotaMergeJoin)
e.memTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.MemTracker)
e.diskTracker = disk.NewTracker(e.id, -1)
e.diskTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.DiskTracker)
e.innerTable.init(e)
e.outerTable.init(e)
return nil
}
// Next implements the Executor Next interface.
func (e *MergeJoinExec) Next(ctx context.Context, req *chunk.Chunk) (err error) {
req.Reset()
innerIter := e.innerTable.groupRowsIter
outerIter := e.outerTable.groupRowsIter
for !req.IsFull() {
if innerIter.Current() == innerIter.End() {
if err := e.innerTable.fetchNextInnerGroup(ctx, e); err != nil {
return err
}
innerIter = e.innerTable.groupRowsIter
}
if outerIter.Current() == outerIter.End() {
if err := e.outerTable.fetchNextOuterGroup(ctx, e, req.RequiredRows()-req.NumRows()); err != nil {
return err
}
outerIter = e.outerTable.groupRowsIter
if e.outerTable.executed {
return nil
}
}
cmpResult := -1
if e.desc {
cmpResult = 1
}
if innerIter.Current() != innerIter.End() {
cmpResult, err = e.compare(outerIter.Current(), innerIter.Current())
if err != nil {
return err
}
}
if (cmpResult > 0 && !e.desc) || (cmpResult < 0 && e.desc) {
innerIter.ReachEnd()
continue
}
if (cmpResult < 0 && !e.desc) || (cmpResult > 0 && e.desc) {
for row := outerIter.Current(); row != outerIter.End() && !req.IsFull(); row = outerIter.Next() {
e.joiner.onMissMatch(false, row, req)
}
continue
}
for row := outerIter.Current(); row != outerIter.End() && !req.IsFull(); row = outerIter.Next() {
if !e.outerTable.filtersSelected[row.Idx()] {
e.joiner.onMissMatch(false, row, req)
continue
}
matched, isNull, err := e.joiner.tryToMatchInners(row, innerIter, req)
if err != nil {
return err
}
e.hasMatch = e.hasMatch || matched
e.hasNull = e.hasNull || isNull
// The inner rows is not exhausted, which means the result chunk is full.
// We should keep match context, so return directly.
if innerIter.Current() != innerIter.End() && req.IsFull() {
return nil
}
if !e.hasMatch {
e.joiner.onMissMatch(e.hasNull, row, req)
}
e.hasMatch = false
e.hasNull = false
innerIter.Begin()
}
}
return nil
}
func (e *MergeJoinExec) compare(outerRow, innerRow chunk.Row) (int, error) {
outerJoinKeys := e.outerTable.joinKeys
innerJoinKeys := e.innerTable.joinKeys
for i := range outerJoinKeys {
cmp, _, err := e.compareFuncs[i](e.ctx, outerJoinKeys[i], innerJoinKeys[i], outerRow, innerRow)
if err != nil {
return 0, err
}
if cmp != 0 {
return int(cmp), nil
}
}
return 0, nil
}