Repository navigation
Subqueries take the connection of the query that holds them (#16886) - #16895
borinquenkid wants to merge 8 commits into
Conversation
…a that holds it (#16886) A subquery written as avg(age).of { } is built with the default connection, so MongoDB and Neo4j, which run it before the outer query, read it from the wrong connection when the outer query uses withConnection. AbstractDetachedCriteria now gives a subquery that has the default connection the connection of the criteria that holds it, when withConnection is applied and when a criterion is added to a criteria that has one. A subquery built with its own connection keeps it. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
…the query that holds them (#16886) Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. A subquery that inherited its connection is left behind when the outer query is switched back, and a subquery that chose the default connection is moved when the outer query uses another one.
| } | ||
|
|
||
| private static QueryableCriteria onConnection(QueryableCriteria subquery, String name) { | ||
| if (subquery instanceof AbstractDetachedCriteria && ((AbstractDetachedCriteria) subquery).connectionName == ConnectionSource.DEFAULT) { |
There was a problem hiding this comment.
Input: an archive query whose age is compared with avg(age), then switched back to the default connection. The subquery stays on the archive connection, so the default rows are compared with the archive average and a match is dropped. Keep an explicit subquery connection. Move an inherited one with the outer query, including back to the default.
…ria in both directions, and keeps one it chose (#16886) A subquery that took the connection of the criteria holding it stayed there when that criteria went back to the default connection, and a subquery that had chosen the default connection was moved when the criteria used another one. Whether the connection was chosen with withConnection is now recorded; only a connection that was not chosen follows the criteria, to any connection including the default. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley Fixed in af8e07b. Whether a subquery's connection was chosen with |
…subquery matches no row) into fix/subquery-connection-8.0.x
jamesfredley
left a comment
There was a problem hiding this comment.
Not approving. An ordinary where subquery is treated as if the caller chose its connection, so it does not follow the outer query.
|
|
||
| AbstractDetachedCriteria<T> withConnection(String name) { | ||
| AbstractDetachedCriteria newCriteria = switchConnection(name) | ||
| newCriteria.connectionExplicit = true |
There was a problem hiding this comment.
On the archive connection, inList('age', Person.where { age > 50 }.property('age')) returns no rows. The archive has Jasper and Mona above 50. Person.where already sets the default connection, and this line marks that as an explicit choice, so the subquery does not inherit archive. Keep a connection the caller asked for. Do not mark the automatic default as an explicit choice.
…tandalone-8.0.x' into fix/subquery-connection-8.0.x
…ws the query that holds it
Entity.where marked the criteria it returns as having a chosen connection, the default one too, so
inList('age', Person.where { age > 50 }.property('age')) inside a query on the archive connection kept
the default connection: MongoDB and Neo4j, which run the subquery by themselves, read the default
rows and found none above 50. A connection the caller names, as in Entity.archive.where, is chosen;
the default is the one nobody chose, so a criteria that has it is left alone and follows the query
that holds it. A subquery given a connection with withConnection keeps it, the default one included.
The Neo4j rows read a count outside a transaction: inside one, the subquery borrows the session that
is bound, and the connection it asked for does not show.
Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley The second case reproduced and is fixed in the new commit; the first one already behaves as you describe on the branch, and I pinned it.
Why your second case was not found by the rows already in the spec: the Neo4j rows ran inside The merge of the base branch (#16875) is in this push: its last commit repaired the Neo4j test sources, which did not compile on the old head this branch was built on. The Neo4j module (643 tests), the MongoDB module (1,053), Branch policy: the throws that are listed in If you find another shape that does not reproduce first time, a failing spec on a throwaway branch, or the few lines you ran, would help me a lot. |
matrei
left a comment
There was a problem hiding this comment.
Reviewed at 0909f55 (base: #16875's branch at 7798193). I ran:
- the three new specs with and without the change, at this head and at #16875;
- a MongoDB probe of my own, with the same two connections and ages, at both heads;
- the tests of
grails-datamapping-core, Hibernate 5, Hibernate 7, MongoDB and Neo4j at this head.
The fix works. At #16875, 11 of the 16 cases of DetachedCriteriaSubqueryConnectionSpec fail, 4 of the 7 for MongoDB and 4 of the 10 for Neo4j. At this head all of them pass, as do the module suites, with the counts that you give (1832, 1165, 4463, 1053 and 643 tests, no failures).
On MongoDB, a subquery now reads the connection of the query that holds it in every shape that I tried:
….withConnection('archive') |
#16875 | This head |
|---|---|---|
Person.where { age > avg(age) }, also count(), get(), under ` |
, and inside Person.withConnection('archive') { … }` |
|
Person.where { inList('age', Person.where { age > 50 }.property('age')) }, also Person.archive.where, and notIn |
0 people / 3 | Mona and Jasper / Abe |
a nested inList('age', Person.where { age > avg(age) }.property('age')) |
0 | Jasper |
A subquery given withConnection(ConnectionSource.DEFAULT) keeps the default connection. A subquery that is stored and used again afterwards on the default connection isn't changed by the first use. withConnection('archive').withConnection(DEFAULT) reads the default connection again.
Needs a fix
1. The example of both doc changes fails. The upgrade note and where.adoc both give Person.archive.where { age > avg(age).of { } } as the example. On MongoDB that fails, at this head and at #16875:
MissingMethodException: No signature of method: avg for class: org.grails.datastore.gorm.query.criteria.AbstractDetachedCriteria
The same happens with max(age).of { }, inside Person.archive.withTransaction { }, and in a where added to a criteria that has a connection. The where transform doesn't reach Person.archive.where, so the aggregate functions aren't defined there. Person.where { age > avg(age).of { } }.withConnection('archive') works, and Person.archive.where works for inList with a subquery. The example should use the form that works. The fact that the aggregate functions are missing from Entity.connection.where is worth an issue of its own.
2. A subquery on a class that isn't mapped to the outer connection now reads the wrong database. A City with only the default connection, and a Person with default and archive, on MongoDB:
Person.where { inList('name', City.where { … }.property('mayor')) } |
#16875 | This head |
|---|---|---|
| on the default connection | Bart | Bart |
.withConnection('archive'), also with new DetachedCriteria(City).property('mayor') |
Abe | nothing |
.withConnection('archive') with City.where { … }.withConnection(ConnectionSource.DEFAULT) |
Abe | Abe |
The subquery is moved to archive, which City isn't mapped to. MongoDB then reads the City collection of the archive database, which is empty, and the query silently returns no rows. At #16875 the subquery reads the cities where they are. Moving a subquery only to a connection that its class is mapped to, and leaving it on its own connection otherwise, keeps such a query working. ConnectionSourcesSupport.usesConnectionSource(entity, name) tells that. A spec with a second class would cover it.
3. Where it goes. This is stacked on #16875, which has open points, and the target follows it. In my review of #16875, I asked for the MongoDB half to go to 7.0.x, where MongoDB where-subqueries fail as well. On 7.0.x, GormStaticApi.where is new DetachedCriteria<D>(persistentClass).build(callable) and doesn't apply the qualifier. So the change there differs from this one, and this pull request would have a 7.0.x version as well.
4. CI. The pull request has the no ci label, and its base is a side branch, so no test job has run on it. Once it's retargeted to 8.0.x, the label has to go for the tests to run. A 7.0.x version needs run ci as well.
Notes
-
A subquery inside an association block still runs on the connection of its own criteria, and the note says so. That's a silent wrong result as well.
DetachedAssociationCriteriais a detached criteria in the list of criteria, soonConnectioncould recurse into it as it does into a junction. The copy would also have to updateassociationCriteriaMap. If that's left for later, an issue for it would help. -
On James's two comments: switching back to the default connection reads the default average (
Lisa), andPerson.archive.where { inList('age', Person.where { age > 50 }.property('age')) }returns Mona and Jasper. Both hold at this head. -
GormStaticApi.wherenow leaves the criteria alone for the default qualifier. The criteria get no clone, but the connection is the same, and the Hibernate 5 and Hibernate 7 suites pass.
…ction keeps its own connection, and the docs give an example that runs Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
Matrei, thanks for the probes.
|
…tandalone-8.0.x' into fix/subquery-connection-8.0.x # Conflicts: # grails-doc/src/en/guide/upgrading/upgrading80x.adoc
Fixes #16886
Stacked on #16875: the base is its branch, so the diff is only this change. When #16875 merges, this one is retargeted to
8.0.x.Problem
MongoDB and Neo4j run a subquery before the outer query.
avg(age).of { }is built by thewheretransform as a criteria with the default connection, so onPerson.where { age > avg(age).of { } }.withConnection('archive')the average was taken on the default connection and compared with the rows ofarchive.Change
AbstractDetachedCriteriagives a subquery that has the default connection the connection of the criteria that holds it, both whenwithConnectionis applied (after the criterion was added) and when a criterion is added to a criteria that already has a connection. It covers==,!=,>,>=,<,<=,inList,notInand the same insideor,andandnot. A subquery built with its ownwithConnectionkeeps it. The criteriawithConnectionwas called on is not changed: criteria that move are copied.Not covered: a subquery inside an association block runs on the connection of its own criteria; the upgrade guide says so.
Tests
DetachedCriteriaSubqueryConnectionSpec(datamapping-core): 13 cases; 10 fail without the change.SubqueryConnectionSpecfor MongoDB (real Mongo) and Neo4j (two embedded servers), with different ages on each connection so the wrong average selects different rows. Without the change 4 of 5 (Mongo) and 2 of 5 (Neo4j) fail. The Neo4jinListcase passes either way and guards against a regression.grails-datamapping-coresuite, Checkstyle and CodeNarc pass.Docs:
wherereference and the 8.0.x upgrade guide.https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL