HHH-18121 Replace dynamic instantiations for non-simple count queries

This commit is contained in:
Marco Belladelli 2024-05-30 12:05:45 +02:00
parent 3845df932b
commit 81f9818af4
1 changed files with 27 additions and 10 deletions

View File

@ -483,16 +483,7 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
return (SqmSelectStatement<Long>) copy;
}
else {
final JpaSelection<?> selection = queryPart.getFirstQuerySpec().getSelection();
if ( selection.isCompoundSelection() ) {
char c = 'a';
for ( JpaSelection<?> item : selection.getSelectionItems() ) {
item.alias( Character.toString( ++c ) + '_' );
}
}
else {
selection.alias( "a_" );
}
aliasSelections( queryPart );
final SqmSubQuery<?> subquery = new SqmSubQuery<>( copy, queryPart, null, nodeBuilder() );
final SqmSelectStatement<Long> query = nodeBuilder().createQuery( Long.class );
query.from( subquery );
@ -500,4 +491,30 @@ public class SqmSelectStatement<T> extends AbstractSqmSelectQuery<T> implements
return query;
}
}
private <S> void aliasSelections(SqmQueryPart<S> queryPart) {
if ( queryPart.isSimpleQueryPart() ) {
final SqmQuerySpec<S> querySpec = queryPart.getFirstQuerySpec();
final LinkedHashSet<JpaSelection<?>> newSelections = new LinkedHashSet<>();
aliasSelection( querySpec.getSelection(), newSelections );
//noinspection unchecked
querySpec.setSelection( (JpaSelection<S>) ( newSelections.size() == 1 ?
newSelections.iterator().next() :
nodeBuilder().tuple( newSelections.toArray( new JpaSelection<?>[0] ) ) ) );
}
else {
( (SqmQueryGroup<?>) queryPart ).getQueryParts().forEach( this::aliasSelections );
}
}
private void aliasSelection(JpaSelection<?> selection, LinkedHashSet<JpaSelection<?>> newSelections) {
if ( selection.isCompoundSelection() || selection instanceof SqmDynamicInstantiation<?> ) {
for ( JpaSelection<?> selectionItem : selection.getSelectionItems() ) {
aliasSelection( selectionItem, newSelections );
}
}
else {
newSelections.add( selection.alias( "c" + newSelections.size() ) );
}
}
}