Skip to content

Commit 7129f86

Browse files
qw4990ti-chi-bot
authored andcommitted
This is an automated cherry-pick of pingcap#52989
Signed-off-by: ti-chi-bot <[email protected]>
1 parent 00a460e commit 7129f86

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

pkg/planner/core/integration_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,6 +2594,23 @@ func TestIssue46177(t *testing.T) {
25942594
` └─TableRangeScan 1.00 cop[tikv] table:sbtest range:[0,1), keep order:false, stats:pseudo`))
25952595
}
25962596

2597+
func TestIssue46556(t *testing.T) {
2598+
store := testkit.CreateMockStore(t)
2599+
tk := testkit.NewTestKit(t, store)
2600+
tk.MustExec(`use test`)
2601+
tk.MustExec(`CREATE TABLE t0(c0 BLOB);`)
2602+
tk.MustExec(`CREATE definer='root'@'localhost' VIEW v0(c0) AS SELECT NULL FROM t0 GROUP BY NULL;`)
2603+
tk.MustExec(`SELECT t0.c0 FROM t0 NATURAL JOIN v0 WHERE v0.c0 LIKE v0.c0;`) // no error
2604+
tk.MustQuery(`explain format='brief' SELECT t0.c0 FROM t0 NATURAL JOIN v0 WHERE v0.c0 LIKE v0.c0`).Check(
2605+
testkit.Rows(`HashJoin 0.00 root inner join, equal:[eq(Column#9, Column#10)]`,
2606+
`├─Projection(Build) 0.00 root <nil>->Column#9`,
2607+
`│ └─TableDual 0.00 root rows:0`,
2608+
`└─Projection(Probe) 9990.00 root test.t0.c0, cast(test.t0.c0, double BINARY)->Column#10`,
2609+
` └─TableReader 9990.00 root data:Selection`,
2610+
` └─Selection 9990.00 cop[tikv] not(isnull(test.t0.c0))`,
2611+
` └─TableFullScan 10000.00 cop[tikv] table:t0 keep order:false, stats:pseudo`))
2612+
}
2613+
25972614
// https://github.com/pingcap/tidb/issues/41458
25982615
func TestIssue41458(t *testing.T) {
25992616
store := testkit.CreateMockStore(t)

pkg/planner/core/rule_join_reorder.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,14 +509,44 @@ func (s *baseSingleGroupJoinOrderSolver) checkConnection(leftPlan, rightPlan Log
509509
rightNode, leftNode = leftPlan, rightPlan
510510
usedEdges = append(usedEdges, edge)
511511
} else {
512+
<<<<<<< HEAD
512513
newSf := expression.NewFunctionInternal(s.ctx, ast.EQ, edge.GetType(), rCol, lCol).(*expression.ScalarFunction)
514+
=======
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+
}
532+
>>>>>>> 06ee59bd9c6 (planner: add projections to keep join keys as `col=col` (#52989))
513533
usedEdges = append(usedEdges, newSf)
514534
}
515535
}
516536
}
517537
return
518538
}
519539

540+
func (*baseSingleGroupJoinOrderSolver) injectExpr(p base.LogicalPlan, expr expression.Expression) (base.LogicalPlan, *expression.Column) {
541+
proj, ok := p.(*LogicalProjection)
542+
if !ok {
543+
proj = LogicalProjection{Exprs: cols2Exprs(p.Schema().Columns)}.Init(p.SCtx(), p.QueryBlockOffset())
544+
proj.SetSchema(p.Schema().Clone())
545+
proj.SetChildren(p)
546+
}
547+
return proj, proj.appendExpr(expr)
548+
}
549+
520550
// makeJoin build join tree for the nodes which have equal conditions to connect them.
521551
func (s *baseSingleGroupJoinOrderSolver) makeJoin(leftPlan, rightPlan LogicalPlan, eqEdges []*expression.ScalarFunction, joinType *joinTypeWithExtMsg) (LogicalPlan, []expression.Expression) {
522552
remainOtherConds := make([]expression.Expression, len(s.otherConds))

0 commit comments

Comments
 (0)