comphead commented on code in PR #9229:
URL: https://github.com/apache/arrow-datafusion/pull/9229#discussion_r1490088922


##########
datafusion/common/src/scalar/struct_builder.rs:
##########
@@ -0,0 +1,152 @@
+// 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.
+
+//! [`ScalarStructBuilder`] for building [`ScalarValue::Struct`]
+
+use crate::error::_internal_err;
+use crate::{DataFusionError, Result, ScalarValue};
+use arrow::array::{ArrayRef, StructArray};
+use arrow::datatypes::{DataType, FieldRef, Fields};
+use arrow_schema::Field;
+use std::sync::Arc;
+
+/// Builder for [`ScalarValue::Struct`].
+///
+/// See examples on [`ScalarValue`]
+#[derive(Debug, Default)]
+pub struct ScalarStructBuilder {
+    fields: Vec<FieldRef>,
+    arrays: Vec<ArrayRef>,
+}
+
+impl ScalarStructBuilder {
+    /// Create a new `ScalarStructBuilder`
+    pub fn new() -> Self {
+        Self::default()
+    }
+
+    /// Return a new [`ScalarValue::Struct`] with the specified fields and a
+    /// single null value
+    pub fn new_null(fields: impl IntoFields) -> ScalarValue {
+        DataType::Struct(fields.into()).try_into().unwrap()
+    }
+
+    /// Add the specified field and [`ArrayRef`] to the struct.
+    ///
+    /// Note the array should have a single row.
+    pub fn with_array(mut self, field: impl IntoFieldRef, value: ArrayRef) -> 
Self {
+        self.fields.push(field.into_field_ref());
+        self.arrays.push(value);
+        self
+    }
+
+    /// Add the specified field and `ScalarValue` to the struct.
+    pub fn with_scalar(self, field: impl IntoFieldRef, value: ScalarValue) -> 
Self {
+        // valid scalar value should not fail
+        let array = value.to_array().unwrap();
+        self.with_array(field, array)
+    }
+
+    /// Add a field with the specified name and value to the struct.
+    /// the field is created with the specified data type and as non nullable
+    pub fn with_name_and_scalar(self, name: &str, value: ScalarValue) -> Self {
+        let field = Field::new(name, value.data_type(), false);
+        self.with_scalar(field, value)
+    }
+
+    /// Return a [`ScalarValue::Struct`] with the fields and values added so 
far
+    ///
+    /// # Errors
+    ///
+    /// If the [`StructArray`] cannot be created (for example if there is a
+    /// mismatch between field types and arrays) or the arrays do not have
+    /// exactly one element.
+    pub fn build(self) -> Result<ScalarValue> {
+        let Self { fields, arrays } = self;
+
+        for array in &arrays {
+            if array.len() != 1 {
+                return _internal_err!(
+                    "Error building SclarValue::Struct. \

Review Comment:
   ```suggestion
                       "Error building ScalarValue::Struct. \
   ```



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