HaoYang670 commented on code in PR #1781:
URL: https://github.com/apache/arrow-rs/pull/1781#discussion_r889632650


##########
arrow/src/compute/util.rs:
##########
@@ -206,25 +212,49 @@ pub(super) mod tests {
             make_data_with_null_bit_buffer(8, 0, 
Some(Buffer::from([0b01001010])));
         let inverse_bitmap =
             make_data_with_null_bit_buffer(8, 0, 
Some(Buffer::from([0b10110101])));
+        let some_other_bitmap =
+            make_data_with_null_bit_buffer(8, 0, 
Some(Buffer::from([0b11010111])));
+        assert!(combine_option_bitmap(&[], 8).is_err());

Review Comment:
   nit: test more precisely:
   ```suggestion
           assert_eq!(
               combine_option_bitmap(&[], 8).unwrap_err().to_string(),
               "Compute error: Arrays must not be empty",
           );
   ```



##########
arrow/src/compute/util.rs:
##########
@@ -24,38 +24,44 @@ use crate::error::{ArrowError, Result};
 use num::{One, ToPrimitive, Zero};
 use std::ops::Add;
 
-/// Combines the null bitmaps of two arrays using a bitwise `and` operation.
+/// Combines the null bitmaps of multiple arrays using a bitwise `and` 
operation.
 ///
 /// This function is useful when implementing operations on higher level 
arrays.
 #[allow(clippy::unnecessary_wraps)]
 pub(super) fn combine_option_bitmap(
-    left_data: &ArrayData,
-    right_data: &ArrayData,
+    arrays: &[&ArrayData],
     len_in_bits: usize,
 ) -> Result<Option<Buffer>> {
-    let left_offset_in_bits = left_data.offset();
-    let right_offset_in_bits = right_data.offset();
-
-    let left = left_data.null_buffer();
-    let right = right_data.null_buffer();
-
-    match left {
-        None => match right {
-            None => Ok(None),
-            Some(r) => Ok(Some(r.bit_slice(right_offset_in_bits, 
len_in_bits))),
-        },
-        Some(l) => match right {
-            None => Ok(Some(l.bit_slice(left_offset_in_bits, len_in_bits))),
-
-            Some(r) => Ok(Some(buffer_bin_and(
-                l,
-                left_offset_in_bits,
-                r,
-                right_offset_in_bits,
-                len_in_bits,
-            ))),
-        },
+    if arrays.is_empty() {
+        return Err(ArrowError::ComputeError(
+            "Arrays must not be empty".to_string(),
+        ));
     }
+
+    let buffers_and_offsets = arrays
+        .iter()
+        .map(|array| (array.null_buffer().cloned(), array.offset()));
+
+    let (output_buffer, output_offset) = buffers_and_offsets
+        .reduce(|acc, buffer_and_offset| match (acc, buffer_and_offset) {
+            ((None, _), (None, _)) => (None, 0),
+            ((Some(buffer), offset), (None, _)) | ((None, _), (Some(buffer), 
offset)) => {
+                (Some(buffer), offset)
+            }
+            ((Some(buffer_left), offset_left), (Some(buffer_right), 
offset_right)) => (
+                Some(buffer_bin_and(
+                    &buffer_left,
+                    offset_left,
+                    &buffer_right,
+                    offset_right,
+                    len_in_bits,
+                )),
+                0,
+            ),
+        })
+        .unwrap();

Review Comment:
   Nit: We can check if `arrays` is empty here
   ```suggestion
           .map_or(
               Err(ArrowError::ComputeError(
                   "Arrays must not be empty".to_string(),
               )),
               |(buffer, offset)| Ok(buffer.map(|buffer| buffer.slice(offset))),
           )
   ```



##########
arrow/src/compute/util.rs:
##########
@@ -24,38 +24,44 @@ use crate::error::{ArrowError, Result};
 use num::{One, ToPrimitive, Zero};
 use std::ops::Add;
 
-/// Combines the null bitmaps of two arrays using a bitwise `and` operation.
+/// Combines the null bitmaps of multiple arrays using a bitwise `and` 
operation.
 ///
 /// This function is useful when implementing operations on higher level 
arrays.
 #[allow(clippy::unnecessary_wraps)]
 pub(super) fn combine_option_bitmap(
-    left_data: &ArrayData,
-    right_data: &ArrayData,
+    arrays: &[&ArrayData],
     len_in_bits: usize,
 ) -> Result<Option<Buffer>> {
-    let left_offset_in_bits = left_data.offset();
-    let right_offset_in_bits = right_data.offset();
-
-    let left = left_data.null_buffer();
-    let right = right_data.null_buffer();
-
-    match left {
-        None => match right {
-            None => Ok(None),
-            Some(r) => Ok(Some(r.bit_slice(right_offset_in_bits, 
len_in_bits))),
-        },
-        Some(l) => match right {
-            None => Ok(Some(l.bit_slice(left_offset_in_bits, len_in_bits))),
-
-            Some(r) => Ok(Some(buffer_bin_and(
-                l,
-                left_offset_in_bits,
-                r,
-                right_offset_in_bits,
-                len_in_bits,
-            ))),
-        },
+    if arrays.is_empty() {
+        return Err(ArrowError::ComputeError(
+            "Arrays must not be empty".to_string(),
+        ));
     }
+
+    let buffers_and_offsets = arrays
+        .iter()
+        .map(|array| (array.null_buffer().cloned(), array.offset()));

Review Comment:
   Nit: we don't need the variable `buffers_and_offsets` here:
   ```rust
   arrays
       .iter(...)
       .map(...)
       .reduce(...)
       .map_or(...)



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