Description
RohitJ23 opened DATAJPA-1544 and commented
Similar to https://jira.spring.io/browse/DATAJPA-1415
When group by is used with pageable of page size 1 and there is exactly 1 result row, totalElements (and totalPages) returned by pageable has the count before group by is applied.
This is because JpaQueryExecution assumes if totals.size() == 1 (which is true in this case) then group by is not applied and therefore totals.get(0) is the actual count:
private long count(AbstractJpaQuery repositoryQuery, Object[] values) {
List<?> totals = repositoryQuery.createCountQuery(values).getResultList();
return (totals.size() == 1 ? CONVERSION_SERVICE.convert(totals.get(0), Long.class) : totals.size());
}
And PageExecutionUtils.getPage does a greater than check instead of greater than equal to, resulting in totalElements being what totalSupplier gives instead of content size.
if (pageable.isUnpaged() || pageable.getPageSize() > content.size()) {
return new PageImpl<>(content, pageable, content.size());
}
return new PageImpl<>(content, pageable, totalSupplier.getAsLong());
Reproduced in https://github.com/RohitJ23/GroupBy-Repro - run GroupByBugTest.groupByBugTest
Affects: 2.1.8 (Lovelace SR8)
Referenced from: pull request #388