-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathcolumnar_operators_test.go
1131 lines (1081 loc) · 39.4 KB
/
columnar_operators_test.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
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package distsql
import (
"context"
"fmt"
"math/rand"
"sort"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/typeconv"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/catalog/descpb"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/colbuilder"
"github.com/cockroachdb/cockroach/pkg/sql/execinfrapb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/randutil"
"github.com/stretchr/testify/require"
)
const nullProbability = 0.2
const randTypesProbability = 0.5
func TestAggregatorAgainstProcessor(t *testing.T) {
defer leaktest.AfterTest(t)()
st := cluster.MakeTestingClusterSettings()
evalCtx := tree.MakeTestingEvalContext(st)
defer evalCtx.Stop(context.Background())
rng, seed := randutil.NewPseudoRand()
nRuns := 20
nRows := 100
nAggFnsToTest := 5
const (
maxNumGroupingCols = 3
nextGroupProb = 0.2
)
groupingCols := make([]uint32, maxNumGroupingCols)
orderingCols := make([]execinfrapb.Ordering_Column, maxNumGroupingCols)
for i := uint32(0); i < maxNumGroupingCols; i++ {
groupingCols[i] = i
orderingCols[i].ColIdx = i
}
var da sqlbase.DatumAlloc
// We need +1 because an entry for index=6 was omitted by mistake.
numSupportedAggFns := len(execinfrapb.AggregatorSpec_Func_name) + 1
aggregations := make([]execinfrapb.AggregatorSpec_Aggregation, 0, nAggFnsToTest)
for len(aggregations) < nAggFnsToTest {
var aggFn execinfrapb.AggregatorSpec_Func
found := false
for !found {
aggFn = execinfrapb.AggregatorSpec_Func(rng.Intn(numSupportedAggFns))
if _, valid := execinfrapb.AggregateFuncToNumArguments[aggFn]; !valid {
continue
}
switch aggFn {
case execinfrapb.AggregatorSpec_ANY_NOT_NULL:
// We skip ANY_NOT_NULL aggregate function because it returns
// non-deterministic results.
case execinfrapb.AggregatorSpec_PERCENTILE_DISC_IMPL,
execinfrapb.AggregatorSpec_PERCENTILE_CONT_IMPL:
// We skip percentile functions because those can only be
// planned as window functions.
default:
found = true
}
}
aggregations = append(aggregations, execinfrapb.AggregatorSpec_Aggregation{Func: aggFn})
}
for _, hashAgg := range []bool{false, true} {
for numGroupingCols := 1; numGroupingCols <= maxNumGroupingCols; numGroupingCols++ {
// We will be grouping based on the first numGroupingCols columns
// (which will be of INT types) with the values for the columns set
// manually below.
inputTypes := make([]*types.T, 0, numGroupingCols+len(aggregations))
for i := 0; i < numGroupingCols; i++ {
inputTypes = append(inputTypes, types.Int)
}
// After all grouping columns, we will have input columns for each
// of the aggregate functions. Here, we will set up the column
// indices, and the types will be regenerated below
numColsSoFar := numGroupingCols
for i := range aggregations {
numArguments := execinfrapb.AggregateFuncToNumArguments[aggregations[i].Func]
aggregations[i].ColIdx = make([]uint32, numArguments)
for j := range aggregations[i].ColIdx {
aggregations[i].ColIdx[j] = uint32(numColsSoFar)
numColsSoFar++
}
}
outputTypes := make([]*types.T, len(aggregations))
for run := 0; run < nRuns; run++ {
inputTypes = inputTypes[:numGroupingCols]
var rows sqlbase.EncDatumRows
for i := range aggregations {
aggFn := aggregations[i].Func
aggFnInputTypes := make([]*types.T, len(aggregations[i].ColIdx))
for {
for j := range aggFnInputTypes {
aggFnInputTypes[j] = sqlbase.RandType(rng)
}
// There is a special case for string_agg when at least
// one of the arguments is an empty tuple. Such case
// passes GetAggregateInfo check below, but it is
// actually invalid, and during normal execution it is
// caught during type-checking. However, we don't want
// to do fully-fledged type checking, so we hard-code
// an exception here.
invalid := false
if aggFn == execinfrapb.AggregatorSpec_STRING_AGG {
for _, typ := range aggFnInputTypes {
if typ.Family() == types.TupleFamily && len(typ.TupleContents()) == 0 {
invalid = true
}
}
}
if invalid {
continue
}
if _, outputType, err := execinfrapb.GetAggregateInfo(aggFn, aggFnInputTypes...); err == nil {
outputTypes[i] = outputType
break
}
}
inputTypes = append(inputTypes, aggFnInputTypes...)
}
rows = sqlbase.RandEncDatumRowsOfTypes(rng, nRows, inputTypes)
groupIdx := 0
for _, row := range rows {
for i := 0; i < numGroupingCols; i++ {
if rng.Float64() < nullProbability {
row[i] = sqlbase.EncDatum{Datum: tree.DNull}
} else {
row[i] = sqlbase.EncDatum{Datum: tree.NewDInt(tree.DInt(groupIdx))}
if rng.Float64() < nextGroupProb {
groupIdx++
}
}
}
}
aggregatorSpec := &execinfrapb.AggregatorSpec{
Type: execinfrapb.AggregatorSpec_NON_SCALAR,
GroupCols: groupingCols[:numGroupingCols],
Aggregations: aggregations,
}
if hashAgg {
// Let's shuffle the rows for the hash aggregator.
rand.Shuffle(nRows, func(i, j int) {
rows[i], rows[j] = rows[j], rows[i]
})
} else {
aggregatorSpec.OrderedGroupCols = groupingCols[:numGroupingCols]
orderedCols := execinfrapb.ConvertToColumnOrdering(
execinfrapb.Ordering{Columns: orderingCols[:numGroupingCols]},
)
// Although we build the input rows in "non-decreasing" order, it is
// possible that some NULL values are present here and there, so we
// need to sort the rows to satisfy the ordering conditions.
sort.Slice(rows, func(i, j int) bool {
cmp, err := rows[i].Compare(inputTypes, &da, orderedCols, &evalCtx, rows[j])
if err != nil {
t.Fatal(err)
}
return cmp < 0
})
}
pspec := &execinfrapb.ProcessorSpec{
Input: []execinfrapb.InputSyncSpec{{ColumnTypes: inputTypes}},
Core: execinfrapb.ProcessorCoreUnion{Aggregator: aggregatorSpec},
}
args := verifyColOperatorArgs{
anyOrder: hashAgg,
inputTypes: [][]*types.T{inputTypes},
inputs: []sqlbase.EncDatumRows{rows},
outputTypes: outputTypes,
pspec: pspec,
}
if err := verifyColOperator(args); err != nil {
if strings.Contains(err.Error(), "different errors returned") {
// Columnar and row-based aggregators are likely to hit
// different errors, and we will swallow those and move
// on.
continue
// TODO(yuzefovich): here is a more tight condition,
// should we use it? I'm worried that it'll be flaky,
// and it is quite likely that we're generating invalid
// input data, so the operators and processors will be
// getting errors often.
//if strings.Contains(err.Error(), "unexpected type *tree.DOidWrapper for ") ||
// strings.Contains(err.Error(), "field name must not be null") ||
// strings.Contains(err.Error(), "out of range") ||
// strings.Contains(err.Error(), "number of values in aggregate exceed max count of 9223372036854775807") ||
// strings.Contains(err.Error(), "bit strings of different sizes") ||
// strings.Contains(err.Error(), "key value must be scalar, not array, tuple, or json") ||
// strings.Contains(err.Error(), "arguments to xor must all be the same length") {
// continue
//}
}
fmt.Printf("--- seed = %d run = %d hash = %t ---\n",
seed, run, hashAgg)
var aggFnNames string
for i, agg := range aggregations {
if i > 0 {
aggFnNames += " "
}
aggFnNames += agg.Func.String()
}
fmt.Printf("--- %s ---\n", aggFnNames)
prettyPrintTypes(inputTypes, "t" /* tableName */)
prettyPrintInput(rows, inputTypes, "t" /* tableName */)
t.Fatal(err)
}
}
}
}
}
func TestDistinctAgainstProcessor(t *testing.T) {
defer leaktest.AfterTest(t)()
var da sqlbase.DatumAlloc
evalCtx := tree.MakeTestingEvalContext(cluster.MakeTestingClusterSettings())
defer evalCtx.Stop(context.Background())
rng, seed := randutil.NewPseudoRand()
nRuns := 10
nRows := 10
maxCols := 3
maxNum := 3
intTyps := make([]*types.T, maxCols)
for i := range intTyps {
intTyps[i] = types.Int
}
for run := 0; run < nRuns; run++ {
for nCols := 1; nCols <= maxCols; nCols++ {
for nDistinctCols := 1; nDistinctCols <= nCols; nDistinctCols++ {
for nOrderedCols := 0; nOrderedCols <= nDistinctCols; nOrderedCols++ {
var (
rows sqlbase.EncDatumRows
inputTypes []*types.T
ordCols []execinfrapb.Ordering_Column
)
if rng.Float64() < randTypesProbability {
inputTypes = generateRandomSupportedTypes(rng, nCols)
rows = sqlbase.RandEncDatumRowsOfTypes(rng, nRows, inputTypes)
} else {
inputTypes = intTyps[:nCols]
rows = sqlbase.MakeRandIntRowsInRange(rng, nRows, nCols, maxNum, nullProbability)
}
distinctCols := make([]uint32, nDistinctCols)
for i, distinctCol := range rng.Perm(nCols)[:nDistinctCols] {
distinctCols[i] = uint32(distinctCol)
}
orderedCols := make([]uint32, nOrderedCols)
for i, orderedColIdx := range rng.Perm(nDistinctCols)[:nOrderedCols] {
// From the set of distinct columns we need to choose nOrderedCols
// to be in the ordered columns set.
orderedCols[i] = distinctCols[orderedColIdx]
}
ordCols = make([]execinfrapb.Ordering_Column, nOrderedCols)
for i, col := range orderedCols {
ordCols[i] = execinfrapb.Ordering_Column{
ColIdx: col,
}
}
sort.Slice(rows, func(i, j int) bool {
cmp, err := rows[i].Compare(
inputTypes, &da,
execinfrapb.ConvertToColumnOrdering(execinfrapb.Ordering{Columns: ordCols}),
&evalCtx, rows[j],
)
if err != nil {
t.Fatal(err)
}
return cmp < 0
})
spec := &execinfrapb.DistinctSpec{
DistinctColumns: distinctCols,
OrderedColumns: orderedCols,
}
pspec := &execinfrapb.ProcessorSpec{
Input: []execinfrapb.InputSyncSpec{{ColumnTypes: inputTypes}},
Core: execinfrapb.ProcessorCoreUnion{Distinct: spec},
}
args := verifyColOperatorArgs{
anyOrder: false,
inputTypes: [][]*types.T{inputTypes},
inputs: []sqlbase.EncDatumRows{rows},
outputTypes: inputTypes,
pspec: pspec,
}
if err := verifyColOperator(args); err != nil {
fmt.Printf("--- seed = %d run = %d nCols = %d distinct cols = %v ordered cols = %v ---\n",
seed, run, nCols, distinctCols, orderedCols)
prettyPrintTypes(inputTypes, "t" /* tableName */)
prettyPrintInput(rows, inputTypes, "t" /* tableName */)
t.Fatal(err)
}
}
}
}
}
}
func TestSorterAgainstProcessor(t *testing.T) {
defer leaktest.AfterTest(t)()
st := cluster.MakeTestingClusterSettings()
evalCtx := tree.MakeTestingEvalContext(st)
defer evalCtx.Stop(context.Background())
rng, seed := randutil.NewPseudoRand()
nRuns := 5
nRows := 8 * coldata.BatchSize()
maxCols := 5
maxNum := 10
intTyps := make([]*types.T, maxCols)
for i := range intTyps {
intTyps[i] = types.Int
}
for _, spillForced := range []bool{false, true} {
for run := 0; run < nRuns; run++ {
for nCols := 1; nCols <= maxCols; nCols++ {
// We will try both general sort and top K sort.
for _, topK := range []uint64{0, uint64(1 + rng.Intn(64))} {
var (
rows sqlbase.EncDatumRows
inputTypes []*types.T
)
if rng.Float64() < randTypesProbability {
inputTypes = generateRandomSupportedTypes(rng, nCols)
rows = sqlbase.RandEncDatumRowsOfTypes(rng, nRows, inputTypes)
} else {
inputTypes = intTyps[:nCols]
rows = sqlbase.MakeRandIntRowsInRange(rng, nRows, nCols, maxNum, nullProbability)
}
// Note: we're only generating column orderings on all nCols columns since
// if there are columns not in the ordering, the results are not fully
// deterministic.
orderingCols := generateColumnOrdering(rng, nCols, nCols)
sorterSpec := &execinfrapb.SorterSpec{
OutputOrdering: execinfrapb.Ordering{Columns: orderingCols},
}
var limit, offset uint64
if topK > 0 {
offset = uint64(rng.Intn(int(topK)))
limit = topK - offset
}
pspec := &execinfrapb.ProcessorSpec{
Input: []execinfrapb.InputSyncSpec{{ColumnTypes: inputTypes}},
Core: execinfrapb.ProcessorCoreUnion{Sorter: sorterSpec},
Post: execinfrapb.PostProcessSpec{Limit: limit, Offset: offset},
}
args := verifyColOperatorArgs{
inputTypes: [][]*types.T{inputTypes},
inputs: []sqlbase.EncDatumRows{rows},
outputTypes: inputTypes,
pspec: pspec,
forceDiskSpill: spillForced,
}
if spillForced {
args.numForcedRepartitions = 2 + rng.Intn(3)
}
if err := verifyColOperator(args); err != nil {
fmt.Printf("--- seed = %d spillForced = %t nCols = %d K = %d ---\n",
seed, spillForced, nCols, topK)
prettyPrintTypes(inputTypes, "t" /* tableName */)
prettyPrintInput(rows, inputTypes, "t" /* tableName */)
t.Fatal(err)
}
}
}
}
}
}
func TestSortChunksAgainstProcessor(t *testing.T) {
defer leaktest.AfterTest(t)()
var da sqlbase.DatumAlloc
st := cluster.MakeTestingClusterSettings()
evalCtx := tree.MakeTestingEvalContext(st)
defer evalCtx.Stop(context.Background())
rng, seed := randutil.NewPseudoRand()
nRuns := 5
nRows := 5 * coldata.BatchSize() / 4
maxCols := 3
maxNum := 10
intTyps := make([]*types.T, maxCols)
for i := range intTyps {
intTyps[i] = types.Int
}
for _, spillForced := range []bool{false, true} {
for run := 0; run < nRuns; run++ {
for nCols := 2; nCols <= maxCols; nCols++ {
for matchLen := 1; matchLen < nCols; matchLen++ {
var (
rows sqlbase.EncDatumRows
inputTypes []*types.T
)
if rng.Float64() < randTypesProbability {
inputTypes = generateRandomSupportedTypes(rng, nCols)
rows = sqlbase.RandEncDatumRowsOfTypes(rng, nRows, inputTypes)
} else {
inputTypes = intTyps[:nCols]
rows = sqlbase.MakeRandIntRowsInRange(rng, nRows, nCols, maxNum, nullProbability)
}
// Note: we're only generating column orderings on all nCols columns since
// if there are columns not in the ordering, the results are not fully
// deterministic.
orderingCols := generateColumnOrdering(rng, nCols, nCols)
matchedCols := execinfrapb.ConvertToColumnOrdering(execinfrapb.Ordering{Columns: orderingCols[:matchLen]})
// Presort the input on first matchLen columns.
sort.Slice(rows, func(i, j int) bool {
cmp, err := rows[i].Compare(inputTypes, &da, matchedCols, &evalCtx, rows[j])
if err != nil {
t.Fatal(err)
}
return cmp < 0
})
sorterSpec := &execinfrapb.SorterSpec{
OutputOrdering: execinfrapb.Ordering{Columns: orderingCols},
OrderingMatchLen: uint32(matchLen),
}
pspec := &execinfrapb.ProcessorSpec{
Input: []execinfrapb.InputSyncSpec{{ColumnTypes: inputTypes}},
Core: execinfrapb.ProcessorCoreUnion{Sorter: sorterSpec},
}
args := verifyColOperatorArgs{
inputTypes: [][]*types.T{inputTypes},
inputs: []sqlbase.EncDatumRows{rows},
outputTypes: inputTypes,
pspec: pspec,
forceDiskSpill: spillForced,
}
if err := verifyColOperator(args); err != nil {
fmt.Printf("--- seed = %d spillForced = %t orderingCols = %v matchLen = %d run = %d ---\n",
seed, spillForced, orderingCols, matchLen, run)
prettyPrintTypes(inputTypes, "t" /* tableName */)
prettyPrintInput(rows, inputTypes, "t" /* tableName */)
t.Fatal(err)
}
}
}
}
}
}
func TestHashJoinerAgainstProcessor(t *testing.T) {
defer leaktest.AfterTest(t)()
evalCtx := tree.MakeTestingEvalContext(cluster.MakeTestingClusterSettings())
defer evalCtx.Stop(context.Background())
type hjTestSpec struct {
joinType descpb.JoinType
onExprSupported bool
}
testSpecs := []hjTestSpec{
{
joinType: descpb.InnerJoin,
onExprSupported: true,
},
{
joinType: descpb.LeftOuterJoin,
},
{
joinType: descpb.RightOuterJoin,
},
{
joinType: descpb.FullOuterJoin,
},
{
joinType: descpb.LeftSemiJoin,
},
{
joinType: descpb.LeftAntiJoin,
},
{
joinType: descpb.IntersectAllJoin,
},
{
joinType: descpb.ExceptAllJoin,
},
}
rng, seed := randutil.NewPseudoRand()
nRuns := 3
nRows := 10
maxCols := 3
maxNum := 5
intTyps := make([]*types.T, maxCols)
for i := range intTyps {
intTyps[i] = types.Int
}
for _, spillForced := range []bool{false, true} {
for run := 0; run < nRuns; run++ {
for _, testSpec := range testSpecs {
for nCols := 1; nCols <= maxCols; nCols++ {
for nEqCols := 1; nEqCols <= nCols; nEqCols++ {
for _, addFilter := range getAddFilterOptions(testSpec.joinType, nEqCols < nCols) {
triedWithoutOnExpr, triedWithOnExpr := false, false
if !testSpec.onExprSupported {
triedWithOnExpr = true
}
for !triedWithoutOnExpr || !triedWithOnExpr {
var (
lRows, rRows sqlbase.EncDatumRows
lEqCols, rEqCols []uint32
lInputTypes, rInputTypes []*types.T
usingRandomTypes bool
)
if rng.Float64() < randTypesProbability {
lInputTypes = generateRandomSupportedTypes(rng, nCols)
lEqCols = generateEqualityColumns(rng, nCols, nEqCols)
rInputTypes = append(rInputTypes[:0], lInputTypes...)
rEqCols = append(rEqCols[:0], lEqCols...)
rng.Shuffle(nEqCols, func(i, j int) {
iColIdx, jColIdx := rEqCols[i], rEqCols[j]
rInputTypes[iColIdx], rInputTypes[jColIdx] = rInputTypes[jColIdx], rInputTypes[iColIdx]
rEqCols[i], rEqCols[j] = rEqCols[j], rEqCols[i]
})
rInputTypes = generateRandomComparableTypes(rng, rInputTypes)
lRows = sqlbase.RandEncDatumRowsOfTypes(rng, nRows, lInputTypes)
rRows = sqlbase.RandEncDatumRowsOfTypes(rng, nRows, rInputTypes)
usingRandomTypes = true
} else {
lInputTypes = intTyps[:nCols]
rInputTypes = lInputTypes
lRows = sqlbase.MakeRandIntRowsInRange(rng, nRows, nCols, maxNum, nullProbability)
rRows = sqlbase.MakeRandIntRowsInRange(rng, nRows, nCols, maxNum, nullProbability)
lEqCols = generateEqualityColumns(rng, nCols, nEqCols)
rEqCols = generateEqualityColumns(rng, nCols, nEqCols)
}
var outputTypes []*types.T
if testSpec.joinType.ShouldIncludeRightColsInOutput() {
outputTypes = append(lInputTypes, rInputTypes...)
} else {
outputTypes = lInputTypes
}
outputColumns := make([]uint32, len(outputTypes))
for i := range outputColumns {
outputColumns[i] = uint32(i)
}
var filter, onExpr execinfrapb.Expression
if addFilter {
colTypes := append(lInputTypes, rInputTypes...)
filter = generateFilterExpr(
rng, nCols, nEqCols, colTypes, usingRandomTypes,
!testSpec.joinType.ShouldIncludeRightColsInOutput(),
)
}
if triedWithoutOnExpr {
colTypes := append(lInputTypes, rInputTypes...)
onExpr = generateFilterExpr(
rng, nCols, nEqCols, colTypes, usingRandomTypes, false, /* forceLeftSide */
)
}
hjSpec := &execinfrapb.HashJoinerSpec{
LeftEqColumns: lEqCols,
RightEqColumns: rEqCols,
OnExpr: onExpr,
Type: testSpec.joinType,
}
pspec := &execinfrapb.ProcessorSpec{
Input: []execinfrapb.InputSyncSpec{
{ColumnTypes: lInputTypes},
{ColumnTypes: rInputTypes},
},
Core: execinfrapb.ProcessorCoreUnion{HashJoiner: hjSpec},
Post: execinfrapb.PostProcessSpec{
Projection: true,
OutputColumns: outputColumns,
Filter: filter,
},
}
args := verifyColOperatorArgs{
anyOrder: true,
inputTypes: [][]*types.T{lInputTypes, rInputTypes},
inputs: []sqlbase.EncDatumRows{lRows, rRows},
outputTypes: outputTypes,
pspec: pspec,
forceDiskSpill: spillForced,
// It is possible that we have a filter that is always false, and this
// will allow us to plan a zero operator which always returns a zero
// batch. In such case, the spilling might not occur and that's ok.
forcedDiskSpillMightNotOccur: !filter.Empty() || !onExpr.Empty(),
numForcedRepartitions: 2,
rng: rng,
}
if testSpec.joinType.IsSetOpJoin() && nEqCols < nCols {
// The output of set operation joins is not fully
// deterministic when there are non-equality
// columns, however, the rows must match on the
// equality columns between vectorized and row
// executions.
args.colIdxsToCheckForEquality = make([]int, nEqCols)
for i := range args.colIdxsToCheckForEquality {
args.colIdxsToCheckForEquality[i] = int(lEqCols[i])
}
}
if err := verifyColOperator(args); err != nil {
fmt.Printf("--- spillForced = %t join type = %s onExpr = %q"+
" filter = %q seed = %d run = %d ---\n",
spillForced, testSpec.joinType.String(), onExpr.Expr, filter.Expr, seed, run)
fmt.Printf("--- lEqCols = %v rEqCols = %v ---\n", lEqCols, rEqCols)
prettyPrintTypes(lInputTypes, "left_table" /* tableName */)
prettyPrintTypes(rInputTypes, "right_table" /* tableName */)
prettyPrintInput(lRows, lInputTypes, "left_table" /* tableName */)
prettyPrintInput(rRows, rInputTypes, "right_table" /* tableName */)
t.Fatal(err)
}
if onExpr.Expr == "" {
triedWithoutOnExpr = true
} else {
triedWithOnExpr = true
}
}
}
}
}
}
}
}
}
// generateEqualityColumns produces a random permutation of nEqCols random
// columns on a table with nCols columns, so nEqCols must be not greater than
// nCols.
func generateEqualityColumns(rng *rand.Rand, nCols int, nEqCols int) []uint32 {
if nEqCols > nCols {
panic("nEqCols > nCols in generateEqualityColumns")
}
eqCols := make([]uint32, 0, nEqCols)
for _, eqCol := range rng.Perm(nCols)[:nEqCols] {
eqCols = append(eqCols, uint32(eqCol))
}
return eqCols
}
func TestMergeJoinerAgainstProcessor(t *testing.T) {
defer leaktest.AfterTest(t)()
var da sqlbase.DatumAlloc
evalCtx := tree.MakeTestingEvalContext(cluster.MakeTestingClusterSettings())
defer evalCtx.Stop(context.Background())
type mjTestSpec struct {
joinType descpb.JoinType
anyOrder bool
onExprSupported bool
}
testSpecs := []mjTestSpec{
{
joinType: descpb.InnerJoin,
onExprSupported: true,
},
{
joinType: descpb.LeftOuterJoin,
},
{
joinType: descpb.RightOuterJoin,
},
{
joinType: descpb.FullOuterJoin,
// FULL OUTER JOIN doesn't guarantee any ordering on its output (since it
// is ambiguous), so we're comparing the outputs as sets.
anyOrder: true,
},
{
joinType: descpb.LeftSemiJoin,
},
{
joinType: descpb.LeftAntiJoin,
},
{
joinType: descpb.IntersectAllJoin,
},
{
joinType: descpb.ExceptAllJoin,
},
}
rng, seed := randutil.NewPseudoRand()
nRuns := 3
nRows := 10
maxCols := 3
maxNum := 5
intTyps := make([]*types.T, maxCols)
for i := range intTyps {
intTyps[i] = types.Int
}
for run := 0; run < nRuns; run++ {
for _, testSpec := range testSpecs {
for nCols := 1; nCols <= maxCols; nCols++ {
for nOrderingCols := 1; nOrderingCols <= nCols; nOrderingCols++ {
for _, addFilter := range getAddFilterOptions(testSpec.joinType, nOrderingCols < nCols) {
triedWithoutOnExpr, triedWithOnExpr := false, false
if !testSpec.onExprSupported {
triedWithOnExpr = true
}
for !triedWithoutOnExpr || !triedWithOnExpr {
var (
lRows, rRows sqlbase.EncDatumRows
lInputTypes, rInputTypes []*types.T
lOrderingCols, rOrderingCols []execinfrapb.Ordering_Column
usingRandomTypes bool
)
if rng.Float64() < randTypesProbability {
lInputTypes = generateRandomSupportedTypes(rng, nCols)
lOrderingCols = generateColumnOrdering(rng, nCols, nOrderingCols)
rInputTypes = append(rInputTypes[:0], lInputTypes...)
rOrderingCols = append(rOrderingCols[:0], lOrderingCols...)
rng.Shuffle(nOrderingCols, func(i, j int) {
iColIdx, jColIdx := rOrderingCols[i].ColIdx, rOrderingCols[j].ColIdx
rInputTypes[iColIdx], rInputTypes[jColIdx] = rInputTypes[jColIdx], rInputTypes[iColIdx]
rOrderingCols[i], rOrderingCols[j] = rOrderingCols[j], rOrderingCols[i]
})
rInputTypes = generateRandomComparableTypes(rng, rInputTypes)
lRows = sqlbase.RandEncDatumRowsOfTypes(rng, nRows, lInputTypes)
rRows = sqlbase.RandEncDatumRowsOfTypes(rng, nRows, rInputTypes)
usingRandomTypes = true
} else {
lInputTypes = intTyps[:nCols]
rInputTypes = lInputTypes
lRows = sqlbase.MakeRandIntRowsInRange(rng, nRows, nCols, maxNum, nullProbability)
rRows = sqlbase.MakeRandIntRowsInRange(rng, nRows, nCols, maxNum, nullProbability)
lOrderingCols = generateColumnOrdering(rng, nCols, nOrderingCols)
rOrderingCols = generateColumnOrdering(rng, nCols, nOrderingCols)
}
// Set the directions of both columns to be the same.
for i, lCol := range lOrderingCols {
rOrderingCols[i].Direction = lCol.Direction
}
lMatchedCols := execinfrapb.ConvertToColumnOrdering(execinfrapb.Ordering{Columns: lOrderingCols})
rMatchedCols := execinfrapb.ConvertToColumnOrdering(execinfrapb.Ordering{Columns: rOrderingCols})
sort.Slice(lRows, func(i, j int) bool {
cmp, err := lRows[i].Compare(lInputTypes, &da, lMatchedCols, &evalCtx, lRows[j])
if err != nil {
t.Fatal(err)
}
return cmp < 0
})
sort.Slice(rRows, func(i, j int) bool {
cmp, err := rRows[i].Compare(rInputTypes, &da, rMatchedCols, &evalCtx, rRows[j])
if err != nil {
t.Fatal(err)
}
return cmp < 0
})
var outputTypes []*types.T
if testSpec.joinType.ShouldIncludeRightColsInOutput() {
outputTypes = append(lInputTypes, rInputTypes...)
} else {
outputTypes = lInputTypes
}
outputColumns := make([]uint32, len(outputTypes))
for i := range outputColumns {
outputColumns[i] = uint32(i)
}
var filter, onExpr execinfrapb.Expression
if addFilter {
colTypes := append(lInputTypes, rInputTypes...)
filter = generateFilterExpr(
rng, nCols, nOrderingCols, colTypes, usingRandomTypes,
!testSpec.joinType.ShouldIncludeRightColsInOutput(),
)
}
if triedWithoutOnExpr {
colTypes := append(lInputTypes, rInputTypes...)
onExpr = generateFilterExpr(
rng, nCols, nOrderingCols, colTypes, usingRandomTypes, false, /* forceLeftSide */
)
}
mjSpec := &execinfrapb.MergeJoinerSpec{
OnExpr: onExpr,
LeftOrdering: execinfrapb.Ordering{Columns: lOrderingCols},
RightOrdering: execinfrapb.Ordering{Columns: rOrderingCols},
Type: testSpec.joinType,
NullEquality: testSpec.joinType.IsSetOpJoin(),
}
pspec := &execinfrapb.ProcessorSpec{
Input: []execinfrapb.InputSyncSpec{{ColumnTypes: lInputTypes}, {ColumnTypes: rInputTypes}},
Core: execinfrapb.ProcessorCoreUnion{MergeJoiner: mjSpec},
Post: execinfrapb.PostProcessSpec{Projection: true, OutputColumns: outputColumns, Filter: filter},
}
args := verifyColOperatorArgs{
anyOrder: testSpec.anyOrder,
inputTypes: [][]*types.T{lInputTypes, rInputTypes},
inputs: []sqlbase.EncDatumRows{lRows, rRows},
outputTypes: outputTypes,
pspec: pspec,
rng: rng,
}
if testSpec.joinType.IsSetOpJoin() && nOrderingCols < nCols {
// The output of set operation joins is not fully
// deterministic when there are non-equality
// columns, however, the rows must match on the
// equality columns between vectorized and row
// executions.
args.colIdxsToCheckForEquality = make([]int, nOrderingCols)
for i := range args.colIdxsToCheckForEquality {
args.colIdxsToCheckForEquality[i] = int(lOrderingCols[i].ColIdx)
}
}
if err := verifyColOperator(args); err != nil {
fmt.Printf("--- join type = %s onExpr = %q filter = %q seed = %d run = %d ---\n",
testSpec.joinType.String(), onExpr.Expr, filter.Expr, seed, run)
fmt.Printf("--- left ordering = %v right ordering = %v ---\n", lOrderingCols, rOrderingCols)
prettyPrintTypes(lInputTypes, "left_table" /* tableName */)
prettyPrintTypes(rInputTypes, "right_table" /* tableName */)
prettyPrintInput(lRows, lInputTypes, "left_table" /* tableName */)
prettyPrintInput(rRows, rInputTypes, "right_table" /* tableName */)
t.Fatal(err)
}
if onExpr.Expr == "" {
triedWithoutOnExpr = true
} else {
triedWithOnExpr = true
}
}
}
}
}
}
}
}
// generateColumnOrdering produces a random ordering of nOrderingCols columns
// on a table with nCols columns, so nOrderingCols must be not greater than
// nCols.
func generateColumnOrdering(
rng *rand.Rand, nCols int, nOrderingCols int,
) []execinfrapb.Ordering_Column {
if nOrderingCols > nCols {
panic("nOrderingCols > nCols in generateColumnOrdering")
}
orderingCols := make([]execinfrapb.Ordering_Column, nOrderingCols)
for i, col := range rng.Perm(nCols)[:nOrderingCols] {
orderingCols[i] = execinfrapb.Ordering_Column{
ColIdx: uint32(col),
Direction: execinfrapb.Ordering_Column_Direction(rng.Intn(2)),
}
}
return orderingCols
}
func getAddFilterOptions(joinType descpb.JoinType, nonEqualityColsPresent bool) []bool {
if joinType.IsSetOpJoin() && nonEqualityColsPresent {
// Output of set operation join when rows have non equality columns is
// not deterministic, so applying a filter on top of it can produce
// arbitrary results, and we skip such configuration.
return []bool{false}
}
return []bool{false, true}
}
// generateFilterExpr populates an execinfrapb.Expression that contains a
// single comparison which can be either comparing a column from the left
// against a column from the right or comparing a column from either side
// against a constant.
// If forceConstComparison is true, then the comparison against the constant
// will be used.
// If forceLeftSide is true, then the comparison of a column from the left
// against a constant will be used.
func generateFilterExpr(
rng *rand.Rand,
nCols int,
nEqCols int,
colTypes []*types.T,
forceConstComparison bool,
forceLeftSide bool,
) execinfrapb.Expression {
var comparison string
r := rng.Float64()
if r < 0.25 {
comparison = "<"
} else if r < 0.5 {
comparison = ">"
} else if r < 0.75 {
comparison = "="
} else {
comparison = "<>"
}
// When all columns are used in equality comparison between inputs, there is
// only one interesting case when a column from either side is compared
// against a constant. The second conditional is us choosing to compare
// against a constant.
if nCols == nEqCols || rng.Float64() < 0.33 || forceConstComparison || forceLeftSide {
colIdx := rng.Intn(nCols)
if !forceLeftSide && rng.Float64() >= 0.5 {
// Use right side.
colIdx += nCols
}
constDatum := sqlbase.RandDatum(rng, colTypes[colIdx], true /* nullOk */)
constDatumString := constDatum.String()
switch colTypes[colIdx].Family() {
case types.FloatFamily, types.DecimalFamily:
if strings.Contains(strings.ToLower(constDatumString), "nan") ||
strings.Contains(strings.ToLower(constDatumString), "inf") {
// We need to surround special numerical values with quotes.
constDatumString = fmt.Sprintf("'%s'", constDatumString)
}
}
return execinfrapb.Expression{Expr: fmt.Sprintf("@%d %s %s", colIdx+1, comparison, constDatumString)}
}
// We will compare a column from the left against a column from the right.
leftColIdx := rng.Intn(nCols) + 1
rightColIdx := rng.Intn(nCols) + nCols + 1
return execinfrapb.Expression{Expr: fmt.Sprintf("@%d %s @%d", leftColIdx, comparison, rightColIdx)}
}
func TestWindowFunctionsAgainstProcessor(t *testing.T) {
defer leaktest.AfterTest(t)()
rng, seed := randutil.NewPseudoRand()
nRows := 2 * coldata.BatchSize()
maxCols := 4
maxNum := 10
typs := make([]*types.T, maxCols)
for i := range typs {
// TODO(yuzefovich): randomize the types of the columns once we support
// window functions that take in arguments.
typs[i] = types.Int
}
for windowFn := range colbuilder.SupportedWindowFns {
for _, partitionBy := range [][]uint32{
{}, // No PARTITION BY clause.
{0}, // Partitioning on the first input column.
{0, 1}, // Partitioning on the first and second input columns.
} {
for _, nOrderingCols := range []int{
0, // No ORDER BY clause.
1, // ORDER BY on at most one column.
2, // ORDER BY on at most two columns.
} {
for nCols := 1; nCols <= maxCols; nCols++ {
if len(partitionBy) > nCols || nOrderingCols > nCols {
continue
}
inputTypes := typs[:nCols:nCols]
rows := sqlbase.MakeRandIntRowsInRange(rng, nRows, nCols, maxNum, nullProbability)
windowerSpec := &execinfrapb.WindowerSpec{
PartitionBy: partitionBy,
WindowFns: []execinfrapb.WindowerSpec_WindowFn{
{
Func: execinfrapb.WindowerSpec_Func{WindowFunc: &windowFn},
Ordering: generateOrderingGivenPartitionBy(rng, nCols, nOrderingCols, partitionBy),
OutputColIdx: uint32(nCols),
FilterColIdx: tree.NoColumnIdx,
},
},
}
if windowFn == execinfrapb.WindowerSpec_ROW_NUMBER &&
len(partitionBy)+len(windowerSpec.WindowFns[0].Ordering.Columns) < nCols {
// The output of row_number is not deterministic if there are
// columns that are not present in either PARTITION BY or ORDER BY
// clauses, so we skip such a configuration.
continue
}
pspec := &execinfrapb.ProcessorSpec{
Input: []execinfrapb.InputSyncSpec{{ColumnTypes: inputTypes}},
Core: execinfrapb.ProcessorCoreUnion{Windower: windowerSpec},
}
// Currently, we only support window functions that take no
// arguments, so we leave the second argument empty.
_, outputType, err := execinfrapb.GetWindowFunctionInfo(execinfrapb.WindowerSpec_Func{WindowFunc: &windowFn})
require.NoError(t, err)
args := verifyColOperatorArgs{