@@ -1239,6 +1239,7 @@ impl DefaultPhysicalPlanner {
12391239 children. one ( ) ?,
12401240 input,
12411241 expr,
1242+ node. schema ( ) ,
12421243 ) ?,
12431244 LogicalPlan :: Filter ( Filter {
12441245 predicate, input, ..
@@ -1462,6 +1463,7 @@ impl DefaultPhysicalPlanner {
14621463 physical_left,
14631464 input,
14641465 expr,
1466+ left. schema ( ) ,
14651467 ) ?,
14661468 _ => physical_left,
14671469 } ;
@@ -1476,6 +1478,7 @@ impl DefaultPhysicalPlanner {
14761478 physical_right,
14771479 input,
14781480 expr,
1481+ right. schema ( ) ,
14791482 ) ?,
14801483 _ => physical_right,
14811484 } ;
@@ -1860,6 +1863,7 @@ impl DefaultPhysicalPlanner {
18601863 join,
18611864 input,
18621865 expr,
1866+ new_logical. schema ( ) ,
18631867 ) ?
18641868 } else {
18651869 join
@@ -3088,6 +3092,7 @@ impl DefaultPhysicalPlanner {
30883092 input_exec : Arc < dyn ExecutionPlan > ,
30893093 input : & Arc < LogicalPlan > ,
30903094 expr : & [ Expr ] ,
3095+ output_schema : & DFSchema ,
30913096 ) -> Result < Arc < dyn ExecutionPlan > > {
30923097 let input_logical_schema = input. as_ref ( ) . schema ( ) ;
30933098 let input_physical_schema = input_exec. schema ( ) ;
@@ -3145,7 +3150,11 @@ impl DefaultPhysicalPlanner {
31453150 . into_iter ( )
31463151 . map ( |( expr, alias) | ProjectionExpr { expr, alias } )
31473152 . collect ( ) ;
3148- Ok ( Arc :: new ( ProjectionExec :: try_new ( proj_exprs, input_exec) ?) )
3153+ Ok ( Arc :: new ( ProjectionExec :: try_new_with_schema_metadata (
3154+ proj_exprs,
3155+ input_exec,
3156+ output_schema. as_arrow ( ) ,
3157+ ) ?) )
31493158 }
31503159 PlanAsyncExpr :: Async (
31513160 async_map,
@@ -3157,8 +3166,11 @@ impl DefaultPhysicalPlanner {
31573166 . into_iter ( )
31583167 . map ( |( expr, alias) | ProjectionExpr { expr, alias } )
31593168 . collect ( ) ;
3160- let new_proj_exec =
3161- ProjectionExec :: try_new ( proj_exprs, Arc :: new ( async_exec) ) ?;
3169+ let new_proj_exec = ProjectionExec :: try_new_with_schema_metadata (
3170+ proj_exprs,
3171+ Arc :: new ( async_exec) ,
3172+ output_schema. as_arrow ( ) ,
3173+ ) ?;
31623174 Ok ( Arc :: new ( new_proj_exec) )
31633175 }
31643176 _ => internal_err ! ( "Unexpected PlanAsyncExpressions variant" ) ,
@@ -3505,6 +3517,43 @@ mod tests {
35053517 Ok ( ( ) )
35063518 }
35073519
3520+ #[ tokio:: test]
3521+ async fn test_projection_preserves_field_metadata_for_aggregate ( ) -> Result < ( ) > {
3522+ use datafusion_common:: metadata:: FieldMetadata ;
3523+ use datafusion_expr:: expr:: AggregateFunction ;
3524+ use datafusion_functions_aggregate:: min_max:: max_udaf;
3525+
3526+ let schema = Schema :: new ( vec ! [ Field :: new( "value" , DataType :: Utf8 , false ) ] ) ;
3527+ let input = LogicalPlan :: EmptyRelation ( EmptyRelation {
3528+ produce_one_row : false ,
3529+ schema : Arc :: new ( schema. to_dfschema ( ) ?) ,
3530+ } ) ;
3531+ let metadata =
3532+ FieldMetadata :: from ( HashMap :: from ( [ ( "foo" . to_string ( ) , "bar" . to_string ( ) ) ] ) ) ;
3533+ let projection = LogicalPlan :: Projection ( Projection :: try_new (
3534+ vec ! [ col( "value" ) . alias_with_metadata( "value" , Some ( metadata) ) ] ,
3535+ Arc :: new ( input) ,
3536+ ) ?) ;
3537+ let aggregate = LogicalPlan :: Aggregate ( Aggregate :: try_new (
3538+ Arc :: new ( projection) ,
3539+ vec ! [ ] ,
3540+ vec ! [ Expr :: AggregateFunction ( AggregateFunction :: new_udf(
3541+ max_udaf( ) ,
3542+ vec![ col( "value" ) ] ,
3543+ false ,
3544+ None ,
3545+ vec![ ] ,
3546+ None ,
3547+ ) ) ] ,
3548+ ) ?) ;
3549+
3550+ DefaultPhysicalPlanner :: default ( )
3551+ . create_physical_plan ( & aggregate, & SessionContext :: new ( ) . state ( ) )
3552+ . await ?;
3553+
3554+ Ok ( ( ) )
3555+ }
3556+
35083557 #[ derive( Debug , Default ) ]
35093558 struct NullAccumulator ;
35103559
0 commit comments