Jefffrey commented on code in PR #12462:
URL: https://github.com/apache/arrow-rs/pull/12462#discussion_r4695276630


##########
arrow-cast/src/cast/mod.rs:
##########
@@ -113,11 +143,21 @@ where
 #[cold]
 #[inline(never)]
 fn decimal_to_f64_rounded_once<D: DecimalType>(x: D::Native, scale: i8, 
unscaled: f64) -> f64 {
-    // `format_decimal` always writes a decimal literal, so the parse cannot 
fail.
-    // The fallback is there to keep that from being a panic, not because it 
runs.
-    D::format_decimal(x, u8::MAX, scale)
-        .parse::<f64>()
-        .unwrap_or_else(|_| unscaled / 10_f64.powi(scale.into()))
+    let mut buffer = DecimalFloatBuffer {
+        bytes: [0; DECIMAL_FLOAT_BUFFER_SIZE],
+        len: 0,
+    };
+
+    // `write_decimal` uses the existing stack-backed decimal digit formatter.
+    // Parsing the bytes directly preserves the single-rounding guarantee while
+    // avoiding the allocation performed by `format_decimal`.
+    if arrow_data::decimal::write_decimal(&mut buffer, x, scale).is_ok()
+        && let Ok(value) = lexical_core::parse::<f64>(buffer.as_bytes())
+    {
+        return value;
+    }
+
+    unscaled / 10_f64.powi(scale.into())

Review Comment:
   in what cases can this fail?



##########
arrow-cast/src/cast/mod.rs:
##########
@@ -144,17 +199,27 @@ where
     F: Fn(D::Native) -> f64,
 {
     let array = array.as_primitive::<D>();
+
+    if scale == 0 {

Review Comment:
   why are we adding a separate path for scale 0 here?



##########
arrow-cast/src/cast/mod.rs:
##########
@@ -73,10 +74,36 @@ pub use decimal::{DecimalCast, float_to_decimal, 
float_to_decimal_opt, rescale_d
 pub use decimal::{parse_string_to_decimal_native, single_float_to_decimal};
 pub use string::cast_single_string_to_boolean_default;
 
-/// Integers below 2^53 convert to an `f64` exactly.
-const F64_EXACT_INT_LIMIT: f64 = 9_007_199_254_740_992.0;
-/// Integers below 2^24 convert to an `f32` exactly.
-const F32_EXACT_INT_LIMIT: f64 = 16_777_216.0;
+/// Integers at or below 2^53 convert to an `f64` exactly.
+const F64_EXACT_INT_LIMIT: i64 = 1 << 53;
+/// Integers at or below 2^24 convert to an `f32` exactly.
+const F32_EXACT_INT_LIMIT: i64 = 1 << 24;

Review Comment:
   why are we turning these to i64s from f64s?



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

Reply via email to