Skip to content

Commit

Permalink
ddl: correct the next key during adding index (#38804)
Browse files Browse the repository at this point in the history
close #38803
  • Loading branch information
tangenta authored Nov 2, 2022
1 parent 722aa68 commit 17bba4d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions ddl/index_modify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 17bba4d

Please sign in to comment.