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: Timestamp literal with time zone offset #57845

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

Conversation

mjonss
Copy link
Contributor

@mjonss mjonss commented Nov 29, 2024

What problem does this PR solve?

Issue Number: close #51742

Problem Summary:
timestamp literal, like TIMESTAMP '2024-11-29 19:20:21' did not allow time zone offset, like TIMESTAMP '2024-11-29 19:20:21+08:00'

What changed and how does it work?

Adjusted the internal regex limiting allowed patterns for TIMESTAMP literals.

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.

Allow time zone offset for TIMESTAMP literal.

@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. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Nov 29, 2024
Copy link

tiprow bot commented Nov 29, 2024

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

@mjonss mjonss requested a review from dveeden November 29, 2024 18:49
Copy link

codecov bot commented Nov 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.4654%. Comparing base (b11d034) to head (dc7b358).
Report is 72 commits behind head on master.

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #57845        +/-   ##
================================================
+ Coverage   72.8126%   73.4654%   +0.6528%     
================================================
  Files          1677       1689        +12     
  Lines        463954     477205     +13251     
================================================
+ Hits         337817     350581     +12764     
- Misses       105277     105554       +277     
- Partials      20860      21070       +210     
Flag Coverage Δ
integration 43.2817% <100.0000%> (?)
unit 72.3259% <0.0000%> (+0.1110%) ⬆️

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

Components Coverage Δ
dumpling 52.6910% <ø> (-0.0764%) ⬇️
parser ∅ <ø> (∅)
br 43.6526% <ø> (-1.8496%) ⬇️

// (regardless if min/sec exists or not...)
`(\.\d*)?` +
// Optionally time zone offset, must be +/-HH:MM format
`([+-]\d{2}[:]\d{2})?` +
Copy link
Contributor Author

Choose a reason for hiding this comment

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

MySQL has this part very strict. It is more strict than what types.ParseTime parses the same string, so I think we should be more strict here as well, to avoid the situation where customer starts to use a more relaxed syntax, and we need to make it more strict.

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 agree with that

// Hour is mandatory
// Any number of leading zeroes
// 1-2 Hour digits
`0*\d{1,2}` +
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a fix to allow SELECT TIMESTAMP '2024-01-01 18'

// Any non-digit separator before Minute and Second parts
// Any number of leading zeroes in Min/Sec!
// 1-2 digit minutes/seconds
`([^\d]0*\d{1,2}){0,2}` +
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Another fix for allowing SELECT TIMESTAMP '2024-01-01 18[:00]'. SELECT TIMESTAMP '2024-01-01 18:00:00' did already work.

Copy link
Contributor

@dveeden dveeden left a comment

Choose a reason for hiding this comment

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

Should it also accept this with the ODBC syntax?

mysql-8.0.11-TiDB-v8.5.0-alpha-219-gecd70f4222> SELECT { ts '2024-01-01 14:00:00-14:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00-14:00' } |
+------------------------------------+
| 2024-01-02 05:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

Note that the MySQL behavior here is interesting:

mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00+00:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00+00:00' } |
+------------------------------------+
| 2024-01-01 14:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00-00:00' };
+---------------------------+
| 2024-01-01 14:00:00-00:00 |
+---------------------------+
| 2024-01-01 14:00:00-00:00 |
+---------------------------+
1 row in set (0.00 sec)

mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00+01:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00+01:00' } |
+------------------------------------+
| 2024-01-01 13:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00-01:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00-01:00' } |
+------------------------------------+
| 2024-01-01 15:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

@@ -67,7 +67,34 @@ var (
durationPattern = regexp.MustCompile(`^\s*[-]?(((\d{1,2}\s+)?0*\d{0,3}(:0*\d{1,2}){0,2})|(\d{1,7}))?(\.\d*)?\s*$`)

// timestampPattern checks whether a string matches the format of timestamp.
timestampPattern = regexp.MustCompile(`^\s*0*\d{1,4}([^\d]0*\d{1,2}){2}\s+(0*\d{0,2}([^\d]0*\d{1,2}){2})?(\.\d*)?\s*$`)
timestampPattern = regexp.MustCompile(`^` +
Copy link
Contributor

Choose a reason for hiding this comment

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

This commented multi line regexp is a big improvement!

// (regardless if min/sec exists or not...)
`(\.\d*)?` +
// Optionally time zone offset, must be +/-HH:MM format
`([+-]\d{2}[:]\d{2})?` +
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 agree with that

Copy link

ti-chi-bot bot commented Dec 2, 2024

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dveeden
Once this PR has been reviewed and has the lgtm label, please assign windtalker for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot ti-chi-bot bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label Dec 2, 2024
Copy link

ti-chi-bot bot commented Dec 2, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-12-02 16:35:28.072764413 +0000 UTC m=+1086315.692418928: ☑️ agreed by dveeden.

@dveeden
Copy link
Contributor

dveeden commented Dec 2, 2024

Should it also accept this with the ODBC syntax?

mysql-8.0.11-TiDB-v8.5.0-alpha-219-gecd70f4222> SELECT { ts '2024-01-01 14:00:00-14:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00-14:00' } |
+------------------------------------+
| 2024-01-02 05:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

Note that the MySQL behavior here is interesting:

mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00+00:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00+00:00' } |
+------------------------------------+
| 2024-01-01 14:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00-00:00' };
+---------------------------+
| 2024-01-01 14:00:00-00:00 |
+---------------------------+
| 2024-01-01 14:00:00-00:00 |
+---------------------------+
1 row in set (0.00 sec)

mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00+01:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00+01:00' } |
+------------------------------------+
| 2024-01-01 13:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00-01:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00-01:00' } |
+------------------------------------+
| 2024-01-01 15:00:00                |
+------------------------------------+
1 row in set (0.00 sec)
mysql-8.0.11-TiDB-v8.5.0-alpha-219-gecd70f4222> SELECT { ts '2024-01-01 14:00:00-00:00' };
ERROR 1292 (22007): Incorrect datetime value: '2024-01-01 14:00:00-00:00'
mysql-8.0.11-TiDB-v8.5.0-alpha-219-gecd70f4222> SELECT { ts '2024-01-01 14:00:00+00:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00+00:00' } |
+------------------------------------+
| 2024-01-01 15:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

mysql-8.0.11-TiDB-v8.5.0-alpha-219-gecd70f4222> SELECT { ts '2024-01-01 14:00:00+01:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00+01:00' } |
+------------------------------------+
| 2024-01-01 14:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

mysql-8.0.11-TiDB-v8.5.0-alpha-219-gecd70f4222> SELECT { ts '2024-01-01 14:00:00-01:00' };
+------------------------------------+
| { ts '2024-01-01 14:00:00-01:00' } |
+------------------------------------+
| 2024-01-01 16:00:00                |
+------------------------------------+
1 row in set (0.00 sec)

@mjonss
Copy link
Contributor Author

mjonss commented Dec 4, 2024

Should it also accept this with the ODBC syntax?
...
Note that the MySQL behavior here is interesting:

... 
mysql-9.1.0> SELECT { ts '2024-01-01 14:00:00-00:00' };
+---------------------------+
| 2024-01-01 14:00:00-00:00 |
+---------------------------+
| 2024-01-01 14:00:00-00:00 |
+---------------------------+
1 row in set (0.00 sec)
...
mysql-8.0.11-TiDB-v8.5.0-alpha-219-gecd70f4222> SELECT { ts '2024-01-01 14:00:00-00:00' };
ERROR 1292 (22007): Incorrect datetime value: '2024-01-01 14:00:00-00:00'

@dveeden I thinks this is due to MySQL falls back to string literal for ODBC syntax, in case it is not a proper timestamp literal:

mysql> SELECT { ts '2024-01-01 14:00:00-00:00' };
+---------------------------+
| 2024-01-01 14:00:00-00:00 |
+---------------------------+
| 2024-01-01 14:00:00-00:00 |
+---------------------------+
1 row in set (0.00 sec)

mysql> select timestamp'2024-01-01 14:00:00-00:00';
ERROR 1525 (HY000): Incorrect DATETIME value: '2024-01-01 14:00:00-00:00'
mysql> select version();
+-----------+
| version() |
+-----------+
| 9.0.1     |
+-----------+
1 row in set (0.00 sec)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-1-more-lgtm Indicates a PR needs 1 more LGTM. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support TIMESTAMP with explicit time zone
2 participants