MaxGekk opened a new pull request, #60226:
URL: https://github.com/apache/spark/pull/60226

   ### What changes were proposed in this pull request?
   
   `Decimal.unary_-` negates a value held as a `scala.math.BigDecimal` with 
Scala's own `unary_-`. Since Scala 2.13 that operator is `new 
BigDecimal(this.bigDecimal.negate(mc), mc)` (scala/bug#11590), so it rounds the 
result to the value's `MathContext`, which is `MathContext.DECIMAL128` (34 
digits) by default. A `DECIMAL` value with 35 to 38 significant digits is 
rounded when negated, and a value at the type's bound can round past it and 
raise.
   
   This PR negates the underlying `java.math.BigDecimal` instead, which is 
exact:
   
   ```scala
   Decimal(decimalVal.bigDecimal.negate(), precision, scale)
   ```
   
   `Decimal.abs` calls `unary_-`, and the interpreted and generated code of 
`UnaryMinus` and `Abs` over a `DecimalType` call these two methods, so the one 
change fixes all four. `+` and `-` already use `java.math.BigDecimal.add` and 
`subtract`, which are exact.
   
   ### Why are the changes needed?
   
   The negation and the absolute value of a valid `DECIMAL(38, s)` return a 
wrong value. For example, with `x = 12345678901234567890123456789012345.67` as 
a `DECIMAL(38, 2)`:
   
   ```
   -x     = -12345678901234567890123456789012350.00   (expected 
-12345678901234567890123456789012345.67)
   abs(-x) = 12345678901234567890123456789012350.00   (expected  
12345678901234567890123456789012345.67)
   ```
   
   At the bound, the negation of `-99999999999999999999999999999999999999` 
rounds to `1.000000000000000000000000000000000E+38` and fails the precision 
check, which is the failure SPARK-45438 reports.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. `-x`, `negative(x)` and `abs(x)` over a decimal with more than 34 
significant digits now return the exact value instead of a value rounded to 34 
digits or an error. Values with 34 digits or fewer are unchanged.
   
   ### How was this patch tested?
   
   New tests:
   - `DecimalSuite`, "SPARK-45438: negate and abs keep every digit of a 
38-digit value": negation and `abs` of six values with 35 to 38 digits, 
including the bound and the value from the JIRA ticket, compared with 
`java.math.BigDecimal.negate()` and `abs()`.
   - `ArithmeticExpressionSuite`, "SPARK-45438: UnaryMinus and Abs keep every 
digit of a 38-digit decimal": `UnaryMinus` and `Abs` through `checkEvaluation` 
(interpreted and codegen) with ANSI mode on and off.
   
   Both tests fail without the fix (`-12345678901234567890123456789012350.00 
did not equal -12345678901234567890123456789012345.67`) and pass with it. 
`DecimalSuite` and `ArithmeticExpressionSuite` pass in full.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Code (Opus 5.5)
   


-- 
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]

Reply via email to