This is an automated email from the ASF dual-hosted git repository.

xudong963 pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git


The following commit(s) were added to refs/heads/main by this push:
     new 72f4eab019 Fix ScalarStructBuilder::build() for an empty struct 
(#16205)
72f4eab019 is described below

commit 72f4eab0192bfaefd48c2c5ac12c76642c86da37
Author: Arttu <blizz...@users.noreply.github.com>
AuthorDate: Thu May 29 15:47:19 2025 +0200

    Fix ScalarStructBuilder::build() for an empty struct (#16205)
    
    * Fix ScalarStructBuilder::build() when the struct is empty
    
    This had been broken by https://github.com/apache/arrow-rs/pull/7247 in 
Arrow 55.1.0
    
    * fix test for error
---
 datafusion/common/src/scalar/mod.rs            |  2 +-
 datafusion/common/src/scalar/struct_builder.rs | 26 ++++++++++++++------------
 2 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/datafusion/common/src/scalar/mod.rs 
b/datafusion/common/src/scalar/mod.rs
index 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.rs
index 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}"), "{}");
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@datafusion.apache.org
For additional commands, e-mail: commits-h...@datafusion.apache.org

Reply via email to