tidb icon indicating copy to clipboard operation
tidb copied to clipboard

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

Open chagelo opened this issue 1 year ago • 30 comments

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
  • [x] 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.

chagelo avatar Oct 05 '24 10:10 chagelo

CLA assistant check
All committers have signed the CLA.

sre-bot avatar Oct 05 '24 10:10 sre-bot

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. :smiley:

ti-chi-bot[bot] avatar Oct 05 '24 10:10 ti-chi-bot[bot]

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.

ti-chi-bot[bot] avatar Oct 05 '24 10:10 ti-chi-bot[bot]

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.

tiprow[bot] avatar Oct 05 '24 10:10 tiprow[bot]

/ok-to-test

chagelo avatar Oct 05 '24 10:10 chagelo

@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.

ti-chi-bot[bot] avatar Oct 05 '24 10:10 ti-chi-bot[bot]

@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.

tiprow[bot] avatar Oct 05 '24 10:10 tiprow[bot]

/ok-to-test

mjonss avatar Oct 05 '24 14:10 mjonss

Codecov Report

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

Project coverage is 56.7738%. Comparing base (64c8d31) to head (ec62aa2). Report is 8 commits behind head on master.

Additional details and impacted files
@@                Coverage Diff                @@
##             master     #56453         +/-   ##
=================================================
- Coverage   73.3322%   56.7738%   -16.5584%     
=================================================
  Files          1631       1784        +153     
  Lines        451087     641505     +190418     
=================================================
+ Hits         330792     364207      +33415     
- Misses        99995     252640     +152645     
- Partials      20300      24658       +4358     
Flag Coverage Δ
integration 38.1786% <30.7692%> (?)
unit 72.7218% <38.4615%> (+0.2572%) :arrow_up:

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

Components Coverage Δ
dumpling 52.9478% <ø> (ø)
parser ∅ <ø> (∅)
br 52.7013% <ø> (+6.6687%) :arrow_up:

codecov[bot] avatar Oct 05 '24 15:10 codecov[bot]

Thank you very much for the suggestions!

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

chagelo avatar Oct 05 '24 17:10 chagelo

/retest

mjonss avatar Oct 05 '24 23:10 mjonss

/retest

mjonss avatar Oct 05 '24 23:10 mjonss

/run-check-issue-triage-complete

mjonss avatar Oct 05 '24 23:10 mjonss

@chagelo I will look for reviewers that have code ownership for expression and parser, so this can be fully approved and merged, but it may take a few days, due to weekend and holidays.

Thank you again for your contribution!

mjonss avatar Oct 06 '24 00:10 mjonss

Thank you as well! 😁

chagelo avatar Oct 06 '24 01:10 chagelo

[LGTM Timeline notifier]

Timeline:

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

ti-chi-bot[bot] avatar Oct 07 '24 05:10 ti-chi-bot[bot]

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 avatar Oct 07 '24 05:10 dveeden

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

dveeden avatar Oct 07 '24 05:10 dveeden

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]

dveeden avatar Oct 07 '24 06:10 dveeden

  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 avatar Oct 07 '24 07:10 chagelo

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

dveeden avatar Oct 07 '24 09:10 dveeden

/retest

chagelo avatar Oct 08 '24 06:10 chagelo

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

dveeden avatar Oct 11 '24 09:10 dveeden

@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.

chagelo avatar Oct 13 '24 01:10 chagelo

@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 avatar Oct 14 '24 07:10 dveeden

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 https://github.com/pingcap/tidb/issues/46372#issuecomment-2405559661

dveeden avatar Oct 14 '24 07:10 dveeden

/retest

chagelo avatar Oct 14 '24 16:10 chagelo

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 avatar Oct 14 '24 16:10 chagelo

@dveeden Required review. Thanks.

chagelo avatar Oct 16 '24 16:10 chagelo

/retest

chagelo avatar Oct 21 '24 10:10 chagelo