Repository navigation
fix: resolve three where-query bugs (#14344, #14480, #14569) - #15460
Conversation
…he#14569) - Add size() query handlers to JpaQueryBuilder for deleteAll/updateAll (apache#14344) - Add missing applyDetachedCriteria call in CountByFinder (apache#14480) - Fix DetachedCriteria.count() returning wrong value with projections (apache#14569) - Add verification test spec covering 8 old where-query issues Fixes apache#14344 Fixes apache#14480 Fixes apache#14569 Assisted-by: Claude Code <[email protected]>
There was a problem hiding this comment.
Pull request overview
Fixes multiple long-standing GORM where-query inconsistencies across the JPA query builder, dynamic finders, and DetachedCriteria.count(), and adds a consolidated regression/verification spec for historical where-query issues.
Changes:
- Add missing
size()comparison criterion handlers toJpaQueryBuildersodeleteAll()/updateAll()can support size-based where queries. - Fix
countBy*dynamic finder behavior to correctly apply detached criteria (where clause) during query building. - Adjust
DetachedCriteria.count()behavior when user-defined projections exist to avoid returning incorrect values.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/query/jpa/JpaQueryBuilder.java |
Registers size-comparison query handlers and implements SIZE(...) predicate generation. |
grails-datamapping-core/src/main/groovy/org/grails/datastore/gorm/finders/CountByFinder.java |
Ensures detached criteria are applied so countBy* honors where-query restrictions. |
grails-datamapping-core/src/main/groovy/grails/gorm/DetachedCriteria.groovy |
Changes count() behavior under projections to avoid incorrect counts. |
grails-data-hibernate5/core/src/test/groovy/grails/gorm/tests/WhereQueryOldIssueVerificationSpec.groovy |
Adds a verification spec covering multiple historical where-query issues. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
I was very careful not to touch data mapping level because it could have effects on the other implementations. I would suggest making changes for 7.0 on the hibernate level and making changes on the data mapper level on the 8.0 branch to ensure the hibernate layer does not get affected. |
|
The more I've thought about this, if this can't be supported by hibernate and it's not been supported by Grails historically, then this is really a feature request and not a bug request. We should fix this in Hibernate 7 (sounds like it already is) and clean up the error handling in Grails 7 to indicate it's not supported. |
Remove unused DetachedCriteria and Transactional imports from the verification test spec. Assisted-by: Claude Code <[email protected]>
Assisted-by: Claude Code <[email protected]>
When user-defined projections exist, count() falls back to list().size() due to a Hibernate 5 limitation. Log a warning so developers are aware of the potential performance and memory implications. Assisted-by: Claude Code <[email protected]>
|
The following comment and warning have been added. |
added a comment and log.warn() when the "correct" but slow solution executes.
Summary
Fixes three long-standing where-query bugs verified through reproduction tests against the current 7.0.x codebase.
Reproducer app: https://github.com/jamesfredley/grails-where-query-bugs-pr1
Bug 1:
size()not supported inJpaQueryBuilder(#14344)Symptom: Using
size()in a where query that goes through the JPA path (deleteAll(),updateAll()) throws:The Hibernate native path (
list(),get()) handlessize()correctly viaAbstractHibernateCriterionAdapter, but the JPA path inJpaQueryBuilderhad no handlers for size criteria.Broken pattern:
Root cause:
JpaQueryBuilder.queryHandlersmap was missing entries forQuery.SizeEquals,SizeNotEquals,SizeGreaterThan,SizeGreaterThanEquals,SizeLessThan, andSizeLessThanEquals. These criterion types were never registered, so any size comparison hit the "not supported" fallback.Fix: Register 6 size comparison query handlers in
JpaQueryBuilderthat generateSIZE(alias.property) <op> ?NJPQL, plus a sharedhandleSizeComparison()helper:Bug 2:
countBydynamic finder drops where clause (#14480)Symptom: When calling
countByX()on aDetachedCriteriabuilt with.where{}, the where clause criteria are silently dropped. The count returns the total for the dynamic finder match, ignoring the where filter entirely.Broken pattern:
Root cause:
CountByFinder.buildQuery()was missing theapplyDetachedCriteria()call. Compare withFindAllByFinderwhich correctly includes it:Fix: One-line addition in
CountByFinder.buildQuery():Bug 3:
DetachedCriteria.count()returns wrong value with projections (#14569)Symptom: When a
DetachedCriteriahas user-defined projections (e.g.,groupProperty+count()), calling.count()returns the wrong value - typically the count from the first grouped row instead of the number of result rows.Broken pattern:
Root cause:
DetachedCriteria.count()callswithPopulatedQuery()then appendsquery.projections().count()alongside the existing user projections. The generated SQL ends up as:Fix: When user projections already exist,
count()falls back tolist().size()to get the correct row count:The single-argument
count(Closure)overload was also updated to delegate to the main method instead of duplicating the (now-buggy) original logic.Changes
JpaQueryBuilder.javagrails-datastore-corehandleSizeComparison()helperCountByFinder.javagrails-datamapping-coreapplyDetachedCriteria(q, invocation.getDetachedCriteria())DetachedCriteria.groovygrails-datamapping-corecount()falls back tolist().size()WhereQueryOldIssueVerificationSpec.groovygrails-data-hibernate5-coreVerification
The new
WhereQueryOldIssueVerificationSpeccovers all 8 issues from the open where-query triage:@PendingFeature)All existing tests pass:
grails-data-hibernate5-core(585 tests),grails-datamapping-core,grails-datastore-core.Fixes #14344
Fixes #14480
Fixes #14569