Repository navigation
Subqueries take the connection of the query that holds them (#16886) - #16895
borinquenkid wants to merge 6 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. |
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