Skip to content

Commit

Permalink
ddl: set proper default flen of decimal(0) columns (#54028) (#55695)
Browse files Browse the repository at this point in the history
close #53779
  • Loading branch information
ti-chi-bot authored Sep 3, 2024
1 parent b9febb7 commit 6338796
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
8 changes: 8 additions & 0 deletions ddl/column_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -958,6 +958,14 @@ func TestGetDefaultValueOfColumn(t *testing.T) {

tk.MustQuery("select * from t1").Check(testkit.RowsWithSep("|", ""+
"1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 2020-03-27"))

tk.MustExec("drop table if exists t;")
tk.MustExec("create table t(a decimal(0,0), b decimal(0));")
tk.MustQuery("show create table t;").Check(testkit.RowsWithSep("|", ""+
"t CREATE TABLE `t` (\n"+
" `a` decimal(10,0) DEFAULT NULL,\n"+
" `b` decimal(10,0) DEFAULT NULL\n"+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
}

func TestIssue39080(t *testing.T) {
Expand Down
17 changes: 9 additions & 8 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1417,21 +1417,22 @@ func checkColumn(colDef *ast.ColumnDef) error {
}
}
case mysql.TypeNewDecimal:
if tp.GetDecimal() > mysql.MaxDecimalScale {
return types.ErrTooBigScale.GenWithStackByArgs(tp.GetDecimal(), colDef.Name.Name.O, mysql.MaxDecimalScale)
tpFlen := tp.GetFlen()
tpDecimal := tp.GetDecimal()
if tpDecimal > mysql.MaxDecimalScale {
return types.ErrTooBigScale.GenWithStackByArgs(tpDecimal, colDef.Name.Name.O, mysql.MaxDecimalScale)
}

if tp.GetFlen() > mysql.MaxDecimalWidth {
return types.ErrTooBigPrecision.GenWithStackByArgs(tp.GetFlen(), colDef.Name.Name.O, mysql.MaxDecimalWidth)
if tpFlen > mysql.MaxDecimalWidth {
return types.ErrTooBigPrecision.GenWithStackByArgs(tpFlen, colDef.Name.Name.O, mysql.MaxDecimalWidth)
}

if tp.GetFlen() < tp.GetDecimal() {
if tpFlen < tpDecimal {
return types.ErrMBiggerThanD.GenWithStackByArgs(colDef.Name.Name.O)
}
// If decimal and flen all equals 0, just set flen to default value.
if tp.GetDecimal() == 0 && tp.GetFlen() == 0 {
if tpFlen == 0 && (tpDecimal == 0 || tpDecimal == types.UnspecifiedLength) {
defaultFlen, _ := mysql.GetDefaultFieldLengthAndDecimal(mysql.TypeNewDecimal)
tp.SetFlen(defaultFlen)
tp.SetDecimal(0)
}
case mysql.TypeBit:
if tp.GetFlen() <= 0 {
Expand Down

0 comments on commit 6338796

Please sign in to comment.