Skip to content

Commit

Permalink
add check for only enlarge decimal size
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 committed Feb 4, 2020
1 parent d0bb787 commit dd4cb55
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
4 changes: 4 additions & 0 deletions ddl/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3705,6 +3705,10 @@ func (s *testDBSuite1) TestModifyColumnWithDecimal(c *C) {
testExecErrorMessage(c, s.tk, "alter table t_decimal modify column b decimal(9,6);",
"[ddl:8200]Unsupported modify column: length 9 is less than origin 10")

// Test modify decimal column that only enlarge decimal size may cause out of range value error.
testExecErrorMessage(c, s.tk, "alter table t_decimal modify column b decimal(10,6);",
"[ddl:8200]Unsupported modify column: modify original decimal(10,5) to decimal(10,6) may cause out of range value error")

// Test modify column successful when no index covered.
s.tk.MustExec("alter table t_decimal modify column b decimal(11,6);")
s.tk.MustExec("insert into t_decimal values (3, 12345.678912, 'c');")
Expand Down
27 changes: 16 additions & 11 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2566,20 +2566,20 @@ func modifiable(tbInfo *model.TableInfo, originalCol *model.ColumnInfo, to *type
}
}
case mysql.TypeNewDecimal:
for _, indexInfo := range tbInfo.Indices {
containColumn := false
for _, col := range indexInfo.Columns {
if col.Name.L == originalCol.Name.L {
containColumn = true
break
if to.Flen != origin.Flen || to.Decimal != origin.Decimal {
for _, indexInfo := range tbInfo.Indices {
containColumn := false
for _, col := range indexInfo.Columns {
if col.Name.L == originalCol.Name.L {
containColumn = true
break
}
}
}
if containColumn {
// The root cause is modifying decimal precision needs to rewrite binary representation of that decimal.
if to.Flen != origin.Flen || to.Decimal != origin.Decimal {
if containColumn {
// The root cause is modifying decimal precision needs to rewrite binary representation of that decimal.
// If has index cover this column, need to re-create the index to avoid data inconsistency.
return errUnsupportedModifyColumn.GenWithStackByArgs("can't change decimal column precision with index covered now")
}
break
}
}
default:
Expand All @@ -2596,6 +2596,11 @@ func modifiable(tbInfo *model.TableInfo, originalCol *model.ColumnInfo, to *type
msg := fmt.Sprintf("decimal %d is less than origin %d", to.Decimal, origin.Decimal)
return errUnsupportedModifyColumn.GenWithStackByArgs(msg)
}
if (to.Flen - to.Decimal) < (origin.Flen - origin.Decimal) {
msg := fmt.Sprintf("modify original decimal(%d,%d) to decimal(%d,%d) may cause out of range value error",
origin.Flen, origin.Decimal, to.Flen, to.Decimal)
return errUnsupportedModifyColumn.GenWithStackByArgs(msg)
}

toUnsigned := mysql.HasUnsignedFlag(to.Flag)
originUnsigned := mysql.HasUnsignedFlag(origin.Flag)
Expand Down

0 comments on commit dd4cb55

Please sign in to comment.