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: prohibit modification decimal precision to avoid inconsistency problem #10433

Merged
merged 4 commits into from
May 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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: 2 additions & 0 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,8 @@ func (s *testColumnSuite) TestModifyColumn(c *C) {
{"varchar(10)", "varchar(8)", errUnsupportedModifyColumn.GenWithStackByArgs("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.GenWithStack("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 @@ -2192,6 +2192,10 @@ func modifiableCharsetAndCollation(toCharset, toCollate, origCharset, origCollat
// 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.GenWithStack("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.GenWithStackByArgs(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 @@ -505,6 +505,16 @@ func (s *testSuite1) 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;`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why check table after all rows have been deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tidb> create table t1 (a decimal(2,1), index(a))
Query OK, 0 rows affected
Time: 0.070s
tidb> insert into t1 set a='-3.9'
Query OK, 1 row affected
Time: 0.009s
tidb> alter table t1 modify column a DECIMAL(22, 3);
Query OK, 0 rows affected
Time: 0.065s
tidb> delete from t1;
Query OK, 1 row affected
Time: 0.003s
tidb> admin check table t1
(8003, u't1 err:[admin:1]index:&admin.RecordData{Handle:1, Values:[]types.Datum{types.Datum{k:0x8, collation:0x0, decimal:0x1, length:0x2, i:0, b:[]uint8(nil), x:(*types.MyDecimal)(0xc000ab48a0)}}} != record:<nil>')

}

func (s *testSuite1) TestAdminCheckPrimaryIndex(c *C) {
Expand Down