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


##########
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)
+        .and_then(|whole| whole.checked_add(rescale_digits(fractional_part, 
adjustment)?));
+    match magnitude {

Review Comment:
   I ran all nine decimal cases in `cast_from_string` against upstream `main` 
(c7a0480), the previous head (61d3e7b), and the revised head (dfbdbc5), 
interleaving the binaries across six rounds on the same CPU. The PR description 
now has the per-case medians. Compared with main, the revised head took 
16.6–17.1% less time for `decimal_38_10`, 23.1–23.5% less for `decimal_18_2`, 
and 9.3–12.0% less for the Unicode cases. [Full benchmark 
output](https://github.com/sunchao/arrow-datafusion-comet/actions/runs/46546832107/job/142299674650).



##########
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:
   I reproduced the wide-target slowdown and added a [single-multiply 
path](https://github.com/apache/datafusion-comet/blob/dfbdbc54318e956451e2e99c697f7af1ea436249/native/spark-expr/src/conversion_funcs/string.rs#L968-L981)
 in dfbdbc5. When no digits are dropped and the coefficient has at most 19 
digits, we accumulate it once in a `u64` and scale it once. This also handles 
exponents when no rounding is needed. For legacy `DECIMAL(38,10)`, the median 
per 8,192 rows was 212.22 µs on main, 231.45 µs on the previous head, and 
175.95 µs on the revised head. ANSI and TRY showed the same improvement. 
[Interleaved benchmark 
results](https://github.com/sunchao/arrow-datafusion-comet/actions/runs/46546832107/job/142299674650).



##########
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:
   Agreed that the driver version cannot tell us which `BigInteger.pow` 
implementation runs on the executors. I removed the proto field and `JdkFixes` 
in dfbdbc5. Comet uses the OpenJDK 17–24 limit, and the [compatibility 
guide](https://github.com/apache/datafusion-comet/blob/dfbdbc54318e956451e2e99c697f7af1ea436249/docs/source/user-guide/latest/compatibility/expressions/_category_template/cast.md#L84-L92)
 calls out the 536,870,920–646,456,992 window on JDK 25 and backports. The 
[parser 
tests](https://github.com/apache/datafusion-comet/blob/dfbdbc54318e956451e2e99c697f7af1ea436249/native/spark-expr/src/conversion_funcs/string.rs#L3004-L3064)
 cover both sides of the fixed limit. The separate digit-count test covers 
Spark’s magnitude check without a half-gigabyte input.



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