Skip to content

Commit

Permalink
Properly implement handling sort items.
Browse files Browse the repository at this point in the history
See #2962
Original Pull Request: #2965
  • Loading branch information
gregturn committed May 22, 2023
1 parent cc90152 commit da37737
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,6 @@ values
: '(' expression (',' expression)* ')'
;

projectedItem
: (expression | instantiation) alias?
;

instantiation
: NEW instantiationTarget '(' instantiationArguments ')'
;
Expand Down Expand Up @@ -254,7 +250,7 @@ groupByClause
;

orderByClause
: ORDER BY projectedItem (',' projectedItem)*
: ORDER BY sortedItem (',' sortedItem)*
;

havingClause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,24 +457,6 @@ public List<JpaQueryParsingToken> visitValues(HqlParser.ValuesContext ctx) {
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) {

Expand Down Expand Up @@ -858,8 +840,8 @@ public List<JpaQueryParsingToken> visitOrderByClause(HqlParser.OrderByClauseCont
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);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1430,4 +1430,32 @@ void hqlQueries() {
"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
""");
});
}
}

0 comments on commit da37737

Please sign in to comment.