-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
join_funcs.go
859 lines (775 loc) · 32.2 KB
/
join_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
// Copyright 2020 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 xform
import (
"fmt"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/invertedidx"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/errors"
)
// GenerateMergeJoins spawns MergeJoinOps, based on any interesting orderings.
func (c *CustomFuncs) GenerateMergeJoins(
grp memo.RelExpr,
originalOp opt.Operator,
left, right memo.RelExpr,
on memo.FiltersExpr,
joinPrivate *memo.JoinPrivate,
) {
if joinPrivate.Flags.Has(memo.DisallowMergeJoin) {
return
}
leftProps := left.Relational()
rightProps := right.Relational()
leftEq, rightEq := memo.ExtractJoinEqualityColumns(
leftProps.OutputCols, rightProps.OutputCols, on,
)
n := len(leftEq)
if n == 0 {
return
}
// We generate MergeJoin expressions based on interesting orderings from the
// left side. The CommuteJoin rule will ensure that we actually try both
// sides.
orders := DeriveInterestingOrderings(left).Copy()
orders.RestrictToCols(leftEq.ToSet())
if !c.NoJoinHints(joinPrivate) || c.e.evalCtx.SessionData.ReorderJoinsLimit == 0 {
// If we are using a hint, or the join limit is set to zero, the join won't
// be commuted. Add the orderings from the right side.
rightOrders := DeriveInterestingOrderings(right).Copy()
rightOrders.RestrictToCols(leftEq.ToSet())
orders = append(orders, rightOrders...)
// If we don't allow hash join, we must do our best to generate a merge
// join, even if it means sorting both sides. We append an arbitrary
// ordering, in case the interesting orderings don't result in any merge
// joins.
o := make(opt.Ordering, len(leftEq))
for i := range o {
o[i] = opt.MakeOrderingColumn(leftEq[i], false /* descending */)
}
orders.Add(o)
}
if len(orders) == 0 {
return
}
var colToEq util.FastIntMap
for i := range leftEq {
colToEq.Set(int(leftEq[i]), i)
colToEq.Set(int(rightEq[i]), i)
}
var remainingFilters memo.FiltersExpr
for _, o := range orders {
if len(o) < n {
// TODO(radu): we have a partial ordering on the equality columns. We
// should augment it with the other columns (in arbitrary order) in the
// hope that we can get the full ordering cheaply using a "streaming"
// sort. This would not useful now since we don't support streaming sorts.
continue
}
if remainingFilters == nil {
remainingFilters = memo.ExtractRemainingJoinFilters(on, leftEq, rightEq)
}
merge := memo.MergeJoinExpr{Left: left, Right: right, On: remainingFilters}
merge.JoinPrivate = *joinPrivate
merge.JoinType = originalOp
merge.LeftEq = make(opt.Ordering, n)
merge.RightEq = make(opt.Ordering, n)
merge.LeftOrdering.Columns = make([]physical.OrderingColumnChoice, 0, n)
merge.RightOrdering.Columns = make([]physical.OrderingColumnChoice, 0, n)
for i := 0; i < n; i++ {
eqIdx, _ := colToEq.Get(int(o[i].ID()))
l, r, descending := leftEq[eqIdx], rightEq[eqIdx], o[i].Descending()
merge.LeftEq[i] = opt.MakeOrderingColumn(l, descending)
merge.RightEq[i] = opt.MakeOrderingColumn(r, descending)
merge.LeftOrdering.AppendCol(l, descending)
merge.RightOrdering.AppendCol(r, descending)
}
// Simplify the orderings with the corresponding FD sets.
merge.LeftOrdering.Simplify(&leftProps.FuncDeps)
merge.RightOrdering.Simplify(&rightProps.FuncDeps)
c.e.mem.AddMergeJoinToGroup(&merge, grp)
}
}
// GenerateLookupJoins looks at the possible indexes and creates lookup join
// expressions in the current group. A lookup join can be created when the ON
// condition has equality constraints on a prefix of the index columns.
//
// There are two cases:
//
// 1. The index has all the columns we need; this is the simple case, where we
// generate a LookupJoin expression in the current group:
//
// Join LookupJoin(t@idx))
// / \ |
// / \ -> |
// Input Scan(t) Input
//
//
// 2. The index is not covering, but we can fully evaluate the ON condition
// using the index, or we are doing an InnerJoin. We have to generate
// an index join above the lookup join. Note that this index join is also
// implemented as a LookupJoin, because an IndexJoin can only output
// columns from one table, whereas we also need to output columns from
// Input.
//
// Join LookupJoin(t@primary)
// / \ |
// / \ -> |
// Input Scan(t) LookupJoin(t@idx)
// |
// |
// Input
//
// For example:
// CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT)
// CREATE TABLE xyz (x INT PRIMARY KEY, y INT, z INT, INDEX (y))
// SELECT * FROM abc JOIN xyz ON a=y
//
// We want to first join abc with the index on y (which provides columns y, x)
// and then use a lookup join to retrieve column z. The "index join" (top
// LookupJoin) will produce columns a,b,c,x,y,z; the lookup columns are just x
// (the original index join produced a,b,c,x,y).
//
// Note that the top LookupJoin "sees" column IDs from the table on both
// "sides" (in this example x,y on the left and z on the right) but there is
// no overlap.
//
// 3. The index is not covering and we cannot fully evaluate the ON condition
// using the index, and we are doing a LeftJoin/SemiJoin/AntiJoin. This is
// handled using a lower-upper pair of joins that are further specialized
// as paired-joins. The first (lower) join outputs a continuation column
// that is used by the second (upper) join. Like case 2, both are lookup
// joins, but paired-joins explicitly know their role in the pair and
// behave accordingly.
// For example, using the same tables in the example for case 2:
// SELECT * FROM abc JOIN xyz ON a=y AND b=z
//
// The first join will evaluate a=y and produce columns a,b,c,x,y,cont
// where cont is the continuation column used to group together rows that
// correspond to the same original a,b,c. The second join will evaluate
// b=z and produce columns a,b,c,x,y,z. A similar approach works for
// anti-joins and semi-joins.
//
//
// A lookup join can be created when the ON condition or implicit filters from
// CHECK constraints and computed columns constrain a prefix of the index
// columns to non-ranging constant values. To support this, the constant values
// are cross-joined with the input and used as key columns for the parent lookup
// join.
//
// For example, consider the tables and query below.
//
// CREATE TABLE abc (a INT PRIMARY KEY, b INT, c INT)
// CREATE TABLE xyz (
// x INT PRIMARY KEY,
// y INT,
// z INT NOT NULL,
// CHECK z IN (1, 2, 3),
// INDEX (z, y)
// )
// SELECT a, x FROM abc JOIN xyz ON a=y
//
// GenerateLookupJoins will perform the following transformation.
//
// Join LookupJoin(t@idx)
// / \ |
// / \ -> |
// Input Scan(t) Join
// / \
// / \
// Input Values(1, 2, 3)
//
// If a column is constrained to a single constant value, inlining normalization
// rules will reduce the cross join into a project.
//
// Join LookupJoin(t@idx)
// / \ |
// / \ -> |
// Input Scan(t) Project
// |
// |
// Input
//
func (c *CustomFuncs) GenerateLookupJoins(
grp memo.RelExpr,
joinType opt.Operator,
input memo.RelExpr,
scanPrivate *memo.ScanPrivate,
on memo.FiltersExpr,
joinPrivate *memo.JoinPrivate,
) {
if joinPrivate.Flags.Has(memo.DisallowLookupJoinIntoRight) {
return
}
md := c.e.mem.Metadata()
inputProps := input.Relational()
leftEq, rightEq := memo.ExtractJoinEqualityColumns(inputProps.OutputCols, scanPrivate.Cols, on)
n := len(leftEq)
if n == 0 {
return
}
// Generate implicit filters from CHECK constraints and computed columns as
// optional filters to help generate lookup join keys.
optionalFilters := c.checkConstraintFilters(scanPrivate.Table)
computedColFilters := c.computedColFilters(scanPrivate.Table, on, optionalFilters)
optionalFilters = append(optionalFilters, computedColFilters...)
var pkCols opt.ColList
var iter scanIndexIter
iter.Init(c.e.mem, &c.im, scanPrivate, on, rejectInvertedIndexes)
iter.ForEach(func(index cat.Index, onFilters memo.FiltersExpr, indexCols opt.ColSet, isCovering bool) {
// Find the longest prefix of index key columns that are constrained by
// an equality with another column or a constant.
numIndexKeyCols := index.LaxKeyColumnCount()
var constFilters memo.FiltersExpr
allFilters := append(onFilters, optionalFilters...)
// Check if the first column in the index has an equality constraint, or if
// it is constrained to a constant value. This check doesn't guarantee that
// we will find lookup join key columns, but it avoids the unnecessary work
// in most cases.
firstIdxCol := scanPrivate.Table.IndexColumnID(index, 0)
if _, ok := rightEq.Find(firstIdxCol); !ok {
if _, _, ok := c.findJoinFilterConstants(allFilters, firstIdxCol); !ok {
return
}
}
lookupJoin := memo.LookupJoinExpr{Input: input}
lookupJoin.JoinPrivate = *joinPrivate
lookupJoin.JoinType = joinType
lookupJoin.Table = scanPrivate.Table
lookupJoin.Index = index.Ordinal()
lookupJoin.KeyCols = make(opt.ColList, 0, numIndexKeyCols)
rightSideCols := make(opt.ColList, 0, numIndexKeyCols)
// All the lookup conditions must apply to the prefix of the index and so
// the projected columns created must be created in order.
for j := 0; j < numIndexKeyCols; j++ {
idxCol := scanPrivate.Table.IndexColumnID(index, j)
if eqIdx, ok := rightEq.Find(idxCol); ok {
lookupJoin.KeyCols = append(lookupJoin.KeyCols, leftEq[eqIdx])
rightSideCols = append(rightSideCols, idxCol)
continue
}
// Try to find a filter that constrains this column to non-NULL
// constant values. We cannot use a NULL value because the lookup
// join implements logic equivalent to simple equality between
// columns (where NULL never equals anything).
foundVals, allIdx, ok := c.findJoinFilterConstants(allFilters, idxCol)
if !ok {
break
}
// We will join these constant values with the input to make
// equality columns for the lookup join.
if constFilters == nil {
constFilters = make(memo.FiltersExpr, 0, numIndexKeyCols-j)
}
idxColType := c.e.f.Metadata().ColumnMeta(idxCol).Type
constColAlias := fmt.Sprintf("lookup_join_const_col_@%d", idxCol)
join, constColID := c.constructJoinWithConstants(
lookupJoin.Input,
foundVals,
idxColType,
constColAlias,
)
lookupJoin.Input = join
lookupJoin.KeyCols = append(lookupJoin.KeyCols, constColID)
rightSideCols = append(rightSideCols, idxCol)
constFilters = append(constFilters, allFilters[allIdx])
}
if len(lookupJoin.KeyCols) == 0 {
// We couldn't find equality columns which we can lookup.
return
}
tableFDs := memo.MakeTableFuncDep(md, scanPrivate.Table)
// A lookup join will drop any input row which contains NULLs, so a lax key
// is sufficient.
lookupJoin.LookupColsAreTableKey = tableFDs.ColsAreLaxKey(rightSideCols.ToSet())
// Remove the redundant filters and update the lookup condition.
lookupJoin.On = memo.ExtractRemainingJoinFilters(onFilters, lookupJoin.KeyCols, rightSideCols)
lookupJoin.On.RemoveCommonFilters(constFilters)
lookupJoin.ConstFilters = constFilters
if isCovering {
// Case 1 (see function comment).
lookupJoin.Cols = scanPrivate.Cols.Union(inputProps.OutputCols)
c.e.mem.AddLookupJoinToGroup(&lookupJoin, grp)
return
}
_, isPartial := index.Predicate()
if isPartial && (joinType == opt.SemiJoinOp || joinType == opt.AntiJoinOp) {
// Typically, the index must cover all columns in the scanPrivate in
// order to generate a lookup join without an additional index join
// (case 1, see function comment). However, if the index is a
// partial index, the filters remaining after proving
// filter-predicate implication may no longer reference some
// columns. A lookup semi- or anti-join can be generated if the
// columns in the new filters from the right side of the join are
// covered by the index. Consider the example:
//
// CREATE TABLE a (a INT)
// CREATE TABLE xy (x INT, y INT, INDEX (x) WHERE y > 0)
//
// SELECT a FROM a WHERE EXISTS (SELECT 1 FROM xyz WHERE a = x AND y > 0)
//
// The original ON filters of the semi-join are (a = x AND y > 0).
// The (y > 0) expression in the filter is an exact match to the
// partial index predicate, so the remaining ON filters are (a = x).
// Column y is no longer referenced, so a lookup semi-join can be
// created despite the partial index not covering y.
//
// Note that this is a special case that only works for semi- and
// anti-joins because they never include columns from the right side
// in their output columns. Other joins include columns from the
// right side in their output columns, so even if the ON filters no
// longer reference an un-covered column, they must be fetched (case
// 2, see function comment).
filterColsFromRight := scanPrivate.Cols.Intersection(onFilters.OuterCols())
if filterColsFromRight.SubsetOf(indexCols) {
lookupJoin.Cols = filterColsFromRight.Union(inputProps.OutputCols)
c.e.mem.AddLookupJoinToGroup(&lookupJoin, grp)
return
}
}
// All code that follows is for cases 2 and 3 (see function comment).
// We need to generate two joins: a lower join followed by an upper join.
// In case 3, this lower-upper pair of joins is further specialized into
// paired-joins where we refer to the lower as first and upper as second.
if scanPrivate.Flags.NoIndexJoin {
return
}
pairedJoins := false
continuationCol := opt.ColumnID(0)
lowerJoinType := joinType
if joinType == opt.SemiJoinOp {
// Case 3: Semi joins are converted to a pair consisting of an inner
// lookup join and semi lookup join.
pairedJoins = true
lowerJoinType = opt.InnerJoinOp
} else if joinType == opt.AntiJoinOp {
// Case 3: Anti joins are converted to a pair consisting of a left
// lookup join and anti lookup join.
pairedJoins = true
lowerJoinType = opt.LeftJoinOp
}
if pkCols == nil {
pkIndex := md.Table(scanPrivate.Table).Index(cat.PrimaryIndex)
pkCols = make(opt.ColList, pkIndex.KeyColumnCount())
for i := range pkCols {
pkCols[i] = scanPrivate.Table.IndexColumnID(pkIndex, i)
}
}
// The lower LookupJoin must return all PK columns (they are needed as key
// columns for the index join).
lookupJoin.Cols = scanPrivate.Cols.Intersection(indexCols)
for i := range pkCols {
lookupJoin.Cols.Add(pkCols[i])
}
lookupJoin.Cols.UnionWith(inputProps.OutputCols)
var indexJoin memo.LookupJoinExpr
// onCols are the columns that the ON condition in the (lower) lookup join
// can refer to: input columns, or columns available in the index.
onCols := indexCols.Union(inputProps.OutputCols)
if c.FiltersBoundBy(lookupJoin.On, onCols) {
// Case 2.
// The ON condition refers only to the columns available in the index.
//
// For LeftJoin, both LookupJoins perform a LeftJoin. A null-extended row
// from the lower LookupJoin will not have any matches in the top
// LookupJoin (it has NULLs on key columns) and will get null-extended
// there as well.
indexJoin.On = memo.TrueFilter
} else {
// ON has some conditions that are bound by the columns in the index (at
// the very least, the equality conditions we used for KeyCols), and some
// conditions that refer to other columns. We can put the former in the
// lower LookupJoin and the latter in the index join.
//
// This works in a straightforward manner for InnerJoin but not for
// LeftJoin because of a technicality: if an input (left) row has
// matches in the lower LookupJoin but has no matches in the index join,
// only the columns looked up by the top index join get NULL-extended.
// Additionally if none of the lower matches are matches in the index
// join, we want to output only one NULL-extended row. To accomplish
// this, we need to use paired-joins.
if joinType == opt.LeftJoinOp {
// Case 3.
pairedJoins = true
// The lowerJoinType continues to be LeftJoinOp.
}
// We have already set pairedJoins=true for SemiJoin,AntiJoin earlier,
// and we don't need to do that for InnerJoin. The following sets up the
// ON conditions for both Case 2 and Case 3, when doing 2 joins that
// will each evaluate part of the ON condition.
conditions := lookupJoin.On
lookupJoin.On = c.ExtractBoundConditions(conditions, onCols)
indexJoin.On = c.ExtractUnboundConditions(conditions, onCols)
}
if pairedJoins {
lookupJoin.JoinType = lowerJoinType
continuationCol = c.constructContinuationColumnForPairedJoin()
lookupJoin.IsFirstJoinInPairedJoiner = true
lookupJoin.ContinuationCol = continuationCol
lookupJoin.Cols.Add(continuationCol)
}
indexJoin.Input = c.e.f.ConstructLookupJoin(
lookupJoin.Input,
lookupJoin.On,
&lookupJoin.LookupJoinPrivate,
)
indexJoin.JoinType = joinType
indexJoin.Table = scanPrivate.Table
indexJoin.Index = cat.PrimaryIndex
indexJoin.KeyCols = pkCols
indexJoin.Cols = scanPrivate.Cols.Union(inputProps.OutputCols)
indexJoin.LookupColsAreTableKey = true
if pairedJoins {
indexJoin.IsSecondJoinInPairedJoiner = true
}
c.addIndexJoinAsSecondJoinToMemo(grp, inputProps.OutputCols, &indexJoin)
})
}
// addIndexJoinAsSecondJoin is a helper function used when constructing two
// joins to accomplish a join in the query. The second join is over the
// primary index, and is added here.
func (c *CustomFuncs) addIndexJoinAsSecondJoinToMemo(
grp memo.RelExpr, inputColsOfFirstJoin opt.ColSet, indexJoin *memo.LookupJoinExpr,
) {
joinType := indexJoin.JoinType
// If this is not a semi- or anti-join, create the LookupJoin for the index
// join in the same group.
if joinType != opt.SemiJoinOp && joinType != opt.AntiJoinOp {
c.e.mem.AddLookupJoinToGroup(indexJoin, grp)
return
}
// Semi and anti joins here are always using paired-joins. Some of these
// require a project on top (see below). Avoid adding that projection if it
// will be a no-op (i.e., we already have the correct output columns from
// the lookup join).
outputCols := indexJoin.Cols.Intersection(indexJoin.Input.Relational().OutputCols)
if outputCols.SubsetOf(inputColsOfFirstJoin) {
c.e.mem.AddLookupJoinToGroup(indexJoin, grp)
return
}
// For some semi and anti joins, we need to add a project on top to ensure
// that only the original left-side columns are output. Normally, the
// LookupJoin would be able to perform the necessary projection for semi
// and anti joins by intersecting Cols with the OutputCols of its input,
// but that doesn't work for paired joins since the input to the second
// join may include more columns than the original input.
var project memo.ProjectExpr
project.Input = c.e.f.ConstructLookupJoin(
indexJoin.Input,
indexJoin.On,
&indexJoin.LookupJoinPrivate,
)
project.Passthrough = grp.Relational().OutputCols
c.e.mem.AddProjectToGroup(&project, grp)
}
// constructContinuationColumnForPairedJoin constructs a continuation column
// ID for the paired-joiners used for left outer/semi/anti joins when the
// first join generates false positives (due to an inverted index or
// non-covering index). The first join will be either a left outer join or
// an inner join.
func (c *CustomFuncs) constructContinuationColumnForPairedJoin() opt.ColumnID {
return c.e.f.Metadata().AddColumn("continuation", c.BoolType())
}
// GenerateInvertedJoins is similar to GenerateLookupJoins, but instead
// of generating lookup joins with regular indexes, it generates lookup joins
// with inverted indexes. Similar to GenerateLookupJoins, there are two cases
// depending on whether or not the index is covering. See the comment above
// GenerateLookupJoins for details.
func (c *CustomFuncs) GenerateInvertedJoins(
grp memo.RelExpr,
joinType opt.Operator,
input memo.RelExpr,
scanPrivate *memo.ScanPrivate,
on memo.FiltersExpr,
joinPrivate *memo.JoinPrivate,
) {
if joinPrivate.Flags.Has(memo.DisallowInvertedJoinIntoRight) {
return
}
inputCols := input.Relational().OutputCols
var pkCols opt.ColList
eqColsCalculated := false
var leftEqCols opt.ColList
var rightEqCols opt.ColList
var rightSideCols opt.ColList
var iter scanIndexIter
iter.Init(c.e.mem, &c.im, scanPrivate, on, rejectNonInvertedIndexes)
iter.ForEach(func(index cat.Index, on memo.FiltersExpr, indexCols opt.ColSet, isCovering bool) {
invertedJoin := memo.InvertedJoinExpr{Input: input}
numPrefixCols := index.NonInvertedPrefixColumnCount()
// Only calculate the left and right equality columns if there is a
// multi-column inverted index.
if numPrefixCols > 0 && !eqColsCalculated {
inputProps := input.Relational()
leftEqCols, rightEqCols = memo.ExtractJoinEqualityColumns(inputProps.OutputCols, scanPrivate.Cols, on)
eqColsCalculated = true
}
// The non-inverted prefix columns of a multi-column inverted index must
// be constrained in order to perform an inverted join. We attempt to
// constrain each prefix column to non-ranging constant values. These
// values are joined with the input to create key columns for the
// InvertedJoin, similar to GenerateLookupJoins.
// TODO(mgartner): Try to constrain prefix columns with CHECK
// constraints and computed column expressions.
var constFilters memo.FiltersExpr
for i := 0; i < numPrefixCols; i++ {
prefixCol := scanPrivate.Table.IndexColumnID(index, i)
// Check if prefixCol is constrained by an equality constraint.
if eqIdx, ok := rightEqCols.Find(prefixCol); ok {
invertedJoin.PrefixKeyCols = append(invertedJoin.PrefixKeyCols, leftEqCols[eqIdx])
rightSideCols = append(rightSideCols, prefixCol)
continue
}
// Try to constrain prefixCol to constant, non-ranging values.
foundVals, onIdx, ok := c.findJoinFilterConstants(on, prefixCol)
if !ok {
// Cannot constrain prefix column and therefore cannot generate
// an inverted join.
return
}
// We will join these constant values with the input to make
// equality columns for the inverted join.
if constFilters == nil {
constFilters = make(memo.FiltersExpr, 0, numPrefixCols)
}
prefixColType := c.e.f.Metadata().ColumnMeta(prefixCol).Type
constColAlias := fmt.Sprintf("inverted_join_const_col_@%d", prefixCol)
join, constColID := c.constructJoinWithConstants(
invertedJoin.Input,
foundVals,
prefixColType,
constColAlias,
)
invertedJoin.Input = join
invertedJoin.PrefixKeyCols = append(invertedJoin.PrefixKeyCols, constColID)
constFilters = append(constFilters, on[onIdx])
}
// Remove the redundant filters and update the ON condition if there are
// non-inverted prefix columns that have been constrained.
if len(rightSideCols) > 0 || len(constFilters) > 0 {
on = memo.ExtractRemainingJoinFilters(on, invertedJoin.PrefixKeyCols, rightSideCols)
on.RemoveCommonFilters(constFilters)
invertedJoin.ConstFilters = constFilters
}
// Check whether the filter can constrain the inverted column.
invertedExpr := invertedidx.TryJoinInvertedIndex(
c.e.evalCtx.Context, c.e.f, on, scanPrivate.Table, index, inputCols,
)
if invertedExpr == nil {
return
}
// All geospatial and JSON inverted joins that are currently supported
// are not covering, so we must wrap them in an index join.
// TODO(rytaft): Avoid adding an index join if possible for Array
// inverted joins.
if scanPrivate.Flags.NoIndexJoin {
return
}
if pkCols == nil {
tab := c.e.mem.Metadata().Table(scanPrivate.Table)
pkIndex := tab.Index(cat.PrimaryIndex)
pkCols = make(opt.ColList, pkIndex.KeyColumnCount())
for i := range pkCols {
pkCols[i] = scanPrivate.Table.IndexColumnID(pkIndex, i)
}
}
// Though the index is marked as containing the column being indexed, it
// doesn't actually, and it is only valid to extract the primary key
// columns and non-inverted prefix columns from it.
indexCols = pkCols.ToSet()
for i, n := 0, index.NonInvertedPrefixColumnCount(); i < n; i++ {
prefixCol := scanPrivate.Table.IndexColumnID(index, i)
indexCols.Add(prefixCol)
}
continuationCol := opt.ColumnID(0)
invertedJoinType := joinType
// Anti joins are converted to a pair consisting of a left inverted join
// and anti lookup join.
if joinType == opt.LeftJoinOp || joinType == opt.AntiJoinOp {
continuationCol = c.constructContinuationColumnForPairedJoin()
invertedJoinType = opt.LeftJoinOp
} else if joinType == opt.SemiJoinOp {
// Semi joins are converted to a pair consisting of an inner inverted
// join and semi lookup join.
continuationCol = c.constructContinuationColumnForPairedJoin()
invertedJoinType = opt.InnerJoinOp
}
invertedJoin.JoinPrivate = *joinPrivate
invertedJoin.JoinType = invertedJoinType
invertedJoin.Table = scanPrivate.Table
invertedJoin.Index = index.Ordinal()
invertedJoin.InvertedExpr = invertedExpr
invertedJoin.InvertedCol = scanPrivate.Table.IndexColumnID(index, 0)
invertedJoin.Cols = indexCols.Union(inputCols)
if continuationCol != 0 {
invertedJoin.Cols.Add(continuationCol)
invertedJoin.IsFirstJoinInPairedJoiner = true
invertedJoin.ContinuationCol = continuationCol
}
var indexJoin memo.LookupJoinExpr
// ON may have some conditions that are bound by the columns in the index
// and some conditions that refer to other columns. We can put the former
// in the InvertedJoin and the latter in the index join.
invertedJoin.On = c.ExtractBoundConditions(on, invertedJoin.Cols)
indexJoin.On = c.ExtractUnboundConditions(on, invertedJoin.Cols)
indexJoin.Input = c.e.f.ConstructInvertedJoin(
invertedJoin.Input,
invertedJoin.On,
&invertedJoin.InvertedJoinPrivate,
)
indexJoin.JoinType = joinType
indexJoin.Table = scanPrivate.Table
indexJoin.Index = cat.PrimaryIndex
indexJoin.KeyCols = pkCols
indexJoin.Cols = scanPrivate.Cols.Union(inputCols)
indexJoin.LookupColsAreTableKey = true
if continuationCol != 0 {
indexJoin.IsSecondJoinInPairedJoiner = true
}
c.addIndexJoinAsSecondJoinToMemo(grp, inputCols, &indexJoin)
})
}
// findJoinFilterConstants tries to find a filter that is exactly equivalent to
// constraining the given column to a constant value or a set of constant
// values. If successful, the constant values and the index of the constraining
// FiltersItem are returned. Note that the returned constant values do not
// contain NULL.
func (c *CustomFuncs) findJoinFilterConstants(
filters memo.FiltersExpr, col opt.ColumnID,
) (values tree.Datums, filterIdx int, ok bool) {
for filterIdx := range filters {
props := filters[filterIdx].ScalarProps()
if props.TightConstraints {
constCol, constVals, ok := props.Constraints.HasSingleColumnConstValues(c.e.evalCtx)
if !ok || constCol != col {
continue
}
hasNull := false
for i := range constVals {
if constVals[i] == tree.DNull {
hasNull = true
break
}
}
if !hasNull {
return constVals, filterIdx, true
}
}
}
return nil, -1, false
}
// constructJoinWithConstants constructs a cross join that joins every row in
// the input with every value in vals. The cross join will be converted into a
// projection by inlining normalization rules if vals contains only a single
// value. The constructed expression and the column ID of the constant value
// column are returned.
func (c *CustomFuncs) constructJoinWithConstants(
input memo.RelExpr, vals tree.Datums, typ *types.T, columnAlias string,
) (join memo.RelExpr, constColID opt.ColumnID) {
constColID = c.e.f.Metadata().AddColumn(columnAlias, typ)
tupleType := types.MakeTuple([]*types.T{typ})
constRows := make(memo.ScalarListExpr, len(vals))
for i := range constRows {
constRows[i] = c.e.f.ConstructTuple(
memo.ScalarListExpr{c.e.f.ConstructConst(vals[i], typ)},
tupleType,
)
}
values := c.e.f.ConstructValues(
constRows,
&memo.ValuesPrivate{
Cols: opt.ColList{constColID},
ID: c.e.mem.Metadata().NextUniqueID(),
},
)
// We purposefully do not propagate any join flags into this JoinPrivate. If
// a LOOKUP join hint was propagated to this cross join, the cost of the
// cross join would be artificially inflated and the lookup join would not
// be selected as the optimal plan.
join = c.e.f.ConstructInnerJoin(input, values, nil /* on */, &memo.JoinPrivate{})
return join, constColID
}
// ShouldReorderJoins returns whether the optimizer should attempt to find
// a better ordering of inner joins. This is the case if the given expression is
// the first expression of its group, and the join tree rooted at the expression
// has not previously been reordered. This is to avoid duplicate work. In
// addition, a join cannot be reordered if it has join hints.
func (c *CustomFuncs) ShouldReorderJoins(root memo.RelExpr) bool {
// Only match the first expression of a group to avoid duplicate work.
if root != root.FirstExpr() {
return false
}
private, ok := root.Private().(*memo.JoinPrivate)
if !ok {
panic(errors.AssertionFailedf("operator does not have a join private: %v", root.Op()))
}
// Ensure that this join expression was not added to the memo by a previous
// reordering, as well as that the join does not have hints.
return !private.WasReordered && c.NoJoinHints(private)
}
// ReorderJoins adds alternate orderings of the given join tree to the memo. The
// first expression of the memo group is used for construction of the join
// graph. For more information, see the comment in join_order_builder.go.
func (c *CustomFuncs) ReorderJoins(grp memo.RelExpr) memo.RelExpr {
c.e.o.JoinOrderBuilder().Init(c.e.f, c.e.evalCtx)
c.e.o.JoinOrderBuilder().Reorder(grp.FirstExpr())
return grp
}
// IsSimpleEquality returns true if all of the filter conditions are equalities
// between simple data types (constants, variables, tuples and NULL).
func (c *CustomFuncs) IsSimpleEquality(filters memo.FiltersExpr) bool {
for i := range filters {
eqFilter, ok := filters[i].Condition.(*memo.EqExpr)
if !ok {
return false
}
left, right := eqFilter.Left, eqFilter.Right
switch left.Op() {
case opt.VariableOp, opt.ConstOp, opt.NullOp, opt.TupleOp:
default:
return false
}
switch right.Op() {
case opt.VariableOp, opt.ConstOp, opt.NullOp, opt.TupleOp:
default:
return false
}
}
return true
}
// ConvertIndexToLookupJoinPrivate constructs a new LookupJoinPrivate using the
// given IndexJoinPrivate with the given output columns.
func (c *CustomFuncs) ConvertIndexToLookupJoinPrivate(
indexPrivate *memo.IndexJoinPrivate, outCols opt.ColSet,
) *memo.LookupJoinPrivate {
// Retrieve an ordered list of primary key columns from the lookup table;
// these will form the lookup key.
md := c.e.mem.Metadata()
primaryIndex := md.Table(indexPrivate.Table).Index(cat.PrimaryIndex)
lookupCols := make(opt.ColList, primaryIndex.KeyColumnCount())
for i := 0; i < primaryIndex.KeyColumnCount(); i++ {
lookupCols[i] = indexPrivate.Table.IndexColumnID(primaryIndex, i)
}
return &memo.LookupJoinPrivate{
JoinType: opt.InnerJoinOp,
Table: indexPrivate.Table,
Index: cat.PrimaryIndex,
KeyCols: lookupCols,
Cols: outCols,
LookupColsAreTableKey: true,
ConstFilters: nil,
JoinPrivate: memo.JoinPrivate{},
}
}