Repository: jtablesaw/tablesaw
Pull request #874 (2021-01-25 20:29:15)

fix for the join issue.
Thanks for contributing.

- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Handled cast issue

## Testing

Did you add a unit test?
yes


Patch:
diff --git a/core/src/main/java/tech/tablesaw/index/StringIndex.java b/core/src/main/java/tech/tablesaw/index/StringIndex.java
index f8aa43e0..c05ab76e 100644
--- a/core/src/main/java/tech/tablesaw/index/StringIndex.java
+++ b/core/src/main/java/tech/tablesaw/index/StringIndex.java
@@ -17,7 +17,7 @@ package tech.tablesaw.index;
 import it.unimi.dsi.fastutil.ints.IntArrayList;
 import java.util.HashMap;
 import java.util.Map;
-import tech.tablesaw.api.StringColumn;
+import tech.tablesaw.columns.strings.AbstractStringColumn;
 import tech.tablesaw.selection.BitmapBackedSelection;
 import tech.tablesaw.selection.Selection;
 
@@ -26,7 +26,7 @@ public class StringIndex implements Index {
 
   private final Map<String, IntArrayList> index;
 
-  public StringIndex(StringColumn column) {
+  public StringIndex(AbstractStringColumn<?> column) {
     int sizeEstimate = Integer.min(1_000_000, column.size() / 100);
     Map<String, IntArrayList> tempMap = new HashMap<>(sizeEstimate);
     for (int i = 0; i < column.size(); i++) {
diff --git a/core/src/main/java/tech/tablesaw/joining/DataFrameJoiner.java b/core/src/main/java/tech/tablesaw/joining/DataFrameJoiner.java
index 9a4af373..fd043cda 100644
--- a/core/src/main/java/tech/tablesaw/joining/DataFrameJoiner.java
+++ b/core/src/main/java/tech/tablesaw/joining/DataFrameJoiner.java
@@ -8,20 +8,7 @@ import java.util.List;
 import java.util.Set;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.stream.Collectors;
-import tech.tablesaw.api.BooleanColumn;
-import tech.tablesaw.api.ColumnType;
-import tech.tablesaw.api.DateColumn;
-import tech.tablesaw.api.DateTimeColumn;
-import tech.tablesaw.api.DoubleColumn;
-import tech.tablesaw.api.FloatColumn;
-import tech.tablesaw.api.InstantColumn;
-import tech.tablesaw.api.IntColumn;
-import tech.tablesaw.api.LongColumn;
-import tech.tablesaw.api.Row;
-import tech.tablesaw.api.ShortColumn;
-import tech.tablesaw.api.StringColumn;
-import tech.tablesaw.api.Table;
-import tech.tablesaw.api.TimeColumn;
+import tech.tablesaw.api.*;
 import tech.tablesaw.columns.Column;
 import tech.tablesaw.columns.booleans.BooleanColumnType;
 import tech.tablesaw.columns.dates.DateColumnType;
@@ -202,10 +189,9 @@ public class DataFrameJoiner {
     List<Index> table2Indexes = buildIndexesForJoinColumns(table2JoinColumnIndexes, table2);
     validateIndexes(table1Indexes, table2Indexes);
 
-    if (table1.rowCount() == 0 &&
-        (joinType == JoinType.LEFT_OUTER || joinType == JoinType.INNER)) {
+    if (table1.rowCount() == 0 && (joinType == JoinType.LEFT_OUTER || joinType == JoinType.INNER)) {
       // Handle special case of empty table here so it doesn't fall through to the behavior
-      // that adds rows for full outer and right outer joins 
+      // that adds rows for full outer and right outer joins
       result.removeColumns(Ints.toArray(resultIgnoreColIndexes));
       return result;
     }
@@ -286,8 +272,10 @@ public class DataFrameJoiner {
       return new LongIndex(table.instantColumn(colIndex));
     } else if (type instanceof TimeColumnType) {
       return new IntIndex(table.timeColumn(colIndex));
-    } else if (type instanceof StringColumnType || type instanceof TextColumnType) {
+    } else if (type instanceof StringColumnType) {
       return new StringIndex(table.stringColumn(colIndex));
+    } else if (type instanceof TextColumnType) {
+      return new StringIndex(table.textColumn(colIndex));
     } else if (type instanceof IntColumnType) {
       return new IntIndex(table.intColumn(colIndex));
     } else if (type instanceof LongColumnType) {
@@ -327,10 +315,14 @@ public class DataFrameJoiner {
       LongIndex index = (LongIndex) rawIndex;
       long value = ((InstantColumn) valueColumn).getLongInternal(rowIndex);
       return index.get(value);
-    } else if (type instanceof StringColumnType || type instanceof TextColumnType) {
+    } else if (type instanceof StringColumnType) {
       StringIndex index = (StringIndex) rawIndex;
       String value = ((StringColumn) valueColumn).get(rowIndex);
       return index.get(value);
+    } else if (type instanceof TextColumnType) {
+      StringIndex index = (StringIndex) rawIndex;
+      String value = ((TextColumn) valueColumn).get(rowIndex);
+      return index.get(value);
     } else if (type instanceof IntColumnType) {
       IntIndex index = (IntIndex) rawIndex;
       int value = ((IntColumn) valueColumn).getInt(rowIndex);


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/api/TextColumnTest.java b/core/src/test/java/tech/tablesaw/api/TextColumnTest.java
index ef34d156..a30bb142 100644
--- a/core/src/test/java/tech/tablesaw/api/TextColumnTest.java
+++ b/core/src/test/java/tech/tablesaw/api/TextColumnTest.java
@@ -19,12 +19,16 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static tech.tablesaw.columns.strings.StringPredicates.isEqualToIgnoringCase;
 
+import com.google.common.base.Joiner;
+import java.io.IOException;
+import java.util.Arrays;
 import java.util.List;
 import java.util.function.Function;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
 import tech.tablesaw.TestDataUtil;
 import tech.tablesaw.columns.strings.StringColumnFormatter;
+import tech.tablesaw.io.csv.CsvReadOptions;
 import tech.tablesaw.selection.Selection;
 
 public class TextColumnTest {
@@ -75,6 +79,36 @@ public class TextColumnTest {
     assertEquals("", c1.get(3));
   }
 
+  @Test
+  public void joinTablesWithStringColumnsSuccess() throws IOException {
+    Table t1 =
+        Table.read()
+            .usingOptions(
+                CsvReadOptions.builderFromString(
+                        Joiner.on(System.lineSeparator())
+                            .join(
+                                "TIME,TEMP SENSOR 1",
+                                "13:21:50.430,23.7",
+                                "13:21:51,23.1",
+                                "13:21:52.451,24.2"))
+                    .columnTypesToDetect(Arrays.asList(ColumnType.DOUBLE, ColumnType.TEXT)));
+
+    Table t2 =
+        Table.read()
+            .usingOptions(
+                CsvReadOptions.builderFromString(
+                        Joiner.on(System.lineSeparator())
+                            .join(
+                                "TIME,TEMP SENSOR 2",
+                                "13:21:50.430,24.9",
+                                "13:21:51,25.2",
+                                "13:21:52.451,26.1"))
+                    .columnTypesToDetect(Arrays.asList(ColumnType.DOUBLE, ColumnType.TEXT)));
+
+    Table joined = t1.joinOn("TIME").fullOuter(t2);
+    assertEquals(3, joined.columnCount());
+  }
+
   @Test
   public void lead() {
     TextColumn c1 = column.lead(1);