File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed
main/java/org/springframework/data/jpa/repository/query
test/java/org/springframework/data/jpa/repository/query Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -93,7 +93,7 @@ public boolean hasConstructorExpression() {
93
93
*/
94
94
private static boolean isSubquery (ParserRuleContext ctx ) {
95
95
96
- if (ctx instanceof HqlParser .SubqueryContext ) {
96
+ if (ctx instanceof HqlParser .SubqueryContext || ctx instanceof HqlParser . CteContext ) {
97
97
return true ;
98
98
} else if (ctx instanceof HqlParser .SelectStatementContext ) {
99
99
return false ;
Original file line number Diff line number Diff line change @@ -500,6 +500,36 @@ select count(user) from User user
500
500
""" );
501
501
}
502
502
503
+ @ Test // GH-3504
504
+ void createCountWithCteShouldWork () {
505
+
506
+ String countQuery = createCountQueryFor ("""
507
+ WITH maxId AS(select max(sr.snapshot.id) snapshotId from SnapshotReference sr
508
+ where sr.id.selectionId = ?1 and sr.enabled
509
+ group by sr.userId)
510
+ select sr from maxId m join SnapshotReference sr on sr.snapshot.id = m.snapshotId
511
+ """ );
512
+
513
+ assertThat (countQuery ).startsWith ("WITH maxId AS(select max(sr.snapshot.id) snapshotId from SnapshotReference sr" )
514
+ .endsWith ("select count(m) from maxId m join SnapshotReference sr on sr.snapshot.id = m.snapshotId" );
515
+ }
516
+
517
+ @ Test // GH-3504
518
+ void createSortedQueryWithCteShouldWork () {
519
+
520
+ String sortedQuery = createQueryFor ("""
521
+ WITH maxId AS(select max(sr.snapshot.id) snapshotId from SnapshotReference sr
522
+ where sr.id.selectionId = ?1 and sr.enabled
523
+ group by sr.userId)
524
+ select sr from maxId m join SnapshotReference sr on sr.snapshot.id = m.snapshotId
525
+ """ , Sort .by ("sr.snapshot" ));
526
+
527
+ assertThat (sortedQuery ).startsWith (
528
+ "WITH maxId AS(select max(sr.snapshot.id) snapshotId from SnapshotReference sr where sr.id.selectionId = ?1 and sr.enabled group by sr.userId )" )
529
+ .endsWith (
530
+ "select sr from maxId m join SnapshotReference sr on sr.snapshot.id = m.snapshotId order by sr.snapshot asc" );
531
+ }
532
+
503
533
@ Test
504
534
void createCountQuerySupportsLineBreaksInSelectClause () {
505
535
You can’t perform that action at this time.
0 commit comments