psvri commented on code in PR #2413:
URL: https://github.com/apache/arrow-rs/pull/2413#discussion_r944139812


##########
arrow/src/array/builder/struct_builder.rs:
##########
@@ -233,6 +230,53 @@ impl StructBuilder {
         let array_data = unsafe { builder.build_unchecked() };
         StructArray::from(array_data)
     }
+
+    /// Constructs and validates contents in the builder to ensure that
+    /// - fields and field_builders are of equal length
+    /// - the number of items in individual field_builders are equal to 
self.len
+    /// - the number of null items in individual field_builders are equal to 
null_count in self.null_buffer_builder.
+    fn validate_and_construct(&mut self) -> (Vec<ArrayData>, Option<Buffer>) {
+        if self.fields.len() != self.field_builders.len() {
+            panic!("Number of fields is not equal to the number of 
field_builders.");
+        }
+        if !self.field_builders.iter().all(|x| x.len() == self.len) {
+            panic!("StructBuilder and field_builders are of unequal lengths.");
+        }
+
+        let mut child_data = Vec::with_capacity(self.field_builders.len());
+        for f in &mut self.field_builders {
+            let arr = f.finish();
+            child_data.push(arr.into_data());
+        }
+
+        let null_bit_buffer = self.null_buffer_builder.finish();
+
+        if !child_data.is_empty() {
+            let self_null_count = count_nulls(null_bit_buffer.as_ref(), 0, 
self.len);
+
+            let mut child_null_count = 0;
+
+            //child_null_count is incremented when all child elements are null
+            for index in 0..child_data[0].len() {
+                let mut is_null = true;
+                for data in &child_data {
+                    if data.is_valid(index) {
+                        is_null = false;
+                        break;
+                    }
+                }
+                if is_null {
+                    child_null_count += 1;
+                }
+            }
+
+            if child_null_count != self_null_count {
+                panic!("StructBuilder and field_builders contain unequal null 
counts.");

Review Comment:
   When we say a struct is null, its child elements should be null. Hence they 
should have same null counts.
   



-- 
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-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to