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

Provide an option for a simple wrapper count query #3706

Open
edudar-chwy opened this issue Dec 7, 2024 · 3 comments
Open

Provide an option for a simple wrapper count query #3706

edudar-chwy opened this issue Dec 7, 2024 · 3 comments
Assignees
Labels
for: team-attention An issue we need to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged

Comments

@edudar-chwy
Copy link

I have many complex native queries where Spring Data can't figure out count queries out of the box. Think of anything involving SELECT DISTINCT. My queries return correct results, but the total count is incorrect when using pagination. The total result is 300 records, but the count returns 650 due to duplicated records without DISTINCT, which the count query creator strips out.

An obvious solution is to provide a custom count query. It works. But it also makes code hardly maintainable because I have to duplicate page-sized queries and just make them double: value and countQuery. I suggest a simple wrapper option that will take a value and create a count query like

SELECT COUNT(dt.*) FROM (<value_query>) AS dt

It won't solve all issues for everyone, but at least it will allow users to not repeat themselves for 100x of queries.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 7, 2024
@mp911de mp911de self-assigned this Dec 9, 2024
@mp911de mp911de added the for: team-attention An issue we need to discuss as a team to make progress label Dec 10, 2024
@mp911de
Copy link
Member

mp911de commented Dec 10, 2024

Paging @schauder. I am not sure how much trouble we create when using subqueries as Spring Data JPA is used with all sorts of database systems, even those that pretend to support SQL, although their backend is a NoSQL database. Using top-level count queries is less invasive.

Our own query rewriter is based on Rexex and has limitations when it comes to more complex queries. We recommend using the JSqlParser-based rewriter (see #3707) or manually-declared queries as alternatives.

@quaff
Copy link
Contributor

quaff commented Dec 12, 2024

I suggest a simple wrapper option that will take a value and create a count query like

Or introduce simpleCount attribute, then you can write @Query(value = "SELECT DISTINCT ... ", nativeQuery = true, simpleCount = true), it will use SELECT COUNT(T.*) FROM (SELECT DISTINCT ...) AS T as count query.

@mp911de
Copy link
Member

mp911de commented Dec 12, 2024

We're exploring a different approach that lets folks select their QueryEnhancer via #3527. It is a much more capable approach because folks can select query enhancers for particular queries and can control how to derive a count query from a given native query.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: team-attention An issue we need to discuss as a team to make progress status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

No branches or pull requests

4 participants