spring-projects / spring-projects/spring-data-jpa
KeysetScrollSpecification null properties support
Open
@christophstrobl is already working on this.
Since Sep 16, 2024.
for: team-attention
status: feedback-provided
status: waiting-for-triage
- Dominant language
- Java
- Stars
- 3.3k
- Forks
- 1.6k
- PR merge metrics
- No merged PRs in 30d
Description
It is documented here that keyset-filtering requires all the keyset properties to be non-nullable.
But will it actually affect indexing/ordering by adding cb.isNull() condition for some of the properties to https://github.com/spring-projects/spring-data-jpa/blob/main/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/KeysetScrollSpecification.java#L120 ?
Generated query examples:
Map<String, Object> keys = new LinkedHashMap<>();
keys.put("createdAt", entity.getCreatedAt());
keys.put("id", entity.getId());
// Maybe somehow here allow to specify which keys can be null, but at least one key is requred to be not null
KeysetScrollPosition scrollPosition = ScrollPosition.of(keys, ScrollPosition.Direction.FORWARD);
Sort sort = Sort.by(Sort.Direction.ASC, "createdAt", "id")
Function<FluentQuery.FetchableFluentQuery<Entity>, Window<Entity>> queryFunction = query -> query
.limit(10)
.sortBy(sort)
.scroll(scrollPosition);
Specification<Entity> specification = ...;
Window<Entity> result = repository.findBy(specification, queryFunction);
SELECT *
FROM table
WHERE (created_at IS NOT NULL AND created_at > ?) OR
(created_at IS NULL AND id > ?) -- use a fallback key like `id` for rows with NULL `created_at`
ORDER BY created_at ASC, id ASC
LIMIT 10;
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.