hibernate/hibernate-orm · error · UnsupportedOperationException

Can't emulate lateral join for query spec with group by clau

Error message

Can't emulate lateral join for query spec with group by clause

What it means

LATERAL emulation on non-supporting dialects works by stripping the lateral query down to its select clause and inlining it into the outer query (stripToSelectClause). A GROUP BY clause inside the lateral query spec cannot be stripped this way - the grouping semantics would be lost - so this UnsupportedOperationException is raised.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/sql/ast/spi/AbstractSqlAstTranslator.java:6953

			return stripToSelectClause( querySpec );
		}
		else {
			throw new AssertionFailure( "Unexpected query part" );
		}
	}

	private QueryGroup stripToSelectClause(QueryGroup queryGroup) {
		final List<QueryPart> parts = new ArrayList<>( queryGroup.getQueryParts().size() );
		for ( QueryPart queryPart : queryGroup.getQueryParts() ) {
			parts.add( stripToSelectClause( queryPart ) );
		}
		return new QueryGroup( queryGroup.isRoot(), queryGroup.getSetOperator(), parts );
	}

	private QuerySpec stripToSelectClause(QuerySpec querySpec) {
		final var groupByExpressions = querySpec.getGroupByClauseExpressions();
		if ( groupByExpressions != null && !groupByExpressions.isEmpty() ) {
			throw new UnsupportedOperationException( "Can't emulate lateral join for query spec with group by clause" );
		}
		final Predicate havingRestrictions = querySpec.getHavingClauseRestrictions();
		if ( havingRestrictions != null && !havingRestrictions.isEmpty() ) {
			throw new UnsupportedOperationException( "Can't emulate lateral join for query spec with having clause" );
		}
		final var roots = querySpec.getFromClause().getRoots();
		final QuerySpec newQuerySpec = new QuerySpec( querySpec.isRoot(), roots.size() );
		for ( TableGroup root : roots ) {
			newQuerySpec.getFromClause().addRoot( root );
		}
		final SelectClause selectClause = querySpec.getSelectClause();
		for ( SqlSelection selection : selectClause.getSqlSelections() ) {
			if ( AggregateFunctionChecker.hasAggregateFunctions( selection.getExpression() ) ) {
				throw new UnsupportedOperationException( "Can't emulate lateral join for query spec with aggregate function" );
			}
			newQuerySpec.getSelectClause().addSqlSelection( selection );
		}
		return newQuerySpec;

View on GitHub (pinned to fad1729dce)

Solutions

  1. Remove the group by from the lateral part or replace grouping with a subselect
  2. Restructure the query so the grouped query is the outer query, not the lateral side
  3. Use a database/dialect with native LATERAL support
  4. Fall back to a native SQL query with the database's lateral syntax

Example fix

// before (no-lateral dialect)
List<Dept> ds = session.createQuery(
    "select d from Dept d join lateral (select e.dept, count(e) c from Emp e where e.dept=d group by e.dept) s on 1=1").list();

// after
List<Dept> ds = session.createQuery(
    "select d from Dept d where d.id in (select e.dept.id from Emp e group by e.dept.id)").list();
Defensive patterns

Strategy: fallback

Validate before calling

// If the dialect lacks lateral support, forbid group by in correlated subquery parts
if (!dialect.supportsLateral() && lateralPartHasGroupBy(sq)) {
    // restructure: aggregate outside or via scalar subquery
}

Try / catch

try {
    query.list();
} catch (UnsupportedOperationException e) {
    if (String.valueOf(e.getMessage()).contains("lateral join for query spec with group by")) {
        // reroute to native SQL with the DB's lateral/grouping semantics
    } else throw e;
}

Prevention

When it happens

Trigger: An HQL query whose lateral part (implicit lateral collection join or explicit lateral subquery) contains a group by clause, executed on a dialect without native LATERAL support (older MySQL, SQL Server variants, DB2 versions without lateral).

Common situations: Joining entity collections with aggregate grouping in the subquery; Hibernate 6.x array/collection function joins that imply lateral; migrating queries from PostgreSQL; tests running on H2/MySQL in CI while production uses PostgreSQL.

Related errors


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