Skip to content

Commit 329fb91

Browse files
himadripalalamb
andauthored
add tests to check precision loss fix (#14284)
* add tests to check precision loss fix * removed println * Update datafusion/physical-expr/src/expressions/cast.rs suggestions from Andrew Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org> * Fix clippy --------- Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
1 parent 3802adf commit 329fb91

File tree

1 file changed

+40
-0
lines changed
  • datafusion/physical-expr/src/expressions

1 file changed

+40
-0
lines changed

datafusion/physical-expr/src/expressions/cast.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ mod tests {
242242
},
243243
datatypes::*,
244244
};
245+
use datafusion_common::assert_contains;
245246

246247
// runs an end-to-end test of physical type cast
247248
// 1. construct a record batch with a column "a" of type A
@@ -399,6 +400,45 @@ mod tests {
399400
Ok(())
400401
}
401402

403+
#[test]
404+
fn test_cast_decimal_to_decimal_overflow() -> Result<()> {
405+
let array = vec![Some(123456789)];
406+
407+
let decimal_array = array
408+
.clone()
409+
.into_iter()
410+
.collect::<Decimal128Array>()
411+
.with_precision_and_scale(10, 3)?;
412+
413+
let schema = Schema::new(vec![Field::new("a", Decimal128(10, 3), false)]);
414+
let batch = RecordBatch::try_new(
415+
Arc::new(schema.clone()),
416+
vec![Arc::new(decimal_array)],
417+
)?;
418+
let expression =
419+
cast_with_options(col("a", &schema)?, &schema, Decimal128(6, 2), None)?;
420+
let e = expression.evaluate(&batch).unwrap_err(); // panics on OK
421+
assert_contains!(
422+
e.to_string(),
423+
"Arrow error: Invalid argument error: 12345679 is too large to store in a Decimal128 of precision 6. Max is 999999"
424+
);
425+
426+
let expression_safe = cast_with_options(
427+
col("a", &schema)?,
428+
&schema,
429+
Decimal128(6, 2),
430+
Some(DEFAULT_SAFE_CAST_OPTIONS),
431+
)?;
432+
let result_safe = expression_safe
433+
.evaluate(&batch)?
434+
.into_array(batch.num_rows())
435+
.expect("failed to convert to array");
436+
437+
assert!(result_safe.is_null(0));
438+
439+
Ok(())
440+
}
441+
402442
#[test]
403443
fn test_cast_decimal_to_numeric() -> Result<()> {
404444
let array = vec![Some(1), Some(2), Some(3), Some(4), Some(5), None];

0 commit comments

Comments
 (0)