fix: round long string inputs at the target decimal scale #7716
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
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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
sunchao
Which issue does this PR close?
Closes #7581.
Rationale for this change
The native string-to-decimal cast accumulated the entire coefficient in an
i128before rounding. For'1.'followed by 50 zeros, that intermediate requires10^50. The result atDECIMAL(10,2)only needs the unscaled integer100. Comet returned NULL or raisedCAST_INVALID_INPUTunder ANSI, where Spark returns1.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_UProunding. 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 oneu64and rescales once. This also covers scientific notation when every digit is retained.With
spark.sql.legacy.allowNegativeScaleOfDecimal=true, the 40-digit string representing10^39yields1.0000E+39atDECIMAL(10,-35). AtDECIMAL(10,2)it raisesNUMERIC_VALUE_OUT_OF_RANGEunder ANSI. With the setting off, Spark's integral-digit check raisesNUMERIC_OUT_OF_SUPPORTED_RANGEfirst.For 40 nines followed by
e-2147483647, casting toDECIMAL(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 throwsArithmeticExceptionin every mode, and Comet raisesCAST_INVALID_INPUTunder ANSI or returns NULL in legacy and TRY modes. See #6966.CometCast.isSupportedchecks 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 toDECIMAL(10,-256), including a cast throughDECIMAL(10,0). Folded decimal literals and existing decimal columns bypass the string parser. Their scales can wrap during Arrow type conversion, for example from-256to0. 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?
CASTagainst 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+36and target scale 2, the cases exercise a carry to1.00, a negative tie at-0.005that rounds to-0.01, and0.124555that rounds to0.12. The fixture also comparesTRY_CASTand checks the ANSI error for the 40-digit integer with the legacy negative-scale setting on and off.CometNativeCastSuitechecks that the ANSI cast of a Parquet string containing 40 nines followed bye-2147483647runs 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-35with Spark. At scale-256it checks the filter result and fallback reason for the direct cast and the cast throughDECIMAL(10,0).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 of2^31and2^31 - 3. With SPARK-60119, a nonzero coefficient still needs the divisor when its significant digit count minus the drop lies between-1and the target precision, inclusive. A test passes digit counts directly to check both endpoints and their neighbors without allocating a huge string. Spark rescalingChecked Rust formatting and
git diff --checklocally. The runtime suites run in hosted CI.Decimal cast benchmarks
The previous revision took about 9% longer than
mainon the ASCIIDECIMAL(38,10)cases. Every non-null input in these cases fits the new single-multiply path. The revised cast takes about 17% less time thanmainfor this target, 23% less forDECIMAL(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| Previous61d3e7b| Reviseddfbdbc5| 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_stringbench 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.