@@ -43,13 +43,15 @@ mod list;
4343mod map;
4444mod run_array;
4545mod string;
46+ mod union;
4647
4748use crate :: cast:: decimal:: * ;
4849use crate :: cast:: dictionary:: * ;
4950use crate :: cast:: list:: * ;
5051use crate :: cast:: map:: * ;
5152use crate :: cast:: run_array:: * ;
5253use crate :: cast:: string:: * ;
54+ pub use crate :: cast:: union:: * ;
5355
5456use arrow_buffer:: IntervalMonthDayNano ;
5557use arrow_data:: ByteView ;
@@ -230,7 +232,8 @@ pub fn can_cast_types(from_type: &DataType, to_type: &DataType) -> bool {
230232 }
231233 ( Struct ( _) , _) => false ,
232234 ( _, Struct ( _) ) => false ,
233-
235+ ( Union ( fields, _) , _) => union:: resolve_variant ( fields, to_type) . is_some ( ) ,
236+ ( _, Union ( _, _) ) => false ,
234237 ( _, Boolean ) => from_type. is_integer ( ) || from_type. is_floating ( ) || from_type. is_string ( ) ,
235238 ( Boolean , _) => to_type. is_integer ( ) || to_type. is_floating ( ) || to_type. is_string ( ) ,
236239
@@ -1180,6 +1183,14 @@ pub fn cast_with_options(
11801183 ( _, Struct ( _) ) => Err ( ArrowError :: CastError ( format ! (
11811184 "Casting from {from_type} to {to_type} not supported"
11821185 ) ) ) ,
1186+ ( Union ( _, _) , _) => union_extract_by_type (
1187+ array. as_any ( ) . downcast_ref :: < UnionArray > ( ) . unwrap ( ) ,
1188+ to_type,
1189+ cast_options,
1190+ ) ,
1191+ ( _, Union ( _, _) ) => Err ( ArrowError :: CastError ( format ! (
1192+ "Casting from {from_type} to {to_type} not supported"
1193+ ) ) ) ,
11831194 ( _, Boolean ) => match from_type {
11841195 UInt8 => cast_numeric_to_bool :: < UInt8Type > ( array) ,
11851196 UInt16 => cast_numeric_to_bool :: < UInt16Type > ( array) ,
0 commit comments