Skip to content

Commit

Permalink
xform: do not propagate join hints to GenerateLookupJoins cross joins
Browse files Browse the repository at this point in the history
This commit fixes a bug that prevented a `LOOKUP` join hint from
producing a plan with a lookup join. Previously, the hint was propagated
to the synthesized cross join created as input to the lookup join. This
artificially inflated the cost of the cross join, making the lookup join
too costly to be selected as the optimal plan.

Release note: None
  • Loading branch information
mgartner committed Dec 10, 2020
1 parent 1a3fd72 commit 4f82ebc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
7 changes: 6 additions & 1 deletion pkg/sql/opt/xform/join_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,12 @@ func (c *CustomFuncs) GenerateLookupJoins(
ID: md.NextUniqueID(),
},
)
lookupJoin.Input = c.e.f.ConstructInnerJoin(lookupJoin.Input, values, nil /* on */, joinPrivate)

// We purposefully do not propagate the join flags from 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.
lookupJoin.Input = c.e.f.ConstructInnerJoin(lookupJoin.Input, values, nil /* on */, &memo.JoinPrivate{})

lookupJoin.KeyCols = append(lookupJoin.KeyCols, constColID)
rightSideCols = append(rightSideCols, idxCol)
Expand Down
31 changes: 31 additions & 0 deletions pkg/sql/opt/xform/testdata/rules/join
Original file line number Diff line number Diff line change
Expand Up @@ -3013,6 +3013,37 @@ inner-join (lookup abcde)
│ └── filters (true)
└── filters (true)

# Test that a LOOKUP join hint does not propagate to the cross join. If it was,
# the cross join would have an artificially high cost and the lookup join would
# not be selected as the optimal plan.
opt expect=GenerateLookupJoinsWithFilter
SELECT * FROM small INNER LOOKUP JOIN abcde ON a=m AND b IN (10, 20, 30)
----
inner-join (lookup abcde)
├── columns: m:1!null n:2 a:5!null b:6!null c:7 d:8 e:9
├── key columns: [10] = [10]
├── lookup columns are key
├── fd: (1)==(5), (5)==(1)
├── inner-join (lookup abcde@secondary)
│ ├── columns: m:1!null n:2 a:5!null b:6!null c:7 abcde.rowid:10!null
│ ├── flags: force lookup join (into right side)
│ ├── key columns: [1 12] = [5 6]
│ ├── fd: (10)-->(5-7), (1)==(5), (5)==(1)
│ ├── inner-join (cross)
│ │ ├── columns: m:1 n:2 "lookup_join_const_col_@6":12!null
│ │ ├── multiplicity: left-rows(one-or-more), right-rows(zero-or-more)
│ │ ├── scan small
│ │ │ └── columns: m:1 n:2
│ │ ├── values
│ │ │ ├── columns: "lookup_join_const_col_@6":12!null
│ │ │ ├── cardinality: [3 - 3]
│ │ │ ├── (10,)
│ │ │ ├── (20,)
│ │ │ └── (30,)
│ │ └── filters (true)
│ └── filters (true)
└── filters (true)

# One column constrained to multiple constants and another constrained to a
# single constant used by lookup joiner.
opt expect=GenerateLookupJoinsWithFilter
Expand Down

0 comments on commit 4f82ebc

Please sign in to comment.