@@ -382,6 +382,18 @@ private static void checkCategorizeGrouping(Aggregate agg, Set<Failure> failures
382382                    );
383383                }
384384            })));
385+         agg .aggregates ().forEach (a  -> a .forEachDown (FilteredExpression .class , fe  -> fe .filter ().forEachDown (Attribute .class , attribute  -> {
386+             var  categorize  = categorizeByAttribute .get (attribute );
387+             if  (categorize  != null ) {
388+                 failures .add (
389+                     fail (
390+                         attribute ,
391+                         "cannot reference CATEGORIZE grouping function [{}] within an aggregation filter" ,
392+                         attribute .sourceText ()
393+                     )
394+                 );
395+             }
396+         })));
385397    }
386398
387399    private  static  void  checkRateAggregates (Expression  expr , int  nestedLevel , Set <Failure > failures ) {
@@ -421,7 +433,8 @@ private static void checkInvalidNamedExpressionUsage(
421433                Expression  filter  = fe .filter ();
422434                failures .add (fail (filter , "WHERE clause allowed only for aggregate functions, none found in [{}]" , fe .sourceText ()));
423435            }
424-             Expression  f  = fe .filter (); // check the filter has to be a boolean term, similar as checkFilterConditionType 
436+             Expression  f  = fe .filter ();
437+             // check the filter has to be a boolean term, similar as checkFilterConditionType 
425438            if  (f .dataType () != NULL  && f .dataType () != BOOLEAN ) {
426439                failures .add (fail (f , "Condition expression needs to be boolean, found [{}]" , f .dataType ()));
427440            }
@@ -432,9 +445,10 @@ private static void checkInvalidNamedExpressionUsage(
432445                        fail (af , "cannot use aggregate function [{}] in aggregate WHERE clause [{}]" , af .sourceText (), fe .sourceText ())
433446                    );
434447                }
435-                 // check the bucketing  function against the group 
448+                 // check the grouping  function against the group 
436449                else  if  (c  instanceof  GroupingFunction  gf ) {
437-                     if  (Expressions .anyMatch (groups , ex  -> ex  instanceof  Alias  a  && a .child ().semanticEquals (gf )) == false ) {
450+                     if  (c  instanceof  Categorize 
451+                         || Expressions .anyMatch (groups , ex  -> ex  instanceof  Alias  a  && a .child ().semanticEquals (gf )) == false ) {
438452                        failures .add (fail (gf , "can only use grouping function [{}] as part of the BY clause" , gf .sourceText ()));
439453                    }
440454                }
0 commit comments