Skip to content

fix(datamapping): a where query subquery resolves a nested path of the alias of the enclosing query - #17060

Open
borinquenkid wants to merge 5 commits into
7.0.xfrom
fix/nested-alias-path-7.0.x
Open

borinquenkid wants to merge 5 commits into
7.0.xfrom
fix/nested-alias-path-7.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

On Hibernate 5, a subquery of a where query that names a nested path of the alias of the enclosing query throws. With def t = Team in Team.where { ... }, this throws MissingPropertyException: No such property: club for class: grails.gorm.tests.Team:

Team.where {
    def t = Team
    exists(Club.where { id == t.club.id && name == 'Manchester United' }.property('name'))
}.list()

DetachedCriteriaTransformer looked only at the object expression one level up (t.club), found no alias named that, and left t.club.id as a Groovy expression on the class. It now follows the chain to its first element and rewrites the comparison when that element is a declared alias. A chain that starts at a method call or at a variable that is not an alias still becomes eq, as before.

This is the 7.0.x part of #16821, split out at matrei's request: a call that throws is a bug that goes to the oldest branch that has it (dev@ rule on fixes that change results). The change makes a failing call work and changes no result that worked, so there is no upgrade note. The Hibernate 7 parts of #16821 (root alias registration) stay there.

  • WhereQueryPropertyAliasSpec (datamapping-core) checks the AST the transform generates: a plain and an association property compared with a nested alias path become eqProperty, and a chain not rooted at a variable stays eq. 2 of its 3 features fail on the unfixed transform.
  • NestedAliasPathSubqueryHibernateSpec (Hibernate 5 core) runs the query above and the id == t.club form against the database; the first fails with the exception above without the fix.
  • Hibernate 5 core and datamapping-core run with 0 failures.

Part of #16813

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

…e alias of the enclosing query

A comparison that names a nested path of a local alias, such as id == t.club.id for def t = Team, was left as
an expression on the class and threw a MissingPropertyException on Hibernate 5. The transform now follows the
chain to its first element and rewrites the comparison when that element is a declared alias; a chain that
starts at a method call or an undeclared variable still becomes eq.

Part of #16813

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A comparison of the subquery's own root with a path of the enclosing alias is dropped.

methodToCall = PROPERTY_COMPARISON_OPERATOR_TO_CRITERIA_METHOD_MAP.get(operator);
}
else if ((leftExpression instanceof VariableExpression) && aliases.containsKey(property)) {
else if ((leftExpression instanceof VariableExpression) && property != null && aliases.containsKey(property)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subquery aliases its own root as c and compares c.id with t.club.id. That should keep only the teams of Manchester United. The comparison is dropped, so Arsenal First Team comes back too. Resolve c.id on the subquery, and t.club.id on the enclosing query.

@sanjana2505006 sanjana2505006 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for splitting this out. I checked it out and ran a few extra cases against Hibernate 5.

I can confirm James's point, and I found one regression, which I've left inline. This change fixed both for me, and datamapping-core plus the H5 where/subquery specs still pass:

@@ visitMethod
         } finally {
             detachedCriteriaVariables.clear();
+            aliases.clear();
+            aliasExpressions.clear();
         }
@@ handleAssociationQueryViaPropertyExpression
-                String targetObject = oppositeProperty.getObjectExpression().getText();
-                if (aliases.containsKey(targetObject)) {
+                String targetObject = aliasRootName(oppositeProperty);
+                if (targetObject != null && aliases.containsKey(targetObject)) {
+                    aliasExpressions.add(oppositeProperty);

A few smaller things, not blocking:

  • name == t.club.name still fails, now with could not resolve property: t, so on H5 only the .id path works. It might be worth narrowing the description or adding a test for it.
  • t.club.id == id (the alias path on the left) still drops the criterion silently, same as before.
  • WhereQueryPropertyAliasSpec uses t.nested.whatever, but nested isn't a property on those entities. A real association would make the test read like a real query.
  • The id == t.club feature is already covered by SubqueryAliasSpec.

PropertyExpression pe = (PropertyExpression) rightExpression;
String property = pe.getObjectExpression().getText();
if ((leftExpression instanceof PropertyExpression) && aliases.containsKey(property)) {
String property = aliasRootName(pe);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes an existing problem a bit wider. aliases is never cleared, so an alias declared in one method still counts in every later method in the same file.

With def t = Team in one test, this in another test of the same spec passes on 7.0.x but fails with this change (QueryException: could not resolve property: t of: Club):

Map t = [club: [name: 'Arsenal']]
Club.where { name == t.club.name }.list()

Clearing aliases and aliasExpressions in visitMethod fixed it for me.

…th of an alias, and aliases stay in their method

A subquery that aliases its own root compared c.id with t.club.id through the object of the opposite side, which
for a nested path is t.club and no alias, so the comparison was dropped; the alias is the root of the chain.
A nested path of an alias on the left of the comparison, t.club.id == c.id, was dropped as well: the
sides change places, and the operator with them. The aliases of a class were never cleared, so a name declared
as an alias in one method was an alias in every later method of the file, and a local variable of that name
in a later one failed to compile or compared the wrong thing; they are cleared with each method and class.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley @sanjana2505006: both findings are fixed in one commit.

James. A subquery that aliases its own root compared c.id with t.club.id through the object of the opposite side, t.club, which is no alias, so the comparison was dropped. The alias is now the root of the chain, t. Checking the neighbours of that case turned up the reversed order, t.club.id == c.id, which was dropped as well and returned all three teams; the sides now change places, and the operator with them for <, >, <= and >=.

sanjana. aliases and aliasExpressions are cleared with each method and each class, as you suggested. Your Map t = [club: [name: 'Arsenal']] case is in NestedAliasPathSubqueryHibernateSpec, after a feature that declares def t = Team.

NestedAliasPathSubqueryHibernateSpec has seven features: the two shapes of your report, an own-root alias against the association, the reversed order, an extra criterion next to the comparison, and the leak. WhereQueryPropertyAliasSpec checks the generated calls. Six of them fail without the change.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A comparison of the enclosing alias with the subquery's own identifier is still dropped.

Expression leftExpression = be.getLeftExpression();
Expression rightExpression = be.getRightExpression();
String swappedOperator = SWAPPED_COMPARISON_OPERATORS.get(operator);
if (swappedOperator != null && isNestedAliasPath(leftExpression) && isAliasProperty(rightExpression)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subquery compares t.club.id with its own id, and the name is Manchester United. That should keep only the United teams. Arsenal First Team comes back too, because the comparison is dropped. Keep that comparison.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: exists(Club.where { t.club.id == id && name == 'Manchester United' }.property('name')) keeps the comparison now, so only the United teams come back. The sides of a comparison change places when the nested path of an alias is on the left and the right is an alias property such as c.id; they did not when the right was a property of the subquery's own class, such as id, so the comparison was dropped. They change places for that case too, with the operator swapped (< becomes >).

Before asking for another look, I ran each of four shapes (the nested alias path on the left or on the right of the subquery's id, and of its aliased c.id) with each of the six comparison operators, over teams of two clubs where the club of the subquery is named: 31 rows in the spec, 5 fail without this change. @sanjana2505006: that is also your t.club.id == id case. name == t.club.name still does not resolve on Hibernate 5; I left it, and the description only claims the .id path.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A comparison of the enclosing alias with the subquery's identifier is still dropped.

Expression leftExpression = be.getLeftExpression();
Expression rightExpression = be.getRightExpression();
String swappedOperator = SWAPPED_COMPARISON_OPERATORS.get(operator);
if (swappedOperator != null && isNestedAliasPath(leftExpression) &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subquery compares t.club with its own id, and the name is Manchester United. That should keep only the United teams. Arsenal First Team comes back too, because the comparison is dropped. Keep that comparison.

@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley: I ran the forms you cited on this head and on plain 7.0.x: 11 forms of the comparison between the enclosing alias and the subquery, with and without def c = Club. Three are still wrong here: t.club == id, t.club != id and t.club == c, the association of the alias written first. They return the same wrong rows on plain 7.0.x, so this PR did not introduce them, and they are not a nested path, which is what this PR is about. The nested path forms (t.club.id == id, id == t.club.id, c.id == t.club.id, t.club.id == c.id and the != form of the id) give the right rows here, and on plain 7.0.x four of them threw or returned every team. I filed the three as #17110 and left them out of this PR.

Workaround: write the subquery side first, id == t.club or c.id == t.club, which return the right rows on both. The shared transformer can take the fix on 8.1.x.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@sanjana2505006

Copy link
Copy Markdown
Member

Thanks for the updates, both of my earlier points are fixed. Two small things I found at the AST level:

  1. An alias declared in a static where field still leaks into the first method, because fields are visited before methods. With static q = PLeak.where { def t = PLeak; ... }, a later Map t method now gets eqProperty('title', 't.author.name') instead of eq(...) as on 7.0.x. Clearing the aliases at the start of visitMethod (or in visitField) should fix it.

  2. isPropertyOfQueriedClass doesn't check variableScope.isReferencedLocalVariable(...) the way addCriteriaCall does, so a captured local with a property's name (t.author.id == title) is read as a property.

Thanks for the thorough tests!

@borinquenkid

Copy link
Copy Markdown
Member Author

@sanjana2505006: thank you, both are fixed.

  1. An alias declared in a static where field is cleared once that field has been visited, as it is after a method. The spec has your shape, a field with def t = ... and then a method with Map t: the method compares with the value of its local variable, and the field still compares the properties.
  2. isPropertyOfQueriedClass now asks the variable scope, as addCriteriaCall does, so a captured local named like a property of the queried class is not read as that property.

One thing I saw on the way, outside this PR: with the nested path of an alias written first and a captured local on the right, t.nested.whatever == title, the generated query has the alias and no comparison. It looks like the family of #17110. I have not yet run it on plain 7.0.x, so I will check that there and file it if it is already there.

@testlens-app

testlens-app Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

🚨 TestLens detected 1 failed test 🚨

Test Summary

Tip

Inspect all details with the TestLens CLI: testlens pr 17060

CI / Build Grails-Core (windows-latest, 25, true) > :grails-gsp:test

Test Runs Flakiness
FormTagLib2Tests > testDatePickerTagWithMinutePrecision() ❌ 0% 🟢

🏷️ Commit: 15e53ab
▶️ Tests: 58374 executed
⚪️ Checks: 57/57 completed

Test Failures

FormTagLib2Tests > testDatePickerTagWithMinutePrecision() (:grails-gsp:test in CI / Build Grails-Core (windows-latest, 25, true))
org.opentest4j.AssertionFailedError: expected: <true> but was: <false>
	at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:151)
	at org.junit.jupiter.api.AssertionFailureBuilder.buildAndThrow(AssertionFailureBuilder.java:132)
	at org.junit.jupiter.api.AssertTrue.failNotTrue(AssertTrue.java:63)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:36)
	at org.junit.jupiter.api.AssertTrue.assertTrue(AssertTrue.java:31)
	at org.junit.jupiter.api.Assertions.assertTrue(Assertions.java:183)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
	at org.grails.web.taglib.AbstractGrailsTagTests.assertXPathExists(AbstractGrailsTagTests.groovy:540)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
	at org.grails.web.taglib.FormTagLib2Tests.assertSelectFieldPresentWithSelectedValue(FormTagLib2Tests.groovy:307)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
	at org.grails.web.taglib.FormTagLib2Tests.validateSelectedMinuteValue(FormTagLib2Tests.groovy:297)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.selectMethod(IndyInterface.java:355)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
	at org.grails.web.taglib.FormTagLib2Tests.testDatePickerTag(FormTagLib2Tests.groovy:237)
	at org.codehaus.groovy.vmplugin.v8.IndyInterface.fromCache(IndyInterface.java:321)
	at org.grails.web.taglib.FormTagLib2Tests.testDatePickerTagWithMinutePrecision(FormTagLib2Tests.groovy:74)
	at java.base/java.lang.reflect.Method.invoke(Method.java:565)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1604)
expected actual
true false

Rerun Controls

Select tests to mute in this pull request:

  • FormTagLib2Tests > testDatePickerTagWithMinutePrecision()

Reuse successful test results:

  • ♻️ Only rerun the tests that failed or were muted before

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. A comparison of the enclosing alias with the subquery identifier is still dropped, and an alias leaks into a later query.

Expression leftExpression = be.getLeftExpression();
Expression rightExpression = be.getRightExpression();
String swappedOperator = SWAPPED_COMPARISON_OPERATORS.get(operator);
if (swappedOperator != null && isNestedAliasPath(leftExpression) &&

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The subquery compares t.club with its own id, and the name is Manchester United. Arsenal First Team still comes back, because this only swaps t.club.id, not t.club. Keep that comparison.

methodToCall = PROPERTY_COMPARISON_OPERATOR_TO_CRITERIA_METHOD_MAP.get(operator);
}
else if ((leftExpression instanceof VariableExpression) && aliases.containsKey(property)) {
else if ((leftExpression instanceof VariableExpression) && property != null && aliases.containsKey(property)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One where query declares t, and a later query in the same method compares name with t.club.name from a local map. The later query uses t as the earlier alias and fails. Use the alias only in the query that declared it.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants