Skip to content

Commit b1dea0a

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

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
@@ -27,9 +27,9 @@
2727
/**
2828
* Tests built around examples of EQL found in the EclipseLink's docs at
2929
* https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL<br/>
30-
* With the exception of {@literal MOD} which is defined as {@literal MOD(arithmetic_expression , arithmetic_expression)},
31-
* but shown in tests as {@literal MOD(arithmetic_expression ? arithmetic_expression)}.
32-
* <br/>
30+
* With the exception of {@literal MOD} which is defined as
31+
* {@literal MOD(arithmetic_expression , arithmetic_expression)}, but shown in tests as
32+
* {@literal MOD(arithmetic_expression ? arithmetic_expression)}. <br/>
3333
* IMPORTANT: Purely verifies the parser without any transformations.
3434
*
3535
* @author Greg Turnquist
@@ -417,7 +417,6 @@ void isNullAndIsNotNull() {
417417
assertQuery("SELECT e FROM Employee e WHERE (e.active IS NOT NULL OR e.active = true)");
418418
}
419419

420-
421420
@Test // GH-3496
422421
void lateralShouldBeAValidParameter() {
423422

@@ -444,13 +443,13 @@ void except() {
444443
}
445444

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

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

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)