Skip to content

Commit

Permalink
ddl: fix the primary key in index is not in restored format (#53118) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Dec 6, 2024
1 parent 4b4b177 commit d61c40d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/ddl/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -1816,9 +1816,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
}
Expand Down
30 changes: 30 additions & 0 deletions tests/integrationtest/r/ddl/index_modify.result
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,33 @@ 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;
24 changes: 24 additions & 0 deletions tests/integrationtest/t/ddl/index_modify.test
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit d61c40d

Please sign in to comment.