Skip to content

Commit

Permalink
ddl, table: fix Duplicate partition field in list partition (pingcap#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Defined2014 authored Jan 25, 2022
1 parent 3c797fd commit 2e39aac
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
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

0 comments on commit 2e39aac

Please sign in to comment.