-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
join.go
765 lines (683 loc) · 25.7 KB
/
join.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
// 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 norm
import (
"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/sqlbase"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// ----------------------------------------------------------------------
//
// Join Rules
// Custom match and replace functions used with join.opt rules.
//
// ----------------------------------------------------------------------
// EmptyJoinPrivate returns an unset JoinPrivate.
func (c *CustomFuncs) EmptyJoinPrivate() *memo.JoinPrivate {
return memo.EmptyJoinPrivate
}
// ConstructNonLeftJoin maps a left join to an inner join and a full join to a
// right join when it can be proved that the right side of the join always
// produces at least one row for every row on the left.
func (c *CustomFuncs) ConstructNonLeftJoin(
joinOp opt.Operator, left, right memo.RelExpr, on memo.FiltersExpr, private *memo.JoinPrivate,
) memo.RelExpr {
switch joinOp {
case opt.LeftJoinOp:
return c.f.ConstructInnerJoin(left, right, on, private)
case opt.LeftJoinApplyOp:
return c.f.ConstructInnerJoinApply(left, right, on, private)
case opt.FullJoinOp:
return c.f.ConstructRightJoin(left, right, on, private)
}
panic(errors.AssertionFailedf("unexpected join operator: %v", log.Safe(joinOp)))
}
// ConstructNonRightJoin maps a right join to an inner join and a full join to a
// left join when it can be proved that the left side of the join always
// produces at least one row for every row on the right.
func (c *CustomFuncs) ConstructNonRightJoin(
joinOp opt.Operator, left, right memo.RelExpr, on memo.FiltersExpr, private *memo.JoinPrivate,
) memo.RelExpr {
switch joinOp {
case opt.RightJoinOp:
return c.f.ConstructInnerJoin(left, right, on, private)
case opt.FullJoinOp:
return c.f.ConstructLeftJoin(left, right, on, private)
}
panic(errors.AssertionFailedf("unexpected join operator: %v", log.Safe(joinOp)))
}
// SimplifyNotNullEquality simplifies an expression of the following form:
//
// (Is | IsNot (Eq) (True | False | Null))
//
// in the case where the Eq expression is guaranteed to never result in null.
// The testOp argument must be IsOp or IsNotOp, and the constOp argument must be
// TrueOp, FalseOp, or NullOp.
func (c *CustomFuncs) SimplifyNotNullEquality(
eq opt.ScalarExpr, testOp, constOp opt.Operator,
) opt.ScalarExpr {
switch testOp {
case opt.IsOp:
switch constOp {
case opt.TrueOp:
return eq
case opt.FalseOp:
return c.f.ConstructNot(eq)
case opt.NullOp:
return c.f.ConstructFalse()
}
case opt.IsNotOp:
switch constOp {
case opt.TrueOp:
return c.f.ConstructNot(eq)
case opt.FalseOp:
return eq
case opt.NullOp:
return c.f.ConstructTrue()
}
}
panic(errors.AssertionFailedf("invalid ops: %v, %v", testOp, constOp))
}
// CanMapJoinOpFilter returns true if it is possible to map a boolean expression
// src, which is a conjunct in the given filters expression, to use the output
// columns of the relational expression dst.
//
// In order for one column to map to another, the two columns must be
// equivalent. This happens when there is an equality predicate such as a.x=b.x
// in the ON or WHERE clause. Additionally, the two columns must be of the same
// type (see GetEquivColsWithEquivType for details). CanMapJoinOpFilter checks
// that for each column in src, there is at least one equivalent column in dst.
//
// For example, consider this query:
//
// SELECT * FROM a INNER JOIN b ON a.x=b.x AND a.x + b.y = 5
//
// Since there is an equality predicate on a.x=b.x, it is possible to map
// a.x + b.y = 5 to b.x + b.y = 5, and that allows the filter to be pushed down
// to the right side of the join. In this case, CanMapJoinOpFilter returns true
// when src is a.x + b.y = 5 and dst is (Scan b), but false when src is
// a.x + b.y = 5 and dst is (Scan a).
//
// If src has a correlated subquery, CanMapJoinOpFilter returns false.
func (c *CustomFuncs) CanMapJoinOpFilter(
filters memo.FiltersExpr, src *memo.FiltersItem, dst memo.RelExpr,
) bool {
// Fast path if src is already bound by dst.
if c.IsBoundBy(src, c.OutputCols(dst)) {
return true
}
scalarProps := src.ScalarProps(c.mem)
if scalarProps.HasCorrelatedSubquery {
return false
}
// For CanMapJoinOpFilter to be true, each column in src must map to at
// least one column in dst.
for i, ok := scalarProps.OuterCols.Next(0); ok; i, ok = scalarProps.OuterCols.Next(i + 1) {
eqCols := c.GetEquivColsWithEquivType(i, filters)
if !eqCols.Intersects(c.OutputCols(dst)) {
return false
}
}
return true
}
// CanMapJoinOpEqualities checks whether it is possible to map equality
// conditions in a join to use different variables so that the number of
// conditions crossing both sides of a join are minimized.
//
// Specifically, it finds the set of columns containing col that forms an
// equivalence group in filters. It splits that group into columns from
// the left and right sides of the join, and checks whether there are multiple
// equality conditions in filters that connect the two groups. If so,
// CanMapJoinOpEqualities returns true.
func (c *CustomFuncs) CanMapJoinOpEqualities(
filters memo.FiltersExpr, col opt.ColumnID, leftCols, rightCols opt.ColSet,
) bool {
eqCols := c.GetEquivColsWithEquivType(col, filters)
// To map equality conditions, the equivalent columns must intersect
// both sides and must be fully bound by both sides.
if !(eqCols.Intersects(leftCols) &&
eqCols.Intersects(rightCols) &&
eqCols.SubsetOf(leftCols.Union(rightCols))) {
return false
}
// If more than one equality condition connecting columns in the equivalence
// group spans both sides of the join, these conditions can be remapped.
found := false
for i := range filters {
fd := &filters[i].ScalarProps(c.mem).FuncDeps
filterEqCols := fd.ComputeEquivClosure(fd.EquivReps())
if filterEqCols.Intersects(leftCols) && filterEqCols.Intersects(rightCols) &&
filterEqCols.SubsetOf(eqCols) {
if found {
return true
}
found = true
}
}
return false
}
// MapJoinOpEqualities maps equality conditions in a join to use different
// variables so that the number of conditions crossing both sides of a join are
// minimized. This is useful for creating additional filter conditions that can
// be pushed down to either side of the join.
//
// To perform the mapping, MapJoinOpEqualities finds the set of columns
// containing col that forms an equivalence group in filters. Then it
// (conceptually) constructs a graph in which the nodes represent columns and
// the edges represent equality conditions between the columns. The goal is to
// select edges that form a minimum spanning tree such that only a single edge
// connects columns from the left side and the right side. For example, suppose
// the initial graph looks like this (L and R indicate left and right columns):
//
// L R
// 0 -- 0
// |
// 0 -- 0
//
// MapJoinOpEqualities would reconstruct the graph to look like this:
//
// L R
// 0 -- 0
// | |
// 0 0
//
// In SQL, this corresponds to converting the query:
//
// SELECT * FROM a, b WHERE a.x = b.x AND a.x = a.y AND a.y = b.y
//
// to:
//
// SELECT * FROM a, b WHERE a.x = b.x AND a.x = a.y AND b.x = b.y
//
func (c *CustomFuncs) MapJoinOpEqualities(
filters memo.FiltersExpr, col opt.ColumnID, leftCols, rightCols opt.ColSet,
) memo.FiltersExpr {
eqCols := c.GetEquivColsWithEquivType(col, filters)
// First remove all the equality conditions for this equivalence group.
newFilters := make(memo.FiltersExpr, 0, len(filters))
for i := range filters {
fd := &filters[i].ScalarProps(c.mem).FuncDeps
filterEqCols := fd.ComputeEquivClosure(fd.EquivReps())
if !filterEqCols.Empty() && filterEqCols.SubsetOf(eqCols) {
continue
}
newFilters = append(newFilters, filters[i])
}
// Now append new equality conditions that represent the minimum spanning
// tree for this equivalence group.
leftEqCols := leftCols.Intersection(eqCols)
rightEqCols := rightCols.Intersection(eqCols)
prevCol, ok := leftEqCols.Next(0)
if !ok {
panic(errors.AssertionFailedf(
"MapJoinOpEqualities called with equivalence group that does not intersect both sides",
))
}
for col, ok := leftEqCols.Next(prevCol + 1); ok; col, ok = leftEqCols.Next(col + 1) {
newFilters = append(newFilters, memo.FiltersItem{
Condition: c.f.ConstructEq(c.f.ConstructVariable(prevCol), c.f.ConstructVariable(col)),
})
prevCol = col
}
for col, ok := rightEqCols.Next(0); ok; col, ok = rightEqCols.Next(col + 1) {
newFilters = append(newFilters, memo.FiltersItem{
Condition: c.f.ConstructEq(c.f.ConstructVariable(prevCol), c.f.ConstructVariable(col)),
})
prevCol = col
}
return newFilters
}
// MapJoinOpFilter maps a boolean expression src, which is a conjunct in
// the given filters expression, to use the output columns of the relational
// expression dst.
//
// MapJoinOpFilter assumes that CanMapJoinOpFilter has already returned true,
// and therefore a mapping is possible (see comment above CanMapJoinOpFilter
// for details).
//
// For each column in src that is not also in dst, MapJoinOpFilter replaces it
// with an equivalent column in dst. If there are multiple equivalent columns
// in dst, it chooses one arbitrarily. MapJoinOpFilter does not replace any
// columns in subqueries, since we know there are no correlated subqueries
// (otherwise CanMapJoinOpFilter would have returned false).
//
// For example, consider this query:
//
// SELECT * FROM a INNER JOIN b ON a.x=b.x AND a.x + b.y = 5
//
// If MapJoinOpFilter is called with src as a.x + b.y = 5 and dst as (Scan b),
// it returns b.x + b.y = 5. MapJoinOpFilter should not be called with the
// equality predicate a.x = b.x, because it would just return the tautology
// b.x = b.x.
func (c *CustomFuncs) MapJoinOpFilter(
filters memo.FiltersExpr, src *memo.FiltersItem, dst memo.RelExpr,
) opt.ScalarExpr {
// Fast path if src is already bound by dst.
if c.IsBoundBy(src, c.OutputCols(dst)) {
return src.Condition
}
// Map each column in src to one column in dst. We choose an arbitrary column
// (the one with the smallest ColumnID) if there are multiple choices.
var colMap util.FastIntMap
outerCols := src.ScalarProps(c.mem).OuterCols
for srcCol, ok := outerCols.Next(0); ok; srcCol, ok = outerCols.Next(srcCol + 1) {
eqCols := c.GetEquivColsWithEquivType(srcCol, filters)
eqCols.IntersectionWith(c.OutputCols(dst))
if eqCols.Contains(srcCol) {
colMap.Set(int(srcCol), int(srcCol))
} else {
dstCol, ok := eqCols.Next(0)
if !ok {
panic(errors.AssertionFailedf(
"MapJoinOpFilter called on src that cannot be mapped to dst. src:\n%s\ndst:\n%s",
src, dst,
))
}
colMap.Set(int(srcCol), int(dstCol))
}
}
// Recursively walk the scalar sub-tree looking for references to columns
// that need to be replaced.
var replace ReplaceFunc
replace = func(nd opt.Expr) opt.Expr {
switch t := nd.(type) {
case *memo.VariableExpr:
outCol, _ := colMap.Get(int(t.Col))
if int(t.Col) == outCol {
// Avoid constructing a new variable if possible.
return nd
}
return c.f.ConstructVariable(opt.ColumnID(outCol))
case *memo.SubqueryExpr, *memo.ExistsExpr, *memo.AnyExpr:
// There are no correlated subqueries, so we don't need to recurse here.
return nd
}
return c.f.Replace(nd, replace)
}
return replace(src.Condition).(opt.ScalarExpr)
}
// MapAllJoinOpEqualities maps all variable equality conditions in filters to
// use columns in either leftCols or rightCols where possible.
// See CanMapJoinOpEqualities and MapJoinOpEqualities for more info.
func (c *CustomFuncs) MapAllJoinOpEqualities(
filters memo.FiltersExpr, leftCols, rightCols opt.ColSet,
) memo.FiltersExpr {
var equivFD props.FuncDepSet
for i := range filters {
equivFD.AddEquivFrom(&filters[i].ScalarProps(c.mem).FuncDeps)
}
equivReps := equivFD.EquivReps()
newFilters := filters
equivReps.ForEach(func(col opt.ColumnID) {
if c.CanMapJoinOpEqualities(newFilters, col, leftCols, rightCols) {
newFilters = c.MapJoinOpEqualities(newFilters, col, leftCols, rightCols)
}
})
return newFilters
}
// GetEquivColsWithEquivType uses the given FuncDepSet to find columns that are
// equivalent to col, and returns only those columns that also have the same
// type as col. This function is used when inferring new filters based on
// equivalent columns, because operations that are valid with one type may be
// invalid with a different type.
//
// In addition, if col has a composite key encoding, we cannot guarantee that
// it will be exactly equal to other "equivalent" columns, so in that case we
// return a set containing only col. This is a conservative measure to ensure
// that we don't infer filters incorrectly. For example, consider this query:
//
// SELECT * FROM
// (VALUES (1.0)) AS t1(x),
// (VALUES (1.00)) AS t2(y)
// WHERE x=y AND x::text = '1.0';
//
// It should return the following result:
//
// x | y
// -----+------
// 1.0 | 1.00
//
// But if we use the equality predicate x=y to map x to y and infer an
// additional filter y::text = '1.0', the query would return nothing.
//
// TODO(rytaft): In the future, we may want to allow the mapping if the
// filter involves a comparison operator, such as x < 5.
func (c *CustomFuncs) GetEquivColsWithEquivType(
col opt.ColumnID, filters memo.FiltersExpr,
) opt.ColSet {
var res opt.ColSet
colType := c.f.Metadata().ColumnMeta(col).Type
// Don't bother looking for equivalent columns if colType has a composite
// key encoding.
if sqlbase.DatumTypeHasCompositeKeyEncoding(colType) {
res.Add(col)
return res
}
// Compute all equivalent columns.
var equivFD props.FuncDepSet
for i := range filters {
equivFD.AddEquivFrom(&filters[i].ScalarProps(c.mem).FuncDeps)
}
eqCols := equivFD.ComputeEquivGroup(col)
eqCols.ForEach(func(i opt.ColumnID) {
// Only include columns that have the same type as col.
eqColType := c.f.Metadata().ColumnMeta(i).Type
if colType.Equivalent(eqColType) {
res.Add(i)
}
})
return res
}
// eqConditionsToColMap returns a map of left columns to right columns
// that are being equated in the specified conditions. leftCols is used
// to identify which column is a left column.
func (c *CustomFuncs) eqConditionsToColMap(
filters memo.FiltersExpr, leftCols opt.ColSet,
) map[opt.ColumnID]opt.ColumnID {
eqColMap := make(map[opt.ColumnID]opt.ColumnID)
for i := range filters {
eq, _ := filters[i].Condition.(*memo.EqExpr)
if eq == nil {
continue
}
leftVarExpr, _ := eq.Left.(*memo.VariableExpr)
rightVarExpr, _ := eq.Right.(*memo.VariableExpr)
if leftVarExpr == nil || rightVarExpr == nil {
continue
}
if leftCols.Contains(leftVarExpr.Col) {
eqColMap[leftVarExpr.Col] = rightVarExpr.Col
} else {
eqColMap[rightVarExpr.Col] = leftVarExpr.Col
}
}
return eqColMap
}
// JoinFiltersMatchAllLeftRows returns true when each row in the given join's
// left input matches at least one row from the right input, according to the
// join filters. This is true when the following conditions are satisfied:
//
// 1. Each conjunct in the join condition is an equality between a not-null
// column from the left input and a not-null column from the right input.
// 2. All left input equality columns come from a single table (called its
// "equality table"), as do all right input equality columns (can be
// different table).
// 3. The right input contains every row from its equality table. There may be
// a subset of columns from the table, and/or duplicate rows, but every row
// must be present.
// 4. If the left equality table is the same as the right equality table, then
// it's the self-join case. The columns in each equality pair must have the
// same ordinal position in the table.
// 5. If the left equality table is different than the right equality table,
// then it's the foreign-key case. The left equality columns must map to
// a foreign key on the left equality table, and the right equality columns
// to the corresponding referenced columns in the right equality table.
//
func (c *CustomFuncs) JoinFiltersMatchAllLeftRows(
left, right memo.RelExpr, filters memo.FiltersExpr,
) bool {
unfilteredCols := c.deriveUnfilteredCols(right)
if unfilteredCols.Empty() {
// Condition #3: right input has no columns which contain values from
// every row.
return false
}
leftColIDs := left.Relational().NotNullCols
rightColIDs := right.Relational().NotNullCols
md := c.f.Metadata()
var leftTab, rightTab opt.TableID
// Any left columns that don't match conditions 1-4 end up in this set.
var remainingLeftColIDs opt.ColSet
for i := range filters {
eq, _ := filters[i].Condition.(*memo.EqExpr)
if eq == nil {
// Condition #1: conjunct is not an equality comparison.
return false
}
leftVar, _ := eq.Left.(*memo.VariableExpr)
rightVar, _ := eq.Right.(*memo.VariableExpr)
if leftVar == nil || rightVar == nil {
// Condition #1: conjunct does not compare two columns.
return false
}
leftColID := leftVar.Col
rightColID := rightVar.Col
// Normalize leftColID to come from leftColIDs.
if !leftColIDs.Contains(leftColID) {
leftColID, rightColID = rightColID, leftColID
}
if !leftColIDs.Contains(leftColID) || !rightColIDs.Contains(rightColID) {
// Condition #1: columns don't come from both sides of join, or
// columns are nullable.
return false
}
if !unfilteredCols.Contains(rightColID) {
// Condition #3: right column doesn't contain values from every row.
return false
}
if leftTab == 0 {
leftTab = md.ColumnMeta(leftColID).Table
rightTab = md.ColumnMeta(rightColID).Table
if leftTab == 0 || rightTab == 0 {
// Condition #2: Columns don't come from base tables.
return false
}
} else if md.ColumnMeta(leftColID).Table != leftTab {
// Condition #2: All left columns don't come from same table.
return false
} else if md.ColumnMeta(rightColID).Table != rightTab {
// Condition #2: All right columns don't come from same table.
return false
}
if md.TableMeta(leftTab).Table == md.TableMeta(rightTab).Table {
// Check self-join case.
leftColOrd := leftTab.ColumnOrdinal(leftColID)
rightColOrd := rightTab.ColumnOrdinal(rightColID)
if leftColOrd != rightColOrd {
// Condition #4: Left and right column ordinals do not match.
return false
}
} else {
// Column could be a potential foreign key match so save it.
remainingLeftColIDs.Add(leftColID)
}
}
if remainingLeftColIDs.Empty() {
return true
}
var leftRightColMap map[opt.ColumnID]opt.ColumnID
// Condition #5: All remaining left columns correspond to a validated foreign
// key relation.
leftTabMeta := md.TableMeta(leftTab)
if leftTabMeta.IgnoreForeignKeys {
// We are not allowed to use any of the left table's outbound foreign keys.
return false
}
rightTabMeta := md.TableMeta(rightTab)
// Search for validated foreign key references from the left table to the
// right table.
for i, cnt := 0, leftTabMeta.Table.OutboundForeignKeyCount(); i < cnt; i++ {
fkRef := leftTabMeta.Table.OutboundForeignKey(i)
if fkRef.ReferencedTableID() != rightTabMeta.Table.ID() || !fkRef.Validated() {
continue
}
fkTable := md.TableByStableID(fkRef.ReferencedTableID())
if fkTable == nil {
continue
}
var leftIndexCols opt.ColSet
numCols := fkRef.ColumnCount()
for j := 0; j < numCols; j++ {
ord := fkRef.OriginColumnOrdinal(leftTabMeta.Table, j)
leftIndexCols.Add(leftTab.ColumnID(ord))
}
if !remainingLeftColIDs.SubsetOf(leftIndexCols) {
continue
}
// Build a mapping of left to right columns as specified
// in the filter conditions - this is used to detect
// whether the filter conditions follow the foreign key
// constraint exactly.
if leftRightColMap == nil {
leftRightColMap = c.eqConditionsToColMap(filters, leftColIDs)
}
// Loop through all columns in fk index that also exist in LHS of match condition,
// and ensure that they correspond to the correct RHS column according to the
// foreign key relation. In other words, each LHS column's index ordinal
// in the foreign key index matches that of the RHS column (in the index being
// referenced) that it's being equated to.
fkMatch := true
for j := 0; j < numCols; j++ {
indexLeftCol := leftTab.ColumnID(fkRef.OriginColumnOrdinal(leftTabMeta.Table, j))
// Not every fk column needs to be in the equality conditions.
if !remainingLeftColIDs.Contains(indexLeftCol) {
continue
}
indexRightCol := rightTab.ColumnID(fkRef.ReferencedColumnOrdinal(fkTable, j))
if rightCol, ok := leftRightColMap[indexLeftCol]; !ok || rightCol != indexRightCol {
fkMatch = false
break
}
}
// Condition #5 satisfied.
if fkMatch {
return true
}
}
return false
}
// deriveUnfilteredCols returns the subset of the given input expression's
// output columns that have values for every row in their owner table. In other
// words, columns from tables that have had none of their rows filtered (but
// it's OK if rows have been duplicated).
//
// deriveUnfilteredCols recursively derives the property, and populates the
// props.Relational.Rule.UnfilteredCols field as it goes to make future calls
// faster.
func (c *CustomFuncs) deriveUnfilteredCols(in memo.RelExpr) opt.ColSet {
// If the UnfilteredCols property has already been derived, return it
// immediately.
relational := in.Relational()
if relational.IsAvailable(props.UnfilteredCols) {
return relational.Rule.UnfilteredCols
}
relational.Rule.Available |= props.UnfilteredCols
// Derive the UnfilteredCols property now.
// TODO(andyk): Could add other cases, such as outer joins and union.
switch t := in.(type) {
case *memo.ScanExpr:
// All un-limited, unconstrained output columns are unfiltered columns.
if t.HardLimit == 0 && t.Constraint == nil {
relational.Rule.UnfilteredCols = relational.OutputCols
}
case *memo.ProjectExpr:
// Project never filters rows, so it passes through unfiltered columns.
unfilteredCols := c.deriveUnfilteredCols(t.Input)
relational.Rule.UnfilteredCols = unfilteredCols.Intersection(relational.OutputCols)
case *memo.InnerJoinExpr, *memo.InnerJoinApplyExpr:
left := t.Child(0).(memo.RelExpr)
right := t.Child(1).(memo.RelExpr)
on := *t.Child(2).(*memo.FiltersExpr)
// Cross join always preserves left/right rows.
isCrossJoin := on.IsTrue()
// Inner joins may preserve left/right rows, according to
// JoinFiltersMatchAllLeftRows conditions.
if isCrossJoin || c.JoinFiltersMatchAllLeftRows(left, right, on) {
relational.Rule.UnfilteredCols.UnionWith(c.deriveUnfilteredCols(left))
}
if isCrossJoin || c.JoinFiltersMatchAllLeftRows(right, left, on) {
relational.Rule.UnfilteredCols.UnionWith(c.deriveUnfilteredCols(right))
}
}
return relational.Rule.UnfilteredCols
}
// CanExtractJoinEquality returns true if:
// - one of a, b is bound by the left columns;
// - the other is bound by the right columns;
// - a and b are not "bare" variables;
// - a and b contain no correlated subqueries;
// - neither a or b are constants.
//
// Such an equality can be converted to a column equality by pushing down
// expressions as projections.
func (c *CustomFuncs) CanExtractJoinEquality(
a, b opt.ScalarExpr, leftCols, rightCols opt.ColSet,
) bool {
// Disallow simple equality between variables.
if a.Op() == opt.VariableOp && b.Op() == opt.VariableOp {
return false
}
// Recursively compute properties for left and right sides.
var leftProps, rightProps props.Shared
memo.BuildSharedProps(c.mem, a, &leftProps)
memo.BuildSharedProps(c.mem, b, &rightProps)
// Disallow cases when one side has a correlated subquery.
// TODO(radu): investigate relaxing this.
if leftProps.HasCorrelatedSubquery || rightProps.HasCorrelatedSubquery {
return false
}
if (leftProps.OuterCols.SubsetOf(leftCols) && rightProps.OuterCols.SubsetOf(rightCols)) ||
(leftProps.OuterCols.SubsetOf(rightCols) && rightProps.OuterCols.SubsetOf(leftCols)) {
// The equality is of the form:
// expression(leftCols) = expression(rightCols)
return true
}
return false
}
// ExtractJoinEquality takes an equality FiltersItem that was identified via a
// call to CanExtractJoinEquality, and converts it to an equality on "bare"
// variables, by pushing down more complicated expressions as projections. See
// the ExtractJoinEqualities rule.
func (c *CustomFuncs) ExtractJoinEquality(
joinOp opt.Operator,
left, right memo.RelExpr,
filters memo.FiltersExpr,
item *memo.FiltersItem,
private *memo.JoinPrivate,
) memo.RelExpr {
leftCols := c.OutputCols(left)
rightCols := c.OutputCols(right)
eq := item.Condition.(*memo.EqExpr)
a, b := eq.Left, eq.Right
var eqLeftProps props.Shared
memo.BuildSharedProps(c.mem, eq.Left, &eqLeftProps)
if eqLeftProps.OuterCols.SubsetOf(rightCols) {
a, b = b, a
}
var leftProj, rightProj projectBuilder
leftProj.init(c.f)
rightProj.init(c.f)
newFilters := make(memo.FiltersExpr, len(filters))
for i := range filters {
if &filters[i] != item {
newFilters[i] = filters[i]
continue
}
newFilters[i] = memo.FiltersItem{
Condition: c.f.ConstructEq(leftProj.add(a), rightProj.add(b)),
}
}
if leftProj.empty() && rightProj.empty() {
panic(errors.AssertionFailedf("no equalities to extract"))
}
join := c.f.ConstructJoin(
joinOp,
leftProj.buildProject(left, leftCols),
rightProj.buildProject(right, rightCols),
newFilters,
private,
)
// Project away the synthesized columns.
return c.f.ConstructProject(join, memo.EmptyProjectionsExpr, leftCols.Union(rightCols))
}