Apache DataFusion SQL Query Engine
Fix ScalarStructBuilder::build() for an empty struct (#16205)
Arttu72f4eab0192b
diff --git a/datafusion/common/src/scalar/mod.rs b/datafusion/common/src/scalar/mod.rsindex a2a131309f..3d4aa78b6d 100644--- a/datafusion/common/src/scalar/mod.rs+++ b/datafusion/common/src/scalar/mod.rs@@ -4179,7 +4179,7 @@ mod tests {#[test]#[should_panic(- expected = "Error building ScalarValue::Struct. Expected array with exactly one element, found array with 4 elements"+ expected = "InvalidArgumentError(\"Incorrect array length for StructArray field \\\"bool\\\", expected 1 got 4\")")]fn test_scalar_value_from_for_struct_should_panic() {let _ = ScalarStructBuilder::new()diff --git a/datafusion/common/src/scalar/struct_builder.rs b/datafusion/common/src/scalar/struct_builder.rsindex 5ed4640184..fd19dccf89 100644--- a/datafusion/common/src/scalar/struct_builder.rs+++ b/datafusion/common/src/scalar/struct_builder.rs@@ -17,7 +17,6 @@//! [`ScalarStructBuilder`] for building [`ScalarValue::Struct`]-use crate::error::_internal_err;use crate::{Result, ScalarValue};use arrow::array::{ArrayRef, StructArray};use arrow::datatypes::{DataType, Field, FieldRef, Fields};@@ -109,17 +108,8 @@ impl ScalarStructBuilder {pub fn build(self) -> Result<ScalarValue> {let Self { fields, arrays } = self;- for array in &arrays {- if array.len() != 1 {- return _internal_err!(- "Error building ScalarValue::Struct. \- Expected array with exactly one element, found array with {} elements",- array.len()- );- }- }-- let struct_array = StructArray::try_new(Fields::from(fields), arrays, None)?;+ let struct_array =+ StructArray::try_new_with_length(Fields::from(fields), arrays, None, 1)?;Ok(ScalarValue::Struct(Arc::new(struct_array)))}}@@ -181,3 +171,15 @@ impl IntoFields for Vec<Field> {Fields::from(self)}}++#[cfg(test)]+mod tests {+ use super::*;++ // Other cases are tested by doc tests+ #[test]+ fn test_empty_struct() {+ let sv = ScalarStructBuilder::new().build().unwrap();+ assert_eq!(format!("{sv}"), "{}");+ }+}