blaze-persistence: group by + having count + DISTINCT does not work
Description
Calling following:
query
.distinct()
.where(field.in(values))
.groupBy(id)
.groupBy(groupByField)
.having(field.countDistinct().goe(1))
does not returns same results as generated sql from same code does.
Expected behavior
Expected is to get actual response that you get when you manually execute generated query.
Actual behavior
- In query dsl 4.x version - response is empty list.
- In query dsl 5.x - there is an exception:
java.lang.NoSuchFieldError: hints at com.blazebit.persistence.querydsl.AbstractBlazeJPAQuery.getQueryable(AbstractBlazeJPAQuery.java:234)
Steps to reproduce
When executing following:
query
.distinct()
.where(field.in(values))
.groupBy(id)
.groupBy(groupByField)
.having(field.count().goe(1))
Generated query when inspecting is:
select distinct entity
where field in ( ... )
group by id, groupByField
having count(field) >= 1
and there are returned results, everything works as expected.
When calling same code from above, but with field.countDistinct(), there are no results returned, empty list. When I inspect and get generated query, I can execute it raw and I actually get results, as query looks like it should and returns what is should return.
query
.distinct()
.where(field.in(values))
.groupBy(id)
.groupBy(groupByField)
.having(field.countDistinct().goe(1))
Generated query when inspecting :
select distinct entity
where field in ( ... )
group by id, groupByField
having count(distinct field) >= 1
This query returns results, but blaze itself returns empty list, or exception, depending on version.
Environment
Version: 1.6.0 (blaze-persistence-core-api, blaze-persistence-core-impl, blaze-persistence-integration-hibernate-5.4, blaze-persistence-integration-querydsl-expressions)
JPA-Provider: Hibernate 5.4.23
DBMS: MySql 8.x
Application Server: Tomcat
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 32 (13 by maintainers)
That ‘distinct’ in select is there as query.select()…distinct() is called.
I just tested again with simple left join of two entities, and everything works as expected! Thank you @beikov @jwgmeligmeyling !
When looking at one case that doesn’t work, that one is working with projections as return type, not pure entity, and that is only difference, so count value in that case seems to be 0 as it’s not matching >= 1 condition and thats why response is empty (what that can be?)
But for working part, now I’m not sure does it work because of update, or I just did not test simple case and it was working all along. I’ll do more testing and get back with answers.