This is an automated email from the ASF dual-hosted git repository.

MaxGekk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/spark.git


The following commit(s) were added to refs/heads/master by this push:
     new f6b469a76336 [SPARK-45438][SQL] Negate a decimal exactly so -x and 
abs(x) keep all 38 digits
f6b469a76336 is described below

commit f6b469a76336c429d9267f1081504bdcab30f3bd
Author: Max Gekk <[email protected]>
AuthorDate: Mon Nov 9 11:51:19 2026 +0100

    [SPARK-45438][SQL] Negate a decimal exactly so -x and abs(x) keep all 38 
digits
    
    ### 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)
    
    Closes #60226 from MaxGekk/SPARK-45438-decimal-negate.
    
    Lead-authored-by: Max Gekk <[email protected]>
    Co-authored-by: t <[email protected]>
    Signed-off-by: Max Gekk <[email protected]>
    
    
    skip-checks: true
---
 .../scala/org/apache/spark/sql/types/Decimal.scala    |  4 +++-
 .../expressions/ArithmeticExpressionSuite.scala       | 14 ++++++++++++++
 .../org/apache/spark/sql/types/DecimalSuite.scala     | 19 +++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/sql/api/src/main/scala/org/apache/spark/sql/types/Decimal.scala 
b/sql/api/src/main/scala/org/apache/spark/sql/types/Decimal.scala
index d693c3550a6d..593bca3ddecb 100644
--- a/sql/api/src/main/scala/org/apache/spark/sql/types/Decimal.scala
+++ b/sql/api/src/main/scala/org/apache/spark/sql/types/Decimal.scala
@@ -605,7 +605,9 @@ final class Decimal extends Ordered[Decimal] with 
Serializable {
 
   def unary_- : Decimal = {
     if (decimalVal.ne(null)) {
-      Decimal(-decimalVal, precision, scale)
+      // Negate the Java value: Scala 2.13's `BigDecimal.unary_-` rounds to 
its math context,
+      // 34 digits by default, which loses digits of a value of 35 to 38 
digits (SPARK-45438).
+      Decimal(decimalVal.bigDecimal.negate(), precision, scale)
     } else {
       Decimal(-longVal, precision, scale)
     }
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala
 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala
index 168cde4a86f0..bd10bb710411 100644
--- 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala
+++ 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/ArithmeticExpressionSuite.scala
@@ -156,6 +156,20 @@ class ArithmeticExpressionSuite extends SparkFunSuite with 
ExpressionEvalHelper
     }
   }
 
+  test("SPARK-45438: UnaryMinus and Abs keep every digit of a 38-digit 
decimal") {
+    Seq("12345678901234567890123456789012345.67", 
"-99999999999999999999999999999999999999")
+      .foreach { s =>
+        val value = new java.math.BigDecimal(s)
+        val input = Literal(Decimal(value, 38, value.scale), DecimalType(38, 
value.scale))
+        Seq("true", "false").foreach { ansi =>
+          withSQLConf(SQLConf.ANSI_ENABLED.key -> ansi) {
+            checkEvaluation(UnaryMinus(input), Decimal(value.negate(), 38, 
value.scale))
+            checkEvaluation(Abs(input), Decimal(value.abs(), 38, value.scale))
+          }
+        }
+      }
+  }
+
   test("- (Minus)") {
     testNumericDataTypes { convert =>
       val left = Literal(convert(1))
diff --git 
a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala 
b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala
index 291b199113b8..8cf19f63f76d 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/types/DecimalSuite.scala
@@ -306,6 +306,25 @@ class DecimalSuite extends SparkFunSuite with 
PrivateMethodTester with SQLHelper
     assert(Decimal(-100).abs === Decimal(BigDecimal("100")))
   }
 
+  test("SPARK-45438: negate and abs keep every digit of a 38-digit value") {
+    Seq(
+      "12345678901234567890123456789012345.67",
+      "-12345678901234567890123456789012345.67",
+      "99999999999999999999999999999999999999",
+      "-99999999999999999999999999999999999999",
+      "-89999999999999999999999999999999999999",
+      "1234567890123456789012345678901234567.8").foreach { s =>
+      // A java.math.BigDecimal, not Decimal(String): the latter carries a 
38-digit context,
+      // which would hide the rounding of the default 34-digit one.
+      val value = new java.math.BigDecimal(s)
+      val d = Decimal(value, 38, value.scale)
+      val negated = -d
+      assert(negated.toJavaBigDecimal === value.negate(), s)
+      assert(d.abs.toJavaBigDecimal === value.abs(), s)
+      assert(negated.precision === 38 && negated.scale === value.scale, s)
+    }
+  }
+
   test("floor & ceil") {
     assert(Decimal("10.03").floor === Decimal(BigDecimal("10")))
     assert(Decimal("10.03").ceil === Decimal(BigDecimal("11")))


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to