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

Return Status for several utility datetime functions #8918

Closed
wants to merge 3 commits into from

Conversation

marin-ma
Copy link
Contributor

Modify utility functions daysSinceEpochFromDate, daysSinceEpochFromDayOfYear and daysSinceEpochFromWeekDate to return Status code. These functions are likely to be frequently used in sparksql functions. As such, they could achieve better performance without throwing exceptions.

@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 Feb 29, 2024
@marin-ma
Copy link
Contributor Author

This PR is based on some discussions here: #8812 (comment)

Copy link

netlify bot commented Feb 29, 2024

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit d844691
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/65e7e9124c6b720008a7d4b0

@marin-ma
Copy link
Contributor Author

marin-ma commented Mar 1, 2024

@rui-mo Please kindly review. 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.

Thanks!

daysSinceEpoch = daysSinceEpochFromDate(year, 1, 1);
if (!daysSinceEpochFromDate(year, 1, 1, daysSinceEpoch).ok()) {
return false;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe simplified with VELOX_RETURN_IF?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like it cannot. VELOX_RETURN_IF returns status code.

@@ -327,8 +335,7 @@ bool tryParseDateString(
}
}

daysSinceEpoch = daysSinceEpochFromDate(year, month, day);
return true;
return daysSinceEpochFromDate(year, month, day, daysSinceEpoch).ok();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to check if daysSinceEpochFromDate returns ok here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

return daysSinceEpochFromDate(year, month, day, daysSinceEpoch).ok();

It will return true when status is ok, otherwise return false.

@marin-ma
Copy link
Contributor Author

marin-ma commented Mar 4, 2024

@mbasmanova Could you help to review? Thanks!

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.

I'm surprised there are not unit test changes. The APIs in TimestampConversion.h have changed, but there are not test changes. Are we missing test coverage for these APIs? If so, let's add it.

@@ -528,10 +535,21 @@ int32_t getMaxDayOfMonth(int32_t year, int32_t month) {
}

int64_t daysSinceEpochFromDate(int32_t year, int32_t month, int32_t day) {
int64_t daysSinceEpoch;
auto status = daysSinceEpochFromDate(year, month, day, daysSinceEpoch);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mbasmanova int64_t daysSinceEpochFromDate is preserved here and the unit tests still use this API. Should I remove it?

Copy link
Contributor

Choose a reason for hiding this comment

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

@marin-ma Is daysSinceEpochFromDate used only in unit tests? If so, then it is better to remove.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mbasmanova The previous one is also in use by non-test code. e.g. in sparksql function call:

template <typename T>
struct MakeDateFunction {
VELOX_DEFINE_FUNCTION_TYPES(T);
FOLLY_ALWAYS_INLINE void call(
out_type<Date>& result,
const int32_t year,
const int32_t month,
const int32_t day) {
auto daysSinceEpoch = util::daysSinceEpochFromDate(year, month, day);
VELOX_USER_CHECK_EQ(
daysSinceEpoch,
(int32_t)daysSinceEpoch,
"Integer overflow in make_date({}, {}, {})",
year,
month,
day);
result = daysSinceEpoch;
}
};

Copy link
Contributor

Choose a reason for hiding this comment

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

@marin-ma I see. Looks like we have inconsistent APIs. Some provide 2 versions: one that returns Status and doesn't throw and another that throws. While other APIs provide only non-throwing version. Would it make sense to align all these APIs to provide only non-throwing behavior?

@marin-ma
Copy link
Contributor Author

marin-ma commented Mar 6, 2024

@mbasmanova The previous APIs that throws have been removed. Could you help to review again? Thanks!

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.

@marin-ma Looks good to me. Thanks.

util::daysSinceEpochFromDate(year, month, day, daysSinceEpoch);
EXPECT_TRUE(status.isUserError());
};
EXPECT_NO_THROW(testDaysSinceEpochFromDateInvalid(1970, 1, -1));
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be possible to verify error message as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated in the latest commit.

@facebook-github-bot
Copy link
Contributor

@mbasmanova has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@marin-ma
Copy link
Contributor Author

marin-ma commented Mar 6, 2024

@mbasmanova There are internal linter and test failures. Could you provide the log?

https://github.com/facebookincubator/velox/runs/22322577035

@facebook-github-bot
Copy link
Contributor

@mbasmanova has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@mbasmanova merged this pull request in d54b76a.

Joe-Abraham pushed a commit to Joe-Abraham/velox that referenced this pull request Jun 7, 2024
…or#8918)

Summary:
Modify utility functions `daysSinceEpochFromDate`, `daysSinceEpochFromDayOfYear` and `daysSinceEpochFromWeekDate` to return Status code. These functions are likely to be frequently used in sparksql functions. As such, they could achieve better performance without throwing exceptions.

Pull Request resolved: facebookincubator#8918

Reviewed By: pedroerp

Differential Revision: D54566326

Pulled By: mbasmanova

fbshipit-source-id: d7d35a216deae01f7a5f07c96803deb0975689a1
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. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants