@@ -196,10 +196,56 @@ func checkChunksEqual(t *testing.T, expectedChunks []*chunk.Chunk, resultChunks
196
196
}
197
197
}
198
198
199
+ // copy data from src to dst, the caller should ensure that src.NumCols() >= dst.NumCols()
200
+ func copySelectedRows (src * chunk.Chunk , dst * chunk.Chunk , selected []bool ) (bool , error ) {
201
+ if src .NumRows () == 0 {
202
+ return false , nil
203
+ }
204
+ if src .Sel () != nil || dst .Sel () != nil {
205
+ return false , errors .New ("copy with sel" )
206
+ }
207
+ if src .NumCols () == 0 {
208
+ numSelected := 0
209
+ for _ , s := range selected {
210
+ if s {
211
+ numSelected ++
212
+ }
213
+ }
214
+ dst .SetNumVirtualRows (dst .GetNumVirtualRows () + numSelected )
215
+ return numSelected > 0 , nil
216
+ }
217
+
218
+ oldLen := dst .NumRows ()
219
+ for j := 0 ; j < src .NumCols (); j ++ {
220
+ if j >= dst .NumCols () {
221
+ break
222
+ }
223
+ srcCol := src .Column (j )
224
+ dstCol := dst .Column (j )
225
+ chunk .CopySelectedRows (dstCol , srcCol , selected )
226
+ }
227
+ numSelected := dst .NumRows () - oldLen
228
+ dst .SetNumVirtualRows (dst .GetNumVirtualRows () + numSelected )
229
+ return numSelected > 0 , nil
230
+ }
231
+
199
232
func testJoinProbe (t * testing.T , withSel bool , leftKeyIndex []int , rightKeyIndex []int , leftKeyTypes []* types.FieldType , rightKeyTypes []* types.FieldType ,
200
233
leftTypes []* types.FieldType , rightTypes []* types.FieldType , rightAsBuildSide bool , leftUsed []int , rightUsed []int ,
201
234
leftUsedByOtherCondition []int , rightUsedByOtherCondition []int , leftFilter expression.CNFExprs , rightFilter expression.CNFExprs ,
202
235
otherCondition expression.CNFExprs , partitionNumber int , joinType logicalop.JoinType , inputRowNumber int ) {
236
+ // leftUsed/rightUsed is nil, it means select all columns
237
+ if leftUsed == nil {
238
+ leftUsed = make ([]int , 0 )
239
+ for index := range leftTypes {
240
+ leftUsed = append (leftUsed , index )
241
+ }
242
+ }
243
+ if rightUsed == nil {
244
+ rightUsed = make ([]int , 0 )
245
+ for index := range rightTypes {
246
+ rightUsed = append (rightUsed , index )
247
+ }
248
+ }
203
249
buildKeyIndex , probeKeyIndex := leftKeyIndex , rightKeyIndex
204
250
buildKeyTypes , probeKeyTypes := leftKeyTypes , rightKeyTypes
205
251
buildTypes , probeTypes := leftTypes , rightTypes
@@ -305,18 +351,27 @@ func testJoinProbe(t *testing.T, withSel bool, leftKeyIndex []int, rightKeyIndex
305
351
}
306
352
}
307
353
// check if build column can be inserted to probe column directly
308
- for i := 0 ; i < len (buildTypes ); i ++ {
354
+ for i := 0 ; i < min ( len (buildTypes ), len ( probeTypes ) ); i ++ {
309
355
buildLength := chunk .GetFixedLen (buildTypes [i ])
310
356
probeLength := chunk .GetFixedLen (probeTypes [i ])
311
357
require .Equal (t , buildLength , probeLength , "build type and probe type is not compatible" )
312
358
}
313
359
for i := 0 ; i < chunkNumber ; i ++ {
314
- buildChunks = append (buildChunks , testutil .GenRandomChunks (buildTypes , inputRowNumber ))
315
- probeChunk := testutil .GenRandomChunks (probeTypes , inputRowNumber * 2 / 3 )
316
- // copy some build data to probe side, to make sure there is some matched rows
317
- _ , err := chunk .CopySelectedJoinRowsDirect (buildChunks [i ], selected , probeChunk )
318
- probeChunks = append (probeChunks , probeChunk )
319
- require .NoError (t , err )
360
+ if len (buildTypes ) >= len (probeTypes ) {
361
+ buildChunks = append (buildChunks , testutil .GenRandomChunks (buildTypes , inputRowNumber ))
362
+ probeChunk := testutil .GenRandomChunks (probeTypes , inputRowNumber * 2 / 3 )
363
+ // copy some build data to probe side, to make sure there is some matched rows
364
+ _ , err := copySelectedRows (buildChunks [i ], probeChunk , selected )
365
+ require .NoError (t , err )
366
+ probeChunks = append (probeChunks , probeChunk )
367
+ } else {
368
+ probeChunks = append (probeChunks , testutil .GenRandomChunks (probeTypes , inputRowNumber ))
369
+ buildChunk := testutil .GenRandomChunks (buildTypes , inputRowNumber * 2 / 3 )
370
+ // copy some build data to probe side, to make sure there is some matched rows
371
+ _ , err := copySelectedRows (probeChunks [i ], buildChunk , selected )
372
+ require .NoError (t , err )
373
+ buildChunks = append (buildChunks , buildChunk )
374
+ }
320
375
}
321
376
322
377
if withSel {
@@ -436,14 +491,16 @@ func TestInnerJoinProbeBasic(t *testing.T) {
436
491
437
492
lTypes := []* types.FieldType {intTp , stringTp , uintTp , stringTp , tinyTp }
438
493
rTypes := []* types.FieldType {intTp , stringTp , uintTp , stringTp , tinyTp }
494
+ rTypes = append (rTypes , retTypes ... )
439
495
rTypes1 := []* types.FieldType {uintTp , stringTp , intTp , stringTp , tinyTp }
496
+ rTypes1 = append (rTypes1 , rTypes1 ... )
440
497
441
498
rightAsBuildSide := []bool {true , false }
442
499
partitionNumber := 4
443
500
444
501
testCases := []testCase {
445
502
// normal case
446
- {[]int {0 }, []int {0 }, []* types.FieldType {intTp }, []* types.FieldType {intTp }, lTypes , rTypes , [] int { 0 , 1 , 2 , 3 }, [] int { 0 , 1 , 2 , 3 } , nil , nil , nil },
503
+ {[]int {0 }, []int {0 }, []* types.FieldType {intTp }, []* types.FieldType {intTp }, lTypes , rTypes , nil , nil , nil , nil , nil },
447
504
// rightUsed is empty
448
505
{[]int {0 }, []int {0 }, []* types.FieldType {intTp }, []* types.FieldType {intTp }, lTypes , rTypes , []int {0 , 1 , 2 , 3 }, []int {}, nil , nil , nil },
449
506
// leftUsed is empty
@@ -570,6 +627,7 @@ func TestInnerJoinProbeOtherCondition(t *testing.T) {
570
627
571
628
lTypes := []* types.FieldType {intTp , intTp , stringTp , uintTp , stringTp }
572
629
rTypes := []* types.FieldType {intTp , intTp , stringTp , uintTp , stringTp }
630
+ rTypes = append (rTypes , rTypes ... )
573
631
574
632
tinyTp := types .NewFieldType (mysql .TypeTiny )
575
633
a := & expression.Column {Index : 1 , RetType : nullableIntTp }
@@ -585,6 +643,7 @@ func TestInnerJoinProbeOtherCondition(t *testing.T) {
585
643
testJoinProbe (t , false , []int {0 }, []int {0 }, []* types.FieldType {intTp }, []* types.FieldType {intTp }, lTypes , rTypes , rightAsBuild , []int {1 , 2 , 4 }, []int {0 }, []int {1 }, []int {3 }, nil , nil , otherCondition , partitionNumber , logicalop .InnerJoin , 200 )
586
644
testJoinProbe (t , false , []int {0 }, []int {0 }, []* types.FieldType {intTp }, []* types.FieldType {intTp }, lTypes , rTypes , rightAsBuild , []int {}, []int {}, []int {1 }, []int {3 }, nil , nil , otherCondition , partitionNumber , logicalop .InnerJoin , 200 )
587
645
testJoinProbe (t , false , []int {0 }, []int {0 }, []* types.FieldType {nullableIntTp }, []* types.FieldType {nullableIntTp }, toNullableTypes (lTypes ), toNullableTypes (rTypes ), rightAsBuild , []int {1 , 2 , 4 }, []int {0 }, []int {1 }, []int {3 }, nil , nil , otherCondition , partitionNumber , logicalop .InnerJoin , 200 )
646
+ testJoinProbe (t , false , []int {0 }, []int {0 }, []* types.FieldType {nullableIntTp }, []* types.FieldType {nullableIntTp }, toNullableTypes (lTypes ), toNullableTypes (rTypes ), rightAsBuild , nil , nil , []int {1 }, []int {3 }, nil , nil , otherCondition , partitionNumber , logicalop .InnerJoin , 200 )
588
647
}
589
648
}
590
649
@@ -602,6 +661,7 @@ func TestInnerJoinProbeWithSel(t *testing.T) {
602
661
603
662
lTypes := []* types.FieldType {intTp , intTp , stringTp , uintTp , stringTp }
604
663
rTypes := []* types.FieldType {intTp , intTp , stringTp , uintTp , stringTp }
664
+ rTypes = append (rTypes , rTypes ... )
605
665
606
666
tinyTp := types .NewFieldType (mysql .TypeTiny )
607
667
a := & expression.Column {Index : 1 , RetType : nullableIntTp }
@@ -620,6 +680,7 @@ func TestInnerJoinProbeWithSel(t *testing.T) {
620
680
testJoinProbe (t , true , []int {0 }, []int {0 }, []* types.FieldType {intTp }, []* types.FieldType {intTp }, lTypes , rTypes , rightAsBuild , []int {1 , 2 , 4 }, []int {0 }, []int {1 }, []int {3 }, nil , nil , oc , partitionNumber , logicalop .InnerJoin , 500 )
621
681
testJoinProbe (t , true , []int {0 }, []int {0 }, []* types.FieldType {intTp }, []* types.FieldType {intTp }, lTypes , rTypes , rightAsBuild , []int {}, []int {}, []int {1 }, []int {3 }, nil , nil , oc , partitionNumber , logicalop .InnerJoin , 500 )
622
682
testJoinProbe (t , true , []int {0 }, []int {0 }, []* types.FieldType {nullableIntTp }, []* types.FieldType {nullableIntTp }, toNullableTypes (lTypes ), toNullableTypes (rTypes ), rightAsBuild , []int {1 , 2 , 4 }, []int {0 }, []int {1 }, []int {3 }, nil , nil , oc , partitionNumber , logicalop .InnerJoin , 500 )
683
+ testJoinProbe (t , true , []int {0 }, []int {0 }, []* types.FieldType {nullableIntTp }, []* types.FieldType {nullableIntTp }, toNullableTypes (lTypes ), toNullableTypes (rTypes ), rightAsBuild , nil , nil , []int {1 }, []int {3 }, nil , nil , oc , partitionNumber , logicalop .InnerJoin , 500 )
623
684
}
624
685
}
625
686
}
0 commit comments