Repository: jtablesaw/tablesaw
Pull request #826 (2020-07-16 11:51:39)

Fixes returnType() of BooleanNumericFunction
- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Changed the `returnType()` of `BooleanNumericFunction` to `ColumnType.DOUBLE`. It was `null` before and would therefore cause a null pointer exception during aggregation.

## Testing

Unit tests were added for the `BooleanNumericFunction` instances `proportionTrue` and `proportionFalse`. Furthermore, a test for said aggregate functions in the context of table summarization was added.


Traderjoe95: Fixes #825 

Patch:
diff --git a/core/src/main/java/tech/tablesaw/aggregate/BooleanNumericFunction.java b/core/src/main/java/tech/tablesaw/aggregate/BooleanNumericFunction.java
index cfb67a3d..fae78910 100644
--- a/core/src/main/java/tech/tablesaw/aggregate/BooleanNumericFunction.java
+++ b/core/src/main/java/tech/tablesaw/aggregate/BooleanNumericFunction.java
@@ -19,6 +19,6 @@ abstract class BooleanNumericFunction extends AggregateFunction<BooleanColumn, D
 
   @Override
   public ColumnType returnType() {
-    return null;
+    return ColumnType.DOUBLE;
   }
 }


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/aggregate/AggregateFunctionsTest.java b/core/src/test/java/tech/tablesaw/aggregate/AggregateFunctionsTest.java
index e36c7bd5..19e2b3f0 100644
--- a/core/src/test/java/tech/tablesaw/aggregate/AggregateFunctionsTest.java
+++ b/core/src/test/java/tech/tablesaw/aggregate/AggregateFunctionsTest.java
@@ -47,6 +47,7 @@ import org.apache.commons.math3.stat.StatUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import tech.tablesaw.api.BooleanColumn;
+import tech.tablesaw.api.ColumnType;
 import tech.tablesaw.api.DateColumn;
 import tech.tablesaw.api.DoubleColumn;
 import tech.tablesaw.api.InstantColumn;
@@ -179,6 +180,38 @@ class AggregateFunctionsTest {
     assertFalse(allTrue.summarize(bc));
   }
 
+  @Test
+  void testBooleanNumericAggregateFunctions() {
+    boolean[] values = {true, false, false, false};
+    BooleanColumn bc = BooleanColumn.create("test", values);
+    assertEquals(0.25, proportionTrue.summarize(bc));
+    assertEquals(0.75, proportionFalse.summarize(bc));
+  }
+
+  @Test
+  void testBooleanNumericFunctionGroup() {
+    boolean[] values = {true, false, false, false, true, true, true, false};
+    String[] group = {"a", "a", "a", "a", "b", "b", "b", "b"};
+    BooleanColumn bc = BooleanColumn.create("test", values);
+    StringColumn sc = StringColumn.create("group_key", group);
+
+    Table table = Table.create(sc, bc);
+    Table summarized = table.summarize("test", proportionTrue, proportionFalse)
+                            .by("group_key");
+
+    assertEquals(2, summarized.rowCount());
+    assertEquals(1, summarized.where(summarized.stringColumn("group_key").isEqualTo("a")).rowCount());
+    assertEquals(1, summarized.where(summarized.stringColumn("group_key").isEqualTo("b")).rowCount());
+    assertEquals(ColumnType.DOUBLE, summarized.where(summarized.stringColumn(0).isEqualTo("a")).column(1).type());
+    assertEquals(ColumnType.DOUBLE, summarized.where(summarized.stringColumn(0).isEqualTo("a")).column(2).type());
+    assertEquals(ColumnType.DOUBLE, summarized.where(summarized.stringColumn(0).isEqualTo("b")).column(1).type());
+    assertEquals(ColumnType.DOUBLE, summarized.where(summarized.stringColumn(0).isEqualTo("b")).column(2).type());
+    assertEquals(0.25, summarized.where(summarized.stringColumn(0).isEqualTo("a")).doubleColumn(1).get(0));
+    assertEquals(0.75, summarized.where(summarized.stringColumn(0).isEqualTo("a")).doubleColumn(2).get(0));
+    assertEquals(0.75, summarized.where(summarized.stringColumn(0).isEqualTo("b")).doubleColumn(1).get(0));
+    assertEquals(0.25, summarized.where(summarized.stringColumn(0).isEqualTo("b")).doubleColumn(2).get(0));
+  }
+
   @Test
   void testGroupMean2() {
     Table result = table.summarize("approval", mean, stdDev).apply();