Skip to content

Commit

Permalink
planner: fix unexpected behavior of UPDATE (pingcap#12597)
Browse files Browse the repository at this point in the history
  • Loading branch information
winoros committed Nov 4, 2019
1 parent ae6b36c commit 8c9b3d5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
11 changes: 11 additions & 0 deletions executor/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,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"))
}
14 changes: 7 additions & 7 deletions planner/core/logical_plan_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2263,15 +2263,16 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) (Plan, error) {
b.visitInfo = appendVisitInfo(b.visitInfo, mysql.SelectPriv, dbName, t.Name.L, "")
}

oldSchemaLen := p.Schema().Len()
oldSchema := p.Schema().Clone()
if sel.Where != nil {
p, err = b.buildSelection(p, sel.Where, nil)
if err != nil {
return nil, errors.Trace(err)
}
}
if update.Order != nil {
p, err = b.buildSort(p, update.Order.Items, nil)

if sel.OrderBy != nil {
p, err = b.buildSort(p, sel.OrderBy.Items, nil)
if err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -2283,10 +2284,9 @@ func (b *planBuilder) buildUpdate(update *ast.UpdateStmt) (Plan, error) {
}
}
// 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
2 changes: 1 addition & 1 deletion planner/core/logical_plan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,7 @@ func (s *testPlanSuite) TestPlanBuilder(c *C) {
},
{
sql: "update t set a = 2 where b in (select c from t)",
plan: "LeftHashJoin{TableReader(Table(t))->IndexReader(Index(t.c_d_e)[[NULL,+inf]])->HashAgg}(Column#2,Column#25)->Projection->Update",
plan: "LeftHashJoin{TableReader(Table(t))->TableReader(Table(t))}(test.t.b,test.t.c)->Update",
},
}
for _, ca := range tests {
Expand Down

0 comments on commit 8c9b3d5

Please sign in to comment.