-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
logical_props_builder.go
1971 lines (1661 loc) · 57.5 KB
/
logical_props_builder.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 2018 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 memo
import (
"math"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/constraint"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
var fdAnnID = opt.NewTableAnnID()
// logicalPropsBuilder is a helper class that consolidates the code that derives
// a parent expression's logical properties from those of its children.
//
// buildProps is called by the memo group construction code in order to
// initialize the new group's logical properties.
// NOTE: When deriving properties from children, be sure to keep the child
// properties immutable by copying them if necessary.
// NOTE: The parent expression is passed as an expression for convenient access
// to children, but certain properties on it are not yet defined (like
// its logical properties!).
type logicalPropsBuilder struct {
evalCtx *tree.EvalContext
mem *Memo
sb statisticsBuilder
// When set to true, disableStats disables stat generation during
// logical prop building. Useful in checkExpr when we don't want
// to create stats for non-normalized expressions and potentially
// mutate opt_tester output compared to cases where checkExpr is
// not run.
disableStats bool
}
func (b *logicalPropsBuilder) init(evalCtx *tree.EvalContext, mem *Memo) {
b.evalCtx = evalCtx
b.mem = mem
b.sb.init(evalCtx, mem.Metadata())
}
func (b *logicalPropsBuilder) clear() {
b.evalCtx = nil
b.mem = nil
b.sb.clear()
}
func (b *logicalPropsBuilder) buildScanProps(scan *ScanExpr, rel *props.Relational) {
md := scan.Memo().Metadata()
hardLimit := scan.HardLimit.RowCount()
// Output Columns
// --------------
// Scan output columns are stored in the definition.
rel.OutputCols = scan.Cols
// Not Null Columns
// ----------------
// Initialize not-NULL columns from the table schema.
rel.NotNullCols = tableNotNullCols(md, scan.Table)
if scan.Constraint != nil {
rel.NotNullCols.UnionWith(scan.Constraint.ExtractNotNullCols(b.evalCtx))
}
rel.NotNullCols.IntersectionWith(rel.OutputCols)
// Outer Columns
// -------------
// Scan operator never has outer columns.
// Functional Dependencies
// -----------------------
// Check the hard limit to determine whether there is at most one row. Note
// that def.HardLimit = 0 indicates there is no known limit.
if hardLimit == 1 {
rel.FuncDeps.MakeMax1Row(rel.OutputCols)
} else {
// Initialize key FD's from the table schema, including constant columns from
// the constraint, minus any columns that are not projected by the Scan
// operator.
rel.FuncDeps.CopyFrom(MakeTableFuncDep(md, scan.Table))
if scan.Constraint != nil {
rel.FuncDeps.AddConstants(scan.Constraint.ExtractConstCols(b.evalCtx))
}
rel.FuncDeps.MakeNotNull(rel.NotNullCols)
rel.FuncDeps.ProjectCols(rel.OutputCols)
}
// Cardinality
// -----------
// Restrict cardinality based on constraint, FDs, and hard limit.
rel.Cardinality = props.AnyCardinality
if scan.Constraint != nil && scan.Constraint.IsContradiction() {
rel.Cardinality = props.ZeroCardinality
} else if rel.FuncDeps.HasMax1Row() {
rel.Cardinality = rel.Cardinality.Limit(1)
} else {
if hardLimit > 0 && hardLimit < math.MaxUint32 {
rel.Cardinality = rel.Cardinality.Limit(uint32(hardLimit))
}
if scan.Constraint != nil {
b.updateCardinalityFromConstraint(scan.Constraint, rel)
}
}
// Statistics
// ----------
if !b.disableStats {
b.sb.buildScan(scan, rel)
}
}
func (b *logicalPropsBuilder) buildVirtualScanProps(scan *VirtualScanExpr, rel *props.Relational) {
// Output Columns
// --------------
// VirtualScan output columns are stored in the definition.
rel.OutputCols = scan.Cols
// Not Null Columns
// ----------------
// All columns are assumed to be nullable.
// Outer Columns
// -------------
// VirtualScan operator never has outer columns.
// Functional Dependencies
// -----------------------
// VirtualScan operator has an empty FD set.
// Cardinality
// -----------
// Don't make any assumptions about cardinality of output.
rel.Cardinality = props.AnyCardinality
// Statistics
// ----------
b.sb.buildVirtualScan(scan, rel)
}
func (b *logicalPropsBuilder) buildSequenceSelectProps(
seq *SequenceSelectExpr, rel *props.Relational,
) {
// Output Columns
// --------------
// Output columns are stored in the definition.
rel.OutputCols = seq.Cols.ToSet()
// Not Null Columns
// ----------------
// Every column is not null.
rel.NotNullCols = rel.OutputCols
// Outer Columns
// -------------
// The operator never has outer columns.
// Functional Dependencies
// -----------------------
rel.FuncDeps.MakeMax1Row(rel.OutputCols)
// Cardinality
// -----------
rel.Cardinality = props.OneCardinality
// Statistics
// ----------
if !b.disableStats {
b.sb.buildSequenceSelect(rel)
}
}
func (b *logicalPropsBuilder) buildSelectProps(sel *SelectExpr, rel *props.Relational) {
BuildSharedProps(b.mem, sel, &rel.Shared)
inputProps := sel.Input.Relational()
// Output Columns
// --------------
// Inherit output columns from input.
rel.OutputCols = inputProps.OutputCols
// Not Null Columns
// ----------------
// A column can become not null due to a null rejecting filter expression:
//
// SELECT y FROM xy WHERE y=5
//
// "y" cannot be null because the SQL equality operator rejects nulls.
rel.NotNullCols = b.rejectNullCols(sel.Filters)
rel.NotNullCols.UnionWith(inputProps.NotNullCols)
rel.NotNullCols.IntersectionWith(rel.OutputCols)
// Outer Columns
// -------------
// Outer columns were derived by buildSharedProps; remove any that are bound
// by input columns.
rel.OuterCols.DifferenceWith(inputProps.OutputCols)
// Functional Dependencies
// -----------------------
// Start with copy of FuncDepSet from input, add FDs from the WHERE clause
// and outer columns, modify with any additional not-null columns, then
// possibly simplify by calling ProjectCols.
rel.FuncDeps.CopyFrom(&inputProps.FuncDeps)
b.addFiltersToFuncDep(sel.Filters, &rel.FuncDeps)
addOuterColsToFuncDep(rel.OuterCols, &rel.FuncDeps)
rel.FuncDeps.MakeNotNull(rel.NotNullCols)
rel.FuncDeps.ProjectCols(rel.OutputCols)
// Cardinality
// -----------
// Select filter can filter any or all rows.
rel.Cardinality = inputProps.Cardinality.AsLowAs(0)
if sel.Filters.IsFalse() {
rel.Cardinality = props.ZeroCardinality
} else if rel.FuncDeps.HasMax1Row() {
rel.Cardinality = rel.Cardinality.Limit(1)
} else {
b.updateCardinalityFromFilters(sel.Filters, rel)
}
// Statistics
// ----------
if !b.disableStats {
b.sb.buildSelect(sel, rel)
}
}
func (b *logicalPropsBuilder) buildProjectProps(prj *ProjectExpr, rel *props.Relational) {
BuildSharedProps(b.mem, prj, &rel.Shared)
inputProps := prj.Input.Relational()
// Output Columns
// --------------
// Output columns are the union of synthesized columns and passthrough columns.
for i := range prj.Projections {
rel.OutputCols.Add(prj.Projections[i].Col)
}
rel.OutputCols.UnionWith(prj.Passthrough)
// Not Null Columns
// ----------------
// Inherit not null columns from input, but only use those that are also
// output columns.
rel.NotNullCols = inputProps.NotNullCols.Copy()
rel.NotNullCols.IntersectionWith(rel.OutputCols)
// Also add any column that projects a constant value, since the optimizer
// sometimes constructs these in order to guarantee a not-null column.
for i := range prj.Projections {
item := &prj.Projections[i]
if opt.IsConstValueOp(item.Element) {
if ExtractConstDatum(item.Element) != tree.DNull {
rel.NotNullCols.Add(item.Col)
}
}
}
// Outer Columns
// -------------
// Outer columns were derived by buildSharedProps; remove any that are bound
// by input columns.
rel.OuterCols.DifferenceWith(inputProps.OutputCols)
// Functional Dependencies
// -----------------------
// Start with copy of FuncDepSet, add synthesized column dependencies, and then
// remove columns that are not projected.
rel.FuncDeps.CopyFrom(&inputProps.FuncDeps)
for i := range prj.Projections {
item := &prj.Projections[i]
if !item.scalar.CanHaveSideEffects {
from := item.scalar.OuterCols.Intersection(inputProps.OutputCols)
// We want to set up the FD: from --> colID.
// This does not necessarily hold for "composite" types like decimals or
// collated strings. For example if d is a decimal, d::TEXT can have
// different values for equal values of d, like 1 and 1.0.
//
// We only add the FD if composite types are not involved.
//
// TODO(radu): add a whitelist of expressions/operators that are ok, like
// arithmetic.
composite := false
for i, ok := from.Next(0); ok; i, ok = from.Next(i + 1) {
typ := b.mem.Metadata().ColumnMeta(i).Type
if sqlbase.DatumTypeHasCompositeKeyEncoding(typ) {
composite = true
break
}
}
if !composite {
rel.FuncDeps.AddSynthesizedCol(from, item.Col)
}
}
}
rel.FuncDeps.MakeNotNull(rel.NotNullCols)
rel.FuncDeps.ProjectCols(rel.OutputCols)
// Cardinality
// -----------
// Inherit cardinality from input.
rel.Cardinality = inputProps.Cardinality
// Statistics
// ----------
if !b.disableStats {
b.sb.buildProject(prj, rel)
}
}
func (b *logicalPropsBuilder) buildInnerJoinProps(join *InnerJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildLeftJoinProps(join *LeftJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildRightJoinProps(join *RightJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildFullJoinProps(join *FullJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildSemiJoinProps(join *SemiJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildAntiJoinProps(join *AntiJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildInnerJoinApplyProps(
join *InnerJoinApplyExpr, rel *props.Relational,
) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildLeftJoinApplyProps(
join *LeftJoinApplyExpr, rel *props.Relational,
) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildSemiJoinApplyProps(
join *SemiJoinApplyExpr, rel *props.Relational,
) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildAntiJoinApplyProps(
join *AntiJoinApplyExpr, rel *props.Relational,
) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildJoinProps(join RelExpr, rel *props.Relational) {
BuildSharedProps(b.mem, join, &rel.Shared)
var h joinPropsHelper
h.init(b, join)
// Output Columns
// --------------
rel.OutputCols = h.outputCols()
// Not Null Columns
// ----------------
rel.NotNullCols = h.notNullCols()
rel.NotNullCols.IntersectionWith(rel.OutputCols)
// Outer Columns
// -------------
// Outer columns were initially set by buildSharedProps. Remove any that are
// bound by the input columns.
inputCols := h.leftProps.OutputCols.Union(h.rightProps.OutputCols)
rel.OuterCols.DifferenceWith(inputCols)
// Functional Dependencies
// -----------------------
h.setFuncDeps(rel)
// Cardinality
// -----------
// Calculate cardinality, depending on join type.
rel.Cardinality = h.cardinality()
if rel.FuncDeps.HasMax1Row() {
rel.Cardinality = rel.Cardinality.Limit(1)
}
// Statistics
// ----------
if !b.disableStats {
b.sb.buildJoin(join, rel, &h)
}
}
func (b *logicalPropsBuilder) buildIndexJoinProps(indexJoin *IndexJoinExpr, rel *props.Relational) {
BuildSharedProps(b.mem, indexJoin, &rel.Shared)
inputProps := indexJoin.Input.Relational()
md := b.mem.Metadata()
// Output Columns
// --------------
rel.OutputCols = indexJoin.Cols
// Not Null Columns
// ----------------
// Add not-NULL columns from the table schema, and filter out any not-NULL
// columns from the input that are not projected by the index join.
rel.NotNullCols = tableNotNullCols(md, indexJoin.Table)
rel.NotNullCols.IntersectionWith(rel.OutputCols)
// Outer Columns
// -------------
// Outer columns were already derived by buildSharedProps.
// Functional Dependencies
// -----------------------
// Start with the input FD set, and join that with the table's FD.
rel.FuncDeps.CopyFrom(&inputProps.FuncDeps)
rel.FuncDeps.AddFrom(MakeTableFuncDep(md, indexJoin.Table))
rel.FuncDeps.MakeNotNull(rel.NotNullCols)
rel.FuncDeps.ProjectCols(rel.OutputCols)
// Cardinality
// -----------
// Inherit cardinality from input.
rel.Cardinality = inputProps.Cardinality
// Statistics
// ----------
if !b.disableStats {
b.sb.buildIndexJoin(indexJoin, rel)
}
}
func (b *logicalPropsBuilder) buildLookupJoinProps(join *LookupJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildZigzagJoinProps(join *ZigzagJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildMergeJoinProps(join *MergeJoinExpr, rel *props.Relational) {
b.buildJoinProps(join, rel)
}
func (b *logicalPropsBuilder) buildGroupByProps(groupBy *GroupByExpr, rel *props.Relational) {
b.buildGroupingExprProps(groupBy, rel)
}
func (b *logicalPropsBuilder) buildScalarGroupByProps(
scalarGroupBy *ScalarGroupByExpr, rel *props.Relational,
) {
b.buildGroupingExprProps(scalarGroupBy, rel)
}
func (b *logicalPropsBuilder) buildDistinctOnProps(
distinctOn *DistinctOnExpr, rel *props.Relational,
) {
b.buildGroupingExprProps(distinctOn, rel)
}
func (b *logicalPropsBuilder) buildGroupingExprProps(groupExpr RelExpr, rel *props.Relational) {
BuildSharedProps(b.mem, groupExpr, &rel.Shared)
inputProps := groupExpr.Child(0).(RelExpr).Relational()
aggs := *groupExpr.Child(1).(*AggregationsExpr)
groupPrivate := groupExpr.Private().(*GroupingPrivate)
// Output Columns
// --------------
// Output columns are the union of grouping columns with columns from the
// aggregate projection list.
rel.OutputCols = groupPrivate.GroupingCols.Copy()
for i := range aggs {
rel.OutputCols.Add(aggs[i].Col)
}
// Not Null Columns
// ----------------
// Propagate not null setting from input columns that are being grouped.
rel.NotNullCols = inputProps.NotNullCols.Intersection(groupPrivate.GroupingCols)
// Outer Columns
// -------------
// Outer columns were derived by buildSharedProps; remove any that are bound
// by input columns.
rel.OuterCols.DifferenceWith(inputProps.OutputCols)
// Functional Dependencies
// -----------------------
rel.FuncDeps.CopyFrom(&inputProps.FuncDeps)
if groupPrivate.GroupingCols.Empty() {
// Scalar group by has no grouping columns and always a single row.
rel.FuncDeps.MakeMax1Row(rel.OutputCols)
} else {
// The grouping columns always form a strict key because the GroupBy
// operation eliminates all duplicates in those columns.
rel.FuncDeps.ProjectCols(rel.OutputCols)
rel.FuncDeps.AddStrictKey(groupPrivate.GroupingCols, rel.OutputCols)
}
// Cardinality
// -----------
if groupExpr.Op() == opt.ScalarGroupByOp {
// Scalar GroupBy returns exactly one row.
rel.Cardinality = props.OneCardinality
} else {
// GroupBy and DistinctOn act like a filter, never returning more rows than the input
// has. However, if the input has at least one row, then at least one row
// will also be returned by GroupBy and DistinctOn.
rel.Cardinality = inputProps.Cardinality.AsLowAs(1)
if rel.FuncDeps.HasMax1Row() {
rel.Cardinality = rel.Cardinality.Limit(1)
}
}
// Statistics
// ----------
if !b.disableStats {
b.sb.buildGroupBy(groupExpr, rel)
}
}
func (b *logicalPropsBuilder) buildUnionProps(union *UnionExpr, rel *props.Relational) {
b.buildSetProps(union, rel)
}
func (b *logicalPropsBuilder) buildIntersectProps(isect *IntersectExpr, rel *props.Relational) {
b.buildSetProps(isect, rel)
}
func (b *logicalPropsBuilder) buildExceptProps(except *ExceptExpr, rel *props.Relational) {
b.buildSetProps(except, rel)
}
func (b *logicalPropsBuilder) buildUnionAllProps(union *UnionAllExpr, rel *props.Relational) {
b.buildSetProps(union, rel)
}
func (b *logicalPropsBuilder) buildIntersectAllProps(
isect *IntersectAllExpr, rel *props.Relational,
) {
b.buildSetProps(isect, rel)
}
func (b *logicalPropsBuilder) buildExceptAllProps(except *ExceptAllExpr, rel *props.Relational) {
b.buildSetProps(except, rel)
}
func (b *logicalPropsBuilder) buildSetProps(setNode RelExpr, rel *props.Relational) {
BuildSharedProps(b.mem, setNode, &rel.Shared)
leftProps := setNode.Child(0).(RelExpr).Relational()
rightProps := setNode.Child(1).(RelExpr).Relational()
setPrivate := setNode.Private().(*SetPrivate)
if len(setPrivate.OutCols) != len(setPrivate.LeftCols) ||
len(setPrivate.OutCols) != len(setPrivate.RightCols) {
panic(errors.AssertionFailedf(
"lists in SetPrivate are not all the same length. new:%d, left:%d, right:%d",
log.Safe(len(setPrivate.OutCols)), log.Safe(len(setPrivate.LeftCols)), log.Safe(len(setPrivate.RightCols)),
))
}
// Output Columns
// --------------
// Output columns are stored in the definition.
rel.OutputCols = setPrivate.OutCols.ToSet()
// Not Null Columns
// ----------------
// Columns have to be not-null on both sides to be not-null in result.
// setPrivate matches columns on the left and right sides of the operator
// with the output columns, since OutputCols are not ordered and may
// not correspond to each other.
for i := range setPrivate.OutCols {
if leftProps.NotNullCols.Contains((setPrivate.LeftCols)[i]) &&
rightProps.NotNullCols.Contains((setPrivate.RightCols)[i]) {
rel.NotNullCols.Add((setPrivate.OutCols)[i])
}
}
// Outer Columns
// -------------
// Outer columns were already derived by buildSharedProps.
// Functional Dependencies
// -----------------------
switch setNode.Op() {
case opt.UnionOp, opt.IntersectOp, opt.ExceptOp:
// These operators eliminate duplicates, so a strict key exists.
rel.FuncDeps.AddStrictKey(rel.OutputCols, rel.OutputCols)
}
// Cardinality
// -----------
// Calculate cardinality of the set operator.
rel.Cardinality = b.makeSetCardinality(
setNode.Op(), leftProps.Cardinality, rightProps.Cardinality)
// Statistics
// ----------
if !b.disableStats {
b.sb.buildSetNode(setNode, rel)
}
}
func (b *logicalPropsBuilder) buildValuesProps(values *ValuesExpr, rel *props.Relational) {
BuildSharedProps(b.mem, values, &rel.Shared)
card := uint32(len(values.Rows))
// Output Columns
// --------------
// Use output columns that are attached to the values op.
rel.OutputCols = values.Cols.ToSet()
// Not Null Columns
// ----------------
// All columns are assumed to be nullable, unless they contain only constant
// non-null values.
for colIdx, col := range values.Cols {
notNull := true
for rowIdx := range values.Rows {
val := values.Rows[rowIdx].(*TupleExpr).Elems[colIdx]
if !opt.IsConstValueOp(val) || val.Op() == opt.NullOp {
// Null or not a constant.
notNull = false
break
}
}
if notNull {
rel.NotNullCols.Add(col)
}
}
// Outer Columns
// -------------
// Outer columns were already derived by buildSharedProps.
// Functional Dependencies
// -----------------------
if card <= 1 {
rel.FuncDeps.MakeMax1Row(rel.OutputCols)
}
// Cardinality
// -----------
// Cardinality is number of tuples in the Values operator.
rel.Cardinality = props.Cardinality{Min: card, Max: card}
// Statistics
// ----------
if !b.disableStats {
b.sb.buildValues(values, rel)
}
}
func (b *logicalPropsBuilder) buildBasicProps(e opt.Expr, cols opt.ColList, rel *props.Relational) {
BuildSharedProps(b.mem, e, &rel.Shared)
// Output Columns
// --------------
rel.OutputCols = cols.ToSet()
// Not Null Columns
// ----------------
// All columns are assumed to be nullable.
// Outer Columns
// -------------
// No outer columns.
// Functional Dependencies
// -----------------------
// Empty FD set.
// Cardinality
// -----------
// Don't make any assumptions about cardinality of output.
rel.Cardinality = props.AnyCardinality
// Statistics
// ----------
if !b.disableStats {
b.sb.buildUnknown(rel)
}
}
func (b *logicalPropsBuilder) buildWithProps(with *WithExpr, rel *props.Relational) {
// Copy over the props from the input.
*rel = *with.Input.Relational()
BuildSharedProps(b.mem, with, &rel.Shared)
// Side Effects
// ------------
// This expression has side effects if either Binding or Input has side
// effects, which is what is computed by the call to BuildSharedProps.
// Output Columns
// --------------
// Passed through from the call above to b.buildProps.
// Not Null Columns
// ----------------
// Passed through from the call above to b.buildProps.
// Outer Columns
// -------------
// Passed through from the call above to b.buildProps.
// Functional Dependencies
// -----------------------
// Passed through from the call above to b.buildProps.
// Cardinality
// -----------
// Passed through from the call above to b.buildProps.
// Statistics
// ----------
// Passed through from the call above to b.buildProps.
}
func (b *logicalPropsBuilder) buildWithScanProps(ref *WithScanExpr, rel *props.Relational) {
// WithScan inherits most of the logical properties of the expression it
// references.
*rel = *ref.BindingProps
// Things like PruneCols are not valid here.
// TODO(justin): we should re-implement the relevant ones for WithScan.
rel.Rule = props.Relational{}.Rule
// Has Placeholder
// ---------------
// Overwrite this from the copied props.
rel.HasPlaceholder = false
// Side Effects
// ------------
// Overwrite this from the copied props.
rel.CanHaveSideEffects = false
// Output Columns
// --------------
rel.OutputCols = ref.OutCols.ToSet()
// Not Null Columns
// ----------------
rel.NotNullCols = translateColSet(rel.NotNullCols, ref.InCols, ref.OutCols)
// Outer Columns
// -------------
rel.OuterCols = opt.ColSet{}
// Functional Dependencies
// -----------------------
rel.FuncDeps = props.FuncDepSet{}
rel.FuncDeps.CopyFrom(&ref.BindingProps.FuncDeps)
for i := range ref.InCols {
rel.FuncDeps.AddEquivalency(ref.InCols[i], ref.OutCols[i])
}
rel.FuncDeps.ProjectCols(ref.OutCols.ToSet())
// Cardinality
// -----------
// Copied from the referenced expression.
// Statistics
// ----------
rel.Stats = props.Statistics{}
if !b.disableStats {
b.sb.buildWithScan(ref, rel)
}
}
func (b *logicalPropsBuilder) buildExplainProps(explain *ExplainExpr, rel *props.Relational) {
b.buildBasicProps(explain, explain.ColList, rel)
}
func (b *logicalPropsBuilder) buildShowTraceForSessionProps(
showTrace *ShowTraceForSessionExpr, rel *props.Relational,
) {
b.buildBasicProps(showTrace, showTrace.ColList, rel)
}
func (b *logicalPropsBuilder) buildOpaqueRelProps(op *OpaqueRelExpr, rel *props.Relational) {
b.buildBasicProps(op, op.Columns, rel)
}
func (b *logicalPropsBuilder) buildOpaqueMutationProps(
op *OpaqueMutationExpr, rel *props.Relational,
) {
b.buildBasicProps(op, op.Columns, rel)
}
func (b *logicalPropsBuilder) buildOpaqueDDLProps(op *OpaqueDDLExpr, rel *props.Relational) {
b.buildBasicProps(op, op.Columns, rel)
}
func (b *logicalPropsBuilder) buildAlterTableSplitProps(
split *AlterTableSplitExpr, rel *props.Relational,
) {
b.buildBasicProps(split, split.Columns, rel)
}
func (b *logicalPropsBuilder) buildAlterTableUnsplitProps(
unsplit *AlterTableUnsplitExpr, rel *props.Relational,
) {
b.buildBasicProps(unsplit, unsplit.Columns, rel)
}
func (b *logicalPropsBuilder) buildAlterTableUnsplitAllProps(
unsplitAll *AlterTableUnsplitAllExpr, rel *props.Relational,
) {
b.buildBasicProps(unsplitAll, unsplitAll.Columns, rel)
}
func (b *logicalPropsBuilder) buildAlterTableRelocateProps(
relocate *AlterTableRelocateExpr, rel *props.Relational,
) {
b.buildBasicProps(relocate, relocate.Columns, rel)
}
func (b *logicalPropsBuilder) buildControlJobsProps(ctl *ControlJobsExpr, rel *props.Relational) {
b.buildBasicProps(ctl, opt.ColList{}, rel)
}
func (b *logicalPropsBuilder) buildCancelQueriesProps(
cancel *CancelQueriesExpr, rel *props.Relational,
) {
b.buildBasicProps(cancel, opt.ColList{}, rel)
}
func (b *logicalPropsBuilder) buildCancelSessionsProps(
cancel *CancelSessionsExpr, rel *props.Relational,
) {
b.buildBasicProps(cancel, opt.ColList{}, rel)
}
func (b *logicalPropsBuilder) buildExportProps(export *ExportExpr, rel *props.Relational) {
b.buildBasicProps(export, export.Columns, rel)
}
func (b *logicalPropsBuilder) buildLimitProps(limit *LimitExpr, rel *props.Relational) {
BuildSharedProps(b.mem, limit, &rel.Shared)
inputProps := limit.Input.Relational()
haveConstLimit := false
constLimit := int64(math.MaxUint32)
if cnst, ok := limit.Limit.(*ConstExpr); ok {
haveConstLimit = true
constLimit = int64(*cnst.Value.(*tree.DInt))
}
// Side Effects
// ------------
// Negative limits can trigger a runtime error.
if constLimit < 0 || !haveConstLimit {
rel.CanHaveSideEffects = true
}
// Output Columns
// --------------
// Output columns are inherited from input.
rel.OutputCols = inputProps.OutputCols
// Not Null Columns
// ----------------
// Not null columns are inherited from input.
rel.NotNullCols = inputProps.NotNullCols
// Outer Columns
// -------------
// Outer columns were already derived by buildSharedProps.
// Functional Dependencies
// -----------------------
// Inherit functional dependencies from input if limit is > 1, else just use
// single row dependencies.
if constLimit > 1 {
rel.FuncDeps.CopyFrom(&inputProps.FuncDeps)
} else {
rel.FuncDeps.MakeMax1Row(rel.OutputCols)
}
// Cardinality
// -----------
// Limit puts a cap on the number of rows returned by input.
rel.Cardinality = inputProps.Cardinality
if constLimit <= 0 {
rel.Cardinality = props.ZeroCardinality
} else if constLimit < math.MaxUint32 {
rel.Cardinality = rel.Cardinality.Limit(uint32(constLimit))
}
// Statistics
// ----------
if !b.disableStats {
b.sb.buildLimit(limit, rel)
}
}
func (b *logicalPropsBuilder) buildOffsetProps(offset *OffsetExpr, rel *props.Relational) {
BuildSharedProps(b.mem, offset, &rel.Shared)
inputProps := offset.Input.Relational()
// Output Columns
// --------------
// Output columns are inherited from input.
rel.OutputCols = inputProps.OutputCols
// Not Null Columns
// ----------------
// Not null columns are inherited from input.
rel.NotNullCols = inputProps.NotNullCols
// Outer Columns
// -------------
// Outer columns were already derived by buildSharedProps.
// Functional Dependencies
// -----------------------
// Inherit functional dependencies from input.
rel.FuncDeps.CopyFrom(&inputProps.FuncDeps)
// Cardinality
// -----------
// Offset decreases the number of rows that are passed through from input.
rel.Cardinality = inputProps.Cardinality
if cnst, ok := offset.Offset.(*ConstExpr); ok {
constOffset := int64(*cnst.Value.(*tree.DInt))
if constOffset > 0 {
if constOffset > math.MaxUint32 {
constOffset = math.MaxUint32
}
rel.Cardinality = inputProps.Cardinality.Skip(uint32(constOffset))
}
}
// Statistics
// ----------
if !b.disableStats {
b.sb.buildOffset(offset, rel)
}
}
func (b *logicalPropsBuilder) buildMax1RowProps(max1Row *Max1RowExpr, rel *props.Relational) {
BuildSharedProps(b.mem, max1Row, &rel.Shared)
inputProps := max1Row.Input.Relational()
// Output Columns
// --------------
// Output columns are inherited from input.
rel.OutputCols = inputProps.OutputCols
// Not Null Columns
// ----------------
// Not null columns are inherited from input.
rel.NotNullCols = inputProps.NotNullCols
// Outer Columns
// -------------
// Outer columns were already derived by buildSharedProps.
// Functional Dependencies
// -----------------------
// Max1Row always returns zero or one rows.
rel.FuncDeps.MakeMax1Row(rel.OutputCols)
// Cardinality
// -----------
// Max1Row ensures that zero or one row is returned from input.