@@ -478,7 +478,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
478
478
private boolean deduplicateSelectionItems ;
479
479
private ForeignKeyDescriptor .Nature currentlyResolvingForeignKeySide ;
480
480
private SqmStatement <?> currentSqmStatement ;
481
- private SqmQueryPart <?> currentSqmQueryPart ;
481
+ private Stack < SqmQueryPart > sqmQueryPartStack = new StandardStack <>( SqmQueryPart . class ) ;
482
482
private CteContainer cteContainer ;
483
483
/**
484
484
* A map from {@link SqmCteTable#getCteName()} to the final SQL name.
@@ -784,9 +784,10 @@ public Stack<Clause> getCurrentClauseStack() {
784
784
return currentClauseStack ;
785
785
}
786
786
787
+ @ SuppressWarnings ("rawtypes" )
787
788
@ Override
788
- public SqmQueryPart <?> getCurrentSqmQueryPart () {
789
- return currentSqmQueryPart ;
789
+ public Stack < SqmQueryPart > getSqmQueryPartStack () {
790
+ return sqmQueryPartStack ;
790
791
}
791
792
792
793
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1735,8 +1736,7 @@ public CteStatement visitCteStatement(SqmCteStatement<?> sqmCteStatement) {
1735
1736
);
1736
1737
final DelegatingSqmAliasedNodeCollector collector = (DelegatingSqmAliasedNodeCollector ) processingState
1737
1738
.getSqlExpressionResolver ();
1738
- final SqmQueryPart <?> oldSqmQueryPart = currentSqmQueryPart ;
1739
- currentSqmQueryPart = queryGroup ;
1739
+ sqmQueryPartStack .push ( queryGroup );
1740
1740
pushProcessingState ( processingState );
1741
1741
1742
1742
try {
@@ -1780,7 +1780,7 @@ public CteStatement visitCteStatement(SqmCteStatement<?> sqmCteStatement) {
1780
1780
}
1781
1781
finally {
1782
1782
popProcessingStateStack ();
1783
- currentSqmQueryPart = oldSqmQueryPart ;
1783
+ sqmQueryPartStack . pop () ;
1784
1784
}
1785
1785
}
1786
1786
finally {
@@ -1993,8 +1993,7 @@ public QueryGroup visitQueryGroup(SqmQueryGroup<?> queryGroup) {
1993
1993
);
1994
1994
final DelegatingSqmAliasedNodeCollector collector = (DelegatingSqmAliasedNodeCollector ) processingState
1995
1995
.getSqlExpressionResolver ();
1996
- final SqmQueryPart <?> sqmQueryPart = currentSqmQueryPart ;
1997
- currentSqmQueryPart = queryGroup ;
1996
+ sqmQueryPartStack .push ( queryGroup );
1998
1997
pushProcessingState ( processingState );
1999
1998
2000
1999
try {
@@ -2016,7 +2015,7 @@ public QueryGroup visitQueryGroup(SqmQueryGroup<?> queryGroup) {
2016
2015
}
2017
2016
finally {
2018
2017
popProcessingStateStack ();
2019
- currentSqmQueryPart = sqmQueryPart ;
2018
+ sqmQueryPartStack . pop () ;
2020
2019
}
2021
2020
}
2022
2021
@@ -2076,9 +2075,8 @@ else if ( sqmQuerySpec.hasPositionalGroupItem() ) {
2076
2075
);
2077
2076
}
2078
2077
2079
- final SqmQueryPart <?> sqmQueryPart = currentSqmQueryPart ;
2080
2078
final boolean originalDeduplicateSelectionItems = deduplicateSelectionItems ;
2081
- currentSqmQueryPart = sqmQuerySpec ;
2079
+ sqmQueryPartStack . push ( sqmQuerySpec ) ;
2082
2080
// In sub-queries, we can never deduplicate the selection items as that might change semantics
2083
2081
deduplicateSelectionItems = false ;
2084
2082
pushProcessingState ( processingState );
@@ -2145,7 +2143,7 @@ else if ( sqmQuerySpec.hasPositionalGroupItem() ) {
2145
2143
inNestedContext = oldInNestedContext ;
2146
2144
popProcessingStateStack ();
2147
2145
queryTransformers .pop ();
2148
- currentSqmQueryPart = sqmQueryPart ;
2146
+ sqmQueryPartStack . pop () ;
2149
2147
deduplicateSelectionItems = originalDeduplicateSelectionItems ;
2150
2148
}
2151
2149
}
@@ -2211,7 +2209,7 @@ public SelectClause visitSelectClause(SqmSelectClause selectClause) {
2211
2209
try {
2212
2210
final SelectClause sqlSelectClause = currentQuerySpec ().getSelectClause ();
2213
2211
if ( selectClause == null ) {
2214
- final SqmFrom <?, ?> implicitSelection = determineImplicitSelection ( (SqmQuerySpec <?>) currentSqmQueryPart );
2212
+ final SqmFrom <?, ?> implicitSelection = determineImplicitSelection ( (SqmQuerySpec <?>) getCurrentSqmQueryPart () );
2215
2213
visitSelection ( 0 , new SqmSelection <>( implicitSelection , implicitSelection .nodeBuilder () ) );
2216
2214
}
2217
2215
else {
@@ -2236,7 +2234,7 @@ public SelectClause visitSelectClause(SqmSelectClause selectClause) {
2236
2234
@ Override
2237
2235
public Void visitSelection (SqmSelection <?> sqmSelection ) {
2238
2236
return visitSelection (
2239
- currentSqmQueryPart .getFirstQuerySpec ().getSelectClause ().getSelections ().indexOf ( sqmSelection ),
2237
+ getCurrentSqmQueryPart () .getFirstQuerySpec ().getSelectClause ().getSelections ().indexOf ( sqmSelection ),
2240
2238
sqmSelection
2241
2239
);
2242
2240
}
@@ -2355,7 +2353,7 @@ else if ( statement.getQuerySource() == SqmQuerySource.CRITERIA && currentClause
2355
2353
// To avoid this issue, we determine the position and let the SqlAstTranslator handle the rest.
2356
2354
// Usually it will render `select ?, count(*) from dual group by 1` if supported
2357
2355
// 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 ();
2359
2357
sqmPosition = indexOfExpression ( querySpec .getSelectClause ().getSelections (), groupByClauseExpression );
2360
2358
path = null ;
2361
2359
}
@@ -2383,7 +2381,7 @@ else if ( statement.getQuerySource() == SqmQuerySource.CRITERIA && currentClause
2383
2381
continue OUTER ;
2384
2382
}
2385
2383
}
2386
- if ( currentSqmQueryPart instanceof SqmQueryGroup <?> ) {
2384
+ if ( getCurrentSqmQueryPart () instanceof SqmQueryGroup <?> ) {
2387
2385
// Reusing the SqlSelection for query groups would be wrong because the aliases do no exist
2388
2386
// So we have to use a literal expression in a new SqlSelection instance to refer to the position
2389
2387
expressions .add (
0 commit comments