HHH-18272 Check for `SqmAliasedNodeRef` when collecting group/order by

This commit is contained in:
Marco Belladelli 2024-06-28 11:17:44 +02:00 committed by Christian Beikov
parent 5e4c35441c
commit c972208fbc
1 changed files with 21 additions and 10 deletions

View File

@ -80,6 +80,7 @@ import org.hibernate.type.internal.BasicTypeImpl;
import org.hibernate.type.internal.ConvertedBasicTypeImpl;
import org.hibernate.type.spi.TypeConfiguration;
import static java.util.stream.Collectors.toList;
import static org.hibernate.internal.util.NullnessUtil.castNonNull;
import static org.hibernate.query.sqm.tree.jpa.ParameterCollector.collectParameters;
@ -214,12 +215,7 @@ public class SqmUtil {
return Collections.emptyList();
}
final List<NavigablePath> navigablePaths = new ArrayList<>( expressions.size() );
final SqmPathVisitor pathVisitor = new SqmPathVisitor( path -> navigablePaths.add( path.getNavigablePath() ) );
for ( SqmExpression<?> expression : expressions ) {
expression.accept( pathVisitor );
}
return navigablePaths;
return collectNavigablePaths( expressions );
}
public static List<NavigablePath> getOrderByNavigablePaths(SqmQuerySpec<?> querySpec) {
@ -228,11 +224,26 @@ public class SqmUtil {
return Collections.emptyList();
}
final List<SqmSortSpecification> sortSpecifications = order.getSortSpecifications();
final List<NavigablePath> navigablePaths = new ArrayList<>( sortSpecifications.size() );
final List<SqmExpression<?>> expressions = order.getSortSpecifications()
.stream()
.map( SqmSortSpecification::getSortExpression )
.collect( toList() );
return collectNavigablePaths( expressions );
}
private static List<NavigablePath> collectNavigablePaths(final List<SqmExpression<?>> expressions) {
final List<NavigablePath> navigablePaths = new ArrayList<>( expressions.size() );
final SqmPathVisitor pathVisitor = new SqmPathVisitor( path -> navigablePaths.add( path.getNavigablePath() ) );
for ( SqmSortSpecification sortSpec : sortSpecifications ) {
sortSpec.getSortExpression().accept( pathVisitor );
for ( final SqmExpression<?> expression : expressions ) {
if ( expression instanceof SqmAliasedNodeRef ) {
final NavigablePath navigablePath = ( (SqmAliasedNodeRef) expression ).getNavigablePath();
if ( navigablePath != null ) {
navigablePaths.add( navigablePath );
}
}
else {
expression.accept( pathVisitor );
}
}
return navigablePaths;
}