Skip to content

Commit

Permalink
ddl: prohibit modification decimal precision to avoid inconsistency p…
Browse files Browse the repository at this point in the history
…roblem (pingcap#10433)
  • Loading branch information
crazycs520 committed May 14, 2019
1 parent 81cf6ca commit 52018b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,8 @@ func (s *testColumnSuite) TestModifyColumn(c *C) {
{"varchar(10)", "varchar(8)", errUnsupportedModifyColumn.GenByArgs("length 8 is less than origin 10")},
{"varchar(10)", "varchar(11)", nil},
{"varchar(10) character set utf8 collate utf8_bin", "varchar(10) character set utf8", nil},
{"decimal(2,1)", "decimal(3,2)", errUnsupportedModifyColumn.Gen("unsupported modify decimal column precision")},
{"decimal(2,1)", "decimal(2,1)", nil},
}
for _, tt := range tests {
ftA := s.colDefStrToFieldType(c, tt.origin)
Expand Down
4 changes: 4 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,10 @@ func (d *ddl) DropColumn(ctx sessionctx.Context, ti ast.Ident, colName model.CIS
// It returns true if the two types has the same Charset and Collation, the same sign, both are
// integer types or string types, and new Flen and Decimal must be greater than or equal to origin.
func modifiable(origin *types.FieldType, to *types.FieldType) error {
// The root cause is modifying decimal precision needs to rewrite binary representation of that decimal.
if origin.Tp == mysql.TypeNewDecimal && (to.Flen != origin.Flen || to.Decimal != origin.Decimal) {
return errUnsupportedModifyColumn.Gen("unsupported modify decimal column precision")
}
if to.Flen > 0 && to.Flen < origin.Flen {
msg := fmt.Sprintf("length %d is less than origin %d", to.Flen, origin.Flen)
return errUnsupportedModifyColumn.GenByArgs(msg)
Expand Down
10 changes: 10 additions & 0 deletions executor/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,16 @@ func (s *testSuite) TestAdminCheckTable(c *C) {
tk.MustExec(`alter table t1 add column b timestamp not null;`)
tk.MustExec(`alter table t1 add index(b);`)
tk.MustExec(`admin check table t1;`)

// Test for index with change decimal precision.
tk.MustExec(`drop table if exists t1`)
tk.MustExec(`create table t1 (a decimal(2,1), index(a))`)
tk.MustExec(`insert into t1 set a='1.9'`)
_, err := tk.Exec(`alter table t1 modify column a decimal(3,2);`)
c.Assert(err, NotNil)
c.Assert(err.Error(), Equals, "[ddl:203]unsupported modify decimal column precision")
tk.MustExec(`delete from t1;`)
tk.MustExec(`admin check table t1;`)
}

func (s *testSuite) TestAdminShowNextID(c *C) {
Expand Down

0 comments on commit 52018b5

Please sign in to comment.