Skip to content

Commit ef2d480

Browse files
authored
[core] Add hashCode to AbstractFileStoreTable for consistency with equals (#7616)
1 parent 70110f2 commit ef2d480

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,4 +796,9 @@ public boolean equals(Object o) {
796796
AbstractFileStoreTable that = (AbstractFileStoreTable) o;
797797
return Objects.equals(path, that.path) && Objects.equals(tableSchema, that.tableSchema);
798798
}
799+
800+
@Override
801+
public int hashCode() {
802+
return Objects.hash(path, tableSchema);
803+
}
799804
}

paimon-core/src/test/java/org/apache/paimon/table/AppendOnlySimpleTableTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,33 @@ public void testBatchOrderWithCompaction() throws Exception {
15651565
}
15661566
}
15671567

1568+
@Test
1569+
public void testEqualsAndHashCode() throws Exception {
1570+
// Test same table equals and hashCode consistency
1571+
FileStoreTable table1 = createFileStoreTable();
1572+
FileStoreTable table2 = table1.copy(table1.schema());
1573+
assertThat(table1.equals(table2)).isTrue();
1574+
assertThat(table1.hashCode()).isEqualTo(table2.hashCode());
1575+
1576+
// Test with different options
1577+
Map<String, String> optionsWithMock = new HashMap<>(table1.schema().options());
1578+
optionsWithMock.put("mockKey", "mockValue");
1579+
TableSchema schemaWithMock = table1.schema().copy(optionsWithMock);
1580+
FileStoreTable tableWithMock = table1.copy(schemaWithMock);
1581+
1582+
assertThat(table1.equals(tableWithMock)).isFalse();
1583+
assertThat(table1.hashCode()).isNotEqualTo(tableWithMock.hashCode());
1584+
1585+
// Test same options should be equal
1586+
Map<String, String> sameOptionsWithMock = new HashMap<>(table1.schema().options());
1587+
sameOptionsWithMock.put("mockKey", "mockValue");
1588+
TableSchema sameSchemaWithMock = table1.schema().copy(sameOptionsWithMock);
1589+
FileStoreTable sameTableWithMock = table1.copy(sameSchemaWithMock);
1590+
1591+
assertThat(tableWithMock.equals(sameTableWithMock)).isTrue();
1592+
assertThat(tableWithMock.hashCode()).isEqualTo(sameTableWithMock.hashCode());
1593+
}
1594+
15681595
private void writeData() throws Exception {
15691596
writeData(options -> {});
15701597
}

paimon-core/src/test/java/org/apache/paimon/table/PrimaryKeySimpleTableTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2473,6 +2473,33 @@ public void testForceLookupCompaction(CoreOptions.MergeEngine mergeEngine) throw
24732473
"1|10|100|binary|varbinary|mapKey:mapVal|multiset"));
24742474
}
24752475

2476+
@Test
2477+
public void testEqualsAndHashCode() throws Exception {
2478+
// Test same table equals and hashCode consistency
2479+
FileStoreTable table1 = createFileStoreTable();
2480+
FileStoreTable table2 = table1.copy(table1.schema());
2481+
assertThat(table1.equals(table2)).isTrue();
2482+
assertThat(table1.hashCode()).isEqualTo(table2.hashCode());
2483+
2484+
// Test with different options
2485+
Map<String, String> optionsWithMock = new HashMap<>(table1.schema().options());
2486+
optionsWithMock.put("mockKey", "mockValue");
2487+
TableSchema schemaWithMock = table1.schema().copy(optionsWithMock);
2488+
FileStoreTable tableWithMock = table1.copy(schemaWithMock);
2489+
2490+
assertThat(table1.equals(tableWithMock)).isFalse();
2491+
assertThat(table1.hashCode()).isNotEqualTo(tableWithMock.hashCode());
2492+
2493+
// Test same options should be equal
2494+
Map<String, String> sameOptionsWithMock = new HashMap<>(table1.schema().options());
2495+
sameOptionsWithMock.put("mockKey", "mockValue");
2496+
TableSchema sameSchemaWithMock = table1.schema().copy(sameOptionsWithMock);
2497+
FileStoreTable sameTableWithMock = table1.copy(sameSchemaWithMock);
2498+
2499+
assertThat(tableWithMock.equals(sameTableWithMock)).isTrue();
2500+
assertThat(tableWithMock.hashCode()).isEqualTo(sameTableWithMock.hashCode());
2501+
}
2502+
24762503
private void assertReadChangelog(int id, FileStoreTable table) throws Exception {
24772504
// read the changelog at #{id}
24782505
table = table.copy(singletonMap(CoreOptions.SCAN_SNAPSHOT_ID.key(), String.valueOf(id)));

0 commit comments

Comments
 (0)