andygrove commented on code in PR #7716:
URL: https://github.com/apache/datafusion-comet/pull/7716#discussion_r4745901490
##########
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)
+ castBuilder.setDecimalFullDivisorRange(
Review Comment:
This flag comes from `Runtime.version()` on the driver, but the limit that
decides the result is the one in the JVM that runs Spark's `BigInteger.TEN.pow`
on the executors. A cluster with different driver and executor JDKs gets the
wrong value, and a build that backports JDK-8355719 into an earlier release is
not seen either. viirya raised the same design question and it has no reply
yet. Would a constant for the JDK 17 to 24 behavior, with the JDK 25 window
listed as a known difference, let us drop the proto field and `JdkFixes`? If
you keep the flag, could `test_decimal_divisor_at_magnitude_boundaries` go
through `parse_string_to_decimal`? The `(-1..=precision)` window it covers
needs about 536 million digits to reach, so the direct helper call tests a
state the real path cannot produce.
##########
native/spark-expr/src/conversion_funcs/string.rs:
##########
@@ -1002,22 +922,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)
+ && needs_decimal_divisor(
+ digits(),
+ adjustment,
+ precision,
+ options.decimal_tiny_to_zero,
+ )
+ {
+ return Err(invalid_decimal_cast(original_str, precision, scale));
+ }
- let final_mantissa = if negative { -mantissa } else { mantissa };
- Ok((final_mantissa, final_scale))
+ // Only one slice can contribute a rounding digit. If we discard integral
digits,
+ // the fractional slice contributes zero. Otherwise the integral slice is
exact.
+ let integral_shift = i64::from(exponent) + i64::from(scale);
+ let magnitude = rescale_digits(integral_part, integral_shift)
Review Comment:
This replaces the single combine-then-rescale step with two `rescale_digits`
calls, and the cost lands on the wide target. I ran the `cast_from_string`
bench groups against the commit this PR is based on, interleaving the two
binaries over five rounds. `decimal_38_10` goes from 175.2 us to 186.1 us,
which is 6% slower, and the unicode groups are 2 to 3% slower. `decimal_18_2`
improves from about 220 us to 180 us. `DECIMAL(38, s)` is the target Spark's
default decimal casts hit, so could the cost be handled before merge? Could the
case where no digit is dropped, meaning the fractional digits fit under the
scale and there is no exponent, take the old single-multiply path so the
38-digit target does not pay for two `digits_to_i128` passes and two
`checked_mul`s? If you keep this shape, could you post before and after numbers
for `decimal_38_10` and say why the slowdown is acceptable?
--
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]