Skip to content

Run an aggregate on the right of a data service @Where comparison as a subquery - #17153

Open
borinquenkid wants to merge 4 commits into
8.0.xfrom
fix/service-where-aggregate-8.0.x
Open

borinquenkid wants to merge 4 commits into
8.0.xfrom
fix/service-where-aggregate-8.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

Fixes #16807.

Replaces #16835, whose base was the branch of a closed pull request and so carried 68 commits that are not part of this change. This is the one commit of it that belongs on 8.0.x, on a clean branch.

Problem

amount > avg(amount) in the @Where closure of a data service was translated into a bare closure, which the enclosing criteria resolved its projections call against. The aggregate then landed on the outer query and the subquery lost it, so every such method failed with SQLGrammarException on Hibernate 5.

Change

DetachedCriteriaTransformer builds the aggregate as an explicit DetachedCriteria subquery, as the .of { } form already was. No public API is added.

Tests

WhereServiceAggregateSpec runs the comparisons with avg, >= avg, max and min through a data service and through the same where query, and compares the rows. All four fail without the change and pass with it. grails-datamapping-core (1831 tests) and the Hibernate 5 module (1221) pass, and so does codeStyle.

Not in this change

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

 comparison as a subquery

amount > avg(amount) in a @where closure of a data service was translated into a bare closure that the
enclosing criteria resolved its projections calls against, so the aggregate landed on the outer query and the
subquery lost it. The aggregate is now built as an explicit DetachedCriteria subquery, as the .of { } form
already was. WhereServiceAggregateSpec runs the comparisons with avg, max and min on Hibernate 5 and checks them
against the same where queries.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

sbglasius
sbglasius previously approved these changes Nov 24, 2026

@sbglasius sbglasius left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an AI assisted review

LGTM.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An average inside an embedded block throws instead of comparing the values.

// declared in a service (@Where), and the aggregate then lands on the outer query.
rightExpression = new MethodCallExpression(
new ConstructorCallExpression(getParameterizedDetachedCriteriaClassNode(null),
new ArgumentListExpression(new ClassExpression(this.currentClassNode))),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stats { qty > avg(qty) } should return the reading whose quantity is above the average. It throws, because the average is built against the embedded type as if it were its own class. Build the average against the reading.

@borinquenkid
borinquenkid requested review from jamesfredley, lhotari and sdelamo and removed request for lhotari and sdelamo November 25, 2026 04:39
@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed d862d7f. You are right that stats { qty > avg(qty) } threw: the subquery was built over the class of the block, and the class of an embedded value is not an entity. I tried to build it over the owner of the value and found that it cannot be done here, so the shape is refused when the query is compiled instead of failing when it runs.

What I found, in a throwaway spec with the owner as the subquery's entity and the path stats.qty as the projection:

  • On Hibernate 5 the transformer then built the query, and it failed one step later: a subquery used as the value of a comparison inside the block of an embedded value is bound as a plain parameter, ClassCastException: DetachedCriteria cannot be cast to Integer. That is in how the Hibernate 5 query builder translates the block, not in the transformer, and Hibernate 5 is the frozen store.
  • On Hibernate 7 the data service @Where with an aggregate fails even for a plain property of the entity (Cannot coerce value 'grails.gorm.DetachedCriteria'), so the embedded case cannot be tested there until that is fixed; the spec of this pull request runs on Hibernate 5 only.

So DetachedCriteriaTransformer, line 1268 now adds a compile error, naming the function and the embedded property, for an aggregate inside the block of an embedded property (the tracking of the block, line 840); an aggregate outside such a block is as it was. The test: WhereQueryAggregateSubqueryErrorSpec line 132, the refusal with the message, and line 162, an aggregate beside an embedded property that still compiles; the refusal row fails without the change. The upgrade note is upgrading80x.adoc line 5036. grails-datamapping-core, grails-data-hibernate5-core, grails-data-mongodb-core and grails-data-hibernate7-core (they compile where closures) pass and codeStyle is clean.

If you want it to work and not be refused, the two stores need their own changes first; I can open an issue for each with the failing query. Nothing in the guide describes an aggregate inside an embedded block; the upgrade note is the only text.

@github-actions
github-actions Bot dismissed sbglasius’s stale review November 25, 2026 15:22

Dismissed automatically: the pull request changed after this approval, from d908a35 to d862d7f.

@borinquenkid

Copy link
Copy Markdown
Member Author

Pushed aaba3bc, which merges 8.0.x into the branch: the pull request conflicted with it in upgrading80x.adoc only, where this note and the cache-manager note landed at the same place. Both are kept, this one first. No other change.

This branch has not been deployed

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

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

Data Services: Fix @Where aggregate subquery translation generating invalid SQL

4 participants