HHH-18862 Small cleanups to SqmUtil

This commit is contained in:
Marco Belladelli 2024-11-19 13:38:29 +01:00
parent 39f78c89fd
commit 817355f8a4
1 changed files with 10 additions and 20 deletions

View File

@ -173,11 +173,9 @@ public class SqmUtil {
if ( sqlAstCreationState.getCurrentClauseStack().getCurrent() != Clause.FROM if ( sqlAstCreationState.getCurrentClauseStack().getCurrent() != Clause.FROM
&& modelPartContainer.getPartMappingType() != modelPartContainer && modelPartContainer.getPartMappingType() != modelPartContainer
&& sqmPath.getLhs() instanceof SqmFrom<?, ?> ) { && sqmPath.getLhs() instanceof SqmFrom<?, ?> ) {
final ModelPart modelPart = final ModelPart modelPart = modelPartContainer instanceof PluralAttributeMapping plural ?
modelPartContainer instanceof PluralAttributeMapping pluralAttributeMapping getCollectionPart( plural, castNonNull( sqmPath.getNavigablePath().getParent() ) )
? getCollectionPart( pluralAttributeMapping, : modelPartContainer;
castNonNull( sqmPath.getNavigablePath().getParent() ) )
: modelPartContainer;
if ( modelPart instanceof EntityAssociationMapping association ) { if ( modelPart instanceof EntityAssociationMapping association ) {
if ( shouldRenderTargetSide( sqmPath, association, sqlAstCreationState ) ) { if ( shouldRenderTargetSide( sqmPath, association, sqlAstCreationState ) ) {
return association.getAssociatedEntityMappingType(); return association.getAssociatedEntityMappingType();
@ -200,26 +198,18 @@ public class SqmUtil {
final Clause clause = sqlAstCreationState.getCurrentClauseStack().getCurrent(); final Clause clause = sqlAstCreationState.getCurrentClauseStack().getCurrent();
return clause == Clause.GROUP || clause == Clause.ORDER return clause == Clause.GROUP || clause == Clause.ORDER
|| !isFkOptimizationAllowed( sqmPath.getLhs(), association ) || !isFkOptimizationAllowed( sqmPath.getLhs(), association )
|| clauseContainsPath( Clause.GROUP, sqmPath, sqlAstCreationState ) || inGroupByOrOrderBy( sqmPath, sqlAstCreationState );
|| clauseContainsPath( Clause.ORDER, sqmPath, sqlAstCreationState );
} }
} }
private static boolean clauseContainsPath( private static boolean inGroupByOrOrderBy(SqmPath<?> sqmPath, SqmToSqlAstConverter converter) {
Clause clauseToCheck, final Stack<SqmQueryPart> queryPartStack = converter.getSqmQueryPartStack();
SqmPath<?> sqmPath, final NavigablePath np = sqmPath.getNavigablePath();
SqmToSqlAstConverter sqlAstCreationState) {
final Stack<SqmQueryPart> queryPartStack = sqlAstCreationState.getSqmQueryPartStack();
final NavigablePath navigablePath = sqmPath.getNavigablePath();
final Boolean found = queryPartStack.findCurrentFirst( queryPart -> { final Boolean found = queryPartStack.findCurrentFirst( queryPart -> {
final SqmQuerySpec<?> spec = queryPart.getFirstQuerySpec(); final SqmQuerySpec<?> spec = queryPart.getFirstQuerySpec();
if ( clauseToCheck == Clause.GROUP && spec.groupByClauseContains( navigablePath, sqlAstCreationState ) return spec.groupByClauseContains( np, converter ) || spec.orderByClauseContains( np, converter ) ?
|| clauseToCheck == Clause.ORDER && spec.orderByClauseContains( navigablePath, sqlAstCreationState ) ) { true :
return true; null;
}
else {
return null;
}
} ); } );
return Boolean.TRUE.equals( found ); return Boolean.TRUE.equals( found );
} }