forked from pingcap/tidb
/
join_result_generators.go
426 lines (374 loc) · 13.3 KB
/
join_result_generators.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
// 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 (
"github.com/juju/errors"
"github.com/pingcap/tidb/expression"
"github.com/pingcap/tidb/plan"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
)
var (
_ joinResultGenerator = &semiJoinResultGenerator{}
_ joinResultGenerator = &antiSemiJoinResultGenerator{}
_ joinResultGenerator = &leftOuterSemiJoinResultGenerator{}
_ joinResultGenerator = &antiLeftOuterSemiJoinResultGenerator{}
_ joinResultGenerator = &leftOuterJoinResultGenerator{}
_ joinResultGenerator = &rightOuterJoinResultGenerator{}
_ joinResultGenerator = &innerJoinResultGenerator{}
)
// joinResultGenerator is used to generate join results according to the join
// type. A typical instruction flow is:
//
// hasMatch := false
// for innerIter.Current() != innerIter.End() {
// matched, err := g.tryToMatch(outer, innerIter, chk)
// // handle err
// hasMatch = hasMatch || matched
// }
// if !hasMatch {
// g.onMissMatch(outer)
// }
//
// NOTE: This interface is **not** thread-safe.
type joinResultGenerator interface {
// tryToMatch tries to join an outer row with a batch of inner rows. When
// 'inners.Len != 0' but all the joined rows are filtered, the outer row is
// considered unmatched. Otherwise, the outer row is matched and some joined
// rows are appended to `chk`. The size of `chk` is limited to MaxChunkSize.
//
// NOTE: Callers need to call this function multiple times to consume all
// the inner rows for an outer row, and dicide whether the outer row can be
// matched with at lease one inner row.
tryToMatch(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (bool, error)
// onMissMatch operates on the unmatched outer row according to the join
// type. An outer row can be considered miss matched if:
// 1. it can not pass the filter on the outer table side.
// 2. there is no inner row with the same join key.
// 3. all the joined rows can not pass the filter on the join result.
//
// On these conditions, the caller calls this function to handle the
// unmatched outer rows according to the current join type:
// 1. 'SemiJoin': ignores the unmatched outer row.
// 2. 'AntiSemiJoin': appends the unmatched outer row to the result buffer.
// 3. 'LeftOuterSemiJoin': concats the unmatched outer row with 0 and
// appends it to the result buffer.
// 4. 'AntiLeftOuterSemiJoin': concats the unmatched outer row with 0 and
// appends it to the result buffer.
// 5. 'LeftOuterJoin': concats the unmatched outer row with a row of NULLs
// and appends it to the result buffer.
// 6. 'RightOuterJoin': concats the unmatched outer row with a row of NULLs
// and appends it to the result buffer.
// 7. 'InnerJoin': ignores the unmatched outer row.
onMissMatch(outer chunk.Row, chk *chunk.Chunk)
}
func newJoinResultGenerator(ctx sessionctx.Context, joinType plan.JoinType,
outerIsRight bool, defaultInner types.DatumRow, filter []expression.Expression,
lhsColTypes, rhsColTypes []*types.FieldType) joinResultGenerator {
base := baseJoinResultGenerator{
ctx: ctx,
conditions: filter,
outerIsRight: outerIsRight,
maxChunkSize: ctx.GetSessionVars().MaxChunkSize,
}
colTypes := make([]*types.FieldType, 0, len(lhsColTypes)+len(rhsColTypes))
colTypes = append(colTypes, lhsColTypes...)
colTypes = append(colTypes, rhsColTypes...)
base.chk = chunk.NewChunk(colTypes)
base.selected = make([]bool, 0, chunk.InitialCapacity)
if joinType == plan.LeftOuterJoin || joinType == plan.RightOuterJoin {
innerColTypes := lhsColTypes
if !outerIsRight {
innerColTypes = rhsColTypes
}
base.initDefaultInner(innerColTypes, defaultInner)
}
switch joinType {
case plan.SemiJoin:
return &semiJoinResultGenerator{base}
case plan.AntiSemiJoin:
return &antiSemiJoinResultGenerator{base}
case plan.LeftOuterSemiJoin:
return &leftOuterSemiJoinResultGenerator{base}
case plan.AntiLeftOuterSemiJoin:
return &antiLeftOuterSemiJoinResultGenerator{base}
case plan.LeftOuterJoin:
return &leftOuterJoinResultGenerator{base}
case plan.RightOuterJoin:
return &rightOuterJoinResultGenerator{base}
case plan.InnerJoin:
return &innerJoinResultGenerator{base}
}
panic("unsupported join type in func newJoinResultGenerator()")
}
type baseJoinResultGenerator struct {
ctx sessionctx.Context
conditions []expression.Expression
defaultInner chunk.Row
outerIsRight bool
chk *chunk.Chunk
selected []bool
maxChunkSize int
}
func (outputer *baseJoinResultGenerator) initDefaultInner(innerTypes []*types.FieldType, defaultInner types.DatumRow) {
mutableRow := chunk.MutRowFromTypes(innerTypes)
mutableRow.SetDatums(defaultInner[:len(innerTypes)]...)
outputer.defaultInner = mutableRow.ToRow()
}
func (outputer *baseJoinResultGenerator) makeJoinRowToChunk(chk *chunk.Chunk, lhs, rhs chunk.Row) {
// Call AppendRow() first to increment the virtual rows.
// Fix: https://github.com/pingcap/tidb/issues/5771
chk.AppendRow(lhs)
chk.AppendPartialRow(lhs.Len(), rhs)
}
func (outputer *baseJoinResultGenerator) filter(input, output *chunk.Chunk) (matched bool, err error) {
outputer.selected, err = expression.VectorizedFilter(outputer.ctx, outputer.conditions, chunk.NewIterator4Chunk(input), outputer.selected)
if err != nil {
return false, errors.Trace(err)
}
for i := 0; i < len(outputer.selected); i++ {
if !outputer.selected[i] {
continue
}
matched = true
output.AppendRow(input.GetRow(i))
}
return matched, nil
}
type semiJoinResultGenerator struct {
baseJoinResultGenerator
}
func (outputer *semiJoinResultGenerator) tryToMatch(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, err error) {
if inners.Len() == 0 {
return false, nil
}
if len(outputer.conditions) == 0 {
chk.AppendPartialRow(0, outer)
inners.ReachEnd()
return true, nil
}
for inner := inners.Current(); inner != inners.End(); inner = inners.Next() {
outputer.chk.Reset()
if outputer.outerIsRight {
outputer.makeJoinRowToChunk(outputer.chk, inner, outer)
} else {
outputer.makeJoinRowToChunk(outputer.chk, outer, inner)
}
matched, err = expression.EvalBool(outputer.ctx, outputer.conditions, outputer.chk.GetRow(0))
if err != nil {
return false, errors.Trace(err)
}
if matched {
chk.AppendPartialRow(0, outer)
inners.ReachEnd()
return true, nil
}
}
return false, nil
}
func (outputer *semiJoinResultGenerator) onMissMatch(outer chunk.Row, chk *chunk.Chunk) {
}
type antiSemiJoinResultGenerator struct {
baseJoinResultGenerator
}
// tryToMatch implements joinResultGenerator interface.
func (outputer *antiSemiJoinResultGenerator) tryToMatch(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, err error) {
if inners.Len() == 0 {
return false, nil
}
if len(outputer.conditions) == 0 {
inners.ReachEnd()
return true, nil
}
for inner := inners.Current(); inner != inners.End(); inner = inners.Next() {
outputer.chk.Reset()
if outputer.outerIsRight {
outputer.makeJoinRowToChunk(outputer.chk, inner, outer)
} else {
outputer.makeJoinRowToChunk(outputer.chk, outer, inner)
}
matched, err = expression.EvalBool(outputer.ctx, outputer.conditions, outputer.chk.GetRow(0))
if err != nil {
return false, errors.Trace(err)
}
if matched {
inners.ReachEnd()
return true, nil
}
}
return false, nil
}
func (outputer *antiSemiJoinResultGenerator) onMissMatch(outer chunk.Row, chk *chunk.Chunk) {
chk.AppendRow(outer)
}
type leftOuterSemiJoinResultGenerator struct {
baseJoinResultGenerator
}
// tryToMatch implements joinResultGenerator interface.
func (outputer *leftOuterSemiJoinResultGenerator) tryToMatch(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, err error) {
if inners.Len() == 0 {
return false, nil
}
if len(outputer.conditions) == 0 {
outputer.onMatch(outer, chk)
inners.ReachEnd()
return true, nil
}
for inner := inners.Current(); inner != inners.End(); inner = inners.Next() {
outputer.chk.Reset()
outputer.makeJoinRowToChunk(outputer.chk, outer, inner)
matched, err = expression.EvalBool(outputer.ctx, outputer.conditions, outputer.chk.GetRow(0))
if err != nil {
return false, errors.Trace(err)
}
if matched {
outputer.onMatch(outer, chk)
inners.ReachEnd()
return true, nil
}
}
return false, nil
}
func (outputer *leftOuterSemiJoinResultGenerator) onMatch(outer chunk.Row, chk *chunk.Chunk) {
chk.AppendPartialRow(0, outer)
chk.AppendInt64(outer.Len(), 1)
}
func (outputer *leftOuterSemiJoinResultGenerator) onMissMatch(outer chunk.Row, chk *chunk.Chunk) {
chk.AppendPartialRow(0, outer)
chk.AppendInt64(outer.Len(), 0)
}
type antiLeftOuterSemiJoinResultGenerator struct {
baseJoinResultGenerator
}
// tryToMatch implements joinResultGenerator interface.
func (outputer *antiLeftOuterSemiJoinResultGenerator) tryToMatch(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, err error) {
if inners.Len() == 0 {
return false, nil
}
if len(outputer.conditions) == 0 {
outputer.onMatch(outer, chk)
inners.ReachEnd()
return true, nil
}
for inner := inners.Current(); inner != inners.End(); inner = inners.Next() {
outputer.chk.Reset()
outputer.makeJoinRowToChunk(outputer.chk, outer, inner)
matched, err := expression.EvalBool(outputer.ctx, outputer.conditions, outputer.chk.GetRow(0))
if err != nil {
return false, errors.Trace(err)
}
if matched {
outputer.onMatch(outer, chk)
inners.ReachEnd()
return true, nil
}
}
return false, nil
}
func (outputer *antiLeftOuterSemiJoinResultGenerator) onMatch(outer chunk.Row, chk *chunk.Chunk) {
chk.AppendPartialRow(0, outer)
chk.AppendInt64(outer.Len(), 0)
}
func (outputer *antiLeftOuterSemiJoinResultGenerator) onMissMatch(outer chunk.Row, chk *chunk.Chunk) {
chk.AppendPartialRow(0, outer)
chk.AppendInt64(outer.Len(), 1)
}
type leftOuterJoinResultGenerator struct {
baseJoinResultGenerator
}
// tryToMatch implements joinResultGenerator interface.
func (outputer *leftOuterJoinResultGenerator) tryToMatch(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (bool, error) {
if inners.Len() == 0 {
return false, nil
}
outputer.chk.Reset()
chkForJoin := outputer.chk
if len(outputer.conditions) == 0 {
chkForJoin = chk
}
numToAppend := outputer.maxChunkSize - chk.NumRows()
for ; inners.Current() != inners.End() && numToAppend > 0; numToAppend-- {
outputer.makeJoinRowToChunk(chkForJoin, outer, inners.Current())
inners.Next()
}
if len(outputer.conditions) == 0 {
return true, nil
}
// reach here, chkForJoin is outputer.chk
matched, err := outputer.filter(chkForJoin, chk)
return matched, errors.Trace(err)
}
func (outputer *leftOuterJoinResultGenerator) onMissMatch(outer chunk.Row, chk *chunk.Chunk) {
chk.AppendPartialRow(0, outer)
chk.AppendPartialRow(outer.Len(), outputer.defaultInner)
}
type rightOuterJoinResultGenerator struct {
baseJoinResultGenerator
}
// tryToMatch implements joinResultGenerator interface.
func (outputer *rightOuterJoinResultGenerator) tryToMatch(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (bool, error) {
if inners.Len() == 0 {
return false, nil
}
outputer.chk.Reset()
chkForJoin := outputer.chk
if len(outputer.conditions) == 0 {
chkForJoin = chk
}
numToAppend := outputer.maxChunkSize - chk.NumRows()
for ; inners.Current() != inners.End() && numToAppend > 0; numToAppend-- {
outputer.makeJoinRowToChunk(chkForJoin, inners.Current(), outer)
inners.Next()
}
if len(outputer.conditions) == 0 {
return true, nil
}
// reach here, chkForJoin is outputer.chk
matched, err := outputer.filter(chkForJoin, chk)
return matched, errors.Trace(err)
}
func (outputer *rightOuterJoinResultGenerator) onMissMatch(outer chunk.Row, chk *chunk.Chunk) {
chk.AppendPartialRow(0, outputer.defaultInner)
chk.AppendPartialRow(outputer.defaultInner.Len(), outer)
}
type innerJoinResultGenerator struct {
baseJoinResultGenerator
}
// tryToMatch implements joinResultGenerator interface.
func (outputer *innerJoinResultGenerator) tryToMatch(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (bool, error) {
if inners.Len() == 0 {
return false, nil
}
outputer.chk.Reset()
chkForJoin := outputer.chk
if len(outputer.conditions) == 0 {
chkForJoin = chk
}
inner, numToAppend := inners.Current(), outputer.maxChunkSize-chk.NumRows()
for ; inner != inners.End() && numToAppend > 0; inner, numToAppend = inners.Next(), numToAppend-1 {
if outputer.outerIsRight {
outputer.makeJoinRowToChunk(chkForJoin, inner, outer)
} else {
outputer.makeJoinRowToChunk(chkForJoin, outer, inner)
}
}
if len(outputer.conditions) == 0 {
return true, nil
}
// reach here, chkForJoin is outputer.chk
matched, err := outputer.filter(chkForJoin, chk)
return matched, errors.Trace(err)
}
func (outputer *innerJoinResultGenerator) onMissMatch(outer chunk.Row, chk *chunk.Chunk) {
}