This is an automated email from the ASF dual-hosted git repository.

tustvold pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/master by this push:
     new 98a77feb4 Fix UnionArray is_null (#1632)
98a77feb4 is described below

commit 98a77feb4fc9536bddd5ecdad85c4c85a3e85183
Author: Liang-Chi Hsieh <[email protected]>
AuthorDate: Mon May 2 14:03:42 2022 -0700

    Fix UnionArray is_null (#1632)
---
 arrow/src/array/array_union.rs | 50 ++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 50 insertions(+)

diff --git a/arrow/src/array/array_union.rs b/arrow/src/array/array_union.rs
index 63cf5c2a0..5ebf3d2d3 100644
--- a/arrow/src/array/array_union.rs
+++ b/arrow/src/array/array_union.rs
@@ -304,6 +304,24 @@ impl Array for UnionArray {
     fn data(&self) -> &ArrayData {
         &self.data
     }
+
+    /// Union types always return non null as there is no validity buffer.
+    /// To check validity correctly you must check the underlying vector.
+    fn is_null(&self, _index: usize) -> bool {
+        false
+    }
+
+    /// Union types always return non null as there is no validity buffer.
+    /// To check validity correctly you must check the underlying vector.
+    fn is_valid(&self, _index: usize) -> bool {
+        true
+    }
+
+    /// Union types always return 0 null count as there is no validity buffer.
+    /// To get null count correctly you must check the underlying vector.
+    fn null_count(&self) -> usize {
+        0
+    }
 }
 
 impl fmt::Debug for UnionArray {
@@ -877,6 +895,38 @@ mod tests {
         }
     }
 
+    fn test_union_validity(union_array: &UnionArray) {
+        assert_eq!(union_array.null_count(), 0);
+
+        for i in 0..union_array.len() {
+            assert!(!union_array.is_null(i));
+            assert!(union_array.is_valid(i));
+        }
+    }
+
+    #[test]
+    fn test_union_array_validaty() {
+        let mut builder = UnionBuilder::new_sparse(5);
+        builder.append::<Int32Type>("a", 1).unwrap();
+        builder.append_null::<Int32Type>("a").unwrap();
+        builder.append::<Float64Type>("c", 3.0).unwrap();
+        builder.append_null::<Float64Type>("c").unwrap();
+        builder.append::<Int32Type>("a", 4).unwrap();
+        let union = builder.build().unwrap();
+
+        test_union_validity(&union);
+
+        let mut builder = UnionBuilder::new_dense(5);
+        builder.append::<Int32Type>("a", 1).unwrap();
+        builder.append_null::<Int32Type>("a").unwrap();
+        builder.append::<Float64Type>("c", 3.0).unwrap();
+        builder.append_null::<Float64Type>("c").unwrap();
+        builder.append::<Int32Type>("a", 4).unwrap();
+        let union = builder.build().unwrap();
+
+        test_union_validity(&union);
+    }
+
     #[test]
     fn test_type_check() {
         let mut builder = UnionBuilder::new_sparse(2);

Reply via email to