This is an automated email from the ASF dual-hosted git repository.
alamb 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 769643206f Fix casting floats to Decimal64 (#8363)
769643206f is described below
commit 769643206fa230a41d60cf3a9044a073b91f431e
Author: Adam Gutglick <[email protected]>
AuthorDate: Tue Sep 16 19:39:42 2025 +0100
Fix casting floats to Decimal64 (#8363)
# Which issue does this PR close?
Closes #8362
# Rationale for this change
Fixes casting codepaths that currently fail.
# What changes are included in this PR?
# Are these changes tested?
yes
If tests are not included in your PR, please explain why (for example,
are they covered by existing tests)?
# Are there any user-facing changes?
Code should now behave as expected
---------
Co-authored-by: Andrew Lamb <[email protected]>
---
arrow-cast/src/cast/decimal.rs | 4 +++-
arrow-cast/src/cast/mod.rs | 29 +++++++++++++++++++++++++++++
parquet-testing | 2 +-
3 files changed, 33 insertions(+), 2 deletions(-)
diff --git a/arrow-cast/src/cast/decimal.rs b/arrow-cast/src/cast/decimal.rs
index 00bfc57e12..095e312748 100644
--- a/arrow-cast/src/cast/decimal.rs
+++ b/arrow-cast/src/cast/decimal.rs
@@ -81,7 +81,9 @@ impl DecimalCast for i64 {
}
fn from_f64(n: f64) -> Option<Self> {
- n.to_i64()
+ // Call implementation explicitly otherwise this resolves to `to_i64`
+ // in arrow-buffer that behaves differently.
+ num::traits::ToPrimitive::to_i64(&n)
}
}
diff --git a/arrow-cast/src/cast/mod.rs b/arrow-cast/src/cast/mod.rs
index 117ad10b11..fc241bea48 100644
--- a/arrow-cast/src/cast/mod.rs
+++ b/arrow-cast/src/cast/mod.rs
@@ -3101,6 +3101,35 @@ mod tests {
);
}
+ #[test]
+ fn test_cast_floating_to_decimals() {
+ for output_type in [
+ DataType::Decimal32(9, 3),
+ DataType::Decimal64(9, 3),
+ DataType::Decimal128(9, 3),
+ DataType::Decimal256(9, 3),
+ ] {
+ let input_type = DataType::Float64;
+ assert!(can_cast_types(&input_type, &output_type));
+
+ let array = vec![Some(1.1_f64)];
+ let array = PrimitiveArray::<Float64Type>::from_iter(array);
+ let result = cast_with_options(
+ &array,
+ &output_type,
+ &CastOptions {
+ safe: false,
+ format_options: FormatOptions::default(),
+ },
+ );
+ assert!(
+ result.is_ok(),
+ "Failed to cast to {output_type} with: {}",
+ result.unwrap_err()
+ );
+ }
+ }
+
#[test]
fn test_cast_decimal128_to_decimal128_overflow() {
let input_type = DataType::Decimal128(38, 3);
diff --git a/parquet-testing b/parquet-testing
index 5cbfc43d48..a3d96a65e1 160000
--- a/parquet-testing
+++ b/parquet-testing
@@ -1 +1 @@
-Subproject commit 5cbfc43d488c9c8404a1a7088cca400ae095b831
+Subproject commit a3d96a65e11e2bbca7d22a894e8313ede90a33a3