Skip to content

Support NULLS {FIRST | LAST} in JPQL queries #3529

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-1280-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data JPA Parent</name>
Expand Down
4 changes: 2 additions & 2 deletions spring-data-envers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-envers</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-1280-SNAPSHOT</version>

<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-1280-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-1280-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-jpa-performance/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-1280-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
4 changes: 2 additions & 2 deletions spring-data-jpa/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-1280-SNAPSHOT</version>

<name>Spring Data JPA</name>
<description>Spring Data module for JPA repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa-parent</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.x-GH-1280-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,11 @@ orderby_clause

// TODO Error in spec BNF, correctly shown elsewhere in spec.
orderby_item
: (state_field_path_expression | general_identification_variable | result_variable ) (ASC | DESC)?
: (state_field_path_expression | general_identification_variable | result_variable ) (ASC | DESC)? nullsPrecedence?
;

nullsPrecedence
: NULLS (FIRST | LAST)
;

subquery
Expand Down Expand Up @@ -879,6 +883,7 @@ EXP : E X P;
EXTRACT : E X T R A C T;
FALSE : F A L S E;
FETCH : F E T C H;
FIRST : F I R S T;
FLOOR : F L O O R;
FROM : F R O M;
FUNCTION : F U N C T I O N;
Expand All @@ -890,6 +895,7 @@ INNER : I N N E R;
IS : I S;
JOIN : J O I N;
KEY : K E Y;
LAST : L A S T;
LEADING : L E A D I N G;
LEFT : L E F T;
LENGTH : L E N G T H;
Expand All @@ -906,6 +912,7 @@ NEW : N E W;
NOT : N O T;
NULL : N U L L;
NULLIF : N U L L I F;
NULLS : N U L L S;
OBJECT : O B J E C T;
OF : O F;
ON : O N;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.NullHandling;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -76,6 +77,12 @@ List<QueryToken> orderBy(String primaryFromAlias, Sort sort) {

builder.append(order.isDescending() ? TOKEN_DESC : TOKEN_ASC);

if(order.getNullHandling() == NullHandling.NULLS_FIRST) {
builder.append(" NULLS FIRST");
} else if (order.getNullHandling() == NullHandling.NULLS_LAST) {
builder.append(" NULLS LAST");
}

if (!tokens.isEmpty()) {
tokens.add(TOKEN_COMMA);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.antlr.v4.runtime.tree.ParseTree;

import org.springframework.data.jpa.repository.query.JpqlParser.NullsPrecedenceContext;
import org.springframework.data.jpa.repository.query.JpqlParser.Reserved_wordContext;
import org.springframework.data.jpa.repository.query.QueryRenderer.QueryRendererBuilder;

Expand Down Expand Up @@ -795,9 +796,19 @@ public QueryTokenStream visitOrderby_item(JpqlParser.Orderby_itemContext ctx) {
builder.append(QueryTokens.expression(ctx.DESC()));
}

if(ctx.nullsPrecedence() != null) {
builder.append(visit(ctx.nullsPrecedence()));
}

return builder;
}

@Override
public QueryTokenStream visitNullsPrecedence(NullsPrecedenceContext ctx) {
// return QueryTokenStream.concat(ctx.children, it-> QueryRendererBuilder.from(QueryTokens.token(it.getText())), TOKEN_SPACE);
return QueryTokenStream.justAs(ctx.children, it-> QueryTokens.token(it.getText()));
}

@Override
public QueryTokenStream visitSubquery(JpqlParser.SubqueryContext ctx) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Iterator;
import java.util.function.Function;

import org.springframework.data.jpa.repository.query.QueryRenderer.QueryRendererBuilder;
import org.springframework.data.util.Streamable;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -54,6 +55,10 @@ static <T> QueryTokenStream concat(Collection<T> elements, Function<T, QueryToke
return concat(elements, visitor, QueryRenderer::inline, separator);
}

static <T> QueryTokenStream justAs(Collection<T> elements, Function<T, QueryToken> converter) {
return concat(elements, it-> QueryRendererBuilder.from(converter.apply(it)), QueryRenderer::inline, QueryTokens.TOKEN_SPACE);
}

/**
* Compose a {@link QueryTokenStream} from a collection of expression elements.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.lang.Nullable;

Expand Down Expand Up @@ -71,6 +72,23 @@ void applyingSortShouldCreateAdditionalOrderByCriteria() {
assertThat(results).contains("ORDER BY e.role, e.hire_date, e.first_name asc, e.last_name asc");
}

@Test // GH-1280
void nullFirstLastSorting() {

// given
var original = "SELECT e FROM Employee e where e.name = :name ORDER BY e.first_name asc NULLS FIRST";

assertThat(createQueryFor(original, Sort.unsorted())).isEqualTo(original);

assertThat(createQueryFor(original, Sort.by(Order.desc("lastName").nullsLast())))
.startsWith(original)
.endsWithIgnoringCase("e.lastName DESC NULLS LAST");

assertThat(createQueryFor(original, Sort.by(Order.desc("lastName").nullsFirst())))
.startsWith(original)
.endsWithIgnoringCase("e.lastName DESC NULLS FIRST");
}

@Test
void applyCountToSimpleQuery() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.lang.Nullable;

Expand Down Expand Up @@ -73,6 +74,23 @@ void applyingSortShouldCreateAdditionalOrderByCriteria() {
assertThat(results).contains("ORDER BY e.role, e.hire_date, e.first_name asc, e.last_name asc");
}

@Test // GH-1280
void nullFirstLastSorting() {

// given
var original = "SELECT e FROM Employee e where e.name = :name ORDER BY e.first_name asc NULLS FIRST";

assertThat(createQueryFor(original, Sort.unsorted())).isEqualTo(original);

assertThat(createQueryFor(original, Sort.by(Order.desc("lastName").nullsLast())))
.startsWith(original)
.endsWithIgnoringCase("e.lastName DESC NULLS LAST");

assertThat(createQueryFor(original, Sort.by(Order.desc("lastName").nullsFirst())))
.startsWith(original)
.endsWithIgnoringCase("e.lastName DESC NULLS FIRST");
}

@Test
void applyCountToSimpleQuery() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.JpaSort;
import org.springframework.lang.Nullable;

Expand Down Expand Up @@ -72,6 +73,23 @@ void applyingSortShouldCreateAdditionalOrderByCriteria() {
assertThat(results).contains("ORDER BY e.role, e.hire_date, e.first_name asc, e.last_name asc");
}

@Test // GH-1280
void nullFirstLastSorting() {

// given
var original = "SELECT e FROM Employee e where e.name = :name ORDER BY e.first_name asc NULLS FIRST";

assertThat(createQueryFor(original, Sort.unsorted())).isEqualTo(original);

assertThat(createQueryFor(original, Sort.by(Order.desc("lastName").nullsLast())))
.startsWith(original)
.endsWithIgnoringCase("e.lastName DESC NULLS LAST");

assertThat(createQueryFor(original, Sort.by(Order.desc("lastName").nullsFirst())))
.startsWith(original)
.endsWithIgnoringCase("e.lastName DESC NULLS FIRST");
}

@Test
void applyCountToSimpleQuery() {

Expand Down
Loading