Skip to content

totalElements value is wrong when using group by with pageable of pageSize 1 [DATAJPA-1544] #1853

Open
@spring-projects-issues

Description

@spring-projects-issues

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

Metadata

Metadata

Assignees

Labels

in: query-parserEverything related to parsing JPQL or SQLtype: bugA general bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions