{"record":{"id":"ca47fc45f5ccc494","repo":"hibernate/hibernate-orm","slug":"the-function-name-is-not-an-ordered-set-aggregat","errorCode":null,"errorMessage":"The function {name} is not an ordered set-aggregate function","messagePattern":"The function (.+?) is not an ordered set-aggregate function","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/function/AbstractSqmSelfRenderingFunctionDescriptor.java","lineNumber":137,"sourceCode":"\t\t\t\targuments,\n\t\t\t\tfilter,\n\t\t\t\timpliedResultType,\n\t\t\t\tgetArgumentsValidator(),\n\t\t\t\tgetReturnTypeResolver(),\n\t\t\t\tqueryEngine.getCriteriaBuilder(),\n\t\t\t\tgetName()\n\t\t);\n\t}\n\n\t@Override\n\tpublic <T> SelfRenderingSqmOrderedSetAggregateFunction<T> generateSqmOrderedSetAggregateFunctionExpression(\n\t\t\tList<? extends SqmTypedNode<?>> arguments,\n\t\t\tSqmPredicate filter,\n\t\t\tSqmOrderByClause withinGroupClause,\n\t\t\tReturnableType<T> impliedResultType,\n\t\t\tQueryEngine queryEngine) {\n\t\tif ( functionKind != FunctionKind.ORDERED_SET_AGGREGATE ) {\n\t\t\tthrow new UnsupportedOperationException( \"The function \" + getName() + \" is not an ordered set-aggregate function\" );\n\t\t}\n\t\treturn new SelfRenderingSqmOrderedSetAggregateFunction<>(\n\t\t\t\tthis,\n\t\t\t\tthis,\n\t\t\t\targuments,\n\t\t\t\tfilter,\n\t\t\t\twithinGroupClause,\n\t\t\t\timpliedResultType,\n\t\t\t\tgetArgumentsValidator(),\n\t\t\t\tgetReturnTypeResolver(),\n\t\t\t\tqueryEngine.getCriteriaBuilder(),\n\t\t\t\tgetName()\n\t\t);\n\t}\n\n\t@Override\n\tprotected <T> SelfRenderingSqmWindowFunction<T> generateSqmWindowFunctionExpression(\n\t\t\tList<? extends SqmTypedNode<?>> arguments,","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/function/AbstractSqmSelfRenderingFunctionDescriptor.java#L119-L155","documentation":"WITHIN GROUP (ORDER BY ...) syntax is only valid for ordered set-aggregate functions (percentile_cont, percentile_disc, mode, listagg...). When the query uses WITHIN GROUP on a function whose descriptor kind is not ORDERED_SET_AGGREGATE, AbstractSqmSelfRenderingFunctionDescriptor.generateSqmOrderedSetAggregateFunctionExpression throws UnsupportedOperationException naming the function.","triggerScenarios":"HQL like 'select someFunc(x) within group (order by y) from Entity e' where someFunc is a normal/aggregate/window function; custom ordered-set UDAFs registered without FunctionKind.ORDERED_SET_AGGREGATE; using an aggregate like sum() with WITHIN GROUP (its kind is AGGREGATE, not ORDERED_SET_AGGREGATE).","commonSituations":"Porting analytic SQL from a database where arbitrary aggregates accept WITHIN GROUP; registering custom Hive/PostgreSQL ordered-set functions in a dialect without setting the function kind; confusing plain aggregates with ordered-set aggregates.","solutions":["Use a function that actually is an ordered set-aggregate: percentile_cont, percentile_disc, mode, listagg/array_agg as registered by the dialect.","Register your custom function descriptor with FunctionKind.ORDERED_SET_AGGREGATE so WITHIN GROUP is accepted.","If WITHIN GROUP was accidental, drop it or rewrite the query with GROUP BY + ORDER BY."],"exampleFix":"// before\nList<Double> r = em.createQuery(\n    \"select sum(e.salary) within group (order by e.hiredAt) from Employee e\", Double.class) // UnsupportedOperationException\n    .getResultList();\n\n// after\nList<Double> r = em.createQuery(\n    \"select percentileDisc(0.5) within group (order by e.salary) from Employee e\", Double.class)\n    .getResultList();","handlingStrategy":"validation","validationCode":"var f = sessionFactory.getQueryEngine().getSqmFunctionRegistry().findFunction(\"my_func\");\nif (f == null || f.getFunctionKind() != org.hibernate.query.sqm.function.FunctionKind.ORDERED_SET_AGGREGATE) {\n    // 'my_func(...) within group (order by ...)' will fail — use percentile_cont/mode/listagg or register with ORDERED_SET_AGGREGATE\n}","typeGuard":null,"tryCatchPattern":"try { em.createQuery(ql, Double.class).getResultList(); } catch (UnsupportedOperationException e) { /* 'not an ordered set-aggregate function': switch to percentile_cont/percentile_disc/mode or fix registration kind */ throw e; }","preventionTips":["Register UDAFs usable with WITHIN GROUP with FunctionKind.ORDERED_SET_AGGREGATE.","Use the HQL function names exposed by Hibernate (percentileCont, percentileDisc, mode, listagg) rather than raw SQL strings."],"tags":["hibernate","hql","sqm","ordered-set-aggregate","within-group","custom-dialect"],"backgroundTag":"sql-function-kind-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}