Skip to content

Commit 33e694f

Browse files
committed
Support NullHandling in JSqlParserQueryEnhancer.
Closes #3886
1 parent 7e87a62 commit 33e694f

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,15 @@ private static OrderByElement getOrderClause(Set<String> joinAliases, Set<String
501501
: property;
502502
Expression orderExpression = order.isIgnoreCase() ? getJSqlLower(reference) : new Column(reference);
503503
orderByElement.setExpression(orderExpression);
504+
505+
switch (order.getNullHandling()) {
506+
case NULLS_FIRST -> orderByElement.setNullOrdering(OrderByElement.NullOrdering.NULLS_FIRST);
507+
case NULLS_LAST -> orderByElement.setNullOrdering(OrderByElement.NullOrdering.NULLS_LAST);
508+
default -> {
509+
// do nothing
510+
}
511+
}
512+
504513
return orderByElement;
505514
}
506515

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import org.junit.jupiter.params.provider.Arguments;
2626
import org.junit.jupiter.params.provider.MethodSource;
2727
import org.springframework.data.domain.Sort;
28+
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
29+
import org.springframework.data.repository.query.ReturnedType;
2830

2931
/**
3032
* TCK Tests for {@link JSqlParserQueryEnhancer}.
@@ -51,6 +53,19 @@ void shouldApplySorting() {
5153
assertThat(sql).isEqualTo("SELECT e FROM Employee e ORDER BY e.foo ASC, e.bar ASC");
5254
}
5355

56+
@Test // GH-3886
57+
void shouldApplySortingWithNullsPrecedence() {
58+
59+
QueryEnhancer enhancer = createQueryEnhancer(DeclaredQuery.of("SELECT e FROM Employee e", true));
60+
61+
String sql = enhancer.rewrite(new DefaultQueryRewriteInformation(
62+
Sort.by(Sort.Order.asc("foo").with(Sort.NullHandling.NULLS_LAST),
63+
Sort.Order.desc("bar").with(Sort.NullHandling.NULLS_FIRST)),
64+
ReturnedType.of(Object.class, Object.class, new SpelAwareProxyProjectionFactory())));
65+
66+
assertThat(sql).isEqualTo("SELECT e FROM Employee e ORDER BY e.foo ASC NULLS LAST, e.bar DESC NULLS FIRST");
67+
}
68+
5469
@Test // GH-3707
5570
void countQueriesShouldConsiderPrimaryTableAlias() {
5671

0 commit comments

Comments
 (0)