@@ -257,18 +257,23 @@ public static <T> EntityValuedPathInterpretation<T> from(
257257 SqmToSqlAstConverter sqlAstCreationState ) {
258258 final boolean expandToAllColumns ;
259259 final Clause currentClause = sqlAstCreationState .getCurrentClauseStack ().getCurrent ();
260- if ( sqlAstCreationState . getCurrentProcessingState (). isTopLevel () &&
261- ( currentClause == Clause . GROUP || currentClause == Clause . ORDER ) ) {
262- final SqmQuerySpec <?> querySpec = ( SqmQuerySpec <?>) sqlAstCreationState .getCurrentSqmQueryPart ();
260+ if ( currentClause == Clause . GROUP || currentClause == Clause . ORDER ) {
261+ assert sqlAstCreationState . getCurrentSqmQueryPart (). isSimpleQueryPart ();
262+ final SqmQuerySpec <?> querySpec = sqlAstCreationState .getCurrentSqmQueryPart (). getFirstQuerySpec ();
263263 if ( currentClause == Clause .ORDER && !querySpec .groupByClauseContains ( navigablePath ) ) {
264264 // We must ensure that the order by expression be expanded but only if the group by
265265 // contained the same expression, and that was expanded as well
266266 expandToAllColumns = false ;
267267 }
268268 else {
269269 // When the table group is selected and the navigablePath is selected we need to expand
270- // to all columns, as we also expand this to all columns in the select clause
271- expandToAllColumns = isSelected ( tableGroup , navigablePath , querySpec );
270+ // to all columns, as we must make sure we include all columns present in the select clause
271+ expandToAllColumns = isSelected (
272+ tableGroup ,
273+ navigablePath ,
274+ querySpec ,
275+ sqlAstCreationState .getCurrentProcessingState ().isTopLevel ()
276+ );
272277 }
273278 }
274279 else {
@@ -342,38 +347,43 @@ public static <T> EntityValuedPathInterpretation<T> from(
342347 );
343348 }
344349
345- private static boolean isSelected (TableGroup tableGroup , NavigablePath path , SqmQuerySpec <?> sqmQuerySpec ) {
346- // If the table group is selected (initialized), check if the entity valued
347- // navigable path or any child path appears in the select clause
348- return tableGroup . isInitialized () && selectClauseContains ( path , sqmQuerySpec );
349- }
350-
351- private static boolean selectClauseContains ( NavigablePath path , SqmQuerySpec <?> sqmQuerySpec ) {
352- final List < SqmSelection <?>> selections = sqmQuerySpec . getSelectClause () == null
353- ? Collections . emptyList ()
354- : sqmQuerySpec . getSelectClause (). getSelections ();
355- for ( SqmSelection <?> selection : selections ) {
356- if ( selectionContains ( selection .getSelectableNode (), path ) ) {
350+ private static boolean isSelected (
351+ TableGroup tableGroup ,
352+ NavigablePath path ,
353+ SqmQuerySpec <?> sqmQuerySpec ,
354+ boolean isTopLevel ) {
355+ // If the table group is not initialized, i.e. not selected, no need to check selections
356+ if ( ! tableGroup . isInitialized () || sqmQuerySpec . getSelectClause () == null ) {
357+ return false ;
358+ }
359+ final NavigablePath tableGroupPath = isTopLevel ? null : tableGroup . getNavigablePath ();
360+ for ( SqmSelection <?> selection : sqmQuerySpec . getSelectClause (). getSelections () ) {
361+ if ( selectionContains ( selection .getSelectableNode (), path , tableGroupPath ) ) {
357362 return true ;
358363 }
359364 }
360365 return false ;
361366 }
362367
363- private static boolean selectionContains (Selection <?> selection , NavigablePath path ) {
364- if ( selection instanceof SqmPath && path .isParentOrEqual ( ( (SqmPath <?>) selection ).getNavigablePath () ) ) {
365- return true ;
368+ private static boolean selectionContains (Selection <?> selection , NavigablePath path , NavigablePath tableGroupPath ) {
369+ if ( selection instanceof SqmPath <?> ) {
370+ final SqmPath <?> sqmPath = (SqmPath <?>) selection ;
371+ // Expansion is needed if the table group is null, i.e. we're in a top level query where EVPs are always
372+ // expanded to all columns, or if the selection is on the same table (lhs) as the group by expression ...
373+ return ( tableGroupPath == null || sqmPath .getLhs ().getNavigablePath ().equals ( tableGroupPath ) )
374+ // ... and if the entity valued path is selected or any of its columns are
375+ && path .isParentOrEqual ( sqmPath .getNavigablePath () );
366376 }
367377 else if ( selection .isCompoundSelection () ) {
368378 for ( Selection <?> compoundSelection : selection .getCompoundSelectionItems () ) {
369- if ( selectionContains ( compoundSelection , path ) ) {
379+ if ( selectionContains ( compoundSelection , path , tableGroupPath ) ) {
370380 return true ;
371381 }
372382 }
373383 }
374384 else if ( selection instanceof SqmDynamicInstantiation ) {
375385 for ( SqmDynamicInstantiationArgument <?> argument : ( (SqmDynamicInstantiation <?>) selection ).getArguments () ) {
376- if ( selectionContains ( argument .getSelectableNode (), path ) ) {
386+ if ( selectionContains ( argument .getSelectableNode (), path , tableGroupPath ) ) {
377387 return true ;
378388 }
379389 }
0 commit comments