Skip to content

Commit

Permalink
ddl: set proper default flen of decimal(0) columns (#54028) (#54619)
Browse files Browse the repository at this point in the history
close #53779
  • Loading branch information
ti-chi-bot authored Jul 22, 2024
1 parent 9f774c8 commit d6e12bb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pkg/planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1415,21 +1415,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
8 changes: 8 additions & 0 deletions tests/integrationtest/r/ddl/column.result
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,11 @@ t2 CREATE TABLE `t2` (
PRIMARY KEY (`id`) /*T![clustered_index] CLUSTERED */,
UNIQUE KEY `authorIdx` (`authorId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
drop table if exists t;
create table t(a decimal(0,0), b decimal(0));
show create table t;
Table Create Table
t CREATE TABLE `t` (
`a` decimal(10,0) DEFAULT NULL,
`b` decimal(10,0) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin
4 changes: 4 additions & 0 deletions tests/integrationtest/t/ddl/column.test
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ show create table t1;
CREATE TABLE `t2`( `id` INTEGER PRIMARY KEY, `authorId` int(11) AUTO_INCREMENT, UNIQUE KEY `authorIdx` (`authorId`));
show create table t2;

# TestIssue53779
drop table if exists t;
create table t(a decimal(0,0), b decimal(0));
show create table t;

0 comments on commit d6e12bb

Please sign in to comment.