Repository navigation
Run an aggregate on the right of a data service @Where comparison as a subquery (7.2.x) - #17176
borinquenkid wants to merge 2 commits into
Conversation
comparison as a subquery A data service is compiled statically. There the aggregate subquery of `qty > avg(qty)` lost its projection: the closure that added it resolved `projections` to the enclosing criteria, and the generated call on the new criteria, implicitly on `this`, was compiled as a call on the closure's owner. The transformer now adds avg, max, min and sum through the criteria's own methods, adds countDistinct in a closure that calls projections on the criteria it is given, and marks those calls as not on `this`. No public API changes. Fixes #16807 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. An aggregate built from a method that names the base class is run against the base class, not the subclass that was passed.
| private MethodCallExpression newSubquery(String methodName, ArgumentListExpression arguments) { | ||
| MethodCallExpression call = new MethodCallExpression( | ||
| new ConstructorCallExpression(getParameterizedDetachedCriteriaClassNode(null), | ||
| new ArgumentListExpression(new ClassExpression(this.currentClassNode))), |
There was a problem hiding this comment.
A method takes a criteria of the base class and compares a value with the average. A subclass criteria is passed in. The average should be of the subclass. It is of the base class, so a subclass row that is above its own average is left out. Build the average against the criteria that was passed.
…the criteria it runs on The previous commit built the subquery of avg, max, min and sum as a new criteria of the class named where the query is written. A method that takes a criteria of the base class and is passed a subclass criteria then compared with the aggregate of the base class (review on #17176). Every aggregate is now a closure that takes the subquery as its parameter, as count already was. The criteria it is passed to builds the subquery when the query runs, of its own class. The aggregate is called on the projection list by name, because a statically compiled closure, such as the @where of a data service, does not send a call without a receiver to its delegate. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley You're right, thanks. My first commit built the subquery as a new criteria of the class named where the query is written, so a criteria of the subclass got the base class aggregate. Fixed in 2b3a170.
|
✅ All tests passed ✅🏷️ Commit: 2b3a170 Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
The items this review found have been addressed. The average is built against the criteria that was passed.
|
Moved to 7.0.x as #17275: the bug is also on 7.0.x, the oldest branch with it. The change is rebuilt there on a new branch. It also fixes two things this branch still has: a where query in a @transactional method with a variable named delegate throws a GroovyCastException, and with the in-memory datastore a data service method with avg throws on an entity with no instances. The review here does not carry over. Closing this one; its review history stays here. |
Fixes #16807 on 7.2.x. The 8.0.x fix is #17153.
Problem
qty > avg(qty)in the@Whereclosure of a data service failed on 7.x: with anSQLGrammarException(select from wsa_reading, the subquery lost its projection), and with the obvious repair, with aGroovyCastException. A data service is compiled statically, and two things went wrong there:{ projections { avg('qty') } }. Under@CompileStatic, the calls of a criteria closure nested in another one resolve to the enclosing criteria, so the projection landed on the outer query. 8.0.x fixed that withDELEGATE_FIRSTon the@DelegatesToof the criteria API (Nested criteria subqueries bind to the outer criteria under @CompileStatic (Groovy 4) #16425), which changes public signatures and the compile-time resolution of user code, so it is not ported to a minor 7.x line.MethodCallExpressionis implicitly onthiswhen created. The dynamic compiler still calls it on its object expression, but the static compiler, in a closure, calls an implicit-this method on the closure's owner. A generatednew DetachedCriteria(Entity).build(...)was therefore compiled as((DetachedCriteria) this).build(...), which is theGroovyCastException.Change
DetachedCriteriaTransformerbuilds the subquery with no criteria closure for the static compiler to misresolve, and marks the generated call as not onthis:avg,max,min,sum:new DetachedCriteria(Entity).avg('qty'), the criteria's own projection methods.count(countDistinct, which has no such method):new DetachedCriteria(Entity).build { DetachedCriteria c -> c.projections { countDistinct('qty') } }.buildcalls the closure with the new criteria, soprojectionsis called on it explicitly.propertykeeps its closure and its...Allsubquery methods.No public API changes. A
wherequery takes the same path in its subquery (gt(String, DetachedCriteria)instead ofgt(String, Closure), which builds that criteria) and gives the same rows.Tests
WhereServiceAggregateSpec(Hibernate 5) runs> avg,>= avg,== max,== min,< sumand<= countthrough a data service and through the samewherequery, and compares the rows. With the transformer of 7.2.x, the four cases of #17153 fail (4 of 4); with this change all six pass.grails-datamapping-core,grails-datamapping-core-test(which holds thewhereaggregate cases ofWhereMethodSpec), the Hibernate 5 module and the MongoDB module pass, and so doescodeStyle. The upgrade note is in the Grails 7.2.6 section.Not in this change
avg(qty).of { ... }in a data service: the block is a user-written criteria closure nested in another one, so it still resolves to the enclosing criteria under@CompileStatic. That is the general Nested criteria subqueries bind to the outer criteria under @CompileStatic (Groovy 4) #16425 behaviour, fixed on 8.0.x, and it needs the signature change above. In awherequery, which is not compiled statically,.of { }works as before.https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL