From 17bba4d7a7b7d505697a5815c1e1ccafc379a882 Mon Sep 17 00:00:00 2001 From: tangenta Date: Wed, 2 Nov 2022 16:07:59 +0800 Subject: [PATCH] ddl: correct the next key during adding index (#38804) close pingcap/tidb#38803 --- ddl/index.go | 5 ++++- ddl/index_modify_test.go | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ddl/index.go b/ddl/index.go index 931580e586af0..4404ea4f551bd 100644 --- a/ddl/index.go +++ b/ddl/index.go @@ -1293,7 +1293,10 @@ func (w *baseIndexWorker) getNextKey(taskRange reorgBackfillTask, taskDone bool) recordKey := tablecodec.EncodeRecordKey(w.table.RecordPrefix(), lastHandle) return recordKey.Next() } - return taskRange.endKey.Next() + if taskRange.endInclude { + return taskRange.endKey.Next() + } + return taskRange.endKey } func (w *baseIndexWorker) updateRowDecoder(handle kv.Handle, rawRecord []byte) error { diff --git a/ddl/index_modify_test.go b/ddl/index_modify_test.go index 18ff753aee618..00695b3da8f6e 100644 --- a/ddl/index_modify_test.go +++ b/ddl/index_modify_test.go @@ -1067,3 +1067,17 @@ func TestAddIndexWithDupIndex(t *testing.T) { err = tk.ExecToErr("alter table test_add_index_with_dup add index idx (a)") require.ErrorIs(t, err, errors.Cause(err2)) } + +func TestAddIndexUniqueFailOnDuplicate(t *testing.T) { + store := testkit.CreateMockStore(t) + tk := testkit.NewTestKit(t, store) + tk.MustExec("use test") + tk.MustExec("create table t (a bigint primary key clustered, b int);") + tk.MustExec("set @@global.tidb_ddl_reorg_worker_cnt = 2;") + for i := 1; i <= 12; i++ { + tk.MustExec("insert into t values (?, ?)", i, i) + } + tk.MustExec("insert into t values (0, 1);") // Insert a duplicate key. + tk.MustQuery("split table t by (0), (1), (2), (3), (4), (5), (6), (7), (8), (9), (10), (11), (12);").Check(testkit.Rows("13 1")) + tk.MustGetErrCode("alter table t add unique index idx (b);", errno.ErrDupEntry) +}