-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
SQL: Fix issue with mins & hours for DATEDIFF #49252
Conversation
DATEDIFF for mins and hours, first should truncate the 2 dates to the respective field (mins or hours) and zero out all the more detailed time fields and then makes the subtraction.
Pinging @elastic/es-search (:Search/SQL) |
@astefan thx for pinpointing the issue and helping with the fix! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Left one comment.
return (long) Math.floor(secondsDiff / 60.0d); | ||
} | ||
// Truncate first to minutes (ignore any seconds and sub-seconds fields) | ||
return (end.toEpochSecond() / 60) - (start.toEpochSecond() / 60); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this equivalent to (end.toEpochSecond() - start.toEpochSecond()) / 60
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's not the same. We need to truncate each date first, if we do a final truncation on the result of the subtraction it fails for example for:
SELECT DATEDIFF(mi, '2019-12-31T20:22:33.987654321', '2022-01-01T04:33:22.123456789');
will return 1053130
instead of 1053131
and this is tested here: https://github.com/elastic/elasticsearch/pull/49252/files#diff-7c21301d54284bcf7b693c9f1eb92ba8R178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good.
Previously, DATEDIFF for minutes and hours was doing a rounding calculation using all the time fields (secs, msecs/micros/nanos). Instead it should first truncate the 2 dates to the respective field (mins or hours) zeroing out all the more detailed time fields and then make the subtraction. (cherry picked from commit 124cd18)
Previously, DATEDIFF for minutes and hours was doing a rounding calculation using all the time fields (secs, msecs/micros/nanos). Instead it should first truncate the 2 dates to the respective field (mins or hours) zeroing out all the more detailed time fields and then make the subtraction. (cherry picked from commit 124cd18)
Previously,
DATEDIFF
forminutes
andhours
was doing arounding calculation using all the time fields (secs, msecs/micros/nanos).
Instead it should first truncate the 2 dates to the respective field (mins or hours)
zeroing out all the more detailed time fields and then make the subtraction.