Skip to content

Commit

Permalink
Executor: fix affected rows count when update (pingcap#6656)
Browse files Browse the repository at this point in the history
  • Loading branch information
spongedu authored and coocood committed May 28, 2018
1 parent d3a69d8 commit eaf8296
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
5 changes: 4 additions & 1 deletion executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,10 @@ func updateRecord(ctx sessionctx.Context, h int64, oldData, newData []types.Datu
if onDup {
sc.AddAffectedRows(2)
} else {
sc.AddAffectedRows(1)
// if handleChanged == true, the `affectedRows` is calculated when add new record.
if !handleChanged {
sc.AddAffectedRows(1)
}
}
colSize := make(map[int64]int64)
for id, col := range t.Cols() {
Expand Down
17 changes: 17 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,3 +1611,20 @@ func (s *testSuite) TestUpdateSelect(c *C) {
tk.MustExec("UPDATE msg SET msg.status = (SELECT detail.status FROM detail WHERE msg.id = detail.id)")
tk.MustExec("admin check table msg")
}

func (s *testSuite) TestUpdateAffectRowCnt(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("create table a(id int auto_increment, a int default null, primary key(id))")
tk.MustExec("insert into a values (1, 1001), (2, 1001), (10001, 1), (3, 1)")
tk.MustExec("update a set id = id*10 where a = 1001")
ctx := tk.Se.(sessionctx.Context)
c.Assert(ctx.GetSessionVars().StmtCtx.AffectedRows(), Equals, uint64(2))

tk.MustExec("drop table a")
tk.MustExec("create table a ( a bigint, b bigint)")
tk.MustExec("insert into a values (1, 1001), (2, 1001), (10001, 1), (3, 1)")
tk.MustExec("update a set a = a*10 where b = 1001")
ctx = tk.Se.(sessionctx.Context)
c.Assert(ctx.GetSessionVars().StmtCtx.AffectedRows(), Equals, uint64(2))
}

0 comments on commit eaf8296

Please sign in to comment.