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
where-query returning wrong result if its expression is not assigned to a variable #14596
I think the problem here can be demonstrated without even executing the query. The criteria associated with the DetachedCriteria is empty in the second case.
@Transactionalvoid"test query by association is null"() {
newFoo(bar: newBar()).save()
when:
def q =Foo.where { bar ==null }
then:
q.criteria.size() ==1
q.criteria[0] instanceofQuery.IsNulland:
// this fails because the criteria is emptyFoo.where { bar ==null }.criteria.size() ==1
}
The empty criteria explains why the query is returning all of the Foo.
thanks @jeffbrown for confirmation.
and no, this is not only happening in a spec. we observed this behavior in a live grails app (service method return statement):
packageissue1026classPersonController {
PersonService personService
defwithFirstName() {
def count = personService.countPeopleThatHaveAFirstName()
render "There are $count people with a first name."
}
defwithNoFirstName() {
def count = personService.countPeopleWithNoFirstName()
render "There are $count people with no first name."
}
}
packageissue1026importgeb.spock.GebSpecimportgrails.testing.mixin.integration.Integration@IntegrationclassPersonSpecextendsGebSpec {
void"test retriving people with a first name"() {
when:
go '/person/withFirstName'then:
$().text() =='There are 3 people with a first name.'
}
void"test retriving people without a first name"() {
when:
go '/person/withNoFirstName'then:
$().text() =='There are 0 people with no first name.'
}
}
i tried debugging into the DetachedCriteriaTransformer by adding this to the different versions of jeffs sample app
(BootStrap.init, breakpoints at transformTestHelper.parse and inside DetachedCriteriaTransformer):
dont know what other possibilities there are to use where queries which are not properly transformed.
i have to express that i am deeply worried about this kind of issue appearing. it produces wrong results out of code that is properly written, compiled without errors and seemingly runs just fine. no way for regular framework "end-users" to get whats going on.
unfortunately, debugging ast transformations (and if its just to get to the point of understanding what happens) is overly complicated. abstraction and ease-of-use is all fine but i think it should not lead to a blackbox for users.
This was reported against GORM 6.1.7 / Grails 3.3.1. Since then, the where-query AST transformation (DetachedCriteriaTransformer) has received several fixes in Grails 7.x that address variable handling and implicit-this resolution:
4299a24 - fix: set implicitThis=false on delegate MethodCallExpressions in DetachedCriteriaTransformer
2e3f6a8 - fix: where query composition fails when variable is re-assigned in if/else blocks
dee7d11 - fix: update variable type for constructor case in visitBinaryExpression
These changes significantly reworked how the transformer handles inline vs. variable-assigned where expressions.
If you are still experiencing this issue on Grails 7.x, please reopen with a minimal reproducer against the current version. Otherwise, we believe this has been addressed by the fixes above.
Thanks for the report and the detailed AST analysis. On current 8.0.x, the reported static Domain.where { property == null }.count() form now matches the variable-assigned form for both empty and matching result sets. The exact case is covered by WhereQueryOldIssueVerificationSpec with @Issue(#14596); the related transformer fixes landed on February 24, 2026 and the verification merged through PR #15460 on February 27, 2026. This closure is limited to the reported static Domain.where case; constructor and method-return receivers remain separate, unverified paths. Please reopen with a minimal current reproducer if the supported static form still differs.
Activity
sample spec reproducing grails/grails-data-mapping#1026
jeffscottbrown commentedon Nov 18, 2017
I expect that only happens in a Spec. Is that correct?
jeffscottbrown commentedon Nov 18, 2017
I think the problem here can be demonstrated without even executing the query. The
criteriaassociated with theDetachedCriteriais empty in the second case.The empty criteria explains why the query is returning all of the
Foo.zyro23 commentedon Nov 18, 2017
thanks @jeffbrown for confirmation.
and no, this is not only happening in a spec. we observed this behavior in a live grails app (service method return statement):
jeffscottbrown commentedon Nov 18, 2017
Thanks for the feedback.
https://github.com/grails/grails-data-mapping/issues/1026
jeffscottbrown commentedon Nov 18, 2017
The project at https://github.com/jeffbrown/issue1026 contains the following:
https://github.com/jeffbrown/issue1026/blob/master/grails-app/domain/issue1026/Person.groovy
https://github.com/jeffbrown/issue1026/blob/master/grails-app/init/issue1026/BootStrap.groovy
https://github.com/jeffbrown/issue1026/blob/master/grails-app/services/issue1026/PersonService.groovy
https://github.com/jeffbrown/issue1026/blob/master/grails-app/controllers/issue1026/PersonController.groovy
https://github.com/jeffbrown/issue1026/blob/master/src/integration-test/groovy/issue1026/PersonSpec.groovy
That test passes.
making jeffs sample app for grails/grails-data-mapping#1026 fail ;)
jeffscottbrown commentedon Nov 18, 2017
I see the change at zyro23/issue1026@4d367b3...
I think that would fail even if the result was assigned to a variable. I think all of the following would fail:
jeffscottbrown commentedon Nov 18, 2017
I think this is a more simple way to demonstrate the problematic behavior:
jeffscottbrown commentedon Nov 18, 2017
The change at jeffscottbrown/issue1026@79cecb0 causes one of the tests to fail.
zyro23 commentedon Nov 19, 2017
i tried debugging into the
DetachedCriteriaTransformerby adding this to the different versions of jeffs sample app(
BootStrap.init, breakpoints attransformTestHelper.parseand insideDetachedCriteriaTransformer):in this example:
it seems like the where closure is not transformed because the
objectExpressionit is called on is aConstructorCallExpressionwhich seems to be not handled by the transformer.ref. the if/elses here https://github.com/grails/grails-data-mapping/blob/v6.1.7/grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformer.java#L309
and in this example:
it also looks like the added/appended where closure is not transformed because this time, the
objectExpressionis aMethodCallExpression. but thetargetTypeisthis(i.e.PersonService), which is not a domain class.ref. https://github.com/grails/grails-data-mapping/blob/v6.1.7/grails-datastore-gorm/src/main/groovy/org/grails/datastore/gorm/query/transform/DetachedCriteriaTransformer.java#L358
dont know what other possibilities there are to use where queries which are not properly transformed.
i have to express that i am deeply worried about this kind of issue appearing. it produces wrong results out of code that is properly written, compiled without errors and seemingly runs just fine. no way for regular framework "end-users" to get whats going on.
unfortunately, debugging ast transformations (and if its just to get to the point of understanding what happens) is overly complicated. abstraction and ease-of-use is all fine but i think it should not lead to a blackbox for users.
grails/grails-data-mapping#1026 - using DetachedCriteria methods inst…
jeffscottbrown commentedon Nov 19, 2017
Thank you for the feedback.
jamesfredley commentedon Feb 26, 2026
This was reported against GORM 6.1.7 / Grails 3.3.1. Since then, the where-query AST transformation (
DetachedCriteriaTransformer) has received several fixes in Grails 7.x that address variable handling and implicit-this resolution:4299a24- fix: setimplicitThis=falseon delegate MethodCallExpressions in DetachedCriteriaTransformer2e3f6a8- fix: where query composition fails when variable is re-assigned in if/else blocksdee7d11- fix: update variable type for constructor case in visitBinaryExpressionThese changes significantly reworked how the transformer handles inline vs. variable-assigned where expressions.
If you are still experiencing this issue on Grails 7.x, please reopen with a minimal reproducer against the current version. Otherwise, we believe this has been addressed by the fixes above.
jamesfredley commentedon Jul 24, 2026
Thanks for the report and the detailed AST analysis. On current
8.0.x, the reported staticDomain.where { property == null }.count()form now matches the variable-assigned form for both empty and matching result sets. The exact case is covered byWhereQueryOldIssueVerificationSpecwith@Issue(#14596); the related transformer fixes landed on February 24, 2026 and the verification merged through PR #15460 on February 27, 2026. This closure is limited to the reported staticDomain.wherecase; constructor and method-return receivers remain separate, unverified paths. Please reopen with a minimal current reproducer if the supported static form still differs.