Support NullHandling in JSqlParserQueryEnhancer.

Closes #3886
This commit is contained in:
Mark Paluch
2025-05-15 09:54:11 +02:00
parent 7e87a62557
commit 33e694f3a4
2 changed files with 24 additions and 0 deletions

View File

@@ -501,6 +501,15 @@ public class JSqlParserQueryEnhancer implements QueryEnhancer {
: property;
Expression orderExpression = order.isIgnoreCase() ? getJSqlLower(reference) : new Column(reference);
orderByElement.setExpression(orderExpression);
switch (order.getNullHandling()) {
case NULLS_FIRST -> orderByElement.setNullOrdering(OrderByElement.NullOrdering.NULLS_FIRST);
case NULLS_LAST -> orderByElement.setNullOrdering(OrderByElement.NullOrdering.NULLS_LAST);
default -> {
// do nothing
}
}
return orderByElement;
}

View File

@@ -25,6 +25,8 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.data.domain.Sort;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.query.ReturnedType;
/**
* TCK Tests for {@link JSqlParserQueryEnhancer}.
@@ -51,6 +53,19 @@ class JSqlParserQueryEnhancerUnitTests extends QueryEnhancerTckTests {
assertThat(sql).isEqualTo("SELECT e FROM Employee e ORDER BY e.foo ASC, e.bar ASC");
}
@Test // GH-3886
void shouldApplySortingWithNullsPrecedence() {
QueryEnhancer enhancer = createQueryEnhancer(DeclaredQuery.of("SELECT e FROM Employee e", true));
String sql = enhancer.rewrite(new DefaultQueryRewriteInformation(
Sort.by(Sort.Order.asc("foo").with(Sort.NullHandling.NULLS_LAST),
Sort.Order.desc("bar").with(Sort.NullHandling.NULLS_FIRST)),
ReturnedType.of(Object.class, Object.class, new SpelAwareProxyProjectionFactory())));
assertThat(sql).isEqualTo("SELECT e FROM Employee e ORDER BY e.foo ASC NULLS LAST, e.bar DESC NULLS FIRST");
}
@Test // GH-3707
void countQueriesShouldConsiderPrimaryTableAlias() {