@@ -651,7 +651,16 @@ impl<'a> Analysis<'a> {
651651 self . analyze_expr ( & mut ctx, & group_by. expr , Type :: Unspecified ) ?;
652652
653653 if let Some ( expr) = & group_by. predicate {
654+ ctx. allow_agg_func = true ;
655+ ctx. use_agg_funcs = true ;
656+
654657 self . analyze_expr ( & mut ctx, expr, Type :: Bool ) ?;
658+ if !self . expect_agg_expr ( expr) ? {
659+ return Err ( AnalysisError :: ExpectAggExpr (
660+ expr. attrs . pos . line ,
661+ expr. attrs . pos . col ,
662+ ) ) ;
663+ }
655664 }
656665
657666 ctx. allow_agg_func = true ;
@@ -669,7 +678,7 @@ impl<'a> Analysis<'a> {
669678 order_by. expr . attrs . pos . col ,
670679 ) ) ;
671680 } else if query. group_by . is_some ( ) {
672- self . expect_agg_expr ( & order_by. expr ) ?;
681+ self . expect_agg_func ( & order_by. expr ) ?;
673682 }
674683 }
675684
@@ -903,7 +912,7 @@ impl<'a> Analysis<'a> {
903912 }
904913
905914 if * aggregate {
906- return self . expect_agg_expr ( expr) ;
915+ return self . expect_agg_func ( expr) ;
907916 }
908917
909918 for arg in & app. args {
@@ -924,7 +933,7 @@ impl<'a> Analysis<'a> {
924933 }
925934 }
926935
927- fn expect_agg_expr ( & self , expr : & Expr ) -> AnalysisResult < ( ) > {
936+ fn expect_agg_func ( & self , expr : & Expr ) -> AnalysisResult < ( ) > {
928937 if let Value :: App ( app) = & expr. value
929938 && let Some ( Type :: App {
930939 aggregate : true , ..
@@ -944,6 +953,42 @@ impl<'a> Analysis<'a> {
944953 ) )
945954 }
946955
956+ fn expect_agg_expr ( & self , expr : & Expr ) -> AnalysisResult < bool > {
957+ match & expr. value {
958+ Value :: Id ( id) => {
959+ if self . scope . entries . contains_key ( id. as_str ( ) ) {
960+ return Err ( AnalysisError :: UnallowedAggFuncUsageWithSrcField (
961+ expr. attrs . pos . line ,
962+ expr. attrs . pos . col ,
963+ ) ) ;
964+ }
965+
966+ Ok ( false )
967+ }
968+ Value :: Group ( expr) => self . expect_agg_expr ( expr) ,
969+ Value :: Binary ( binary) => {
970+ let lhs = self . expect_agg_expr ( & binary. lhs ) ?;
971+ let rhs = self . expect_agg_expr ( & binary. rhs ) ?;
972+
973+ if !lhs && !rhs {
974+ return Err ( AnalysisError :: ExpectAggExpr (
975+ expr. attrs . pos . line ,
976+ expr. attrs . pos . col ,
977+ ) ) ;
978+ }
979+
980+ Ok ( true )
981+ }
982+ Value :: Unary ( unary) => self . expect_agg_expr ( unary. expr . as_ref ( ) ) ,
983+ Value :: App ( _) => {
984+ self . expect_agg_func ( expr) ?;
985+ Ok ( true )
986+ }
987+
988+ _ => Ok ( false ) ,
989+ }
990+ }
991+
947992 fn ensure_agg_param_is_source_bound ( & self , expr : & Expr ) -> AnalysisResult < ( ) > {
948993 match & expr. value {
949994 Value :: Id ( id) if !self . options . default_scope . entries . contains_key ( id. as_str ( ) ) => {
0 commit comments