@@ -382,6 +382,19 @@ private static void checkCategorizeGrouping(Aggregate agg, Set<Failure> failures
382382                    );
383383                }
384384            })));
385+         agg .aggregates ()
386+             .forEach (a  -> a .forEachDown (FilteredExpression .class , fe  -> fe .filter ().forEachDown (Attribute .class , attribute  -> {
387+                 var  categorize  = categorizeByAttribute .get (attribute );
388+                 if  (categorize  != null ) {
389+                     failures .add (
390+                         fail (
391+                             attribute ,
392+                             "cannot reference CATEGORIZE grouping function [{}] within an aggregation filter" ,
393+                             attribute .sourceText ()
394+                         )
395+                     );
396+                 }
397+             })));
385398    }
386399
387400    private  static  void  checkRateAggregates (Expression  expr , int  nestedLevel , Set <Failure > failures ) {
@@ -421,7 +434,8 @@ private static void checkInvalidNamedExpressionUsage(
421434                Expression  filter  = fe .filter ();
422435                failures .add (fail (filter , "WHERE clause allowed only for aggregate functions, none found in [{}]" , fe .sourceText ()));
423436            }
424-             Expression  f  = fe .filter (); // check the filter has to be a boolean term, similar as checkFilterConditionType 
437+             Expression  f  = fe .filter ();
438+             // check the filter has to be a boolean term, similar as checkFilterConditionType 
425439            if  (f .dataType () != NULL  && f .dataType () != BOOLEAN ) {
426440                failures .add (fail (f , "Condition expression needs to be boolean, found [{}]" , f .dataType ()));
427441            }
@@ -432,9 +446,10 @@ private static void checkInvalidNamedExpressionUsage(
432446                        fail (af , "cannot use aggregate function [{}] in aggregate WHERE clause [{}]" , af .sourceText (), fe .sourceText ())
433447                    );
434448                }
435-                 // check the bucketing  function against the group 
449+                 // check the grouping  function against the group 
436450                else  if  (c  instanceof  GroupingFunction  gf ) {
437-                     if  (Expressions .anyMatch (groups , ex  -> ex  instanceof  Alias  a  && a .child ().semanticEquals (gf )) == false ) {
451+                     if  (c  instanceof  Categorize 
452+                         || Expressions .anyMatch (groups , ex  -> ex  instanceof  Alias  a  && a .child ().semanticEquals (gf )) == false ) {
438453                        failures .add (fail (gf , "can only use grouping function [{}] as part of the BY clause" , gf .sourceText ()));
439454                    }
440455                }
0 commit comments