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

PanacheQuery count method ignoring distinct after using project #38033

Open
TigasArreis opened this issue Jan 4, 2024 · 6 comments · May be fixed by #38316
Open

PanacheQuery count method ignoring distinct after using project #38033

TigasArreis opened this issue Jan 4, 2024 · 6 comments · May be fixed by #38316
Labels
area/panache kind/bug Something isn't working

Comments

@TigasArreis
Copy link

Describe the bug

I have a query that uses distinct on the select
select distinct entity.id from Entity entity...

I want to use this query in a EntityRepository from where I want to return a PanacheQuery<Long> instead of a PanacheQuery<Entity>
When doing the following code:

String query = "...";
PanacheQuery pQuery= (PanacheQuery) find(query);
pQuery.count();

This will return a correct count value because it generates the following queries:

select distinct entity.id from ...
select count(distinct entity.id) from ...

However when applying .project (so I can have a PanacheQuery<Long> and not use unchecked raw types)

String query = "...";
PanacheQuery<Long> pQuery= find(query).project(Long.class);
pQuery.count();

It will remove the distinct from the count

select distinct entity.id from...
select count(*) from...

And not returning the same .count() value

Expected behavior

After applying .project() to a generic PanacheQuery the .count() value should remain the same after the projection

Actual behavior

The .count() value changes because the distinct from the select clause is being removed after applying .project()

How to Reproduce?

No response

Output of uname -a or ver

No response

Output of java -version

17.0.7

Quarkus version or git rev

3.2.2.Final

Build tool (ie. output of mvnw --version or gradlew --version)

No response

Additional information

As a workarround it's possible to create a EntityIdRepository implements PanacheRepository<Long> to avoid the unchecked raw types

@TigasArreis TigasArreis added the kind/bug Something isn't working label Jan 4, 2024
Copy link

quarkus-bot bot commented Jan 4, 2024

/cc @FroMage (panache), @loicmathieu (panache)

@TigasArreis
Copy link
Author

Issue is in io.quarkus.hibernate.orm.panache.common.runtime.CommonPanacheQueryImpl arround line 121

return new CommonPanacheQueryImpl<>(this, newQuery.toString(), "select count(*) " + from);

Should not have "select count(*) " but instead the select clause that actually is being considered on the previous lines

boolean distinctQuery = selectClause.toLowerCase().startsWith("distinct ");
if (distinctQuery) {
    // 9 is the length of "distinct "
    selectClause = selectClause.substring(9).trim();
    newQuery.append("distinct ");
}

newQuery.append("new ").append(type.getName()).append("(").append(selectClause).append(")").append(from);

@rysurd
Copy link
Contributor

rysurd commented Jan 16, 2024

Hello, I can have a look if no one is currently on this.

@rysurd
Copy link
Contributor

rysurd commented Jan 20, 2024

Hello again @TigasArreis, I have opened a PR here : #38316
Feel free to test if it fixes well your issue ! I have reproduced your issue with a local postgres db and tested the fix as well.

There are some errors in the build, I'll have a look at them btw.

@TigasArreis
Copy link
Author

Hello @rysurd
Yes, this fix my issue (also in postgres)
Thank you

@rysurd
Copy link
Contributor

rysurd commented Jan 23, 2024

Thanks for testing!! I'll try to finish up the PR this week so it can be merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/panache kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants