@@ -245,17 +245,6 @@ func (b *ConstraintBuilder) Build(
245
245
rightSideCols = append (rightSideCols , rightCol )
246
246
}
247
247
248
- // rightEqIdenticalTypeCols is the set of columns in rightEq that have
249
- // identical types to the corresponding columns in leftEq. This is used to
250
- // determine if a computed column can be synthesized for a column in the
251
- // index in order to allow a lookup join.
252
- var rightEqIdenticalTypeCols opt.ColSet
253
- for i := range rightEq {
254
- if b .md .ColumnMeta (rightEq [i ]).Type .Identical (b .md .ColumnMeta (leftEq [i ]).Type ) {
255
- rightEqIdenticalTypeCols .Add (rightEq [i ])
256
- }
257
- }
258
-
259
248
// All the lookup conditions must apply to the prefix of the index and so
260
249
// the projected columns created must be created in order.
261
250
for j := 0 ; j < numIndexKeyCols ; j ++ {
@@ -280,7 +269,7 @@ func (b *ConstraintBuilder) Build(
280
269
//
281
270
// NOTE: we must only consider equivalent columns with identical types,
282
271
// since column remapping is otherwise not valid.
283
- if expr , ok := b .findComputedColJoinEquality (b .table , idxCol , rightEqIdenticalTypeCols ); ok {
272
+ if expr , ok := b .findComputedColJoinEquality (b .table , idxCol , rightEq . ToSet () ); ok {
284
273
colMeta := b .md .ColumnMeta (idxCol )
285
274
compEqCol := b .md .AddColumn (fmt .Sprintf ("%s_eq" , colMeta .Alias ), colMeta .Type )
286
275
@@ -293,7 +282,22 @@ func (b *ConstraintBuilder) Build(
293
282
294
283
// Project the computed column expression, mapping all columns
295
284
// in rightEq to corresponding columns in leftEq.
296
- projection := b .f .ConstructProjectionsItem (b .f .RemapCols (expr , b .eqColMap ), compEqCol )
285
+ var replace norm.ReplaceFunc
286
+ replace = func (e opt.Expr ) opt.Expr {
287
+ if v , ok := e .(* memo.VariableExpr ); ok {
288
+ if col , ok := b .eqColMap .Get (int (v .Col )); ok {
289
+ // If the column is a computed column, we need to
290
+ // cast it to the type of the original column so that
291
+ // functions which are type sensitive still return the
292
+ // expected results.
293
+ return b .f .ConstructCast (b .f .ConstructVariable (opt .ColumnID (col )), b .md .ColumnMeta (v .Col ).Type )
294
+ } else {
295
+ return e
296
+ }
297
+ }
298
+ return b .f .Replace (e , replace )
299
+ }
300
+ projection := b .f .ConstructProjectionsItem (b .f .Replace (expr , replace ).(opt.ScalarExpr ), compEqCol )
297
301
inputProjections = append (inputProjections , projection )
298
302
addEqualityColumns (compEqCol , idxCol )
299
303
derivedEquivCols .Add (compEqCol )
0 commit comments