Properly implement handling sort items.

See #2962
Original Pull Request: #2965
This commit is contained in:
Greg L. Turnquist
2023-05-22 14:29:31 -05:00
parent cc901527cf
commit da37737c22
3 changed files with 31 additions and 25 deletions

View File

@@ -139,10 +139,6 @@ values
: '(' expression (',' expression)* ')'
;
projectedItem
: (expression | instantiation) alias?
;
instantiation
: NEW instantiationTarget '(' instantiationArguments ')'
;
@@ -254,7 +250,7 @@ groupByClause
;
orderByClause
: ORDER BY projectedItem (',' projectedItem)*
: ORDER BY sortedItem (',' sortedItem)*
;
havingClause

View File

@@ -457,24 +457,6 @@ class HqlQueryRenderer extends HqlBaseVisitor<List<JpaQueryParsingToken>> {
return tokens;
}
@Override
public List<JpaQueryParsingToken> visitProjectedItem(HqlParser.ProjectedItemContext ctx) {
List<JpaQueryParsingToken> tokens = new ArrayList<>();
if (ctx.expression() != null) {
tokens.addAll(visit(ctx.expression()));
} else if (ctx.instantiation() != null) {
tokens.addAll(visit(ctx.instantiation()));
}
if (ctx.alias() != null) {
tokens.addAll(visit(ctx.alias()));
}
return tokens;
}
@Override
public List<JpaQueryParsingToken> visitInstantiation(HqlParser.InstantiationContext ctx) {
@@ -858,8 +840,8 @@ class HqlQueryRenderer extends HqlBaseVisitor<List<JpaQueryParsingToken>> {
tokens.add(new JpaQueryParsingToken(ctx.ORDER()));
tokens.add(new JpaQueryParsingToken(ctx.BY()));
ctx.projectedItem().forEach(projectedItemContext -> {
tokens.addAll(visit(projectedItemContext));
ctx.sortedItem().forEach(sortedItemContext -> {
tokens.addAll(visit(sortedItemContext));
NOSPACE(tokens);
tokens.add(TOKEN_COMMA);
});

View File

@@ -1430,4 +1430,32 @@ class HqlQueryRendererTests {
"order by p " + //
"limit 50");
}
@Test // GH-2962
void orderByWithNullsFirstOrLastShouldWork() {
assertThatNoException().isThrownBy(() -> {
parseWithoutChanges("""
select a,
case
when a.geaendertAm is null then a.erstelltAm
else a.geaendertAm end as mutationAm
from Element a
where a.erstelltDurch = :variable
order by mutationAm desc nulls first
""");
});
assertThatNoException().isThrownBy(() -> {
parseWithoutChanges("""
select a,
case
when a.geaendertAm is null then a.erstelltAm
else a.geaendertAm end as mutationAm
from Element a
where a.erstelltDurch = :variable
order by mutationAm desc nulls last
""");
});
}
}