-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathcustom_funcs.go
1375 lines (1220 loc) · 46.2 KB
/
custom_funcs.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.
//
// 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package norm
import (
"fmt"
"math"
"reflect"
"sort"
"github.com/cockroachdb/cockroach/pkg/sql/opt/constraint"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/sql/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sem/types"
"github.com/cockroachdb/cockroach/pkg/util/json"
)
// CustomFuncs contains all the custom match and replace functions used by
// the normalization rules. These are also imported and used by the explorer.
type CustomFuncs struct {
f *Factory
mem *memo.Memo
}
// Init initializes a new CustomFuncs with the given factory.
func (c *CustomFuncs) Init(f *Factory) {
c.f = f
c.mem = f.Memo()
}
// Succeeded returns true if a result expression is not nil.
func (c *CustomFuncs) Succeeded(result opt.Expr) bool {
return result != nil
}
// ----------------------------------------------------------------------
//
// ScalarList functions
// General custom match and replace functions used to test and construct
// scalar lists.
//
// ----------------------------------------------------------------------
// NeedSortedUniqueList returns true if the given list is composed entirely of
// constant values that are either not in sorted order or have duplicates. If
// true, then ConstructSortedUniqueList needs to be called on the list to
// normalize it.
func (c *CustomFuncs) NeedSortedUniqueList(list memo.ScalarListExpr) bool {
if len(list) <= 1 {
return false
}
ls := listSorter{cf: c, list: list}
for i, item := range list {
if !opt.IsConstValueOp(item) {
return false
}
if i != 0 && !ls.less(i-1, i) {
return true
}
}
return false
}
// ConstructSortedUniqueList sorts the given list and removes duplicates, and
// returns the resulting list. See the comment for listSorter.compare for
// comparison rule details.
func (c *CustomFuncs) ConstructSortedUniqueList(
list memo.ScalarListExpr,
) (memo.ScalarListExpr, types.T) {
// Make a copy of the list, since it needs to stay immutable.
newList := make(memo.ScalarListExpr, len(list))
copy(newList, list)
ls := listSorter{cf: c, list: newList}
// Sort the list.
sort.Slice(ls.list, ls.less)
// Remove duplicates from the list.
n := 0
for i := range newList {
if i == 0 || ls.compare(i-1, i) < 0 {
newList[n] = newList[i]
n++
}
}
newList = newList[:n]
// Construct the type of the tuple.
typ := types.TTuple{Types: make([]types.T, n)}
for i := range newList {
typ.Types[i] = newList[i].DataType()
}
return newList, typ
}
// ----------------------------------------------------------------------
//
// Typing functions
// General custom match and replace functions used to test and construct
// expression data types.
//
// ----------------------------------------------------------------------
// HasColType returns true if the given scalar expression has a static type
// that's equivalent to the requested coltype.
func (c *CustomFuncs) HasColType(scalar opt.ScalarExpr, dstTyp coltypes.T) bool {
srcTyp, _ := coltypes.DatumTypeToColumnType(scalar.DataType())
if reflect.TypeOf(srcTyp) != reflect.TypeOf(dstTyp) {
return false
}
return coltypes.ColTypeAsString(srcTyp) == coltypes.ColTypeAsString(dstTyp)
}
// IsString returns true if the given scalar expression is of type String.
func (c *CustomFuncs) IsString(scalar opt.ScalarExpr) bool {
return scalar.DataType() == types.String
}
// ColTypeToDatumType maps the given column type to a datum type.
func (c *CustomFuncs) ColTypeToDatumType(colTyp coltypes.T) types.T {
return coltypes.CastTargetToDatumType(colTyp)
}
// BoolType returns the boolean SQL type.
func (c *CustomFuncs) BoolType() types.T {
return types.Bool
}
// AnyType returns the wildcard Any type.
func (c *CustomFuncs) AnyType() types.T {
return types.Any
}
// CanConstructBinary returns true if (op left right) has a valid binary op
// overload and is therefore legal to construct. For example, while
// (Minus <date> <int>) is valid, (Minus <int> <date>) is not.
func (c *CustomFuncs) CanConstructBinary(op opt.Operator, left, right opt.ScalarExpr) bool {
return memo.BinaryOverloadExists(op, left.DataType(), right.DataType())
}
// ----------------------------------------------------------------------
//
// Property functions
// General custom match and replace functions used to test expression
// logical properties.
//
// ----------------------------------------------------------------------
// OutputCols returns the set of columns returned by the input expression.
func (c *CustomFuncs) OutputCols(input memo.RelExpr) opt.ColSet {
return input.Relational().OutputCols
}
// OutputCols2 returns the union of columns returned by the left and right
// expressions.
func (c *CustomFuncs) OutputCols2(left, right memo.RelExpr) opt.ColSet {
return left.Relational().OutputCols.Union(right.Relational().OutputCols)
}
// CandidateKey returns the candidate key columns from the given input
// expression. If there is no candidate key, CandidateKey returns ok=false.
func (c *CustomFuncs) CandidateKey(input memo.RelExpr) (key opt.ColSet, ok bool) {
return input.Relational().FuncDeps.StrictKey()
}
// IsColNotNull returns true if the given input column is never null.
func (c *CustomFuncs) IsColNotNull(col opt.ColumnID, input memo.RelExpr) bool {
return input.Relational().NotNullCols.Contains(int(col))
}
// IsColNotNull2 returns true if the given column is part of the left or right
// expressions' set of not-null columns.
func (c *CustomFuncs) IsColNotNull2(col opt.ColumnID, left, right memo.RelExpr) bool {
return left.Relational().NotNullCols.Contains(int(col)) ||
right.Relational().NotNullCols.Contains(int(col))
}
// OuterCols returns the set of outer columns associated with the given
// expression, whether it be a relational or scalar operator.
func (c *CustomFuncs) OuterCols(e opt.Expr) opt.ColSet {
return c.sharedProps(e).OuterCols
}
// HasOuterCols returns true if the input expression has at least one outer
// column, or in other words, a reference to a variable that is not bound within
// its own scope. For example:
//
// SELECT * FROM a WHERE EXISTS(SELECT * FROM b WHERE b.x = a.x)
//
// The a.x variable in the EXISTS subquery references a column outside the scope
// of the subquery. It is an "outer column" for the subquery (see the comment on
// RelationalProps.OuterCols for more details).
func (c *CustomFuncs) HasOuterCols(input opt.Expr) bool {
return !c.OuterCols(input).Empty()
}
// IsBoundBy returns true if all outer references in the source expression are
// bound by the given columns. For example:
//
// (InnerJoin
// (Scan a)
// (Scan b)
// [ ... $item:(FiltersItem (Eq (Variable a.x) (Const 1))) ... ]
// )
//
// The $item expression is fully bound by the output columns of the (Scan a)
// expression because all of its outer references are satisfied by the columns
// produced by the Scan.
func (c *CustomFuncs) IsBoundBy(src opt.Expr, cols opt.ColSet) bool {
return c.OuterCols(src).SubsetOf(cols)
}
// IsCorrelated returns true if any variable in the source expression references
// a column from the destination expression. For example:
// (InnerJoin
// (Scan a)
// (Scan b)
// [ ... (FiltersItem $item:(Eq (Variable a.x) (Const 1))) ... ]
// )
//
// The $item expression is correlated with the (Scan a) expression because it
// references one of its columns. But the $item expression is not correlated
// with the (Scan b) expression.
func (c *CustomFuncs) IsCorrelated(src, dst memo.RelExpr) bool {
return src.Relational().OuterCols.Intersects(dst.Relational().OutputCols)
}
// HasNoCols returns true if the input expression has zero output columns.
func (c *CustomFuncs) HasNoCols(input memo.RelExpr) bool {
return input.Relational().OutputCols.Empty()
}
// HasZeroRows returns true if the input expression never returns any rows.
func (c *CustomFuncs) HasZeroRows(input memo.RelExpr) bool {
return input.Relational().Cardinality.IsZero()
}
// HasOneRow returns true if the input expression always returns exactly one
// row.
func (c *CustomFuncs) HasOneRow(input memo.RelExpr) bool {
return input.Relational().Cardinality.IsOne()
}
// HasZeroOrOneRow returns true if the input expression returns at most one row.
func (c *CustomFuncs) HasZeroOrOneRow(input memo.RelExpr) bool {
return input.Relational().Cardinality.IsZeroOrOne()
}
// CanHaveZeroRows returns true if the input expression might return zero rows.
func (c *CustomFuncs) CanHaveZeroRows(input memo.RelExpr) bool {
return input.Relational().Cardinality.CanBeZero()
}
// ColsAreSubset returns true if the left columns are a subset of the right
// columns.
func (c *CustomFuncs) ColsAreSubset(left, right opt.ColSet) bool {
return left.SubsetOf(right)
}
// ColsAreEqual returns true if left and right contain the same set of columns.
func (c *CustomFuncs) ColsAreEqual(left, right opt.ColSet) bool {
return left.Equals(right)
}
// UnionCols returns the union of the left and right column sets.
func (c *CustomFuncs) UnionCols(left, right opt.ColSet) opt.ColSet {
return left.Union(right)
}
// UnionCols3 returns the union of the three column sets.
func (c *CustomFuncs) UnionCols3(cols1, cols2, cols3 opt.ColSet) opt.ColSet {
cols := cols1.Union(cols2)
cols.UnionWith(cols3)
return cols
}
// UnionCols4 returns the union of the four column sets.
func (c *CustomFuncs) UnionCols4(cols1, cols2, cols3, cols4 opt.ColSet) opt.ColSet {
cols := cols1.Union(cols2)
cols.UnionWith(cols3)
cols.UnionWith(cols4)
return cols
}
// DifferenceCols returns the difference of the left and right column sets.
func (c *CustomFuncs) DifferenceCols(left, right opt.ColSet) opt.ColSet {
return left.Difference(right)
}
// sharedProps returns the shared logical properties for the given expression.
// Only relational expressions and certain scalar list items (e.g. FiltersItem,
// ProjectionsItem, AggregationsItem) have shared properties.
func (c *CustomFuncs) sharedProps(e opt.Expr) *props.Shared {
switch t := e.(type) {
case memo.RelExpr:
return &t.Relational().Shared
case memo.ScalarPropsExpr:
return &t.ScalarProps(c.mem).Shared
}
panic(fmt.Sprintf("no logical properties available for node: %s", e))
}
// ----------------------------------------------------------------------
//
// Ordering functions
// General custom match and replace functions related to orderings.
//
// ----------------------------------------------------------------------
// HasColsInOrdering returns true if all columns that appear in an ordering are
// output columns of the input expression.
func (c *CustomFuncs) HasColsInOrdering(input memo.RelExpr, ordering physical.OrderingChoice) bool {
return ordering.CanProjectCols(input.Relational().OutputCols)
}
// OrderingCols returns all non-optional columns that are part of the given
// OrderingChoice.
func (c *CustomFuncs) OrderingCols(ordering physical.OrderingChoice) opt.ColSet {
return ordering.ColSet()
}
// PruneOrdering removes any columns referenced by an OrderingChoice that are
// not part of the needed column set. Should only be called if HasColsInOrdering
// is true.
func (c *CustomFuncs) PruneOrdering(
ordering physical.OrderingChoice, needed opt.ColSet,
) physical.OrderingChoice {
if ordering.SubsetOfCols(needed) {
return ordering
}
ordCopy := ordering.Copy()
ordCopy.ProjectCols(needed)
return ordCopy
}
// -----------------------------------------------------------------------
//
// Filter functions
// General custom match and replace functions used to test and construct
// filters in Select and Join rules.
//
// -----------------------------------------------------------------------
// FilterOuterCols returns the union of all outer columns from the given filter
// conditions.
func (c *CustomFuncs) FilterOuterCols(filters memo.FiltersExpr) opt.ColSet {
var colSet opt.ColSet
for i := range filters {
colSet.UnionWith(filters[i].ScalarProps(c.mem).OuterCols)
}
return colSet
}
// FilterHasCorrelatedSubquery returns true if any of the filter conditions
// contain a correlated subquery.
func (c *CustomFuncs) FilterHasCorrelatedSubquery(filters memo.FiltersExpr) bool {
for i := range filters {
if filters[i].ScalarProps(c.mem).HasCorrelatedSubquery {
return true
}
}
return false
}
// IsFilterFalse returns true if the filters always evaluate to false. The only
// case that's checked is the fully normalized case, when the list contains a
// single False condition.
func (c *CustomFuncs) IsFilterFalse(filters memo.FiltersExpr) bool {
return filters.IsFalse()
}
// IsContradiction returns true if the given filter item contains a
// contradiction constraint.
func (c *CustomFuncs) IsContradiction(item *memo.FiltersItem) bool {
return item.ScalarProps(c.mem).Constraints == constraint.Contradiction
}
// ConcatFilters creates a new Filters operator that contains conditions from
// both the left and right boolean filter expressions.
func (c *CustomFuncs) ConcatFilters(left, right memo.FiltersExpr) memo.FiltersExpr {
// No need to recompute properties on the new filters, since they should
// still be valid.
newFilters := make(memo.FiltersExpr, len(left)+len(right))
copy(newFilters, left)
copy(newFilters[len(left):], right)
return newFilters
}
// RemoveFiltersItem returns a new list that is a copy of the given list, except
// that it does not contain the given search item. If the list contains the item
// multiple times, then only the first instance is removed. If the list does not
// contain the item, then the method panics.
func (c *CustomFuncs) RemoveFiltersItem(
filters memo.FiltersExpr, search *memo.FiltersItem,
) memo.FiltersExpr {
newFilters := make(memo.FiltersExpr, len(filters)-1)
for i := range filters {
if search == &filters[i] {
copy(newFilters, filters[:i])
copy(newFilters[i:], filters[i+1:])
return newFilters
}
}
panic(fmt.Sprintf("item to remove is not in the list: %v", search))
}
// ReplaceFiltersItem returns a new list that is a copy of the given list,
// except that the given search item has been replaced by the given replace
// item. If the list contains the search item multiple times, then only the
// first instance is replaced. If the list does not contain the item, then the
// method panics.
func (c *CustomFuncs) ReplaceFiltersItem(
filters memo.FiltersExpr, search *memo.FiltersItem, replace opt.ScalarExpr,
) memo.FiltersExpr {
newFilters := make([]memo.FiltersItem, len(filters))
for i := range filters {
if search == &filters[i] {
copy(newFilters, filters[:i])
newFilters[i].Condition = replace
copy(newFilters[i+1:], filters[i+1:])
return newFilters
}
}
panic(fmt.Sprintf("item to replace is not in the list: %v", search))
}
// FiltersBoundBy returns true if all outer references in any of the filter
// conditions are bound by the given columns. For example:
//
// (InnerJoin
// (Scan a)
// (Scan b)
// $filters:[ (FiltersItem (Eq (Variable a.x) (Const 1))) ]
// )
//
// The $filters expression is fully bound by the output columns of the (Scan a)
// expression because all of its outer references are satisfied by the columns
// produced by the Scan.
func (c *CustomFuncs) FiltersBoundBy(filters memo.FiltersExpr, cols opt.ColSet) bool {
for i := range filters {
if !filters[i].ScalarProps(c.mem).OuterCols.SubsetOf(cols) {
return false
}
}
return true
}
// ExtractBoundConditions returns a new list containing only those expressions
// from the given list that are fully bound by the given columns (i.e. all
// outer references are to one of these columns). For example:
//
// (InnerJoin
// (Scan a)
// (Scan b)
// (Filters [
// (Eq (Variable a.x) (Variable b.x))
// (Gt (Variable a.x) (Const 1))
// ])
// )
//
// Calling ExtractBoundConditions with the filter conditions list and the output
// columns of (Scan a) would extract the (Gt) expression, since its outer
// references only reference columns from a.
func (c *CustomFuncs) ExtractBoundConditions(
filters memo.FiltersExpr, cols opt.ColSet,
) memo.FiltersExpr {
newFilters := make(memo.FiltersExpr, 0, len(filters))
for i := range filters {
if c.IsBoundBy(&filters[i], cols) {
newFilters = append(newFilters, filters[i])
}
}
return newFilters
}
// ExtractUnboundConditions is the opposite of ExtractBoundConditions. Instead of
// extracting expressions that are bound by the given expression, it extracts
// list expressions that have at least one outer reference that is *not* bound
// by the given columns (i.e. it has a "free" variable).
func (c *CustomFuncs) ExtractUnboundConditions(
filters memo.FiltersExpr, cols opt.ColSet,
) memo.FiltersExpr {
newFilters := make(memo.FiltersExpr, 0, len(filters))
for i := range filters {
if !c.IsBoundBy(&filters[i], cols) {
newFilters = append(newFilters, filters[i])
}
}
return newFilters
}
// ----------------------------------------------------------------------
//
// Project functions
// General custom match and replace functions used to test and construct
// Project and Projections operators in Project rules.
//
// ----------------------------------------------------------------------
// CanMergeProjections returns true if the outer Projections operator never
// references any of the inner Projections columns. If true, then the outer does
// not depend on the inner, and the two can be merged into a single set.
func (c *CustomFuncs) CanMergeProjections(outer, inner memo.ProjectionsExpr) bool {
innerCols := c.ProjectionCols(inner)
for i := range outer {
if outer[i].ScalarProps(c.mem).OuterCols.Intersects(innerCols) {
return false
}
}
return true
}
// MergeProjections concatenates the synthesized columns from the outer
// Projections operator, and the synthesized columns from the inner Projections
// operator that are passed through by the outer. Note that the outer
// synthesized columns must never contain references to the inner synthesized
// columns; this can be verified by first calling CanMergeProjections.
func (c *CustomFuncs) MergeProjections(
outer, inner memo.ProjectionsExpr, passthrough opt.ColSet,
) memo.ProjectionsExpr {
// No need to recompute properties on the new projections, since they should
// still be valid.
newProjections := make(memo.ProjectionsExpr, len(outer), len(outer)+len(inner))
copy(newProjections, outer)
for i := range inner {
item := &inner[i]
if passthrough.Contains(int(item.Col)) {
newProjections = append(newProjections, *item)
}
}
return newProjections
}
// MergeProjectWithValues merges a Project operator with its input Values
// operator. This is only possible in certain circumstances, which must first be
// validated by calling CanMergeProjectWithValues (see its header comment).
// Values columns that are part of the Project passthrough columns are retained
// in the final Values operator, and Project synthesized columns are added to
// it. Any unreferenced Values columns are discarded. For example:
//
// SELECT column1, 3 FROM (VALUES (1, 2))
// =>
// (VALUES (1, 3))
//
func (c *CustomFuncs) MergeProjectWithValues(
projections memo.ProjectionsExpr, passthrough opt.ColSet, input memo.RelExpr,
) memo.RelExpr {
newExprs := make(memo.ScalarListExpr, 0, len(projections)+passthrough.Len())
newTypes := make([]types.T, 0, len(newExprs))
newCols := make(opt.ColList, 0, len(newExprs))
values := input.(*memo.ValuesExpr)
tuple := values.Rows[0].(*memo.TupleExpr)
for i, colID := range values.Cols {
if passthrough.Contains(int(colID)) {
newExprs = append(newExprs, tuple.Elems[i])
newTypes = append(newTypes, tuple.Elems[i].DataType())
newCols = append(newCols, colID)
}
}
for i := range projections {
item := &projections[i]
newExprs = append(newExprs, item.Element)
newTypes = append(newTypes, item.Element.DataType())
newCols = append(newCols, item.Col)
}
rows := memo.ScalarListExpr{c.f.ConstructTuple(newExprs, types.TTuple{Types: newTypes})}
return c.f.ConstructValues(rows, newCols)
}
// ProjectionCols returns the ids of the columns synthesized by the given
// Projections operator.
func (c *CustomFuncs) ProjectionCols(projections memo.ProjectionsExpr) opt.ColSet {
var colSet opt.ColSet
for i := range projections {
colSet.Add(int(projections[i].Col))
}
return colSet
}
// ProjectionOuterCols returns the union of all outer columns from the given
// projection expressions.
func (c *CustomFuncs) ProjectionOuterCols(projections memo.ProjectionsExpr) opt.ColSet {
var colSet opt.ColSet
for i := range projections {
colSet.UnionWith(projections[i].ScalarProps(c.mem).OuterCols)
}
return colSet
}
// AreProjectionsCorrelated returns true if any element in the projections
// references any of the given columns.
func (c *CustomFuncs) AreProjectionsCorrelated(
projections memo.ProjectionsExpr, cols opt.ColSet,
) bool {
for i := range projections {
if projections[i].ScalarProps(c.mem).OuterCols.Intersects(cols) {
return true
}
}
return false
}
// ProjectColMapLeft returns a Projections operator that maps the left side
// columns in a SetPrivate to the output columns in it. Useful for replacing set
// operations with simpler constructs.
func (c *CustomFuncs) ProjectColMapLeft(set *memo.SetPrivate) memo.ProjectionsExpr {
return c.projectColMapSide(set.OutCols, set.LeftCols)
}
// ProjectColMapRight returns a Project operator that maps the right side
// columns in a SetPrivate to the output columns in it. Useful for replacing set
// operations with simpler constructs.
func (c *CustomFuncs) ProjectColMapRight(set *memo.SetPrivate) memo.ProjectionsExpr {
return c.projectColMapSide(set.OutCols, set.RightCols)
}
// projectColMapSide implements the side-agnostic logic from ProjectColMapLeft
// and ProjectColMapRight.
func (c *CustomFuncs) projectColMapSide(toList, fromList opt.ColList) memo.ProjectionsExpr {
items := make(memo.ProjectionsExpr, len(toList))
for idx, fromCol := range fromList {
toCol := toList[idx]
items[idx].Element = c.f.ConstructVariable(fromCol)
items[idx].Col = toCol
}
return items
}
// MakeEmptyColSet returns a column set with no columns in it.
func (c *CustomFuncs) MakeEmptyColSet() opt.ColSet {
return opt.ColSet{}
}
// ----------------------------------------------------------------------
//
// Select Rules
// Custom match and replace functions used with select.opt rules.
//
// ----------------------------------------------------------------------
// SimplifyFilters removes True operands from a FiltersExpr, and normalizes any
// False or Null condition to a single False condition. Null values map to False
// because FiltersExpr are only used by Select and Join, both of which treat a
// Null filter conjunct exactly as if it were false.
//
// SimplifyFilters also "flattens" any And operator child by merging its
// conditions into a new FiltersExpr list. If, after simplification, no operands
// remain, then SimplifyFilters returns an empty FiltersExpr.
//
// This method assumes that the NormalizeNestedAnds rule has already run and
// ensured a left deep And tree. If not (maybe because it's a testing scenario),
// then this rule may rematch, but it should still make forward progress).
func (c *CustomFuncs) SimplifyFilters(filters memo.FiltersExpr) memo.FiltersExpr {
// Start by counting the number of conjuncts that will be flattened so that
// the capacity of the FiltersExpr list can be determined.
cnt := 0
for _, item := range filters {
cnt++
condition := item.Condition
for condition.Op() == opt.AndOp {
cnt++
condition = condition.(*memo.AndExpr).Left
}
}
// Construct new filter list.
newFilters := make(memo.FiltersExpr, 0, cnt)
for _, item := range filters {
var ok bool
if newFilters, ok = c.addConjuncts(item.Condition, newFilters); !ok {
return memo.FalseFilter
}
}
return newFilters
}
// addConjuncts recursively walks a scalar expression as long as it continues to
// find nested And operators. It adds any conjuncts (ignoring True operators) to
// the given FiltersExpr and returns true. If it finds a False or Null operator,
// it propagates a false return value all the up the call stack, and
// SimplifyFilters maps that to a FiltersExpr that is always false.
func (c *CustomFuncs) addConjuncts(
scalar opt.ScalarExpr, filters memo.FiltersExpr,
) (_ memo.FiltersExpr, ok bool) {
switch t := scalar.(type) {
case *memo.AndExpr:
var ok bool
if filters, ok = c.addConjuncts(t.Left, filters); !ok {
return nil, false
}
return c.addConjuncts(t.Right, filters)
case *memo.FalseExpr, *memo.NullExpr:
// Filters expression evaluates to False if any operand is False or Null.
return nil, false
case *memo.TrueExpr:
// Filters operator skips True operands.
default:
filters = append(filters, memo.FiltersItem{Condition: t})
}
return filters, true
}
// ConstructEmptyValues constructs a Values expression with no rows.
func (c *CustomFuncs) ConstructEmptyValues(cols opt.ColSet) memo.RelExpr {
colList := make(opt.ColList, 0, cols.Len())
for i, ok := cols.Next(0); ok; i, ok = cols.Next(i + 1) {
colList = append(colList, opt.ColumnID(i))
}
return c.f.ConstructValues(memo.EmptyScalarListExpr, colList)
}
// ----------------------------------------------------------------------
//
// GroupBy Rules
// Custom match and replace functions used with groupby.opt rules.
//
// ----------------------------------------------------------------------
// AggregationOuterCols returns the union of all outer columns from the given
// aggregation expressions.
func (c *CustomFuncs) AggregationOuterCols(aggregations memo.AggregationsExpr) opt.ColSet {
var colSet opt.ColSet
for i := range aggregations {
colSet.UnionWith(aggregations[i].ScalarProps(c.mem).OuterCols)
}
return colSet
}
// GroupingAndConstCols returns the grouping columns and ConstAgg columns (for
// which the input and output column IDs match). A filter on these columns can
// be pushed through a GroupBy.
func (c *CustomFuncs) GroupingAndConstCols(
grouping *memo.GroupingPrivate, aggs memo.AggregationsExpr,
) opt.ColSet {
result := grouping.GroupingCols.Copy()
// Add any ConstAgg columns.
for i := range aggs {
item := &aggs[i]
if constAgg, ok := item.Agg.(*memo.ConstAggExpr); ok {
// Verify that the input and output column IDs match.
if item.Col == constAgg.Input.(*memo.VariableExpr).Col {
result.Add(int(item.Col))
}
}
}
return result
}
// GroupingColsAreKey returns true if the input expression's grouping columns
// form a strict key for its output rows. A strict key means that any two rows
// will have unique key column values. Nulls are treated as equal to one another
// (i.e. no duplicate nulls allowed). Having a strict key means that the set of
// key column values uniquely determine the values of all other columns in the
// relation.
func (c *CustomFuncs) GroupingColsAreKey(grouping *memo.GroupingPrivate, input memo.RelExpr) bool {
colSet := grouping.GroupingCols
return input.Relational().FuncDeps.ColsAreStrictKey(colSet)
}
// IsUnorderedGrouping returns true if the given grouping ordering is not
// specified.
func (c *CustomFuncs) IsUnorderedGrouping(grouping *memo.GroupingPrivate) bool {
return grouping.Ordering.Any()
}
// ----------------------------------------------------------------------
//
// Limit Rules
// Custom match and replace functions used with limit.opt rules.
//
// ----------------------------------------------------------------------
// LimitGeMaxRows returns true if the given constant limit value is greater than
// or equal to the max number of rows returned by the input expression.
func (c *CustomFuncs) LimitGeMaxRows(limit tree.Datum, input memo.RelExpr) bool {
limitVal := int64(*limit.(*tree.DInt))
maxRows := input.Relational().Cardinality.Max
return limitVal >= 0 && maxRows < math.MaxUint32 && limitVal >= int64(maxRows)
}
// ----------------------------------------------------------------------
//
// ProjectSet Rules
// Custom match and replace functions used with ProjectSet rules.
//
// ----------------------------------------------------------------------
// IsZipCorrelated returns true if any element in the zip references
// any of the given columns.
func (c *CustomFuncs) IsZipCorrelated(zip memo.ZipExpr, cols opt.ColSet) bool {
for i := range zip {
if zip[i].ScalarProps(c.mem).OuterCols.Intersects(cols) {
return true
}
}
return false
}
// ZipOuterCols returns the union of all outer columns from the given
// zip expressions.
func (c *CustomFuncs) ZipOuterCols(zip memo.ZipExpr) opt.ColSet {
var colSet opt.ColSet
for i := range zip {
colSet.UnionWith(zip[i].ScalarProps(c.mem).OuterCols)
}
return colSet
}
// ----------------------------------------------------------------------
//
// Boolean Rules
// Custom match and replace functions used with bool.opt rules.
//
// ----------------------------------------------------------------------
// ConcatLeftDeepAnds concatenates any left-deep And expressions in the right
// expression with any left-deep And expressions in the left expression. The
// result is a combined left-deep And expression. Note that NormalizeNestedAnds
// has already guaranteed that both inputs will already be left-deep.
func (c *CustomFuncs) ConcatLeftDeepAnds(left, right opt.ScalarExpr) opt.ScalarExpr {
if and, ok := right.(*memo.AndExpr); ok {
return c.f.ConstructAnd(c.ConcatLeftDeepAnds(left, and.Left), and.Right)
}
return c.f.ConstructAnd(left, right)
}
// NegateComparison negates a comparison op like:
// a.x = 5
// to:
// a.x <> 5
func (c *CustomFuncs) NegateComparison(
cmp opt.Operator, left, right opt.ScalarExpr,
) opt.ScalarExpr {
negate := opt.NegateOpMap[cmp]
return c.f.DynamicConstruct(negate, left, right).(opt.ScalarExpr)
}
// CommuteInequality swaps the operands of an inequality comparison expression,
// changing the operator to compensate:
// 5 < x
// to:
// x > 5
func (c *CustomFuncs) CommuteInequality(
op opt.Operator, left, right opt.ScalarExpr,
) opt.ScalarExpr {
switch op {
case opt.GeOp:
return c.f.ConstructLe(right, left)
case opt.GtOp:
return c.f.ConstructLt(right, left)
case opt.LeOp:
return c.f.ConstructGe(right, left)
case opt.LtOp:
return c.f.ConstructGt(right, left)
}
panic(fmt.Sprintf("called commuteInequality with operator %s", op))
}
// FindRedundantConjunct takes the left and right operands of an Or operator as
// input. It examines each conjunct from the left expression and determines
// whether it appears as a conjunct in the right expression. If so, it returns
// the matching conjunct. Otherwise, it returns nil. For example:
//
// A OR A => A
// B OR A => nil
// A OR (A AND B) => A
// (A AND B) OR (A AND C) => A
// (A AND B AND C) OR (A AND (D OR E)) => A
//
// Once a redundant conjunct has been found, it is extracted via a call to the
// ExtractRedundantConjunct function. Redundant conjuncts are extracted from
// multiple nested Or operators by repeated application of these functions.
func (c *CustomFuncs) FindRedundantConjunct(left, right opt.ScalarExpr) opt.ScalarExpr {
// Recurse over each conjunct from the left expression and determine whether
// it's redundant.
for {
// Assume a left-deep And expression tree normalized by NormalizeNestedAnds.
if and, ok := left.(*memo.AndExpr); ok {
if c.isConjunct(and.Right, right) {
return and.Right
}
left = and.Left
} else {
if c.isConjunct(left, right) {
return left
}
return nil
}
}
}
// isConjunct returns true if the candidate expression is a conjunct within the
// given conjunction. The conjunction is assumed to be left-deep (normalized by
// the NormalizeNestedAnds rule).
func (c *CustomFuncs) isConjunct(candidate, conjunction opt.ScalarExpr) bool {
for {
if and, ok := conjunction.(*memo.AndExpr); ok {
if and.Right == candidate {
return true
}
conjunction = and.Left
} else {
return conjunction == candidate
}
}
}
// ExtractRedundantConjunct extracts a redundant conjunct from an Or expression,
// and returns an And of the conjunct with the remaining Or expression (a
// logically equivalent expression). For example:
//
// (A AND B) OR (A AND C) => A AND (B OR C)
//
// If extracting the conjunct from one of the OR conditions would result in an
// empty condition, the conjunct itself is returned (a logically equivalent
// expression). For example:
//
// A OR (A AND B) => A
//
// These transformations are useful for finding a conjunct that can be pushed
// down in the query tree. For example, if the redundant conjunct A is fully
// bound by one side of a join, it can be pushed through the join, even if B and
// C cannot.
func (c *CustomFuncs) ExtractRedundantConjunct(
conjunct, left, right opt.ScalarExpr,
) opt.ScalarExpr {
if conjunct == left || conjunct == right {
return conjunct
}
return c.f.ConstructAnd(
conjunct,
c.f.ConstructOr(
c.extractConjunct(conjunct, left.(*memo.AndExpr)),
c.extractConjunct(conjunct, right.(*memo.AndExpr)),
),
)
}
// extractConjunct traverses the And subtree looking for the given conjunct,
// which must be present. Once it's located, it's removed from the tree, and
// the remaining expression is returned.
func (c *CustomFuncs) extractConjunct(conjunct opt.ScalarExpr, and *memo.AndExpr) opt.ScalarExpr {
if and.Right == conjunct {
return and.Left
}
if and.Left == conjunct {
return and.Right
}
return c.f.ConstructAnd(c.extractConjunct(conjunct, and.Left.(*memo.AndExpr)), and.Right)
}
// ----------------------------------------------------------------------
//
// Comparison Rules
// Custom match and replace functions used with comp.opt rules.
//
// ----------------------------------------------------------------------
// NormalizeTupleEquality remaps the elements of two tuples compared for
// equality, like this:
// (a, b, c) = (x, y, z)
// into this:
// (a = x) AND (b = y) AND (c = z)
func (c *CustomFuncs) NormalizeTupleEquality(left, right memo.ScalarListExpr) opt.ScalarExpr {
if len(left) != len(right) {
panic("tuple length mismatch")
}
var result opt.ScalarExpr
for i := range left {
eq := c.f.ConstructEq(left[i], right[i])
if result == nil {