Skip to content

Commit 6338796

Browse files
authored
ddl: set proper default flen of decimal(0) columns (#54028) (#55695)
close #53779
1 parent b9febb7 commit 6338796

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

ddl/column_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,14 @@ func TestGetDefaultValueOfColumn(t *testing.T) {
958958

959959
tk.MustQuery("select * from t1").Check(testkit.RowsWithSep("|", ""+
960960
"1962-03-03 1962-03-03 00:00:00 12:23:23 2020-10-13 2020-03-27"))
961+
962+
tk.MustExec("drop table if exists t;")
963+
tk.MustExec("create table t(a decimal(0,0), b decimal(0));")
964+
tk.MustQuery("show create table t;").Check(testkit.RowsWithSep("|", ""+
965+
"t CREATE TABLE `t` (\n"+
966+
" `a` decimal(10,0) DEFAULT NULL,\n"+
967+
" `b` decimal(10,0) DEFAULT NULL\n"+
968+
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin"))
961969
}
962970

963971
func TestIssue39080(t *testing.T) {

planner/core/preprocess.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1417,21 +1417,22 @@ func checkColumn(colDef *ast.ColumnDef) error {
14171417
}
14181418
}
14191419
case mysql.TypeNewDecimal:
1420-
if tp.GetDecimal() > mysql.MaxDecimalScale {
1421-
return types.ErrTooBigScale.GenWithStackByArgs(tp.GetDecimal(), colDef.Name.Name.O, mysql.MaxDecimalScale)
1420+
tpFlen := tp.GetFlen()
1421+
tpDecimal := tp.GetDecimal()
1422+
if tpDecimal > mysql.MaxDecimalScale {
1423+
return types.ErrTooBigScale.GenWithStackByArgs(tpDecimal, colDef.Name.Name.O, mysql.MaxDecimalScale)
14221424
}
1423-
1424-
if tp.GetFlen() > mysql.MaxDecimalWidth {
1425-
return types.ErrTooBigPrecision.GenWithStackByArgs(tp.GetFlen(), colDef.Name.Name.O, mysql.MaxDecimalWidth)
1425+
if tpFlen > mysql.MaxDecimalWidth {
1426+
return types.ErrTooBigPrecision.GenWithStackByArgs(tpFlen, colDef.Name.Name.O, mysql.MaxDecimalWidth)
14261427
}
1427-
1428-
if tp.GetFlen() < tp.GetDecimal() {
1428+
if tpFlen < tpDecimal {
14291429
return types.ErrMBiggerThanD.GenWithStackByArgs(colDef.Name.Name.O)
14301430
}
14311431
// If decimal and flen all equals 0, just set flen to default value.
1432-
if tp.GetDecimal() == 0 && tp.GetFlen() == 0 {
1432+
if tpFlen == 0 && (tpDecimal == 0 || tpDecimal == types.UnspecifiedLength) {
14331433
defaultFlen, _ := mysql.GetDefaultFieldLengthAndDecimal(mysql.TypeNewDecimal)
14341434
tp.SetFlen(defaultFlen)
1435+
tp.SetDecimal(0)
14351436
}
14361437
case mysql.TypeBit:
14371438
if tp.GetFlen() <= 0 {

0 commit comments

Comments
 (0)