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/05/18 17:10:09 UTC

[GitHub] [arrow-rs] tustvold commented on a diff in pull request #4210: feat: Support bitwise and boolean aggregate functions

tustvold commented on code in PR #4210:
URL: https://github.com/apache/arrow-rs/pull/4210#discussion_r1198071904


##########
arrow-arith/src/aggregate.rs:
##########
@@ -326,6 +327,115 @@ where
     }
 }
 
+macro_rules! bit_operation {
+    ($NAME:ident, $OP:ident, $NATIVE:ident, $DEFAULT:expr, $DOC:expr) => {
+        #[doc = $DOC]
+        ///
+        /// Returns `None` if the array is empty or only contains null values.
+        pub fn $NAME<T>(array: &PrimitiveArray<T>) -> Option<T::Native>
+        where
+            T: ArrowNumericType,
+            T::Native: $NATIVE<Output = T::Native> + ArrowNativeTypeOp,
+        {
+            let default;
+            if $DEFAULT == -1 {
+                default = T::Native::ONE.neg_wrapping();
+            } else {
+                default = T::default_value();
+            }
+
+            let null_count = array.null_count();
+
+            if null_count == array.len() {
+                return None;
+            }
+
+            let data: &[T::Native] = array.values();
+
+            match array.nulls() {
+                None => {
+                    let result = data
+                        .iter()
+                        .fold(default, |accumulator, value| accumulator.$OP(*value));
+
+                    Some(result)
+                }
+                Some(nulls) => {
+                    let mut result = default;
+                    let data_chunks = data.chunks_exact(64);
+                    let remainder = data_chunks.remainder();
+
+                    let bit_chunks = nulls.inner().bit_chunks();

Review Comment:
   FWIW [try_for_each_valid_idx](https://docs.rs/arrow-buffer/latest/arrow_buffer/buffer/struct.NullBuffer.html#method.try_for_each_valid_idx) may be faster for sparse arrays as it uses the CLZ to quickly skip over unset bits.
   
   It would also be less code.



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