Skip to content

Commit 607e297

Browse files
authored
planner: add projections to keep join keys as col=col (pingcap#52989) (pingcap#53326)
close pingcap#46556
1 parent 27b6278 commit 607e297

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

pkg/planner/core/integration_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,6 +2166,23 @@ func TestWindowRangeFramePushDownTiflash(t *testing.T) {
21662166
" └─TableFullScan_9 10000.00 mpp[tiflash] table:first_range keep order:false, stats:pseudo"))
21672167
}
21682168

2169+
func TestIssue46556(t *testing.T) {
2170+
store := testkit.CreateMockStore(t)
2171+
tk := testkit.NewTestKit(t, store)
2172+
tk.MustExec(`use test`)
2173+
tk.MustExec(`CREATE TABLE t0(c0 BLOB);`)
2174+
tk.MustExec(`CREATE definer='root'@'localhost' VIEW v0(c0) AS SELECT NULL FROM t0 GROUP BY NULL;`)
2175+
tk.MustExec(`SELECT t0.c0 FROM t0 NATURAL JOIN v0 WHERE v0.c0 LIKE v0.c0;`) // no error
2176+
tk.MustQuery(`explain format='brief' SELECT t0.c0 FROM t0 NATURAL JOIN v0 WHERE v0.c0 LIKE v0.c0`).Check(
2177+
testkit.Rows(`HashJoin 0.00 root inner join, equal:[eq(Column#9, Column#10)]`,
2178+
`├─Projection(Build) 0.00 root <nil>->Column#9`,
2179+
`│ └─TableDual 0.00 root rows:0`,
2180+
`└─Projection(Probe) 9990.00 root test.t0.c0, cast(test.t0.c0, double BINARY)->Column#10`,
2181+
` └─TableReader 9990.00 root data:Selection`,
2182+
` └─Selection 9990.00 cop[tikv] not(isnull(test.t0.c0))`,
2183+
` └─TableFullScan 10000.00 cop[tikv] table:t0 keep order:false, stats:pseudo`))
2184+
}
2185+
21692186
// https://github.com/pingcap/tidb/issues/41458
21702187
func TestIssue41458(t *testing.T) {
21712188
store := testkit.CreateMockStore(t)

pkg/planner/core/rule_join_reorder.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,13 +513,39 @@ func (s *baseSingleGroupJoinOrderSolver) checkConnection(leftPlan, rightPlan Log
513513
usedEdges = append(usedEdges, edge)
514514
} else {
515515
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+
}
516532
usedEdges = append(usedEdges, newSf)
517533
}
518534
}
519535
}
520536
return
521537
}
522538

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+
523549
// makeJoin build join tree for the nodes which have equal conditions to connect them.
524550
func (s *baseSingleGroupJoinOrderSolver) makeJoin(leftPlan, rightPlan LogicalPlan, eqEdges []*expression.ScalarFunction, joinType *joinTypeWithExtMsg) (LogicalPlan, []expression.Expression) {
525551
remainOtherConds := make([]expression.Expression, len(s.otherConds))

0 commit comments

Comments
 (0)