fix: handle float-to-integer overflow boundary - #813
Draft
Pybsama wants to merge 1 commit into
Draft
Conversation
Use the exact exclusive upper bound for Spark-compatible floating-point-to-integer casts so rounded representations of INT_MAX and INT64_MAX saturate instead of entering undefined conversions. Add shared conversion and Spark expression coverage for the last in-range value, the positive overflow boundary, and the negative minimum. Fixes bytedance#809
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.
What problem does this PR solve?
Spark-compatible floating-point-to-integer casts currently compare the input
against the target's maximum after converting that maximum to the floating-point
source type.
INT_MAXrounds to2^31as afloat, andINT64_MAXrounds to2^63as adouble. Equality therefore misses the first positive out-of-rangevalue and lets it enter an undefined C++ conversion, which can produce the
target's minimum value.
Issue Number: close #809
Type of Change
Description
integer minimum and use
>=against that boundary.int32_tas the first-stage type for tinyint and smallint so Spark'sJava-style two-stage narrowing behavior remains unchanged.
Spark Cast expression path.
and the negative minimum for float/double conversions to all integral widths.
Using
>= max()would reject a validdouble(INT_MAX). In contrast, thenegated minimum is a power of two and is exactly representable in the relevant
floating-point type, so it identifies the first positive out-of-range value
without narrowing the valid range.
Local verification:
ConversionsTest.*: 5 passed.SparkCastExprTest.*: 23 passed.698 passed, 4 skipped, 0 failed.
does not contain
data/large_slow_parse.json; no Cast or conversion test wasexcluded.
headers, secret detection, and codespell, passed.
2^31fconversionenters undefined behavior.
Performance Impact
compile-time-derived boundary; no allocation, loop, or additional branch is
introduced.
Release Note
Release Note:
Checklist (For Author)
Breaking Changes