@@ -257,18 +257,23 @@ public static <T> EntityValuedPathInterpretation<T> from(
257
257
SqmToSqlAstConverter sqlAstCreationState ) {
258
258
final boolean expandToAllColumns ;
259
259
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 ();
263
263
if ( currentClause == Clause .ORDER && !querySpec .groupByClauseContains ( navigablePath ) ) {
264
264
// We must ensure that the order by expression be expanded but only if the group by
265
265
// contained the same expression, and that was expanded as well
266
266
expandToAllColumns = false ;
267
267
}
268
268
else {
269
269
// 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
+ );
272
277
}
273
278
}
274
279
else {
@@ -342,38 +347,43 @@ public static <T> EntityValuedPathInterpretation<T> from(
342
347
);
343
348
}
344
349
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 ) ) {
357
362
return true ;
358
363
}
359
364
}
360
365
return false ;
361
366
}
362
367
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 () );
366
376
}
367
377
else if ( selection .isCompoundSelection () ) {
368
378
for ( Selection <?> compoundSelection : selection .getCompoundSelectionItems () ) {
369
- if ( selectionContains ( compoundSelection , path ) ) {
379
+ if ( selectionContains ( compoundSelection , path , tableGroupPath ) ) {
370
380
return true ;
371
381
}
372
382
}
373
383
}
374
384
else if ( selection instanceof SqmDynamicInstantiation ) {
375
385
for ( SqmDynamicInstantiationArgument <?> argument : ( (SqmDynamicInstantiation <?>) selection ).getArguments () ) {
376
- if ( selectionContains ( argument .getSelectableNode (), path ) ) {
386
+ if ( selectionContains ( argument .getSelectableNode (), path , tableGroupPath ) ) {
377
387
return true ;
378
388
}
379
389
}
0 commit comments