Skip to content

Commit

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

close #44565
  • Loading branch information
ti-chi-bot authored Jun 25, 2023
1 parent 62e13fa commit a049956
Show file tree
Hide file tree
Showing 2 changed files with 19 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 @@ -147,6 +147,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
11 changes: 11 additions & 0 deletions executor/writetest/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3551,3 +3551,14 @@ 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 := testkit.CreateMockStore(t)
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 a049956

Please sign in to comment.