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

SQL: Fix issue with mins & hours for DATEDIFF #49252

Merged
merged 2 commits into from
Nov 19, 2019

Conversation

matriv
Copy link
Contributor

@matriv matriv commented Nov 18, 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.

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.
@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-search (:Search/SQL)

@matriv
Copy link
Contributor Author

matriv commented Nov 18, 2019

@astefan thx for pinpointing the issue and helping with the fix!

Copy link
Contributor

@astefan astefan left a 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);
Copy link
Contributor

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?

Copy link
Contributor Author

@matriv matriv Nov 18, 2019

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good.

@matriv matriv merged commit 124cd18 into elastic:master Nov 19, 2019
@matriv matriv deleted the fix-datediff-min-hours branch November 19, 2019 12:40
@jimczi jimczi added v7.5.0 and removed v7.5.1 labels Nov 19, 2019
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants