Repository navigation
Add a setting that keeps the rows without an association when sorting by it - #17065
Conversation
Sorting a where query by a property of an association, such as sort: 'club.name', failed with a QueryException when the query also had criteria on that association. The criteria join the association under a generated alias, and the order was added with the association path, which Hibernate cannot resolve. The order now uses the alias of the join.
A sort by a path through more than one association, such as owner.employer.name, is still not supported.
This reverts commit b494caa.
A sort by a path that crosses more than one association, such as team.club.name, failed with a QueryException whether or not the where query joined the nested association, because order() split the path at its first dot only. The sort property is now qualified with the alias of the last association on its path: an association the query has already joined is reused, and one it has not joined yet is joined for the sort and registered like any other join. A sort by two properties of the same association no longer fails with "duplicate association path" either, since the sort join is registered instead of created anew for every order.
Since order() walks the whole sort path, a sort by the identifier of an association that follows another one on the path, such as team.club.id, joined that last association too. The join is an inner join unless fetch requests another type, so the sort left out the rows where the association is null. Before, only the first association was joined and Hibernate read club.id from the foreign key of team, which kept them. The identifier is sorted by the foreign key again when the entity before the association holds it. An association without such a foreign key, a hasOne or the inverse side of a one-to-one, is still joined, and so is the first association of a path, as it always has been. The spec also covers the join type of a sort join: an inner join by default, and a left join when fetch requests one. Team.club is nullable in the fixture for these cases. The docs describe the inner join, how to request a left join, and the identifier of a nested association.
list with a map of sort properties, such as Team.list(sort: ['club.name': 'asc', name: 'desc']), sorted by the last entry only, because each entry replaced the orders of the query. The same held for a default sort that the mapping declares for more than one property. Every entry is now applied, in the order of the map. An association is joined once for all the sort properties that use it. list joined it once for each, and find and findAll with an example, findWhere and findAllWhere failed with "duplicate association path". Part 2 of the split of apache#16853.
A paged query of a detached criteria that has an alias for the queried
class, and criteria that use it, failed when totalCount was read:
new DetachedCriteria(Team, 't').eq('t.name', 'Arsenal').list(max: 2).totalCount
The count runs on a copy of the criteria, and the copy was created
without the alias, so Hibernate could not resolve t.name. The copy now
has the alias of the original.
Part 3 of the split of apache#16853.
count on a where query or a detached criteria that has a sort, such as
Team.where { ... }.sort('name').count(), kept the order by in the query
that counts the rows, which H2 rejects with "must be in the GROUP BY
list". A query that only counts its rows now has no orders.
The join that a sort by a property of an association makes is kept, so
the count is that of the rows which list returns for the same query.
Part 4 of the split of apache#16853.
… by it A sort by a property of an association joins the association with an inner join, so it leaves out the instances whose association is null: Team.list(sort: 'club.name') does not return the teams without a club, and count and the totalCount of a paged list do not count them. The new grails.gorm.leftJoinAssociationSort setting makes an association that is joined only for a sort a left join, in where queries, detached criteria, dynamic finders, list, find and findAll with an example, findWhere, findAllWhere and a criteria query that is passed pagination arguments. It is disabled by default, so the results of existing applications do not change in a patch release. Grails 8.1 enables it by default. With the setting, a sort by a nested identifier, such as team.club.id, goes through the left join. Without it, it is still read from the foreign key. A sort by a property of a collection keeps its inner join: Hibernate fetches a collection through a left join, which is handled separately. Part 5 of the split of apache#16853.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A sort that should keep a row with no association still drops it when the call also fetches that association eagerly.
| for (Object existing : from.getJoins()) { | ||
| Join join = (Join) existing; | ||
| if (associationName.equals(join.getAttribute().getName())) { | ||
| return join; |
There was a problem hiding this comment.
The setting that keeps a row with no association is on. The list sorts by club.name and fetches club eagerly. A team with no club should stay. It is dropped, because the eager fetch joins club first and the sort reuses that join. Make the sort's join the one the setting asks for.
There was a problem hiding this comment.
If this is Team.list(sort: "club.name", fetch: [club: "eager"]), the eager-fetch join of static list is still the old implementation at this point in the stack. Changing that fetch join is part 11, #17071, including the Hibernate 5 counterpart of #16898. It should be reviewed there rather than pulling that change into the sort-setting PR.
The setting controls a join made only for sorting; it does not replace a join that the query already has. If you meant an eager fetch declared in the mapping rather than the fetch argument, could you post that mapping and the public GORM call so we can identify the owning part?
matrei
left a comment
There was a problem hiding this comment.
Reviewed at 310a227 (base 7.0.x), the last commit. I probed sorts with the setting off and on:
- at this head, on 7.0.x (aee4c04) and at the stack top (e359719);
- with a team that has no club and a player whose team has no club.
With the setting off, every result is the one of 7.0.x, except for the calls that throw on 7.0.x. With the setting on, a sort keeps the instances without the association:
| Setting off, and 7.0.x | Setting on | |
|---|---|---|
Team.list(sort: 'club.name'), also sort: 'club.id' |
2 teams | 3 |
Team.list(sort: 'club.name', max: 2).totalCount |
2 | 3 |
Player.list(sort: 'team.club.name'), also team.club.id |
1 player | 2 |
a where query, a dynamic finder and a paged criteria query, sorted by club.name with fetch: [club: 'join'] |
2 teams | 3 |
a mapping with sort 'club.name', and a mapping with club fetch: 'join' and a sort |
1 team | 2 |
Team.list(sort: 'players.name') |
2 teams | 2: the collection keeps its inner join |
Needs a fix
1. With the setting on, the sort's left join wins over a join that the query asks for. The description says that an association is joined with a left join "unless the query requests another join type for it". The upgrade note says that criteria on the association keep their inner join. With the setting on, at this head, neither holds:
| Setting off | Setting on | Stack top, off and on | |
|---|---|---|---|
Team.where { name == 'Unattached First Team' }.sort('club.name').list(fetch: [club: JoinType.INNER]), also findAllWhere |
nothing | the team | nothing |
Team.list(sort: 'club.name', fetch: [club: JoinType.INNER]), also a paged criteria query |
2 teams | 3 | 2 |
Team.where { name ==~ '%First%' }.sort('club.name').list { club { isNull('name') } }, also count { … } |
nothing | the team | nothing |
the same with or { club { eq('name', 'Arsenal') }; eq('name', 'Unattached First Team') } |
1 team | 2 | 1 |
The features that cover these cases are skipped with the setting, through @IgnoreIf({ instance.leftJoinSort }):
- two in
WhereQueryWithAssociationSortSpec, until #17066; - three in
AssociationJoinTypeSpec, until #17069 and #17071.
So with the setting on, this part on its own returns rows that the later parts take away again. Either it merges together with those three, into the same release, or it moves after them in the stack. The description should say which.
2. The settings page. #17061 names the page Compatibility Settings (grails-doc/src/en/guide/upgrading/compatibilitySettings.adoc). This page is resultSettings.adoc, titled "Settings for Fixes That Change a Result". This is the first setting, so this part creates the page, and the name should match.
The enum row doesn't fit the column "Default in Grails 7 and 8.0": 8.0 has no such setting, since 8.0.0 removed the legacy marshaller. A default for each release line, as #17061 lists it, would fit both rows.
3. The new links don't resolve. The guide has no guide/upgrading60x.html and no guide/resultSettings.html. A section is on the page of its chapter and on its own page under guide/pages/.
I built the guide at this head. Three links point to pages that don't exist, on upgrading.html, on single.html and on the section pages:
link:resultSettings.htmlin the note;link:upgrading60x.html#grails-7-0-18and#grails-7-0-2on the page.
link:upgrading.html#grails-7-0-18[…] resolves on every page, as link:upgrading.html#_12_5_hibernate_ehcache already does in the same file.
4. The warning, the default in 8.1 and the removal in 9.0. The dev@ thread and #17061 agree on this: while an application keeps the old result through the setting, it logs a warning at startup that names the major release that removes the setting. This part has no such warning.
From 8.1.0, the warning concerns every application that sets the setting to false. Before that, it concerns one that sets it explicitly. The new default and the removal don't come with a merge-up either. Please say in the description which change does each:
- the warning;
- the default in 8.1.x, with its notes and the row of the page;
- the removal in 9.0.x, for Hibernate 5 and for #16863.
An issue for each would keep them from being forgotten.
5. Add run ci, as on the other parts: the test jobs were skipped. Locally, the tests of grails-data-hibernate5-core pass at this head (907 tests, 57 skipped).
Notes
-
On James's comment: with the setting on, the team is dropped here, but the sort isn't the cause.
Team.list(fetch: [club: 'join'])joins the club with an inner join with no sort at all. It returns 2 teams on 7.0.x and here, with the setting on or off.- The sort reuses that join, so it leaves out only what the fetch already leaves out.
- #17071 fetches the club through a left join instead. At the stack top, the list keeps the team with or without the sort.
- A mapping with
club fetch: 'join'keeps the team here already.
So there's nothing to change in this part for it. At the stack top,
Team.list(sort: 'club.name', fetch: [club: 'join'])also returns 3 teams with the setting off, which I'll look at on #17071. -
As a fix that changes a result, this needs a second reviewer (#17061).
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A plain join the call asked for is reused by a sort, so a row with no association is left out.
| for (Object existing : from.getJoins()) { | ||
| Join join = (Join) existing; | ||
| if (associationName.equals(join.getAttribute().getName())) { | ||
| return join; |
There was a problem hiding this comment.
The setting that keeps a row with no association is on. Team.list(sort: 'club.name', fetch: [club: 'join']) should include Unattached First Team. It is left out, because the plain join is an inner join and the sort reuses it. Keep the row unless the call asked for an inner join.
Keep join lookup private to its only caller. Document changed page contents and collection duplicates, and cover sorting before pagination. Preserve both sets of upgrade notes when merging the release branch.
Keep the count-projection and listener-order regressions, import RowCountProjection, and remove upgrade entries for calls that previously failed. Counts preserve joined rows rather than promising a distinct entity total.
Warn once at datastore startup for an explicitly false compatibility setting and cover unset, true and false. Rename the settings page, repair its links and list defaults separately for each release line. Carry the reviewed parent changes forward.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A plain join the call asked for is still reused by a sort, so a row with no association is left out.
|
|
||
| final FetchMode fetchMode = getFetchMode(fetch.get(associationName)); | ||
| if (fetchMode == FetchMode.JOIN) { | ||
| queryRoot.join(associationName); |
There was a problem hiding this comment.
The setting that keeps a row with no association is on. Team.list(sort: 'club.name', fetch: [club: 'join']) should include Unattached First Team. It is left out, because the plain join is an inner join and the sort reuses it. Keep the row unless the call asked for an inner join.
matrei
left a comment
There was a problem hiding this comment.
Reviewed again at c9d60fa. The points from my first review are fixed:
- "Release ordering" says that this part ships in the same release as #17066, #17069 and #17071. With the setting on, a requested inner join and criteria on the association still lose to the sort's left join at this head, as in my first review, and the later parts fix that.
- The page is now Compatibility Settings,
compatibilitySettings.adoc, with a default for each release line. - The links resolve. I built the guide:
upgrading.html#grails-7-0-18,#grails-7-0-2and#compatibilitySettingsexist onupgrading.htmlandsingle.html, and the section pages link to them with../upgrading.html#…. - An explicit
falselogs a warning at startup that names Grails 9. Unset andtruelog nothing, and the parent datastore logs it once, not each datastore of a connection source. - The pull request has
run ci, and CI passed with the tests.
My probes from the first review give the same results here as at 310a227, with the setting off and on.
Two smaller points:
- The description and the commit message say that without the setting, a sort by a nested identifier such as
team.club.id"is still read from the foreign key". That holds for where queries, detached criteria and dynamic finders.Player.list(sort: 'team.club.id')and a criteria query with that sort joinclubwith an inner join and leave out the player whose team has no club, on 7.0.x and here. That's #17160, which I opened for James's comment on #17062. The sentence could name the where queries, or point to #17160. - The default in 8.1.x and the removal in 9.0.x are follow-ups now, which is fine, but nothing tracks them yet. An issue for each, for Hibernate 5 and for #16863, would keep them from being forgotten when 8.1.0 and 9.0.0 are cut.
James's two new comments ask about Team.list(sort: 'club.name', fetch: [club: 'join']) again. That's the inner join of the fetch argument, which part 11 (#17071) changes, as you said in your reply.
As a fix that changes a result, this needs a second approval under #17061.
✅ All tests passed ✅Test SummaryTip Inspect all details with the TestLens CLI: CI - Groovy Joint Validation Build / build_grails > :grails-test-examples-micronaut:integrationTest
🏷️ Commit: 4170200 Test FailuresMicronautErsatzAdvancedSpec > Micronaut client handles 401 Unauthorized from ersatz (:grails-test-examples-micronaut:integrationTest in CI - Groovy Joint Validation Build / build_grails | Attempt 1/2)MicronautErsatzAdvancedSpec > full roundtrip: PATCH through Grails controller to ersatz mock (:grails-test-examples-micronaut:integrationTest in CI - Groovy Joint Validation Build / build_grails | Attempt 1/2)Learn more about TestLens at testlens.app/docs. |
matrei
left a comment
There was a problem hiding this comment.
Reviewed again at 4170200. Since my approval, the only change is the merge of the page assertion from #17062 into AssociationSortSpec (git diff c9d60fac0e 41702004fd). Here it expects the page for each value of the setting. With grails.gorm.leftJoinAssociationSort, the team without a club sorts first, so the page at offset 1 is Arsenal Second Team, Arsenal First Team. Without the setting, it's Arsenal First Team, Chelsea First Team. AssociationSortSpec and AssociationSortLeftJoinSpec, which runs it with the setting on, both pass at #17066, which has the same lines.
The one failed check is build_grails of the Groovy Joint Validation Build. It failed in two features of MicronautErsatzAdvancedSpec in the Micronaut example app, with Connection reset from the Ersatz server, so it's unrelated. The same job passed on #17066. It needs a rerun.
As before, this changes results, so it needs a second approval under #17061.
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A plain join the call asked for is still reused by a sort, so a row with no association is left out.
| for (Object existing : from.getJoins()) { | ||
| Join join = (Join) existing; | ||
| if (associationName.equals(join.getAttribute().getName())) { | ||
| return join; |
There was a problem hiding this comment.
The setting that keeps a row with no association is on. Team.list(sort: 'club.name', fetch: [club: 'join'], max: 10) should include Unattached First Team. It is left out, because the plain join is an inner join and the sort reuses it. Keep the row unless the call asked for an inner join.
borinquenkid
left a comment
There was a problem hiding this comment.
Approved. It should merge together with #17066, which closes the gaps this one leaves open.
sbglasius
left a comment
There was a problem hiding this comment.
This is an AI assisted review
Reviewed at 4170200 against #17064. The opt-in setting, the collection exception, the startup warning for an explicit false and the docs look right, and running the sort specs with the setting both on and off is good coverage.
I agree with @jamesfredley's point on GrailsHibernateQueryUtils.java:406: with the setting on, a sort that reuses a plain fetch: [club: 'join'] join should still keep the row without a club. Approving on the understanding that this is fixed here or in #17066 before the release.
Nit: the startup warning sits inside the non-SingletonConnectionSources branch of HibernateDatastore, so a datastore built on singleton connection sources never logs it.
|
@sbglasius I'll address in #17066 |
The sort now matches the Hibernate 5 parts on 7.0.x (#16853, #17062, #17064, #17065 and the approved #17070) and nothing beyond them: - A sort by a property of a collection joins it with an inner join with or without leftJoinAssociationSort, as merged Hibernate 5 does; the left join with the setting is #17072, still under review. - An order in an association block of a criteria query becomes an order by the path through the association when the block ends, and sorts through the join of the block, as with Hibernate 5. AssociationOrder, AssociationOrders and the per-block contexts are removed, and with them the orders by aliases of a block or of the criteria around it. - Orders in the blocks of a detached criteria that the builder did not build, and in the block of an embedded component, are ignored, as with Hibernate 5 and on 8.0.0. Sorts through an embedded component are read as a path, as on 8.0.0, and the fixes for blocks inside an embedded block are removed. - A block order in a distinct, grouped or aggregate query reaches the database, as with Hibernate 5, instead of being rejected beforehand. - grails.gorm.PagedResultList and the TCK DetachedCriteriaSpec are back as on 8.0.x. HibernateQuery.clearOrders() keeps the cleared orders, whose joins a count still makes, as Hibernate 5 keeps the alias that an order joined. - The new methods of ExpressionResolver and JpaQueryContext are package-private.
Description
Part 5 of the split of #16853, as agreed there and in the dev@ thread Fixes that change results on patch and minor branches. Relates to #15589.
Stacked on #17064. Until that merges, this pull request also shows the commits below it. Review the changes introduced by this part, including its review fixes, relative to #17064.
A sort by a property of an association joins the association with an
inner join, so it leaves out the instances whose association is null:
Team.list(sort: 'club.name') does not return the teams without a club,
and count and the totalCount of a paged list do not count them.
The new grails.gorm.leftJoinAssociationSort setting makes an association
that is joined only for a sort a left join, in where queries, detached
criteria, dynamic finders, list, find and findAll with an example,
findWhere, findAllWhere and a criteria query that is passed pagination
arguments. It is disabled by default, so the results of existing
applications do not change in a patch release. Enabling it by default in
Grails 8.1 is tracked in #17164.
With the setting, a sort by a nested identifier, such as team.club.id,
goes through the left join. With the setting off, where queries, detached
criteria and dynamic finders still read that nested identifier from the
foreign key. Static list and the sort argument of a criteria query join
the final association and can leave out instances without it; that separate
behavior is tracked by #17160.
A sort by a property of a collection keeps its inner join: Hibernate
fetches a collection through a left join, which is handled separately.
Results that change
grails.gorm.leftJoinAssociationSort: true, a sort by a property of an association joins it with a left join, solist,countandtotalCountkeep the instances whose association isnull.grails.gorm.leftJoinAssociationSort, off by default, planned to be on by default from 8.1.0 (GORM: enable leftJoinAssociationSort by default in Grails 8.1 #17164) and removed in 9.0.0 (GORM: remove the leftJoinAssociationSort compatibility setting in Grails 9 #17165). Every GORM API returns the old result today and many applications sort a list by an association, so this follows the setting rule from the dev@ thread. The Hibernate 7 fix (Sort by an association with Hibernate 7 as with Hibernate 5, and keep the rows without it with leftJoinAssociationSort #16863) reads the same key.The upgrade guide has a note under Grails 7.0.18, and the upgrade skill has the same entry.
Release ordering
This part must ship in the same release as #17066, #17069 and #17071. Those parts make explicitly requested joins and criteria take precedence over the optional sort join; enabling this setting before they land would expose the intermediate behavior identified in review.
The startup warning is implemented here for an explicitly false setting. The Compatibility Settings page now has separate release-line defaults and working chapter links. Enabling the default on 8.1.x is tracked by #17164 and removing the setting on 9.0.x by #17165, both covering Hibernate 5 and Hibernate 7. They must be completed before 8.1.0 and 9.0.0 respectively; merging this 7.0.x change forward does not implement either.
Verification
At 4170200 the full Hibernate 5 core suite passes (919 cases, zero failures, 62 skipped), including all three startup-warning cases and explicit expected page names in both compatibility-setting modes. The datastore-core suite, full codeStyle, RAT and guide build pass. The generated guide contains all three link targets used by the compatibility-settings page and upgrade note.
Contributor Checklist
Please review the following checklist before submitting your pull request. Pull requests that do not meet these requirements may be closed without review.
Issue and Scope
7.0.x): Bug fixes only. No new features or API changes.7.1.x): New features are welcome, but breaking existing APIs must be avoided.8.0.x): Reserved for major changes. Breaking API changes are permitted.Code Quality
./gradlew build --rerun-tasks../gradlew codeStyleand resolved any violations. See Code Style for details.Licensing and Attribution
Documentation