Skip to content

Commit

Permalink
ddl: fix double/float data not being truncated for column modificatio…
Browse files Browse the repository at this point in the history
…n operations when the number of decimal places is reduced. (#41555)

close #41281
  • Loading branch information
zimulala committed Feb 20, 2023
1 parent df249e9 commit 109b3b6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
3 changes: 2 additions & 1 deletion ddl/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,8 @@ func needChangeColumnData(oldCol, newCol *model.ColumnInfo) bool {
toUnsigned := mysql.HasUnsignedFlag(newCol.GetFlag())
originUnsigned := mysql.HasUnsignedFlag(oldCol.GetFlag())
needTruncationOrToggleSign := func() bool {
return (newCol.GetFlen() > 0 && newCol.GetFlen() < oldCol.GetFlen()) || (toUnsigned != originUnsigned)
return (newCol.GetFlen() > 0 && (newCol.GetFlen() < oldCol.GetFlen() || newCol.GetDecimal() < oldCol.GetDecimal())) ||
(toUnsigned != originUnsigned)
}
// Ignore the potential max display length represented by integer's flen, use default flen instead.
defaultOldColFlen, _ := mysql.GetDefaultFieldLengthAndDecimal(oldCol.GetType())
Expand Down
6 changes: 5 additions & 1 deletion ddl/column_type_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2321,11 +2321,15 @@ func TestColumnTypeChangeBetweenFloatAndDouble(t *testing.T) {
prepare := func(createTableStmt string) {
tk.MustExec("drop table if exists t;")
tk.MustExec(createTableStmt)
tk.MustExec("insert into t values (36.4), (24.1);")
tk.MustExec("insert into t values (36.43), (24.1);")
}

prepare("create table t (a float(6,2));")
tk.MustExec("alter table t modify a double(6,2)")
tk.MustQuery("select a from t;").Check(testkit.Rows("36.43", "24.1"))

prepare("create table t (a float(6,2));")
tk.MustExec("alter table t modify a float(6,1)")
tk.MustQuery("select a from t;").Check(testkit.Rows("36.4", "24.1"))

prepare("create table t (a double(6,2));")
Expand Down

0 comments on commit 109b3b6

Please sign in to comment.