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

Conversation

TszKitLo40
Copy link
Contributor

What problem does this PR solve?

Issue Number: close #21835

Problem Summary:

What is changed and how it works?

What's Changed:
add M>=D checking for decimal column definition with default value
How it Works:
return error if M >= D for decimal column definition

Related changes

Check List

Tests

  • Unit test
  • Integration test

Side effects

  • Performance regression
    • Consumes more CPU
    • Consumes more MEM

Release note

  • No release note

@TszKitLo40 TszKitLo40 requested a review from a team as a code owner December 17, 2020 02:47
@TszKitLo40 TszKitLo40 requested review from qw4990 and removed request for a team December 17, 2020 02:47
@blacktear23
Copy link
Contributor

blacktear23 commented Dec 17, 2020

@TszKitLo40 please take a look at:
https://github.com/pingcap/tidb/blob/master/planner/core/preprocess.go#L882
I think you can put some grammar check at there.

@TszKitLo40
Copy link
Contributor Author

@TszKitLo40 please take a look at:
https://github.com/pingcap/tidb/blob/master/planner/core/preprocess.go#L882
I think you can put some grammar check at there.

But the grammar check method here is private

@blacktear23
Copy link
Contributor

blacktear23 commented Dec 17, 2020

@TszKitLo40 I mean grammar check such as M >= D can put in planner/core/preprocess.go in checkColumn function. As you can see the M's max and D's max check is in this function. Every SQL query is parsed by parser and AST will process by executor module and executor module will use planner to create a plan and preprocess.go will check the AST's grammar errors before generate the real plan. So I think we should put M >= D check in preprocess.go. This will make grammar check code more centralized. :)

@TszKitLo40
Copy link
Contributor Author

@TszKitLo40 I mean grammar check such as M >= D can put in planner/core/preprocess.go in checkColumn function. As you can see the M's max and D's max check is in this function. Every SQL query is parsed by parser and AST will process by executor module and executor module will use planner to create a plan and preprocess.go will check the AST's grammar errors before generate the real plan. So I think we should put M >= D check in preprocess.go. This will make grammar check code more centralized. :)

It is reasonable. If we add this check in preprocess.go, will the planner check M >= D before the check in ddl?

@blacktear23
Copy link
Contributor

@TszKitLo40 I mean grammar check such as M >= D can put in planner/core/preprocess.go in checkColumn function. As you can see the M's max and D's max check is in this function. Every SQL query is parsed by parser and AST will process by executor module and executor module will use planner to create a plan and preprocess.go will check the AST's grammar errors before generate the real plan. So I think we should put M >= D check in preprocess.go. This will make grammar check code more centralized. :)

It is reasonable. If we add this check in preprocess.go, will the planner check M >= D before the check in ddl?

Yes!

@TszKitLo40
Copy link
Contributor Author

TszKitLo40 commented Dec 17, 2020

Do you think we should keep the logic I changed? I think we shouldn't ignore the err in convertToMysqlDecimal. @blacktear23

@ichn-hu ichn-hu mentioned this pull request Dec 17, 2020
@TszKitLo40 TszKitLo40 requested a review from a team as a code owner December 17, 2020 05:24
@TszKitLo40 TszKitLo40 requested review from hanfei1991 and removed request for a team December 17, 2020 05:24
@TszKitLo40 TszKitLo40 changed the title ddl, types: add M>=D checking for decimal column definition with default value ddl, planner, types: add M>=D checking for decimal column definition with default value Dec 17, 2020
@blacktear23
Copy link
Contributor

Do you think we should keep the logic I changed? I think we shouldn't ignore the err in convertToMysqlDecimal. @blacktear23

I think yes.

@@ -935,6 +935,9 @@ func checkColumn(colDef *ast.ColumnDef) error {
return types.ErrTooBigDisplayWidth.GenWithStackByArgs(colDef.Name.Name.O, mysql.MaxFloatingTypeWidth)
}
}
if tp.Flen < tp.Decimal {
Copy link
Contributor

@blacktear23 blacktear23 Dec 17, 2020

Choose a reason for hiding this comment

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

Below code should move into upper else block. float type can only provide Flen and the Decimal is -1. So current code could break f float(64).
Rest LGTM.

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.

ok, I have moved it into the upper else block.

@TszKitLo40
Copy link
Contributor Author

It seems this PR can also close #21063

Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

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

Rest LGTM

ddl/ddl_api.go Outdated
@@ -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.

types/datum.go Outdated
@@ -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

@AilinKid
Copy link
Contributor

Since we have processed in precessor.go, the rest code seems unnecessary?

@lysu lysu self-requested a review December 21, 2020 13:15
Copy link
Contributor

@AilinKid AilinKid left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-srebot ti-srebot added the status/LGT1 Indicates that a PR has LGTM 1. label Dec 24, 2020
Copy link
Member

@wjhuang2016 wjhuang2016 left a comment

Choose a reason for hiding this comment

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

LGTM

@ti-srebot ti-srebot removed the status/LGT1 Indicates that a PR has LGTM 1. label Dec 24, 2020
ti-srebot
ti-srebot previously approved these changes Dec 24, 2020
@ti-srebot ti-srebot added the status/LGT2 Indicates that a PR has LGTM 2. label Dec 24, 2020
@wjhuang2016
Copy link
Member

/merge

@ti-srebot
Copy link
Contributor

Your auto merge job has been accepted, waiting for:

  • 22036

@ti-srebot ti-srebot added the status/can-merge Indicates a PR has been approved by a committer. label Dec 29, 2020
@wjhuang2016
Copy link
Member

/merge

@ti-srebot
Copy link
Contributor

/run-all-tests

@ti-srebot
Copy link
Contributor

@TszKitLo40 merge failed.

@TszKitLo40
Copy link
Contributor Author

/run-unit-test

wjhuang2016
wjhuang2016 previously approved these changes Dec 29, 2020
@wjhuang2016
Copy link
Member

/merge

@ti-srebot
Copy link
Contributor

Your auto merge job has been accepted, waiting for:

  • 22039

@wjhuang2016
Copy link
Member

/merge

@ti-srebot
Copy link
Contributor

/run-all-tests

@wjhuang2016 wjhuang2016 merged commit 3e64072 into pingcap:master Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression sig/sql-infra SIG: SQL Infra status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ddl: add M>=D checking for decimal column definition with default value
6 participants