sunchao commented on code in PR #7716:
URL: https://github.com/apache/datafusion-comet/pull/7716#discussion_r4761324916


##########
spark/src/test/resources/sql-tests/expressions/cast/cast_string_to_decimal_integral_digits_ansi.sql:
##########
@@ -23,14 +23,17 @@
 -- skips the limit, and the same strings fail the second way.
 -- https://github.com/apache/datafusion-comet/issues/7188
 
--- Config: spark.sql.ansi.enabled=true
+-- Config: spark.sql.ansi.enabled=false

Review Comment:
   Fixed in 61d3e7b. The [fixture 
header](https://github.com/apache/datafusion-comet/blob/dfbdbc54318e956451e2e99c697f7af1ea436249/spark/src/test/resources/sql-tests/expressions/cast/cast_string_to_decimal_integral_digits_ansi.sql#L18-L19)
 now says the first two queries run with ANSI off and then on, and the 
remaining queries run with ANSI on.



##########
spark/src/main/scala/org/apache/comet/expressions/CometCast.scala:
##########
@@ -209,6 +209,8 @@ object CometCast
         // Spark does not apply the 38 integral-digit limit to a string cast 
to decimal with
         // spark.sql.legacy.allowNegativeScaleOfDecimal.
         
castBuilder.setDecimalSkipIntegralDigitLimit(allowNegativeScaleOfDecimal)
+        // JDK-8355719 relaxed the overflow check in BigInteger.pow.
+        castBuilder.setDecimalFullDivisorRange(Runtime.version().feature() >= 
25)

Review Comment:
   I took your suggestion in dfbdbc5 and removed the proto field. Comet now 
uses the OpenJDK 17–24 limit of 536,870,919. The [compatibility 
guide](https://github.com/apache/datafusion-comet/blob/dfbdbc54318e956451e2e99c697f7af1ea436249/docs/source/user-guide/latest/compatibility/expressions/_category_template/cast.md#L84-L92)
 documents the extra range on JDK 25 and builds with JDK-8355719. The 40-nines 
example exceeds both limits, so it still fails under ANSI on older Spark.



##########
spark/src/test/scala/org/apache/comet/CometNativeCastSuite.scala:
##########
@@ -1557,6 +1557,7 @@ class CometNativeCastSuite
         Some(SparkFixes.castsZeroWithAnyExponentToDecimal(version)))
     assert(
       cast.map(_.getDecimalTinyToZero) == 
Some(SparkFixes.castsTinyStringToDecimalZero(version)))
+    assert(cast.map(_.getDecimalFullDivisorRange) == 
Some(Runtime.version().feature() >= 25))

Review Comment:
   Removed this assertion along with the driver JDK flag and its version 
predicate in dfbdbc5. The [parser 
tests](https://github.com/apache/datafusion-comet/blob/dfbdbc54318e956451e2e99c697f7af1ea436249/native/spark-expr/src/conversion_funcs/string.rs#L3004-L3064)
 now exercise the last accepted and first rejected scale drops with both 
settings of `tiny_to_zero`.



##########
native/spark-expr/src/conversion_funcs/string.rs:
##########
@@ -1002,22 +917,37 @@ fn parse_decimal_str(
         }
     }
 
-    let integral_value = digits_to_i128(integral_part)
-        .ok_or_else(|| invalid_decimal_cast(original_str, precision, scale))?;
-
-    let fractional_value = digits_to_i128(fractional_part)
-        .ok_or_else(|| invalid_decimal_cast(original_str, precision, scale))?;
-
-    // Combine: value = integral * 10^fractional_scale + fractional.
-    // A fractional_scale beyond 38 cannot fit in an i128, so pow10_i128 
returns None and this
-    // maps to the invalid-decimal error path instead of panicking.
-    let mantissa = pow10_i128(fractional_scale as u32)
-        .and_then(|p| integral_value.checked_mul(p))
-        .and_then(|v| v.checked_add(fractional_value))
-        .ok_or_else(|| invalid_decimal_cast(original_str, precision, scale))?;
+    let adjustment = i64::from(scale) - i64::from(final_scale);
+    let digits = || significant_digits(integral_part, fractional_part) as i64;
+    if adjustment > 38 {
+        return Ok((digits() == 0).then_some(0));
+    }
+    if -adjustment > max_power_of_ten(options.decimal_full_divisor_range) {
+        let digits = digits();
+        // SPARK-60119 avoids the divisor for values below 0.01 ulp or above
+        // the target precision. Otherwise setScale needs it for a nonzero 
value.
+        let needs_divisor = !options.decimal_tiny_to_zero

Review Comment:
   I kept the window because Spark still calls `setScale` when the significant 
digit count minus the drop is between −1 and the target precision. For example, 
with as many significant digits as places dropped, Spark still needs the 
divisor. Skipping the check there could return a rounded value where Spark 
fails. The [boundary 
test](https://github.com/apache/datafusion-comet/blob/dfbdbc54318e956451e2e99c697f7af1ea436249/native/spark-expr/src/conversion_funcs/string.rs#L3067-L3074)
 passes digit counts directly so it can cover both endpoints without allocating 
a 536-million-digit string.



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