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: Truncate FSP instead of rounding for utc_timestamp() | tidb-test=pr/2405 #56431

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

Conversation

mjonss
Copy link
Contributor

@mjonss mjonss commented Oct 1, 2024

What problem does this PR solve?

Issue Number: close #56430 , close #31938

Problem Summary:
utc_timestamp() was rounding instead of truncating.

What changed and how does it work?

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.

Changing CURTIME(), SYSDATE() and UTC_TIMESTAMP() to truncate the FSP part instead of rounding it, so it is compatible with MySQL and in-line with NOW().

@ti-chi-bot ti-chi-bot bot added do-not-merge/needs-triage-completed release-note-none Denotes a PR that doesn't merit a release note. size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 1, 2024
Copy link

tiprow bot commented Oct 1, 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.

Copy link

codecov bot commented Oct 1, 2024

Codecov Report

Attention: Patch coverage is 85.41667% with 7 lines in your changes missing coverage. Please review.

Project coverage is 75.3508%. Comparing base (74034d4) to head (2400cf9).

Additional details and impacted files
@@               Coverage Diff                @@
##             master     #56431        +/-   ##
================================================
+ Coverage   73.3645%   75.3508%   +1.9862%     
================================================
  Files          1624       1624                
  Lines        448069     448161        +92     
================================================
+ Hits         328724     337693      +8969     
+ Misses        99207      89904      -9303     
- Partials      20138      20564       +426     
Flag Coverage Δ
integration 48.8092% <43.7500%> (?)
unit 72.4688% <85.4166%> (+0.0051%) ⬆️

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

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

@mjonss
Copy link
Contributor Author

mjonss commented Oct 1, 2024

Also sysdate() is affected:

tidb> select now(2), now(6), utc_timestamp(2), utc_timestamp(6), sysdate(2), sysdate(6);
+------------------------+----------------------------+------------------------+----------------------------+------------------------+----------------------------+
| now(2)                 | now(6)                     | utc_timestamp(2)       | utc_timestamp(6)           | sysdate(2)             | sysdate(6)                 |
+------------------------+----------------------------+------------------------+----------------------------+------------------------+----------------------------+
| 2024-10-01 18:34:39.11 | 2024-10-01 18:34:39.117223 | 2024-10-01 16:34:39.12 | 2024-10-01 16:34:39.117224 | 2024-10-01 18:34:39.12 | 2024-10-01 18:34:39.117387 |
+------------------------+----------------------------+------------------------+----------------------------+------------------------+----------------------------+

vs MySQL 8.0.32:

mysql> select now(2), now(6), utc_timestamp(2), utc_timestamp(6), sysdate(2), sysdate(6);
+------------------------+----------------------------+------------------------+----------------------------+------------------------+----------------------------+
| now(2)                 | now(6)                     | utc_timestamp(2)       | utc_timestamp(6)           | sysdate(2)             | sysdate(6)                 |
+------------------------+----------------------------+------------------------+----------------------------+------------------------+----------------------------+
| 2024-10-01 18:35:08.18 | 2024-10-01 18:35:08.186503 | 2024-10-01 16:35:08.18 | 2024-10-01 16:35:08.186503 | 2024-10-01 18:35:08.18 | 2024-10-01 18:35:08.186681 |
+------------------------+----------------------------+------------------------+----------------------------+------------------------+----------------------------+

@ti-chi-bot ti-chi-bot bot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/XS Denotes a PR that changes 0-9 lines, ignoring generated files. labels Oct 1, 2024
@dveeden
Copy link
Contributor

dveeden commented Oct 2, 2024

/test build

Copy link

ti-chi-bot bot commented Oct 2, 2024

[LGTM Timeline notifier]

Timeline:

  • 2024-10-02 06:46:25.744486953 +0000 UTC m=+423741.164699963: ☑️ agreed by dveeden.

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

tiprow bot commented Oct 2, 2024

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

In response to this:

/test build

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.

@dveeden
Copy link
Contributor

dveeden commented Oct 2, 2024

/check-issue-triage-complete

@dveeden
Copy link
Contributor

dveeden commented Oct 2, 2024

/ok-to-test

@ti-chi-bot ti-chi-bot bot added the ok-to-test Indicates a PR is ready to be tested. label Oct 2, 2024
@dveeden
Copy link
Contributor

dveeden commented Oct 2, 2024

I think this does need a release note

@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 2, 2024
@mjonss
Copy link
Contributor Author

mjonss commented Oct 2, 2024

/retest

mjonss added a commit to mjonss/tidb that referenced this pull request Oct 3, 2024
@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 3, 2024
tz = gotime.Local
}

func (d Duration) RoundFrac(fsp int) (Duration, error) {
Copy link
Contributor Author

@mjonss mjonss Oct 3, 2024

Choose a reason for hiding this comment

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

Duration should not use gotime.Time for rounding or truncating. There is simply no need or benefit to convert back and forth.

@mjonss mjonss changed the title expression: Truncate FSP instead of rounding for utc_timestamp() expression: Truncate FSP instead of rounding for utc_timestamp() | tidb-test=pr/2405 Oct 3, 2024
@mjonss
Copy link
Contributor Author

mjonss commented Oct 3, 2024

/retest

@mjonss mjonss requested a review from dveeden October 3, 2024 21:51
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.

So this is not yet fixing SYSDATE() right?

sql> select utc_timestamp(6), utc_timestamp(), now(), now(6), utc_timestamp(6), utc_timestamp(), now(1), now(3), utc_timestamp(1), utc_timestamp(3), now(1), now(3), utc_timestamp(6)+0, now(6)+0, sysdate(6), current_timestamp(6), unix_timestamp(now(6)), unix_timestamp(sysdate(6)), from_unixtime(unix_timestamp(now(6))), microsecond(now(6))\G
*************************** 1. row ***************************
                     utc_timestamp(6): 2024-10-04 08:31:43.397413
                      utc_timestamp(): 2024-10-04 08:31:43
                                now(): 2024-10-04 10:31:43
                               now(6): 2024-10-04 10:31:43.397413
                     utc_timestamp(6): 2024-10-04 08:31:43.397413
                      utc_timestamp(): 2024-10-04 08:31:43
                               now(1): 2024-10-04 10:31:43.3
                               now(3): 2024-10-04 10:31:43.397
                     utc_timestamp(1): 2024-10-04 08:31:43.3
                     utc_timestamp(3): 2024-10-04 08:31:43.397
                               now(1): 2024-10-04 10:31:43.3
                               now(3): 2024-10-04 10:31:43.397
                   utc_timestamp(6)+0: 20241004083143.397413
                             now(6)+0: 20241004103143.397413
                           sysdate(6): 2024-10-04 10:31:43.397786
                 current_timestamp(6): 2024-10-04 10:31:43.397413
               unix_timestamp(now(6)): 1728030703.397413
           unix_timestamp(sysdate(6)): 1728030703.397790
from_unixtime(unix_timestamp(now(6))): 2024-10-04 10:31:43.397413
                  microsecond(now(6)): 397413
1 row in set (0.0009 sec)

Maybe add some tests in tests/integrationtest

Copy link

ti-chi-bot bot commented Oct 4, 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 xuhuaiyu 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

Copy link

ti-chi-bot bot commented Oct 4, 2024

@dveeden: Your lgtm message is repeated, so it is ignored.

In response to this:

So this is not yet fixing SYSDATE() right?

sql> select utc_timestamp(6), utc_timestamp(), now(), now(6), utc_timestamp(6), utc_timestamp(), now(1), now(3), utc_timestamp(1), utc_timestamp(3), now(1), now(3), utc_timestamp(6)+0, now(6)+0, sysdate(6), current_timestamp(6), unix_timestamp(now(6)), unix_timestamp(sysdate(6)), from_unixtime(unix_timestamp(now(6))), microsecond(now(6))\G
*************************** 1. row ***************************
                    utc_timestamp(6): 2024-10-04 08:31:43.397413
                     utc_timestamp(): 2024-10-04 08:31:43
                               now(): 2024-10-04 10:31:43
                              now(6): 2024-10-04 10:31:43.397413
                    utc_timestamp(6): 2024-10-04 08:31:43.397413
                     utc_timestamp(): 2024-10-04 08:31:43
                              now(1): 2024-10-04 10:31:43.3
                              now(3): 2024-10-04 10:31:43.397
                    utc_timestamp(1): 2024-10-04 08:31:43.3
                    utc_timestamp(3): 2024-10-04 08:31:43.397
                              now(1): 2024-10-04 10:31:43.3
                              now(3): 2024-10-04 10:31:43.397
                  utc_timestamp(6)+0: 20241004083143.397413
                            now(6)+0: 20241004103143.397413
                          sysdate(6): 2024-10-04 10:31:43.397786
                current_timestamp(6): 2024-10-04 10:31:43.397413
              unix_timestamp(now(6)): 1728030703.397413
          unix_timestamp(sysdate(6)): 1728030703.397790
from_unixtime(unix_timestamp(now(6))): 2024-10-04 10:31:43.397413
                 microsecond(now(6)): 397413
1 row in set (0.0009 sec)

Maybe add some tests in tests/integrationtest

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.

@mjonss
Copy link
Contributor Author

mjonss commented Oct 4, 2024

So this is not yet fixing SYSDATE() right?

It should fix SYSDATE() as well, where do you see it is not fixed?

tidb> select sysdate(6)+0, sysdate(3)+0, sysdate(), sysdate(3), sysdate(6), current_timestamp(6),  unix_timestamp(sysdate(3)), unix_timestamp(sysdate(6)\G
*************************** 1. row ***************************
                         sysdate(6)+0: 20241004121402.971827
                         sysdate(3)+0: 20241004121402.971
                            sysdate(): 2024-10-04 12:14:02
                           sysdate(3): 2024-10-04 12:14:02.971
                           sysdate(6): 2024-10-04 12:14:02.971842
                 current_timestamp(6): 2024-10-04 12:14:02.971174
           unix_timestamp(sysdate(3)): 1728036842.971
           unix_timestamp(sysdate(6)): 1728036842.971851

Here you see that both fsp == 0 and fsp == 3 is truncating instead of rounding.

Maybe add some tests in tests/integrationtest

It is tested here, doing it in an integration test is harder, since SYSDATE() is evaluated every time, using the current wall clock time, not the start of the statement as NOW(), CURRENT_TIMESTAMP() etc.

@dveeden
Copy link
Contributor

dveeden commented Oct 4, 2024

It should fix SYSDATE() as well, where do you see it is not fixed?

Please ignore my earlier comment. SYSDATE() is working correctly.

@dveeden
Copy link
Contributor

dveeden commented Oct 4, 2024

https://github.com/mysql/mysql-server/blob/trunk/mysql-test/t/func_time.test seems to use SET TIMESTAMP=... for testing. But it seems like TiDB doesn't support that?

@mjonss
Copy link
Contributor Author

mjonss commented Oct 7, 2024

https://github.com/mysql/mysql-server/blob/trunk/mysql-test/t/func_time.test seems to use SET TIMESTAMP=... for testing. But it seems like TiDB doesn't support that?

TiDB also supports SET TIMESTAMP=<unix timestamp, possibly with fsp>

@dveeden
Copy link
Contributor

dveeden commented Oct 7, 2024

https://github.com/mysql/mysql-server/blob/trunk/mysql-test/t/func_time.test seems to use SET TIMESTAMP=... for testing. But it seems like TiDB doesn't support that?

TiDB also supports SET TIMESTAMP=<unix timestamp, possibly with fsp>

I guess I must have made a typo. I tried again with TiDB and it indeed works.

@mjonss mjonss requested a review from XuHuaiyu October 15, 2024 11:35
Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

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

Please improve the description and title of the PR. This commit not only modifies the behavior of the utc_timestamp method.

// fractional seconds part, returns the duration type Time value and bool to indicate
// whether the result is null. It also truncates FSP part instead of rounding it as
// ParseDuration above does.
func ParseDurationTruncateFsp(ctx Context, str string, fsp int) (Duration, bool, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to add a test for this?

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. 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
3 participants