diff --git a/pkg/planner/core/integration_test.go b/pkg/planner/core/integration_test.go index 66444ef38e88b..453a9b4c0dabc 100644 --- a/pkg/planner/core/integration_test.go +++ b/pkg/planner/core/integration_test.go @@ -2166,6 +2166,23 @@ func TestWindowRangeFramePushDownTiflash(t *testing.T) { " └─TableFullScan_9 10000.00 mpp[tiflash] table:first_range keep order:false, stats:pseudo")) } +func TestIssue46556(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec(`use test`) + tk.MustExec(`CREATE TABLE t0(c0 BLOB);`) + tk.MustExec(`CREATE definer='root'@'localhost' VIEW v0(c0) AS SELECT NULL FROM t0 GROUP BY NULL;`) + tk.MustExec(`SELECT t0.c0 FROM t0 NATURAL JOIN v0 WHERE v0.c0 LIKE v0.c0;`) // no error + tk.MustQuery(`explain format='brief' SELECT t0.c0 FROM t0 NATURAL JOIN v0 WHERE v0.c0 LIKE v0.c0`).Check( + testkit.Rows(`HashJoin 0.00 root inner join, equal:[eq(Column#9, Column#10)]`, + `├─Projection(Build) 0.00 root ->Column#9`, + `│ └─TableDual 0.00 root rows:0`, + `└─Projection(Probe) 9990.00 root test.t0.c0, cast(test.t0.c0, double BINARY)->Column#10`, + ` └─TableReader 9990.00 root data:Selection`, + ` └─Selection 9990.00 cop[tikv] not(isnull(test.t0.c0))`, + ` └─TableFullScan 10000.00 cop[tikv] table:t0 keep order:false, stats:pseudo`)) +} + // https://github.com/pingcap/tidb/issues/41458 func TestIssue41458(t *testing.T) { store := testkit.CreateMockStore(t) diff --git a/pkg/planner/core/rule_join_reorder.go b/pkg/planner/core/rule_join_reorder.go index 6acdb08a21381..f37b734d19c9f 100644 --- a/pkg/planner/core/rule_join_reorder.go +++ b/pkg/planner/core/rule_join_reorder.go @@ -513,6 +513,22 @@ func (s *baseSingleGroupJoinOrderSolver) checkConnection(leftPlan, rightPlan Log usedEdges = append(usedEdges, edge) } else { newSf := expression.NewFunctionInternal(s.ctx.GetExprCtx(), ast.EQ, edge.GetType(), rCol, lCol).(*expression.ScalarFunction) + + // after creating the new EQ function, the 2 args might not be column anymore, for example `sf=sf(cast(col))`, + // which breaks the assumption that join eq keys must be `col=col`, to handle this, inject 2 projections. + _, isCol0 := newSf.GetArgs()[0].(*expression.Column) + _, isCol1 := newSf.GetArgs()[1].(*expression.Column) + if !isCol0 || !isCol1 { + if !isCol0 { + leftPlan, rCol = s.injectExpr(leftPlan, newSf.GetArgs()[0]) + } + if !isCol1 { + rightPlan, lCol = s.injectExpr(rightPlan, newSf.GetArgs()[1]) + } + leftNode, rightNode = leftPlan, rightPlan + newSf = expression.NewFunctionInternal(s.ctx.GetExprCtx(), ast.EQ, edge.GetType(), + rCol, lCol).(*expression.ScalarFunction) + } usedEdges = append(usedEdges, newSf) } } @@ -520,6 +536,16 @@ func (s *baseSingleGroupJoinOrderSolver) checkConnection(leftPlan, rightPlan Log return } +func (*baseSingleGroupJoinOrderSolver) injectExpr(p LogicalPlan, expr expression.Expression) (LogicalPlan, *expression.Column) { + proj, ok := p.(*LogicalProjection) + if !ok { + proj = LogicalProjection{Exprs: cols2Exprs(p.Schema().Columns)}.Init(p.SCtx(), p.QueryBlockOffset()) + proj.SetSchema(p.Schema().Clone()) + proj.SetChildren(p) + } + return proj, proj.appendExpr(expr) +} + // makeJoin build join tree for the nodes which have equal conditions to connect them. func (s *baseSingleGroupJoinOrderSolver) makeJoin(leftPlan, rightPlan LogicalPlan, eqEdges []*expression.ScalarFunction, joinType *joinTypeWithExtMsg) (LogicalPlan, []expression.Expression) { remainOtherConds := make([]expression.Expression, len(s.otherConds))