Skip to content

Commit

Permalink
planner: generate wrong plan when update has subquery (pingcap#25660) (
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Jul 7, 2021
1 parent e6c94d9 commit e96c2a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
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

0 comments on commit e96c2a6

Please sign in to comment.