gstvg commented on code in PR #6303:
URL: https://github.com/apache/arrow-rs/pull/6303#discussion_r1739329073
##########
arrow-array/src/array/union_array.rs:
##########
@@ -479,20 +675,91 @@ impl Array for UnionArray {
None
}
+ fn logical_nulls(&self) -> Option<NullBuffer> {
+ let DataType::Union(fields, _) = &self.data_type else {
+ unreachable!()
+ };
+
+ if fields.len() <= 1 {
+ return self
+ .fields
+ .iter()
+ .flatten()
+ .map(Array::logical_nulls)
+ .next()
+ .flatten();
+ }
+
+ let with_nulls = fields
+ .iter()
+ .filter_map(|(type_id, _)| Some((type_id,
self.child(type_id).logical_nulls()?)))
+ .filter(|(_, nulls)| nulls.null_count() > 0)
+ .collect::<Vec<_>>();
+
+ if with_nulls.is_empty() {
+ return None;
+ }
+
+ if with_nulls
+ .iter()
+ .all(|(_, nulls)| nulls.null_count() == nulls.len())
+ {
+ if let Some((_, exactly_sized)) = with_nulls
+ .iter()
+ .find(|(_, nulls)| nulls.len() == self.len())
+ {
+ return Some(exactly_sized.clone());
+ }
+
+ if let Some((_, bigger)) = with_nulls
+ .iter()
+ .find(|(_, nulls)| nulls.len() > self.len())
+ {
+ return Some(bigger.slice(0, self.len()));
+ }
+
+ return Some(NullBuffer::new_null(self.len()));
+ }
+
+ let buffer = match &self.offsets {
+ Some(_) => self.gather_nulls(&with_nulls),
+ None => {
+ // masking is only efficient up to a certain point
+ // when all fields contains nulls, one field mask can be
cheaply calculated
+ // by negating the others, so it's threshold is higher
+ if with_nulls.len() <= 10 && with_nulls.len() == fields.len() {
+ self.mask_sparse_all_nulls(&with_nulls)
+ } else if with_nulls.len() <= 9 {
+ self.mask_sparse_mixed_nulls(&with_nulls)
+ } else {
+ self.gather_nulls(&with_nulls)
+ }
+ }
+ };
+
+ let null_buffer = NullBuffer::from(buffer);
+
+ if null_buffer.null_count() > 0 {
+ Some(null_buffer)
+ } else {
+ None
+ }
+ }
+
/// Union types always return non null as there is no validity buffer.
Review Comment:
I updated docs below, but the others arrays which also should and correctly
implements `logical_nulls` doesn't include those default methods. Should we
delete them too?
[DictionaryArray](https://github.com/apache/arrow-rs/blob/678517018ddfd21b202a94df13b06dfa1ab8a378/arrow-array/src/array/dictionary_array.rs#L731)
[RunArray](https://github.com/apache/arrow-rs/blob/678517018ddfd21b202a94df13b06dfa1ab8a378/arrow-array/src/array/run_array.rs#L342)
[NullArray](https://github.com/apache/arrow-rs/blob/678517018ddfd21b202a94df13b06dfa1ab8a378/arrow-array/src/array/null_array.rs#L115)
--
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]