SQL: Fix issue with mins & hours for DATEDIFF#49252
Merged
matriv merged 2 commits intoelastic:masterfrom Nov 19, 2019
Merged
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.
Collaborator
|
Pinging @elastic/es-search (:Search/SQL) |
Contributor
Author
|
@astefan thx for pinpointing the issue and helping with the fix! |
astefan
approved these changes
Nov 18, 2019
| 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); |
Contributor
There was a problem hiding this comment.
Isn't this equivalent to (end.toEpochSecond() - start.toEpochSecond()) / 60?
Contributor
Author
There was a problem hiding this comment.
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
matriv
added a commit
that referenced
this pull request
Nov 19, 2019
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)
matriv
added a commit
that referenced
this pull request
Nov 19, 2019
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)
This was referenced Feb 3, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Previously,
DATEDIFFforminutesandhourswas 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.