Repository navigation
fix(hibernate5): the total of a paged criteria with groupProperty counts the groups - #17054
borinquenkid wants to merge 8 commits into
Conversation
…nts the groups The totalCount of a paged criteria that uses a groupProperty projection replaced the grouped projection with a row count, so it disagreed with the grouped page it belongs to. It now counts the groups. Fixes #14431 Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
7.0.18 is staged without this change, so the note belongs to the next patch release. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. The total of a grouped page now counts the groups by loading every group into memory.
| } | ||
| totalCriteria.setProjection(impl.getProjection()); | ||
| if (impl.getProjection() != null && impl.getProjection().isGrouped()) { | ||
| totalCriteria.setProjection(impl.getProjection()); |
There was a problem hiding this comment.
A page of two groups should report how many groups there are without loading them all. This loads every group and then counts the list. Count the groups in the database.
…n the database The total of a page of a criteria grouped by one property loaded every group to count the list. It is now the distinct values of the property, counted by the database, plus the group of the null values, which SQL groups but does not count as a value. A projection with more than one grouped property is counted from its groups, as before. Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: the total of a page grouped by one property is counted by the database now, as |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A page grouped by two properties still counts the groups by loading them.
| } | ||
| Criteria grouped = totalCriteria(session, impl); | ||
| grouped.setProjection(projection); | ||
| return grouped.list().size(); |
There was a problem hiding this comment.
A page of two groups, grouped by city and name, should report how many groups there are without loading them. This loads every group and then counts the list. Count those groups in the database.
… counted in the database Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: a page grouped by city and name now reports its total without loading the groups. For several grouped properties the database counts the rows of the grouped query, Before asking for another look, I ran nine shapes (city and name, name and city, three properties, a property and an aggregate, a property of an association, a property and one of an association, two properties under a condition, no rows, all-null groups) and compared each total with the size of the same query unpaged. 12 rows; each reaches the database with one count query (two for a single property), and 10 fail without this change. |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. A page grouped by an embedded value counts one group twice.
| if (projection instanceof ProjectionList && ((ProjectionList) projection).getLength() == 1) { | ||
| only = ((ProjectionList) projection).getProjection(0); | ||
| } | ||
| if (only instanceof PropertyProjection && ((PropertyProjection) only).isGrouped()) { |
There was a problem hiding this comment.
A page grouped by an embedded value of two columns should report how many groups there are. A group whose columns are all empty is counted twice. Count that group once.
…e, whatever the columns of the grouped value Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: a page grouped by an embedded value of two columns counted the group whose columns are all empty twice. A group by one property was counted as its distinct values plus a group for "the property is null", and for an embedded value a distinct count already includes the group of rows whose columns are all null, so the extra one counted it again. Every grouped page is now counted the same way, as the rows of its grouped query ( Before asking for another look, I added three shapes to the matrix: an embedded value, an embedded value with a property, and an embedded value under a condition, over rows whose street and zip are both set, only one set, and both empty. 15 rows, each total compared with the size of the same query unpaged; 4 fail without this change (the embedded ones and the two that now count in one query). |
…h has not been staged again Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@matrei: the note is back under |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. The count of a grouped page fails when query caching is on.
| } | ||
|
|
||
| @Override | ||
| protected String preprocessSQL(String sql, QueryParameters parameters, SessionFactoryImplementor factory, |
There was a problem hiding this comment.
A page grouped by two properties should report how many groups there are when query caching is on. Reading the total throws, because the count is cached as if it were the grouped rows. Do not cache that count as the grouped query.
… with the query cache on, and the count is never cached Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
|
@jamesfredley: I could not reproduce the failure of the count of a grouped page with query caching on, at 9c4633f. The case I did find is another route: a |
jamesfredley
left a comment
There was a problem hiding this comment.
Requested changes. The count of a grouped page still fails when query caching is on.
| * whose values are all null is one group, whatever the number of columns of the grouped properties. | ||
| */ | ||
| private int countGroups(Session session, CriteriaImpl impl, Projection projection) { | ||
| CriteriaImpl grouped = (CriteriaImpl) totalCriteria(session, impl); |
There was a problem hiding this comment.
A grouped list of cities is cached, then the same grouping is paged and the total is read. That should be 3. It throws, because the count is looked up as if it were the cached cities. Do not cache that count as the grouped query.
|
@jamesfredley: merged 7.0.x into the branch; the only conflict was the upgrade guide, where I kept both sections. On the count that throws with query caching on: I could not reproduce it at bbe0b66, and I tried your sequence again. A grouped list of cities is cached, then the same grouping is paged and its total is read, and the total is 3. I ran it twelve ways, on an entity with What setup throws for you: the database, the cache provider (mine is H2 with Ehcache), and whether the query cache comes from |
The
totalCountof a paged criteria that uses agroupPropertyprojection replaced the grouped projection with a row count, so it disagreed with the grouped page it belongs to: 3 groups from 6 rows reported a total of 6. It now counts the groups.This is the Hibernate 5 half of #16843, split out for 7.0.x after the dev@ discussion on fixes that change results: the total contradicts the page it belongs to, so the fix goes to the oldest branch that has the bug. The Hibernate 7 half stays on #16843 against 8.0.x.
PagedResultListcounts the groups of a grouped criteria; an ungrouped criteria counts rows as before.GroupedCriteriaTotalCountSpec(core) andGroupedCriteriaTotalCountScenarioSpec(example app) pin 3 groups from 6 rows, with and without a restriction, and the unchanged ungrouped count. Two of the three core rows fail on the unfixed code.Fixes #14431
https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL