Skip to content

Grails where count gives incorrect results with projection #14569

Closed

Description

@monetschemist

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.

Steps to Reproduce

This query

def c = gtest01.TestA.where {
    projections {
        groupProperty 'grouping'
        count()
    }
}
c.list().each { t ->
    println "t $t"
}
println c.count()

produces the following result:

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.

Environment Information

Example Application

https://github.com/monetschemist/grails-where-count-problem

Activity

  1. monetschemist commented on Jul 17, 2018

    @monetschemist
    Author

    Just wondering if anyone has any thoughts on this problem?

    Thanks in advance.

  2. tircnf commented on May 24, 2021

    @tircnf

    @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. :(

  3. monetschemist commented on May 25, 2021

    @monetschemist
    Author

    @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.

  4. jamesfredley commented on Feb 26, 2026

    @jamesfredley
    Contributor

    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.

  5. jamesfredley commented on Feb 26, 2026

    @jamesfredley
    Contributor

    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.

  6. added a commit that references this issue on Feb 27, 2026
  7. monetschemist commented on Feb 28, 2026

    @monetschemist
    Author

    @jamesfredley and everyone, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions