comphead commented on code in PR #20189:
URL: https://github.com/apache/datafusion/pull/20189#discussion_r2777860472
##########
datafusion/spark/src/function/math/negative.rs:
##########
@@ -147,56 +189,154 @@ fn spark_negative(args: &[ColumnarValue]) ->
Result<ColumnarValue> {
Ok(ColumnarValue::Array(Arc::new(result)))
}
- // Decimal types - wrapping negation
+ // Decimal types - use checked negation in ANSI mode, wrapping in
legacy mode
DataType::Decimal32(_, _) => {
let array = array.as_primitive::<Decimal32Type>();
- let result: PrimitiveArray<Decimal32Type> =
- array.unary(|x| x.wrapping_neg());
+ let result: PrimitiveArray<Decimal32Type> = if
enable_ansi_mode {
+ array
+ .try_unary(|x| {
+ x.checked_neg().ok_or_else(|| {
+ ArrowError::ComputeError(format!(
+ "Decimal32 overflow on negative({x})"
+ ))
+ })
+ })?
+ .with_data_type(array.data_type().clone())
+ } else {
+ array.unary(|x| x.wrapping_neg())
+ };
Ok(ColumnarValue::Array(Arc::new(result)))
}
DataType::Decimal64(_, _) => {
let array = array.as_primitive::<Decimal64Type>();
- let result: PrimitiveArray<Decimal64Type> =
- array.unary(|x| x.wrapping_neg());
+ let result: PrimitiveArray<Decimal64Type> = if
enable_ansi_mode {
+ array
+ .try_unary(|x| {
+ x.checked_neg().ok_or_else(|| {
+ ArrowError::ComputeError(format!(
+ "Decimal64 overflow on negative({x})"
+ ))
+ })
+ })?
+ .with_data_type(array.data_type().clone())
+ } else {
+ array.unary(|x| x.wrapping_neg())
+ };
Ok(ColumnarValue::Array(Arc::new(result)))
}
DataType::Decimal128(_, _) => {
let array = array.as_primitive::<Decimal128Type>();
- let result: PrimitiveArray<Decimal128Type> =
- array.unary(|x| x.wrapping_neg());
+ let result: PrimitiveArray<Decimal128Type> = if
enable_ansi_mode {
Review Comment:
I would suggest having a local macro or generic to reduce boilerplate
--
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]