@@ -1189,12 +1189,12 @@ pub fn cast_with_options(
1189
1189
let array = StructArray :: try_new ( to_fields. clone ( ) , fields, array. nulls ( ) . cloned ( ) ) ?;
1190
1190
Ok ( Arc :: new ( array) as ArrayRef )
1191
1191
}
1192
- ( Struct ( _) , _) => Err ( ArrowError :: CastError (
1193
- "Cannot cast from struct to other types except struct" . to_string ( ) ,
1194
- ) ) ,
1195
- ( _, Struct ( _) ) => Err ( ArrowError :: CastError (
1196
- "Cannot cast to struct from other types except struct" . to_string ( ) ,
1197
- ) ) ,
1192
+ ( Struct ( _) , _) => Err ( ArrowError :: CastError ( format ! (
1193
+ "Casting from {from_type:?} to {to_type:?} not supported"
1194
+ ) ) ) ,
1195
+ ( _, Struct ( _) ) => Err ( ArrowError :: CastError ( format ! (
1196
+ "Casting from {from_type:?} to {to_type:?} not supported"
1197
+ ) ) ) ,
1198
1198
( _, Boolean ) => match from_type {
1199
1199
UInt8 => cast_numeric_to_bool :: < UInt8Type > ( array) ,
1200
1200
UInt16 => cast_numeric_to_bool :: < UInt16Type > ( array) ,
@@ -9941,6 +9941,32 @@ mod tests {
9941
9941
) ;
9942
9942
}
9943
9943
9944
+ #[ test]
9945
+ fn test_cast_struct_to_non_struct ( ) {
9946
+ let boolean = Arc :: new ( BooleanArray :: from ( vec ! [ true , false ] ) ) ;
9947
+ let struct_array = StructArray :: from ( vec ! [ (
9948
+ Arc :: new( Field :: new( "a" , DataType :: Boolean , false ) ) ,
9949
+ boolean. clone( ) as ArrayRef ,
9950
+ ) ] ) ;
9951
+ let to_type = DataType :: Utf8 ;
9952
+ let result = cast ( & struct_array, & to_type) ;
9953
+ assert_eq ! (
9954
+ 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"# ,
9955
+ result. unwrap_err( ) . to_string( )
9956
+ ) ;
9957
+ }
9958
+
9959
+ #[ test]
9960
+ fn test_cast_non_struct_to_struct ( ) {
9961
+ let array = StringArray :: from ( vec ! [ "a" , "b" ] ) ;
9962
+ let to_type = DataType :: Struct ( vec ! [ Field :: new( "a" , DataType :: Boolean , false ) ] . into ( ) ) ;
9963
+ let result = cast ( & array, & to_type) ;
9964
+ assert_eq ! (
9965
+ 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"# ,
9966
+ result. unwrap_err( ) . to_string( )
9967
+ ) ;
9968
+ }
9969
+
9944
9970
#[ test]
9945
9971
fn test_decimal_to_decimal_throw_error_on_precision_overflow_same_scale ( ) {
9946
9972
let array = vec ! [ Some ( 123456789 ) ] ;
0 commit comments