@@ -513,13 +513,39 @@ func (s *baseSingleGroupJoinOrderSolver) checkConnection(leftPlan, rightPlan Log
513
513
usedEdges = append (usedEdges , edge )
514
514
} else {
515
515
newSf := expression .NewFunctionInternal (s .ctx .GetExprCtx (), ast .EQ , edge .GetType (), rCol , lCol ).(* expression.ScalarFunction )
516
+
517
+ // after creating the new EQ function, the 2 args might not be column anymore, for example `sf=sf(cast(col))`,
518
+ // which breaks the assumption that join eq keys must be `col=col`, to handle this, inject 2 projections.
519
+ _ , isCol0 := newSf .GetArgs ()[0 ].(* expression.Column )
520
+ _ , isCol1 := newSf .GetArgs ()[1 ].(* expression.Column )
521
+ if ! isCol0 || ! isCol1 {
522
+ if ! isCol0 {
523
+ leftPlan , rCol = s .injectExpr (leftPlan , newSf .GetArgs ()[0 ])
524
+ }
525
+ if ! isCol1 {
526
+ rightPlan , lCol = s .injectExpr (rightPlan , newSf .GetArgs ()[1 ])
527
+ }
528
+ leftNode , rightNode = leftPlan , rightPlan
529
+ newSf = expression .NewFunctionInternal (s .ctx .GetExprCtx (), ast .EQ , edge .GetType (),
530
+ rCol , lCol ).(* expression.ScalarFunction )
531
+ }
516
532
usedEdges = append (usedEdges , newSf )
517
533
}
518
534
}
519
535
}
520
536
return
521
537
}
522
538
539
+ func (* baseSingleGroupJoinOrderSolver ) injectExpr (p LogicalPlan , expr expression.Expression ) (LogicalPlan , * expression.Column ) {
540
+ proj , ok := p .(* LogicalProjection )
541
+ if ! ok {
542
+ proj = LogicalProjection {Exprs : cols2Exprs (p .Schema ().Columns )}.Init (p .SCtx (), p .QueryBlockOffset ())
543
+ proj .SetSchema (p .Schema ().Clone ())
544
+ proj .SetChildren (p )
545
+ }
546
+ return proj , proj .appendExpr (expr )
547
+ }
548
+
523
549
// makeJoin build join tree for the nodes which have equal conditions to connect them.
524
550
func (s * baseSingleGroupJoinOrderSolver ) makeJoin (leftPlan , rightPlan LogicalPlan , eqEdges []* expression.ScalarFunction , joinType * joinTypeWithExtMsg ) (LogicalPlan , []expression.Expression ) {
525
551
remainOtherConds := make ([]expression.Expression , len (s .otherConds ))
0 commit comments