← Back to Pull requests

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

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 04, 2026 23:17 UTC
Author

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 <!-- At least one of them must be included. -->

  • - [x] Regression test (test_cast.out rows re-derived, see below)
    - [x] 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 #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.
    - [x] Yes. <!-- Explain the behavior change -->
    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?

  • - [x] No.
    - [ ] Yes. <!-- Add document PR link here. -->

    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.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: abhinav-phi Review duration: 81617m
    All Vouch review confidence checks passed
    Residual risk 0.35 = Change Risk (0.59) × (1 − Review Confidence (0.41)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.59 · 650 lines diff across 8 files
    0.59
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    40%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #68733"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 40.9% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 59% (MEDIUM) · Higher = larger blast radius
    59%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 40.9% · Lower = rushed or shallow review
    40.9%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 34.8% · Safe to merge
    34.8%
    Repository