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


##########
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:
   This replaces the combine-then-rescale step that every string to decimal 
cast goes through with two `rescale_digits` calls, each with its own 
`digits_to_i128` pass and `checked_mul`. The description does not give before 
and after numbers. `native/spark-expr/benches/cast_from_string.rs` already has 
the `cast_string_to_decimal/{legacy,ansi,try}` groups (`decimal_38_10`, 
`decimal_18_2`) and the unicode group, so the comparison needs no new 
benchmark. Please run it against `upstream/main` and post the numbers, as 
`docs/source/contributor-guide/optimizing_expressions.md` asks for any change 
to an existing kernel's hot path. I did not run it.



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