@@ -341,26 +341,23 @@ pub(crate) fn cast_with_target_field_and_options(
341341 cast_options : Option < CastOptions < ' static > > ,
342342) -> Result < Arc < dyn PhysicalExpr > > {
343343 let expr_type = expr. data_type ( input_schema) ?;
344- let cast_type = target_field. data_type ( ) ;
345- let candidate = CastExpr :: new_with_target_field (
346- Arc :: clone ( & expr) ,
347- Arc :: clone ( & target_field) ,
348- cast_options. clone ( ) ,
349- ) ;
350-
351- if expr_type == * cast_type
344+ let cast_type = target_field. data_type ( ) . clone ( ) ;
345+ let candidate =
346+ CastExpr :: new_with_target_field ( Arc :: clone ( & expr) , target_field, cast_options) ;
347+
348+ if expr_type == cast_type
352349 && candidate. preserves_child_field_semantics ( input_schema) ?
353350 {
354- return Ok ( Arc :: clone ( & expr) ) ;
351+ return Ok ( expr) ;
355352 }
356353
357- let is_valid_cast = match ( & expr_type, cast_type) {
354+ let is_valid_cast = match ( & expr_type, & cast_type) {
358355 // Allow struct-to-struct casts that pass name-based compatibility validation.
359356 // This validation is applied at planning time (now) to fail fast, rather than
360357 // deferring errors to execution time. The name-based casting logic will be
361358 // executed at runtime via ColumnarValue::cast_to.
362- ( Struct ( _) , Struct ( _) ) => can_cast_struct_types ( & expr_type, cast_type) ,
363- _ => can_cast_types ( & expr_type, cast_type) ,
359+ ( Struct ( _) , Struct ( _) ) => can_cast_struct_types ( & expr_type, & cast_type) ,
360+ _ => can_cast_types ( & expr_type, & cast_type) ,
364361 } ;
365362
366363 if !is_valid_cast {
@@ -933,15 +930,12 @@ mod tests {
933930 #[ test]
934931 fn field_aware_same_type_cast_preserves_explicit_target_field ( ) -> Result < ( ) > {
935932 let schema = Schema :: new ( vec ! [ Field :: new( "a" , Int32 , false ) ] ) ;
933+ let a_col = col ( "a" , & schema) ?;
934+ let logical_field = Arc :: new ( Field :: new ( "logical_a" , Int32 , true ) . with_metadata (
935+ HashMap :: from ( [ ( "target_meta" . to_string ( ) , "1" . to_string ( ) ) ] ) ,
936+ ) ) ;
936937 let expr =
937- cast_with_target_field_and_options (
938- col ( "a" , & schema) ?,
939- & schema,
940- Arc :: new ( Field :: new ( "logical_a" , Int32 , true ) . with_metadata (
941- HashMap :: from ( [ ( "target_meta" . to_string ( ) , "1" . to_string ( ) ) ] ) ,
942- ) ) ,
943- None ,
944- ) ?;
938+ cast_with_target_field_and_options ( a_col, & schema, logical_field, None ) ?;
945939
946940 let cast_expr = expr
947941 . as_any ( )
@@ -962,12 +956,10 @@ mod tests {
962956 #[ test]
963957 fn default_same_type_cast_is_elided ( ) -> Result < ( ) > {
964958 let schema = Schema :: new ( vec ! [ Field :: new( "a" , Int32 , false ) ] ) ;
965- let expr = cast_with_target_field_and_options (
966- col ( "a" , & schema) ?,
967- & schema,
968- Int32 . into_nullable_field_ref ( ) ,
969- None ,
970- ) ?;
959+ let a_col = col ( "a" , & schema) ?;
960+ let target_field = Int32 . into_nullable_field_ref ( ) ;
961+ let expr =
962+ cast_with_target_field_and_options ( a_col, & schema, target_field, None ) ?;
971963
972964 assert ! ( expr. as_any( ) . downcast_ref:: <Column >( ) . is_some( ) ) ;
973965 assert ! ( expr. as_any( ) . downcast_ref:: <CastExpr >( ) . is_none( ) ) ;
0 commit comments