Skip to content

Commit

Permalink
Make sure sorting is rendered correctly for JPQL query using set oper…
Browse files Browse the repository at this point in the history
…ator.

Original Pull Request: #3695
  • Loading branch information
christophstrobl authored and mp911de committed Jan 14, 2025
1 parent 33fc15d commit b1dea0a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ public QueryTokenStream visitSelect_query(JpqlParser.Select_queryContext ctx) {
if (ctx.having_clause() != null) {
builder.appendExpression(visit(ctx.having_clause()));
}
if(ctx.set_fuction() != null) {
builder.appendExpression(visit(ctx.set_fuction()));
}

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public QueryTokenStream visitSelect_query(JpqlParser.Select_queryContext ctx) {
builder.appendExpression(visit(ctx.having_clause()));
}

doVisitOrderBy(builder, ctx);
if(ctx.set_fuction() != null) {
builder.appendExpression(visit(ctx.set_fuction()));
} else {
doVisitOrderBy(builder, ctx);
}

return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
/**
* Tests built around examples of EQL found in the EclipseLink's docs at
* https://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Querying/JPQL<br/>
* With the exception of {@literal MOD} which is defined as {@literal MOD(arithmetic_expression , arithmetic_expression)},
* but shown in tests as {@literal MOD(arithmetic_expression ? arithmetic_expression)}.
* <br/>
* With the exception of {@literal MOD} which is defined as
* {@literal MOD(arithmetic_expression , arithmetic_expression)}, but shown in tests as
* {@literal MOD(arithmetic_expression ? arithmetic_expression)}. <br/>
* IMPORTANT: Purely verifies the parser without any transformations.
*
* @author Greg Turnquist
Expand Down Expand Up @@ -417,7 +417,6 @@ void isNullAndIsNotNull() {
assertQuery("SELECT e FROM Employee e WHERE (e.active IS NOT NULL OR e.active = true)");
}


@Test // GH-3496
void lateralShouldBeAValidParameter() {

Expand All @@ -444,13 +443,13 @@ void except() {
}

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

@ParameterizedTest // GH-3136
@ValueSource(strings = {"LEFT", "RIGHT"})
@ValueSource(strings = { "LEFT", "RIGHT" })
void leftRightStringFunctions(String keyword) {
assertQuery("SELECT %s(e.name, 3) FROM Employee e".formatted(keyword));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,15 @@ void sortingRecognizesJoinAliases() {
""");
}

@Test // GH-3427
void sortShouldBeAppendedToFullSelectOnlyInCaseOfSetOperator() {

String source = "SELECT tb FROM Test tb WHERE (tb.type='A') UNION SELECT tb FROM Test tb WHERE (tb.type='B')";
String target = createQueryFor(source, Sort.by("Type").ascending());

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");
}

static Stream<Arguments> queriesWithReservedWordsAsIdentifiers() {

return Stream.of( //
Expand Down

0 comments on commit b1dea0a

Please sign in to comment.