Repository: jtablesaw/tablesaw
Pull request #795 (2020-04-22 13:47:31)

remove precondition on aggregate such that empty slices are allowed
Thanks for contributing.

- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

A precondition was removed from `tech.tablesaw.table.TableSliceGroup.aggregate(ListMultimap<String, AggregateFunction<?, ?>> functions)` such that the method no longer throws an `IllegalArgumentException` when the table is empty.

This PR has also been discussed in #785 

## Testing

A unit test is included that makes sure that aggregations on an empty table can be done, that the resulting table is also empty and that the table has the expected number of columns.

See `tech.tablesaw.table.TableSliceGroupTest.aggregateWithEmptyResult()`


Patch:
diff --git a/core/src/main/java/tech/tablesaw/table/TableSliceGroup.java b/core/src/main/java/tech/tablesaw/table/TableSliceGroup.java
index 5fd6d9f8..55887ad6 100644
--- a/core/src/main/java/tech/tablesaw/table/TableSliceGroup.java
+++ b/core/src/main/java/tech/tablesaw/table/TableSliceGroup.java
@@ -14,7 +14,6 @@
 
 package tech.tablesaw.table;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Splitter;
 import com.google.common.collect.ArrayListMultimap;
 import com.google.common.collect.ListMultimap;
@@ -187,7 +186,6 @@ public class TableSliceGroup implements Iterable<TableSlice> {
    */
   @SuppressWarnings({"unchecked", "rawtypes"})
   public Table aggregate(ListMultimap<String, AggregateFunction<?, ?>> functions) {
-    Preconditions.checkArgument(!getSlices().isEmpty());
     Table groupTable = summaryTableName(sourceTable);
     StringColumn groupColumn = StringColumn.create("Group");
     groupTable.addColumns(groupColumn);


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/table/TableSliceGroupTest.java b/core/src/test/java/tech/tablesaw/table/TableSliceGroupTest.java
index 69571524..c95e487e 100644
--- a/core/src/test/java/tech/tablesaw/table/TableSliceGroupTest.java
+++ b/core/src/test/java/tech/tablesaw/table/TableSliceGroupTest.java
@@ -21,6 +21,7 @@ import com.google.common.collect.ImmutableListMultimap;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
+import java.util.stream.IntStream;
 import org.apache.commons.math3.stat.StatUtils;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -139,4 +140,29 @@ public class TableSliceGroupTest {
         group.aggregate(ImmutableListMultimap.of("approval", exaggerate, "approval2", exaggerate));
     assertEquals(aggregated.rowCount(), group.size());
   }
+
+  /**
+   * Make sure that aggregations are allowed on empty tables. They should however just create new
+   * empty tables.
+   *
+   * <p>see <a href="https://github.com/jtablesaw/tablesaw/issues/785">Issue#785</a>
+   */
+  @Test
+  public void aggregateWithEmptyResult() {
+    // drop all rows in order to carry out aggregation on an empty table
+    table = table.dropRows(IntStream.range(0, table.column(0).size()).toArray());
+
+    TableSliceGroup group = StandardTableSliceGroup.create(table, table.categoricalColumn("who"));
+    Table aggregated = group.aggregate("approval", exaggerate);
+    assertEquals(0, aggregated.rowCount(), "result should be empty");
+    assertEquals(2, aggregated.columnCount()); // 1 original column + the aggregation column
+
+    table.addColumns(table.categoricalColumn("approval").copy().setName("approval2"));
+    group = StandardTableSliceGroup.create(table, table.categoricalColumn("who"));
+
+    aggregated =
+        group.aggregate(ImmutableListMultimap.of("approval", exaggerate, "approval2", exaggerate));
+    assertEquals(0, aggregated.rowCount(), "result should be empty");
+    assertEquals(3, aggregated.columnCount()); // 2 original columns + the aggregation column
+  }
 }