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) #17213

Merged
merged 4 commits into from
May 21, 2020
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
6 changes: 3 additions & 3 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,13 @@ func generateOriginDefaultValue(col *model.ColumnInfo) (interface{}, error) {
return odValue, nil
}

func findColumnInIndexCols(c string, cols []*model.IndexColumn) bool {
func findColumnInIndexCols(c string, cols []*model.IndexColumn) *model.IndexColumn {
for _, c1 := range cols {
if c == c1.Name.L {
return true
return c1
}
}
return false
return nil
}

func getColumnInfoByName(tbInfo *model.TableInfo, column string) *model.ColumnInfo {
Expand Down
16 changes: 16 additions & 0 deletions ddl/db_partition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,22 @@ 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)
sql13 := `create table part13 (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'))`
tk.MustExec(sql13)
}

func (s *testIntegrationSuite2) TestPartitionDropPrimaryKey(c *C) {
Expand Down
8 changes: 7 additions & 1 deletion ddl/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -861,7 +861,13 @@ type stringSlice interface {
func checkUniqueKeyIncludePartKey(partCols stringSlice, idxCols []*model.IndexColumn) bool {
for i := 0; i < partCols.Len(); i++ {
partCol := partCols.At(i)
if !findColumnInIndexCols(partCol, idxCols) {
idxCol := findColumnInIndexCols(partCol, idxCols)
if idxCol == nil {
// Partition column is not found in the index columns.
return false
}
if idxCol.Length > 0 {
// The partition column is found in the index columns, but the index column is a prefix index
return false
}
}
Expand Down