Skip to content

QueryRewriter not applied to count queries #3801

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

Closed
Chandlerigs opened this issue Mar 5, 2025 · 3 comments
Closed

QueryRewriter not applied to count queries #3801

Chandlerigs opened this issue Mar 5, 2025 · 3 comments
Assignees
Labels
type: bug A general bug

Comments

@Chandlerigs
Copy link

Chandlerigs commented Mar 5, 2025

If there are any areas that do not comply with the specifications when submitting an issue for the first time, please help me point them out

I use Spring Data REST and Spring Data JPA to complete my project.

When I use QueryRewrite to complete dynamic queries, there is an error in pagination.

My test demo link is as follows:
github Chandler : data-rest-test

@NoRepositoryBean
public interface BaseRepository<T, ID> extends JpaRepository<T, ID> {
    @Query(value = "select u from #{#entityName} u where 1 = 1", queryRewriter = DynamicQueryRewriter.class)
    Page<T> findByParams(@Nullable @Param("params") String params, @Nullable Pageable pageable);
}
public class DynamicQueryRewriter implements QueryRewriter {
    @Override
    public String rewrite(String query, Sort sort) {
        return query.replaceAll("1 = 1", " u.id >3 ");
    }
}

From the console, it can be seen that, Query SQLis different from countQuery SQL.

The Query SQL calls rewrite and modifies the where condition, while the countQuery SQL does not change, resulting in a total error

Image

There are 5 records in the database, 2 of which comply with the rule (id>3)
Image
When requesting the parameter size=2, the returned totalElements are 5,

Image

Image

When requesting the parameter size!=2 (eg: 4), The returned totalElements are 2,

Image

Image

Thank you for your reading and assistance

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Mar 5, 2025
@mp911de mp911de self-assigned this Mar 10, 2025
@mp911de
Copy link
Member

mp911de commented Mar 10, 2025

QueryRewriter has been introduced to rewrite entity queries as these are typically the more complex ones. We omitted count query rewriting because that wasn't really in scope. I can see how this is confusing and we should do something about this.

Another aspect to consider is that Hibernate has introduced SelectionQuery.getResultCount() allowing usage of an optimized Hibernate Query without us being required to derive a count query. In such a case, QueryRewriter isn't applied.

@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 19, 2025
@mp911de
Copy link
Member

mp911de commented Mar 19, 2025

QueryRewriter doesn't say that it is only used for entity queries, however, it makes sense to apply rewriting to both, the entity- and the count query. We need to fix that.

@mp911de mp911de changed the title When using queryWriter for pagination queries, the generated CountQuery is different from the Query and returns the error total QueryRewriter not applied to count queries Mar 19, 2025
@mp911de mp911de added this to the 3.4.5 (2024.1.5) milestone Mar 19, 2025
mp911de added a commit that referenced this issue Mar 19, 2025
We now use QueryRewriter to post-process count queries as well. Previously, only the actual result query has been processed.

Closes #3801
mp911de added a commit that referenced this issue Mar 19, 2025
We now use QueryRewriter to post-process count queries as well. Previously, only the actual result query has been processed.

Closes #3801
@mp911de
Copy link
Member

mp911de commented Mar 19, 2025

That's fixed and backported into 3.4.x now.

mp911de added a commit that referenced this issue Apr 14, 2025
We now use QueryRewriter to post-process count queries as well. Previously, only the actual result query has been processed.

Closes #3801
mp911de added a commit that referenced this issue May 13, 2025
We now use QueryRewriter to post-process count queries as well. Previously, only the actual result query has been processed.

Closes #3801
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

3 participants