Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl, table: fix Duplicate partition field in list partition #31953

Merged
merged 5 commits into from
Jan 25, 2022
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
8 changes: 8 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,14 @@ func (s *testIntegrationSuite1) TestCreateTableWithListColumnsPartition(c *C) {
"create table t (a bigint, b int) partition by list columns (a,b) (partition p0 values in ((1,1),(2,2)), partition p1 values in ((+1,1)));",
ddl.ErrMultipleDefConstInListPart,
},
{
"create table t1 (a int, b int) partition by list columns(a,a) ( partition p values in ((1,1)));",
ddl.ErrSameNamePartitionField,
},
{
"create table t1 (a int, b int) partition by list columns(a,b,b) ( partition p values in ((1,1,1)));",
ddl.ErrSameNamePartitionField,
},
{
`create table t1 (id int key, name varchar(10), unique index idx(name)) partition by list columns (id) (
partition p0 values in (3,5,6,9,17),
Expand Down
3 changes: 3 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,9 @@ func checkPartitionDefinitionConstraints(ctx sessionctx.Context, tbInfo *model.T
if err = checkAddPartitionOnTemporaryMode(tbInfo); err != nil {
return err
}
if err = checkPartitionColumnsUnique(tbInfo); err != nil {
return err
}

switch tbInfo.Partition.Type {
case model.PartitionTypeRange:
Expand Down
2 changes: 2 additions & 0 deletions ddl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ var (
ErrDropPartitionNonExistent = dbterror.ClassDDL.NewStd(mysql.ErrDropPartitionNonExistent)
// ErrSameNamePartition returns duplicate partition name.
ErrSameNamePartition = dbterror.ClassDDL.NewStd(mysql.ErrSameNamePartition)
// ErrSameNamePartitionField returns duplicate partition field.
ErrSameNamePartitionField = dbterror.ClassDDL.NewStd(mysql.ErrSameNamePartitionField)
// ErrRangeNotIncreasing returns values less than value must be strictly increasing for each partition.
ErrRangeNotIncreasing = dbterror.ClassDDL.NewStd(mysql.ErrRangeNotIncreasing)
// ErrPartitionMaxvalue returns maxvalue can only be used in last partition definition.
Expand Down
14 changes: 14 additions & 0 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -1679,6 +1679,20 @@ func checkAddPartitionOnTemporaryMode(tbInfo *model.TableInfo) error {
return nil
}

func checkPartitionColumnsUnique(tbInfo *model.TableInfo) error {
if len(tbInfo.Partition.Columns) <= 1 {
return nil
}
var columnsMap = make(map[string]struct{})
for _, col := range tbInfo.Partition.Columns {
if _, ok := columnsMap[col.L]; ok {
return ErrSameNamePartitionField.GenWithStackByArgs(col.L)
}
columnsMap[col.L] = struct{}{}
}
return nil
}

func checkNoHashPartitions(ctx sessionctx.Context, partitionNum uint64) error {
if partitionNum == 0 {
return ast.ErrNoParts.GenWithStackByArgs("partitions")
Expand Down
5 changes: 5 additions & 0 deletions errors.toml
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,11 @@ error = '''
This partition function is not allowed
'''

["ddl:1652"]
error = '''
Duplicate partition field name '%-.192s'
'''

["ddl:1654"]
error = '''
Partition column values of incorrect type
Expand Down