Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddl, planner, types: add M>=D checking for decimal column definition with default value #21845

Merged
merged 8 commits into from
Dec 29, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ddl/db_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2665,3 +2665,11 @@ func (s *testIntegrationSuite7) TestDuplicateErrorMessage(c *C) {
}
}
}

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').")
}
3 changes: 3 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,9 @@ func checkDefaultValue(ctx sessionctx.Context, c *table.Column, hasDefaultValue
return nil
}
if _, err := table.GetColDefaultValue(ctx, c.ToInfo()); err != nil {
if types.ErrMBiggerThanD.Equal(err) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I have removed it.

return types.ErrMBiggerThanD.GenWithStackByArgs(c.Name)
}
return types.ErrInvalidDefault.GenWithStackByArgs(c.Name)
}
return nil
Expand Down
7 changes: 7 additions & 0 deletions planner/core/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,9 @@ func checkColumn(colDef *ast.ColumnDef) error {
if tp.Flen > mysql.MaxFloatingTypeWidth {
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 @@ -953,6 +956,10 @@ func checkColumn(colDef *ast.ColumnDef) error {
if tp.Flen > mysql.MaxDecimalWidth {
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)
}
case mysql.TypeBit:
if tp.Flen <= 0 {
return types.ErrInvalidFieldSize.GenWithStackByArgs(colDef.Name.Name.O)
Expand Down
4 changes: 4 additions & 0 deletions types/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,10 @@ func (d *Datum) convertToMysqlDecimal(sc *stmtctx.StatementContext, target *Fiel
if err == nil && err1 != nil {
err = err1
}
if err != nil {
ret.SetMysqlDecimal(dec)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be removed?

Copy link
Contributor Author

@TszKitLo40 TszKitLo40 Dec 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove the logic in ddl_api.go, but it seems this error should be handled.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PTAL @breeswish @lysu

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added comment the this line. Do you think it is appropriate? PTAL @breeswish @lysu

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could you add a test to cover the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L242 in types/convert_test.go can reach the code.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when I remove your code here, the test can also pass...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you run the test TestConvertType?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I did

return ret, err
}
if dec.negative && mysql.HasUnsignedFlag(target.Flag) {
*dec = zeroMyDecimal
if err == nil {
Expand Down