ddl: fix default decimal Flen value (#22036)

This commit is contained in:
Rain Li
2020-12-29 14:35:13 +08:00
committed by GitHub
parent 5886c4b6f6
commit fa952307bf
2 changed files with 41 additions and 0 deletions

View File

@ -6489,3 +6489,38 @@ func (s *testDBSuite4) TestUnsupportedAlterTableOption(c *C) {
tk.MustExec("create table t(a char(10) not null,b char(20)) shard_row_id_bits=6;")
tk.MustGetErrCode("alter table t pre_split_regions=6;", errno.ErrUnsupportedDDLOperation)
}
func (s *testDBSuite4) TestCreateTableWithDecimalWithDoubleZero(c *C) {
tk := testkit.NewTestKit(c, s.store)
checkType := func(db, table, field string) {
ctx := tk.Se.(sessionctx.Context)
is := domain.GetDomain(ctx).InfoSchema()
tableInfo, err := is.TableByName(model.NewCIStr(db), model.NewCIStr(table))
c.Assert(err, IsNil)
tblInfo := tableInfo.Meta()
for _, col := range tblInfo.Columns {
if col.Name.L == field {
c.Assert(col.Flen, Equals, 10)
}
}
}
tk.MustExec("use test")
tk.MustExec("drop table if exists tt")
tk.MustExec("create table tt(d decimal(0, 0))")
checkType("test", "tt", "d")
tk.MustExec("drop table tt")
tk.MustExec("create table tt(a int)")
tk.MustExec("alter table tt add column d decimal(0, 0)")
checkType("test", "tt", "d")
/*
Currently not support change column to decimal
tk.MustExec("drop table tt")
tk.MustExec("create table tt(d int)")
tk.MustExec("alter table tt change column d d decimal(0, 0)")
checkType("test", "tt", "d")
*/
}

View File

@ -992,6 +992,12 @@ func checkColumn(colDef *ast.ColumnDef) error {
if tp.Flen > mysql.MaxDecimalWidth {
return types.ErrTooBigPrecision.GenWithStackByArgs(tp.Flen, colDef.Name.Name.O, mysql.MaxDecimalWidth)
}
// 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)
tp.Flen = defaultFlen
}
case mysql.TypeBit:
if tp.Flen <= 0 {
return types.ErrInvalidFieldSize.GenWithStackByArgs(colDef.Name.Name.O)