gstvg commented on code in PR #6303:
URL: https://github.com/apache/arrow-rs/pull/6303#discussion_r1730150821


##########
arrow-array/src/array/union_array.rs:
##########
@@ -479,6 +480,164 @@ impl Array for UnionArray {
         None
     }
 
+    fn logical_nulls(&self) -> Option<NullBuffer> {
+        let DataType::Union(fields, _) = &self.data_type else {
+            unreachable!()
+        };
+
+        if fields.iter().all(|(_, field)| !field.is_nullable()) {
+            return None;
+        }
+
+        if fields.len() <= 1 {
+            return self
+                .fields
+                .iter()
+                .flatten()
+                .map(Array::logical_nulls)
+                .next()
+                .flatten();
+        }
+
+        let children_logical_nulls = self
+            .fields
+            .iter()
+            .map(|child| child.as_ref().and_then(Array::logical_nulls))
+            .collect::<Vec<_>>();
+
+        if children_logical_nulls
+            .iter()
+            .flatten()
+            .all(|child| child.null_count() == 0)
+        {
+            return None;
+        }
+
+        if children_logical_nulls
+            .iter()
+            .flatten()
+            .all(|child| child.null_count() == child.len())
+        {
+            if let Some(exactly_sized) = children_logical_nulls
+                .iter()
+                .flatten()
+                .find(|nulls| nulls.len() == self.len())
+            {
+                return Some(exactly_sized.clone());
+            }
+
+            if let Some(bigger) = children_logical_nulls
+                .iter()
+                .flatten()
+                .find(|nulls| nulls.len() > self.len())
+            {
+                return Some(bigger.slice(0, self.len()));
+            }
+
+            return Some(NullBuffer::new_null(self.len()));
+        }
+
+        #[derive(Copy, Clone)]
+        #[repr(usize)]
+        enum Mask {
+            Zero = 0,
+            // false positive, see 
https://github.com/rust-lang/rust-clippy/issues/8043
+            #[allow(clippy::enum_clike_unportable_variant)]
+            Max = usize::MAX,
+        }
+
+        let one_valid = NullBuffer::new_valid(1);
+
+        let children_logical_nulls: [(&NullBuffer, Mask); u8::MAX as usize + 
1] =
+            std::array::from_fn(|i| {

Review Comment:
   
[`std::array::from_fn`](https://doc.rust-lang.org/stable/std/array/fn.from_fn.html)
 requires 1.63



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