Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion arrow-cast/benches/cast_decimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,5 +245,31 @@ fn to_integer(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, downscale, downscale_small, to_integer);
fn to_float(c: &mut Criterion) {
let mut group = c.benchmark_group("decimal_to_float");
bench_cast(
&mut group,
"decimal128 (15, 6) -> float64 all fast-path",
inputs::<Decimal128Type>(15, 38, 6),
DataType::Float64,
&[true],
);
bench_cast(
&mut group,
"decimal128 (38, 6) -> float64 large values",
inputs::<Decimal128Type>(38, 38, 6),
DataType::Float64,
&[true],
);
bench_cast(
&mut group,
"decimal256 (60, 20) -> float64 large values",
inputs::<Decimal256Type>(60, 76, 20),
DataType::Float64,
&[true],
);
group.finish();
}

criterion_group!(benches, downscale, downscale_small, to_integer, to_float);
criterion_main!(benches);
119 changes: 98 additions & 21 deletions arrow-cast/src/cast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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;

Copy link
Copy Markdown
Contributor

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?

Copy link
Copy Markdown
Contributor Author

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


/// 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.
///
Expand All @@ -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 {
Expand All @@ -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())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in what cases can this fail?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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.
Expand All @@ -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.
Expand All @@ -144,17 +199,27 @@ where
F: Fn(D::Native) -> f64,
{
let array = array.as_primitive::<D>();

if scale == 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The 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)
Expand All @@ -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)
Expand All @@ -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,
Expand All @@ -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)
Expand All @@ -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)
Expand Down