Repository navigation
fix(datamapping): a where query subquery resolves a nested path of the alias of the enclosing query - #17060
fix(datamapping): a where query subquery resolves a nested path of the alias of the enclosing query#17060borinquenkid wants to merge 5 commits into
Conversation
…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
jamesfredley
left a comment
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.namestill fails, now withcould not resolve property: t, so on H5 only the.idpath 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.WhereQueryPropertyAliasSpecusest.nested.whatever, butnestedisn't a property on those entities. A real association would make the test read like a real query.- The
id == t.clubfeature is already covered bySubqueryAliasSpec.
| PropertyExpression pe = (PropertyExpression) rightExpression; | ||
| String property = pe.getObjectExpression().getText(); | ||
| if ((leftExpression instanceof PropertyExpression) && aliases.containsKey(property)) { | ||
| String property = aliasRootName(pe); |
There was a problem hiding this comment.
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
|
@jamesfredley @sanjana2505006: both findings are fixed in one commit. James. A subquery that aliases its own root compared sanjana.
|
jamesfredley
left a comment
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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.
…f an alias with a property of the queried class Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: 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 |
jamesfredley
left a comment
There was a problem hiding this comment.
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) && |
There was a problem hiding this comment.
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.
|
@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 Workaround: write the subquery side first, |
|
Thanks for the updates, both of my earlier points are fixed. Two small things I found at the AST level:
Thanks for the thorough tests! |
…lares it, and a captured local is not read as a property of the queried class Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
… and the captured local case shows what is generated Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@sanjana2505006: thank you, both are fixed.
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, |
🚨 TestLens detected 1 failed test 🚨Test SummaryTip Inspect all details with the TestLens CLI: CI / Build Grails-Core (windows-latest, 25, true) > :grails-gsp:test
🏷️ Commit: 15e53ab Test FailuresFormTagLib2Tests > testDatePickerTagWithMinutePrecision() (:grails-gsp:test in CI / Build Grails-Core (windows-latest, 25, true))
Rerun ControlsSelect tests to mute in this pull request:
Reuse successful test results:
Click the checkbox to trigger a rerun:
Learn more about TestLens at testlens.app/docs. |
jamesfredley
left a comment
There was a problem hiding this comment.
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) && |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
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.
On Hibernate 5, a subquery of a
wherequery that names a nested path of the alias of the enclosing query throws. Withdef t = TeaminTeam.where { ... }, this throwsMissingPropertyException: No such property: club for class: grails.gorm.tests.Team:DetachedCriteriaTransformerlooked only at the object expression one level up (t.club), found no alias named that, and leftt.club.idas 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 becomeseq, 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 becomeeqProperty, and a chain not rooted at a variable stayseq. 2 of its 3 features fail on the unfixed transform.NestedAliasPathSubqueryHibernateSpec(Hibernate 5 core) runs the query above and theid == t.clubform against the database; the first fails with the exception above without the fix.Part of #16813
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL