← Back to Pull requests

fix: round long string inputs at the target decimal scale #7716

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 20, 2026 22:14 UTC
Author

Which issue does this PR close?

Closes #7581.

Rationale for this change

The native string-to-decimal cast accumulated the entire coefficient in an i128 before rounding. For '1.' followed by 50 zeros, that intermediate requires 10^50. The result at DECIMAL(10,2) only needs the unscaled integer 100. Comet returned NULL or raised CAST_INVALID_INPUT under ANSI, where Spark returns 1.00.

What changes are included in this PR?

After validating the full string and applying Spark's integral-digit limit, the parser uses the exponent and target scale to accumulate the retained digits. The first discarded digit determines HALF_UP rounding. The precision check includes any carry from rounding. When no digits are discarded and the coefficient has at most 19 digits, the parser accumulates both slices into one u64 and rescales once. This also covers scientific notation when every digit is retained.

With spark.sql.legacy.allowNegativeScaleOfDecimal=true, the 40-digit string representing 10^39 yields 1.0000E+39 at DECIMAL(10,-35). At DECIMAL(10,2) it raises NUMERIC_VALUE_OUT_OF_RANGE under ANSI. With the setting off, Spark's integral-digit check raises NUMERIC_OUT_OF_SUPPORTED_RANGE first.

For 40 nines followed by e-2147483647, casting to DECIMAL(10,2) drops 2,147,483,645 places. Before SPARK-60119, Spark needs a power of ten that exceeds the JDK's limit. Comet uses the OpenJDK 17–24 divisor limit of 536,870,919 places. No JDK version flag is sent from the driver. Its version cannot identify executor backports or a different executor JDK. OpenJDK 25 and builds with JDK-8355719 can accept drops from 536,870,920 to 646,456,992 when a divisor is needed. In this window Spark can return a value where Comet raises under ANSI or returns NULL otherwise. With a short coefficient on older Spark, Comet previously returned zero throughout this window.

For the 40 nines example the drop exceeds both limits. On Spark releases with SPARK-60119 this cast returns 0.00. On older Spark the exception still differs. Spark throws ArithmeticException in every mode, and Comet raises CAST_INVALID_INPUT under ANSI or returns NULL in legacy and TRY modes. See #6966.

CometCast.isSupported checks that the decimal target scale fits in a signed byte when converting between types. The JVM dispatcher checks its input and output types too. Spark evaluates a cast from a string column to DECIMAL(10,-256), including a cast through DECIMAL(10,0). Folded decimal literals and existing decimal columns bypass the string parser. Their scales can wrap during Arrow type conversion, for example from -256 to 0. The compatibility guide documents this pre-existing gap. Both serialization paths are tracked in #7748. See Scala type serialization and Rust type conversion.

#7576 tracks ANSI errors for scale increases above 38. #7605 reorganizes some of the existing assertions.

How are these changes tested?

  • The Parquet SQL fixture checks native CAST against Spark with ANSI enabled and disabled on the two long fractions from #7581 and three values whose 39th fractional digit determines rounding. With an exponent of +36 and target scale 2, the cases exercise a carry to 1.00, a negative tie at -0.005 that rounds to -0.01, and 0.124555 that rounds to 0.12. The fixture also compares TRY_CAST and checks the ANSI error for the 40-digit integer with the legacy negative-scale setting on and off.

  • CometNativeCastSuite checks that the ANSI cast of a Parquet string containing 40 nines followed by e-2147483647 runs in the Comet pipeline. It compares whether Spark and Comet fail and checks the returned values on success. The suite also compares the result at scale -35 with Spark. At scale -256 it checks the filter result and fallback reason for the direct cast and the cast through DECIMAL(10,0).

  • Rust tests cover exponent shifts, negative ties, a carry past precision 38, malformed trailing text, and both settings of tiny_to_zero. The parser tests cover both sides of the 19-digit fast path and the OpenJDK 17–24 divisor limit. The 40-digit coefficient exercises drops of 2^31 and 2^31 - 3. With SPARK-60119, a nonzero coefficient still needs the divisor when its significant digit count minus the drop lies between -1 and the target precision, inclusive. A test passes digit counts directly to check both endpoints and their neighbors without allocating a huge string. Spark rescaling
  • Checked Rust formatting and git diff --check locally. The runtime suites run in hosted CI.

    Decimal cast benchmarks

    The previous revision took about 9% longer than main on the ASCII DECIMAL(38,10) cases. Every non-null input in these cases fits the new single-multiply path. The revised cast takes about 17% less time than main for this target, 23% less for DECIMAL(18,2) and 9–12% less for Unicode. All nine cases took less time on the revised head than on either other revision in each of the six rounds.

    Median Criterion time in µs per 8,192 rows, with every tenth row null. A negative change means less time.

    | Digits | Precision, scale | Mode | Main c7a0480 | Previous 61d3e7b | Revised dfbdbc5 | Change vs. main |
    |---|---|---|---:|---:|---:|---:|
    | ASCII | 18,2 | ansi | 255.13 | 209.96 | 195.36 | -23.4% |
    | ASCII | 18,2 | legacy | 256.01 | 209.68 | 195.94 | -23.5% |
    | ASCII | 18,2 | try | 254.88 | 210.22 | 196.11 | -23.1% |
    | ASCII | 38,10 | ansi | 211.63 | 231.96 | 176.26 | -16.7% |
    | ASCII | 38,10 | legacy | 212.22 | 231.45 | 175.95 | -17.1% |
    | ASCII | 38,10 | try | 211.44 | 231.52 | 176.41 | -16.6% |
    | arabic indic | 38,10 | legacy | 898.11 | 888.87 | 814.25 | -9.3% |
    | fullwidth | 38,10 | legacy | 501.92 | 505.05 | 441.54 | -12.0% |
    | mixed ascii fullwidth | 38,10 | legacy | 454.54 | 464.76 | 400.47 | -11.9% |

    Hosted benchmark and per-round estimates · Benchmark source

    The runner built the unchanged cast_from_string bench and lockfile at each revision on Ubuntu 24.04 with Rust 1.100.1 on an Intel Xeon 6975P-C. All three used the optimized bench profile with LTO off, 16 codegen units and no debug info. Each binary ran on the same logical CPU in all six permutations of execution order. Criterion used 0.5 s warmup, 1.5 s measurement and 50 samples per case. The table gives the median of the six point estimates. These batches cover ordinary short coefficients, including exponents and Unicode. They do not measure long coefficients or end-to-end Spark queries.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: sunchao Review duration: 15939m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    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%
    46%
    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 #7716"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% 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: 52% (MEDIUM) · Higher = larger blast radius
    52%
    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: 46.7% · Lower = rushed or shallow review
    46.7%
    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: 27.7% · Safe to merge
    27.7%