Repository: jtablesaw/tablesaw
Pull request #967 (2021-08-12 13:37:35)

fix issue #963
Moved the structure implementation from Table up to Relation, replacing the existing version there.

Thanks for contributing.

- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Moved the structure implementation from Table up to Relation, replacing the existing version there.

## Testing

Did you add a unit test?
Yes



Patch:
diff --git a/core/src/main/java/tech/tablesaw/api/Table.java b/core/src/main/java/tech/tablesaw/api/Table.java
index 17c43f8d..214eab30 100644
--- a/core/src/main/java/tech/tablesaw/api/Table.java
+++ b/core/src/main/java/tech/tablesaw/api/Table.java
@@ -836,24 +836,6 @@ public class Table extends Relation implements Iterable<Row> {
     return StandardTableSliceGroup.create(this, columns);
   }
 
-  @Override
-  public Table structure() {
-    Table t = new Table("Structure of " + name());
-
-    IntColumn index = IntColumn.indexColumn("Index", columnCount(), 0);
-    StringColumn columnName = StringColumn.create("Column Name", columnCount());
-    StringColumn columnType = StringColumn.create("Column Type", columnCount());
-    t.addColumns(index);
-    t.addColumns(columnName);
-    t.addColumns(columnType);
-    for (int i = 0; i < columnCount(); i++) {
-      Column<?> column = columnList.get(i);
-      columnType.set(i, column.type().name());
-      columnName.set(i, columnNames().get(i));
-    }
-    return t;
-  }
-
   /** Returns the unique records in this table Note: Uses a lot of memory for a sort */
   public Table dropDuplicateRows() {
 
diff --git a/core/src/main/java/tech/tablesaw/table/Relation.java b/core/src/main/java/tech/tablesaw/table/Relation.java
index a8ca304f..712240fd 100644
--- a/core/src/main/java/tech/tablesaw/table/Relation.java
+++ b/core/src/main/java/tech/tablesaw/table/Relation.java
@@ -211,33 +211,20 @@ public abstract class Relation implements Iterable<Row> {
   }
 
   public Table structure() {
-    StringBuilder nameBuilder = new StringBuilder();
-    nameBuilder
-        .append("Table: ")
-        .append(name())
-        .append(" - ")
-        .append(rowCount())
-        .append(" observations (rows) of ")
-        .append(columnCount())
-        .append(" variables (cols)");
-
-    Table structure = Table.create(nameBuilder.toString());
-    structure.addColumns(DoubleColumn.create("Index"));
-    structure.addColumns(StringColumn.create("Column Name"));
-    structure.addColumns(StringColumn.create("Type"));
-    structure.addColumns(DoubleColumn.create("Unique Values"));
-    structure.addColumns(StringColumn.create("First"));
-    structure.addColumns(StringColumn.create("Last"));
-
-    for (Column<?> column : columns()) {
-      structure.intColumn("Index").append(columnIndex(column));
-      structure.stringColumn("Column Name").append(column.name());
-      structure.stringColumn("Type").append(column.type().name());
-      structure.intColumn("Unique Values").append(column.countUnique());
-      structure.stringColumn("First").append(column.getString(0));
-      structure.stringColumn("Last").append(column.getString(column.size() - 1));
+    Table t = Table.create("Structure of " + name());
+
+    IntColumn index = IntColumn.indexColumn("Index", columnCount(), 0);
+    StringColumn columnName = StringColumn.create("Column Name", columnCount());
+    StringColumn columnType = StringColumn.create("Column Type", columnCount());
+    t.addColumns(index);
+    t.addColumns(columnName);
+    t.addColumns(columnType);
+    for (int i = 0; i < columnCount(); i++) {
+      Column<?> column = this.columns().get(i);
+      columnType.set(i, column.type().name());
+      columnName.set(i, columnNames().get(i));
     }
-    return structure;
+    return t;
   }
 
   public Table summary() {


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/table/TableSliceTest.java b/core/src/test/java/tech/tablesaw/table/TableSliceTest.java
index 8af2903c..81ddef6b 100644
--- a/core/src/test/java/tech/tablesaw/table/TableSliceTest.java
+++ b/core/src/test/java/tech/tablesaw/table/TableSliceTest.java
@@ -305,4 +305,15 @@ public class TableSliceTest {
 
     assertArrayEquals(expected, actual);
   }
+
+  @Test
+  void structure() {
+
+    Table table = Table.create(StringColumn.create("MyCol", "A", "A", "B"));
+    TableSliceGroup sliceGroup = table.splitOn("MyCol");
+
+    for (TableSlice slice : sliceGroup.getSlices()) {
+      assertNotNull(slice.structure());
+    }
+  }
 }