@@ -488,7 +488,7 @@ public abstract class BaseSqmToSqlAstConverter<T extends Statement> extends Base
488
488
private boolean deduplicateSelectionItems ;
489
489
private ForeignKeyDescriptor .Nature currentlyResolvingForeignKeySide ;
490
490
private SqmStatement <?> currentSqmStatement ;
491
- private SqmQueryPart <?> currentSqmQueryPart ;
491
+ private Stack < SqmQueryPart > sqmQueryPartStack = new StandardStack <>( SqmQueryPart . class ) ;
492
492
private CteContainer cteContainer ;
493
493
/**
494
494
* A map from {@link SqmCteTable#getCteName()} to the final SQL name.
@@ -792,9 +792,10 @@ public Stack<Clause> getCurrentClauseStack() {
792
792
return currentClauseStack ;
793
793
}
794
794
795
+ @ SuppressWarnings ("rawtypes" )
795
796
@ Override
796
- public SqmQueryPart <?> getCurrentSqmQueryPart () {
797
- return currentSqmQueryPart ;
797
+ public Stack < SqmQueryPart > getSqmQueryPartStack () {
798
+ return sqmQueryPartStack ;
798
799
}
799
800
800
801
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -1726,8 +1727,7 @@ public CteStatement visitCteStatement(SqmCteStatement<?> sqmCteStatement) {
1726
1727
);
1727
1728
final DelegatingSqmAliasedNodeCollector collector =
1728
1729
(DelegatingSqmAliasedNodeCollector ) processingState .getSqlExpressionResolver ();
1729
- final SqmQueryPart <?> oldSqmQueryPart = currentSqmQueryPart ;
1730
- currentSqmQueryPart = queryGroup ;
1730
+ sqmQueryPartStack .push ( queryGroup );
1731
1731
pushProcessingState ( processingState );
1732
1732
1733
1733
try {
@@ -1771,7 +1771,7 @@ public CteStatement visitCteStatement(SqmCteStatement<?> sqmCteStatement) {
1771
1771
}
1772
1772
finally {
1773
1773
popProcessingStateStack ();
1774
- currentSqmQueryPart = oldSqmQueryPart ;
1774
+ sqmQueryPartStack . pop () ;
1775
1775
}
1776
1776
}
1777
1777
finally {
@@ -1985,8 +1985,7 @@ public QueryGroup visitQueryGroup(SqmQueryGroup<?> queryGroup) {
1985
1985
);
1986
1986
final DelegatingSqmAliasedNodeCollector collector = (DelegatingSqmAliasedNodeCollector ) processingState
1987
1987
.getSqlExpressionResolver ();
1988
- final SqmQueryPart <?> sqmQueryPart = currentSqmQueryPart ;
1989
- currentSqmQueryPart = queryGroup ;
1988
+ sqmQueryPartStack .push ( queryGroup );
1990
1989
pushProcessingState ( processingState );
1991
1990
1992
1991
try {
@@ -2008,7 +2007,7 @@ public QueryGroup visitQueryGroup(SqmQueryGroup<?> queryGroup) {
2008
2007
}
2009
2008
finally {
2010
2009
popProcessingStateStack ();
2011
- currentSqmQueryPart = sqmQueryPart ;
2010
+ sqmQueryPartStack . pop () ;
2012
2011
}
2013
2012
}
2014
2013
@@ -2043,9 +2042,8 @@ public QuerySpec visitQuerySpec(SqmQuerySpec<?> sqmQuerySpec) {
2043
2042
);
2044
2043
}
2045
2044
2046
- final SqmQueryPart <?> sqmQueryPart = currentSqmQueryPart ;
2047
2045
final boolean originalDeduplicateSelectionItems = deduplicateSelectionItems ;
2048
- currentSqmQueryPart = sqmQuerySpec ;
2046
+ sqmQueryPartStack . push ( sqmQuerySpec ) ;
2049
2047
// In sub-queries, we can never deduplicate the selection items as that might change semantics
2050
2048
deduplicateSelectionItems = false ;
2051
2049
pushProcessingState ( processingState );
@@ -2062,7 +2060,7 @@ public QuerySpec visitQuerySpec(SqmQuerySpec<?> sqmQuerySpec) {
2062
2060
inNestedContext = oldInNestedContext ;
2063
2061
popProcessingStateStack ();
2064
2062
queryTransformers .pop ();
2065
- currentSqmQueryPart = sqmQueryPart ;
2063
+ sqmQueryPartStack . pop () ;
2066
2064
deduplicateSelectionItems = originalDeduplicateSelectionItems ;
2067
2065
}
2068
2066
}
@@ -2203,7 +2201,7 @@ public SelectClause visitSelectClause(SqmSelectClause selectClause) {
2203
2201
try {
2204
2202
final SelectClause sqlSelectClause = currentQuerySpec ().getSelectClause ();
2205
2203
if ( selectClause == null ) {
2206
- final SqmFrom <?, ?> implicitSelection = determineImplicitSelection ( (SqmQuerySpec <?>) currentSqmQueryPart );
2204
+ final SqmFrom <?, ?> implicitSelection = determineImplicitSelection ( (SqmQuerySpec <?>) getCurrentSqmQueryPart () );
2207
2205
visitSelection ( 0 , new SqmSelection <>( implicitSelection , implicitSelection .nodeBuilder () ) );
2208
2206
}
2209
2207
else {
@@ -2228,7 +2226,7 @@ public SelectClause visitSelectClause(SqmSelectClause selectClause) {
2228
2226
@ Override
2229
2227
public Void visitSelection (SqmSelection <?> sqmSelection ) {
2230
2228
visitSelection (
2231
- currentSqmQueryPart .getFirstQuerySpec ().getSelectClause ().getSelections ().indexOf ( sqmSelection ),
2229
+ getCurrentSqmQueryPart () .getFirstQuerySpec ().getSelectClause ().getSelections ().indexOf ( sqmSelection ),
2232
2230
sqmSelection
2233
2231
);
2234
2232
return null ;
@@ -2353,7 +2351,7 @@ else if ( statement.getQuerySource() == SqmQuerySource.CRITERIA && currentClause
2353
2351
// To avoid this issue, we determine the position and let the SqlAstTranslator handle the rest.
2354
2352
// Usually it will render `select ?, count(*) from dual group by 1` if supported
2355
2353
// 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 ();
2357
2355
sqmPosition = indexOfExpression ( querySpec .getSelectClause ().getSelections (), groupByClauseExpression );
2358
2356
path = null ;
2359
2357
}
@@ -2395,7 +2393,7 @@ private SqlSelectionExpression selectionExpression(List<SqlSelection> selections
2395
2393
return null ;
2396
2394
}
2397
2395
}
2398
- return currentSqmQueryPart instanceof SqmQueryGroup <?>
2396
+ return getCurrentSqmQueryPart () instanceof SqmQueryGroup <?>
2399
2397
// Reusing the SqlSelection for query groups would be wrong because the aliases do no exist
2400
2398
// So we have to use a literal expression in a new SqlSelection instance to refer to the position
2401
2399
? sqlSelectionExpression ( selection )
0 commit comments