Skip to content

Commit

Permalink
ddl, planner, types: add M>=D checking for decimal column definition …
Browse files Browse the repository at this point in the history
…with default value (#21845)
  • Loading branch information
TszKitLo40 committed Dec 29, 2020
1 parent 2c429bd commit 3e64072
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
8 changes: 8 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2679,3 +2679,11 @@ func (s *testIntegrationSuite3) TestIssue22028(c *C) {
_, err = tk.Exec("ALTER TABLE t MODIFY COLUMN a DOUBLE(0,0);")
c.Assert(err.Error(), Equals, "[types:1439]Display width out of range for column 'a' (max = 255)")
}

func (s *testIntegrationSuite3) TestIssue21835(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t;")
_, err := tk.Exec("create table t( col decimal(1,2) not null default 0);")
c.Assert(err.Error(), Equals, "[types:1427]For float(M,D), double(M,D) or decimal(M,D), M must be >= D (column 'col').")
}
6 changes: 6 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,9 @@ func checkColumn(colDef *ast.ColumnDef) error {
if tp.Flen > mysql.MaxFloatingTypeWidth || tp.Flen == 0 {
return types.ErrTooBigDisplayWidth.GenWithStackByArgs(colDef.Name.Name.O, mysql.MaxFloatingTypeWidth)
}
if tp.Flen < tp.Decimal {
return types.ErrMBiggerThanD.GenWithStackByArgs(colDef.Name.Name.O)
}
}
case mysql.TypeSet:
if len(tp.Elems) > mysql.MaxTypeSetMembers {
Expand All @@ -993,6 +996,9 @@ func checkColumn(colDef *ast.ColumnDef) error {
return types.ErrTooBigPrecision.GenWithStackByArgs(tp.Flen, colDef.Name.Name.O, mysql.MaxDecimalWidth)
}

if tp.Flen < tp.Decimal {
return types.ErrMBiggerThanD.GenWithStackByArgs(colDef.Name.Name.O)
}
// If decimal and flen all equals 0, just set flen to default value.
if tp.Decimal == 0 && tp.Flen == 0 {
defaultFlen, _ := mysql.GetDefaultFieldLengthAndDecimal(mysql.TypeNewDecimal)
Expand Down

0 comments on commit 3e64072

Please sign in to comment.