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

   ### What changes were proposed in this pull request?
   
   `Decimal.set(unscaled: Long, precision: Int, scale: Int)` now builds the 
overflow error from the rejected `unscaled` and `scale` instead of from `this`.
   
   `set` calls `setOrNull`, which returns `null` on overflow before writing any 
field. The old code then passed `this` to 
`unscaledValueTooLargeForPrecisionError`, but `this` still holds its previous 
value. That is `0` for a new `Decimal`, or the previous value when the instance 
is reused. The error now gets `Decimal(BigDecimal(unscaled, scale))`, which is 
the value that was rejected. The error condition, `precision`, `scale` and 
`config` stay the same.
   
   ### Why are the changes needed?
   
   The error reports the wrong value. For example:
   
   ```scala
   Decimal(170L, 2, 1)
   // before: [NUMERIC_VALUE_OUT_OF_RANGE.WITH_SUGGESTION] 0 cannot be 
represented as Decimal(2, 1). ...
   // after:  [NUMERIC_VALUE_OUT_OF_RANGE.WITH_SUGGESTION] 17.0 cannot be 
represented as Decimal(2, 1). ...
   ```
   
   Callers that hit this include `MakeDecimal` with ANSI mode on, and 
`AvroDeserializer.createDecimal` through `Decimal.apply(unscaled, precision, 
scale)`.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. In the `NUMERIC_VALUE_OUT_OF_RANGE.WITH_SUGGESTION` error that 
`Decimal.set(unscaled, precision, scale)` raises on overflow, the `value` 
parameter is now the rejected value instead of the decimal's previous value 
(usually `0`). The error condition, SQL state and other parameters do not 
change.
   
   ### How was this patch tested?
   
   Updated the existing assertions in `DecimalSuite` ("creating decimals"). 
They had expected `"value" -> "0"` and now expect `"17.0"`, `"170"` and 
`"100000000000000000"` for `Decimal(170L, 2, 1)`, `Decimal(170L, 2, 0)` and 
`Decimal(1e17.toLong, 17, 0)`. They fail without the fix.
   
   ```
   build/sbt 'catalyst/testOnly org.apache.spark.sql.types.DecimalSuite'
   ```
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Claude Opus 5.5
   Fix written by and all code manually reviewed by @ykisana.
   Tests, PR description, and comments by Claude.
   


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