Skip to content
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

Keyset-scrolling queries add identifier columns twice when Sort already sorts by Id #2996

Closed
sergey-morenets opened this issue Jun 3, 2023 · 7 comments
Assignees
Labels
type: bug A general bug

Comments

@sergey-morenets
Copy link

Hi

I have a repository with the following method:

	Window<Order> findAllBy(ScrollPosition position, 
			Sort sort);

I tried to use keyset-scrolling and specified createdAt and id as keyset properties. Also I added "createdAt" to sort properties:

		orders = orderRepository.findAllBy(
				ScrollPosition.forward(Map.of("id", id,
						"createdAt", LocalDateTime.now())), 
					Sort.by("createdAt"));

The resulting SQL query uses following sort order: firstly createdAt, then id:

from ORDERS o1_0 where o1_0.createdAt>? or o1_0.createdAt=? and o1_0.id>? order by o1_0.createdAt,o1_0.id

But I need the opposite order (id and then createdAt). So I added "id" property to the Sort.by construction:

		orders = orderRepository.findAllBy(
				ScrollPosition.forward(Map.of("id", id,
						"createdAt", LocalDateTime.now())), 
					Sort.by("id", "createdAt"));

However the resulting SQL query contains ORDER BY clause where "id" column is specified twice:

from ORDERS o1_0 where o1_0.id>? or o1_0.id=? and o1_0.createdAt>? or o1_0.id=? and o1_0.createdAt=? and o1_0.id>? order by o1_0.id,o1_0.createdAt,o1_0.id

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 3, 2023
@sergey-morenets
Copy link
Author

Similar issue occurs if I specified ordering in the method name:

Window<Order> findAllByOrderByIdAscCreatedAt(ScrollPosition position);

If I call this method:

		orders = orderRepository.findAllByOrderByIdAscCreatedAt(
				ScrollPosition.forward(Map.of("id", id,
						"createdAt", LocalDateTime.now())));

Then resulting SQL query contains two mistakes:

from ORDERS o1_0 where o1_0.id>? or o1_0.id=? and o1_0.createdAt>? or o1_0.id=? and o1_0.createdAt=? and o1_0.id>? order by o1_0.id,o1_0.createdAt,o1_0.id

  1. Multiple o1_0.id=? and o1_0.id>? parts.
  2. Duplicate fragment o1_0.id in ORDER BY clause.

@mp911de mp911de self-assigned this Jun 5, 2023
@quaff
Copy link
Contributor

quaff commented Jun 6, 2023

public static Sort createSort(KeysetScrollPosition position, Sort sort, JpaEntityInformation<?, ?> entity) {
KeysetScrollDelegate delegate = KeysetScrollDelegate.of(position.getDirection());
Sort sortToUse;
if (entity.hasCompositeId()) {
sortToUse = sort.and(Sort.by(entity.getIdAttributeNames().toArray(new String[0])));
} else {
sortToUse = sort.and(Sort.by(entity.getRequiredIdAttribute().getName()));
}
return delegate.getSortOrders(sortToUse);
}

Id shouldn't be added to sort if user provide explicit one, WDYT? @mp911de

@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 8, 2023
@mp911de mp911de changed the title Incorrect ORDER BY clause for keyset-scrolling queries Keyset-scrolling queries add identifier columns twice when Sort already sorts by Id Jun 8, 2023
@mp911de
Copy link
Member

mp911de commented Jun 8, 2023

You're right, we should not add identifiers to Sort if Sort contains them.

@mp911de mp911de added this to the 3.1.1 (2023.0.1) milestone Jun 8, 2023
mp911de added a commit that referenced this issue Jun 8, 2023
We now avoid adding sort properties if they are already specified by Sort.

Closes #2996
@mp911de mp911de closed this as completed in 2e489cb Jun 8, 2023
@quaff
Copy link
Contributor

quaff commented Jun 10, 2023

@mp911de It seems that id property will always be used for keyset scrolling, what if id property is not applicable for sort and we could scroll base on another unique property?
For example we have an UUID as id property and an Long as biz_sequence property which is unique and monotone increasing, then this biz_sequence is perfect for keyset scrolling, and id property should not participate in.

quaff added a commit to quaff/spring-data-jpa that referenced this issue Jun 12, 2023
id shouldn't be added to sort if sort property already provided.

See spring-projectsGH-2996
@mp911de
Copy link
Member

mp911de commented Jun 12, 2023

Please file a new ticket to discuss this matter. We require a property to make the database rows uniquely identifiable to avoid skipping rows with the same sort value as their previous row.

@quaff
Copy link
Contributor

quaff commented Jun 12, 2023

Please file a new ticket to discuss this matter. We require a property to make the database rows uniquely identifiable to avoid skipping rows with the same sort value as their previous row.

I've created #3013, should I file another ticket?

@mp911de
Copy link
Member

mp911de commented Jun 12, 2023

That one is a pull request already. We can continue the discussion there.

quaff added a commit to quaff/spring-data-jpa that referenced this issue Jun 13, 2023
id shouldn't be added to sort if sort property already provided.

See spring-projectsGH-2996
quaff added a commit to quaff/spring-data-jpa that referenced this issue Jun 13, 2023
id shouldn't be added to sort if sort property already provided.

See spring-projectsGH-2996
quaff added a commit to quaff/spring-data-jpa that referenced this issue Jun 16, 2023
id shouldn't be added to sort if sort property already provided.

See spring-projectsGH-2996
quaff added a commit to quaff/spring-data-jpa that referenced this issue Jun 16, 2023
id shouldn't be added to sort if sort property already provided.

See spring-projectsGH-2996
quaff added a commit to quaff/spring-data-jpa that referenced this issue Jun 16, 2023
id shouldn't be added to sort if sort property already provided.

See spring-projectsGH-2996
quaff added a commit to quaff/spring-data-jpa that referenced this issue Oct 24, 2023
quaff added a commit to quaff/spring-data-jpa that referenced this issue Oct 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

4 participants