dwsmith1983 opened a new pull request, #60751: URL: https://github.com/apache/spark/pull/60751
### What changes were proposed in this pull request? With `spark.sql.legacy.allowNegativeScaleOfDecimal=true`, a `Decimal` can keep a negative scale in its compact (long-backed) form, for example after `changePrecision` to `DECIMAL(5, -2)`. `Decimal.actualLongVal` divides by `POW_10(_scale)`, which has no entry for a negative scale. This PR converts such a value the way a BigDecimal-backed value is converted, in `toLong`, `toScalaBigInt`, `toJavaBigInteger`, `roundToLong` and `roundToNumeric` (used by `roundToInt`, `roundToShort` and `roundToByte`). Compact values with a non-negative scale keep the existing path. ### Why are the changes needed? Converting such a value to an integral type, for example casting it to `INT` or `BIGINT`, fails with `ArrayIndexOutOfBoundsException` instead of returning the value or a `CAST_OVERFLOW` error. ### Does this PR introduce _any_ user-facing change? Yes, only with `spark.sql.legacy.allowNegativeScaleOfDecimal=true`: integral conversions of a compact decimal with a negative scale return the same result as for a BigDecimal-backed decimal of the same value, instead of failing with `ArrayIndexOutOfBoundsException`. ### How was this patch tested? New `DecimalSuite` test comparing compact and BigDecimal-backed decimals with negative scales across `toLong`, `toInt`, `toScalaBigInt`, `toJavaBigInteger` and `roundToLong`/`roundToInt`/`roundToShort`/`roundToByte`, including values that overflow `Long`. ### Was this patch authored or co-authored using generative AI tooling? No -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
