question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

group by + having count + DISTINCT does not work

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:32 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
alohaalohacommented, Oct 23, 2021

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.

0reactions
beikovcommented, Feb 22, 2022

@alohaaloha: If you can provide us a reproducer or at least more details like the full entity model and actual executable code we can look into this more deeply, but as it is right now, we just couldn’t reproduce your issue, so I’m going to close the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Server how can I use COUNT DISTINCT(*) in HAVING ...
When outside of the COUNT , the DISTINCT will eliminate duplicate rows from a result set, which will have no effect in your...
Read more >
SQL COUNT() with distinct - w3resource
SQL COUNT () function with DISTINCT clause eliminates the repetitive appearance of the same data. The DISTINCT can come only once in a...
Read more >
Advanced Group Operators [DISTINCT, UNIQUE, HAVING]
UNIQUE clause is a group function that does not require you to use the GROUP BY clause. Add this to a query and...
Read more >
Overview of the SQL Count Distinct Function - SQLShack
SQL COUNT Distinct does not eliminate duplicate and NULL values from the result set. SQL Count Distinct function.
Read more >
SQL COUNT DISTINCT vs. DISTINCT - PDQ
COUNT() with the DISTINCT clause removes duplicate rows of the same data in the result set. It also removes 'NULL' values in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found