Skip to content

Commit 897c4c4

Browse files
authored
executor: set handle changed when col gets auto-updated (#44566) (#44569)
close #44565
1 parent 43cf5ea commit 897c4c4

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

executor/write.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ func updateRecord(ctx context.Context, sctx sessionctx.Context, h kv.Handle, old
149149
if v, err := expression.GetTimeValue(sctx, strings.ToUpper(ast.CurrentTimestamp), col.GetType(), col.GetDecimal(), nil); err == nil {
150150
newData[i] = v
151151
modified[i] = true
152+
// Only TIMESTAMP and DATETIME columns can be automatically updated, so it cannot be PKIsHandle.
153+
// Ref: https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html
154+
if col.IsPKHandleColumn(t.Meta()) {
155+
return false, errors.Errorf("on-update-now column should never be pk-is-handle")
156+
}
157+
if col.IsCommonHandleColumn(t.Meta()) {
158+
handleChanged = true
159+
}
152160
} else {
153161
return false, err
154162
}

executor/write_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4319,3 +4319,14 @@ func TestMutipleReplaceAndInsertInOneSession(t *testing.T) {
43194319

43204320
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"))
43214321
}
4322+
4323+
func TestHandleColumnWithOnUpdateCurrentTimestamp(t *testing.T) {
4324+
// Test https://github.com/pingcap/tidb/issues/44565
4325+
store := testkit.CreateMockStore(t)
4326+
tk := testkit.NewTestKit(t, store)
4327+
tk.MustExec("use test")
4328+
tk.MustExec("create table t (a timestamp on update current_timestamp(0), b int, primary key (a) clustered, key idx (a))")
4329+
tk.MustExec("insert into t values ('2023-06-11 10:00:00', 1)")
4330+
tk.MustExec("update t force index(primary) set b = 10 where a = '2023-06-11 10:00:00'")
4331+
tk.MustExec("admin check table t")
4332+
}

0 commit comments

Comments
 (0)