Skip to content

feat(arrow-cast): optimize decimal to float casts - #12462

Draft
Surajsuthar wants to merge 4 commits into
apache:mainfrom
Surajsuthar:feat/#11397-optimize-round-casting
Draft

Surajsuthar wants to merge 4 commits into
apache:mainfrom
Surajsuthar:feat/#11397-optimize-round-casting

Conversation

@Surajsuthar

Copy link
Copy Markdown
Contributor

Which issue does this PR close?

Rationale for this change

  • Avoid intermediate String allocations in the correctly rounded fallback path.
  • Use a stack-backed decimal buffer with lexical-core parsing.
  • Improve exact-boundary handling for f32 and f64.

What changes are included in this PR?

Are these changes tested?

yes cargo test -p arrow-cast

Are there any significant user-facing changes?

AI usage disclosure

@boring-cyborg boring-cyborg Bot added the arrow-kernels Crates that transform arrow data (e.g. compute, cast) label Nov 25, 2026
@Surajsuthar
Surajsuthar marked this pull request as draft November 25, 2026 09:17
@Surajsuthar

Copy link
Copy Markdown
Contributor Author

@Jefffrey Hey, could u look out

@Jefffrey Jefffrey left a comment

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.

do we have some benchmark numbers to show this is a performance improvement?

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

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.

{
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

@Surajsuthar

Copy link
Copy Markdown
Contributor Author

do we have some benchmark numbers to show this is a performance improvement?

mainly it improves the large-decimal fallback path by avoiding the intermediate String allocation (which were previously doing D::format_decimal(x, u8::MAX, scale).parse::<f64>()) and parsing directly from a stack-backed buffer, fast-path case is effectively unchanged.

@Surajsuthar

Copy link
Copy Markdown
Contributor Author

run benchmark format_decimal

@adriangbot

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

arrow-kernels Crates that transform arrow data (e.g. compute, cast)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize casting round decimal to float conversions once

3 participants