Skip to content

Commit

Permalink
ddl: fix the primary key in index is not in restored format (pingcap#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Dec 6, 2024
1 parent 46e9c73 commit 0e847c3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
10 changes: 7 additions & 3 deletions ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1679,7 +1679,11 @@ func (w *addIndexIngestWorker) WriteLocal(rs *idxRecResult) (count int, nextKey
oprStartTime := time.Now()
copCtx := w.copReqSenderPool.copCtx
vars := w.sessCtx.GetSessionVars()
cnt, lastHandle, err := writeChunkToLocal(w.writer, w.index, copCtx, vars, rs.chunk)
pkNeedRestore := false
if copCtx.pkInfo != nil && copCtx.tblInfo.IsCommonHandle && copCtx.tblInfo.CommonHandleVersion != 0 {
pkNeedRestore = tables.NeedRestoredData(copCtx.pkInfo.Columns, copCtx.tblInfo.Columns)
}
cnt, lastHandle, err := writeChunkToLocal(w.writer, w.index, copCtx, vars, rs.chunk, pkNeedRestore)
if err != nil || cnt == 0 {
return 0, nil, err
}
Expand All @@ -1691,7 +1695,7 @@ func (w *addIndexIngestWorker) WriteLocal(rs *idxRecResult) (count int, nextKey

func writeChunkToLocal(writer ingest.Writer,
index table.Index, copCtx *copContext, vars *variable.SessionVars,
copChunk *chunk.Chunk) (int, kv.Handle, error) {
copChunk *chunk.Chunk, pkNeedRestore bool) (int, kv.Handle, error) {
sCtx, writeBufs := vars.StmtCtx, vars.GetWriteStmtBufs()
iter := chunk.NewIterator4Chunk(copChunk)
idxDataBuf := make([]types.Datum, len(copCtx.idxColOutputOffsets))
Expand All @@ -1701,7 +1705,7 @@ func writeChunkToLocal(writer ingest.Writer,
unlock := writer.LockForWrite()
defer unlock()
var restoreDataBuf []types.Datum
restore := tables.NeedRestoredData(index.Meta().Columns, copCtx.tblInfo.Columns)
restore := pkNeedRestore || tables.NeedRestoredData(index.Meta().Columns, copCtx.tblInfo.Columns)
if restore {
restoreDataBuf = make([]types.Datum, len(copCtx.handleOutputOffsets))
}
Expand Down
32 changes: 32 additions & 0 deletions tests/realtikvtest/addindextest/add_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,35 @@ func TestAddUKWithSmallIntHandles(t *testing.T) {
tk.MustExec("insert into t values (-9223372036854775808, 1),(-9223372036854775807, 1)")
tk.MustContainErrMsg("alter table t add unique index uk(b)", "Duplicate entry '1' for key 't.uk'")
}

func TestAddIndexRestoreData(t *testing.T) {
store := realtikvtest.CreateMockStoreAndSetup(t)
tk := testkit.NewTestKit(t, store)
defer func() {
tk.MustExec("set global tidb_ddl_enable_fast_reorg=default")
tk.MustExec("drop table if exists test_add_index_restore_data;")
}()

tk.MustExec("use test")
tk.MustExec("set global tidb_ddl_enable_fast_reorg=true;")
tk.MustExec("drop table if exists test_add_index_restore_data;")
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;")
tk.MustExec("insert test_add_index_restore_data value('abc', 0);")
tk.MustExec("alter table test_add_index_restore_data add index idx(b);")
tk.MustQuery("select a from test_add_index_restore_data use index(idx);").Check(testkit.Rows("abc"))
tk.MustExec("admin check table test_add_index_restore_data;")

tk.MustExec("drop table if exists test_add_index_restore_data;")
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;")
tk.MustExec("insert test_add_index_restore_data value('abc', 0);")
tk.MustExec("alter table test_add_index_restore_data add index idx(b);")
tk.MustQuery("select a from test_add_index_restore_data use index(idx);").Check(testkit.Rows("abc"))
tk.MustExec("admin check table test_add_index_restore_data;")

tk.MustExec("drop table if exists test_add_index_restore_data;")
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;")
tk.MustExec("insert test_add_index_restore_data value('abc', '1972-11-10', 0);")
tk.MustExec("alter table test_add_index_restore_data add index idx(c);")
tk.MustQuery("select a from test_add_index_restore_data use index(idx);").Check(testkit.Rows("abc"))
tk.MustExec("admin check table test_add_index_restore_data;")
}

0 comments on commit 0e847c3

Please sign in to comment.