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

feat: Add Spark months_between function #12110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

@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 Jan 17, 2025
Copy link

netlify bot commented Jan 17, 2025

Deploy Preview for meta-velox canceled.

Name Link
🔨 Latest commit 3416eab
🔍 Latest deploy log https://app.netlify.com/sites/meta-velox/deploys/67935c25ef69940008c31648

@leoluan2009 leoluan2009 force-pushed the months_between branch 2 times, most recently from b65b99b to ecd7e0f Compare January 22, 2025 05:22
const auto monthsBetween2 =
[&](const StringView date1, const StringView date2, const bool roundOff) {
return evaluateOnce<double>(
"months_between(c0,c1)",
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the test really covered? Here should be months_between(c0,c1,c2)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks

struct MonthsBetweenFunction : InitSessionTimezone<TExec> {
VELOX_DEFINE_FUNCTION_TYPES(TExec);

FOLLY_ALWAYS_INLINE bool isEndDayOfMonth(const std::tm& dateTime) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please ,move the function to struct private scope

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks

FOLLY_ALWAYS_INLINE double monthsBetween(
const std::tm& datetime1,
const std::tm& datetime2,
const bool roundOff) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Drop const when passing by value

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks

auto dayDiff = datetime1.tm_mday - datetime2.tm_mday;
auto hourDiff = datetime1.tm_hour - datetime2.tm_hour;
auto minuteDiff = datetime1.tm_min - datetime2.tm_min;
auto secondsDiff = dayDiff * 86400 + hourDiff * 3600 + minuteDiff * 60 +
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use kSecondsInDay in TimeUtils.h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks

auto diff =
monthDiff + static_cast<double>(secondsDiff) / (31 * 24 * 60 * 60);
if (roundOff) {
return std::round(diff * 100000000) / 100000000;
Copy link
Contributor

Choose a reason for hiding this comment

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

100000000 -> 100'000'000 for readable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks

VELOX_DEFINE_FUNCTION_TYPES(TExec);

FOLLY_ALWAYS_INLINE bool isEndDayOfMonth(const std::tm& dateTime) {
auto year = dateTime.tm_year + 1900;
Copy link
Contributor

Choose a reason for hiding this comment

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

This is function getYear in TimeUtils.h

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks

(isEndDayOfMonth(datetime1) && isEndDayOfMonth(datetime2))) {
return static_cast<double>(monthDiff);
}
auto dayDiff = datetime1.tm_mday - datetime2.tm_mday;
Copy link
Contributor

Choose a reason for hiding this comment

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

Please refer to other functions in TimeUtils.h, you need month and secondsInDay

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, 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.

@leoluan2009 Thanks. We had one PR #4859 which was closed due to stale. But would you like to check the comments inside it to see if all are addressed in this PR? Also, we could refer to its documentation.

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.

4 participants