@@ -260,17 +260,6 @@ func (b *ConstraintBuilder) Build(
260
260
keyCols = nil
261
261
}
262
262
263
- // rightEqIdenticalTypeCols is the set of columns in rightEq that have
264
- // identical types to the corresponding columns in leftEq. This is used to
265
- // determine if a computed column can be synthesized for a column in the
266
- // index in order to allow a lookup join.
267
- var rightEqIdenticalTypeCols opt.ColSet
268
- for i := range rightEq {
269
- if b .md .ColumnMeta (rightEq [i ]).Type .Identical (b .md .ColumnMeta (leftEq [i ]).Type ) {
270
- rightEqIdenticalTypeCols .Add (rightEq [i ])
271
- }
272
- }
273
-
274
263
// All the lookup conditions must apply to the prefix of the index and so
275
264
// the projected columns created must be created in order.
276
265
for j := 0 ; j < numIndexKeyCols ; j ++ {
@@ -294,7 +283,7 @@ func (b *ConstraintBuilder) Build(
294
283
//
295
284
// NOTE: we must only consider equivalent columns with identical types,
296
285
// since column remapping is otherwise not valid.
297
- if expr , ok := b .findComputedColJoinEquality (b .table , idxCol , rightEqIdenticalTypeCols ); ok {
286
+ if expr , ok := b .findComputedColJoinEquality (b .table , idxCol , rightEq . ToSet () ); ok {
298
287
colMeta := b .md .ColumnMeta (idxCol )
299
288
compEqCol := b .md .AddColumn (fmt .Sprintf ("%s_eq" , colMeta .Alias ), colMeta .Type )
300
289
@@ -307,7 +296,22 @@ func (b *ConstraintBuilder) Build(
307
296
308
297
// Project the computed column expression, mapping all columns
309
298
// in rightEq to corresponding columns in leftEq.
310
- projection := b .f .ConstructProjectionsItem (b .f .RemapCols (expr , b .eqColMap ), compEqCol )
299
+ var replace norm.ReplaceFunc
300
+ replace = func (e opt.Expr ) opt.Expr {
301
+ if v , ok := e .(* memo.VariableExpr ); ok {
302
+ if col , ok := b .eqColMap .Get (int (v .Col )); ok {
303
+ // If the column is a computed column, we need to
304
+ // cast it to the type of the original column so that
305
+ // functions which are type sensitive still return the
306
+ // expected results.
307
+ return b .f .ConstructCast (b .f .ConstructVariable (opt .ColumnID (col )), b .md .ColumnMeta (v .Col ).Type )
308
+ } else {
309
+ return e
310
+ }
311
+ }
312
+ return b .f .Replace (e , replace )
313
+ }
314
+ projection := b .f .ConstructProjectionsItem (b .f .Replace (expr , replace ).(opt.ScalarExpr ), compEqCol )
311
315
inputProjections = append (inputProjections , projection )
312
316
addEqualityColumns (compEqCol , idxCol )
313
317
derivedEquivCols .Add (compEqCol )
0 commit comments