datafusion_common/scalar/
struct_builder.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! [`ScalarStructBuilder`] for building [`ScalarValue::Struct`]
19
20use crate::{Result, ScalarValue};
21use arrow::array::{ArrayRef, StructArray};
22use arrow::datatypes::{DataType, Field, FieldRef, Fields};
23use std::sync::Arc;
24
25/// Builder for [`ScalarValue::Struct`].
26///
27/// See examples on [`ScalarValue`]
28#[derive(Debug, Default)]
29pub struct ScalarStructBuilder {
30    fields: Vec<FieldRef>,
31    arrays: Vec<ArrayRef>,
32}
33
34impl ScalarStructBuilder {
35    /// Create a new `ScalarStructBuilder`
36    pub fn new() -> Self {
37        Self::default()
38    }
39
40    /// Return a new [`ScalarValue::Struct`] with a single `null` value.
41    ///
42    /// Note this is different from a struct where each of the specified fields
43    /// are null (e.g. `{a: NULL}`)
44    ///
45    /// # Example
46    ///
47    /// ```rust
48    /// # use arrow::datatypes::{DataType, Field};
49    /// # use datafusion_common::scalar::ScalarStructBuilder;
50    /// let fields = vec![Field::new("a", DataType::Int32, false)];
51    /// let sv = ScalarStructBuilder::new_null(fields);
52    /// // Note this is `NULL`, not `{a: NULL}`
53    /// assert_eq!(format!("{sv}"), "NULL");
54    /// ```
55    ///
56    /// To create a struct where the *fields* are null, use `Self::new()` and
57    /// pass null values for each field:
58    ///
59    /// ```rust
60    /// # use arrow::datatypes::{DataType, Field};
61    /// # use datafusion_common::scalar::{ScalarStructBuilder, ScalarValue};
62    /// // make a nullable field
63    /// let field = Field::new("a", DataType::Int32, true);
64    /// // add a null value for the "a" field
65    /// let sv = ScalarStructBuilder::new()
66    ///     .with_scalar(field, ScalarValue::Int32(None))
67    ///     .build()
68    ///     .unwrap();
69    /// // value is not null, but field is
70    /// assert_eq!(format!("{sv}"), "{a:}");
71    /// ```
72    pub fn new_null(fields: impl IntoFields) -> ScalarValue {
73        DataType::Struct(fields.into()).try_into().unwrap()
74    }
75
76    /// Add the specified field and [`ArrayRef`] to the struct.
77    ///
78    /// Note the array should have a single row.
79    pub fn with_array(mut self, field: impl IntoFieldRef, value: ArrayRef) -> Self {
80        self.fields.push(field.into_field_ref());
81        self.arrays.push(value);
82        self
83    }
84
85    /// Add the specified field and `ScalarValue` to the struct.
86    pub fn with_scalar(self, field: impl IntoFieldRef, value: ScalarValue) -> Self {
87        // valid scalar value should not fail
88        let array = value.to_array().unwrap();
89        self.with_array(field, array)
90    }
91
92    /// Add a field with the specified name and value to the struct.
93    /// the field is created with the specified data type and as non nullable
94    pub fn with_name_and_scalar(self, name: &str, value: ScalarValue) -> Self {
95        let field = Field::new(name, value.data_type(), false);
96        self.with_scalar(field, value)
97    }
98
99    /// Return a [`ScalarValue::Struct`] with the fields and values added so far
100    ///
101    /// # Errors
102    ///
103    /// If the [`StructArray`] cannot be created (for example if there is a
104    /// mismatch between field types and arrays) or the arrays do not have
105    /// exactly one element.
106    pub fn build(self) -> Result<ScalarValue> {
107        let Self { fields, arrays } = self;
108
109        let struct_array =
110            StructArray::try_new_with_length(Fields::from(fields), arrays, None, 1)?;
111        Ok(ScalarValue::Struct(Arc::new(struct_array)))
112    }
113}
114
115/// Trait for converting a type into a [`FieldRef`]
116///
117/// Used to avoid having to call `clone()` on a `FieldRef` when adding a field to
118/// a `ScalarStructBuilder`.
119///
120/// TODO potentially upstream this to arrow-rs so that we can
121/// use impl `Into<FieldRef>` instead
122pub trait IntoFieldRef {
123    fn into_field_ref(self) -> FieldRef;
124}
125
126impl IntoFieldRef for FieldRef {
127    fn into_field_ref(self) -> FieldRef {
128        self
129    }
130}
131
132impl IntoFieldRef for &FieldRef {
133    fn into_field_ref(self) -> FieldRef {
134        Arc::clone(self)
135    }
136}
137
138impl IntoFieldRef for Field {
139    fn into_field_ref(self) -> FieldRef {
140        FieldRef::new(self)
141    }
142}
143
144/// Trait for converting a type into a [`Fields`]
145///
146/// This avoids to avoid having to call clone() on an Arc'd `Fields` when adding
147/// a field to a `ScalarStructBuilder`
148///
149/// TODO potentially upstream this to arrow-rs so that we can
150/// use impl `Into<Fields>` instead
151pub trait IntoFields {
152    fn into(self) -> Fields;
153}
154
155impl IntoFields for Fields {
156    fn into(self) -> Fields {
157        self
158    }
159}
160
161impl IntoFields for &Fields {
162    fn into(self) -> Fields {
163        self.clone()
164    }
165}
166
167impl IntoFields for Vec<Field> {
168    fn into(self) -> Fields {
169        Fields::from(self)
170    }
171}
172
173#[cfg(test)]
174mod tests {
175    use super::*;
176
177    // Other cases are tested by doc tests
178    #[test]
179    fn test_empty_struct() {
180        let sv = ScalarStructBuilder::new().build().unwrap();
181        assert_eq!(format!("{sv}"), "{}");
182    }
183}