Skip to content

[fix](cast) Use the binary value when casting FLOAT/DOUBLE literals to integrals - #68733

Open
abhinav-phi wants to merge 4 commits into
apache:masterfrom
abhinav-phi:fix-68722-integral-cast-uses-binary-value
Open

abhinav-phi wants to merge 4 commits into
apache:masterfrom
abhinav-phi:fix-68722-integral-cast-uses-binary-value

Conversation

@abhinav-phi

@abhinav-phi abhinav-phi commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

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 with
new BigDecimal(value.toString()). For a Float/Double that is the shortest decimal
representation
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.

SELECT CAST(CAST(1e38 AS DOUBLE) AS LARGEINT) AS fe, CAST(d1 AS LARGEINT) AS be FROM t;
+-----------------------------------------+------------------------------------------+
| 100000000000000000000000000000000000000 | 99999999999999997748809823456034029568   |
+-----------------------------------------+------------------------------------------+

1e38 as a double really is 99999999999999997748809823456034029568; Double.toString(1e38) is
"1.0E38", and new BigDecimal("1.0E38") is exactly 10^38 — hence the mismatch, and a constant
fold that LARGEINT cannot even represent round-trip.

The fix keeps the decimal-string behaviour for BigDecimal sources (decimal literals are already
exact) and uses the binary value for Float/Double sources, which is what BE does. Truncation
toward zero, the finite check and the overflow check are unchanged, so
CAST(12.999 AS INT) = 12, CAST(-12.999 AS BIGINT) = -12 and out-of-range values still throw
CastException — all of which the existing assertions still cover.

The same argument applies one step earlier, in the overflow precheck, which the review pointed
out: checkedCastTo calls Literal.numericOverflow(getStringValue(), targetType), and
getStringValue() 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 cast
folded to NULL for a value that is exactly -9223372036854775808 and that BE accepts. FLOAT -2^31
to INT is the same shape (-2.14748365E9). For the two floating literal kinds the precheck now
tests the binary value. Literal.getDouble() cannot be used for that - it parses
getValue().toString(), which for a Float is the 9-digit shortest form again - so the Number is
widened directly. The infinity/NaN names still return early above, so BigDecimal(double) is never
handed 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(); a DOUBLE -> DATE
fold 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

    • Regression test (test_cast.out rows re-derived, see below)
    • Unit Test
    • Manual test (add detailed scripts or steps below)
    • No need to test or manual test. Explain why:

    New DoubleLiteralTest.testCastToIntegralUsesTheBinaryValue asserts the reported case
    (CAST(1e38 AS LARGEINT) -> 99999999999999997748809823456034029568), that the exactly
    representable case is unchanged (CAST(1e9 AS LARGEINT) -> 1000000000), that ordinary
    truncation still trims toward zero (CAST(-12.999 AS BIGINT) -> -12) and that an out-of-range
    magnitude still raises CastException instead of saturating.

    Red, on master with only the test added:

    [ERROR]   DoubleLiteralTest.testCastToIntegralUsesTheBinaryValue:388
              expected: <99999999999999997748809823456034029568> but was: <100000000000000000000000000000000000000>
    

    Green, with the fix (whole net):

    DoubleLiteralTest        Tests run: 3,  Failures: 0   (focused run: BUILD SUCCESS, exit 0)
    FloatLiteralTest         Tests run: 3,  Failures: 0
    LargeIntLiteralTest      Tests run: 3,  Failures: 0
    IntegerLiteralTest       Tests run: 2,  Failures: 0
    DecimalLiteralTest       Tests run: 2,  Failures: 0
    CompareLiteralTest       Tests run: 2,  Failures: 0
    CastTest                 Tests run: 23, Failures: 0
    ConstantProjectionFoldingTest Tests run: 1, Failures: 0
    org.apache.doris.analysis.FloatLiteralTest Tests run: 2, Failures: 0
    

    The precheck fix has its own pair of tests: DoubleLiteralTest asserts
    checkedCastTo(BIGINT) of -2^63 gives Long.MIN_VALUE and FloatLiteralTest asserts
    checkedCastTo(INT) of -2^31 gives Integer.MIN_VALUE.

    red   (fix reverted, tests kept): Tests run: 8, Failures: 0, Errors: 2
                                      CastException: -9.223372036854776E18 can't cast to BIGINT, overflow.
                                      CastException: -2.14748365E9 can't cast to INT, overflow.
    green (with the fix):             Tests run: 8, Failures: 0, Errors: 0
    whole net on the branch:          Tests run: 68, Failures: 2, Errors: 0  (the two timezone cases)
    

    P0 Regression / cloud_p0 were red on the previous head, and in TeamCity builds 1064115 and
    1064117 the only non-muted failing occurrence in both is query_p0.cast.test_cast.test_cast; every
    other failure in those builds is reported currentlyMuted=true. That suite is exactly what this
    behaviour 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.groovy through
    FoldConstantRuleOnFE on this branch, and each of the 40 cells 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 master still folds through the printed value. Before
    trusting it as a re-recorder the same probe was run against a pristine origin/master worktree,
    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 -> LARGEINT fold) 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 on
    purpose; the case belongs there once that lands.

    Two FoldConstantTest failures remain in the net; they are pre-existing on this machine and
    unrelated
    — they reproduce identically on pristine master with every edit of mine stashed, and
    they are session-time-zone dependent timestamp assertions:

    [ERROR]   FoldConstantTest.testDateConstructFunction:1552 expected: <'1977-06-03 17:57:24'> but was: <'1977-06-03 15:27:24'>
    [ERROR]   FoldConstantTest.testFoldString:581 expected: <'2025-10-27 14:58:08.100000'> but was: <'2025-10-27 12:28:08.100000'>
    

    Command (project flags from run-fe-ut.sh):

    mvn test -pl fe-core -am -Dcheckstyle.skip=true -DfailIfNoTests=false \
        -Dmaven.build.cache.enabled=false -Dtest=DoubleLiteralTest
    

    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:

    • No.
    • Yes.
      Folding a FLOAT/DOUBLE literal to an integral type now uses the binary value, both for the
      overflow 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 boundary
      literals 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?

    • No.
    • Yes.

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

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.

@hello-stephen

Copy link
Copy Markdown
Contributor

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR.

Please clearly describe your PR:

  1. What problem was fixed (it's best to include specific error reporting information). How it was fixed.
  2. Which behaviors were modified. What was the previous behavior, what is it now, why was it modified, and what possible impacts might there be.
  3. What features were added. Why was this function added?
  4. Which code was refactored and why was this part of the code refactored?
  5. Which functions were optimized and what is the difference before and after the optimization?

@morrySnow morrySnow changed the title [fix](nereids) Use the binary value when casting FLOAT/DOUBLE literals to integrals [fix](cast) Use the binary value when casting FLOAT/DOUBLE literals to integrals Oct 9, 2026
morrySnow
morrySnow previously approved these changes Oct 9, 2026
@morrySnow

Copy link
Copy Markdown
Contributor

/review

@morrySnow

Copy link
Copy Markdown
Contributor

run buildall

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 uncheckedCastTo for 1e38, 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.checkedCastTo can reject valid minima before the new branch; FloatLiteral still widens through decimal text. Both are detailed inline.
  • Conditions and errors: The finite guard rejects NaN/Infinity; exact-decimal range checks and RoundingMode.DOWN match 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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-H: Total hot run time: 29134 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpch-tools
Tpch sf100 test result on commit 0a84640245e88ac5283f033eacfb38577c505e75, data reload: false

------ Round 1 ----------------------------------
============================================
q1	17604	5603	5556	5556
q2	2160	318	256	256
q3	10168	1441	827	827
q4	4667	471	349	349
q5	7433	796	554	554
q6	188	183	153	153
q7	785	794	620	620
q8	9250	1343	1099	1099
q9	5683	4466	4433	4433
q10	6829	1314	1035	1035
q11	419	252	232	232
q12	633	404	282	282
q13	18073	3084	2383	2383
q14	269	273	242	242
q15	q16	743	728	678	678
q17	1199	759	573	573
q18	7237	6243	6158	6158
q19	1117	963	599	599
q20	391	331	222	222
q21	6043	2584	2590	2584
q22	400	356	299	299
Total cold run time: 101291 ms
Total hot run time: 29134 ms

----- Round 2, with runtime_filter_mode=off -----
============================================
q1	6647	6639	6735	6639
q2	772	621	556	556
q3	5620	5686	5362	5362
q4	2099	2150	1589	1589
q5	5755	5536	5785	5536
q6	247	192	141	141
q7	2239	2031	1850	1850
q8	3146	2740	2756	2740
q9	8220	8183	8053	8053
q10	3700	3597	3238	3238
q11	589	409	372	372
q12	672	704	497	497
q13	3027	3073	2372	2372
q14	283	286	262	262
q15	q16	668	684	637	637
q17	7710	6965	6911	6911
q18	13086	12287	13111	12287
q19	904	788	787	787
q20	2182	2159	1965	1965
q21	5822	4710	4972	4710
q22	495	464	396	396
Total cold run time: 73883 ms
Total hot run time: 66900 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
TPC-DS: Total hot run time: 151773 ms
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/tpcds-tools
TPC-DS sf100 test result on commit 0a84640245e88ac5283f033eacfb38577c505e75, data reload: false

query5	4311	581	447	447
query6	424	206	191	191
query7	4794	458	226	226
query8	323	182	168	168
query9	8767	3953	3969	3953
query10	482	309	260	260
query11	5893	3434	3207	3207
query12	150	91	87	87
query13	1268	467	340	340
query14	6540	4831	4463	4463
query14_1	4232	4256	4213	4213
query15	202	195	189	189
query16	961	438	419	419
query17	832	633	526	526
query18	2412	409	323	323
query19	192	160	158	158
query20	79	79	76	76
query21	211	129	111	111
query22	13089	12964	12838	12838
query23	13056	12458	11968	11968
query23_1	12166	12217	12271	12217
query24	7089	800	456	456
query24_1	464	453	451	451
query25	503	396	357	357
query26	1254	253	145	145
query27	2754	435	271	271
query28	4656	1930	1951	1930
query29	1576	603	451	451
query30	300	225	192	192
query31	895	776	655	655
query32	150	97	99	97
query33	506	305	252	252
query34	967	855	496	496
query35	763	770	643	643
query36	818	829	772	772
query37	134	97	91	91
query38	1780	1760	1724	1724
query39	717	704	691	691
query39_1	675	664	673	664
query40	221	118	101	101
query41	71	71	69	69
query42	90	88	85	85
query43	369	377	325	325
query44	1301	687	695	687
query45	182	176	164	164
query46	858	962	582	582
query47	2944	2942	2896	2896
query48	297	295	216	216
query49	563	394	285	285
query50	660	272	212	212
query51	10323	10564	10390	10390
query52	82	78	68	68
query53	184	210	153	153
query54	249	204	174	174
query55	71	67	65	65
query56	242	222	215	215
query57	1602	1601	1551	1551
query58	280	259	249	249
query59	2346	2377	2123	2123
query60	272	238	220	220
query61	150	151	143	143
query62	391	339	291	291
query63	200	167	159	159
query64	2743	918	801	801
query65	3457	3363	3371	3363
query66	1781	417	297	297
query67	20098	20125	19895	19895
query68	2957	975	600	600
query69	378	288	246	246
query70	936	806	841	806
query71	308	234	202	202
query72	2698	2616	2475	2475
query73	516	523	295	295
query74	4580	4489	4290	4290
query75	2322	2307	1933	1933
query76	2298	1019	583	583
query77	345	400	294	294
query78	9112	8965	8453	8453
query79	871	831	514	514
query80	467	403	338	338
query81	524	318	271	271
query82	207	134	112	112
query83	192	193	179	179
query84	289	131	108	108
query85	756	410	362	362
query86	288	237	246	237
query87	2042	2022	1811	1811
query88	3539	2684	2676	2676
query89	335	299	257	257
query90	2108	186	179	179
query91	163	148	117	117
query92	90	89	86	86
query93	971	971	554	554
query94	462	312	267	267
query95	589	393	344	344
query96	663	527	233	233
query97	2454	2451	2354	2354
query98	153	155	141	141
query99	774	773	648	648
Total cold run time: 232538 ms
Total hot run time: 151773 ms

@hello-stephen

Copy link
Copy Markdown
Contributor
ClickBench: Total hot run time: 24.37 s
machine: 'aliyun_ecs.c7a.8xlarge_32C64G'
scripts: https://github.com/apache/doris/tree/master/tools/clickbench-tools
ClickBench test result on commit 0a84640245e88ac5283f033eacfb38577c505e75, data reload: false

query1	0.01	0.01	0.01
query2	0.11	0.05	0.04
query3	0.27	0.12	0.12
query4	1.61	0.12	0.12
query5	0.24	0.26	0.21
query6	1.18	0.69	0.68
query7	0.04	0.00	0.00
query8	0.05	0.04	0.04
query9	0.40	0.35	0.35
query10	0.56	0.58	0.57
query11	0.21	0.14	0.13
query12	0.19	0.15	0.14
query13	0.50	0.51	0.49
query14	0.98	0.97	0.98
query15	0.63	0.60	0.61
query16	0.30	0.31	0.34
query17	1.16	1.13	1.15
query18	0.23	0.21	0.21
query19	2.06	1.92	1.94
query20	0.02	0.01	0.02
query21	15.49	0.27	0.14
query22	4.37	0.06	0.05
query23	16.30	0.33	0.13
query24	2.94	0.72	0.38
query25	0.10	0.07	0.04
query26	1.57	0.25	0.15
query27	0.04	0.04	0.03
query28	2.33	0.67	0.42
query29	12.49	4.12	3.26
query30	0.29	0.15	0.13
query31	2.77	0.56	0.34
query32	3.22	0.62	0.52
query33	3.28	3.22	3.20
query34	15.52	4.00	3.40
query35	3.36	3.33	3.37
query36	0.57	0.45	0.48
query37	0.10	0.06	0.07
query38	0.05	0.04	0.04
query39	0.04	0.03	0.03
query40	0.17	0.14	0.14
query41	0.08	0.03	0.03
query42	0.03	0.03	0.02
query43	0.04	0.03	0.03
Total cold run time: 95.9 s
Total hot run time: 24.37 s

@hello-stephen

Copy link
Copy Markdown
Contributor

FE Regression Coverage Report

Increment line coverage 23.08% (3/13) 🎉
Increment coverage report
Complete coverage report

@hello-stephen

Copy link
Copy Markdown
Contributor

FE UT Coverage Report

Increment line coverage 100.00% (3/3) 🎉
Increment coverage report
Complete coverage report

…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
@abhinav-phi
abhinav-phi force-pushed the fix-68722-integral-cast-uses-binary-value branch from 0a84640 to fe53adf Compare October 14, 2026 20:08
@abhinav-phi

Copy link
Copy Markdown
Contributor Author

@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 run p0 and run cloud_p0 on fe53adf would tell us whether that was the right fix or whether there is a real folding difference left. I cannot run a FE plus BE cluster locally to check that myself, so I would rather see the suites report than guess.

…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.
@abhinav-phi

Copy link
Copy Markdown
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] DOUBLE to LARGEINT cast folded by FE uses the decimal string, BE converts the binary value

3 participants