Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkg/ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4603,6 +4603,38 @@ func (d *ddl) AlterTablePartitioning(ctx sessionctx.Context, ident ast.Ident, sp
}
newPartInfo := newMeta.Partition

for _, index := range newMeta.Indices {
if index.Unique {
ck, err := checkPartitionKeysConstraint(newMeta.GetPartitionInfo(), index.Columns, newMeta)
if err != nil {
return err
}
if !ck {
if ctx.GetSessionVars().EnableGlobalIndex {
return dbterror.ErrCancelledDDLJob.GenWithStack("global index is not supported yet for alter table partitioning")
}
indexTp := "UNIQUE INDEX"
if index.Primary {
indexTp = "PRIMARY"
}
return dbterror.ErrUniqueKeyNeedAllFieldsInPf.GenWithStackByArgs(indexTp)
}
}
}
if newMeta.PKIsHandle {
indexCols := []*model.IndexColumn{{
Name: newMeta.GetPkName(),
Length: types.UnspecifiedLength,
}}
ck, err := checkPartitionKeysConstraint(newMeta.GetPartitionInfo(), indexCols, newMeta)
if err != nil {
return err
}
if !ck {
return dbterror.ErrUniqueKeyNeedAllFieldsInPf.GenWithStackByArgs("PRIMARY")
}
}

if err = handlePartitionPlacement(ctx, newPartInfo); err != nil {
return errors.Trace(err)
}
Expand Down
35 changes: 35 additions & 0 deletions tests/integrationtest/r/ddl/integration.result
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,38 @@ create table t (d int default '18446744073709551616' );
Level Code Message
Warning 1690 BIGINT value is out of range in '18446744073709551616'
set sql_mode=DEFAULT;
drop table if exists t;
create table t(a int not null, b int, primary key(a), unique idx_b(b));
drop table if exists t2;
create table t2(a int not null, b int, primary key(a) nonclustered, unique idx_b(b));
drop table if exists t3;
set tidb_enable_global_index=1;
create table t3(a int not null, b int, primary key(a) nonclustered, unique idx_b(b)) partition by hash(a) partitions 3;
drop table if exists t4;
create table t4(a int not null, b int, primary key(a)) partition by hash(a) partitions 3;
alter table t partition by hash(a) partitions 3;
Error 8214 (HY000): global index is not supported yet for alter table partitioning
alter table t partition by key() partitions 3;
Error 8214 (HY000): global index is not supported yet for alter table partitioning
alter table t partition by hash(b) partitions 3;
Error 1503 (HY000): A PRIMARY must include all columns in the table's partitioning function
alter table t2 partition by hash(b) partitions 3;
Error 8214 (HY000): global index is not supported yet for alter table partitioning
alter table t3 partition by key(a) partitions 3;
Error 8214 (HY000): global index is not supported yet for alter table partitioning
alter table t4 partition by hash(b) partitions 3;
Error 1503 (HY000): A PRIMARY must include all columns in the table's partitioning function
set tidb_enable_global_index=0;
alter table t partition by hash(a) partitions 3;
Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function
alter table t partition by key() partitions 3;
Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function
alter table t partition by hash(b) partitions 3;
Error 1503 (HY000): A PRIMARY must include all columns in the table's partitioning function
alter table t2 partition by hash(b) partitions 3;
Error 1503 (HY000): A PRIMARY must include all columns in the table's partitioning function
alter table t3 partition by key(a) partitions 3;
Error 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function
alter table t4 partition by hash(b) partitions 3;
Error 1503 (HY000): A PRIMARY must include all columns in the table's partitioning function
drop table t, t2, t3, t4;
37 changes: 37 additions & 0 deletions tests/integrationtest/t/ddl/integration.test
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,40 @@ set sql_mode='';
create table t (d int default '18446744073709551616' );
-- disable_warnings
set sql_mode=DEFAULT;

# Test alter non-partition table to partition with global index needed.
drop table if exists t;
create table t(a int not null, b int, primary key(a), unique idx_b(b));
drop table if exists t2;
create table t2(a int not null, b int, primary key(a) nonclustered, unique idx_b(b));
drop table if exists t3;
set tidb_enable_global_index=1;
create table t3(a int not null, b int, primary key(a) nonclustered, unique idx_b(b)) partition by hash(a) partitions 3;
drop table if exists t4;
create table t4(a int not null, b int, primary key(a)) partition by hash(a) partitions 3;
-- error 8214
alter table t partition by hash(a) partitions 3;
-- error 8214
alter table t partition by key() partitions 3;
-- error 1503
alter table t partition by hash(b) partitions 3;
-- error 8214
alter table t2 partition by hash(b) partitions 3;
-- error 8214
alter table t3 partition by key(a) partitions 3;
-- error 1503
alter table t4 partition by hash(b) partitions 3;
set tidb_enable_global_index=0;
-- error 1503
alter table t partition by hash(a) partitions 3;
-- error 1503
alter table t partition by key() partitions 3;
-- error 1503
alter table t partition by hash(b) partitions 3;
-- error 1503
alter table t2 partition by hash(b) partitions 3;
-- error 1503
alter table t3 partition by key(a) partitions 3;
-- error 1503
alter table t4 partition by hash(b) partitions 3;
drop table t, t2, t3, t4;