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