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: generate wrong plan when update has subquery (#25660) #25698

Merged
merged 2 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions planner/core/point_get_plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -1280,6 +1280,12 @@ func findInPairs(colName string, pairs []nameValuePair) int {
}

func tryUpdatePointPlan(ctx sessionctx.Context, updateStmt *ast.UpdateStmt) Plan {
// avoid using the point_get when assignment_list contains the subquery in the UPDATE.
for _, list := range updateStmt.List {
if _, ok := list.Expr.(*ast.SubqueryExpr); ok {
return nil
}
}
selStmt := &ast.SelectStmt{
Fields: &ast.FieldList{},
From: updateStmt.TableRefs,
Expand Down
13 changes: 13 additions & 0 deletions planner/core/point_get_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,19 @@ func (s *testPointGetSuite) TestPointGetForUpdate(c *C) {
tk.MustExec("rollback")
}

func (s *testPointGetSuite) TestPointGetForUpdateWithSubquery(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("CREATE TABLE users (id bigint(20) unsigned NOT NULL primary key, name longtext DEFAULT NULL, company_id bigint(20) DEFAULT NULL)")
tk.MustExec("create table companies(id bigint primary key, name longtext default null)")
tk.MustExec("insert into companies values(14, 'Company14')")
tk.MustExec("insert into companies values(15, 'Company15')")
tk.MustExec("insert into users(id, company_id, name) values(239, 15, 'xxxx')")
tk.MustExec("UPDATE users SET name=(SELECT name FROM companies WHERE companies.id = users.company_id) WHERE id = 239")

tk.MustQuery("select * from users").Check(testkit.Rows("239 Company15 15"))
}

func checkUseForUpdate(tk *testkit.TestKit, c *C, expectLock bool) {
res := tk.MustQuery("explain format = 'brief' select * from fu where id = 6 for update")
// Point_Get_1 1.00 root table:fu, handle:6
Expand Down