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

Override query limit in R2dbcEntityTemplate.selectOne only if the query has no limit #758

Closed
RobertHeim opened this issue Apr 26, 2022 · 8 comments
Labels
status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement

Comments

@RobertHeim
Copy link

Using selectOne on a query with multiple results obviously throws a "non unique result" exception. When the query is limited to 1, it still throws:

return template.selectOne(
  Query.query(
    Criteria.where("notificationId").isEqual(notificationId)
  )
    .sort(Sort.by("serverTimestamp").ascending())
    .limit(1), // <----
  MyEntity::class.java
)
  .awaitSingleOrNull()

In the query log I see that the limit is set to 2 instead of 1.

org.springframework.dao.IncorrectResultSizeDataAccessException: Query [SELECT my_entity.* FROM my_entity WHERE my_entity.notification_id = $1 ORDER BY server_timestamp ASC LIMIT 2] returned non unique result.

This makes me think it's a bug because the limit seems to be ignored?

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 26, 2022
@mp911de
Copy link
Member

mp911de commented Apr 27, 2022

selectOne(…) overrides unconditionally the limit of your query to 2. We guarantee with that method to catch also cases where the query would yield more than one result, see the documentation:

@throws org.springframework.dao.IncorrectResultSizeDataAccessException if more than one match found

We could make the limit override conditional and set the limit only if it wasn't already set.

@mp911de mp911de added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 27, 2022
@mp911de mp911de changed the title R2dbcEntityTemplate.selectOne throws "non unique result" with limit(1) Override query limit in R2dbcEntityTemplate.selectOne only if the query has no limit Apr 27, 2022
@RobertHeim
Copy link
Author

RobertHeim commented Apr 27, 2022

Yes, that would be the expected behavior.

@mp911de
Copy link
Member

mp911de commented Apr 27, 2022

Do you want to submit a pull request including test cases?

@mp911de mp911de added the status: ideal-for-contribution An issue that a contributor can help us with label Apr 27, 2022
RobertHeim added a commit to RobertHeim/spring-data-relational that referenced this issue Apr 27, 2022
RobertHeim added a commit to RobertHeim/spring-data-relational that referenced this issue Apr 27, 2022
RobertHeim added a commit to RobertHeim/spring-data-relational that referenced this issue Apr 27, 2022
@RobertHeim
Copy link
Author

Please have a look at the PR spring-projects/spring-data-relational#1233

I implemented it for the R2dbcEntityTemplate as well as for ReactiveSelectOperationSupport.

@mp911de
Copy link
Member

mp911de commented Apr 27, 2022

Thanks a lot. The change in ReactiveSelectOperationSupport doesn't make sense since the API exposes first and one methods. first silently takes the first result while one provides stronger guarantees in the sense to fail if a result yields more than one result.

Happy to merge the Template API part though.

@RobertHeim
Copy link
Author

I understand the argument that the developers my use first instead of limiting the query. but I would still argue that ReactiveSelectOperationSupport should employ the same change. 1) It should be semantically identical to template's selectOne and 2) given a query with a limit 1 it is strange that it would fail:

entityTemplate.select(Person.class) //
				.matching(query(where("name").is("Walter")).limit(1)) //
				.one() //

@mp911de
Copy link
Member

mp911de commented Apr 27, 2022

We strive to have strict API guarantees. Reacting to the query limit as in the pull request is already widening what the methods are capable of.

On the other side, the R2DBC part is more strict than e.g. the MongoDB Template API which already returns the first result without checking whether a query would yield more results.

@RobertHeim
Copy link
Author

ok. I removed the changes to ReactiveSelectOperationSupportTest and its test and updated the PR. Thanks for the immediate feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: ideal-for-contribution An issue that a contributor can help us with type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants