alamb commented on code in PR #14284:
URL: https://github.com/apache/datafusion/pull/14284#discussion_r1929762107
##########
datafusion/physical-expr/src/expressions/cast.rs:
##########
@@ -399,6 +399,50 @@ mod tests {
Ok(())
}
+ #[test]
+ fn test_cast_decimal_to_decimal_overflow() -> Result<()> {
+ let array = vec![Some(123456789)];
+
+ let decimal_array = array
+ .clone()
+ .into_iter()
+ .collect::<Decimal128Array>()
+ .with_precision_and_scale(10, 3)?;
+
+ let schema = Schema::new(vec![Field::new("a", Decimal128(10, 3),
false)]);
+ let batch = RecordBatch::try_new(
+ Arc::new(schema.clone()),
+ vec![Arc::new(decimal_array)],
+ )?;
+ let expression =
+ cast_with_options(col("a", &schema)?, &schema, Decimal128(6, 2),
None)?;
+ let result = expression.evaluate(&batch);
+
+ match result {
+ Ok(_) => panic!("expected error"),
+ Err(e) => {
+ assert!(e
+ .to_string()
+ .contains("Arrow error: Invalid argument error: 12345679
is too large to store in a Decimal128 of precision 6. Max is 999999"))
+ }
+ }
Review Comment:
FYI you can also write this more succinctly with `unwrap_err()`
```suggestion
let result = result.unwrap_err(); // panics on OK
assert_contains!(e.to_string(), "Arrow error: Invalid argument
error: 12345679 is too large to store in a Decimal128 of precision 6. Max is
999999"));
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]