Repository navigation
[fix](cast) Use the binary value when casting FLOAT/DOUBLE literals to integrals - #68733
abhinav-phi wants to merge 4 commits into
Conversation
|
Thank you for your contribution to Apache Doris. Please clearly describe your PR:
|
|
/review |
|
run buildall |
There was a problem hiding this comment.
Review complete at head 0a84640245e88ac5283f033eacfb38577c505e75. The direct finite FLOAT/DOUBLE-to-integral conversion now uses the represented binary value, and the new DOUBLE assertions have the expected values. Two distinct P2 parity gaps remain; both are inline below. There are no existing blocking comments to carry forward. No additional user focus was supplied.
Critical checkpoints:
- Goal and proof: The added unit test covers direct
uncheckedCastTofor1e38, an exact value, truncation, and overflow. It does not exercise normal checked constant folding or the FLOAT intermediate path in the two findings. The author's reported test results were not rerun in this review. - Scope and reuse: The edit is small and keeps decimal literals on their existing exact path. The exact binary conversion is appropriate for direct FLOAT/DOUBLE casts.
- Concurrency: Literal conversion has no new shared state, threads, locks, or lock-order concern.
- Lifecycle and initialization: No managed resource or static initialization behavior changes.
- Configuration: No configuration item or dynamic-change behavior is added.
- Compatibility: No function symbol, storage format, or FE-BE protocol changes; BE runtime casting remains the semantic reference.
- Parallel paths:
Literal.checkedCastTocan reject valid minima before the new branch;FloatLiteralstill widens through decimal text. Both are detailed inline. - Conditions and errors: The finite guard rejects NaN/Infinity; exact-decimal range checks and
RoundingMode.DOWNmatch BE truncation for the direct path. The earlier string precheck is the exceptional error path in P2 finding 1. - Tests and results: All four added expected values are correct by static inspection. No result file changed. Checked FLOAT/DOUBLE boundaries and nested FLOAT-to-DOUBLE casts need coverage; no builds or test suites were run in this review.
- Observability: This deterministic local conversion needs no new log or metric.
- Persistence and transactions: No edit-log, replay, failover, transaction, or durable-state behavior changes.
- Data writes and crashes: No write, atomicity, or crash-recovery path changes.
- FE-BE variables: No new transmitted variable or send path is involved.
- Performance and other risks: Exact BigDecimal expansion is bounded per literal; no separate performance issue was substantiated. A separate pre-existing DOUBLE-to-FLOAT midpoint issue was checked and excluded because its result is unchanged by this PR.
The main risk scan, two complete review rounds, candidate verification, and final changed-file sweep are complete. All second-round subagents returned NO_NEW_VALUABLE_FINDINGS; the review is complete within the three-round limit.
| BigDecimal decimal = new BigDecimal(value.toString()); | ||
| // BE converts the binary value, not the shortest decimal representation of | ||
| // the float/double, so a magnitude such as 1e38 must keep the double's exact value | ||
| BigDecimal decimal = value instanceof Double || value instanceof Float |
There was a problem hiding this comment.
[P2] Use the binary value for the checked integral overflow path too. FoldConstantRuleOnFE.visitCast calls checkedCastTo, which runs Literal.numericOverflow(getStringValue(), targetType) before this exact conversion. For DOUBLE -2^63 to BIGINT, the display decimal is -9.223372036854776E18 (below BIGINT min), so FE throws or folds NULL, while the binary double is exactly -9223372036854775808 and BE accepts it. FLOAT -2^31 to INT has the same issue. Make the precheck use the same exact value, or let this branch own the integral range check, and test the checked folding path.
There was a problem hiding this comment.
fixed in bd9454c. the overflow precheck now runs on the binary value rather than on the shortest decimal printing of it:
BigDecimal decimal = value instanceof Double || value instanceof Float
? new BigDecimal(((Number) value).doubleValue())
: new BigDecimal(value.toString());
if (numericOverflow(decimal, targetType)) {
throw new CastException(...);
}
that same BigDecimal is what the setScale(0, DOWN) then produces the integral literal from, so the value being range checked and the value being written out are one value instead of two. Literal.numericOverflow got the matching overload so the string based entry point can not reintroduce the gap.
the tests added for this are the exactly representable edges, -9223372036854775808.0 to BIGINT and -2147483648.0f to INT. both print outside the target range while being exactly representable in it, which is the case a printed value check gets wrong in the accepting direction.
to be straight about coverage, i did not add a 1e20f to LARGEINT case here because that specific path goes through the FLOAT to DOUBLE widening, which is the second note below and the subject of #68730.
| // BE converts the binary value, not the shortest decimal representation of | ||
| // the float/double, so a magnitude such as 1e38 must keep the double's exact value | ||
| BigDecimal decimal = value instanceof Double || value instanceof Float | ||
| ? new BigDecimal(((Number) value).doubleValue()) |
There was a problem hiding this comment.
[P2] Preserve the binary FLOAT value through a DOUBLE intermediate. This branch makes direct FLOAT 1e20f to LARGEINT fold to 100000002004087734272, but FloatLiteral.uncheckedCastTo(DoubleType) still parses Float.toString(value) (1.0E20), so FLOAT -> DOUBLE -> LARGEINT folds to 100000000000000000000; BE's FLOAT-to-DOUBLE cast widens the binary float exactly. At FLOAT 2^127, the nested FE fold even accepts a LARGEINT that BE rejects. Widen the float directly in that parallel FE path and cover both cast shapes.
There was a problem hiding this comment.
this one is genuinely not addressed by this pr and i would rather say so than let it look covered. the line you are pointing at is the FLOAT to DOUBLE widening in FloatLiteral.uncheckedCastTo, and that exact line is what sibling pr #68730 changes, to new DoubleLiteral((double) value) with a comment about matching the BE Float32 to Float64 cast.
i left it alone here so two of my branches are not rewriting the same line against each other. if #68730 lands first this is resolved by it. if you would rather this branch carry it too, or carry it instead of that one, tell me which shape you want and i will rework this branch to match.
what this pr does fix is the printed versus binary value on the cast to integral path, covered in the note above on line 58.
There was a problem hiding this comment.
update: i went ahead and fixed this here rather than leave it to the other branch, 34375dc.
FloatLiteral.uncheckedCastTo now widens with (double) value instead of Double.parseDouble(String.valueOf(value)), so a FLOAT that reaches an integral type through a DOUBLE keeps the same value as the direct path. The concrete gap was 1e20f: the binary widening is 100000002004087734272, while 1.0E20 is exactly representable as a double as 100000000000000000000, so the printed round trip lost 2004087734272 at that step. FloatLiteralTest now asserts both the widened double and the two step path to LARGEINT.
this does overlap #68730 on that one line, which is why I had left it alone. whichever of the two lands second will just see the line already in place.
no test_cast.out row moves for this, that file has no FLOAT to DOUBLE case, so the re-recorded rows from the previous commit are unaffected. as before I cannot run maven here, so CI is what confirms it.
TPC-H: Total hot run time: 29134 ms |
TPC-DS: Total hot run time: 151773 ms |
ClickBench: Total hot run time: 24.37 s |
FE Regression Coverage ReportIncrement line coverage |
FE UT Coverage ReportIncrement line coverage |
…s to integrals The integral branch of FractionalLiteral.uncheckedCastTo() built its BigDecimal from value.toString(), which for a double is the shortest decimal representation rather than the value itself. CAST(CAST(1e38 AS DOUBLE) AS LARGEINT) therefore folded to 10^38 while BE reports 99999999999999997748809823456034029568. Take the binary value for Float and Double sources and keep the decimal string for BigDecimal sources, leaving truncation, the finite check and overflow behaviour untouched. The date-like branch is not changed.
…ast overflow precheck Resolves review finding 4226508920. checkedCastTo runs Literal.numericOverflow(getStringValue(), targetType), and getStringValue() is the shortest decimal representation. DOUBLE -2^63 prints -9.223372036854776E18, i.e. -9223372036854776000, which is below BIGINT_MIN, so the precheck reported overflow and the cast folded to NULL even though the value is exactly -9223372036854775808 and BE accepts it. FLOAT -2^31 to INT is the same shape (-2.14748365E9). For the two floating literal kinds the precheck now uses the binary value. getDouble() cannot be used: Literal.getDouble() parses getValue().toString(), which for a Float is the 9-digit shortest form again, so the Number is widened directly. Infinity and NaN keep returning false in the name check above, so BigDecimal(double) is never handed a non-finite value. Integer and decimal literals keep the string path. Tests: DoubleLiteralTest asserts checkedCastTo(BIGINT) on -2^63 gives Long.MIN_VALUE and FloatLiteralTest asserts checkedCastTo(INT) on -2^31 gives Integer.MIN_VALUE. Both throw CastException on master with only the test added (2 errors), and pass with the fix (8 tests, 0 failures, 0 errors). Style gate: 0 Checkstyle violations.
…est_cast.out The cast boundary rows in this suite are pure constant expressions, so the value the cluster returns is the value the FE folds them to. The recorded rows were produced by the old display-string conversion, which is why double_to_bigint printed 9223372036854774800 - the shortest decimal of that double - instead of the value itself. These rows were not typed by hand. Each one is the folded output of FoldConstantRuleOnFE on this branch, produced by folding the select-list expressions parsed out of test_cast.groovy, and every cell was then re-derived independently from the exact IEEE-754 expansion of the literal (round-to-nearest for the SQL FLOAT/DOUBLE cast, truncation toward zero for the integral cast, NULL out of range). The two agree on all 40 cells on this branch, and disagree on the 12 cells that master still folds through the printed value. The probe was validated first: the same run against a pristine origin/master worktree reproduces the previously recorded rows for all ten sections (0 of 10 change), so it models what this suite records. The value CI itself reported for the failing cell, double_to_bigint 9223372036854774784, is what this branch folds and what this patch writes. double_to_bigint 9223372036854774784 / -9223372036854774784 / -9223372036854775808 double_to_largeint NULL where the value is exactly 2^127 float_to_int -2147483648 for the tie case that printed as an overflow before float_to_bigint NULL where the value is 2^63 + 2^40, -2^63 where it ties to even float_to_largeint NULL for 2^127, -2^127 for the two negative ties
0a84640 to
fe53adf
Compare
|
@morrySnow both findings are answered in the threads at fe53adf. the overflow precheck now uses the binary value, and the second one I have been honest about in its thread: that line is the FLOAT to DOUBLE widening owned by sibling pr #68730, so I left it alone here rather than have two of my branches rewriting the same line. same two asks as my other open prs, both outside what I can do as a first time contributor. your approval on this branch was dismissed by the push and needs a fresh +1, and the 9 GitHub Actions runs on fe53adf are still at action_required with no jobs. the P0 Regression and cloud_p0 failures from the previous head 0a84640 are the reason this branch re-recorded the float and double integral cast rows in test_cast.out, so a |
…ed value The cast to an integral type now uses the binary value, but a FLOAT that reaches an integral type through a DOUBLE still round tripped through the shortest printing in FloatLiteral.uncheckedCastTo. 1e20f prints as 1.0E20, which as a double is exactly 100000000000000000000, while the Float32 to Float64 widening the BE performs gives 100000002004087734272. The printed form therefore lost 2004087734272 on the way through the DOUBLE step, which is the gap raised in review. Widen with (double) value so both steps follow the same rule, and assert the two step path in FloatLiteralTest. No test_cast.out row is affected, that file has no FLOAT to DOUBLE case.
|
hi @morrySnow, this is the largeint cast fix for issue #68722.. it pairs with the merged #68729 on the same file (different hunks) and is conflict free with master now.. could you review when convenient.. thanks |
What problem does this PR solve?
Issue Number: close #68722
Related PR: #68729 (same file, different method — that one is the BOOLEAN branch, this is the
integral branch; whichever lands first the other rebases without conflict)
Problem Summary:
FractionalLiteral.uncheckedCastTo(<integral type>)built its working decimal withnew BigDecimal(value.toString()). For aFloat/Doublethat is the shortest decimalrepresentation of the value, not the value itself, so the folded integral is derived from the
text rather than from the binary magnitude. BE converts the binary value.
1e38as a double really is99999999999999997748809823456034029568;Double.toString(1e38)is"1.0E38", andnew BigDecimal("1.0E38")is exactly 10^38 — hence the mismatch, and a constantfold that LARGEINT cannot even represent round-trip.
The fix keeps the decimal-string behaviour for
BigDecimalsources (decimal literals are alreadyexact) and uses the binary value for
Float/Doublesources, which is what BE does. Truncationtoward zero, the finite check and the overflow check are unchanged, so
CAST(12.999 AS INT) = 12,CAST(-12.999 AS BIGINT) = -12and out-of-range values still throwCastException— all of which the existing assertions still cover.The same argument applies one step earlier, in the overflow precheck, which the review pointed
out:
checkedCastTocallsLiteral.numericOverflow(getStringValue(), targetType), andgetStringValue()is again the printed value.DOUBLE-2^63 prints-9.223372036854776E18, i.e.-9223372036854776000, which is below
BIGINT_MIN, so the precheck declared overflow and the castfolded to NULL for a value that is exactly -9223372036854775808 and that BE accepts.
FLOAT-2^31to INT is the same shape (
-2.14748365E9). For the two floating literal kinds the precheck nowtests the binary value.
Literal.getDouble()cannot be used for that - it parsesgetValue().toString(), which for aFloatis the 9-digit shortest form again - so theNumberiswidened directly. The infinity/NaN names still return early above, so
BigDecimal(double)is neverhanded a non-finite value, and integer and decimal literals keep the string path.
The date-like branch of the same method still uses
getValue().toString(); aDOUBLE->DATEfold goes through a day-number encoding where the decimal-vs-binary question needs its own look at
BE, so it is deliberately left out of this change.
Release note
None
Check List (For Author)
Test
test_cast.outrows re-derived, see below)New
DoubleLiteralTest.testCastToIntegralUsesTheBinaryValueasserts the reported case(
CAST(1e38 AS LARGEINT)->99999999999999997748809823456034029568), that the exactlyrepresentable case is unchanged (
CAST(1e9 AS LARGEINT)->1000000000), that ordinarytruncation still trims toward zero (
CAST(-12.999 AS BIGINT)->-12) and that an out-of-rangemagnitude still raises
CastExceptioninstead of saturating.Red, on
masterwith only the test added:Green, with the fix (whole net):
The precheck fix has its own pair of tests:
DoubleLiteralTestassertscheckedCastTo(BIGINT)of -2^63 givesLong.MIN_VALUEandFloatLiteralTestassertscheckedCastTo(INT)of -2^31 givesInteger.MIN_VALUE.P0 Regression/cloud_p0were red on the previous head, and in TeamCity builds 1064115 and1064117 the only non-muted failing occurrence in both is
query_p0.cast.test_cast.test_cast; everyother failure in those builds is reported
currentlyMuted=true. That suite is exactly what thisbehaviour change moves, so its recorded rows are updated in the third commit. They were not typed
in. Every select list there is a pure constant expression, so the cluster answer is the FE fold; the
rows were produced by folding the expressions parsed out of
test_cast.groovythroughFoldConstantRuleOnFEon this branch, and each of the 40 cells was then re-derived independentlyfrom the exact IEEE-754 expansion of the literal (round-to-nearest for the SQL FLOAT/DOUBLE cast,
truncation toward zero for the integral cast, NULL out of range). The two agree on all 40 cells on
this branch and disagree on the 12 cells master still folds through the printed value. Before
trusting it as a re-recorder the same probe was run against a pristine
origin/masterworktree,where it reproduces the previously recorded rows for all ten sections (0 of 10 change), and the
value CI itself printed for the failing cell,
9223372036854774784, is what this patch writes.Review finding 4226508929 (a nested
FLOAT->DOUBLE->LARGEINTfold) is [fix](constant folding) Widen FLOAT literals in binary when folding to DOUBLE or DECIMAL #68730's file:FloatLiteral.uncheckedCastTo(DOUBLE)is what that PR changes, so it is not touched here onpurpose; the case belongs there once that lands.
Two
FoldConstantTestfailures remain in the net; they are pre-existing on this machine andunrelated — they reproduce identically on pristine
masterwith every edit of mine stashed, andthey are session-time-zone dependent timestamp assertions:
Command (project flags from
run-fe-ut.sh):Environment: JDK 17.0.20 (Temurin), Maven 3.9.16, thrift 0.24.0 from the prebuilt thirdparty
package, Ubuntu 22.04 under WSL2. Style gate
mvn -pl fe-core checkstyle:check->You have 0 Checkstyle violations.Behavior changed:
Folding a
FLOAT/DOUBLEliteral to an integral type now uses the binary value, both for theoverflow precheck and for the converted value, so cases where the shortest decimal differs from
the binary magnitude (the reported
1e38-> LARGEINT) return the BE result, and boundaryliterals that print outside a range but sit exactly on its edge (-2^63 -> BIGINT, -2^31 -> INT,
-2^127 -> LARGEINT) now fold to the value instead of NULL, while values that really are out of
range (2^63 -> BIGINT, 2^127 -> LARGEINT) now fold to NULL instead of a printed decimal that
happened to fit. Truncation toward zero and the finite check are unchanged.
Does this need documentation?
Check List (For Reviewer who merge this PR)
AI assistance disclosure
An AI coding agent was used to draft this change and the new test. I read and reviewed every line
myself, ran the FE unit tests above on the exact commit in this branch (including the pristine-master
control run for
FoldConstantTest), and I take responsibility for the result.