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: fix creating partition table with unique prefix index #17196

Merged
merged 4 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 11 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,17 @@ func (s *testIntegrationSuite5) TestPartitionUniqueKeyNeedAllFieldsInPf(c *C) {
partition p2 values less than (11, 22)
)`
tk.MustGetErrCode(sql11, tmysql.ErrUniqueKeyNeedAllFieldsInPf)

sql12 := `create table part12 (a varchar(20), b binary, unique index (a(5))) partition by range columns (a) (
partition p0 values less than ('aaaaa'),
partition p1 values less than ('bbbbb'),
partition p2 values less than ('ccccc'))`
tk.MustGetErrCode(sql12, tmysql.ErrUniqueKeyNeedAllFieldsInPf)
tk.MustExec(`create table part12 (a varchar(20), b binary) partition by range columns (a) (
partition p0 values less than ('aaaaa'),
partition p1 values less than ('bbbbb'),
partition p2 values less than ('ccccc'))`)
tk.MustGetErrCode("alter table part12 add unique index (a(5))", tmysql.ErrUniqueKeyNeedAllFieldsInPf)
}

func (s *testIntegrationSuite2) TestPartitionDropPrimaryKey(c *C) {
Expand Down
21 changes: 16 additions & 5 deletions ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,11 +741,13 @@ func checkPartitioningKeysConstraints(sctx sessionctx.Context, s *ast.CreateTabl
// Every unique key on the table must use every column in the table's partitioning expression.
// See https://dev.mysql.com/doc/refman/5.7/en/partitioning-limitations-partitioning-keys-unique-keys.html
for _, index := range tblInfo.Indices {
if index.Unique && !checkUniqueKeyIncludePartKey(partCols, index.Columns) {
if index.Primary {
return ErrUniqueKeyNeedAllFieldsInPf.GenWithStackByArgs("PRIMARY KEY")
if index.Unique {
if hasPrefixIndexColumn(index.Columns) || !checkUniqueKeyIncludePartKey(partCols, index.Columns) {
if index.Primary {
return ErrUniqueKeyNeedAllFieldsInPf.GenWithStackByArgs("PRIMARY KEY")
}
return ErrUniqueKeyNeedAllFieldsInPf.GenWithStackByArgs("UNIQUE INDEX")
}
return ErrUniqueKeyNeedAllFieldsInPf.GenWithStackByArgs("UNIQUE INDEX")
}
}
// when PKIsHandle, tblInfo.Indices will not contain the primary key.
Expand All @@ -761,6 +763,15 @@ func checkPartitioningKeysConstraints(sctx sessionctx.Context, s *ast.CreateTabl
return nil
}

func hasPrefixIndexColumn(idxCols []*model.IndexColumn) bool {
for _, col := range idxCols {
if col.Length > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check the table name too?

Below sql will execute sucessfull in Mysql but faild in This PR:

create table part12 (a varchar(20), b varchar(10), unique index (a(5),b)) partition by range columns (b) (
			partition p0 values less than ('aaaaa'),
			partition p1 values less than ('bbbbb'),
			partition p2 values less than ('ccccc'))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

return true
}
}
return false
}

func checkPartitionKeysConstraint(pi *model.PartitionInfo, indexColumns []*model.IndexColumn, tblInfo *model.TableInfo, isPK bool) error {
var (
partCols []*model.ColumnInfo
Expand Down Expand Up @@ -788,7 +799,7 @@ func checkPartitionKeysConstraint(pi *model.PartitionInfo, indexColumns []*model
// Every unique key on the table must use every column in the table's partitioning expression.(This
// also includes the table's primary key.)
// See https://dev.mysql.com/doc/refman/5.7/en/partitioning-limitations-partitioning-keys-unique-keys.html
if !checkUniqueKeyIncludePartKey(columnInfoSlice(partCols), indexColumns) {
if hasPrefixIndexColumn(indexColumns) || !checkUniqueKeyIncludePartKey(columnInfoSlice(partCols), indexColumns) {
if isPK {
return ErrUniqueKeyNeedAllFieldsInPf.GenWithStackByArgs("PRIMARY")
}
Expand Down