From a0499567db621342aef783447bd23274650c1ef9 Mon Sep 17 00:00:00 2001 From: Ti Chi Robot Date: Sun, 25 Jun 2023 11:50:44 +0800 Subject: [PATCH] executor: set handle changed when col gets auto-updated (#44566) (#44570) close pingcap/tidb#44565 --- executor/write.go | 8 ++++++++ executor/writetest/write_test.go | 11 +++++++++++ 2 files changed, 19 insertions(+) diff --git a/executor/write.go b/executor/write.go index 2ff925194c96f..82d5cd25f7a4d 100644 --- a/executor/write.go +++ b/executor/write.go @@ -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 } diff --git a/executor/writetest/write_test.go b/executor/writetest/write_test.go index 7c8b78ddaffc6..2fbcedab334a8 100644 --- a/executor/writetest/write_test.go +++ b/executor/writetest/write_test.go @@ -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") +}