hibernate/hibernate-orm · error · UnsupportedOperationException

The function {name} is not an ordered set-aggregate function

Error message

The function {name} is not an ordered set-aggregate function

What it means

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.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/query/sqm/function/AbstractSqmSelfRenderingFunctionDescriptor.java:137

				arguments,
				filter,
				impliedResultType,
				getArgumentsValidator(),
				getReturnTypeResolver(),
				queryEngine.getCriteriaBuilder(),
				getName()
		);
	}

	@Override
	public <T> SelfRenderingSqmOrderedSetAggregateFunction<T> generateSqmOrderedSetAggregateFunctionExpression(
			List<? extends SqmTypedNode<?>> arguments,
			SqmPredicate filter,
			SqmOrderByClause withinGroupClause,
			ReturnableType<T> impliedResultType,
			QueryEngine queryEngine) {
		if ( functionKind != FunctionKind.ORDERED_SET_AGGREGATE ) {
			throw new UnsupportedOperationException( "The function " + getName() + " is not an ordered set-aggregate function" );
		}
		return new SelfRenderingSqmOrderedSetAggregateFunction<>(
				this,
				this,
				arguments,
				filter,
				withinGroupClause,
				impliedResultType,
				getArgumentsValidator(),
				getReturnTypeResolver(),
				queryEngine.getCriteriaBuilder(),
				getName()
		);
	}

	@Override
	protected <T> SelfRenderingSqmWindowFunction<T> generateSqmWindowFunctionExpression(
			List<? extends SqmTypedNode<?>> arguments,

View on GitHub (pinned to fad1729dce)

Solutions

  1. Use a function that actually is an ordered set-aggregate: percentile_cont, percentile_disc, mode, listagg/array_agg as registered by the dialect.
  2. Register your custom function descriptor with FunctionKind.ORDERED_SET_AGGREGATE so WITHIN GROUP is accepted.
  3. If WITHIN GROUP was accidental, drop it or rewrite the query with GROUP BY + ORDER BY.

Example fix

// before
List<Double> r = em.createQuery(
    "select sum(e.salary) within group (order by e.hiredAt) from Employee e", Double.class) // UnsupportedOperationException
    .getResultList();

// after
List<Double> r = em.createQuery(
    "select percentileDisc(0.5) within group (order by e.salary) from Employee e", Double.class)
    .getResultList();
Defensive patterns

Strategy: validation

Validate before calling

var f = sessionFactory.getQueryEngine().getSqmFunctionRegistry().findFunction("my_func");
if (f == null || f.getFunctionKind() != org.hibernate.query.sqm.function.FunctionKind.ORDERED_SET_AGGREGATE) {
    // 'my_func(...) within group (order by ...)' will fail — use percentile_cont/mode/listagg or register with ORDERED_SET_AGGREGATE
}

Try / catch

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; }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/ca47fc45f5ccc494. Report an issue: GitHub.