Skip to content

Commit

Permalink
Improve GroupBy/Having serialization for count queries
Browse files Browse the repository at this point in the history
  • Loading branch information
timowest committed Oct 24, 2015
1 parent 3ec8366 commit 2b4652f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
10 changes: 7 additions & 3 deletions querydsl-jpa/src/main/java/com/querydsl/jpa/JPQLSerializer.java
Expand Up @@ -216,8 +216,12 @@ public void serialize(QueryMetadata metadata, boolean forCountRow, @Nullable Str
}

// where
if (where != null) {
append(WHERE).handle(where);
if (where != null || forCountRow && having != null) {
Predicate predicate = where;
if (forCountRow && having != null) {
predicate = ExpressionUtils.and(predicate, having);
}
append(WHERE).handle(predicate);
}

// group by
Expand All @@ -226,7 +230,7 @@ public void serialize(QueryMetadata metadata, boolean forCountRow, @Nullable Str
}

// having
if (having != null) {
if (having != null && !forCountRow) {
append(HAVING).handle(having);
}

Expand Down
20 changes: 16 additions & 4 deletions querydsl-jpa/src/test/java/com/querydsl/jpa/AbstractJPATest.java
Expand Up @@ -838,7 +838,6 @@ public void GroupBy_YearMonth() {
}

@Test
@Ignore // FIXME
public void GroupBy_Count() {
List<Integer> ids = query().from(cat).groupBy(cat.id).select(cat.id).fetch();
long count = query().from(cat).groupBy(cat.id).fetchCount();
Expand All @@ -848,20 +847,33 @@ public void GroupBy_Count() {
long catCount = query().from(cat).fetchCount();
assertEquals(catCount, ids.size());
assertEquals(catCount, count);
assertEquals(catCount, results.getResults().size());
assertEquals(1, results.getResults().size());
assertEquals(catCount, results.getTotal());
}

@Test
@Ignore // FIXME
public void GroupBy_Having_Count() {
List<Integer> ids = query().from(cat).groupBy(cat.id).having(cat.id.loe(2)).select(cat.id).fetch();
long count = query().from(cat).groupBy(cat.id).having(cat.id.loe(2)).fetchCount();
QueryResults<Integer> results = query().from(cat).groupBy(cat.id).having(cat.id.loe(2))
.limit(1).select(cat.id).fetchResults();

assertEquals(2, ids.size());
assertEquals(2, count);
assertEquals(1, results.getResults().size());
assertEquals(2, results.getTotal());
}


@Test
public void GroupBy_Distinct_Count() {
List<Integer> ids = query().from(cat).groupBy(cat.id).distinct().select(Expressions.ONE).fetch();
QueryResults<Integer> results = query().from(cat).groupBy(cat.id)
.limit(1).distinct().select(Expressions.ONE).fetchResults();

assertEquals(1, ids.size());
assertEquals(1, results.getResults().size());
assertEquals(1, results.getTotal());
assertEquals(6, results.getTotal());
}

@Test
Expand Down
16 changes: 16 additions & 0 deletions querydsl-sql/src/test/java/com/querydsl/sql/SelectBase.java
Expand Up @@ -827,6 +827,22 @@ public void GroupBy_Count() {
assertEquals(10, results.getTotal());
}

@Test
@ExcludeIn({FIREBIRD})
public void GroupBy_Having_Count() {
List<Integer> ids = query().from(employee).groupBy(employee.id)
.having(employee.id.isNotNull()).select(employee.id).fetch();
long count = query().from(employee).groupBy(employee.id).having(employee.id.isNotNull()).fetchCount();
QueryResults<Integer> results = query().from(employee).groupBy(employee.id)
.having(employee.id.isNotNull())
.limit(1).select(employee.id).fetchResults();

assertEquals(10, ids.size());
assertEquals(10, count);
assertEquals(1, results.getResults().size());
assertEquals(10, results.getTotal());
}

@Test
@ExcludeIn({FIREBIRD, SQLSERVER, TERADATA})
public void GroupBy_Distinct_Count() {
Expand Down

0 comments on commit 2b4652f

Please sign in to comment.