@@ -324,34 +324,35 @@ pub fn cast_with_options(
324324 cast_type : DataType ,
325325 cast_options : Option < CastOptions < ' static > > ,
326326) -> Result < Arc < dyn PhysicalExpr > > {
327+ // borrow expr and temporary field reference; the helper clones as needed
327328 cast_with_target_field_and_options (
328- expr,
329+ & expr,
329330 input_schema,
330- cast_type. into_nullable_field_ref ( ) ,
331+ & cast_type. into_nullable_field_ref ( ) ,
331332 cast_options,
332333 )
333334}
334335
335336/// Return a [`PhysicalExpr`] representing `expr` casted to `target_field`,
336337/// preserving the field metadata in the resulting expression.
337338pub ( crate ) fn cast_with_target_field_and_options (
338- expr : Arc < dyn PhysicalExpr > ,
339+ expr : & Arc < dyn PhysicalExpr > ,
339340 input_schema : & Schema ,
340- target_field : FieldRef ,
341+ target_field : & FieldRef ,
341342 cast_options : Option < CastOptions < ' static > > ,
342- ) -> Result < Arc < dyn PhysicalExpr > > {
343+ ) -> Result < Arc < dyn PhysicalExpr > > { // borrow inputs to satisfy clippy
343344 let expr_type = expr. data_type ( input_schema) ?;
344345 let cast_type = target_field. data_type ( ) ;
345346 let candidate = CastExpr :: new_with_target_field (
346- Arc :: clone ( & expr) ,
347- Arc :: clone ( & target_field) ,
347+ Arc :: clone ( expr) ,
348+ Arc :: clone ( target_field) ,
348349 cast_options. clone ( ) ,
349350 ) ;
350351
351352 if expr_type == * cast_type
352353 && candidate. preserves_child_field_semantics ( input_schema) ?
353354 {
354- return Ok ( Arc :: clone ( & expr) ) ;
355+ return Ok ( Arc :: clone ( expr) ) ;
355356 }
356357
357358 let is_valid_cast = match ( & expr_type, cast_type) {
@@ -933,15 +934,16 @@ mod tests {
933934 #[ test]
934935 fn field_aware_same_type_cast_preserves_explicit_target_field ( ) -> Result < ( ) > {
935936 let schema = Schema :: new ( vec ! [ Field :: new( "a" , Int32 , false ) ] ) ;
936- 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- ) ?;
937+ let a_col = col ( "a" , & schema) ?;
938+ let logical_field = Arc :: new ( Field :: new ( "logical_a" , Int32 , true ) . with_metadata (
939+ HashMap :: from ( [ ( "target_meta" . to_string ( ) , "1" . to_string ( ) ) ] ) ,
940+ ) ) ;
941+ let expr = cast_with_target_field_and_options (
942+ & a_col,
943+ & schema,
944+ & logical_field,
945+ None ,
946+ ) ?;
945947
946948 let cast_expr = expr
947949 . as_any ( )
@@ -962,10 +964,12 @@ mod tests {
962964 #[ test]
963965 fn default_same_type_cast_is_elided ( ) -> Result < ( ) > {
964966 let schema = Schema :: new ( vec ! [ Field :: new( "a" , Int32 , false ) ] ) ;
967+ let a_col = col ( "a" , & schema) ?;
968+ let target_field = Int32 . into_nullable_field_ref ( ) ;
965969 let expr = cast_with_target_field_and_options (
966- col ( "a" , & schema ) ? ,
970+ & a_col ,
967971 & schema,
968- Int32 . into_nullable_field_ref ( ) ,
972+ & target_field ,
969973 None ,
970974 ) ?;
971975
0 commit comments