-
Notifications
You must be signed in to change notification settings - Fork 5.9k
/
Copy pathjoiner.go
877 lines (794 loc) · 28.5 KB
/
joiner.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
// 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/pingcap/tidb/expression"
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/logutil"
"go.uber.org/zap"
)
var (
_ joiner = &semiJoiner{}
_ joiner = &antiSemiJoiner{}
_ joiner = &leftOuterSemiJoiner{}
_ joiner = &antiLeftOuterSemiJoiner{}
_ joiner = &leftOuterJoiner{}
_ joiner = &rightOuterJoiner{}
_ joiner = &innerJoiner{}
)
// joiner is used to generate join results according to the join type.
// A typical instruction flow is:
//
// hasMatch, hasNull := false, false
// for innerIter.Current() != innerIter.End() {
// matched, isNull, err := j.tryToMatchInners(outer, innerIter, chk)
// // handle err
// hasMatch = hasMatch || matched
// hasNull = hasNull || isNull
// }
// if !hasMatch {
// j.onMissMatch(hasNull, outer, chk)
// }
//
// NOTE: This interface is **not** thread-safe.
// TODO: unit test
// for all join type
// 1. no filter, no inline projection
// 2. no filter, inline projection
// 3. no filter, inline projection to empty column
// 4. filter, no inline projection
// 5. filter, inline projection
// 6. filter, inline projection to empty column
type joiner interface {
// tryToMatchInners 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 that when the outer row is considered unmatched, we need to differentiate
// whether the join conditions return null or false, because that matters for
// AntiSemiJoin/LeftOuterSemiJoin/AntiLeftOuterSemiJoin, by setting the return
// value isNull; for other join types, isNull is always false.
//
// NOTE: Callers need to call this function multiple times to consume all
// the inner rows for an outer row, and decide whether the outer row can be
// matched with at lease one inner row.
tryToMatchInners(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, isNull bool, err error)
// tryToMatchOuters tries to join a batch of outer rows with one inner row.
// It's used when the join is an outer join and the hash table is built
// using the outer side.
tryToMatchOuters(outer chunk.Iterator, inner chunk.Row, chk *chunk.Chunk, outerRowStatus []outerRowStatusFlag) (_ []outerRowStatusFlag, err 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 1 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.
//
// Note that, for LeftOuterSemiJoin, AntiSemiJoin and AntiLeftOuterSemiJoin,
// we need to know the reason of outer row being treated as unmatched:
// whether the join condition returns false, or returns null, because
// it decides if this outer row should be outputted, hence we have a `hasNull`
// parameter passed to `onMissMatch`.
onMissMatch(hasNull bool, outer chunk.Row, chk *chunk.Chunk)
// Clone deep copies a joiner.
Clone() joiner
}
func newJoiner(ctx sessionctx.Context, joinType plannercore.JoinType,
outerIsRight bool, defaultInner []types.Datum, filter []expression.Expression,
lhsColTypes, rhsColTypes []*types.FieldType, childrenUsed [][]bool) joiner {
base := baseJoiner{
ctx: ctx,
conditions: filter,
outerIsRight: outerIsRight,
maxChunkSize: ctx.GetSessionVars().MaxChunkSize,
}
base.selected = make([]bool, 0, chunk.InitialCapacity)
base.isNull = make([]bool, 0, chunk.InitialCapacity)
if childrenUsed != nil {
base.lUsed = make([]int, 0, len(childrenUsed[0])) // make it non-nil
for i, used := range childrenUsed[0] {
if used {
base.lUsed = append(base.lUsed, i)
}
}
base.rUsed = make([]int, 0, len(childrenUsed[1])) // make it non-nil
for i, used := range childrenUsed[1] {
if used {
base.rUsed = append(base.rUsed, i)
}
}
logutil.BgLogger().Debug("InlineProjection",
zap.Ints("lUsed", base.lUsed), zap.Ints("rUsed", base.rUsed),
zap.Int("lCount", len(lhsColTypes)), zap.Int("rCount", len(rhsColTypes)))
}
if joinType == plannercore.LeftOuterJoin || joinType == plannercore.RightOuterJoin {
innerColTypes := lhsColTypes
if !outerIsRight {
innerColTypes = rhsColTypes
}
base.initDefaultInner(innerColTypes, defaultInner)
}
// shallowRowType may be different with the output columns because output columns may
// be pruned inline, while shallow row should not be because each column may need
// be used in filter.
shallowRowType := make([]*types.FieldType, 0, len(lhsColTypes)+len(rhsColTypes))
shallowRowType = append(shallowRowType, lhsColTypes...)
shallowRowType = append(shallowRowType, rhsColTypes...)
switch joinType {
case plannercore.SemiJoin:
base.shallowRow = chunk.MutRowFromTypes(shallowRowType)
return &semiJoiner{base}
case plannercore.AntiSemiJoin:
base.shallowRow = chunk.MutRowFromTypes(shallowRowType)
return &antiSemiJoiner{base}
case plannercore.LeftOuterSemiJoin:
base.shallowRow = chunk.MutRowFromTypes(shallowRowType)
return &leftOuterSemiJoiner{base}
case plannercore.AntiLeftOuterSemiJoin:
base.shallowRow = chunk.MutRowFromTypes(shallowRowType)
return &antiLeftOuterSemiJoiner{base}
case plannercore.LeftOuterJoin, plannercore.RightOuterJoin, plannercore.InnerJoin:
if len(base.conditions) > 0 {
base.chk = chunk.NewChunkWithCapacity(shallowRowType, ctx.GetSessionVars().MaxChunkSize)
}
switch joinType {
case plannercore.LeftOuterJoin:
return &leftOuterJoiner{base}
case plannercore.RightOuterJoin:
return &rightOuterJoiner{base}
case plannercore.InnerJoin:
return &innerJoiner{base}
}
}
panic("unsupported join type in func newJoiner()")
}
type outerRowStatusFlag byte
const (
outerRowUnmatched outerRowStatusFlag = iota
outerRowMatched
outerRowHasNull
)
type baseJoiner struct {
ctx sessionctx.Context
conditions []expression.Expression
defaultInner chunk.Row
outerIsRight bool
chk *chunk.Chunk
shallowRow chunk.MutRow
selected []bool
isNull []bool
maxChunkSize int
// lUsed/rUsed show which columns are used by father for left child and right child.
// NOTE:
// 1. every columns are used if lUsed/rUsed is nil.
// 2. no columns are used if lUsed/rUsed is not nil but the size of lUsed/rUsed is 0.
lUsed, rUsed []int
}
func (j *baseJoiner) initDefaultInner(innerTypes []*types.FieldType, defaultInner []types.Datum) {
mutableRow := chunk.MutRowFromTypes(innerTypes)
mutableRow.SetDatums(defaultInner[:len(innerTypes)]...)
j.defaultInner = mutableRow.ToRow()
}
func (j *baseJoiner) makeJoinRowToChunk(chk *chunk.Chunk, lhs, rhs chunk.Row, lUsed, rUsed []int) {
// Call AppendRow() first to increment the virtual rows.
// Fix: https://github.com/pingcap/tidb/issues/5771
lWide := chk.AppendRowByColIdxs(lhs, lUsed)
chk.AppendPartialRowByColIdxs(lWide, rhs, rUsed)
}
// makeShallowJoinRow shallow copies `inner` and `outer` into `shallowRow`.
// It should not consider `j.lUsed` and `j.rUsed`, because the columns which
// need to be used in `j.conditions` may not exist in outputs.
func (j *baseJoiner) makeShallowJoinRow(isRightJoin bool, inner, outer chunk.Row) {
if !isRightJoin {
inner, outer = outer, inner
}
j.shallowRow.ShallowCopyPartialRow(0, inner)
j.shallowRow.ShallowCopyPartialRow(inner.Len(), outer)
}
// filter is used to filter the result constructed by tryToMatchInners, the result is
// built by one outer row and multiple inner rows. The returned bool value
// indicates whether the outer row matches any inner rows.
func (j *baseJoiner) filter(
input, output *chunk.Chunk, outerColsLen int,
lUsed, rUsed []int) (bool, error) {
var err error
j.selected, err = expression.VectorizedFilter(j.ctx, j.conditions, chunk.NewIterator4Chunk(input), j.selected)
if err != nil {
return false, err
}
// Batch copies selected rows to output chunk.
innerColOffset, outerColOffset := 0, input.NumCols()-outerColsLen
if !j.outerIsRight {
innerColOffset, outerColOffset = outerColsLen, 0
}
if lUsed != nil || rUsed != nil {
lSize := outerColOffset
if !j.outerIsRight {
lSize = innerColOffset
}
used := make([]int, len(lUsed)+len(rUsed))
copy(used, lUsed)
for i := range rUsed {
used[i+len(lUsed)] = rUsed[i] + lSize
}
input = input.Prune(used)
innerColOffset, outerColOffset = 0, len(lUsed)
if !j.outerIsRight {
innerColOffset, outerColOffset = len(lUsed), 0
}
}
return chunk.CopySelectedJoinRowsWithSameOuterRows(input, innerColOffset, outerColOffset, j.selected, output)
}
// filterAndCheckOuterRowStatus is used to filter the result constructed by
// tryToMatchOuters, the result is built by multiple outer rows and one inner
// row. The returned outerRowStatusFlag slice value indicates the status of
// each outer row (matched/unmatched/hasNull).
func (j *baseJoiner) filterAndCheckOuterRowStatus(
input, output *chunk.Chunk, innerColsLen int, outerRowStatus []outerRowStatusFlag,
lUsed, rUsed []int) ([]outerRowStatusFlag, error) {
var err error
j.selected, j.isNull, err = expression.VectorizedFilterConsiderNull(j.ctx, j.conditions, chunk.NewIterator4Chunk(input), j.selected, j.isNull)
if err != nil {
return nil, err
}
for i := 0; i < len(j.selected); i++ {
if j.isNull[i] {
outerRowStatus[i] = outerRowHasNull
} else if !j.selected[i] {
outerRowStatus[i] = outerRowUnmatched
}
}
if lUsed != nil || rUsed != nil {
lSize := innerColsLen
if !j.outerIsRight {
lSize = input.NumCols() - innerColsLen
}
used := make([]int, len(lUsed)+len(rUsed))
copy(used, lUsed)
for i := range rUsed {
used[i+len(lUsed)] = rUsed[i] + lSize
}
input = input.Prune(used)
}
// Batch copies selected rows to output chunk.
_, err = chunk.CopySelectedJoinRowsDirect(input, j.selected, output)
return outerRowStatus, err
}
func (j *baseJoiner) Clone() baseJoiner {
base := baseJoiner{
ctx: j.ctx,
conditions: make([]expression.Expression, 0, len(j.conditions)),
outerIsRight: j.outerIsRight,
maxChunkSize: j.maxChunkSize,
selected: make([]bool, 0, len(j.selected)),
isNull: make([]bool, 0, len(j.isNull)),
}
for _, con := range j.conditions {
base.conditions = append(base.conditions, con.Clone())
}
if j.chk != nil {
base.chk = j.chk.CopyConstruct()
} else {
base.shallowRow = chunk.MutRow(j.shallowRow.ToRow())
}
if !j.defaultInner.IsEmpty() {
base.defaultInner = j.defaultInner.CopyConstruct()
}
if j.lUsed != nil {
base.lUsed = make([]int, len(j.lUsed))
copy(base.lUsed, j.lUsed)
}
if j.rUsed != nil {
base.rUsed = make([]int, len(j.rUsed))
copy(base.rUsed, j.rUsed)
}
return base
}
type semiJoiner struct {
baseJoiner
}
func (j *semiJoiner) tryToMatchInners(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, hasNull bool, err error) {
if inners.Len() == 0 {
return false, false, nil
}
if len(j.conditions) == 0 {
chk.AppendRowByColIdxs(outer, j.lUsed) // TODO: need test numVirtualRow
inners.ReachEnd()
return true, false, nil
}
for inner := inners.Current(); inner != inners.End(); inner = inners.Next() {
j.makeShallowJoinRow(j.outerIsRight, inner, outer)
// For SemiJoin, we can safely treat null result of join conditions as false,
// so we ignore the nullness returned by EvalBool here.
matched, _, err = expression.EvalBool(j.ctx, j.conditions, j.shallowRow.ToRow())
if err != nil {
return false, false, err
}
if matched {
chk.AppendRowByColIdxs(outer, j.lUsed)
inners.ReachEnd()
return true, false, nil
}
}
err = inners.Error()
return false, false, err
}
func (j *semiJoiner) tryToMatchOuters(outers chunk.Iterator, inner chunk.Row, chk *chunk.Chunk, outerRowStatus []outerRowStatusFlag) (_ []outerRowStatusFlag, err error) {
outerRowStatus = outerRowStatus[:0]
outer, numToAppend := outers.Current(), chk.RequiredRows()-chk.NumRows()
if len(j.conditions) == 0 {
for ; outer != outers.End() && numToAppend > 0; outer, numToAppend = outers.Next(), numToAppend-1 {
chk.AppendRowByColIdxs(outer, j.lUsed)
outerRowStatus = append(outerRowStatus, outerRowMatched)
}
return outerRowStatus, nil
}
for outer := outers.Current(); outer != outers.End() && numToAppend > 0; outer, numToAppend = outers.Next(), numToAppend-1 {
j.makeShallowJoinRow(j.outerIsRight, inner, outer)
// For SemiJoin, we can safely treat null result of join conditions as false,
// so we ignore the nullness returned by EvalBool here.
matched, _, err := expression.EvalBool(j.ctx, j.conditions, j.shallowRow.ToRow())
if err != nil {
return outerRowStatus, err
}
outerRowStatus = append(outerRowStatus, outerRowMatched)
if matched {
chk.AppendRowByColIdxs(outer, j.lUsed)
}
}
err = outers.Error()
return outerRowStatus, err
}
func (j *semiJoiner) onMissMatch(_ bool, outer chunk.Row, chk *chunk.Chunk) {
}
// Clone implements joiner interface.
func (j *semiJoiner) Clone() joiner {
return &semiJoiner{baseJoiner: j.baseJoiner.Clone()}
}
type antiSemiJoiner struct {
baseJoiner
}
// tryToMatchInners implements joiner interface.
func (j *antiSemiJoiner) tryToMatchInners(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, hasNull bool, err error) {
if inners.Len() == 0 {
return false, false, nil
}
if len(j.conditions) == 0 {
inners.ReachEnd()
return true, false, nil
}
for inner := inners.Current(); inner != inners.End(); inner = inners.Next() {
j.makeShallowJoinRow(j.outerIsRight, inner, outer)
matched, isNull, err := expression.EvalBool(j.ctx, j.conditions, j.shallowRow.ToRow())
if err != nil {
return false, false, err
}
if matched {
inners.ReachEnd()
return true, false, nil
}
hasNull = hasNull || isNull
}
err = inners.Error()
return false, hasNull, err
}
func (j *antiSemiJoiner) tryToMatchOuters(outers chunk.Iterator, inner chunk.Row, chk *chunk.Chunk, outerRowStatus []outerRowStatusFlag) (_ []outerRowStatusFlag, err error) {
outerRowStatus = outerRowStatus[:0]
numToAppend := chk.RequiredRows() - chk.NumRows()
if len(j.conditions) == 0 {
for ; outers.Current() != outers.End(); outers.Next() {
outerRowStatus = append(outerRowStatus, outerRowMatched)
}
return outerRowStatus, nil
}
for outer := outers.Current(); outer != outers.End() && numToAppend > 0; outer, numToAppend = outers.Next(), numToAppend-1 {
j.makeShallowJoinRow(j.outerIsRight, inner, outer)
matched, isNull, err := expression.EvalBool(j.ctx, j.conditions, j.shallowRow.ToRow())
if err != nil {
return outerRowStatus, err
}
if matched {
outerRowStatus = append(outerRowStatus, outerRowMatched)
} else if isNull {
outerRowStatus = append(outerRowStatus, outerRowHasNull)
} else {
outerRowStatus = append(outerRowStatus, outerRowUnmatched)
}
}
err = outers.Error()
return outerRowStatus, err
}
func (j *antiSemiJoiner) onMissMatch(hasNull bool, outer chunk.Row, chk *chunk.Chunk) {
if !hasNull {
chk.AppendRowByColIdxs(outer, j.lUsed)
}
}
func (j *antiSemiJoiner) Clone() joiner {
return &antiSemiJoiner{baseJoiner: j.baseJoiner.Clone()}
}
type leftOuterSemiJoiner struct {
baseJoiner
}
// tryToMatchInners implements joiner interface.
func (j *leftOuterSemiJoiner) tryToMatchInners(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, hasNull bool, err error) {
if inners.Len() == 0 {
return false, false, nil
}
if len(j.conditions) == 0 {
j.onMatch(outer, chk)
inners.ReachEnd()
return true, false, nil
}
for inner := inners.Current(); inner != inners.End(); inner = inners.Next() {
j.makeShallowJoinRow(false, inner, outer)
matched, isNull, err := expression.EvalBool(j.ctx, j.conditions, j.shallowRow.ToRow())
if err != nil {
return false, false, err
}
if matched {
j.onMatch(outer, chk)
inners.ReachEnd()
return true, false, nil
}
hasNull = hasNull || isNull
}
err = inners.Error()
return false, hasNull, err
}
func (j *leftOuterSemiJoiner) tryToMatchOuters(outers chunk.Iterator, inner chunk.Row, chk *chunk.Chunk, outerRowStatus []outerRowStatusFlag) (_ []outerRowStatusFlag, err error) {
outerRowStatus = outerRowStatus[:0]
outer, numToAppend := outers.Current(), chk.RequiredRows()-chk.NumRows()
if len(j.conditions) == 0 {
for ; outer != outers.End() && numToAppend > 0; outer, numToAppend = outers.Next(), numToAppend-1 {
j.onMatch(outer, chk)
outerRowStatus = append(outerRowStatus, outerRowMatched)
}
return outerRowStatus, nil
}
for ; outer != outers.End() && numToAppend > 0; outer, numToAppend = outers.Next(), numToAppend-1 {
j.makeShallowJoinRow(false, inner, outer)
matched, isNull, err := expression.EvalBool(j.ctx, j.conditions, j.shallowRow.ToRow())
if err != nil {
return nil, err
}
if matched {
j.onMatch(outer, chk)
outerRowStatus = append(outerRowStatus, outerRowMatched)
} else if isNull {
outerRowStatus = append(outerRowStatus, outerRowHasNull)
} else {
outerRowStatus = append(outerRowStatus, outerRowUnmatched)
}
}
err = outers.Error()
return outerRowStatus, err
}
func (j *leftOuterSemiJoiner) onMatch(outer chunk.Row, chk *chunk.Chunk) {
lWide := chk.AppendRowByColIdxs(outer, j.lUsed)
chk.AppendInt64(lWide, 1)
}
func (j *leftOuterSemiJoiner) onMissMatch(hasNull bool, outer chunk.Row, chk *chunk.Chunk) {
lWide := chk.AppendRowByColIdxs(outer, j.lUsed)
if hasNull {
chk.AppendNull(lWide)
} else {
chk.AppendInt64(lWide, 0)
}
}
func (j *leftOuterSemiJoiner) Clone() joiner {
return &leftOuterSemiJoiner{baseJoiner: j.baseJoiner.Clone()}
}
type antiLeftOuterSemiJoiner struct {
baseJoiner
}
// tryToMatchInners implements joiner interface.
func (j *antiLeftOuterSemiJoiner) tryToMatchInners(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, hasNull bool, err error) {
if inners.Len() == 0 {
return false, false, nil
}
if len(j.conditions) == 0 {
j.onMatch(outer, chk)
inners.ReachEnd()
return true, false, nil
}
for inner := inners.Current(); inner != inners.End(); inner = inners.Next() {
j.makeShallowJoinRow(false, inner, outer)
matched, isNull, err := expression.EvalBool(j.ctx, j.conditions, j.shallowRow.ToRow())
if err != nil {
return false, false, err
}
if matched {
j.onMatch(outer, chk)
inners.ReachEnd()
return true, false, nil
}
hasNull = hasNull || isNull
}
err = inners.Error()
return false, hasNull, err
}
func (j *antiLeftOuterSemiJoiner) tryToMatchOuters(outers chunk.Iterator, inner chunk.Row, chk *chunk.Chunk, outerRowStatus []outerRowStatusFlag) (_ []outerRowStatusFlag, err error) {
outerRowStatus = outerRowStatus[:0]
outer, numToAppend := outers.Current(), chk.RequiredRows()-chk.NumRows()
if len(j.conditions) == 0 {
for ; outer != outers.End() && numToAppend > 0; outer, numToAppend = outers.Next(), numToAppend-1 {
j.onMatch(outer, chk)
outerRowStatus = append(outerRowStatus, outerRowMatched)
}
return outerRowStatus, nil
}
for i := 0; outer != outers.End() && numToAppend > 0; outer, numToAppend, i = outers.Next(), numToAppend-1, i+1 {
j.makeShallowJoinRow(false, inner, outer)
matched, isNull, err := expression.EvalBool(j.ctx, j.conditions, j.shallowRow.ToRow())
if err != nil {
return nil, err
}
if matched {
j.onMatch(outer, chk)
outerRowStatus = append(outerRowStatus, outerRowMatched)
} else if isNull {
outerRowStatus = append(outerRowStatus, outerRowHasNull)
} else {
outerRowStatus = append(outerRowStatus, outerRowUnmatched)
}
}
err = outers.Error()
if err != nil {
return
}
return outerRowStatus, nil
}
func (j *antiLeftOuterSemiJoiner) onMatch(outer chunk.Row, chk *chunk.Chunk) {
lWide := chk.AppendRowByColIdxs(outer, j.lUsed)
chk.AppendInt64(lWide, 0)
}
func (j *antiLeftOuterSemiJoiner) onMissMatch(hasNull bool, outer chunk.Row, chk *chunk.Chunk) {
lWide := chk.AppendRowByColIdxs(outer, j.lUsed)
if hasNull {
chk.AppendNull(lWide)
} else {
chk.AppendInt64(lWide, 1)
}
}
func (j *antiLeftOuterSemiJoiner) Clone() joiner {
return &antiLeftOuterSemiJoiner{baseJoiner: j.baseJoiner.Clone()}
}
type leftOuterJoiner struct {
baseJoiner
}
// tryToMatchInners implements joiner interface.
func (j *leftOuterJoiner) tryToMatchInners(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, hasNull bool, err error) {
if inners.Len() == 0 {
return false, false, nil
}
chkForJoin := chk
lUsed, rUsed := j.lUsed, j.rUsed
var lUsedForFilter, rUsedForFilter []int
if len(j.conditions) > 0 {
j.chk.Reset()
chkForJoin = j.chk
lUsed, rUsed = nil, nil
lUsedForFilter, rUsedForFilter = j.lUsed, j.rUsed
}
numToAppend := chk.RequiredRows() - chk.NumRows()
for ; inners.Current() != inners.End() && numToAppend > 0; numToAppend-- {
j.makeJoinRowToChunk(chkForJoin, outer, inners.Current(), lUsed, rUsed)
inners.Next()
}
err = inners.Error()
if err != nil {
return false, false, err
}
if len(j.conditions) == 0 {
return true, false, nil
}
// reach here, chkForJoin is j.chk
matched, err = j.filter(chkForJoin, chk, outer.Len(), lUsedForFilter, rUsedForFilter)
if err != nil {
return false, false, err
}
return matched, false, nil
}
func (j *leftOuterJoiner) tryToMatchOuters(outers chunk.Iterator, inner chunk.Row, chk *chunk.Chunk, outerRowStatus []outerRowStatusFlag) (_ []outerRowStatusFlag, err error) {
chkForJoin := chk
lUsed, rUsed := j.lUsed, j.rUsed
var lUsedForFilter, rUsedForFilter []int
if len(j.conditions) > 0 {
j.chk.Reset()
chkForJoin = j.chk
lUsed, rUsed = nil, nil
lUsedForFilter, rUsedForFilter = j.lUsed, j.rUsed
}
outer, numToAppend, cursor := outers.Current(), chk.RequiredRows()-chk.NumRows(), 0
for ; outer != outers.End() && cursor < numToAppend; outer, cursor = outers.Next(), cursor+1 {
j.makeJoinRowToChunk(chkForJoin, outer, inner, lUsed, rUsed)
}
err = outers.Error()
if err != nil {
return
}
outerRowStatus = outerRowStatus[:0]
for i := 0; i < cursor; i++ {
outerRowStatus = append(outerRowStatus, outerRowMatched)
}
if len(j.conditions) == 0 {
return outerRowStatus, nil
}
// reach here, chkForJoin is j.chk
return j.filterAndCheckOuterRowStatus(chkForJoin, chk, inner.Len(), outerRowStatus, lUsedForFilter, rUsedForFilter)
}
func (j *leftOuterJoiner) onMissMatch(_ bool, outer chunk.Row, chk *chunk.Chunk) {
lWide := chk.AppendRowByColIdxs(outer, j.lUsed)
chk.AppendPartialRowByColIdxs(lWide, j.defaultInner, j.rUsed)
}
func (j *leftOuterJoiner) Clone() joiner {
return &leftOuterJoiner{baseJoiner: j.baseJoiner.Clone()}
}
type rightOuterJoiner struct {
baseJoiner
}
// tryToMatchInners implements joiner interface.
func (j *rightOuterJoiner) tryToMatchInners(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, hasNull bool, err error) {
if inners.Len() == 0 {
return false, false, nil
}
chkForJoin := chk
lUsed, rUsed := j.lUsed, j.rUsed
var lUsedForFilter, rUsedForFilter []int
if len(j.conditions) > 0 {
j.chk.Reset()
chkForJoin = j.chk
lUsed, rUsed = nil, nil
lUsedForFilter, rUsedForFilter = j.lUsed, j.rUsed
}
numToAppend := chk.RequiredRows() - chk.NumRows()
for ; inners.Current() != inners.End() && numToAppend > 0; numToAppend-- {
j.makeJoinRowToChunk(chkForJoin, inners.Current(), outer, lUsed, rUsed)
inners.Next()
}
err = inners.Error()
if err != nil {
return false, false, err
}
if len(j.conditions) == 0 {
return true, false, nil
}
matched, err = j.filter(chkForJoin, chk, outer.Len(), lUsedForFilter, rUsedForFilter)
if err != nil {
return false, false, err
}
return matched, false, nil
}
func (j *rightOuterJoiner) tryToMatchOuters(outers chunk.Iterator, inner chunk.Row, chk *chunk.Chunk, outerRowStatus []outerRowStatusFlag) (_ []outerRowStatusFlag, err error) {
chkForJoin := chk
lUsed, rUsed := j.lUsed, j.rUsed
var lUsedForFilter, rUsedForFilter []int
if len(j.conditions) > 0 {
j.chk.Reset()
chkForJoin = j.chk
lUsed, rUsed = nil, nil
lUsedForFilter, rUsedForFilter = j.lUsed, j.rUsed
}
outer, numToAppend, cursor := outers.Current(), chk.RequiredRows()-chk.NumRows(), 0
for ; outer != outers.End() && cursor < numToAppend; outer, cursor = outers.Next(), cursor+1 {
j.makeJoinRowToChunk(chkForJoin, inner, outer, lUsed, rUsed)
}
outerRowStatus = outerRowStatus[:0]
for i := 0; i < cursor; i++ {
outerRowStatus = append(outerRowStatus, outerRowMatched)
}
if len(j.conditions) == 0 {
return outerRowStatus, nil
}
// reach here, chkForJoin is j.chk
return j.filterAndCheckOuterRowStatus(chkForJoin, chk, inner.Len(), outerRowStatus, lUsedForFilter, rUsedForFilter)
}
func (j *rightOuterJoiner) onMissMatch(_ bool, outer chunk.Row, chk *chunk.Chunk) {
lWide := chk.AppendRowByColIdxs(j.defaultInner, j.lUsed)
chk.AppendPartialRowByColIdxs(lWide, outer, j.rUsed)
}
func (j *rightOuterJoiner) Clone() joiner {
return &rightOuterJoiner{baseJoiner: j.baseJoiner.Clone()}
}
type innerJoiner struct {
baseJoiner
}
// tryToMatchInners implements joiner interface.
func (j *innerJoiner) tryToMatchInners(outer chunk.Row, inners chunk.Iterator, chk *chunk.Chunk) (matched bool, hasNull bool, err error) {
if inners.Len() == 0 {
return false, false, nil
}
chkForJoin := chk
lUsed, rUsed := j.lUsed, j.rUsed
var lUsedForFilter, rUsedForFilter []int
if len(j.conditions) > 0 {
j.chk.Reset()
chkForJoin = j.chk
lUsed, rUsed = nil, nil
lUsedForFilter, rUsedForFilter = j.lUsed, j.rUsed
}
inner, numToAppend := inners.Current(), chk.RequiredRows()-chk.NumRows()
for ; inner != inners.End() && numToAppend > 0; inner, numToAppend = inners.Next(), numToAppend-1 {
if j.outerIsRight {
j.makeJoinRowToChunk(chkForJoin, inner, outer, lUsed, rUsed)
} else {
j.makeJoinRowToChunk(chkForJoin, outer, inner, lUsed, rUsed)
}
}
err = inners.Error()
if err != nil {
return false, false, err
}
if len(j.conditions) == 0 {
return true, false, nil
}
// reach here, chkForJoin is j.chk
matched, err = j.filter(chkForJoin, chk, outer.Len(), lUsedForFilter, rUsedForFilter)
if err != nil {
return false, false, err
}
return matched, false, nil
}
func (j *innerJoiner) tryToMatchOuters(outers chunk.Iterator, inner chunk.Row, chk *chunk.Chunk, outerRowStatus []outerRowStatusFlag) (_ []outerRowStatusFlag, err error) {
chkForJoin := chk
lUsed, rUsed := j.lUsed, j.rUsed
var lUsedForFilter, rUsedForFilter []int
if len(j.conditions) > 0 {
j.chk.Reset()
chkForJoin = j.chk
lUsed, rUsed = nil, nil
lUsedForFilter, rUsedForFilter = j.lUsed, j.rUsed
}
outer, numToAppend, cursor := outers.Current(), chk.RequiredRows()-chk.NumRows(), 0
for ; outer != outers.End() && cursor < numToAppend; outer, cursor = outers.Next(), cursor+1 {
if j.outerIsRight {
j.makeJoinRowToChunk(chkForJoin, inner, outer, lUsed, rUsed)
} else {
j.makeJoinRowToChunk(chkForJoin, outer, inner, lUsed, rUsed)
}
}
err = outers.Error()
if err != nil {
return
}
outerRowStatus = outerRowStatus[:0]
for i := 0; i < cursor; i++ {
outerRowStatus = append(outerRowStatus, outerRowMatched)
}
if len(j.conditions) == 0 {
return outerRowStatus, nil
}
// reach here, chkForJoin is j.chk
return j.filterAndCheckOuterRowStatus(chkForJoin, chk, inner.Len(), outerRowStatus, lUsedForFilter, rUsedForFilter)
}
func (j *innerJoiner) onMissMatch(_ bool, outer chunk.Row, chk *chunk.Chunk) {
}
func (j *innerJoiner) Clone() joiner {
return &innerJoiner{baseJoiner: j.baseJoiner.Clone()}
}