@@ -488,7 +488,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
488488 private boolean deduplicateSelectionItems ;
489489 private ForeignKeyDescriptor .Nature currentlyResolvingForeignKeySide ;
490490 private SqmStatement <?> currentSqmStatement ;
491- private SqmQueryPart <?> currentSqmQueryPart ;
491+ private Stack < SqmQueryPart > sqmQueryPartStack = new StandardStack <>( SqmQueryPart . class ) ;
492492 private CteContainer cteContainer ;
493493 /**
494494 * A map from {@link SqmCteTable#getCteName()} to the final SQL name.
@@ -792,9 +792,10 @@ public Stack<Clause> getCurrentClauseStack() {
792792 return currentClauseStack ;
793793 }
794794
795+ @ SuppressWarnings ("rawtypes" )
795796 @ Override
796- public SqmQueryPart <?> getCurrentSqmQueryPart () {
797- return currentSqmQueryPart ;
797+ public Stack < SqmQueryPart > getSqmQueryPartStack () {
798+ return sqmQueryPartStack ;
798799 }
799800
800801 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1726,8 +1727,7 @@ public CteStatement visitCteStatement(SqmCteStatement<?> sqmCteStatement) {
17261727 );
17271728 final DelegatingSqmAliasedNodeCollector collector =
17281729 (DelegatingSqmAliasedNodeCollector ) processingState .getSqlExpressionResolver ();
1729- final SqmQueryPart <?> oldSqmQueryPart = currentSqmQueryPart ;
1730- currentSqmQueryPart = queryGroup ;
1730+ sqmQueryPartStack .push ( queryGroup );
17311731 pushProcessingState ( processingState );
17321732
17331733 try {
@@ -1771,7 +1771,7 @@ public CteStatement visitCteStatement(SqmCteStatement<?> sqmCteStatement) {
17711771 }
17721772 finally {
17731773 popProcessingStateStack ();
1774- currentSqmQueryPart = oldSqmQueryPart ;
1774+ sqmQueryPartStack . pop () ;
17751775 }
17761776 }
17771777 finally {
@@ -1985,8 +1985,7 @@ public QueryGroup visitQueryGroup(SqmQueryGroup<?> queryGroup) {
19851985 );
19861986 final DelegatingSqmAliasedNodeCollector collector = (DelegatingSqmAliasedNodeCollector ) processingState
19871987 .getSqlExpressionResolver ();
1988- final SqmQueryPart <?> sqmQueryPart = currentSqmQueryPart ;
1989- currentSqmQueryPart = queryGroup ;
1988+ sqmQueryPartStack .push ( queryGroup );
19901989 pushProcessingState ( processingState );
19911990
19921991 try {
@@ -2008,7 +2007,7 @@ public QueryGroup visitQueryGroup(SqmQueryGroup<?> queryGroup) {
20082007 }
20092008 finally {
20102009 popProcessingStateStack ();
2011- currentSqmQueryPart = sqmQueryPart ;
2010+ sqmQueryPartStack . pop () ;
20122011 }
20132012 }
20142013
@@ -2043,9 +2042,8 @@ public QuerySpec visitQuerySpec(SqmQuerySpec<?> sqmQuerySpec) {
20432042 );
20442043 }
20452044
2046- final SqmQueryPart <?> sqmQueryPart = currentSqmQueryPart ;
20472045 final boolean originalDeduplicateSelectionItems = deduplicateSelectionItems ;
2048- currentSqmQueryPart = sqmQuerySpec ;
2046+ sqmQueryPartStack . push ( sqmQuerySpec ) ;
20492047 // In sub-queries, we can never deduplicate the selection items as that might change semantics
20502048 deduplicateSelectionItems = false ;
20512049 pushProcessingState ( processingState );
@@ -2062,7 +2060,7 @@ public QuerySpec visitQuerySpec(SqmQuerySpec<?> sqmQuerySpec) {
20622060 inNestedContext = oldInNestedContext ;
20632061 popProcessingStateStack ();
20642062 queryTransformers .pop ();
2065- currentSqmQueryPart = sqmQueryPart ;
2063+ sqmQueryPartStack . pop () ;
20662064 deduplicateSelectionItems = originalDeduplicateSelectionItems ;
20672065 }
20682066 }
@@ -2203,7 +2201,7 @@ public SelectClause visitSelectClause(SqmSelectClause selectClause) {
22032201 try {
22042202 final SelectClause sqlSelectClause = currentQuerySpec ().getSelectClause ();
22052203 if ( selectClause == null ) {
2206- final SqmFrom <?, ?> implicitSelection = determineImplicitSelection ( (SqmQuerySpec <?>) currentSqmQueryPart );
2204+ final SqmFrom <?, ?> implicitSelection = determineImplicitSelection ( (SqmQuerySpec <?>) getCurrentSqmQueryPart () );
22072205 visitSelection ( 0 , new SqmSelection <>( implicitSelection , implicitSelection .nodeBuilder () ) );
22082206 }
22092207 else {
@@ -2228,7 +2226,7 @@ public SelectClause visitSelectClause(SqmSelectClause selectClause) {
22282226 @ Override
22292227 public Void visitSelection (SqmSelection <?> sqmSelection ) {
22302228 visitSelection (
2231- currentSqmQueryPart .getFirstQuerySpec ().getSelectClause ().getSelections ().indexOf ( sqmSelection ),
2229+ getCurrentSqmQueryPart () .getFirstQuerySpec ().getSelectClause ().getSelections ().indexOf ( sqmSelection ),
22322230 sqmSelection
22332231 );
22342232 return null ;
@@ -2353,7 +2351,7 @@ else if ( statement.getQuerySource() == SqmQuerySource.CRITERIA && currentClause
23532351 // To avoid this issue, we determine the position and let the SqlAstTranslator handle the rest.
23542352 // Usually it will render `select ?, count(*) from dual group by 1` if supported
23552353 // or force rendering the parameter as literal instead so that the database can see the grouping is fine
2356- final SqmQuerySpec <?> querySpec = currentSqmQueryPart .getFirstQuerySpec ();
2354+ final SqmQuerySpec <?> querySpec = getCurrentSqmQueryPart () .getFirstQuerySpec ();
23572355 sqmPosition = indexOfExpression ( querySpec .getSelectClause ().getSelections (), groupByClauseExpression );
23582356 path = null ;
23592357 }
@@ -2395,7 +2393,7 @@ private SqlSelectionExpression selectionExpression(List<SqlSelection> selections
23952393 return null ;
23962394 }
23972395 }
2398- return currentSqmQueryPart instanceof SqmQueryGroup <?>
2396+ return getCurrentSqmQueryPart () instanceof SqmQueryGroup <?>
23992397 // Reusing the SqlSelection for query groups would be wrong because the aliases do no exist
24002398 // So we have to use a literal expression in a new SqlSelection instance to refer to the position
24012399 ? sqlSelectionExpression ( selection )
0 commit comments