You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Grails where count gives incorrect results with projection #14569
The count() method of detached criteria / where gives incorrect results when a projection is used in the where {}. In fact it appears that detachedCriteria.count() returns the same value as the first returned projection count.
t [1, 12]
t [2, 16]
t [3, 9]
t [4, 18]
t [5, 5]
t [6, 20]
t [7, 13]
t [8, 7]
12
Note that there are 8 results returned, not 12 as indicated by count(); and that the value returned by count is the same as the value of count() in the first result from the projection (this is consistent across three separate cases I have tried).
Expected Behaviour
As noted above, detachedCriteria.count() in the above example should return 8.
Actual Behaviour
Instead, detachedCriteria.count() in the above example returns 12, which appears to be the same value as the count returned from the first projection / grouping result.
@monetschemist -- Just ran into this same problem today. We were trying to have a pageable search and use the same detachedCriteria for both the list and the count, but it breaks when our list method has a projection.
If you examine the sql that gets created when using .count() you can see that it just adds an additional count(*) to the select list.
That is why you are seeing those results. the count() method is just grabbing the first row.
We finally just had to create two different criterias. One for fetching the results and one for counting. It is really easy to mess things up though. :(
@tircnf wow so wonderful to run into someone else trying to do complicated shzt and struggling! I wonder if we could exchange some stories to mutual benefit? I'd love to put good info here if anyone wants to listen.
We have verified that this issue still exists in Grails 7.x / GORM 8.x.
A reproduction test confirms that DetachedCriteria.count() returns the wrong result when projections (e.g. groupProperty + count()) are present. Specifically, count() returns the value of the first projection row's count (e.g. 12) instead of the actual number of grouped results (e.g. 5).
Fix submitted in PR #15460 - when user-defined projections (e.g. groupProperty + count()) exist on a DetachedCriteria, count() now falls back to list().size() instead of appending a row count projection to the existing projection list. This correctly returns the number of result rows (groups) instead of the first projection row's count value.
Activity
monetschemist commentedon Jul 17, 2018
Just wondering if anyone has any thoughts on this problem?
Thanks in advance.
tircnf commentedon May 24, 2021
@monetschemist -- Just ran into this same problem today. We were trying to have a pageable search and use the same detachedCriteria for both the list and the count, but it breaks when our list method has a projection.
If you examine the sql that gets created when using .count() you can see that it just adds an additional count(*) to the select list.
That is why you are seeing those results. the count() method is just grabbing the first row.
We finally just had to create two different criterias. One for fetching the results and one for counting. It is really easy to mess things up though. :(
monetschemist commentedon May 25, 2021
@tircnf wow so wonderful to run into someone else trying to do complicated shzt and struggling! I wonder if we could exchange some stories to mutual benefit? I'd love to put good info here if anyone wants to listen.
jamesfredley commentedon Feb 26, 2026
We have verified that this issue still exists in Grails 7.x / GORM 8.x.
A reproduction test confirms that
DetachedCriteria.count()returns the wrong result when projections (e.g.groupProperty+count()) are present. Specifically,count()returns the value of the first projection row's count (e.g. 12) instead of the actual number of grouped results (e.g. 5).This will be addressed in an upcoming fix.
fix: resolve three where-query bugs (apache#14344, apache#14480, apac…
jamesfredley commentedon Feb 26, 2026
Fix submitted in PR #15460 - when user-defined projections (e.g.
groupProperty+count()) exist on a DetachedCriteria,count()now falls back tolist().size()instead of appending a row count projection to the existing projection list. This correctly returns the number of result rows (groups) instead of the first projection row's count value.Merge pull request #15460 from jamesfredley/fix/where-query-bugs
monetschemist commentedon Feb 28, 2026
@jamesfredley and everyone, thank you so much!