Surajsuthar commented on code in PR #12462:
URL: https://github.com/apache/arrow-rs/pull/12462#discussion_r4716518618
##########
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:
write_decimal can fail if the fixed buffer is insufficient,
lexical_core::parse can fail if the bytes are not a valid floating-point
literal, but i think valid Arrow decimal values and the current buffer size
neither is expected to fail and fallback preserves can previous behavior
without panicking.
--
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]