@@ -478,7 +478,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
478478 private boolean deduplicateSelectionItems ;
479479 private ForeignKeyDescriptor .Nature currentlyResolvingForeignKeySide ;
480480 private SqmStatement <?> currentSqmStatement ;
481- private SqmQueryPart <?> currentSqmQueryPart ;
481+ private Stack < SqmQueryPart > sqmQueryPartStack = new StandardStack <>( SqmQueryPart . class ) ;
482482 private CteContainer cteContainer ;
483483 /**
484484 * A map from {@link SqmCteTable#getCteName()} to the final SQL name.
@@ -784,9 +784,10 @@ public Stack<Clause> getCurrentClauseStack() {
784784 return currentClauseStack ;
785785 }
786786
787+ @ SuppressWarnings ("rawtypes" )
787788 @ Override
788- public SqmQueryPart <?> getCurrentSqmQueryPart () {
789- return currentSqmQueryPart ;
789+ public Stack < SqmQueryPart > getSqmQueryPartStack () {
790+ return sqmQueryPartStack ;
790791 }
791792
792793 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1735,8 +1736,7 @@ public CteStatement visitCteStatement(SqmCteStatement<?> sqmCteStatement) {
17351736 );
17361737 final DelegatingSqmAliasedNodeCollector collector = (DelegatingSqmAliasedNodeCollector ) processingState
17371738 .getSqlExpressionResolver ();
1738- final SqmQueryPart <?> oldSqmQueryPart = currentSqmQueryPart ;
1739- currentSqmQueryPart = queryGroup ;
1739+ sqmQueryPartStack .push ( queryGroup );
17401740 pushProcessingState ( processingState );
17411741
17421742 try {
@@ -1780,7 +1780,7 @@ public CteStatement visitCteStatement(SqmCteStatement<?> sqmCteStatement) {
17801780 }
17811781 finally {
17821782 popProcessingStateStack ();
1783- currentSqmQueryPart = oldSqmQueryPart ;
1783+ sqmQueryPartStack . pop () ;
17841784 }
17851785 }
17861786 finally {
@@ -1993,8 +1993,7 @@ public QueryGroup visitQueryGroup(SqmQueryGroup<?> queryGroup) {
19931993 );
19941994 final DelegatingSqmAliasedNodeCollector collector = (DelegatingSqmAliasedNodeCollector ) processingState
19951995 .getSqlExpressionResolver ();
1996- final SqmQueryPart <?> sqmQueryPart = currentSqmQueryPart ;
1997- currentSqmQueryPart = queryGroup ;
1996+ sqmQueryPartStack .push ( queryGroup );
19981997 pushProcessingState ( processingState );
19991998
20001999 try {
@@ -2016,7 +2015,7 @@ public QueryGroup visitQueryGroup(SqmQueryGroup<?> queryGroup) {
20162015 }
20172016 finally {
20182017 popProcessingStateStack ();
2019- currentSqmQueryPart = sqmQueryPart ;
2018+ sqmQueryPartStack . pop () ;
20202019 }
20212020 }
20222021
@@ -2076,9 +2075,8 @@ else if ( sqmQuerySpec.hasPositionalGroupItem() ) {
20762075 );
20772076 }
20782077
2079- final SqmQueryPart <?> sqmQueryPart = currentSqmQueryPart ;
20802078 final boolean originalDeduplicateSelectionItems = deduplicateSelectionItems ;
2081- currentSqmQueryPart = sqmQuerySpec ;
2079+ sqmQueryPartStack . push ( sqmQuerySpec ) ;
20822080 // In sub-queries, we can never deduplicate the selection items as that might change semantics
20832081 deduplicateSelectionItems = false ;
20842082 pushProcessingState ( processingState );
@@ -2145,7 +2143,7 @@ else if ( sqmQuerySpec.hasPositionalGroupItem() ) {
21452143 inNestedContext = oldInNestedContext ;
21462144 popProcessingStateStack ();
21472145 queryTransformers .pop ();
2148- currentSqmQueryPart = sqmQueryPart ;
2146+ sqmQueryPartStack . pop () ;
21492147 deduplicateSelectionItems = originalDeduplicateSelectionItems ;
21502148 }
21512149 }
@@ -2211,7 +2209,7 @@ public SelectClause visitSelectClause(SqmSelectClause selectClause) {
22112209 try {
22122210 final SelectClause sqlSelectClause = currentQuerySpec ().getSelectClause ();
22132211 if ( selectClause == null ) {
2214- final SqmFrom <?, ?> implicitSelection = determineImplicitSelection ( (SqmQuerySpec <?>) currentSqmQueryPart );
2212+ final SqmFrom <?, ?> implicitSelection = determineImplicitSelection ( (SqmQuerySpec <?>) getCurrentSqmQueryPart () );
22152213 visitSelection ( 0 , new SqmSelection <>( implicitSelection , implicitSelection .nodeBuilder () ) );
22162214 }
22172215 else {
@@ -2236,7 +2234,7 @@ public SelectClause visitSelectClause(SqmSelectClause selectClause) {
22362234 @ Override
22372235 public Void visitSelection (SqmSelection <?> sqmSelection ) {
22382236 return visitSelection (
2239- currentSqmQueryPart .getFirstQuerySpec ().getSelectClause ().getSelections ().indexOf ( sqmSelection ),
2237+ getCurrentSqmQueryPart () .getFirstQuerySpec ().getSelectClause ().getSelections ().indexOf ( sqmSelection ),
22402238 sqmSelection
22412239 );
22422240 }
@@ -2355,7 +2353,7 @@ else if ( statement.getQuerySource() == SqmQuerySource.CRITERIA && currentClause
23552353 // To avoid this issue, we determine the position and let the SqlAstTranslator handle the rest.
23562354 // Usually it will render `select ?, count(*) from dual group by 1` if supported
23572355 // or force rendering the parameter as literal instead so that the database can see the grouping is fine
2358- final SqmQuerySpec <?> querySpec = currentSqmQueryPart .getFirstQuerySpec ();
2356+ final SqmQuerySpec <?> querySpec = getCurrentSqmQueryPart () .getFirstQuerySpec ();
23592357 sqmPosition = indexOfExpression ( querySpec .getSelectClause ().getSelections (), groupByClauseExpression );
23602358 path = null ;
23612359 }
@@ -2383,7 +2381,7 @@ else if ( statement.getQuerySource() == SqmQuerySource.CRITERIA && currentClause
23832381 continue OUTER ;
23842382 }
23852383 }
2386- if ( currentSqmQueryPart instanceof SqmQueryGroup <?> ) {
2384+ if ( getCurrentSqmQueryPart () instanceof SqmQueryGroup <?> ) {
23872385 // Reusing the SqlSelection for query groups would be wrong because the aliases do no exist
23882386 // So we have to use a literal expression in a new SqlSelection instance to refer to the position
23892387 expressions .add (
0 commit comments