jamesfredley commented on code in PR #16573:
URL: https://github.com/apache/grails-core/pull/16573#discussion_r4338701772


##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/ExpressionResolver.java:
##########
@@ -112,7 +112,13 @@ private Expression<?> resolveFromAlias(String alias, 
String subPath) {
         if (aliasRegistry.isDefined(alias)) {
             HibernateAlias def = aliasRegistry.getDefinition(alias);
             if (def != null && def.path() != null) {
-                aliased = joinTracker.getRoot().join(def.path(), 
def.joinType());
+                // The association criteria and its alias must refer to the 
same physical join,
+                // regardless of whether a predicate, projection or order 
resolves it first.
+                aliased = joinTracker.getJoin(def.path());

Review Comment:
   This lookup is not scoped to the current query. `JoinTracker.getJoin` (lines 
47-51) returns the parent tracker's join when the path is absent locally, and 
`PredicateGenerator.handleExists()` installs that parent through 
`setParentContext()`.
   
   A detached subquery that declares its own alias for an association the outer 
query already joined therefore binds to the outer join. Example: teams A and B, 
favorites labeled `first` and `second`. Outer criteria filters `outside.label 
== 'first'` and `exists` a detached criteria that aliases the same `favorite` 
path as `inside` and filters `inside.label == 'second'`. That should return A. 
After this change `inside` reuses the outer `favorite` join, so the EXISTS 
predicate tests A's favorite against `second` and drops A. Before this change 
the alias was materialized on the subquery root.
   
   Reuse a path join only when it belongs to the current root. Keep parent 
lookup for an alias the subquery deliberately references from the outer query. 
Please add a Hibernate-backed public-API regression with the same association 
path, different outer and inner aliases, and different matching rows.



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