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

alamb pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/arrow-rs.git


The following commit(s) were added to refs/heads/main by this push:
     new 7d9bb379a6 Improve error message for unsupported cast between struct 
and other types (#6919)
7d9bb379a6 is described below

commit 7d9bb379a6676c317907452bc4879bb65809006f
Author: Takahiro Ebato <[email protected]>
AuthorDate: Mon Dec 30 19:58:56 2024 +0900

    Improve error message for unsupported cast between struct and other types 
(#6919)
---
 arrow-cast/src/cast/mod.rs | 38 ++++++++++++++++++++++++++++++++------
 1 file changed, 32 insertions(+), 6 deletions(-)

diff --git a/arrow-cast/src/cast/mod.rs b/arrow-cast/src/cast/mod.rs
index ba470635c6..7aff32b0be 100644
--- a/arrow-cast/src/cast/mod.rs
+++ b/arrow-cast/src/cast/mod.rs
@@ -1189,12 +1189,12 @@ pub fn cast_with_options(
             let array = StructArray::try_new(to_fields.clone(), fields, 
array.nulls().cloned())?;
             Ok(Arc::new(array) as ArrayRef)
         }
-        (Struct(_), _) => Err(ArrowError::CastError(
-            "Cannot cast from struct to other types except struct".to_string(),
-        )),
-        (_, Struct(_)) => Err(ArrowError::CastError(
-            "Cannot cast to struct from other types except struct".to_string(),
-        )),
+        (Struct(_), _) => Err(ArrowError::CastError(format!(
+            "Casting from {from_type:?} to {to_type:?} not supported"
+        ))),
+        (_, Struct(_)) => Err(ArrowError::CastError(format!(
+            "Casting from {from_type:?} to {to_type:?} not supported"
+        ))),
         (_, Boolean) => match from_type {
             UInt8 => cast_numeric_to_bool::<UInt8Type>(array),
             UInt16 => cast_numeric_to_bool::<UInt16Type>(array),
@@ -9941,6 +9941,32 @@ mod tests {
         );
     }
 
+    #[test]
+    fn test_cast_struct_to_non_struct() {
+        let boolean = Arc::new(BooleanArray::from(vec![true, false]));
+        let struct_array = StructArray::from(vec![(
+            Arc::new(Field::new("a", DataType::Boolean, false)),
+            boolean.clone() as ArrayRef,
+        )]);
+        let to_type = DataType::Utf8;
+        let result = cast(&struct_array, &to_type);
+        assert_eq!(
+            r#"Cast error: Casting from Struct([Field { name: "a", data_type: 
Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, metadata: {} }]) 
to Utf8 not supported"#,
+            result.unwrap_err().to_string()
+        );
+    }
+
+    #[test]
+    fn test_cast_non_struct_to_struct() {
+        let array = StringArray::from(vec!["a", "b"]);
+        let to_type = DataType::Struct(vec![Field::new("a", DataType::Boolean, 
false)].into());
+        let result = cast(&array, &to_type);
+        assert_eq!(
+            r#"Cast error: Casting from Utf8 to Struct([Field { name: "a", 
data_type: Boolean, nullable: false, dict_id: 0, dict_is_ordered: false, 
metadata: {} }]) not supported"#,
+            result.unwrap_err().to_string()
+        );
+    }
+
     #[test]
     fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale() {
         let array = vec![Some(123456789)];

Reply via email to