@@ -1782,21 +1782,24 @@ pub fn create_aggregate_expr_and_maybe_filter(
1782
1782
physical_input_schema : & Schema ,
1783
1783
execution_props : & ExecutionProps ,
1784
1784
) -> Result < AggregateExprWithOptionalArgs > {
1785
- // unpack (nested) aliased logical expressions, e.g. "sum(col) as total"
1785
+ // Unpack (potentially nested) aliased logical expressions, e.g. "sum(col) as total"
1786
+ // Some functions like `count_all()` create internal aliases,
1787
+ // Unwrap all alias layers to get to the underlying aggregate function
1786
1788
let ( name, human_display, e) = match e {
1787
- Expr :: Alias ( Alias { expr, name, .. } ) => {
1788
- ( Some ( name. clone ( ) ) , String :: default ( ) , expr. as_ref ( ) )
1789
+ Expr :: Alias ( Alias { name, .. } ) => {
1790
+ let unaliased = e. clone ( ) . unalias_nested ( ) . data ;
1791
+ ( Some ( name. clone ( ) ) , e. human_display ( ) . to_string ( ) , unaliased)
1789
1792
}
1790
1793
Expr :: AggregateFunction ( _) => (
1791
1794
Some ( e. schema_name ( ) . to_string ( ) ) ,
1792
1795
e. human_display ( ) . to_string ( ) ,
1793
- e,
1796
+ e. clone ( ) ,
1794
1797
) ,
1795
- _ => ( None , String :: default ( ) , e) ,
1798
+ _ => ( None , String :: default ( ) , e. clone ( ) ) ,
1796
1799
} ;
1797
1800
1798
1801
create_aggregate_expr_with_name_and_maybe_filter (
1799
- e,
1802
+ & e,
1800
1803
name,
1801
1804
human_display,
1802
1805
logical_input_schema,
@@ -2416,6 +2419,7 @@ mod tests {
2416
2419
use datafusion_expr:: {
2417
2420
col, lit, LogicalPlanBuilder , Operator , UserDefinedLogicalNodeCore ,
2418
2421
} ;
2422
+ use datafusion_functions_aggregate:: count:: count_all;
2419
2423
use datafusion_functions_aggregate:: expr_fn:: sum;
2420
2424
use datafusion_physical_expr:: expressions:: { BinaryExpr , IsNotNullExpr } ;
2421
2425
use datafusion_physical_expr:: EquivalenceProperties ;
@@ -2876,6 +2880,25 @@ mod tests {
2876
2880
Ok ( ( ) )
2877
2881
}
2878
2882
2883
+ #[ tokio:: test]
2884
+ async fn test_aggregate_count_all_with_alias ( ) -> Result < ( ) > {
2885
+ let schema = Arc :: new ( Schema :: new ( vec ! [
2886
+ Field :: new( "c1" , DataType :: Utf8 , false ) ,
2887
+ Field :: new( "c2" , DataType :: UInt32 , false ) ,
2888
+ ] ) ) ;
2889
+
2890
+ let logical_plan = scan_empty ( None , schema. as_ref ( ) , None ) ?
2891
+ . aggregate ( Vec :: < Expr > :: new ( ) , vec ! [ count_all( ) . alias( "total_rows" ) ] ) ?
2892
+ . build ( ) ?;
2893
+
2894
+ let physical_plan = plan ( & logical_plan) . await ?;
2895
+ assert_eq ! (
2896
+ "total_rows" ,
2897
+ physical_plan. schema( ) . field( 0 ) . name( ) . as_str( )
2898
+ ) ;
2899
+ Ok ( ( ) )
2900
+ }
2901
+
2879
2902
#[ tokio:: test]
2880
2903
async fn test_explain ( ) {
2881
2904
let schema = Schema :: new ( vec ! [ Field :: new( "id" , DataType :: Int32 , false ) ] ) ;
0 commit comments