Skip to content

Commit

Permalink
executor: set handle changed when col gets auto-updated (#44566) (#44568
Browse files Browse the repository at this point in the history
)

close #44565
  • Loading branch information
ti-chi-bot authored Jun 30, 2023
1 parent b75040a commit 15f9d17
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions executor/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,14 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old
if v, err := expression.GetTimeValue(sctx, strings.ToUpper(ast.CurrentTimestamp), col.GetType(), col.GetDecimal(), nil); err == nil {
newData[i] = v
modified[i] = true
// Only TIMESTAMP and DATETIME columns can be automatically updated, so it cannot be PKIsHandle.
// Ref: https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html
if col.IsPKHandleColumn(t.Meta()) {
return false, errors.Errorf("on-update-now column should never be pk-is-handle")
}
if col.IsCommonHandleColumn(t.Meta()) {
handleChanged = true
}
} else {
return false, err
}
Expand Down
12 changes: 12 additions & 0 deletions executor/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4397,3 +4397,15 @@ func TestMutipleReplaceAndInsertInOneSession(t *testing.T) {

tk2.MustQuery("select * from t_securities").Sort().Check(testkit.Rows("1 1 2 7", "2 7 1 7", "3 8 1 7", "8 9 1 7"))
}

func TestHandleColumnWithOnUpdateCurrentTimestamp(t *testing.T) {
// Test https://github.com/pingcap/tidb/issues/44565
store, clean := testkit.CreateMockStore(t)
defer clean()
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t (a timestamp on update current_timestamp(0), b int, primary key (a) clustered, key idx (a))")
tk.MustExec("insert into t values ('2023-06-11 10:00:00', 1)")
tk.MustExec("update t force index(primary) set b = 10 where a = '2023-06-11 10:00:00'")
tk.MustExec("admin check table t")
}

0 comments on commit 15f9d17

Please sign in to comment.