viirya commented on code in PR #7716:
URL: https://github.com/apache/datafusion-comet/pull/7716#discussion_r4703070353
##########
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:
This flag only changes the result for a nonzero value whose scale drop is
between 536,870,920 and 646,456,992, on a Spark without SPARK-60119 running on
JDK 25. In that window Spark spends minutes and hundreds of MB per row
computing `10^n` before it returns 0. Keeping the ANSI failure for `'9'*40 +
'e-2147483647'` only needs one limit. Could we use the JDK 17–24 constant and
document the JDK 25 window as a known difference? That would avoid a proto
field, which is hard to remove later. If you'd rather keep the flag, could the
JDK check move into a named predicate next to `SparkFixes`?
##########
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:
The file now defaults to non-ANSI, but the header comment above still says
"ANSI mode". Could it say that the first two queries run in both modes and the
rest of the file runs under ANSI?
##########
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:
This assertion restates the expression from `CometCast`, so it can't fail.
If the flag stays, testing the named predicate on its own would mean something.
##########
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:
When `decimal_tiny_to_zero` is set, `digits + adjustment` can only land in
`[-1, precision]` here if the string has more than ~536 million significant
digits, so no test can reach this branch. Would `digits != 0 &&
!options.decimal_tiny_to_zero` be enough? Outside that range, the rescale below
already returns the same result.
--
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]