Skip to content

Commit

Permalink
ddl: check constraint when alter enum/set type column (pingcap#19806)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiongjiwei authored Sep 17, 2020
1 parent f0db8c6 commit 73e03c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
18 changes: 14 additions & 4 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3275,19 +3275,25 @@ func CheckModifyTypeCompatible(origin *types.FieldType, to *types.FieldType) (al
default:
return "", errUnsupportedModifyColumn.GenWithStackByArgs(unsupportedMsg)
}
case mysql.TypeEnum:
case mysql.TypeEnum, mysql.TypeSet:
var typeVar string
if origin.Tp == mysql.TypeEnum {
typeVar = "enum"
} else {
typeVar = "set"
}
if origin.Tp != to.Tp {
msg := fmt.Sprintf("cannot modify enum type column's to type %s", to.String())
msg := fmt.Sprintf("cannot modify %s type column's to type %s", typeVar, to.String())
return "", errUnsupportedModifyColumn.GenWithStackByArgs(msg)
}
if len(to.Elems) < len(origin.Elems) {
msg := fmt.Sprintf("the number of enum column's elements is less than the original: %d", len(origin.Elems))
msg := fmt.Sprintf("the number of %s column's elements is less than the original: %d", typeVar, len(origin.Elems))
return "", errUnsupportedModifyColumn.GenWithStackByArgs(msg)
}
for index, originElem := range origin.Elems {
toElem := to.Elems[index]
if originElem != toElem {
msg := fmt.Sprintf("cannot modify enum column value %s to %s", originElem, toElem)
msg := fmt.Sprintf("cannot modify %s column value %s to %s", typeVar, originElem, toElem)
return "", errUnsupportedModifyColumn.GenWithStackByArgs(msg)
}
}
Expand Down Expand Up @@ -3560,6 +3566,10 @@ func (d *ddl) getModifiableColumnJob(ctx sessionctx.Context, ident ast.Ident, or
return nil, errors.Trace(err)
}

if err = checkColumnValueConstraint(newCol, newCol.Collate); err != nil {
return nil, errors.Trace(err)
}

if err = checkModifyTypes(ctx, &col.FieldType, &newCol.FieldType, isColumnWithIndex(col.Name.L, t.Meta().Indices)); err != nil {
if strings.Contains(err.Error(), "Unsupported modifying collation") {
colErrMsg := "Unsupported modifying collation of column '%s' from '%s' to '%s' when index is defined on it."
Expand Down
18 changes: 18 additions & 0 deletions expression/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7277,6 +7277,24 @@ func (s *testIntegrationSuite) TestIssue19504(c *C) {
Check(testkit.Rows("1 1", "0 0", "0 0"))
}

func (s *testIntegrationSerialSuite) TestIssue19804(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)

tk := testkit.NewTestKit(c, s.store)
tk.MustExec(`use test;`)
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a set('a', 'b', 'c'));`)
tk.MustGetErrMsg("alter table t change a a set('a', 'b', 'c', 'c');", "[types:1291]Column 'a' has duplicated value 'c' in SET")
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a enum('a', 'b', 'c'));`)
tk.MustGetErrMsg("alter table t change a a enum('a', 'b', 'c', 'c');", "[types:1291]Column 'a' has duplicated value 'c' in ENUM")
tk.MustExec(`drop table if exists t;`)
tk.MustExec(`create table t(a set('a', 'b', 'c'));`)
tk.MustExec(`alter table t change a a set('a', 'b', 'c', 'd');`)
tk.MustGetErrMsg(`alter table t change a a set('a', 'b', 'c', 'e', 'f');`, "[ddl:8200]Unsupported modify column: cannot modify set column value d to e")
}

func (s *testIntegrationSerialSuite) TestIssue18949(c *C) {
collate.SetNewCollationEnabledForTest(true)
defer collate.SetNewCollationEnabledForTest(false)
Expand Down

0 comments on commit 73e03c7

Please sign in to comment.