This is an automated email from the ASF dual-hosted git repository.
github-merge-queue[bot] pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git
The following commit(s) were added to refs/heads/main by this push:
new dfef34e677 refactor(cast): stop passing `base` as parameter and use
wrapping div (#11369)
dfef34e677 is described below
commit dfef34e67737ee6847ded8b15f49751719375f1b
Author: Jeffrey Vo <[email protected]>
AuthorDate: Sat Oct 10 08:45:30 2026 +0000
refactor(cast): stop passing `base` as parameter and use wrapping div
(#11369)
# Which issue does this PR close?
<!--
It is not necessary to raise an issue, but it is encouraged if it is a
non-trivial
enhancement/bug.
If this PR relates to an existing issue, but does not fully resolve it,
please change
the wording below from "Closes #NNN" to "Relates to #NNN" or "Part of
#NNN" to avoid
false closing an issue.
-->
- N/A
# Rationale for this change
<!--
Why are you proposing this change? If this is already explained clearly
in the
issue then this section is not needed. Explaining clearly why changes
are proposed
helps reviewers understand your changes and offer better suggestions for
fixes.
Please try to explain the problem you are trying to solve in terms of
the user-visible
behavior, rather than the implementation.
-->
Simplifying some internal code.
# What changes are included in this PR?
<!--
There is no need to duplicate the description in the issue here but it
is
sometimes worth providing a summary of the individual changes in this
PR.
-->
Stop passing `base` around as a parameter, we can calculate it in the
function that needs it.
Change `div_checked` -> `div_wrapping` as it can never fail.
# Are these changes tested?
<!--
We typically require tests for all PRs in order to:
1. Prevent the code from being accidentally broken by subsequent changes
2. Serve as another way to document the expected behavior of the code
If tests are not included in your PR, please explain why (for example,
are they
covered by existing tests, if so identify these tests)?
If this PR claims a performance improvement, please include evidence
such as
benchmark results.
DO NOT simply paste a list of commands ran to verify the code; this is
what our
CI is for and it pollutes the PR. ONLY explain if testing was included
with this
PR, or why it wasn't.
-->
Covered by existing test suite in arrow-cast
# Are there any significant user-facing changes?
<!--
Significant user-facing changes include:
- Breaking public API change
- Significant change to existing functionality (e.g. function returns
something different)
These need to be explicitly called out to ensure we have proper
documentation if
required and to ensure we follow proper semantic versioning.
DO NOT restate the changes of this PR; if a bug was fixed and already
explained
above, we don't need this to be restated as a user-facing change.
-->
No
---
arrow-cast/src/cast/decimal.rs | 27 +++++++++--------
arrow-cast/src/cast/mod.rs | 66 +++++++++++++++---------------------------
2 files changed, 36 insertions(+), 57 deletions(-)
diff --git a/arrow-cast/src/cast/decimal.rs b/arrow-cast/src/cast/decimal.rs
index be98b4b4df..cfc8e3e624 100644
--- a/arrow-cast/src/cast/decimal.rs
+++ b/arrow-cast/src/cast/decimal.rs
@@ -717,7 +717,6 @@ where
pub(crate) fn cast_decimal_to_integer<D, T>(
array: &dyn Array,
- base: D::Native,
scale: i8,
cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError>
@@ -729,13 +728,16 @@ where
{
let array = array.as_primitive::<D>();
- let div: D::Native = base.pow_checked(scale.unsigned_abs() as
u32).map_err(|_| {
- ArrowError::CastError(format!(
- "Cannot cast to {:?}. The scale {} causes overflow.",
- D::PREFIX,
- scale,
- ))
- })?;
+ // div is always > 0, so div_wrapping(div) can never overflow/panic
+ let div: D::Native = D::Native::usize_as(10)
+ .pow_checked(scale.unsigned_abs() as u32)
+ .map_err(|_| {
+ ArrowError::CastError(format!(
+ "Cannot cast to {:?}. The scale {} causes overflow.",
+ D::PREFIX,
+ scale,
+ ))
+ })?;
let mut value_builder = PrimitiveBuilder::<T>::with_capacity(array.len());
@@ -783,11 +785,8 @@ where
if array.is_null(i) {
value_builder.append_null();
} else {
- let v = array
- .value(i)
- .div_checked(div)
- .ok()
- .and_then(<T::Native as
NumCast>::from::<D::Native>);
+ let v = array.value(i).div_wrapping(div);
+ let v = <T::Native as NumCast>::from(v);
value_builder.append_option(v);
}
}
@@ -797,7 +796,7 @@ where
if array.is_null(i) {
value_builder.append_null();
} else {
- let v = array.value(i).div_checked(div)?;
+ let v = array.value(i).div_wrapping(div);
let value =
<T::Native as
NumCast>::from::<D::Native>(v).ok_or_else(|| {
diff --git a/arrow-cast/src/cast/mod.rs b/arrow-cast/src/cast/mod.rs
index e81585ec13..9b92493206 100644
--- a/arrow-cast/src/cast/mod.rs
+++ b/arrow-cast/src/cast/mod.rs
@@ -525,7 +525,6 @@ fn cast_integer_to_decimal<
array: &PrimitiveArray<T>,
precision: u8,
scale: i8,
- base: M,
cast_options: &CastOptions,
) -> Result<ArrayRef, ArrowError>
where
@@ -560,6 +559,9 @@ where
// Compute the scale factor once in the source type. Scaling before the
// checked conversion permits values that only fit the decimal native
// type after scaling.
+ //
+ // scale_factor is always > 0, so div_wrapping(scale_factor) can never
+ // overflow/panic
let scale_factor = T::Native::usize_as(10)
.pow_checked(scale.unsigned_abs() as u32)
.ok();
@@ -570,17 +572,11 @@ where
.expect("value fits the decimal")
}),
(Some(scale_factor), true) => array.unary_opt::<_, D>(|v| {
- let v = v
- .div_checked(scale_factor)
- .ok()
- .and_then(integer_to_decimal_native::<_, M>)?;
+ let v = integer_to_decimal_native::<_,
M>(v.div_wrapping(scale_factor))?;
(D::is_valid_decimal_precision(v, precision)).then_some(v)
}),
(Some(scale_factor), false) => array.try_unary::<_, D, _>(|v| {
- let v = v
- .div_checked(scale_factor)
- .ok()
- .and_then(integer_to_decimal_native::<_, M>)
+ let v = integer_to_decimal_native::<_,
M>(v.div_wrapping(scale_factor))
.ok_or_else(|| overflow(v))?;
D::validate_decimal_precision(v, precision, scale).map(|()| v)
})?,
@@ -591,14 +587,16 @@ where
(None, _) => array.unary::<_, D>(|_| M::ZERO),
}
} else {
- let scale_factor = base.pow_checked(scale.unsigned_abs() as
u32).map_err(|_| {
- ArrowError::CastError(format!(
- "Cannot cast to {:?}({}, {}). The scale causes overflow.",
- D::PREFIX,
- precision,
- scale,
- ))
- })?;
+ let scale_factor = M::usize_as(10)
+ .pow_checked(scale.unsigned_abs() as u32)
+ .map_err(|_| {
+ ArrowError::CastError(format!(
+ "Cannot cast to {:?}({}, {}). The scale causes overflow.",
+ D::PREFIX,
+ precision,
+ scale,
+ ))
+ })?;
if all_fit {
array.unary::<_, D>(|v| {
@@ -1123,7 +1121,6 @@ pub fn cast_with_options(
(Decimal32(_, scale), _) if !to_type.is_temporal() => {
cast_from_decimal::<Decimal32Type, _>(
array,
- 10_i32,
scale,
from_type,
to_type,
@@ -1134,7 +1131,6 @@ pub fn cast_with_options(
(Decimal64(_, scale), _) if !to_type.is_temporal() => {
cast_from_decimal::<Decimal64Type, _>(
array,
- 10_i64,
scale,
from_type,
to_type,
@@ -1145,7 +1141,6 @@ pub fn cast_with_options(
(Decimal128(_, scale), _) if !to_type.is_temporal() => {
cast_from_decimal::<Decimal128Type, _>(
array,
- 10_i128,
scale,
from_type,
to_type,
@@ -1156,7 +1151,6 @@ pub fn cast_with_options(
(Decimal256(_, scale), _) if !to_type.is_temporal() => {
cast_from_decimal::<Decimal256Type, _>(
array,
- i256::from_i128(10_i128),
scale,
from_type,
to_type,
@@ -1168,7 +1162,6 @@ pub fn cast_with_options(
(_, Decimal32(precision, scale)) if !from_type.is_temporal() => {
cast_to_decimal::<Decimal32Type, _>(
array,
- 10_i32,
precision,
scale,
from_type,
@@ -1179,7 +1172,6 @@ pub fn cast_with_options(
(_, Decimal64(precision, scale)) if !from_type.is_temporal() => {
cast_to_decimal::<Decimal64Type, _>(
array,
- 10_i64,
precision,
scale,
from_type,
@@ -1190,7 +1182,6 @@ pub fn cast_with_options(
(_, Decimal128(precision, scale)) if !from_type.is_temporal() => {
cast_to_decimal::<Decimal128Type, _>(
array,
- 10_i128,
precision,
scale,
from_type,
@@ -1201,7 +1192,6 @@ pub fn cast_with_options(
(_, Decimal256(precision, scale)) if !from_type.is_temporal() => {
cast_to_decimal::<Decimal256Type, _>(
array,
- i256::from_i128(10_i128),
precision,
scale,
from_type,
@@ -2145,7 +2135,6 @@ pub fn cast_with_options(
fn cast_from_decimal<D, F>(
array: &dyn Array,
- base: D::Native,
scale: &i8,
from_type: &DataType,
to_type: &DataType,
@@ -2160,14 +2149,14 @@ where
use DataType::*;
// cast decimal to other type
match to_type {
- UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, base, *scale,
cast_options),
- UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, base,
*scale, cast_options),
- UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, base,
*scale, cast_options),
- UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, base,
*scale, cast_options),
- Int8 => cast_decimal_to_integer::<D, Int8Type>(array, base, *scale,
cast_options),
- Int16 => cast_decimal_to_integer::<D, Int16Type>(array, base, *scale,
cast_options),
- Int32 => cast_decimal_to_integer::<D, Int32Type>(array, base, *scale,
cast_options),
- Int64 => cast_decimal_to_integer::<D, Int64Type>(array, base, *scale,
cast_options),
+ UInt8 => cast_decimal_to_integer::<D, UInt8Type>(array, *scale,
cast_options),
+ UInt16 => cast_decimal_to_integer::<D, UInt16Type>(array, *scale,
cast_options),
+ UInt32 => cast_decimal_to_integer::<D, UInt32Type>(array, *scale,
cast_options),
+ UInt64 => cast_decimal_to_integer::<D, UInt64Type>(array, *scale,
cast_options),
+ Int8 => cast_decimal_to_integer::<D, Int8Type>(array, *scale,
cast_options),
+ Int16 => cast_decimal_to_integer::<D, Int16Type>(array, *scale,
cast_options),
+ Int32 => cast_decimal_to_integer::<D, Int32Type>(array, *scale,
cast_options),
+ Int64 => cast_decimal_to_integer::<D, Int64Type>(array, *scale,
cast_options),
Float16 => cast_decimal_to_float::<D, Float16Type, _>(array, |x| {
half::f16::from_f64(single_decimal_to_float_lossy::<D, F>(
&as_float,
@@ -2189,7 +2178,6 @@ where
fn cast_to_decimal<D, M>(
array: &dyn Array,
- base: M,
precision: &u8,
scale: &i8,
from_type: &DataType,
@@ -2207,56 +2195,48 @@ where
array.as_primitive::<UInt8Type>(),
*precision,
*scale,
- base,
cast_options,
),
UInt16 => cast_integer_to_decimal::<_, D, _>(
array.as_primitive::<UInt16Type>(),
*precision,
*scale,
- base,
cast_options,
),
UInt32 => cast_integer_to_decimal::<_, D, _>(
array.as_primitive::<UInt32Type>(),
*precision,
*scale,
- base,
cast_options,
),
UInt64 => cast_integer_to_decimal::<_, D, _>(
array.as_primitive::<UInt64Type>(),
*precision,
*scale,
- base,
cast_options,
),
Int8 => cast_integer_to_decimal::<_, D, _>(
array.as_primitive::<Int8Type>(),
*precision,
*scale,
- base,
cast_options,
),
Int16 => cast_integer_to_decimal::<_, D, _>(
array.as_primitive::<Int16Type>(),
*precision,
*scale,
- base,
cast_options,
),
Int32 => cast_integer_to_decimal::<_, D, _>(
array.as_primitive::<Int32Type>(),
*precision,
*scale,
- base,
cast_options,
),
Int64 => cast_integer_to_decimal::<_, D, _>(
array.as_primitive::<Int64Type>(),
*precision,
*scale,
- base,
cast_options,
),
Float16 => cast_floating_point_to_decimal::<_, D>(