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

Add support for "week of month" in date parsing pattern #10511

Conversation

NEUpanning
Copy link
Contributor

In date parsing pattern, "week of month" is used by java.text.SimpleDateFormat. So we need to support it for supporting SimpleDateFormat date parsing mode in DateTimeFormatter. It's presented by number and the range is [1,5].

relates issue:#6374

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 19, 2024
Copy link

netlify bot commented Jul 19, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit e6d51ae
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/66a211db55a36f0008159315

@NEUpanning
Copy link
Contributor Author

@PHILO-HE @mbasmanova Could you help to review this PR please? Thanks.

Copy link
Collaborator

@rui-mo rui-mo left a comment

Choose a reason for hiding this comment

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

Would you add some unit tests? Thanks.

@@ -56,6 +56,13 @@ DateTimeFormatterBuilder& DateTimeFormatterBuilder::appendWeekOfWeekYear(
return *this;
}

DateTimeFormatterBuilder& DateTimeFormatterBuilder::appendWeekOfMonth(
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this function be called in buildJodaDateTimeFormatter or buildMysqlDateTimeFormatter? Like https://github.com/facebookincubator/velox/blob/main/velox/functions/lib/DateTimeFormatter.cpp#L1463.

Copy link
Contributor Author

@NEUpanning NEUpanning Jul 22, 2024

Choose a reason for hiding this comment

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

No. "Week Of Month" format is not supported in Joda and Mysql.

@NEUpanning
Copy link
Contributor Author

@rui-mo Since "week of month" format is not supported in in Joda and Mysql date patterns, I can't add unit tests using MysqlDateTimeFormatter or JodaDateTimeFormatter. Could I add unit tests in the PR supporting SimpleDateFormat laterly? Or do you have any other suggestions?

Copy link
Contributor

@mbasmanova mbasmanova left a comment

Choose a reason for hiding this comment

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

@NEUpanning As @rui-mo mentioned, it would be nice to add some tests. Some specific suggestions are below. Thanks.

@@ -107,6 +107,15 @@ Status daysSinceEpochFromWeekDate(
int32_t dayOfWeek,
int64_t& out);

/// Computes the (signed) number of days since unix epoch (1970-01-01).
/// Returns UserError status if the date is invalid.
Status daysSinceEpochFromWeekOfMonthDate(
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps, extract this function into a separate PR and add a unit test.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The DateTimeFormatter.parse function depends on this function. Do you mean to submit all changes to the DateTimeFormatter.parse function and this function as a separate PR?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume this comment suggest us opening another PR which contains the implementation and tests for daysSinceEpochFromWeekOfMonthDate function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I opened a PR for this. #10604

int32_t weekOfMonth,
int32_t dayOfWeek,
int64_t& out) {
if (!isValidWeekOfMonthDate(year, month, weekOfMonth, dayOfWeek)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

isValidWeekOfMonthDate doesn't perform full validation. For example, it allows weekOfMonth = 5 and dayOfWeek = 6, but these may not exist for all months. Where / how do we handle these cases of invalid inputs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have tested SimpleDateFormat and I see it can parse the "invalid" weekOfMonth and dayOfWeek etc. Perhaps we should remove the validation to align with SimpleDateFormat. Here is an example:

SimpleDateFormat sdf = new SimpleDateFormat("y W");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = sdf.parse("2024 1");
System.out.println(date);
date = sdf.parse("2024 5");
System.out.println(date);
// invalid week of month
date = sdf.parse("2024 99");
System.out.println(date);

-------------console output--------------
Sun Dec 31 00:00:00 CST 2023
Sun Jan 28 00:00:00 CST 2024
Sun Nov 16 00:00:00 CST 2025

@NEUpanning
Copy link
Contributor Author

@mbasmanova @rui-mo I've added some unit tests in 7ec5bf4

Copy link
Collaborator

@rui-mo rui-mo left a comment

Choose a reason for hiding this comment

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

Thanks.

@@ -735,7 +740,6 @@ int32_t parseFromPattern(
return -1;
}
cur += size;
date.weekDateFormat = true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I wonder why we need this change, and could you clarify what is covered by weekDateFormat which seems to be a larger scope than weekOfMonthDateFormat?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

weekDateFormat has different format with weekOfMonthDateFormat.weekDateFormat is composed of year, weekOfYear and dayOfWeek and weekOfMonthDateFormat is composed of year, month, weekOfMonth and dayOfWeek. dayOfWeek is used by both weekDateFormat and weekOfMonthDateFormat, so dayOfWeek can't indicate the format is weekDateFormat

int32_t year,
int32_t month,
int32_t weekOfMonth,
int32_t dayOfWeek) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add some unit tests for this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added tests in e6d51ae

@NEUpanning NEUpanning requested review from rui-mo and PHILO-HE July 29, 2024 06:42
Copy link
Collaborator

@rui-mo rui-mo left a comment

Choose a reason for hiding this comment

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

@NEUpanning I notice some comments seems to be unresolved. Would you take a look first? Thanks.
#10511 (review) and #10511 (review)

@NEUpanning
Copy link
Contributor Author

@rui-mo The two comments you mentioned, one said "added test" and the other said "thanks". I have already added the test. Are there any other comments I missed?

@rui-mo
Copy link
Collaborator

rui-mo commented Jul 30, 2024

one said "added test" and the other said "thanks"

@NEUpanning Could you check the specific comments below them, e.g #10511 (comment)? If they have been addressed, it would be nice to reply at the comment section.

@NEUpanning
Copy link
Contributor Author

@rui-mo Sorry. I haven't submitted comments in a while. I think you can see it now.

facebook-github-bot pushed a commit that referenced this pull request Sep 4, 2024
…ek-of-month date (#10604)

Summary:
This helper function is only used by Spark. To align with Spark's SimpleDateFormat behavior, this function offers two modes: lenient and non-lenient. For non-lenient mode, it returns an error status if the date is invalid. For lenient mode, it accepts a wider range of arguments.

Part of #10511

Pull Request resolved: #10604

Reviewed By: xiaoxmeng

Differential Revision: D62033315

Pulled By: bikramSingh91

fbshipit-source-id: f17e50c07272dc656038b221129aff6882bcf02c
@NEUpanning
Copy link
Contributor Author

Opened a new PR #11103 for this feature.

@NEUpanning NEUpanning closed this Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants