Repository navigation
Run an aggregate on the right of a data service @Where comparison as a subquery - #16835
borinquenkid wants to merge 2 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: select avg(amount) from t where amount > (select amount from t). The aggregate is now built as an explicit DetachedCriteria subquery, as the .of { } form already was. WhereServiceAggregateScenarioSpec runs it on H5 and H7. Fixes #16807 Claude-Session: https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX
a4a1797 to
d0eae47
Compare
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. An aggregate on the right of a comparison uses the declared type, not the runtime type.
| // 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.
A helper typed as DetachedCriteria of Animal is called with a criteria of Cat, and the query is weight greater than avg(weight). The average includes every animal, not only the cats. Use the criteria that was passed in.
…taken over the criteria that runs it The subquery of weight > avg(weight) was built from the class the code declares for the criteria. A helper typed DetachedCriteria<Animal> and called with the criteria of a Cat therefore averaged every animal, and min and max were taken over every animal as well, so cats were compared with the animals of the other classes. The subquery is built from the class of the criteria the closure runs against, read with the new getTargetClass(), which needs no datastore: a criteria built in memory, as the where specs do, has no persistent entity to ask. It is the declared class wherever the two are the same, as in a data service @where. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Reproduced, and fixed in the new commit. The transformer built the subquery of The Hibernate 7 module (4,631 tests), the Hibernate 5 module (1,184), Branch policy: the new public types and throws that If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot. |
matrei
left a comment
There was a problem hiding this comment.
Reviewed at d076adf (base fix/h7-criteria-query-args-8.0.x, #16823's head branch). This PR's own change is the 9 files GitHub shows, measured from 21573cb, the #16823 commit it last merged; #16823's head is 20510a5 now. The PR is labelled no ci, so CI has not run on it.
What I ran locally:
-
A throwaway spec with a data service whose
@Whereclosures compareqtywithavg,maxandminofqty, the same comparisons as awherequery, and James's helper typedDetachedCriteria<Animal>and given the criteria of aCat. The cats weigh 10, 20 and 30 and the other animals 1, 2 and 3, so the average of the cats is 20 and that of every animal 11:Query Hibernate 5, 7.0.x Hibernate 5, 8.0.x Hibernate 7, 8.0.x this head, Hibernate 5 and 7 data service @Where({ qty > avg(qty) }), and>=,== max,== minand one with an argumentSQLGrammarExceptionSQLGrammarException, with the aggregate in the outerselectQueryArgumentExceptionorClassCastExceptionthe right rows where { qty > avg(qty) }.list()andwhere { qty == max(qty) }the right rows the right rows QueryArgumentExceptionorClassCastExceptionthe right rows the helper with weight > avg(weight), given the cats[30][30]QueryArgumentException[30]the helper with weight == min(weight), given the cats[10][10]ClassCastException[10]the helper with weight > avg(weight).of { weight > 0 }, given the cats[20, 30][20, 30]QueryArgumentException[30]The
SQLGrammarExceptionis H2'sValues of types "INTEGER" and "ROW()" are not comparable. -
This PR's diff applied to 7.0.x and to 8.0.x: it applies without conflicts.
Needs a fix
-
Branch: the fix goes to 7.0.x. The bug is in
DetachedCriteriaTransformeringrails-datamapping-core, and Hibernate 5 has it on 7.0.x: every data service method of the table fails there. By the rule in the dev@ thread, the fix goes to the oldest branch that has the bug and is merged up. Hibernate 7 cannot run an aggregate comparison on 8.0.x at all, in awherequery either, until #16816 is merged, so the Hibernate 7 specs need #16816: they can follow on 8.0.x after the merge-up, on top of #16816. For 7.0.x:- the example-app specs use
ScenarioItemfrom #16816. A data service spec ingrails-data-hibernate5-core, built withdatastore.getService(...)asDataServiceSpecdoes, or a domain class of the Hibernate 5 app's own, would do. WhereAggregateHelperSpecextendsHibernateGormDatastoreSpec, which the Hibernate 5 tests on 7.0.x do not have.
- the example-app specs use
-
getTargetClass()is new public API on the patch line, and compiled applications would call it. The transformer writes the call into every query with an aggregate on the right of a comparison, so application classes compiled against the release call it, and it cannot be removed without breaking them. The patch rule asks for no new public methods on 7.0.x or 8.0.x. Outside a data service, the bare form already takes the aggregate over the criteria the closure runs against on 7.0.x: the helper gives[30]there, becauseAbstractDetachedCriteria.addturns a closure value intonew DetachedCriteria(targetClass).build(closure). What fails in a data service is theprojectionscall that the transformer adds inside that closure: it does not reach the subquery's criteria. On 8.0.x it lands on the enclosing query (the outerselect avg(...)), and on 7.0.x the subquery returns whole rows. A fix that keeps the bare closure and gets that call to the subquery's criteria would need no new method. I tried calling it on the closure'sdelegate: that gives the right rows everywhere but in the data service, wheredelegateis the service implementation, so I do not have a version to suggest. If there is no way without the method, please say in the description why the patch line needs it. -
The
.of { }form changes a result. On 7.0.x and 8.0.x,weight > avg(weight).of { weight > 0 }in the helper averages over every animal, the class the code declares ([20, 30]). At this head it averages over the cats ([30]), as the bare form does. Making the two forms agree is reasonable, but it is a change to a result that is not part of #16807, so the rule in the dev@ thread asks for a second reviewer, a description of the change and an upgrade note that says how to get the old result, for example with a subquery built for the parent class. I would keep it out of this fix, in a pull request of its own. -
Description. Please update it to match this head:
- The branch and the stacking change with item 1. You are right that the bug does not depend on the domain binder; it does not depend on Hibernate 7 either, as Hibernate 5 fails the same way.
- It says that the 7 scenarios fail on Hibernate 7 without the change; on Hibernate 7 they need #16816 as well, and on Hibernate 5 they fail without the change too.
- The second commit,
getTargetClass()and the.of { }change appear only in your comment of 11-17. - "Mongo, Neo4j and graphql are left to CI", but with
no ciCI does not run. The transformer is shared, so the MongoDB module's tests should run before the merge: locally, or withrun cion the 7.0.x pull request.
Suggestions
WhereAggregateHelperSpecpasses on 7.0.x and 8.0.x with Hibernate 5 without this PR, since the bare form takes the class of the criteria there. It guards against the first commit of this PR, which is useful; it could say so.- The example-app specs can use a bare
@Integration: each app has oneApplicationclass, and the annotation finds it.
Verified as correct
- At this head, the five data service methods and the
wherequeries of the table return the right rows on Hibernate 5 and 7, and James's helper takes the average and the minimum over the cats. - The
propertyprojection that feedsgtAlland its siblings is unchanged.
|
Thanks for the table. Status on your items, before I change the PR:
Until the data service problem on 7.0.x is understood, this PR stays as it is. |
Fixes #16807
@Where({ amount > avg(amount) })in a data service generatedselect avg(amount) from t where amount > (select amount from t), which the database rejects ('Scalar subquery contains more than one row').DetachedCriteriaTransformertranslated the aggregate into a bare closure; inside a service the enclosing criteria resolved itsprojectionscall, so the aggregate landed on the outer query and the subquery lost it.The aggregate is now built as an explicit
new DetachedCriteria(X).build { projections { ... } }, the way the.of { }form already was. Thepropertyprojection form that feeds thegtAllfamily is unchanged.WhereServiceAggregateScenarioSpecruns>,>=,<,<=,== max,== minand a combination with a method argument on both the H5 and H7 example apps. All 7 fail on H7 without the change.The issue was marked for Grails 9 on the assumption that it depends on the classic domain binder. It does not: the fix is in the shared AST transformer in grails-datamapping-core. Stacked on the chain below it. Its base is the head of #16823, the top of the #16816 chain (the earlier base, the closed #16825, was replaced by #16876, which has merged); the functional spec needs the
ScenarioItemfixture from #16816, so this one stays in the stack.https://claude.ai/code/session_01Dmki4SQGwRSj8pRJ5KCWyX