BalusC opened a new pull request, #324:
URL: https://github.com/apache/openjpa/pull/324

   https://issues.apache.org/jira/browse/OPENJPA-2841
   
   A subquery nested in another subquery which refers to the root of the 
enclosing subquery gives that root a second alias. The enclosing subquery then 
cross joins its candidate table once more, and the query returns wrong rows:
   
   ```
   SELECT COUNT(e) FROM NestedSubqueryOwner e WHERE EXISTS (SELECT s.id FROM 
NestedSubqueryOwner s
       WHERE s.id = e.id AND EXISTS (SELECT n.id FROM NestedSubqueryOwner n 
WHERE n.id = s.id AND n.name = 'x'))
   -- before
   ... EXISTS (SELECT t0.id FROM NestedSubqueryOwner t0, NestedSubqueryOwner t3 
WHERE (t0.id = t1.id
       AND EXISTS (SELECT t2.id FROM NestedSubqueryOwner t2 WHERE (t2.id = 
t3.id AND t2.name = 'x'))))
   -- after
   ... EXISTS (SELECT t0.id FROM NestedSubqueryOwner t0 WHERE (t0.id = t1.id
       AND EXISTS (SELECT t2.id FROM NestedSubqueryOwner t2 WHERE (t2.id = 
t0.id AND t2.name = 'x'))))
   ```
   
   ### Cause
   
   The root of a subquery is aliased with the candidate alias of the subquery 
as path, such as `Key("jpqlalias1", NestedSubqueryOwner)`. A field reference 
such as `s.id` in the nested subquery has no path. So 
`SelectImpl.findAliasForQuery()` looks up the plain table name in the select of 
`s`, does not find it, and creates a second alias there. A subquery of the 
top-level query works, because the root of the top-level query has no path 
either.
   
   ### Changes
   
   - `PCPath.initialize()` correlates a field path from the root of an 
enclosing subquery with that subquery, the same as a bare variable, through the 
new `correlateWithSubquery()`. The inheritance check of the bare variable does 
not apply to the field path, because it turns the path into an object path for 
a `JOINED` subclass. This fixes comparisons of a field or a relation, `SIZE()`, 
`IS NULL`, a reference two levels up, and a bulk update or delete with such a 
subquery.
   - `SelectImpl.findAliasForQuery()`: when a key without path is not found in 
the select of the context of an enclosing subquery, the new 
`getSubqueryRootAlias()` looks up the alias of the root of that subquery under 
its candidate alias. This fixes `MEMBER OF`, `KEY()` and `VALUE()` on a 
collection of that root. A second root or a join variable of the enclosing 
subquery comes with a path, so this lookup does not apply to it.
   
   Both changes are needed: with only one of them, 4 or 2 of the new tests 
fail. A subquery of the top-level query generates the same SQL as before.
   
   ### Tests
   
   `TestNestedCorrelatedSubquery` has 15 tests: JPQL and criteria, with an 
element collection, a relation, a map, a join variable, a second root, a 
`JOINED` subclass, and a reference two levels up. Without the change, 12 of 
them fail; the other 3 cover a single subquery level. The full 
`openjpa-persistence-jdbc` suite on Derby passes: 4273 tests, 134 skipped.
   
   ### History
   
   [OmniPersistence](https://github.com/omnifaces/omnipersistence) found this 
in its integration tests on TomEE 10.2.0 with OpenJPA 4.1.1. Its count query 
wraps the query in `EXISTS (SELECT ... WHERE id = e.id)`, so a criteria on an 
element collection becomes a subquery in a subquery, and the count came out 
wrong. It is the same on 4.2.0 and master.
   
   - 5290e7d82 and f74c5d7a6 (OPENJPA-1185, 2009) add 
`SelectImpl.findAliasForQuery()`, which looks up an alias in the select of the 
context that declares the schema alias.
   - ef3ee9e28 (2009) adds `navigateFromParentRootInSubselect()`, which 
correlates a path of more than one field from a root of an enclosing select.
   - 339d9df32 (2009, "fix nested subquery correlation problem") adds 
`findSubqAlias()`, which correlates a bare variable with the subquery that 
declares it. A path of a single field, such as `s.id`, is covered by neither.
   
   ### Candidates for follow-up issues
   
   These shapes are wrong before and after this change:
   
   - `n.name = s.parent.name` in the nested subquery: the table of `s.parent` 
is added to the select of `s` without its join condition.
   - `s.children IS NOT EMPTY` in the nested subquery: the count subselect gets 
a second alias for `s`.
   - A criteria query whose enclosing subquery has two roots: the reference to 
the first root comes with the path of the alias of the second root, so 
`getSubqueryRootAlias()` does not apply.
   - Two subquery levels on a `JOINED` subclass fail even without any 
correlation: the nested subquery repeats the join to the superclass table of 
the top-level query.
   
   🤖 Assisted by Claude Opus 5.5
   


-- 
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