Fix silent overflow in DateTimeEncoding.pack #24674
Merged
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.
Description
Throw an exception when DateTimeEncoding's pack method will see the millisUtc overflow/underflow when constructing a TimestampWithTimeZone long.
Motivation and Context
Today operations that produce a TimestampWithTimeZone can sliently overflow/underflow when packing the millisUtc and time zone ID into a long. The DateTimeEncoding.pack method accepts the millisUtc as a 64-bit long, and then uses 52 bits of the TimestampWithTimeZone long to hold that value. If the value requires more than 52 bits, it will silently overflow/underflow producing incorrect results.
This change adds a check to see if the millisUtc can fit in 52 bits, and throws an ArithmeticException if it detects the value will overflow/underflow.
As an example
Impact
Users will see exceptions now where they were previously seeing incorrect results.
Test Plan
Added unit tests for a few UDFs I identified as producing TimestampWithTimeZone values to ensure their behavior with values that just fit in 52 bits, and values that require more than 52 bits.
Contributor checklist
Release Notes
Please follow release notes guidelines and fill in the release notes below.