Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/sql/logictest/testdata/logic_test/lookup_join
Original file line number Diff line number Diff line change
Expand Up @@ -1601,8 +1601,10 @@ SELECT col1_6 FROM table_1_124732 INNER HASH JOIN table_3_124732 ON col3_0 = col
----
-

statement error pgcode XXUUU pq: could not produce a query plan conforming to the LOOKUP JOIN hint
query T
SELECT col1_6 FROM table_1_124732 INNER LOOKUP JOIN table_3_124732 ON col3_0 = col1_6;
----
-

# Regression test for incorrectly remapping columns in a composite-sensitive
# expression to produce a lookup join (#124732).
Expand Down
30 changes: 17 additions & 13 deletions pkg/sql/opt/lookupjoin/constraint_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,6 @@ func (b *ConstraintBuilder) Build(
rightSideCols = append(rightSideCols, rightCol)
}

// rightEqIdenticalTypeCols is the set of columns in rightEq that have
// identical types to the corresponding columns in leftEq. This is used to
// determine if a computed column can be synthesized for a column in the
// index in order to allow a lookup join.
var rightEqIdenticalTypeCols opt.ColSet
for i := range rightEq {
if b.md.ColumnMeta(rightEq[i]).Type.Identical(b.md.ColumnMeta(leftEq[i]).Type) {
rightEqIdenticalTypeCols.Add(rightEq[i])
}
}

// 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++ {
Expand All @@ -280,7 +269,7 @@ func (b *ConstraintBuilder) Build(
//
// NOTE: we must only consider equivalent columns with identical types,
// since column remapping is otherwise not valid.
if expr, ok := b.findComputedColJoinEquality(b.table, idxCol, rightEqIdenticalTypeCols); ok {
if expr, ok := b.findComputedColJoinEquality(b.table, idxCol, rightEq.ToSet()); ok {
colMeta := b.md.ColumnMeta(idxCol)
compEqCol := b.md.AddColumn(fmt.Sprintf("%s_eq", colMeta.Alias), colMeta.Type)

Expand All @@ -293,7 +282,22 @@ func (b *ConstraintBuilder) Build(

// Project the computed column expression, mapping all columns
// in rightEq to corresponding columns in leftEq.
projection := b.f.ConstructProjectionsItem(b.f.RemapCols(expr, b.eqColMap), compEqCol)
var replace norm.ReplaceFunc
replace = func(e opt.Expr) opt.Expr {
if v, ok := e.(*memo.VariableExpr); ok {
if col, ok := b.eqColMap.Get(int(v.Col)); ok {
// If the column is a computed column, we need to
// cast it to the type of the original column so that
// functions which are type sensitive still return the
// expected results.
return b.f.ConstructCast(b.f.ConstructVariable(opt.ColumnID(col)), b.md.ColumnMeta(v.Col).Type)
} else {
return e
}
}
return b.f.Replace(e, replace)
}
projection := b.f.ConstructProjectionsItem(b.f.Replace(expr, replace).(opt.ScalarExpr), compEqCol)
inputProjections = append(inputProjections, projection)
addEqualityColumns(compEqCol, idxCol)
derivedEquivCols.Add(compEqCol)
Expand Down
6 changes: 5 additions & 1 deletion pkg/sql/opt/lookupjoin/testdata/computed
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,11 @@ lookup expression:
lookup-constraints left=(a regclass, b int) right=(x oid, v string not null as (x::string) stored) index=(v, x)
x = a
----
lookup join not possible
key cols:
v = v_eq
x = a
input projections:
v_eq = a::OID::STRING [type=STRING]

# Computed columns cannot be remapped if the expression is composite-sensitive.
lookup-constraints left=(a decimal, b int) right=(x decimal, v int not null as (x::int) stored) index=(v, x)
Expand Down