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 authored and zz-jason committed Oct 10, 2019
1 parent 8a93d6b commit 32bd4a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
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

0 comments on commit 32bd4a7

Please sign in to comment.