linhongliu-db commented on code in PR #59889:
URL: https://github.com/apache/spark/pull/59889#discussion_r4426432214


##########
sql/api/src/main/scala/org/apache/spark/sql/types/Decimal.scala:
##########
@@ -556,9 +556,29 @@ final class Decimal extends Ordered[Decimal] with 
Serializable {
     if (that.isZero) null
     else Decimal(toJavaBigDecimal.divide(that.toJavaBigDecimal, scale, 
RoundingMode.HALF_UP))
 
-  def %(that: Decimal): Decimal =
-    if (that.isZero) null
-    else Decimal(toJavaBigDecimal.remainder(that.toJavaBigDecimal, 
MATH_CONTEXT))
+  def %(that: Decimal): Decimal = {
+    if (that.isZero) {
+      null
+    } else {
+      val dividend = toJavaBigDecimal
+      val divisor = that.toJavaBigDecimal
+      // Try the context first for speed and for huge trailing-zero quotients 
that exact division
+      // cannot materialize. On failure, retry without its quotient precision 
limit: a quotient
+      // wider than the context may still have a representable remainder.
+      val remainder = try {
+        dividend.remainder(divisor, MATH_CONTEXT)
+      } catch {
+        case error: ArithmeticException =>
+          // Exact division materializes the integral quotient. Two maximum 
SQL decimal
+          // precisions cover ordinary decimal operands; keep the original 
failure for larger
+          // legacy scale gaps instead of attempting a potentially enormous 
allocation.

Review Comment:
   Updated in 6daeafd83d6: the comment now covers both legacy negative scales 
and direct Decimal values outside SQL bounds. The guard and arithmetic are 
unchanged. I also fixed the SQL API scalafmt layout; scalafmt validation and 
all 79 DecimalSuite/ArithmeticExpressionSuite tests pass. The PR description is 
updated as well.



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