@@ -985,35 +985,32 @@ public SqmQueryPart<?> visitQueryOrderExpression(HqlParser.QueryOrderExpressionC
985
985
final SqmFromClause fromClause = buildInferredFromClause (null );
986
986
sqmQuerySpec .setFromClause ( fromClause );
987
987
sqmQuerySpec .setSelectClause ( buildInferredSelectClause ( fromClause ) );
988
- visitQueryOrder ( sqmQuerySpec , ctx .queryOrder () );
988
+ visitOrderBy ( sqmQuerySpec , ctx .orderByClause () );
989
+ visitLimitOffset ( sqmQuerySpec , ctx .limitOffset () );
989
990
return sqmQuerySpec ;
990
991
}
991
992
992
993
@ Override
993
994
public SqmQueryPart <?> visitQuerySpecExpression (HqlParser .QuerySpecExpressionContext ctx ) {
994
995
final SqmQueryPart <?> queryPart = visitQuery ( ctx .query () );
995
- final HqlParser .QueryOrderContext queryOrderContext = ctx .queryOrder ();
996
- if ( queryOrderContext != null ) {
997
- visitQueryOrder ( queryPart , queryOrderContext );
998
- }
996
+ visitOrderBy ( queryPart , ctx .orderByClause () );
997
+ visitLimitOffset ( queryPart , ctx .limitOffset () );
999
998
return queryPart ;
1000
999
}
1001
1000
1002
1001
@ Override
1003
1002
public SqmQueryPart <?> visitNestedQueryExpression (HqlParser .NestedQueryExpressionContext ctx ) {
1004
1003
final SqmQueryPart <?> queryPart = (SqmQueryPart <?>) ctx .queryExpression ().accept ( this );
1005
- final HqlParser .QueryOrderContext queryOrderContext = ctx .queryOrder ();
1006
- if ( queryOrderContext != null ) {
1007
- final SqmCreationProcessingState firstProcessingState = processingStateStack .pop ();
1008
- processingStateStack .push (
1009
- new SqmQueryPartCreationProcessingStateStandardImpl (
1010
- processingStateStack .getCurrent (),
1011
- firstProcessingState .getProcessingQuery (),
1012
- this
1013
- )
1014
- );
1015
- visitQueryOrder ( queryPart , queryOrderContext );
1016
- }
1004
+ final SqmCreationProcessingState firstProcessingState = processingStateStack .pop ();
1005
+ processingStateStack .push (
1006
+ new SqmQueryPartCreationProcessingStateStandardImpl (
1007
+ processingStateStack .getCurrent (),
1008
+ firstProcessingState .getProcessingQuery (),
1009
+ this
1010
+ )
1011
+ );
1012
+ visitOrderBy ( queryPart , ctx .orderByClause () );
1013
+ visitLimitOffset ( queryPart , ctx .limitOffset () );
1017
1014
return queryPart ;
1018
1015
}
1019
1016
@@ -1120,44 +1117,38 @@ public SetOperator visitSetOperator(HqlParser.SetOperatorContext ctx) {
1120
1117
};
1121
1118
}
1122
1119
1123
- protected void visitQueryOrder (SqmQueryPart <?> sqmQueryPart , HqlParser .QueryOrderContext ctx ) {
1124
- if ( ctx == null ) {
1125
- return ;
1126
- }
1127
- final SqmOrderByClause orderByClause ;
1128
- final HqlParser .OrderByClauseContext orderByClauseContext = ctx .orderByClause ();
1129
- if ( orderByClauseContext != null ) {
1130
- if ( creationOptions .useStrictJpaCompliance () && processingStateStack .depth () > 1 ) {
1131
- throw new StrictJpaComplianceViolation (
1132
- StrictJpaComplianceViolation .Type .SUBQUERY_ORDER_BY
1133
- );
1134
- }
1120
+ protected void visitLimitOffset (SqmQueryPart <?> sqmQueryPart , HqlParser .LimitOffsetContext ctx ) {
1121
+ if ( ctx != null ) {
1122
+ final HqlParser .LimitClauseContext limitClauseContext = ctx .limitClause ();
1123
+ final HqlParser .OffsetClauseContext offsetClauseContext = ctx .offsetClause ();
1124
+ final HqlParser .FetchClauseContext fetchClauseContext = ctx .fetchClause ();
1125
+ if ( limitClauseContext != null || offsetClauseContext != null || fetchClauseContext != null ) {
1126
+ if ( getCreationOptions ().useStrictJpaCompliance () ) {
1127
+ throw new StrictJpaComplianceViolation (
1128
+ StrictJpaComplianceViolation .Type .LIMIT_OFFSET_CLAUSE
1129
+ );
1130
+ }
1135
1131
1136
- orderByClause = visitOrderByClause ( orderByClauseContext );
1137
- sqmQueryPart . setOrderByClause ( orderByClause );
1138
- }
1139
- else {
1140
- orderByClause = null ;
1141
- }
1132
+ if ( processingStateStack . depth () > 1 && sqmQueryPart . getOrderByClause () == null ) {
1133
+ throw new SemanticException (
1134
+ "A 'limit', 'offset', or 'fetch' clause requires an 'order by' clause when used in a subquery" ,
1135
+ query
1136
+ ) ;
1137
+ }
1142
1138
1143
- final HqlParser .LimitClauseContext limitClauseContext = ctx .limitClause ();
1144
- final HqlParser .OffsetClauseContext offsetClauseContext = ctx .offsetClause ();
1145
- final HqlParser .FetchClauseContext fetchClauseContext = ctx .fetchClause ();
1146
- if ( limitClauseContext != null || offsetClauseContext != null || fetchClauseContext != null ) {
1147
- if ( getCreationOptions ().useStrictJpaCompliance () ) {
1148
- throw new StrictJpaComplianceViolation (
1149
- StrictJpaComplianceViolation .Type .LIMIT_OFFSET_CLAUSE
1150
- );
1139
+ setOffsetFetchLimit ( sqmQueryPart , limitClauseContext , offsetClauseContext , fetchClauseContext );
1151
1140
}
1141
+ }
1142
+ }
1152
1143
1153
- if ( processingStateStack .depth () > 1 && orderByClause == null ) {
1154
- throw new SemanticException (
1155
- "A 'limit', 'offset', or 'fetch' clause requires an 'order by' clause when used in a subquery" ,
1156
- query
1144
+ protected void visitOrderBy (SqmQueryPart <?> sqmQueryPart , HqlParser .OrderByClauseContext ctx ) {
1145
+ if ( ctx != null ) {
1146
+ if ( creationOptions .useStrictJpaCompliance () && processingStateStack .depth () > 1 ) {
1147
+ throw new StrictJpaComplianceViolation (
1148
+ StrictJpaComplianceViolation .Type .SUBQUERY_ORDER_BY
1157
1149
);
1158
1150
}
1159
-
1160
- setOffsetFetchLimit (sqmQueryPart , limitClauseContext , offsetClauseContext , fetchClauseContext );
1151
+ sqmQueryPart .setOrderByClause ( visitOrderByClause ( ctx ) );
1161
1152
}
1162
1153
}
1163
1154
0 commit comments