Skip to content

Commit 87ed8d5

Browse files
christophstroblmp911de
authored andcommitted
Make sure sorting is rendered correctly for JPQL query using set operator.
Original Pull Request: #3695
1 parent 66bbd1f commit 87ed8d5

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlCountQueryTransformer.java

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public QueryTokenStream visitSelect_query(JpqlParser.Select_queryContext ctx) {
6868
if (ctx.having_clause() != null) {
6969
builder.appendExpression(visit(ctx.having_clause()));
7070
}
71+
if(ctx.set_fuction() != null) {
72+
builder.appendExpression(visit(ctx.set_fuction()));
73+
}
7174

7275
return builder;
7376
}

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlSortedQueryTransformer.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ public QueryTokenStream visitSelect_query(JpqlParser.Select_queryContext ctx) {
8181
builder.appendExpression(visit(ctx.having_clause()));
8282
}
8383

84-
doVisitOrderBy(builder, ctx);
84+
if(ctx.set_fuction() != null) {
85+
builder.appendExpression(visit(ctx.set_fuction()));
86+
} else {
87+
doVisitOrderBy(builder, ctx);
88+
}
8589

8690
return builder;
8791
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/EqlComplianceTests.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
/**
2727
* Tests built around examples of EQL found in the EclipseLink's docs at
2828
* https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL<br/>
29-
* With the exception of {@literal MOD} which is defined as {@literal MOD(arithmetic_expression , arithmetic_expression)},
30-
* but shown in tests as {@literal MOD(arithmetic_expression ? arithmetic_expression)}.
31-
* <br/>
29+
* With the exception of {@literal MOD} which is defined as
30+
* {@literal MOD(arithmetic_expression , arithmetic_expression)}, but shown in tests as
31+
* {@literal MOD(arithmetic_expression ? arithmetic_expression)}. <br/>
3232
* IMPORTANT: Purely verifies the parser without any transformations.
3333
*
3434
* @author Greg Turnquist
@@ -411,7 +411,6 @@ void isNullAndIsNotNull() {
411411
assertQuery("SELECT e FROM Employee e WHERE (e.active IS NOT NULL OR e.active = true)");
412412
}
413413

414-
415414
@Test // GH-3496
416415
void lateralShouldBeAValidParameter() {
417416

@@ -438,13 +437,13 @@ void except() {
438437
}
439438

440439
@ParameterizedTest // GH-3136
441-
@ValueSource(strings = {"STRING", "INTEGER", "FLOAT", "DOUBLE"})
440+
@ValueSource(strings = { "STRING", "INTEGER", "FLOAT", "DOUBLE" })
442441
void jpqlCast(String targetType) {
443442
assertQuery("SELECT CAST(e.salary AS %s) FROM Employee e".formatted(targetType));
444443
}
445444

446445
@ParameterizedTest // GH-3136
447-
@ValueSource(strings = {"LEFT", "RIGHT"})
446+
@ValueSource(strings = { "LEFT", "RIGHT" })
448447
void leftRightStringFunctions(String keyword) {
449448
assertQuery("SELECT %s(e.name, 3) FROM Employee e".formatted(keyword));
450449
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/JpqlQueryTransformerTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,15 @@ void sortingRecognizesJoinAliases() {
784784
""");
785785
}
786786

787+
@Test // GH-3427
788+
void sortShouldBeAppendedToFullSelectOnlyInCaseOfSetOperator() {
789+
790+
String source = "SELECT tb FROM Test tb WHERE (tb.type='A') UNION SELECT tb FROM Test tb WHERE (tb.type='B')";
791+
String target = createQueryFor(source, Sort.by("Type").ascending());
792+
793+
assertThat(target).isEqualTo("SELECT tb FROM Test tb WHERE (tb.type = 'A') UNION SELECT tb FROM Test tb WHERE (tb.type = 'B') order by tb.Type asc");
794+
}
795+
787796
static Stream<Arguments> queriesWithReservedWordsAsIdentifiers() {
788797

789798
return Stream.of( //

0 commit comments

Comments
 (0)