Skip to content

Commit

Permalink
planner: add projections to keep join keys as col=col (#52989) (#53326
Browse files Browse the repository at this point in the history
)

close #46556
  • Loading branch information
ti-chi-bot committed May 16, 2024
1 parent 27b6278 commit 607e297
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 <nil>->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)
Expand Down
26 changes: 26 additions & 0 deletions pkg/planner/core/rule_join_reorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,39 @@ 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)
}
}
}
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))
Expand Down

0 comments on commit 607e297

Please sign in to comment.