Skip to content

Commit

Permalink
ddl: fix creating partition table with unique prefix index (#17196)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored May 14, 2020
1 parent 5f867c5 commit a81e2c3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
6 changes: 3 additions & 3 deletions ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,13 +874,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

0 comments on commit a81e2c3

Please sign in to comment.