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: Improve the performance of str_to_date #25389

Merged
merged 9 commits into from
Aug 16, 2021

Conversation

JaySon-Huang
Copy link
Contributor

@JaySon-Huang JaySon-Huang commented Jun 13, 2021

What problem does this PR solve?

Issue Number: related to #24928

Problem Summary:
While parsing parts for StrToDate, we first match the length of the digit by regex then parsing the number by strconv.ParseUint. The regex matching is costly and somehow useless, we can rewrite parseDigits to reduce the cost of parsing.

What is changed and how it works?

Depending on #25386, should merge that PR first.

Use parseNDigits instead of matching length by oneOrTwoDigitRegex then parsing number by strconv.ParseUint.

Test the performance by go test -run=^$ -bench=BenchmarkStrToDate -count=5 -v

>  benchstat old.txt new.txt
name                                        old time/op  new time/op  delta
StrToDate/strToDate_yyyyMMdd_hhmmss_ffff-8  1.79µs ± 4%  0.85µs ± 3%  -52.55%  (p=0.008 n=5+5)
StrToDate/strToDate_%r_ddMMyyyy-8           1.21µs ±15%  0.47µs ± 1%  -61.23%  (p=0.008 n=5+5)
StrToDate/strToDate_%T_ddMMyyyy-8           1.18µs ± 2%  0.48µs ± 8%  -59.56%  (p=0.008 n=5+5)

Related changes

  • PR to update pingcap/docs/pingcap/docs-cn:
  • Need to cherry-pick to the release branch

Check List

Tests

  • Unit test

Side effects

  • N/A

Release note

  • Improve the performance of str_to_date

@JaySon-Huang JaySon-Huang requested a review from a team as a code owner June 13, 2021 19:58
@JaySon-Huang JaySon-Huang requested review from qw4990 and removed request for a team June 13, 2021 19:58
@ti-chi-bot ti-chi-bot added the size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. label Jun 13, 2021
@JaySon-Huang
Copy link
Contributor Author

/run-check_dev_2

@qw4990 qw4990 requested review from XuHuaiyu and wshwsh12 and removed request for qw4990 June 21, 2021 03:30
@wshwsh12
Copy link
Contributor

This pr is depending on #25386, so I cancel the review request now. After that pr merged, feel free to re-request review again.

@JaySon-Huang JaySon-Huang marked this pull request as draft June 22, 2021 09:41
@ti-chi-bot ti-chi-bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 22, 2021
@ti-chi-bot ti-chi-bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. labels Jun 25, 2021
@JaySon-Huang JaySon-Huang marked this pull request as ready for review June 25, 2021 12:40
@ti-chi-bot ti-chi-bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jun 25, 2021
@JaySon-Huang
Copy link
Contributor Author

@wshwsh12 PTAL if you are free

}

func dayOfYearThreeDigits(t *CoreTime, input string, ctx map[string]int) (string, bool) {
v, succ := parseDigits(input, 3)
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems you have changed the behavior of dayOfYearThreeDigits.
Please add some test case that len(input) < 3, and compare the result with mysql.

Copy link
Contributor Author

@JaySon-Huang JaySon-Huang Jul 1, 2021

Choose a reason for hiding this comment

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

MySQL declares that "%j" should be "Day of year (001..366)"[1]. But actually, it accepts a number that is up to three digits[2]. (The same with "%d", "Day of the month, numeric (00..31)" but still accept one digit as the day).
TiDB add parsing function dayOfYearThreeDigits for '%j', but actually it doesn't support parsing format with '%j' [3]. So it is not easy to add cases for testing this function by testing StrToDate.
Instead of adding cases for 'dayOfYearThreeDigits', I think I will add some test cases for parseNDigits, later.

  • [1] https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_date-format
  • [2] Some result for '%j' in MySQL 5.7
    > select date, format, str_to_date(date, format) as str_to_date from a;
    +-----------+--------+----------------------------+
    | date      | format | str_to_date                |
    +-----------+--------+----------------------------+
    | 015 2021  | %j %Y  | 2021-01-15 00:00:00.000000 |
    | 15 2021   | %j %Y  | 2021-01-15 00:00:00.000000 |
    | 9 2021    | %j %Y  | 2021-01-09 00:00:00.000000 |
    | 900 2021  | %j %Y  | 2023-06-19 00:00:00.000000 |
    | 999 2021  | %j %Y  | 2023-09-26 00:00:00.000000 |
    | 1000 2021 | %j %Y  | 2000-04-09 00:00:00.000000 | -- don't know why, need further investigation
    | 9999 2021 | %j %Y  | 2011-09-26 00:00:00.000000 | -- don't know why, need further investigation
    +-----------+--------+----------------------------+
    > select version();
    +-------------------------+
    | version()               |
    +-------------------------+
    | 5.7.33-0ubuntu0.16.04.1 |
    +-------------------------+
    
  • [3]

    tidb/types/time.go

    Lines 2752 to 2756 in 2093349

    // Key of the ctx is the format char, such as `%j` `%p` and so on.
    if yearOfDay, ok := ctx["%j"]; ok {
    // TODO: Implement the function that converts day of year to yy:mm:dd.
    _ = yearOfDay
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of adding cases for 'dayOfYearThreeDigits', I think I will add some test cases for parseNDigits

Find it hard to add test cases for the private method parseNDigits, add more test cases for StrToDate instead.

Copy link
Contributor

@wshwsh12 wshwsh12 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-chi-bot
Copy link
Member

ti-chi-bot commented Jul 1, 2021

[REVIEW NOTIFICATION]

This pull request has been approved by:

  • guo-shaoge
  • wshwsh12

To complete the pull request process, please ask the reviewers in the list to review by filling /cc @reviewer in the comment.
After your PR has acquired the required number of LGTMs, you can assign this pull request to the committer in the list by filling /assign @committer in the comment to help you merge this pull request.

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

Reviewer can indicate their review by submitting an approval review.
Reviewer can cancel approval by submitting a request changes review.

@ti-chi-bot ti-chi-bot added the status/LGT1 Indicates that a PR has LGTM 1. label Jul 1, 2021
@ti-chi-bot ti-chi-bot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 8, 2021
@ti-chi-bot ti-chi-bot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 9, 2021
JaySon-Huang and others added 3 commits July 9, 2021 13:42
Signed-off-by: JaySon-Huang <tshent@qq.com>
Signed-off-by: JaySon-Huang <tshent@qq.com>
Signed-off-by: JaySon-Huang <jayson.hjs@gmail.com>
@JaySon-Huang
Copy link
Contributor Author

JaySon-Huang commented Jul 19, 2021

@wshwsh12 Could you find another reviewer to continue to review this PR when they have spare time? It has not been active for several weeks.

@wshwsh12
Copy link
Contributor

/cc @guo-shaoge

@wzru wzru added the type/enhancement The issue or PR belongs to an enhancement. label Aug 13, 2021
@guo-shaoge
Copy link
Collaborator

Will review this today.

@ti-chi-bot ti-chi-bot added status/LGT2 Indicates that a PR has LGTM 2. and removed status/LGT1 Indicates that a PR has LGTM 1. labels Aug 16, 2021
@JaySon-Huang
Copy link
Contributor Author

/merge

@ti-chi-bot
Copy link
Member

This pull request has been accepted and is ready to merge.

Commit hash: f7f33e8

@ti-chi-bot ti-chi-bot added the status/can-merge Indicates a PR has been approved by a committer. label Aug 16, 2021
@ti-chi-bot
Copy link
Member

@JaySon-Huang: Your PR was out of date, I have automatically updated it for you.

At the same time I will also trigger all tests for you:

/run-all-tests

If the CI test fails, you just re-trigger the test that failed and the bot will merge the PR for you after the CI passes.

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 ti-community-infra/tichi repository.

@JaySon-Huang
Copy link
Contributor Author

/run-unit-test

@JaySon-Huang
Copy link
Contributor Author

/run_check_dev_2

@JaySon-Huang
Copy link
Contributor Author

/run-check_dev_2

@ti-chi-bot ti-chi-bot merged commit 36ed093 into pingcap:master Aug 16, 2021
@JaySon-Huang JaySon-Huang deleted the improve_str_to_date_perf branch August 16, 2021 05:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/expression size/L Denotes a PR that changes 100-499 lines, ignoring generated files. status/can-merge Indicates a PR has been approved by a committer. status/LGT2 Indicates that a PR has LGTM 2. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants