Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leftOuterJoin on multiple tables #905

Merged
merged 1 commit into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 5 additions & 14 deletions core/src/main/java/tech/tablesaw/joining/DataFrameJoiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,11 @@
import tech.tablesaw.columns.dates.DateColumnType;
import tech.tablesaw.columns.datetimes.DateTimeColumnType;
import tech.tablesaw.columns.instant.InstantColumnType;
import tech.tablesaw.columns.numbers.DoubleColumnType;
import tech.tablesaw.columns.numbers.FloatColumnType;
import tech.tablesaw.columns.numbers.IntColumnType;
import tech.tablesaw.columns.numbers.LongColumnType;
import tech.tablesaw.columns.numbers.ShortColumnType;
import tech.tablesaw.columns.numbers.*;
import tech.tablesaw.columns.strings.StringColumnType;
import tech.tablesaw.columns.strings.TextColumnType;
import tech.tablesaw.columns.times.TimeColumnType;
import tech.tablesaw.index.ByteIndex;
import tech.tablesaw.index.DoubleIndex;
import tech.tablesaw.index.FloatIndex;
import tech.tablesaw.index.Index;
import tech.tablesaw.index.IntIndex;
import tech.tablesaw.index.LongIndex;
import tech.tablesaw.index.ShortIndex;
import tech.tablesaw.index.StringIndex;
import tech.tablesaw.index.*;
import tech.tablesaw.selection.Selection;

public class DataFrameJoiner {
Expand Down Expand Up @@ -442,7 +431,9 @@ public Table leftOuter(Table... tables) {
public Table leftOuter(boolean allowDuplicateColumnNames, Table... tables) {
Table joined = table;
for (Table table2 : tables) {
joined = leftOuter(table2, allowDuplicateColumnNames, joinColumnNames);
joined =
joinInternal(
joined, table2, JoinType.LEFT_OUTER, allowDuplicateColumnNames, joinColumnNames);
}
return joined;
}
Expand Down
63 changes: 61 additions & 2 deletions core/src/test/java/tech/tablesaw/joining/DataFrameJoinerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tech.tablesaw.joining;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.google.common.base.Joiner;
import java.util.Arrays;
Expand Down Expand Up @@ -246,6 +247,37 @@ private static Table createINSTRUCTOR() {
"Instructor");
}

private static Table createGOODS1() {
return Table.read()
.csv(
Joiner.on(System.lineSeparator())
.join(
"ID,Name,Price",
"1,Chips,10",
"2,Coca-cola,20",
"3,Bread,30",
"4,Toothbrush,10",
"5,Soap,20",
"6,Towel,30"),
"Goods1");
}

private static Table createGOODS2() {
return Table.read()
.csv(
Joiner.on(System.lineSeparator())
.join("ID,Name,Price", "1,Chips,11", "2,Bread,29", "3,Soap,21", "4,Towel,33"),
"Goods2");
}

private static Table createGOODS3() {
return Table.read()
.csv(
Joiner.on(System.lineSeparator())
.join("ID,Name,Price", "1,Chips,9", "2,Coca-cola,22", "3,Bread,29"),
"Goods3");
}

private static Table createDEPTHEAD() {
return Table.read()
.csv(
Expand Down Expand Up @@ -570,9 +602,16 @@ public void leftOuterJoinWithDoubles3() {
public void leftOuterJoinWithDoubles4() {
Table joined =
DOUBLE_INDEXED_DOGS.joinOn("ID").leftOuter(DOUBLE_INDEXED_PEOPLE, DOUBLE_INDEXED_CATS);
assertEquals(3, joined.columnCount());
assertEquals(4, joined.rowCount());
assertTrue(
joined.columnNames().containsAll(Arrays.asList("ID", "Dog Name", "Name", "Cat Name")));
assertEquals(4, joined.column("ID").size());
assertEquals(4, joined.column("Dog Name").size());
assertEquals(0, joined.column("Dog Name").countMissing());
assertEquals(4, joined.column("Name").size());
assertEquals(1, joined.column("Name").countMissing());
assertEquals(4, joined.column("Cat Name").size());
assertEquals(3, joined.column("Cat Name").countMissing());
assertEquals(4, joined.rowCount());
}

@Test
Expand Down Expand Up @@ -843,6 +882,26 @@ public void leftJoinStudentInstructorOnStateAge() {
assertEquals(10, joined.column("Age").size());
}

@Test
public void leftJoinStoreWithMultiTables() {
Table table1 = createGOODS1();
Table table2 = createGOODS2();
Table table3 = createGOODS3();
Table joined = table1.joinOn("Name").leftOuter(true, table2, table3);
assertEquals(7, joined.columnCount());
assertTrue(
joined
.columnNames()
.containsAll(
Arrays.asList("ID", "Name", "Price", "T2.ID", "T2.Price", "T3.ID", "T3.Price")));
assertEquals(6, joined.rowCount());
assertEquals(6, joined.column("Price").size());
assertEquals(6, joined.column("T2.ID").size());
assertEquals(2, joined.column("T2.ID").countMissing());
assertEquals(6, joined.column("T3.ID").size());
assertEquals(3, joined.column("T3.ID").countMissing());
}

@Test
public void innerJoinHouseBoatOnBedroomsOwner() {
Table table1 = createHOUSE();
Expand Down