From fa952307bfc8a0352f385c0abae9803aafeefc8d Mon Sep 17 00:00:00 2001 From: Rain Li Date: Tue, 29 Dec 2020 14:35:13 +0800 Subject: [PATCH] ddl: fix default decimal Flen value (#22036) --- ddl/db_test.go | 35 +++++++++++++++++++++++++++++++++++ planner/core/preprocess.go | 6 ++++++ 2 files changed, 41 insertions(+) diff --git a/ddl/db_test.go b/ddl/db_test.go index 6d7fab25f5..5a06cbaa61 100644 --- a/ddl/db_test.go +++ b/ddl/db_test.go @@ -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") + */ +} diff --git a/planner/core/preprocess.go b/planner/core/preprocess.go index 68e9e28fe3..5fd78a2d59 100644 --- a/planner/core/preprocess.go +++ b/planner/core/preprocess.go @@ -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)