@@ -655,17 +655,21 @@ impl<'a> Analysis<'a> {
655655 }
656656 }
657657
658+ let project = self . analyze_projection ( & mut ctx, & query. projection ) ?;
659+
658660 if let Some ( order_by) = & query. order_by {
659- if !matches ! ( & order_by. expr. value, Value :: Access ( _) ) {
661+ self . analyze_expr ( & mut ctx, & order_by. expr , Type :: Unspecified ) ?;
662+
663+ if query. group_by . is_none ( ) && !matches ! ( & order_by. expr. value, Value :: Access ( _) ) {
660664 return Err ( AnalysisError :: ExpectFieldLiteral (
661665 order_by. expr . attrs . pos . line ,
662666 order_by. expr . attrs . pos . col ,
663667 ) ) ;
668+ } else if query. group_by . is_some ( ) {
669+ self . expect_agg_expr ( & order_by. expr ) ?;
664670 }
665- self . analyze_expr ( & mut ctx, & order_by. expr , Type :: Unspecified ) ?;
666671 }
667672
668- let project = self . analyze_projection ( & mut ctx, & query. projection ) ?;
669673 let scope = self . exit_scope ( ) ;
670674
671675 Ok ( Query {
@@ -875,11 +879,11 @@ impl<'a> Analysis<'a> {
875879 ) ) ;
876880 }
877881
878- for arg in & app. args {
879- if * aggregate {
880- self . ensure_agg_param_is_source_bound ( arg) ?;
881- }
882+ if * aggregate {
883+ return self . expect_agg_expr ( expr) ;
884+ }
882885
886+ for arg in & app. args {
883887 self . invalidate_agg_func_usage ( arg) ?;
884888 }
885889 }
@@ -897,7 +901,27 @@ impl<'a> Analysis<'a> {
897901 }
898902 }
899903
900- fn ensure_agg_param_is_source_bound ( & mut self , expr : & Expr ) -> AnalysisResult < ( ) > {
904+ fn expect_agg_expr ( & self , expr : & Expr ) -> AnalysisResult < ( ) > {
905+ if let Value :: App ( app) = & expr. value
906+ && let Some ( Type :: App {
907+ aggregate : true , ..
908+ } ) = self . options . default_scope . entries . get ( app. func . as_str ( ) )
909+ {
910+ for arg in & app. args {
911+ self . ensure_agg_param_is_source_bound ( arg) ?;
912+ self . invalidate_agg_func_usage ( arg) ?;
913+ }
914+
915+ return Ok ( ( ) ) ;
916+ }
917+
918+ Err ( AnalysisError :: ExpectAggExpr (
919+ expr. attrs . pos . line ,
920+ expr. attrs . pos . col ,
921+ ) )
922+ }
923+
924+ fn ensure_agg_param_is_source_bound ( & self , expr : & Expr ) -> AnalysisResult < ( ) > {
901925 match & expr. value {
902926 Value :: Id ( id) if !self . options . default_scope . entries . contains_key ( id. as_str ( ) ) => {
903927 Ok ( ( ) )
@@ -914,7 +938,7 @@ impl<'a> Analysis<'a> {
914938 }
915939
916940 fn ensure_agg_binary_op_is_source_bound (
917- & mut self ,
941+ & self ,
918942 attrs : & Attrs ,
919943 binary : & Binary ,
920944 ) -> AnalysisResult < ( ) > {
@@ -930,7 +954,7 @@ impl<'a> Analysis<'a> {
930954 Ok ( ( ) )
931955 }
932956
933- fn ensure_agg_binary_op_branch_is_source_bound ( & mut self , expr : & Expr ) -> bool {
957+ fn ensure_agg_binary_op_branch_is_source_bound ( & self , expr : & Expr ) -> bool {
934958 match & expr. value {
935959 Value :: Id ( id) => !self . options . default_scope . entries . contains_key ( id. as_str ( ) ) ,
936960 Value :: Array ( exprs) => {
@@ -965,7 +989,7 @@ impl<'a> Analysis<'a> {
965989 }
966990 }
967991
968- fn invalidate_agg_func_usage ( & mut self , expr : & Expr ) -> AnalysisResult < ( ) > {
992+ fn invalidate_agg_func_usage ( & self , expr : & Expr ) -> AnalysisResult < ( ) > {
969993 match & expr. value {
970994 Value :: Number ( _)
971995 | Value :: String ( _)
0 commit comments