Skip to content

Commit 7696432

Browse files
AdamGSalamb
andauthored
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]>
1 parent df8b38e commit 7696432

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

arrow-cast/src/cast/decimal.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ impl DecimalCast for i64 {
8181
}
8282

8383
fn from_f64(n: f64) -> Option<Self> {
84-
n.to_i64()
84+
// Call implementation explicitly otherwise this resolves to `to_i64`
85+
// in arrow-buffer that behaves differently.
86+
num::traits::ToPrimitive::to_i64(&n)
8587
}
8688
}
8789

arrow-cast/src/cast/mod.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3101,6 +3101,35 @@ mod tests {
31013101
);
31023102
}
31033103

3104+
#[test]
3105+
fn test_cast_floating_to_decimals() {
3106+
for output_type in [
3107+
DataType::Decimal32(9, 3),
3108+
DataType::Decimal64(9, 3),
3109+
DataType::Decimal128(9, 3),
3110+
DataType::Decimal256(9, 3),
3111+
] {
3112+
let input_type = DataType::Float64;
3113+
assert!(can_cast_types(&input_type, &output_type));
3114+
3115+
let array = vec![Some(1.1_f64)];
3116+
let array = PrimitiveArray::<Float64Type>::from_iter(array);
3117+
let result = cast_with_options(
3118+
&array,
3119+
&output_type,
3120+
&CastOptions {
3121+
safe: false,
3122+
format_options: FormatOptions::default(),
3123+
},
3124+
);
3125+
assert!(
3126+
result.is_ok(),
3127+
"Failed to cast to {output_type} with: {}",
3128+
result.unwrap_err()
3129+
);
3130+
}
3131+
}
3132+
31043133
#[test]
31053134
fn test_cast_decimal128_to_decimal128_overflow() {
31063135
let input_type = DataType::Decimal128(38, 3);

parquet-testing

Submodule parquet-testing updated 276 files

0 commit comments

Comments
 (0)