From d65a92132e662d5b664960d6747f026e1f2177fd Mon Sep 17 00:00:00 2001 From: djshow832 Date: Thu, 9 May 2024 15:10:09 +0800 Subject: [PATCH] ddl: fix the primary key in index is not in restored format (#53118) close pingcap/tidb#52510 --- pkg/ddl/index.go | 7 +++-- .../integrationtest/r/ddl/index_modify.result | 27 +++++++++++++++++++ tests/integrationtest/t/ddl/index_modify.test | 24 +++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/pkg/ddl/index.go b/pkg/ddl/index.go index f0468554be25b3..7e6e157244f7a4 100644 --- a/pkg/ddl/index.go +++ b/pkg/ddl/index.go @@ -1749,9 +1749,12 @@ func writeChunkToLocal( } }() needRestoreForIndexes := make([]bool, len(indexes)) - restore := false + restore, pkNeedRestore := false, false + if c.PrimaryKeyInfo != nil && c.TableInfo.IsCommonHandle && c.TableInfo.CommonHandleVersion != 0 { + pkNeedRestore = tables.NeedRestoredData(c.PrimaryKeyInfo.Columns, c.TableInfo.Columns) + } for i, index := range indexes { - needRestore := tables.NeedRestoredData(index.Meta().Columns, c.TableInfo.Columns) + needRestore := pkNeedRestore || tables.NeedRestoredData(index.Meta().Columns, c.TableInfo.Columns) needRestoreForIndexes[i] = needRestore restore = restore || needRestore } diff --git a/tests/integrationtest/r/ddl/index_modify.result b/tests/integrationtest/r/ddl/index_modify.result index c17c07f8d97f62..acb4af657d9ed6 100644 --- a/tests/integrationtest/r/ddl/index_modify.result +++ b/tests/integrationtest/r/ddl/index_modify.result @@ -20,3 +20,30 @@ Error 1060 (42S21): Duplicate column name 'b' alter table test_add_index_with_dup add index c (b, a, B); Error 1060 (42S21): Duplicate column name 'B' drop table test_add_index_with_dup; +set global tidb_ddl_enable_fast_reorg=true; +drop table if exists test_add_index_restore_data; +create table test_add_index_restore_data (a char(100) NOT NULL primary key, b int) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +insert test_add_index_restore_data value('abc', 0); +alter table test_add_index_restore_data add index idx(b); +select a from test_add_index_restore_data use index(idx); +a +abc +admin check table test_add_index_restore_data; +drop table if exists test_add_index_restore_data; +create table test_add_index_restore_data (a char(100), b int NOT NULL primary key) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +insert test_add_index_restore_data value('abc', 0); +alter table test_add_index_restore_data add index idx(b); +select a from test_add_index_restore_data use index(idx); +a +abc +admin check table test_add_index_restore_data; +drop table if exists test_add_index_restore_data; +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; +insert test_add_index_restore_data value('abc', '1972-11-10', 0); +alter table test_add_index_restore_data add index idx(c); +select a from test_add_index_restore_data use index(idx); +a +abc +admin check table test_add_index_restore_data; +drop table if exists test_add_index_restore_data; +set global tidb_ddl_enable_fast_reorg=default; diff --git a/tests/integrationtest/t/ddl/index_modify.test b/tests/integrationtest/t/ddl/index_modify.test index d4eeddd1d43e2c..a3ccad6eb607eb 100644 --- a/tests/integrationtest/t/ddl/index_modify.test +++ b/tests/integrationtest/t/ddl/index_modify.test @@ -24,4 +24,28 @@ alter table test_add_index_with_dup add index c (b, a, b); alter table test_add_index_with_dup add index c (b, a, B); drop table test_add_index_with_dup; +# TestAddIndexRestoreData +set global tidb_ddl_enable_fast_reorg=true; +drop table if exists test_add_index_restore_data; +create table test_add_index_restore_data (a char(100) NOT NULL primary key, b int) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +insert test_add_index_restore_data value('abc', 0); +alter table test_add_index_restore_data add index idx(b); +select a from test_add_index_restore_data use index(idx); +admin check table test_add_index_restore_data; +drop table if exists test_add_index_restore_data; +create table test_add_index_restore_data (a char(100), b int NOT NULL primary key) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +insert test_add_index_restore_data value('abc', 0); +alter table test_add_index_restore_data add index idx(b); +select a from test_add_index_restore_data use index(idx); +admin check table test_add_index_restore_data; + +drop table if exists test_add_index_restore_data; +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; +insert test_add_index_restore_data value('abc', '1972-11-10', 0); +alter table test_add_index_restore_data add index idx(c); +select a from test_add_index_restore_data use index(idx); +admin check table test_add_index_restore_data; + +drop table if exists test_add_index_restore_data; +set global tidb_ddl_enable_fast_reorg=default;