{"record":{"id":"950d16f93d22e94d","repo":"hibernate/hibernate-orm","slug":"filter-clause-is-illegal-for-non-aggregate-funct","errorCode":null,"errorMessage":"'FILTER' clause is illegal for non-aggregate function: {functionName}","messagePattern":"'FILTER' clause is illegal for non-aggregate function: (.+?)","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java","lineNumber":4713,"sourceCode":"\t\t\t\t\ttrue,\n\t\t\t\t\tnull,\n\t\t\t\t\tStandardFunctionReturnTypeResolvers.invariant(\n\t\t\t\t\t\t\tresolveExpressibleTypeBasic( Object.class )\n\t\t\t\t\t),\n\t\t\t\t\tnull,\n\t\t\t\t\tfunctionName,\n\t\t\t\t\tinferFunctionKind( ctx ),\n\t\t\t\t\tnull,\n\t\t\t\t\tSqlAstNodeRenderingMode.DEFAULT\n\t\t\t);\n\t\t}\n\t\telse {\n\t\t\tif ( safeModeEnabled && \"sql\".equals( functionName ) ) {\n\t\t\t\tfailIfSafeModeEnabled( safeModeEnabled, functionName, query );\n\t\t\t}\n\t\t\tfinal var functionKind = functionTemplate.getFunctionKind();\n\t\t\tif ( ctx.filterClause() != null && functionKind == FunctionKind.NORMAL ) {\n\t\t\t\tthrow new SemanticException( \"'FILTER' clause is illegal for non-aggregate function: \"\n\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\tquery );\n\t\t\t}\n\t\t\tif ( ctx.overClause() != null && functionKind == FunctionKind.NORMAL ) {\n\t\t\t\tthrow new SemanticException( \"'OVER' clause is illegal for non-aggregate function: \"\n\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\tquery );\n\t\t\t}\n\t\t\tif ( ctx.withinGroupClause() != null && functionKind == FunctionKind.NORMAL ) {\n\t\t\t\tthrow new SemanticException( \"'WITHIN' GROUP clause is illegal for non-aggregate function: \"\n\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\tquery );\n\t\t\t}\n\t\t\tif ( ctx.overClause() == null && functionKind == FunctionKind.WINDOW ) {\n\t\t\t\tthrow new SemanticException( \"'OVER' clause is mandatory for window-only function: \"\n\t\t\t\t\t\t+ functionName,\n\t\t\t\t\t\tquery );\n\t\t\t}","sourceCodeStart":4695,"sourceCodeEnd":4731,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java#L4695-L4731","documentation":"Error \"'FILTER' clause is illegal for non-aggregate function: {functionName}\" thrown in hibernate/hibernate-orm.","triggerScenarios":"An aggregate/window clause is applied to a function that does not accept it: 'FILTER' clause is illegal for non-aggregate function: <value>","commonSituations":"HQL queries that attach FILTER, OVER, WITHIN GROUP, RESPECT/IGNORE NULLS, or FROM FIRST/LAST to functions that are not aggregate or window functions, or omit a mandatory OVER/WITHIN GROUP clause.","solutions":["FILTER is only legal on aggregate functions. Remove the FILTER clause, or use it with an aggregate function (count, sum, avg, min, max, ...)."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}