Repository navigation
Run an aggregate on the right of a data service @Where comparison as a subquery - #17153
borinquenkid wants to merge 4 commits into
Conversation
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
sbglasius
left a comment
There was a problem hiding this comment.
This is an AI assisted review
LGTM.
jamesfredley
left a comment
There was a problem hiding this comment.
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))), |
There was a problem hiding this comment.
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.
…ed property is refused when the query is compiled Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Pushed d862d7f. You are right that What I found, in a throwaway spec with the owner as the subquery's entity and the path
So 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. |
|
Pushed aaba3bc, which merges |
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@Whereclosure of a data service was translated into a bare closure, which the enclosing criteria resolved itsprojectionscall against. The aggregate then landed on the outer query and the subquery lost it, so every such method failed withSQLGrammarExceptionon Hibernate 5.Change
DetachedCriteriaTransformerbuilds the aggregate as an explicitDetachedCriteriasubquery, as the.of { }form already was. No public API is added.Tests
WhereServiceAggregateSpecruns the comparisons withavg,>= avg,maxandminthrough a data service and through the samewherequery, 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 doescodeStyle.Not in this change
GroovyCastException, because 7.0.x lacks the scope recompute of Nested criteria subqueries bind to the outer criteria under @CompileStatic (Groovy 4) #16425 andDELEGATE_FIRSTinprojections. Porting those is a larger change; on 7.0.x the workaround is the.of { }form.getTargetClass()it needed: that is public API and waits for the 8.1.x pull request after fix(hibernate7): support where-query functions and refactor PredicateGenerator #16816.gt('qty', { projections { avg('qty') } }), fails the same way with or without this change: it does not run itsprojectionscall on its own subquery.https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL