Skip to content

Commit

Permalink
ISPN-5732 Aggregation functions should ignore NULL values
Browse files Browse the repository at this point in the history
  • Loading branch information
anistor committed Sep 10, 2015
1 parent 2132479 commit f8a1014
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,30 @@ public void testOrderBySum() {
assertEquals(22, list.get(0)[0]);
}

@Test
public void testCountNull() {
QueryFactory qf = getQueryFactory();
Query q = qf.from(getModelFactory().getUserImplClass())
.select(Expression.count("age"))
.build();
List<Object[]> list = q.list();
assertEquals(1, list.size());
assertEquals(1, list.get(0).length);
assertEquals(1L, list.get(0)[0]); // only non-null "age"s were counted
}

@Test
public void testAvgNull() {
QueryFactory qf = getQueryFactory();
Query q = qf.from(getModelFactory().getUserImplClass())
.select(Expression.avg("age"))
.build();
List<Object[]> list = q.list();
assertEquals(1, list.size());
assertEquals(1, list.get(0).length);
assertEquals(22.0, list.get(0)[0]); // only non-null "age"s were counted
}

@Test
public void testParam() throws Exception {
QueryFactory qf = getQueryFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public final class AvgAccumulator extends FieldAccumulator {

private static class Avg {
double sum;
int count;
long count;

Avg(double sum, int count) {
Avg(double sum, long count) {
this.sum = sum;
this.count = count;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.infinispan.objectfilter.impl.aggregation;

/**
* COUNT returns a Long greater or equal than 0. Null values are not counted.
*
* @author anistor@redhat.com
* @since 8.0
*/
Expand All @@ -12,11 +14,13 @@ public CountAccumulator(int pos) {

@Override
public void init(Object[] row) {
row[pos] = 1;
row[pos] = row[pos] != null ? 1L : 0L;
}

@Override
public void update(Object[] srcRow, Object[] destRow) {
destRow[pos] = (Integer) destRow[pos] + 1;
if (srcRow[pos] != null) {
destRow[pos] = (Long) destRow[pos] + 1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,28 @@ public void testOrderBySum() {
assertEquals(22, list.get(0)[0]);
}

public void testCountNull() {
QueryFactory qf = getQueryFactory();
Query q = qf.from(getModelFactory().getUserImplClass())
.select(Expression.count("age"))
.build();
List<Object[]> list = q.list();
assertEquals(1, list.size());
assertEquals(1, list.get(0).length);
assertEquals(1L, list.get(0)[0]); // only non-null "age"s were counted
}

public void testAvgNull() {
QueryFactory qf = getQueryFactory();
Query q = qf.from(getModelFactory().getUserImplClass())
.select(Expression.avg("age"))
.build();
List<Object[]> list = q.list();
assertEquals(1, list.size());
assertEquals(1, list.get(0).length);
assertEquals(22.0, list.get(0)[0]); // only non-null "age"s were counted
}

public void testParam() throws Exception {
QueryFactory qf = getQueryFactory();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,8 @@ public void testGlobalCount() {
List<Object[]> list = q.list();
assertEquals(1, list.size());
assertEquals(2, list.get(0).length);
assertEquals(3, list.get(0)[0]);
assertEquals(3, list.get(0)[1]);
assertEquals(3L, list.get(0)[0]);
assertEquals(2L, list.get(0)[1]);
}

public void testGlobalAvg() {
Expand Down Expand Up @@ -332,9 +332,9 @@ public void testAggregateGroupingField() {
List<Object[]> list = q.list();
assertEquals(2, list.size());
assertEquals(1, list.get(0).length);
assertEquals(1, list.get(0)[0]);
assertEquals(1L, list.get(0)[0]);
assertEquals(1, list.get(1).length);
assertEquals(2, list.get(1)[0]);
assertEquals(2L, list.get(1)[0]);
}

public void testAggregateEmbedded1() {
Expand Down

0 comments on commit f8a1014

Please sign in to comment.