Skip to content

Commit c552d0f

Browse files
christophstroblmp911de
authored andcommitted
Make sure sorting is rendered correctly for JPQL query using set operator.
Original Pull Request: #3695
1 parent 53abf08 commit c552d0f

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

Lines changed: 3 additions & 0 deletions
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

Lines changed: 5 additions & 1 deletion
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

Lines changed: 5 additions & 6 deletions
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
@@ -415,7 +415,6 @@ void isNullAndIsNotNull() {
415415
assertQuery("SELECT e FROM Employee e WHERE (e.active IS NOT NULL OR e.active = true)");
416416
}
417417

418-
419418
@Test // GH-3496
420419
void lateralShouldBeAValidParameter() {
421420

@@ -442,13 +441,13 @@ void except() {
442441
}
443442

444443
@ParameterizedTest // GH-3136
445-
@ValueSource(strings = {"STRING", "INTEGER", "FLOAT", "DOUBLE"})
444+
@ValueSource(strings = { "STRING", "INTEGER", "FLOAT", "DOUBLE" })
446445
void jpqlCast(String targetType) {
447446
assertQuery("SELECT CAST(e.salary AS %s) FROM Employee e".formatted(targetType));
448447
}
449448

450449
@ParameterizedTest // GH-3136
451-
@ValueSource(strings = {"LEFT", "RIGHT"})
450+
@ValueSource(strings = { "LEFT", "RIGHT" })
452451
void leftRightStringFunctions(String keyword) {
453452
assertQuery("SELECT %s(e.name, 3) FROM Employee e".formatted(keyword));
454453
}

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

Lines changed: 9 additions & 0 deletions
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)