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

expression: fix errors set utc_timestamp precision | tidb-test=pr/2406 #56453

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

chagelo
Copy link

@chagelo chagelo commented Oct 5, 2024

What problem does this PR solve?

Issue Number: close #56451

Problem Summary:

What changed and how does it work?

Correct the error when set invalid precision for utc_timestamp

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No need to test
    • I checked and no code files have been changed.

Side effects

  • Performance regression: Consumes more CPU
  • Performance regression: Consumes more Memory
  • Breaking backward compatibility

Documentation

  • Affects user behaviors
  • Contains syntax changes
  • Contains variable changes
  • Contains experimental features
  • Changes MySQL compatibility

Release note

Please refer to Release Notes Language Style Guide to write a quality release note.

Fix errors when set invalid precision of date or time related functions, such as too big precision.

@ti-chi-bot ti-chi-bot bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note-none Denotes a PR that doesn't merit a release note. do-not-merge/needs-triage-completed labels Oct 5, 2024
@sre-bot
Copy link
Contributor

sre-bot commented Oct 5, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

ti-chi-bot bot commented Oct 5, 2024

Welcome @chagelo!

It looks like this is your first PR to pingcap/tidb 🎉.

I'm the bot to help you request reviewers, add labels and more, See available commands.

We want to make sure your contribution gets all the attention it needs!



Thank you, and welcome to pingcap/tidb. 😃

@ti-chi-bot ti-chi-bot bot added needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 5, 2024
Copy link

ti-chi-bot bot commented Oct 5, 2024

Hi @chagelo. Thanks for your PR.

I'm waiting for a pingcap member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

tiprow bot commented Oct 5, 2024

Hi @chagelo. Thanks for your PR.

PRs from untrusted users cannot be marked as trusted with /ok-to-test in this repo meaning untrusted PR authors can never trigger tests themselves. Collaborators can still trigger tests on the PR using /test all.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@chagelo
Copy link
Author

chagelo commented Oct 5, 2024

/ok-to-test

Copy link

ti-chi-bot bot commented Oct 5, 2024

@chagelo: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/ok-to-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

Copy link

tiprow bot commented Oct 5, 2024

@chagelo: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/ok-to-test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@mjonss
Copy link
Contributor

mjonss commented Oct 5, 2024

/ok-to-test

@ti-chi-bot ti-chi-bot bot added ok-to-test Indicates a PR is ready to be tested. and removed needs-ok-to-test Indicates a PR created by contributors and need ORG member send '/ok-to-test' to start testing. labels Oct 5, 2024
Copy link
Contributor

@mjonss mjonss left a comment

Choose a reason for hiding this comment

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

Looks good, just missing the statement in the .test file.

tests/integrationtest/r/expression/builtin.result Outdated Show resolved Hide resolved
Copy link

codecov bot commented Oct 5, 2024

Codecov Report

Attention: Patch coverage is 38.46154% with 24 lines in your changes missing coverage. Please review.

Project coverage is 56.3106%. Comparing base (e017e1b) to head (cb64e84).
Report is 17 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #56453         +/-   ##
=================================================
- Coverage   73.3414%   56.3106%   -17.0309%     
=================================================
  Files          1629       1753        +124     
  Lines        449976     633500     +183524     
=================================================
+ Hits         330019     356728      +26709     
- Misses        99734     252786     +153052     
- Partials      20223      23986       +3763     
Flag Coverage Δ
integration 36.8174% <30.7692%> (?)
unit 72.4452% <38.4615%> (-0.0277%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
dumpling 52.9478% <ø> (ø)
parser ∅ <ø> (∅)
br 52.3614% <ø> (+6.7983%) ⬆️

@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Oct 5, 2024
@chagelo chagelo force-pushed the fix-precision-error-message branch 2 times, most recently from 353bffc to 0229b66 Compare October 5, 2024 17:10
@chagelo
Copy link
Author

chagelo commented Oct 5, 2024

Thank you very much for the suggestions!

And I'm gonna fix the rest errors tomorrow.😀

@chagelo chagelo marked this pull request as ready for review October 5, 2024 17:15
@ti-chi-bot ti-chi-bot bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 5, 2024
@mjonss mjonss changed the title pkg/expression: fix errors set utc_timestamp precision expression: fix errors set utc_timestamp precision | tidb-test=pr/2406 Oct 5, 2024
Copy link
Contributor

@mjonss mjonss left a comment

Choose a reason for hiding this comment

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

Looks good to me!

I have re-recorded the test results for the failing CI test idc-jenkins-ci-tidb/mysql-test, which is a private repository for various integration tests and mysql compatibility tests. so hopefully the tests will pass.

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Oct 5, 2024
@mjonss
Copy link
Contributor

mjonss commented Oct 5, 2024

/retest

1 similar comment
@mjonss
Copy link
Contributor

mjonss commented Oct 5, 2024

/retest

@mjonss
Copy link
Contributor

mjonss commented Oct 5, 2024

/run-check-issue-triage-complete

@ti-chi-bot ti-chi-bot bot added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Oct 7, 2024
Copy link

ti-chi-bot bot commented Oct 7, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-10-05 23:47:21.004009259 +0000 UTC m=+744196.424222285: ☑️ agreed by mjonss.
  • 2024-10-07 05:32:37.699472883 +0000 UTC m=+851313.119685895: ☑️ agreed by dveeden.

@dveeden
Copy link
Contributor

dveeden commented Oct 7, 2024

There are a few places that check for a fsp that's less than MinFsp. But looks like there is no way to reach that error as the parser is not allowing this.

        } else if fsp > int64(types.MaxFsp) {
                return types.ZeroTime, true, types.ErrTooBigPrecision.GenWithStackByArgs(fsp, "now", types.MaxFsp)
        } else if fsp < int64(types.MinFsp) {
                return types.ZeroTime, true, errors.Errorf("Invalid negative %d specified, must in [0, 6]", fsp)
        }

Maybe:

  • Remove the check for fsp < MinFsp
  • Add a comment to indicate that this should never be reached and/or change to message to indicate this
  • Add tests for things like NOW(-1) to ensure it gets rejected by the parser

Side note: Looks like there is a MySQL bug in the error message that we don't follow:

MySQL 9.0.1:

sql> SELECT NOW(99999);
ERROR: 1426 (42000): Too-big precision 159 specified for 'now'. Maximum is 6.

TiDB:

sql> SELECT NOW(99999);
ERROR: 1426 (42000): Too-big precision 99999 specified for 'now'. Maximum is 6.

@dveeden
Copy link
Contributor

dveeden commented Oct 7, 2024

For the bug in MySQL:
https://bugs.mysql.com/bug.php?id=116307

@dveeden
Copy link
Contributor

dveeden commented Oct 7, 2024

Inspired by the MySQL bug it looks like we have a similar bug (but way less severe). This also answers my question about negative fsp's somewhat:

sql> SELECT SYSDATE(999999999999999999);
ERROR: 1426 (42000): Too-big precision 999999999999999999 specified for 'sysdate'. Maximum is 6.

sql> SELECT SYSDATE(9999999999999999999);
ERROR: 1105 (HY000): Invalid negative -8446744073709551617 specified, must in [0, 6]

@chagelo
Copy link
Author

chagelo commented Oct 7, 2024

  1. For MySQL, it uses the last byte in binary representation as the precision, so SELECT NOW(257) equals to SELECT NOW(1).
  2. And if precision has more than 32 bits like 2147483647 + 1, it return a SQL syntax error.
  3. I'll add a comment.

@chagelo chagelo force-pushed the fix-precision-error-message branch from 8f0e846 to 52dbcb0 Compare October 7, 2024 08:58
@ti-chi-bot ti-chi-bot bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Oct 7, 2024
@chagelo chagelo force-pushed the fix-precision-error-message branch from 52dbcb0 to a2b0d7b Compare October 7, 2024 09:17
@dveeden
Copy link
Contributor

dveeden commented Oct 7, 2024

MariaDB seems to be more strict than MySQL here. Looks like MariaDB accepts larger numbers in the parser but checks them later on.

mysql-11.4.2-MariaDB-ubu2404> SELECT NOW(9999999999999999999);
ERROR 1426 (42000): Too big precision specified for 'current_timestamp'. Maximum is 6
mysql-11.4.2-MariaDB-ubu2404> SELECT NOW(99999999999999999999);
ERROR 1064 (42000): Only integers allowed as number here near '99999999999999999999)' at line 1

@chagelo
Copy link
Author

chagelo commented Oct 8, 2024

/retest

@dveeden
Copy link
Contributor

dveeden commented Oct 11, 2024

@chagelo are the tests failing for something related to this PR?

@chagelo
Copy link
Author

chagelo commented Oct 13, 2024

@dveeden yes, it is.

  1. Because MySQL has the bug, I don't know what should I do to make it be compatible with MySQL.
  2. I read the parser code. I want parser to limit precision's type like what MySQL does, but I don't understand the yyParseTab, it seems complicated.
  3. It seems not good to throw a syntax error in pkg/expression/builtin_time.go(like negative or too long number precision), it should done by parser, it's right? I'm not sure about this.

@dveeden
Copy link
Contributor

dveeden commented Oct 14, 2024

@dveeden yes, it is.

  1. Because MySQL has the bug, I don't know what should I do to make it be compatible with MySQL.

I don't think we should follow the behavior of https://bugs.mysql.com/bug.php?id=116307 as I don't think people are relying on this behavior or should rely on this behavior.

  1. I read the parser code. I want parser to limit precision's type like what MySQL does, but I don't understand the yyParseTab, it seems complicated.

yyParseTab is in pkg/parser/parser.go which is generated by goyacc based on pkg/parser/parser.y, so you need to ecit parser.y and run make parser.

  1. It seems not good to throw a syntax error in pkg/expression/builtin_time.go(like negative or too long number precision), it should done by parser, it's right? I'm not sure about this.

I'm not sure. One of the issues with having the parser deal with things is that it might not have the right context to fill in the fields of the error message (e.g. the name of the table or the name of the function).

@dveeden
Copy link
Contributor

dveeden commented Oct 14, 2024

From the issue description:

  • error code is different
  • sqlstate is different
  • function name is not correct.

It looks like these are all fixed.

The following might be seen as a separate bug if it is not a regression:

mysql-8.0.11-TiDB-v8.4.0-alpha-326-ga2b0d7b115> SELECT UTC_TIMESTAMP(9990);
+----------------------------+
| UTC_TIMESTAMP(9990)        |
+----------------------------+
| 2024-10-14 07:12:01.054447 |
+----------------------------+
1 row in set (0.00 sec)

Note that this is somewhat similar to #46372 (comment)

@chagelo chagelo force-pushed the fix-precision-error-message branch 3 times, most recently from a2b0d7b to 33af583 Compare October 14, 2024 13:31
@chagelo
Copy link
Author

chagelo commented Oct 14, 2024

/retest

@chagelo
Copy link
Author

chagelo commented Oct 14, 2024

SELECT CURRENT_TIME(-1);
Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 1 column 21 near "-1);" 
SELECT CURRENT_TIME(2147483648);
Error 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use

It seems TiDB parser handles the negative precision, but not resolve the overlong number precision, parser should have dealt with this, because this error contains some context.

@chagelo
Copy link
Author

chagelo commented Oct 16, 2024

@dveeden Required review. Thanks.

@ti-chi-bot ti-chi-bot bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. and removed release-note-none Denotes a PR that doesn't merit a release note. labels Oct 17, 2024
@chagelo chagelo requested a review from dveeden October 17, 2024 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm ok-to-test Indicates a PR is ready to be tested. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

UTC_TIMESTAMP(7) gives different error code and message from MySQL
4 participants