linhongliu-db opened a new pull request, #59889:
URL: https://github.com/apache/spark/pull/59889

   ### What changes were proposed in this pull request?
   
   Fix decimal remainder when the integral quotient exceeds the 39-digit math 
context even though the remainder is representable. Keep the existing 
context-based calculation first and retry with exact remainder only if it 
throws `ArithmeticException`.
   
   ### Why are the changes needed?
   
   `Decimal.%` can throw `ArithmeticException: Division impossible` for a large 
dividend and a small divisor. For example:
   
   ```sql
   SELECT
     CAST('99999999999999999999999999999999999999' AS DECIMAL(38, 0)) %
     CAST('0.0007' AS DECIMAL(4, 4));
   ```
   
   The representable result is `0.0004`. The failure comes from the 
intermediate quotient's precision, rather than the remainder.
   
   Trying the existing operation first preserves its successful results and 
extreme-scale behavior. The fallback is limited to the failed arithmetic call; 
decimal construction and the existing zero-divisor handling are unchanged.
   
   ### Does this PR introduce _any_ user-facing change?
   
   Yes. Decimal `%` and `pmod` succeed for inputs whose representable remainder 
previously failed because of the intermediate quotient limit. Existing 
successful calculations continue to use the same operation.
   
   ### How was this patch tested?
   
   Added regressions cover large quotients, both operand signs, quotient 
precision boundaries, `%` and `pmod`, scale preservation, zero divisors, and 
negative and extreme scales. They also cover inputs where the existing 
operation succeeds but exact division cannot materialize its quotient, and 
cases where both attempts fail.
   
   `DecimalSuite` and `ArithmeticExpressionSuite`: all 77 tests passed with 
SBT. The new large-quotient regressions reproduced `Division impossible` before 
the production change. SQL API and Catalyst test Scala style checks passed.
   
   ### Was this patch authored or co-authored using generative AI tooling?
   
   Generated-by: Codex CLI 0.164.0 (GPT-6)
   


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