Skip to content

Commit 1ac50e4

Browse files
committed
Properly implement handling sort items.
See #2962 Original Pull Request: #2965
1 parent 850aaac commit 1ac50e4

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Hql.g4

+1-5
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ values
139139
: '(' expression (',' expression)* ')'
140140
;
141141

142-
projectedItem
143-
: (expression | instantiation) alias?
144-
;
145-
146142
instantiation
147143
: NEW instantiationTarget '(' instantiationArguments ')'
148144
;
@@ -254,7 +250,7 @@ groupByClause
254250
;
255251

256252
orderByClause
257-
: ORDER BY projectedItem (',' projectedItem)*
253+
: ORDER BY sortedItem (',' sortedItem)*
258254
;
259255

260256
havingClause

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

+2-20
Original file line numberDiff line numberDiff line change
@@ -457,24 +457,6 @@ public List<JpaQueryParsingToken> visitValues(HqlParser.ValuesContext ctx) {
457457
return tokens;
458458
}
459459

460-
@Override
461-
public List<JpaQueryParsingToken> visitProjectedItem(HqlParser.ProjectedItemContext ctx) {
462-
463-
List<JpaQueryParsingToken> tokens = new ArrayList<>();
464-
465-
if (ctx.expression() != null) {
466-
tokens.addAll(visit(ctx.expression()));
467-
} else if (ctx.instantiation() != null) {
468-
tokens.addAll(visit(ctx.instantiation()));
469-
}
470-
471-
if (ctx.alias() != null) {
472-
tokens.addAll(visit(ctx.alias()));
473-
}
474-
475-
return tokens;
476-
}
477-
478460
@Override
479461
public List<JpaQueryParsingToken> visitInstantiation(HqlParser.InstantiationContext ctx) {
480462

@@ -858,8 +840,8 @@ public List<JpaQueryParsingToken> visitOrderByClause(HqlParser.OrderByClauseCont
858840
tokens.add(new JpaQueryParsingToken(ctx.ORDER()));
859841
tokens.add(new JpaQueryParsingToken(ctx.BY()));
860842

861-
ctx.projectedItem().forEach(projectedItemContext -> {
862-
tokens.addAll(visit(projectedItemContext));
843+
ctx.sortedItem().forEach(sortedItemContext -> {
844+
tokens.addAll(visit(sortedItemContext));
863845
NOSPACE(tokens);
864846
tokens.add(TOKEN_COMMA);
865847
});

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

+28
Original file line numberDiff line numberDiff line change
@@ -1430,4 +1430,32 @@ void hqlQueries() {
14301430
"order by p " + //
14311431
"limit 50");
14321432
}
1433+
1434+
@Test // GH-2962
1435+
void orderByWithNullsFirstOrLastShouldWork() {
1436+
1437+
assertThatNoException().isThrownBy(() -> {
1438+
parseWithoutChanges("""
1439+
select a,
1440+
case
1441+
when a.geaendertAm is null then a.erstelltAm
1442+
else a.geaendertAm end as mutationAm
1443+
from Element a
1444+
where a.erstelltDurch = :variable
1445+
order by mutationAm desc nulls first
1446+
""");
1447+
});
1448+
1449+
assertThatNoException().isThrownBy(() -> {
1450+
parseWithoutChanges("""
1451+
select a,
1452+
case
1453+
when a.geaendertAm is null then a.erstelltAm
1454+
else a.geaendertAm end as mutationAm
1455+
from Element a
1456+
where a.erstelltDurch = :variable
1457+
order by mutationAm desc nulls last
1458+
""");
1459+
});
1460+
}
14331461
}

0 commit comments

Comments
 (0)