@@ -19,15 +19,9 @@ use crate::utils::array_with_timezone;
1919use crate :: { timezone, BinaryOutputStyle } ;
2020use crate :: { EvalMode , SparkError , SparkResult } ;
2121use arrow:: array:: builder:: StringBuilder ;
22- use arrow:: array:: {
23- BooleanBuilder , Decimal128Builder , DictionaryArray , GenericByteArray , ListArray ,
24- PrimitiveBuilder , StringArray , StructArray ,
25- } ;
22+ use arrow:: array:: { BooleanBuilder , Decimal128Builder , DictionaryArray , GenericByteArray , ListArray , PrimitiveBuilder , StringArray , StructArray , TimestampMicrosecondArray , TimestampMillisecondArray } ;
2623use arrow:: compute:: can_cast_types;
27- use arrow:: datatypes:: {
28- i256, ArrowDictionaryKeyType , ArrowNativeType , DataType , Decimal256Type , GenericBinaryType ,
29- Schema ,
30- } ;
24+ use arrow:: datatypes:: { i256, ArrowDictionaryKeyType , ArrowNativeType , DataType , Decimal256Type , GenericBinaryType , Schema , TimeUnit } ;
3125use arrow:: {
3226 array:: {
3327 cast:: AsArray ,
@@ -964,9 +958,11 @@ fn cast_array(
964958 cast_options : & SparkCastOptions ,
965959) -> DataFusionResult < ArrayRef > {
966960 use DataType :: * ;
967- let array = array_with_timezone ( array, cast_options. timezone . clone ( ) , Some ( to_type) ) ?;
968961 let from_type = array. data_type ( ) . clone ( ) ;
969962
963+ let array = array_with_timezone ( array, cast_options. timezone . clone ( ) , Some ( to_type) ) ?;
964+ let eval_mode = cast_options. eval_mode ;
965+
970966 let native_cast_options: CastOptions = CastOptions {
971967 safe : !matches ! ( cast_options. eval_mode, EvalMode :: Ansi ) , // take safe mode from cast_options passed
972968 format_options : FormatOptions :: new ( )
@@ -1015,10 +1011,8 @@ fn cast_array(
10151011 }
10161012 }
10171013 } ;
1018- let from_type = array. data_type ( ) ;
1019- let eval_mode = cast_options. eval_mode ;
10201014
1021- let cast_result = match ( from_type, to_type) {
1015+ let cast_result = match ( & from_type, to_type) {
10221016 ( Utf8 , Boolean ) => spark_cast_utf8_to_boolean :: < i32 > ( & array, eval_mode) ,
10231017 ( LargeUtf8 , Boolean ) => spark_cast_utf8_to_boolean :: < i64 > ( & array, eval_mode) ,
10241018 ( Utf8 , Timestamp ( _, _) ) => {
@@ -1044,10 +1038,10 @@ fn cast_array(
10441038 | ( Int16 , Int8 )
10451039 if eval_mode != EvalMode :: Try =>
10461040 {
1047- spark_cast_int_to_int ( & array, eval_mode, from_type, to_type)
1041+ spark_cast_int_to_int ( & array, eval_mode, & from_type, to_type)
10481042 }
10491043 ( Int8 | Int16 | Int32 | Int64 , Decimal128 ( precision, scale) ) => {
1050- cast_int_to_decimal128 ( & array, eval_mode, from_type, to_type, * precision, * scale)
1044+ cast_int_to_decimal128 ( & array, eval_mode, & from_type, to_type, * precision, * scale)
10511045 }
10521046 ( Utf8 , Int8 | Int16 | Int32 | Int64 ) => {
10531047 cast_string_to_int :: < i32 > ( to_type, & array, eval_mode)
@@ -1079,19 +1073,19 @@ fn cast_array(
10791073 | ( Decimal128 ( _, _) , Int64 )
10801074 if eval_mode != EvalMode :: Try =>
10811075 {
1082- spark_cast_nonintegral_numeric_to_integral ( & array, eval_mode, from_type, to_type)
1076+ spark_cast_nonintegral_numeric_to_integral ( & array, eval_mode, & from_type, to_type)
10831077 }
10841078 ( Decimal128 ( _p, _s) , Boolean ) => spark_cast_decimal_to_boolean ( & array) ,
10851079 ( Utf8View , Utf8 ) => Ok ( cast_with_options ( & array, to_type, & CAST_OPTIONS ) ?) ,
10861080 ( Struct ( _) , Utf8 ) => Ok ( casts_struct_to_string ( array. as_struct ( ) , cast_options) ?) ,
10871081 ( Struct ( _) , Struct ( _) ) => Ok ( cast_struct_to_struct (
10881082 array. as_struct ( ) ,
1089- from_type,
1083+ & from_type,
10901084 to_type,
10911085 cast_options,
10921086 ) ?) ,
10931087 ( List ( _) , Utf8 ) => Ok ( cast_array_to_string ( array. as_list ( ) , cast_options) ?) ,
1094- ( List ( _) , List ( _) ) if can_cast_types ( from_type, to_type) => {
1088+ ( List ( _) , List ( _) ) if can_cast_types ( & from_type, to_type) => {
10951089 Ok ( cast_with_options ( & array, to_type, & CAST_OPTIONS ) ?)
10961090 }
10971091 ( UInt8 | UInt16 | UInt32 | UInt64 , Int8 | Int16 | Int32 | Int64 )
@@ -1101,7 +1095,7 @@ fn cast_array(
11011095 }
11021096 ( Binary , Utf8 ) => Ok ( cast_binary_to_string :: < i32 > ( & array, cast_options) ?) ,
11031097 _ if cast_options. is_adapting_schema
1104- || is_datafusion_spark_compatible ( from_type, to_type) =>
1098+ || is_datafusion_spark_compatible ( & from_type, to_type) =>
11051099 {
11061100 // use DataFusion cast only when we know that it is compatible with Spark
11071101 Ok ( cast_with_options ( & array, to_type, & native_cast_options) ?)
@@ -1115,7 +1109,7 @@ fn cast_array(
11151109 ) ) )
11161110 }
11171111 } ;
1118- Ok ( spark_cast_postprocess ( cast_result?, from_type, to_type) )
1112+ Ok ( spark_cast_postprocess ( cast_result?, & from_type, to_type) )
11191113}
11201114
11211115fn cast_string_to_float (
0 commit comments