Repository navigation
feat(arrow-cast): optimize decimal to float casts #12462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dae7b52
d14f97c
4053faf
cb6c78f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,7 @@ pub use crate::cast::union::*; | |
|
|
||
| use chrono::{TimeZone, Utc}; | ||
| use std::cmp::Ordering; | ||
| use std::fmt; | ||
| use std::sync::Arc; | ||
|
|
||
| use crate::display::{ArrayFormatter, FormatOptions}; | ||
|
|
@@ -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; | ||
|
|
||
| /// The largest decimal native value has 78 characters, including its sign. | ||
| /// The scale may append up to 128 zeroes for a valid Arrow decimal value. | ||
| const DECIMAL_FLOAT_BUFFER_SIZE: usize = 78 + 128 + 2; | ||
|
|
||
| struct DecimalFloatBuffer { | ||
| bytes: [u8; DECIMAL_FLOAT_BUFFER_SIZE], | ||
| len: usize, | ||
| } | ||
|
|
||
| impl DecimalFloatBuffer { | ||
| #[inline] | ||
| fn as_bytes(&self) -> &[u8] { | ||
| &self.bytes[..self.len] | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Write for DecimalFloatBuffer { | ||
| fn write_str(&mut self, value: &str) -> fmt::Result { | ||
| let end = self.len.checked_add(value.len()).ok_or(fmt::Error)?; | ||
| let target = self.bytes.get_mut(self.len..end).ok_or(fmt::Error)?; | ||
| target.copy_from_slice(value.as_bytes()); | ||
| self.len = end; | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Lossy conversion from decimal to float. | ||
| /// | ||
|
|
@@ -95,7 +122,10 @@ where | |
| let unscaled = f(x); | ||
| // Both operands are exact in this range, so this rounds once. A negative scale | ||
| // multiplies, since `10^-scale` is the exact power of ten there. | ||
| if (-22..=22).contains(&scale) && unscaled.abs() < F64_EXACT_INT_LIMIT { | ||
| if (-22..=22).contains(&scale) | ||
| && (unscaled.abs() < F64_EXACT_INT_LIMIT as f64 | ||
| || decimal_value_fits_float_fast_path(x, F64_EXACT_INT_LIMIT)) | ||
| { | ||
| return if scale >= 0 { | ||
| unscaled / 10_f64.powi(scale) | ||
| } else { | ||
|
|
@@ -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()) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. in what cases can this fail?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 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. |
||
| } | ||
|
|
||
| /// As [`decimal_to_f64_rounded_once`], but narrowing to `f32` in one step. | ||
|
|
@@ -127,10 +167,25 @@ fn decimal_to_f64_rounded_once<D: DecimalType>(x: D::Native, scale: i8, unscaled | |
| #[cold] | ||
| #[inline(never)] | ||
| fn decimal_to_f32_rounded_once<D: DecimalType>(x: D::Native, scale: i8, unscaled: f64) -> f32 { | ||
| // Unreachable, as in [`decimal_to_f64_rounded_once`]. | ||
| D::format_decimal(x, u8::MAX, scale) | ||
| .parse::<f32>() | ||
| .unwrap_or_else(|_| unscaled as f32 / 10_f32.powi(scale.into())) | ||
| let mut buffer = DecimalFloatBuffer { | ||
| bytes: [0; DECIMAL_FLOAT_BUFFER_SIZE], | ||
| len: 0, | ||
| }; | ||
|
|
||
| if arrow_data::decimal::write_decimal(&mut buffer, x, scale).is_ok() | ||
| && let Ok(value) = lexical_core::parse::<f32>(buffer.as_bytes()) | ||
| { | ||
| return value; | ||
| } | ||
|
|
||
| unscaled as f32 / 10_f32.powi(scale.into()) | ||
| } | ||
|
|
||
| #[inline] | ||
| fn decimal_value_fits_float_fast_path<N: ArrowNativeType>(value: N, limit: i64) -> bool { | ||
| value | ||
| .to_i64() | ||
| .is_some_and(|value| (-limit..=limit).contains(&value)) | ||
| } | ||
|
|
||
| /// Casts a decimal array to `Float64`, rounding each value once. | ||
|
|
@@ -144,17 +199,27 @@ where | |
| F: Fn(D::Native) -> f64, | ||
| { | ||
| let array = array.as_primitive::<D>(); | ||
|
|
||
| if scale == 0 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why are we adding a separate path for scale 0 here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so reason i separate this path only for that if scale is 0 then integer-to-float can directly converted without any additional work no div, mul, power calculation, or per-value scale branch is needed, we could add on slow path as well wont make much difference |
||
| let values = array.unary::<_, Float64Type>(as_float); | ||
| return Ok(Arc::new(values)); | ||
| } | ||
|
|
||
| // No power of ten outside this range is exactly representable. | ||
| if !(-22..=22).contains(&scale) { | ||
| let values = array | ||
| .unary::<_, Float64Type>(|x| decimal_to_f64_rounded_once::<D>(x, scale, as_float(x))); | ||
| return Ok(Arc::new(values)); | ||
| } | ||
|
|
||
| let pow = 10_f64.powi(scale.unsigned_abs().into()); | ||
| let values = if scale >= 0 { | ||
|
|
||
| let values = if scale > 0 { | ||
| array.unary::<_, Float64Type>(|x| { | ||
| let unscaled = as_float(x); | ||
| if unscaled.abs() < F64_EXACT_INT_LIMIT { | ||
| if unscaled.abs() < F64_EXACT_INT_LIMIT as f64 | ||
| || decimal_value_fits_float_fast_path(x, F64_EXACT_INT_LIMIT) | ||
| { | ||
| unscaled / pow | ||
| } else { | ||
| decimal_to_f64_rounded_once::<D>(x, scale, unscaled) | ||
|
|
@@ -163,7 +228,9 @@ where | |
| } else { | ||
| array.unary::<_, Float64Type>(|x| { | ||
| let unscaled = as_float(x); | ||
| if unscaled.abs() < F64_EXACT_INT_LIMIT { | ||
| if unscaled.abs() < F64_EXACT_INT_LIMIT as f64 | ||
| || decimal_value_fits_float_fast_path(x, F64_EXACT_INT_LIMIT) | ||
| { | ||
| unscaled * pow | ||
| } else { | ||
| decimal_to_f64_rounded_once::<D>(x, scale, unscaled) | ||
|
|
@@ -175,7 +242,7 @@ where | |
|
|
||
| /// Casts a decimal array to `Float32`, rounding each value once. Same shape as | ||
| /// [`cast_decimal_to_f64`], with the smaller bounds an `f32` allows: integers are | ||
| /// exact below 2^24, and `10^k` only up to `k = 10`. | ||
| /// exact at or below 2^24, and `10^k` only up to `k = 10`. | ||
| fn cast_decimal_to_f32<D, F>( | ||
| array: &dyn Array, | ||
| as_float: &F, | ||
|
|
@@ -186,17 +253,25 @@ where | |
| F: Fn(D::Native) -> f64, | ||
| { | ||
| let array = array.as_primitive::<D>(); | ||
| if scale == 0 { | ||
| let values = array.unary::<_, Float32Type>(|x| as_float(x) as f32); | ||
| return Ok(Arc::new(values)); | ||
| } | ||
|
|
||
| // No power of ten outside this range is exactly representable. | ||
| if !(-10..=10).contains(&scale) { | ||
| let values = array | ||
| .unary::<_, Float32Type>(|x| decimal_to_f32_rounded_once::<D>(x, scale, as_float(x))); | ||
| return Ok(Arc::new(values)); | ||
| } | ||
| let pow = 10_f32.powi(scale.unsigned_abs().into()); | ||
| let values = if scale >= 0 { | ||
|
|
||
| let values = if scale > 0 { | ||
| array.unary::<_, Float32Type>(|x| { | ||
| let unscaled = as_float(x); | ||
| if unscaled.abs() < F32_EXACT_INT_LIMIT { | ||
| if unscaled.abs() < F32_EXACT_INT_LIMIT as f64 | ||
| || decimal_value_fits_float_fast_path(x, F32_EXACT_INT_LIMIT) | ||
| { | ||
| unscaled as f32 / pow | ||
| } else { | ||
| decimal_to_f32_rounded_once::<D>(x, scale, unscaled) | ||
|
|
@@ -205,7 +280,9 @@ where | |
| } else { | ||
| array.unary::<_, Float32Type>(|x| { | ||
| let unscaled = as_float(x); | ||
| if unscaled.abs() < F32_EXACT_INT_LIMIT { | ||
| if unscaled.abs() < F32_EXACT_INT_LIMIT as f64 | ||
| || decimal_value_fits_float_fast_path(x, F32_EXACT_INT_LIMIT) | ||
| { | ||
| unscaled as f32 * pow | ||
| } else { | ||
| decimal_to_f32_rounded_once::<D>(x, scale, unscaled) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are we turning these to i64s from f64s?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only to compared with the original decimal integer value before converting it to f64