Rich-T-kid commented on code in PR #10636:
URL: https://github.com/apache/arrow-rs/pull/10636#discussion_r3824108854


##########
arrow-array/src/builder/struct_array_builder.rs:
##########
@@ -0,0 +1,127 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::{ArrayRef, StructArray};
+use arrow_buffer::NullBuffer;
+use arrow_schema::{Field, FieldRef, Fields};
+use std::sync::Arc;
+
+/// Builds a [`StructArray`] from completed child arrays.
+///
+/// Unlike [`StructBuilder`](super::StructBuilder), which incrementally builds
+/// child arrays, this builder assembles arrays that have already been built.
+///
+/// # Example
+///
+/// ```
+/// use std::sync::Arc;
+/// use arrow_array::builder::StructArrayBuilder;
+/// use arrow_array::{Array, ArrayRef, Int32Array, StringArray};
+///
+/// let names = Arc::new(StringArray::from(vec!["one", "two"])) as ArrayRef;
+/// let values = Arc::new(Int32Array::from(vec![1, 2])) as ArrayRef;
+/// let array = StructArrayBuilder::new()
+///     .with_field("name", names, false)
+///     .with_field("value", values, false)
+///     .build();
+///
+/// assert_eq!(array.len(), 2);
+/// ```
+#[derive(Debug, Default, Clone)]
+pub struct StructArrayBuilder {
+    fields: Vec<FieldRef>,
+    arrays: Vec<ArrayRef>,
+    nulls: Option<NullBuffer>,
+}
+
+impl StructArrayBuilder {
+    /// Creates a new empty [`StructArrayBuilder`].
+    pub fn new() -> Self {
+        Self::default()
+    }
+
+    /// Adds an array with a field constructed from its data type.
+    pub fn with_field(
+        mut self,
+        field_name: impl Into<String>,
+        array: ArrayRef,
+        nullable: bool,
+    ) -> Self {
+        self.fields.push(Arc::new(Field::new(
+            field_name,
+            array.data_type().clone(),
+            nullable,
+        )));
+        self.arrays.push(array);
+        self
+    }
+
+    /// Adds an array using a caller-supplied field.
+    ///
+    /// This preserves field metadata that would be lost if the field were
+    /// constructed from the array's data type alone.
+    pub fn with_field_ref(mut self, field: FieldRef, array: ArrayRef) -> Self {
+        self.fields.push(field);
+        self.arrays.push(array);
+        self
+    }
+
+    /// Sets the top-level null buffer for the struct array.
+    pub fn with_nulls(mut self, nulls: NullBuffer) -> Self {
+        self.nulls = Some(nulls);
+        self
+    }
+
+    /// Builds the [`StructArray`].
+    ///
+    /// # Panics
+    ///
+    /// Panics if the fields, child arrays, or null buffer have incompatible
+    /// data types or lengths.
+    pub fn build(self) -> StructArray {
+        StructArray::new(Fields::from(self.fields), self.arrays, self.nulls)
+    }
+}
+
+#[cfg(test)]
+mod tests {

Review Comment:
   mabey we could add an extra test or two. asserting an error on invalid input 
could be an easy one



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