Skip to content

Commit

Permalink
Fix, document, and test ColumnType.compare and Table.compareRows (#1266)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccleva authored Jan 8, 2025
1 parent a1c9e2d commit 7a716f2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
9 changes: 2 additions & 7 deletions core/src/main/java/tech/tablesaw/api/ColumnType.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,11 @@ static ColumnType valueOf(String name) {
/** TODO: Research this method to provide a good comment */
AbstractColumnParser<?> customParser(ReadOptions options);

/** TODO: Research this method to provide a good comment */
default boolean compare(int rowNumber, Column<?> temp, Column<?> original) {
Object o1 = original.get(rowNumber);
Object o2 = temp.get(temp.size() - 1);
return o1 == null ? o2 == null : o1.equals(o2);
}

/**
* Returns true if the value at the specified index in column1 is equal to the value at the
* specified index in column 2
*
* @throws {@code IndexOutOfBoundsException} if either index exceeds the corresponding column size
*/
default boolean compare(int col1Row, Column<?> col1, int col2Row, Column<?> col2) {
Object o1 = col1.get(col1Row);
Expand Down
19 changes: 12 additions & 7 deletions core/src/main/java/tech/tablesaw/api/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -529,17 +529,22 @@ public void copyRowsToTable(int[] rows, Table newTable) {
}

/**
* Returns {@code true} if the row @rowNumber in table1 holds the same data as the row at
* rowNumber in table2
* Returns {@code true} if the row {@code rowNumber} in {@code table1} holds the same values than
* the row at {@code rowNumber} in {@code table2}. Returns {@code false} if the number of columns
* is different in the two tables.
*
* @throws {@code IndexOutOfBoundsException} if {@code rowNumber} exceeds either table number of
* rows
*/
public static boolean compareRows(int rowNumber, Table table1, Table table2) {
int columnCount = table1.columnCount();
boolean result;
final int columnCount = table1.columnCount();
if (columnCount != table2.columnCount()) {
return false;
}
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
ColumnType columnType = table1.column(columnIndex).type();
result =
columnType.compare(rowNumber, table2.column(columnIndex), table1.column(columnIndex));
if (!result) {
if (!columnType.compare(
rowNumber, table2.column(columnIndex), rowNumber, table1.column(columnIndex))) {
return false;
}
}
Expand Down
35 changes: 35 additions & 0 deletions core/src/test/java/tech/tablesaw/api/TableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class TableTest {
private static final ColumnType[] BUSH_COLUMN_TYPES = {LOCAL_DATE, INTEGER, STRING};
private static Table bush;
private static Table bushMinimized;
private static Table missingValues;

private Table table;
private final DoubleColumn f1 = DoubleColumn.create("f1");
Expand All @@ -64,6 +65,8 @@ static void readTables() {
.columnTypes(BUSH_COLUMN_TYPES));
ColumnType[] types = {LOCAL_DATE, SHORT, STRING};
bushMinimized = Table.read().csv(CsvReadOptions.builder("../data/bush.csv").columnTypes(types));
missingValues = Table.read().csv(CsvReadOptions.builder("../data/missing_values.csv")
.missingValueIndicator("-"));
}

@BeforeEach
Expand Down Expand Up @@ -916,4 +919,36 @@ public void testToStringColumnsWithVaryingSizes() {
fail("toString shouldn't throw " + e);
}
}

@Test
void testCompareRowsIdentical() {
for(int i = 0; i < missingValues.rowCount(); i++) {
assertTrue(Table.compareRows(i, missingValues, missingValues), "Row " + i + " is not equal to itself");
}
}

@Test
void testCompareRowsDifferent() {
Table differentTable = missingValues.copy().sortDescendingOn("Sales");
for(int i = 0; i < missingValues.rowCount(); i++) {
assertFalse(Table.compareRows(i, missingValues, differentTable), "Row " + i + " is equal to a different row");
}
}

@Test
void testCompareRowsDifferentColumns() {
Table differentTable = missingValues.copy().removeColumns("Sales");
for(int i = 0; i < missingValues.rowCount(); i++) {
assertFalse(Table.compareRows(i, missingValues, differentTable), "Row " + i + " is equal to a row with less columns");
}
}

@Test
void testCompareRowsOutOfBound() {
Table differentTable = missingValues.copy().dropRows(0);
int lastRowNumber = missingValues.rowCount() - 1;
assertThrows(IndexOutOfBoundsException.class,
() -> Table.compareRows(lastRowNumber, missingValues, differentTable),
"Row outside range does not throw exception");
}
}

0 comments on commit 7a716f2

Please sign in to comment.