-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
join_funcs.go
582 lines (525 loc) · 19.8 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
// 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/catalog/colinfo"
"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/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)))
}
// 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))
}
// 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.
// See canMapJoinOpEquivalenceGroup for details.
func (c *CustomFuncs) CanMapJoinOpEqualities(
filters memo.FiltersExpr, leftCols, rightCols opt.ColSet,
) bool {
var equivFD props.FuncDepSet
for i := range filters {
equivFD.AddEquivFrom(&filters[i].ScalarProps().FuncDeps)
}
equivReps := equivFD.EquivReps()
for col, ok := equivReps.Next(0); ok; col, ok = equivReps.Next(col + 1) {
if c.canMapJoinOpEquivalenceGroup(filters, col, leftCols, rightCols, equivFD) {
return true
}
}
return false
}
// canMapJoinOpEquivalenceGroup checks whether it is possible to map equality
// conditions in a join that form an equivalence group 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 the provided FuncDepSet, equivFD, which should contain
// the equivalence dependencies from the 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, canMapJoinOpEquivalenceGroup returns true.
func (c *CustomFuncs) canMapJoinOpEquivalenceGroup(
filters memo.FiltersExpr,
col opt.ColumnID,
leftCols, rightCols opt.ColSet,
equivFD props.FuncDepSet,
) bool {
eqCols := c.GetEquivColsWithEquivType(col, equivFD, false /* allowCompositeEncoding */)
// 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 := 0
for i := range filters {
fd := &filters[i].ScalarProps().FuncDeps
filterEqCols := fd.ComputeEquivClosure(fd.EquivReps())
if filterEqCols.Intersects(leftCols) && filterEqCols.Intersects(rightCols) &&
filterEqCols.SubsetOf(eqCols) {
found++
if found > 1 {
return true
}
}
}
return false
}
// MapJoinOpEqualities maps all variable equality conditions in filters to
// use columns in either leftCols or rightCols where possible. See
// canMapJoinOpEquivalenceGroup and mapJoinOpEquivalenceGroup for more info.
func (c *CustomFuncs) MapJoinOpEqualities(
filters memo.FiltersExpr, leftCols, rightCols opt.ColSet,
) memo.FiltersExpr {
var equivFD props.FuncDepSet
for i := range filters {
equivFD.AddEquivFrom(&filters[i].ScalarProps().FuncDeps)
}
equivReps := equivFD.EquivReps()
newFilters := filters
equivReps.ForEach(func(col opt.ColumnID) {
if c.canMapJoinOpEquivalenceGroup(newFilters, col, leftCols, rightCols, equivFD) {
newFilters = c.mapJoinOpEquivalenceGroup(newFilters, col, leftCols, rightCols, equivFD)
}
})
return newFilters
}
// mapJoinOpEquivalenceGroup maps equality conditions in a join that form an
// equivalence group 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, mapJoinOpEquivalenceGroup finds the set of columns
// containing col that forms an equivalence group in filters. The result is
// a set of columns that are all equivalent, some on the left side of the join
// and some on the right side. mapJoinOpEquivalenceGroup constructs a new set of
// equalities that implies the same equivalency group, with the property that
// there is a single condition with one left column and one right column.
// For example, consider this query:
//
// SELECT * FROM a, b WHERE a.x = b.x AND a.x = a.y AND a.y = b.y
//
// It has an equivalence group {a.x, a.y, b.x, b.y}. The columns a.x and a.y
// are on the left side, and b.x and b.y are on the right side. Initially there
// are two conditions that cross both sides. After mapping, the query would be
// converted to:
//
// SELECT * FROM a, b WHERE a.x = a.y AND b.x = b.y AND a.x = b.x
//
func (c *CustomFuncs) mapJoinOpEquivalenceGroup(
filters memo.FiltersExpr,
col opt.ColumnID,
leftCols, rightCols opt.ColSet,
equivFD props.FuncDepSet,
) memo.FiltersExpr {
eqCols := c.GetEquivColsWithEquivType(col, equivFD, false /* allowCompositeEncoding */)
// 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().FuncDeps
filterEqCols := fd.ComputeEquivClosure(fd.EquivReps())
if !filterEqCols.Empty() && filterEqCols.SubsetOf(eqCols) {
continue
}
newFilters = append(newFilters, filters[i])
}
// Now append new equality conditions that imply the same equivalency group,
// but only one condition should contain columns from both sides.
leftEqCols := leftCols.Intersection(eqCols)
rightEqCols := rightCols.Intersection(eqCols)
firstLeftCol, ok := leftEqCols.Next(0)
if !ok {
panic(errors.AssertionFailedf(
"mapJoinOpEquivalenceGroup called with equivalence group that does not intersect both sides",
))
}
firstRightCol, ok := rightEqCols.Next(0)
if !ok {
panic(errors.AssertionFailedf(
"mapJoinOpEquivalenceGroup called with equivalence group that does not intersect both sides",
))
}
// Connect all the columns on the left.
for col, ok := leftEqCols.Next(firstLeftCol + 1); ok; col, ok = leftEqCols.Next(col + 1) {
newFilters = append(newFilters, c.f.ConstructFiltersItem(
c.f.ConstructEq(c.f.ConstructVariable(firstLeftCol), c.f.ConstructVariable(col)),
))
}
// Connect all the columns on the right.
for col, ok := rightEqCols.Next(firstRightCol + 1); ok; col, ok = rightEqCols.Next(col + 1) {
newFilters = append(newFilters, c.f.ConstructFiltersItem(
c.f.ConstructEq(c.f.ConstructVariable(firstRightCol), c.f.ConstructVariable(col)),
))
}
// Connect the two sides.
newFilters = append(newFilters, c.f.ConstructFiltersItem(
c.f.ConstructEq(
c.f.ConstructVariable(firstLeftCol), c.f.ConstructVariable(firstRightCol),
),
))
return newFilters
}
// 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(
src *memo.FiltersItem, dstCols opt.ColSet, equivFD props.FuncDepSet,
) bool {
// Fast path if src is already bound by dst.
if c.IsBoundBy(src, dstCols) {
return true
}
scalarProps := src.ScalarProps()
if scalarProps.HasCorrelatedSubquery {
return false
}
allowCompositeEncoding := !memo.CanBeCompositeSensitive(c.mem.Metadata(), src)
// 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, equivFD, allowCompositeEncoding)
if !eqCols.Intersects(dstCols) {
return false
}
}
return true
}
// 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(
src *memo.FiltersItem, dstCols opt.ColSet, equivFD props.FuncDepSet,
) opt.ScalarExpr {
// Fast path if src is already bound by dst.
if c.IsBoundBy(src, dstCols) {
return src.Condition
}
allowCompositeEncoding := !memo.CanBeCompositeSensitive(c.mem.Metadata(), src)
// 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().OuterCols
for srcCol, ok := outerCols.Next(0); ok; srcCol, ok = outerCols.Next(srcCol + 1) {
eqCols := c.GetEquivColsWithEquivType(srcCol, equivFD, allowCompositeEncoding)
eqCols.IntersectionWith(dstCols)
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, dstCols,
))
}
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)
}
// 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, equivFD props.FuncDepSet, allowCompositeEncoding bool,
) 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 !allowCompositeEncoding && colinfo.HasCompositeKeyEncoding(colType) {
res.Add(col)
return res
}
// Compute all equivalent columns.
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
}
// GetEquivFD gets a FuncDepSet with all equivalence dependencies from
// filters, left and right.
func (c *CustomFuncs) GetEquivFD(
filters memo.FiltersExpr, left, right memo.RelExpr,
) (equivFD props.FuncDepSet) {
for i := range filters {
equivFD.AddEquivFrom(&filters[i].ScalarProps().FuncDeps)
}
equivFD.AddEquivFrom(&left.Relational().FuncDeps)
equivFD.AddEquivFrom(&right.Relational().FuncDeps)
return equivFD
}
// 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.
func (c *CustomFuncs) JoinFiltersMatchAllLeftRows(
left, right memo.RelExpr, on memo.FiltersExpr,
) bool {
multiplicity := memo.DeriveJoinMultiplicityFromInputs(left, right, on)
return multiplicity.JoinFiltersMatchAllLeftRows()
}
// 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(a, &leftProps)
memo.BuildSharedProps(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.Empty() || rightProps.OuterCols.Empty() {
// It's possible for one side to have no outer cols and still not be a
// ConstValue (see #44746).
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(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] = c.f.ConstructFiltersItem(
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))
}
// CommuteJoinFlags returns a join private for the commuted join (where the left
// and right sides are swapped). It adjusts any join flags that are specific to
// one side.
func (c *CustomFuncs) CommuteJoinFlags(p *memo.JoinPrivate) *memo.JoinPrivate {
if p.Flags.Empty() {
return p
}
// swap is a helper function which swaps the values of two (single-bit) flags.
swap := func(f, a, b memo.JoinFlags) memo.JoinFlags {
// If the bits are different, flip them both.
if f.Has(a) != f.Has(b) {
f ^= (a | b)
}
return f
}
f := p.Flags
f = swap(f, memo.DisallowLookupJoinIntoLeft, memo.DisallowLookupJoinIntoRight)
f = swap(f, memo.DisallowHashJoinStoreLeft, memo.DisallowHashJoinStoreRight)
f = swap(f, memo.PreferLookupJoinIntoLeft, memo.PreferLookupJoinIntoRight)
if p.Flags == f {
return p
}
res := *p
res.Flags = f
return &res
}