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: ignore integer zerofill size attribute when changing the column types (#20862) #20986

Merged
merged 5 commits into from
Nov 18, 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
2 changes: 1 addition & 1 deletion ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,7 @@ func (s *testColumnSuite) TestModifyColumn(c *C) {
err error
}{
{"int", "bigint", nil},
{"int", "int unsigned", errUnsupportedModifyColumn.GenWithStackByArgs("length 10 is less than origin 11")},
{"int", "int unsigned", errUnsupportedModifyColumn.GenWithStackByArgs("can't change unsigned integer to signed or vice versa")},
{"varchar(10)", "text", nil},
{"varbinary(10)", "blob", nil},
{"text", "blob", errUnsupportedModifyCharset.GenWithStackByArgs("charset from utf8mb4 to binary")},
Expand Down
19 changes: 19 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4930,3 +4930,22 @@ func init() {
domain.SchemaOutOfDateRetryInterval = int64(50 * time.Millisecond)
domain.SchemaOutOfDateRetryTimes = int32(50)
}

// Test issue #20529.
func (s *testSerialDBSuite) TestColumnTypeChangeIgnoreDisplayLength(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

// Change int to int(3).
// Although display length is increased, the default flen is decreased, reorg is needed.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("alter table t modify column a int(3)")

// Change int to bigint(1)
// Although display length is decreased, default flen is the same, reorg is not needed.
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int)")
tk.MustExec("alter table t modify column a bigint(1)")
tk.MustExec("drop table if exists t")
}
13 changes: 10 additions & 3 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2706,6 +2706,10 @@ func checkModifyCharsetAndCollation(toCharset, toCollate, origCharset, origColla
// field length and precision.
func CheckModifyTypeCompatible(origin *types.FieldType, to *types.FieldType) error {
unsupportedMsg := fmt.Sprintf("type %v not match origin %v", to.CompactStr(), origin.CompactStr())
var (
toFlen = to.Flen
originFlen = origin.Flen
)
switch origin.Tp {
case mysql.TypeVarchar, mysql.TypeString, mysql.TypeVarString,
mysql.TypeBlob, mysql.TypeTinyBlob, mysql.TypeMediumBlob, mysql.TypeLongBlob:
Expand All @@ -2718,6 +2722,10 @@ func CheckModifyTypeCompatible(origin *types.FieldType, to *types.FieldType) err
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
switch to.Tp {
case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
// For integers, we should ignore the potential display length represented by flen, using
// the default flen of the type.
originFlen, _ = mysql.GetDefaultFieldLengthAndDecimal(origin.Tp)
toFlen, _ = mysql.GetDefaultFieldLengthAndDecimal(to.Tp)
default:
return errUnsupportedModifyColumn.GenWithStackByArgs(unsupportedMsg)
}
Expand Down Expand Up @@ -2756,9 +2764,8 @@ func CheckModifyTypeCompatible(origin *types.FieldType, to *types.FieldType) err
return errUnsupportedModifyColumn.GenWithStackByArgs(unsupportedMsg)
}
}

if to.Flen > 0 && to.Flen < origin.Flen {
msg := fmt.Sprintf("length %d is less than origin %d", to.Flen, origin.Flen)
if toFlen > 0 && toFlen < originFlen {
msg := fmt.Sprintf("length %d is less than origin %d", toFlen, originFlen)
return errUnsupportedModifyColumn.GenWithStackByArgs(msg)
}
if to.Decimal > 0 && to.Decimal < origin.Decimal {
Expand Down
2 changes: 1 addition & 1 deletion ddl/ddl_worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func (s *testDDLSuite) TestCancelJob(c *C) {
// modify column
col.DefaultValue = "1"
updateTest(&tests[15])
modifyColumnArgs := []interface{}{col, col.Name, &ast.ColumnPosition{}, byte(0)}
modifyColumnArgs := []interface{}{col, col.Name, &ast.ColumnPosition{}, byte(0), uint64(0)}
doDDLJobErrWithSchemaState(ctx, d, c, dbInfo.ID, tblInfo.ID, test.act, modifyColumnArgs, &test.cancelState)
c.Check(checkErr, IsNil)
changedTable = testGetTable(c, d, dbInfo.ID, tblInfo.ID)
Expand Down