Skip to content

Commit b18951e

Browse files
committed
HHH-19240 Simplify queryExpression grammar rule
(cherry picked from commit 0e158e0)
1 parent 71ce6f5 commit b18951e

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

hibernate-core/src/main/antlr/org/hibernate/grammars/hql/HqlParser.g4

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ cycleClause
151151
* A toplevel query of subquery, which may be a union or intersection of subqueries
152152
*/
153153
queryExpression
154-
: withClause? orderedQuery # SimpleQueryGroup
155-
| withClause? orderedQuery (setOperator orderedQuery)+ # SetQueryGroup
154+
: withClause? orderedQuery (setOperator orderedQuery)*
156155
;
157156

158157
/**

hibernate-core/src/main/java/org/hibernate/query/hql/internal/SemanticQueryBuilder.java

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,10 @@ public Object visitCte(HqlParser.CteContext ctx) {
775775
final JpaCteCriteria<?> oldCte = currentPotentialRecursiveCte;
776776
try {
777777
currentPotentialRecursiveCte = null;
778-
if ( queryExpressionContext instanceof HqlParser.SetQueryGroupContext setContext ) {
779-
// A recursive query is only possible if the child count is lower than 5 e.g. `withClause? q1 op q2`
780-
if ( setContext.getChildCount() < 5 ) {
781-
if ( handleRecursive( ctx, setContext, cteContainer, name, cte, materialization ) ) {
782-
return null;
783-
}
778+
// A recursive query is only possible if there are 2 ordered queries e.g. `q1 op q2`
779+
if ( queryExpressionContext.orderedQuery().size() == 2 ) {
780+
if ( handleRecursive( ctx, queryExpressionContext, cteContainer, name, cte, materialization ) ) {
781+
return null;
784782
}
785783
}
786784
queryExpressionContext.accept( this );
@@ -798,7 +796,7 @@ public Object visitCte(HqlParser.CteContext ctx) {
798796

799797
private boolean handleRecursive(
800798
HqlParser.CteContext cteContext,
801-
HqlParser.SetQueryGroupContext setContext,
799+
HqlParser.QueryExpressionContext setContext,
802800
SqmCteContainer cteContainer,
803801
String name,
804802
SqmSelectQuery<Object> cte,
@@ -967,15 +965,6 @@ private static CteSearchClauseKind getCteSearchClauseKind(HqlParser.SearchClause
967965
return ctx.BREADTH() != null ? CteSearchClauseKind.BREADTH_FIRST : CteSearchClauseKind.DEPTH_FIRST;
968966
}
969967

970-
@Override
971-
public SqmQueryPart<?> visitSimpleQueryGroup(HqlParser.SimpleQueryGroupContext ctx) {
972-
final var withClauseContext = ctx.withClause();
973-
if ( withClauseContext != null ) {
974-
withClauseContext.accept( this );
975-
}
976-
return (SqmQueryPart<?>) ctx.orderedQuery().accept( this );
977-
}
978-
979968
@Override
980969
public SqmQueryPart<?> visitQueryOrderExpression(HqlParser.QueryOrderExpressionContext ctx) {
981970
final SqmQuerySpec<?> sqmQuerySpec = currentQuerySpec();
@@ -1012,25 +1001,28 @@ public SqmQueryPart<?> visitNestedQueryExpression(HqlParser.NestedQueryExpressio
10121001
}
10131002

10141003
@Override
1015-
public SqmQueryGroup<?> visitSetQueryGroup(HqlParser.SetQueryGroupContext ctx) {
1004+
public SqmQueryPart<?> visitQueryExpression(HqlParser.QueryExpressionContext ctx) {
10161005
var withClauseContext = ctx.withClause();
10171006
if ( withClauseContext != null ) {
10181007
withClauseContext.accept( this );
10191008
}
1009+
final var orderedQueryContexts = ctx.orderedQuery();
10201010
final SqmQueryPart<?> firstQueryPart =
1021-
(SqmQueryPart<?>) ctx.orderedQuery(0).accept( this );
1011+
(SqmQueryPart<?>) orderedQueryContexts.get( 0 ).accept( this );
1012+
if ( orderedQueryContexts.size() == 1 ) {
1013+
return firstQueryPart;
1014+
}
10221015
SqmQueryGroup<?> queryGroup =
10231016
firstQueryPart instanceof SqmQueryGroup<?> sqmQueryGroup
10241017
? sqmQueryGroup
10251018
: new SqmQueryGroup<>( firstQueryPart );
10261019
setCurrentQueryPart( queryGroup );
1027-
final var orderedQueryContexts = ctx.orderedQuery();
10281020
final var setOperatorContexts = ctx.setOperator();
10291021
final SqmCreationProcessingState firstProcessingState = processingStateStack.pop();
10301022
for ( int i = 0; i < setOperatorContexts.size(); i++ ) {
10311023
queryGroup = getSqmQueryGroup(
10321024
visitSetOperator( setOperatorContexts.get(i) ),
1033-
orderedQueryContexts.get( i+1 ),
1025+
orderedQueryContexts.get( i + 1 ),
10341026
queryGroup,
10351027
setOperatorContexts.size(),
10361028
firstProcessingState,

0 commit comments

Comments
 (0)