diff --git a/ddl/column.go b/ddl/column.go index b2c743593aed1..71f9994cd9eb0 100644 --- a/ddl/column.go +++ b/ddl/column.go @@ -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 { diff --git a/ddl/db_partition_test.go b/ddl/db_partition_test.go index 8c6ed90d03289..3ca6d95f42097 100644 --- a/ddl/db_partition_test.go +++ b/ddl/db_partition_test.go @@ -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) { diff --git a/ddl/partition.go b/ddl/partition.go index 25b9fcf37323f..7e799a350592c 100644 --- a/ddl/partition.go +++ b/ddl/partition.go @@ -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 } }