You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "tustvold (via GitHub)" <gi...@apache.org> on 2023/07/09 18:34:19 UTC

[GitHub] [arrow-rs] tustvold opened a new pull request, #4494: Add negate kernels (#4488)

tustvold opened a new pull request, #4494:
URL: https://github.com/apache/arrow-rs/pull/4494

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #4488
   
   # Rationale for this change
    
   <!--
   Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
   Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.
   -->
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   # Are there any user-facing changes?
   
   
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!---
   If there are any breaking changes to public APIs, please add the `breaking change` label.
   -->
   


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] alamb commented on a diff in pull request #4494: Add negate kernels (#4488)

Posted by "alamb (via GitHub)" <gi...@apache.org>.
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on pull request #4494: Add negate kernels (#4488)

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on PR #4494:
URL: https://github.com/apache/arrow-rs/pull/4494#issuecomment-1627805985

   Will add interval tests shortly, realise I forgot


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] alamb commented on a diff in pull request #4494: Add negate kernels (#4488)

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #4494:
URL: https://github.com/apache/arrow-rs/pull/4494#discussion_r1258313081


##########
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:
   sorry it just looked strange to me



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold merged pull request #4494: Add negate kernels (#4488)

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold merged PR #4494:
URL: https://github.com/apache/arrow-rs/pull/4494


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] alamb commented on pull request #4494: Add negate kernels (#4488)

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on PR #4494:
URL: https://github.com/apache/arrow-rs/pull/4494#issuecomment-1628624836

   > Will add interval tests shortly, realise I forgot
   
   Should I wait to review this PR until you wrote those tests? Or did you add them to https://github.com/apache/arrow-rs/pull/4493 ?


-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-rs] tustvold commented on a diff in pull request #4494: Add negate kernels (#4488)

Posted by "tustvold (via GitHub)" <gi...@apache.org>.
tustvold commented on code in PR #4494:
URL: https://github.com/apache/arrow-rs/pull/4494#discussion_r1258286818


##########
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:
   This is necessary to avoid ambiguity, will change to a link



-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org