Skip to content

Commit 72f4eab

Browse files
authored
Fix ScalarStructBuilder::build() for an empty struct (#16205)
* Fix ScalarStructBuilder::build() when the struct is empty This had been broken by apache/arrow-rs#7247 in Arrow 55.1.0 * fix test for error
1 parent 5944e8b commit 72f4eab

2 files changed

Lines changed: 15 additions & 13 deletions

File tree

datafusion/common/src/scalar/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4179,7 +4179,7 @@ mod tests {
41794179

41804180
#[test]
41814181
#[should_panic(
4182-
expected = "Error building ScalarValue::Struct. Expected array with exactly one element, found array with 4 elements"
4182+
expected = "InvalidArgumentError(\"Incorrect array length for StructArray field \\\"bool\\\", expected 1 got 4\")"
41834183
)]
41844184
fn test_scalar_value_from_for_struct_should_panic() {
41854185
let _ = ScalarStructBuilder::new()

datafusion/common/src/scalar/struct_builder.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717

1818
//! [`ScalarStructBuilder`] for building [`ScalarValue::Struct`]
1919
20-
use crate::error::_internal_err;
2120
use crate::{Result, ScalarValue};
2221
use arrow::array::{ArrayRef, StructArray};
2322
use arrow::datatypes::{DataType, Field, FieldRef, Fields};
@@ -109,17 +108,8 @@ impl ScalarStructBuilder {
109108
pub fn build(self) -> Result<ScalarValue> {
110109
let Self { fields, arrays } = self;
111110

112-
for array in &arrays {
113-
if array.len() != 1 {
114-
return _internal_err!(
115-
"Error building ScalarValue::Struct. \
116-
Expected array with exactly one element, found array with {} elements",
117-
array.len()
118-
);
119-
}
120-
}
121-
122-
let struct_array = StructArray::try_new(Fields::from(fields), arrays, None)?;
111+
let struct_array =
112+
StructArray::try_new_with_length(Fields::from(fields), arrays, None, 1)?;
123113
Ok(ScalarValue::Struct(Arc::new(struct_array)))
124114
}
125115
}
@@ -181,3 +171,15 @@ impl IntoFields for Vec<Field> {
181171
Fields::from(self)
182172
}
183173
}
174+
175+
#[cfg(test)]
176+
mod tests {
177+
use super::*;
178+
179+
// Other cases are tested by doc tests
180+
#[test]
181+
fn test_empty_struct() {
182+
let sv = ScalarStructBuilder::new().build().unwrap();
183+
assert_eq!(format!("{sv}"), "{}");
184+
}
185+
}

0 commit comments

Comments
 (0)