alamb commented on code in PR #4494:
URL: https://github.com/apache/arrow-rs/pull/4494#discussion_r1258196620


##########
arrow-arith/src/numeric.rs:
##########
@@ -74,6 +74,97 @@ pub fn rem(lhs: &dyn Datum, rhs: &dyn Datum) -> 
Result<ArrayRef, ArrowError> {
     arithmetic_op(Op::Rem, lhs, rhs)
 }
 
+macro_rules! neg_checked {
+    ($t:ty, $a:ident) => {{
+        let array = $a
+            .as_primitive::<$t>()
+            .try_unary::<_, $t, _>(|x| x.neg_checked())?;
+        Ok(Arc::new(array))
+    }};
+}
+
+macro_rules! neg_wrapping {
+    ($t:ty, $a:ident) => {{
+        let array = $a.as_primitive::<$t>().unary::<_, $t>(|x| 
x.neg_wrapping());
+        Ok(Arc::new(array))
+    }};
+}
+
+/// Perform `!array`, returning an error on overflow
+///
+/// Note: negation of unsigned arrays is not supported and will return in an 
error,
+/// for wrapping unsigned negation consider using [`neg_wrapping()`]
+pub fn neg(array: &dyn Array) -> Result<ArrayRef, ArrowError> {
+    use DataType::*;
+    use IntervalUnit::*;
+    use TimeUnit::*;
+
+    match array.data_type() {
+        Int8 => neg_checked!(Int8Type, array),
+        Int16 => neg_checked!(Int16Type, array),
+        Int32 => neg_checked!(Int32Type, array),
+        Int64 => neg_checked!(Int64Type, array),
+        Float16 => neg_wrapping!(Float16Type, array),
+        Float32 => neg_wrapping!(Float32Type, array),
+        Float64 => neg_wrapping!(Float64Type, array),
+        Decimal128(p, s) => {
+            let a = array
+                .as_primitive::<Decimal128Type>()
+                .try_unary::<_, Decimal128Type, _>(|x| x.neg_checked())?;
+
+            Ok(Arc::new(a.with_precision_and_scale(*p, *s)?))
+        }
+        Decimal256(p, s) => {
+            let a = array
+                .as_primitive::<Decimal256Type>()
+                .try_unary::<_, Decimal256Type, _>(|x| x.neg_checked())?;
+
+            Ok(Arc::new(a.with_precision_and_scale(*p, *s)?))
+        }
+        Duration(Second) => neg_checked!(DurationSecondType, array),
+        Duration(Millisecond) => neg_checked!(DurationMillisecondType, array),
+        Duration(Microsecond) => neg_checked!(DurationMicrosecondType, array),
+        Duration(Nanosecond) => neg_checked!(DurationNanosecondType, array),
+        Interval(YearMonth) => neg_checked!(IntervalYearMonthType, array),
+        Interval(DayTime) => {
+            let a = array
+                .as_primitive::<IntervalDayTimeType>()
+                .try_unary::<_, IntervalDayTimeType, ArrowError>(|x| {
+                    let (days, ms) = IntervalDayTimeType::to_parts(x);
+                    Ok(IntervalDayTimeType::make_value(
+                        days.neg_checked()?,
+                        ms.neg_checked()?,
+                    ))
+                })?;
+            Ok(Arc::new(a))
+        }
+        Interval(MonthDayNano) => {
+            let a = array
+                .as_primitive::<IntervalMonthDayNanoType>()
+                .try_unary::<_, IntervalMonthDayNanoType, ArrowError>(|x| {
+                let (months, days, nanos) = 
IntervalMonthDayNanoType::to_parts(x);
+                Ok(IntervalMonthDayNanoType::make_value(
+                    months.neg_checked()?,
+                    days.neg_checked()?,
+                    nanos.neg_checked()?,
+                ))
+            })?;
+            Ok(Arc::new(a))
+        }
+        t => Err(ArrowError::InvalidArgumentError(format!(
+            "Invalid arithmetic operation: !{t}"
+        ))),
+    }
+}
+
+/// Perform `!array`, wrapping on overflow for [`DataType::is_integer`]

Review Comment:
   ```suggestion
   /// Negates each element of  `array` , wrapping on overflow for 
[`DataType::is_integer`]
   ```
   
   



##########
arrow-arith/src/numeric.rs:
##########
@@ -74,6 +74,97 @@ pub fn rem(lhs: &dyn Datum, rhs: &dyn Datum) -> 
Result<ArrayRef, ArrowError> {
     arithmetic_op(Op::Rem, lhs, rhs)
 }
 
+macro_rules! neg_checked {
+    ($t:ty, $a:ident) => {{
+        let array = $a
+            .as_primitive::<$t>()
+            .try_unary::<_, $t, _>(|x| x.neg_checked())?;
+        Ok(Arc::new(array))
+    }};
+}
+
+macro_rules! neg_wrapping {
+    ($t:ty, $a:ident) => {{
+        let array = $a.as_primitive::<$t>().unary::<_, $t>(|x| 
x.neg_wrapping());
+        Ok(Arc::new(array))
+    }};
+}
+
+/// Perform `!array`, returning an error on overflow

Review Comment:
   ```suggestion
   /// Negates each element of  `array`, returning an error on overflow
   ```
   
   I am not really sure what "Performs `!array`" means



##########
arrow-arith/src/numeric.rs:
##########
@@ -74,6 +74,97 @@ pub fn rem(lhs: &dyn Datum, rhs: &dyn Datum) -> 
Result<ArrayRef, ArrowError> {
     arithmetic_op(Op::Rem, lhs, rhs)
 }
 
+macro_rules! neg_checked {
+    ($t:ty, $a:ident) => {{
+        let array = $a
+            .as_primitive::<$t>()
+            .try_unary::<_, $t, _>(|x| x.neg_checked())?;
+        Ok(Arc::new(array))
+    }};
+}
+
+macro_rules! neg_wrapping {
+    ($t:ty, $a:ident) => {{
+        let array = $a.as_primitive::<$t>().unary::<_, $t>(|x| 
x.neg_wrapping());
+        Ok(Arc::new(array))
+    }};
+}
+
+/// Perform `!array`, returning an error on overflow
+///
+/// Note: negation of unsigned arrays is not supported and will return in an 
error,
+/// for wrapping unsigned negation consider using [`neg_wrapping()`]
+pub fn neg(array: &dyn Array) -> Result<ArrayRef, ArrowError> {
+    use DataType::*;
+    use IntervalUnit::*;
+    use TimeUnit::*;
+
+    match array.data_type() {
+        Int8 => neg_checked!(Int8Type, array),
+        Int16 => neg_checked!(Int16Type, array),
+        Int32 => neg_checked!(Int32Type, array),
+        Int64 => neg_checked!(Int64Type, array),
+        Float16 => neg_wrapping!(Float16Type, array),
+        Float32 => neg_wrapping!(Float32Type, array),
+        Float64 => neg_wrapping!(Float64Type, array),
+        Decimal128(p, s) => {
+            let a = array
+                .as_primitive::<Decimal128Type>()
+                .try_unary::<_, Decimal128Type, _>(|x| x.neg_checked())?;
+
+            Ok(Arc::new(a.with_precision_and_scale(*p, *s)?))
+        }
+        Decimal256(p, s) => {
+            let a = array
+                .as_primitive::<Decimal256Type>()
+                .try_unary::<_, Decimal256Type, _>(|x| x.neg_checked())?;
+
+            Ok(Arc::new(a.with_precision_and_scale(*p, *s)?))
+        }
+        Duration(Second) => neg_checked!(DurationSecondType, array),
+        Duration(Millisecond) => neg_checked!(DurationMillisecondType, array),
+        Duration(Microsecond) => neg_checked!(DurationMicrosecondType, array),
+        Duration(Nanosecond) => neg_checked!(DurationNanosecondType, array),
+        Interval(YearMonth) => neg_checked!(IntervalYearMonthType, array),

Review Comment:
   I double checked that 
[YearMonth](https://docs.rs/arrow/latest/arrow/datatypes/enum.IntervalUnit.html#variant.YearMonth)
 intervals are stored as number of whole intervals and thus don't need to be 
treated field by field



##########
arrow-arith/src/numeric.rs:
##########
@@ -74,6 +74,97 @@ pub fn rem(lhs: &dyn Datum, rhs: &dyn Datum) -> 
Result<ArrayRef, ArrowError> {
     arithmetic_op(Op::Rem, lhs, rhs)
 }
 
+macro_rules! neg_checked {
+    ($t:ty, $a:ident) => {{
+        let array = $a
+            .as_primitive::<$t>()
+            .try_unary::<_, $t, _>(|x| x.neg_checked())?;
+        Ok(Arc::new(array))
+    }};
+}
+
+macro_rules! neg_wrapping {
+    ($t:ty, $a:ident) => {{
+        let array = $a.as_primitive::<$t>().unary::<_, $t>(|x| 
x.neg_wrapping());
+        Ok(Arc::new(array))
+    }};
+}
+
+/// Perform `!array`, returning an error on overflow
+///
+/// Note: negation of unsigned arrays is not supported and will return in an 
error,
+/// for wrapping unsigned negation consider using [`neg_wrapping()`]

Review Comment:
   ```suggestion
   /// for wrapping unsigned negation consider using [`neg_wrapping`]
   ```



-- 
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]

Reply via email to