borinquenkid opened a new pull request, #16875:
URL: https://github.com/apache/grails-core/pull/16875

   Fixes #16809
   
   Replaces #16824, which sat on top of a chain of PRs. This branch has only 
this change, on 8.0.x.
   
   ## Problem
   
   `Person.where { age > avg(age) }` and the other aggregate subqueries 
documented for `where` put a `DetachedCriteria` in the comparison. SQL 
datastores run it as a subquery. MongoDB and Neo4j compared the property with 
the criteria object and returned **no rows**, without an error (the issue 
reports a `MissingMethodException` for some forms). `inList` / `notIn` against 
a subquery, `exists` and the `all` / `some` forms had the same gap.
   
   ## Changes
   
   - `Query.resolveSubqueries(Junction)` (new, protected, `@since 8.0.1`) runs 
each subquery and returns a copy of the criteria that holds the results. The 
criteria passed in are not changed, so a reused `where` query runs its 
subqueries again each time.
     - `==`, `!=`, `>`, `>=`, `<`, `<=` take the one value the subquery returns 
(an empty result compares with `null`);
     - `inList` takes every row, and `notIn` becomes a negated `inList`;
     - a comparison against a subquery that returns more than one row, 
`exists`, `notExists` and the `all` / `some` forms throw 
`InvalidDataAccessResourceUsageException` naming the form or the property.
   - `MongoQuery.executeQuery` and `Neo4jQuery.executeQuery` call it first.
   - Docs: `where` reference and upgrade guide section 26.16.
   
   Two queries instead of one `$expr` pipeline or a Cypher subquery: the result 
is a snapshot taken when the query runs, which is what a `where` query already 
is on the SQL datastores' first read. A native `$expr` / `CALL` translation can 
replace the substitution later without changing behavior.
   
   ## Tests
   
   - `AggregateSubquerySpec` for MongoDB (12) and Neo4j (9): `avg` with `>`, 
`>=`, `<`, `<=`, the `.of { }` closure, `max`, `min`, `sum`, `==`, `!=`, 
negation, `||`, `inList`, `notIn`, the multi-row and `exists` errors, and a 
repeat run that sees new data. All fail without the change (10 of 10 and 7 of 7 
when I checked before adding the later cases).
   - Full suites: `grails-data-mongodb-core` 1036 tests and 
`grails-data-neo4j-core` 623, 0 failures; `grails-datastore-core` 348, 0 
failures. `codeStyle`, `validateRepositoryConventions` and `rat` pass.
   - PITest (#16819, cross-module): 43 mutants in the new `Query` code, 43 
killed.
   
   ## Not in this PR
   
   - A native `$expr` / Cypher translation (see above).
   - `count` as an aggregate comparison is untested here and not documented.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to