{"record":{"id":"be03b08304cca9f7","repo":"hibernate/hibernate-orm","slug":"inverse-distribution-function-s-must-specify-w","errorCode":null,"errorMessage":"Inverse distribution function '%s' must specify 'WITHIN GROUP'","messagePattern":"Inverse distribution function '(.+?)' must specify 'WITHIN GROUP'","errorType":"exception","errorClass":"SemanticException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/dialect/function/InverseDistributionFunction.java","lineNumber":152,"sourceCode":"\t\t\t\tList<? extends SqmTypedNode<?>> arguments,\n\t\t\t\tSqmPredicate filter,\n\t\t\t\tSqmOrderByClause withinGroupClause,\n\t\t\t\tReturnableType<T> impliedResultType,\n\t\t\t\tQueryEngine queryEngine) {\n\t\t\tsuper(\n\t\t\t\t\tInverseDistributionFunction.this,\n\t\t\t\t\tInverseDistributionFunction.this,\n\t\t\t\t\targuments,\n\t\t\t\t\tfilter,\n\t\t\t\t\twithinGroupClause,\n\t\t\t\t\timpliedResultType,\n\t\t\t\t\tInverseDistributionFunction.this.getArgumentsValidator(),\n\t\t\t\t\tInverseDistributionFunction.this.getReturnTypeResolver(),\n\t\t\t\t\tqueryEngine.getCriteriaBuilder(),\n\t\t\t\t\tInverseDistributionFunction.this.getName()\n\t\t\t);\n\t\t\tif ( withinGroupClause == null ) {\n\t\t\t\tthrow new SemanticException(\"Inverse distribution function '\" + getFunctionName()\n\t\t\t\t\t\t+ \"' must specify 'WITHIN GROUP'\");\n\t\t\t}\n\t\t}\n\n\t\t@Override\n\t\tprotected ReturnableType<?> determineResultType(\n\t\t\t\tSqmToSqlAstConverter converter,\n\t\t\t\tTypeConfiguration typeConfiguration) {\n\t\t\treturn (ReturnableType<?>)\n\t\t\t\t\tgetWithinGroup().getSortSpecifications().get( 0 )\n\t\t\t\t\t\t\t.getSortExpression()\n\t\t\t\t\t\t\t.getExpressible()\n\t\t\t\t\t\t\t.getSqmType();\n\t\t}\n\n\t\t@Override\n\t\tprotected MappingModelExpressible<?> getMappingModelExpressible(\n\t\t\t\tSqmToSqlAstConverter walker,","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/dialect/function/InverseDistributionFunction.java#L134-L170","documentation":"Inverse distribution (ordered-set aggregate) functions are defined by the SQL standard to require a WITHIN GROUP clause naming the sort expression they operate on. Hibernate's SQM representation enforces this: when the function expression is built or converted without a withinGroupClause, a SemanticException is thrown during query interpretation.","triggerScenarios":"HQL: select percentile_cont(0.5) from T (no 'within group (order by ...)'); the same for percentile_disc(...) or mode() called like a plain aggregate.","commonSituations":"Treating percentile functions like ordinary aggregates; HQL generators that omit WITHIN GROUP; copy-paste from SQL dialects where the clause is optional or has defaults.","solutions":["Add the required clause: percentile_cont(0.5) within group (order by t.value)","For MODE use mode() within group (order by x) with exactly one order-by item","Fix query-builder code that strips or never emits WITHIN GROUP"],"exampleFix":"// before\nselect percentile_cont(0.5) from Sale s\n\n// after\nselect percentile_cont(0.5) within group (order by s.amount) from Sale s","handlingStrategy":"validation","validationCode":"// Ordered-set aggregates require WITHIN GROUP in HQL\nstatic void requireWithinGroup(String hql, String fn) {\n    String lower = hql.toLowerCase(java.util.Locale.ROOT);\n    if (lower.contains(fn.toLowerCase()) && !lower.contains(\"within group\")) {\n        throw new IllegalStateException(fn + \" requires 'within group (order by ...)'\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    return em.createQuery(hql, Object.class).getResultList();\n} catch (org.hibernate.query.SemanticException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"WITHIN GROUP\")) {\n        throw new QuerySetupException(\"Add 'within group (order by ...)' to the ordered-set aggregate\", e);\n    }\n    throw e;\n}","preventionTips":["Treat WITHIN GROUP as mandatory syntax for percentile_/mode in HQL templates","Lint generated HQL for ordered-set function calls missing WITHIN GROUP","Copy tested examples, not partial snippets, when using ordered-set aggregates"],"tags":["hibernate","hql","within-group","ordered-set-aggregate","semantic-error"],"backgroundTag":"within-group-clause-missing","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}