Skip to content

Commit 0e847c3

Browse files
authored
ddl: fix the primary key in index is not in restored format (pingcap#53118) (pingcap#57999)
close pingcap#58036
1 parent 46e9c73 commit 0e847c3

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

ddl/index.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1679,7 +1679,11 @@ func (w *addIndexIngestWorker) WriteLocal(rs *idxRecResult) (count int, nextKey
16791679
oprStartTime := time.Now()
16801680
copCtx := w.copReqSenderPool.copCtx
16811681
vars := w.sessCtx.GetSessionVars()
1682-
cnt, lastHandle, err := writeChunkToLocal(w.writer, w.index, copCtx, vars, rs.chunk)
1682+
pkNeedRestore := false
1683+
if copCtx.pkInfo != nil && copCtx.tblInfo.IsCommonHandle && copCtx.tblInfo.CommonHandleVersion != 0 {
1684+
pkNeedRestore = tables.NeedRestoredData(copCtx.pkInfo.Columns, copCtx.tblInfo.Columns)
1685+
}
1686+
cnt, lastHandle, err := writeChunkToLocal(w.writer, w.index, copCtx, vars, rs.chunk, pkNeedRestore)
16831687
if err != nil || cnt == 0 {
16841688
return 0, nil, err
16851689
}
@@ -1691,7 +1695,7 @@ func (w *addIndexIngestWorker) WriteLocal(rs *idxRecResult) (count int, nextKey
16911695

16921696
func writeChunkToLocal(writer ingest.Writer,
16931697
index table.Index, copCtx *copContext, vars *variable.SessionVars,
1694-
copChunk *chunk.Chunk) (int, kv.Handle, error) {
1698+
copChunk *chunk.Chunk, pkNeedRestore bool) (int, kv.Handle, error) {
16951699
sCtx, writeBufs := vars.StmtCtx, vars.GetWriteStmtBufs()
16961700
iter := chunk.NewIterator4Chunk(copChunk)
16971701
idxDataBuf := make([]types.Datum, len(copCtx.idxColOutputOffsets))
@@ -1701,7 +1705,7 @@ func writeChunkToLocal(writer ingest.Writer,
17011705
unlock := writer.LockForWrite()
17021706
defer unlock()
17031707
var restoreDataBuf []types.Datum
1704-
restore := tables.NeedRestoredData(index.Meta().Columns, copCtx.tblInfo.Columns)
1708+
restore := pkNeedRestore || tables.NeedRestoredData(index.Meta().Columns, copCtx.tblInfo.Columns)
17051709
if restore {
17061710
restoreDataBuf = make([]types.Datum, len(copCtx.handleOutputOffsets))
17071711
}

tests/realtikvtest/addindextest/add_index_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,35 @@ func TestAddUKWithSmallIntHandles(t *testing.T) {
226226
tk.MustExec("insert into t values (-9223372036854775808, 1),(-9223372036854775807, 1)")
227227
tk.MustContainErrMsg("alter table t add unique index uk(b)", "Duplicate entry '1' for key 't.uk'")
228228
}
229+
230+
func TestAddIndexRestoreData(t *testing.T) {
231+
store := realtikvtest.CreateMockStoreAndSetup(t)
232+
tk := testkit.NewTestKit(t, store)
233+
defer func() {
234+
tk.MustExec("set global tidb_ddl_enable_fast_reorg=default")
235+
tk.MustExec("drop table if exists test_add_index_restore_data;")
236+
}()
237+
238+
tk.MustExec("use test")
239+
tk.MustExec("set global tidb_ddl_enable_fast_reorg=true;")
240+
tk.MustExec("drop table if exists test_add_index_restore_data;")
241+
tk.MustExec("create table test_add_index_restore_data (a char(100) NOT NULL primary key, b int) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;")
242+
tk.MustExec("insert test_add_index_restore_data value('abc', 0);")
243+
tk.MustExec("alter table test_add_index_restore_data add index idx(b);")
244+
tk.MustQuery("select a from test_add_index_restore_data use index(idx);").Check(testkit.Rows("abc"))
245+
tk.MustExec("admin check table test_add_index_restore_data;")
246+
247+
tk.MustExec("drop table if exists test_add_index_restore_data;")
248+
tk.MustExec("create table test_add_index_restore_data (a char(100), b int NOT NULL primary key) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;")
249+
tk.MustExec("insert test_add_index_restore_data value('abc', 0);")
250+
tk.MustExec("alter table test_add_index_restore_data add index idx(b);")
251+
tk.MustQuery("select a from test_add_index_restore_data use index(idx);").Check(testkit.Rows("abc"))
252+
tk.MustExec("admin check table test_add_index_restore_data;")
253+
254+
tk.MustExec("drop table if exists test_add_index_restore_data;")
255+
tk.MustExec("create table test_add_index_restore_data (a char(100) NOT NULL, b date NOT NULL DEFAULT '2005-02-12', c int, primary key(a, b)) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;")
256+
tk.MustExec("insert test_add_index_restore_data value('abc', '1972-11-10', 0);")
257+
tk.MustExec("alter table test_add_index_restore_data add index idx(c);")
258+
tk.MustQuery("select a from test_add_index_restore_data use index(idx);").Check(testkit.Rows("abc"))
259+
tk.MustExec("admin check table test_add_index_restore_data;")
260+
}

0 commit comments

Comments
 (0)