Skip to content

[Enhancement]Enhance datetime format #39986

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

Merged
merged 3 commits into from
Jan 30, 2024
Merged

[Enhancement]Enhance datetime format #39986

merged 3 commits into from
Jan 30, 2024

Conversation

leoyy0316
Copy link
Contributor

@leoyy0316 leoyy0316 commented Jan 25, 2024

Why I'm doing:

when iceberg table partition filed is timestamp and partitions value second is 0 , starrocks will throw [date literal [2024-01-24T10:10] is invalid] exception.Because starrocks DATETIME_FORMATTERS not support format [%Y-%m-%e %H:%i].

What I'm doing:

Enhance datetime format

Fixes #issue

What type of PR is this:

  • BugFix
  • Feature
  • Enhancement
  • Refactor
  • UT
  • Doc
  • Tool

Does this PR entail a change in behavior?

  • Yes, this PR will result in a change in behavior.
  • No, this PR will not result in a change in behavior.

If yes, please specify the type of change:

  • Interface/UI changes: syntax, type conversion, expression evaluation, display information
  • Parameter changes: default values, similar parameters but with different default values
  • Policy changes: use new policy to replace old one, functionality automatically enabled
  • Feature removed
  • Miscellaneous: upgrade & downgrade compatibility, etc.

Checklist:

  • I have added test cases for my bug fix or my new feature
  • This pr needs user documentation (for new or modified features or behaviors)
    • I have added documentation for my new feature or new function
  • This is a backport pr

Bugfix cherry-pick branch check:

  • I have checked the version labels which the pr will be auto-backported to the target branch
    • 3.2
    • 3.1
    • 3.0
    • 2.5

int withMs = str.contains(".") ? 1 : 0;
int withSplitT = str.contains("T") ? 1 : 0;
DateTimeFormatter formatter = DATETIME_FORMATTERS[isTwoDigit][withMs][withSplitT];
DateTimeFormatter formatter = DATETIME_FORMATTERS[isTwoDigit][withMs][withSplitT][withSec];
return parseStringWithDefaultHSM(str, formatter);
} else {
// date
Copy link

Choose a reason for hiding this comment

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

The most risky bug in this code is:
Missing DateTimeFormatter initializations for when milliseconds are not included (withMs is 0) but seconds are included (withSec is 1).

You can modify the code like this:

@@ -91,19 +91,23 @@ public class DateUtils {
     ...
+        // Initialization for cases when withMs is 0 and withSec is 1
+        DATETIME_FORMATTERS[0][0][0][1] = unixDatetimeStrictFormatter("%Y-%m-%e %H:%i:%s", false);
+        DATETIME_FORMATTERS[0][0][1][1] = unixDatetimeStrictFormatter("%Y-%m-%eT%H:%i:%s", false);
+        DATETIME_FORMATTERS[1][0][0][1] = unixDatetimeStrictFormatter("%y-%m-%e %H:%i:%s", false);
+        DATETIME_FORMATTERS[1][0][1][1] = unixDatetimeStrictFormatter("%y-%m-%eT%H:%i:%s", false);

The final initializations should look like this:

DATETIME_FORMATTERS[0][0][0][0] = unixDatetimeStrictFormatter("%Y-%m-%e %H:%i", false);
DATETIME_FORMATTERS[0][0][0][1] = unixDatetimeStrictFormatter("%Y-%m-%e %H:%i:%s", false);
// ... continued original initialization for other options...
DATETIME_FORMATTERS[0][1][1][1] = unixDatetimeStrictFormatter("%Y-%m-%eT%H:%i:%s.%f", false);
// New initializations for missing formatters
DATETIME_FORMATTERS[0][0][1][1] = unixDatetimeStrictFormatter("%Y-%m-%eT%H:%i:%s", false);
DATETIME_FORMATTERS[1][0][0][1] = unixDatetimeStrictFormatter("%y-%m-%e %H:%i:%s", false);
DATETIME_FORMATTERS[1][0][1][1] = unixDatetimeStrictFormatter("%y-%m-%eT%H:%i:%s", false);

Note that each new formatter I've added assumes %s must be formatted strictly (which you will want to verify).

@stephen-shelby
Copy link
Contributor

it's best to add sql test about iceberg

DATETIME_FORMATTERS[1][0][1][0] = unixDatetimeStrictFormatter("%y-%m-%eT%H:%i", false);
DATETIME_FORMATTERS[1][0][1][1] = unixDatetimeStrictFormatter("%y-%m-%eT%H:%i:%s", false);
DATETIME_FORMATTERS[1][1][0][1] = unixDatetimeStrictFormatter("%y-%m-%e %H:%i:%s.%f", false);
DATETIME_FORMATTERS[1][1][1][1] = unixDatetimeStrictFormatter("%y-%m-%eT%H:%i:%s.%f", false);
}

public static String formatDateTimeUnix(LocalDateTime dateTime) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add some unit tests for this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @packy92 i add ut for DateUtils.parseStrictDateTime(s) method. This PR not affect formatDateTimeUnix method.

Copy link

Quality Gate Passed Quality Gate passed

The SonarCloud Quality Gate passed, but some issues were introduced.

4 New issues
0 Security Hotspots
0.0% Coverage on New Code
0.0% Duplication on New Code

See analysis details on SonarCloud

Copy link

[FE Incremental Coverage Report]

pass : 15 / 15 (100.00%)

file detail

path covered_line new_line coverage not_covered_line_detail
🔵 com/starrocks/common/util/DateUtils.java 15 15 100.00% []

Copy link

[BE Incremental Coverage Report]

pass : 0 / 0 (0%)

@leoyy0316 leoyy0316 requested a review from packy92 January 30, 2024 07:44
@stephen-shelby stephen-shelby merged commit 20e682c into StarRocks:main Jan 30, 2024
@leoyy0316 leoyy0316 deleted the leo.cheng branch January 30, 2024 10:44
@stephen-shelby
Copy link
Contributor

@Mergifyio backport branch-3.2

Copy link
Contributor

mergify bot commented Jan 31, 2024

backport branch-3.2

✅ Backports have been created

@stephen-shelby
Copy link
Contributor

@Mergifyio backport branch-3.1

@stephen-shelby
Copy link
Contributor

https://github.com/Mergifyio backport branch-3.0

@stephen-shelby
Copy link
Contributor

@Mergifyio backport branch-2.5

mergify bot pushed a commit that referenced this pull request Jan 31, 2024
Signed-off-by: leoyy0316 <[email protected]>
(cherry picked from commit 20e682c)
Copy link
Contributor

mergify bot commented Jan 31, 2024

backport branch-3.1

✅ Backports have been created

Copy link
Contributor

mergify bot commented Jan 31, 2024

backport branch-3.0

✅ Backports have been created

Copy link
Contributor

mergify bot commented Jan 31, 2024

backport branch-2.5

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jan 31, 2024
Signed-off-by: leoyy0316 <[email protected]>
(cherry picked from commit 20e682c)
mergify bot pushed a commit that referenced this pull request Jan 31, 2024
Signed-off-by: leoyy0316 <[email protected]>
(cherry picked from commit 20e682c)
mergify bot pushed a commit that referenced this pull request Jan 31, 2024
Signed-off-by: leoyy0316 <[email protected]>
(cherry picked from commit 20e682c)
wanpengfei-git pushed a commit that referenced this pull request Jan 31, 2024
@github-actions github-actions bot added 3.2-merged and removed 2.5 labels Jan 31, 2024
murphyatwork pushed a commit that referenced this pull request Feb 4, 2024
murphyatwork pushed a commit that referenced this pull request Feb 4, 2024
wanpengfei-git pushed a commit that referenced this pull request Feb 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants