Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: add projections to keep join keys as col=col #52989

Merged
merged 11 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions pkg/planner/core/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2165,6 +2165,24 @@ 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 cast(Column#5, double BINARY)->Column#9`,
`│ └─Projection 0.00 root <nil>->Column#5`,
`│ └─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 @@ -514,13 +514,39 @@ func (s *baseSingleGroupJoinOrderSolver) checkConnection(leftPlan, rightPlan bas
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, 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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is SF means ? Scalar Function ?

rCol, lCol).(*expression.ScalarFunction)
}
usedEdges = append(usedEdges, newSf)
}
}
}
return
}

func (s *baseSingleGroupJoinOrderSolver) injectExpr(p base.LogicalPlan, expr expression.Expression) (base.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 base.LogicalPlan, eqEdges []*expression.ScalarFunction, joinType *joinTypeWithExtMsg) (base.LogicalPlan, []expression.Expression) {
remainOtherConds := make([]expression.Expression, len(s.otherConds))
Expand Down