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: fix unexpected behavior of UPDATE #12597

Merged
merged 2 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions executor/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,14 @@ func (s *testUpdateSuite) TestUpdateWithAutoidSchema(c *C) {
tk.MustQuery(tt.query).Check(tt.result)
}
}

func (s *testUpdateSuite) TestUpdateWithSubquery(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table t1(id varchar(30) not null, status varchar(1) not null default 'N', id2 varchar(30))")
tk.MustExec("create table t2(id varchar(30) not null, field varchar(4) not null)")
tk.MustExec("insert into t1 values('abc', 'F', 'abc')")
tk.MustExec("insert into t2 values('abc', 'MAIN')")
tk.MustExec("update t1 set status = 'N' where status = 'F' and (id in (select id from t2 where field = 'MAIN') or id2 in (select id from t2 where field = 'main'))")
tk.MustQuery("select * from t1").Check(testkit.Rows("abc N abc"))
}
9 changes: 4 additions & 5 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2584,18 +2584,17 @@ func (b *PlanBuilder) buildUpdate(ctx context.Context, update *ast.UpdateStmt) (
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, dbName, t.Name.L, "", nil)
}

oldSchemaLen := p.Schema().Len()
oldSchema := p.Schema().Clone()
if sel.Where != nil {
p, err = b.buildSelection(ctx, p, update.Where, nil)
if err != nil {
return nil, err
}
}
// TODO: expression rewriter should not change the output columns. We should cut the columns here.
if p.Schema().Len() != oldSchemaLen {
proj := LogicalProjection{Exprs: expression.Column2Exprs(p.Schema().Columns[:oldSchemaLen])}.Init(b.ctx)
proj.SetSchema(expression.NewSchema(make([]*expression.Column, oldSchemaLen)...))
copy(proj.schema.Columns, p.Schema().Columns[:oldSchemaLen])
if p.Schema().Len() != oldSchema.Len() {
proj := LogicalProjection{Exprs: expression.Column2Exprs(oldSchema.Columns)}.Init(b.ctx)
proj.SetSchema(oldSchema)
proj.SetChildren(p)
p = proj
}
Expand Down