From 87dfe0b69443a01ea2acb661dd6ecfdfc9ffdc60 Mon Sep 17 00:00:00 2001 From: Edson Date: Thu, 1 Oct 2026 01:37:46 -0400 Subject: [PATCH 1/2] fix(java): include baseId in DataFile/DeletionFile equals and add hashCode DataFile and DeletionFile overrode equals() without hashCode(), so equal instances got identity hash codes. FragmentMetadata.hashCode() hashes both, which made equal FragmentMetadata objects hash differently and miss in HashSet/HashMap lookups. Both equals() methods also ignored baseId, so the same relative path under two different bases compared equal. The Rust DataFile and DeletionFile derive PartialEq over all fields, including base_id. DataOverlayFile.hashCode() now delegates to DataFile.hashCode() instead of hashing DataFile fields by hand. --- .../java/org/lance/fragment/DataFile.java | 11 ++- .../java/org/lance/fragment/DeletionFile.java | 8 +- .../java/org/lance/operation/DataOverlay.java | 10 +-- .../lance/fragment/DataFileEqualityTest.java | 80 +++++++++++++++++++ 4 files changed, 98 insertions(+), 11 deletions(-) create mode 100644 java/src/test/java/org/lance/fragment/DataFileEqualityTest.java diff --git a/java/src/main/java/org/lance/fragment/DataFile.java b/java/src/main/java/org/lance/fragment/DataFile.java index 1120a5286c1..9755c271d67 100644 --- a/java/src/main/java/org/lance/fragment/DataFile.java +++ b/java/src/main/java/org/lance/fragment/DataFile.java @@ -85,7 +85,16 @@ public boolean equals(Object o) { && Objects.equals(path, that.path) && Arrays.equals(fields, that.fields) && Arrays.equals(columnIndices, that.columnIndices) - && Objects.equals(fileSizeBytes, that.fileSizeBytes); + && Objects.equals(fileSizeBytes, that.fileSizeBytes) + && Objects.equals(baseId, that.baseId); + } + + @Override + public int hashCode() { + int result = Objects.hash(path, fileMajorVersion, fileMinorVersion, fileSizeBytes, baseId); + result = 31 * result + Arrays.hashCode(fields); + result = 31 * result + Arrays.hashCode(columnIndices); + return result; } @Override diff --git a/java/src/main/java/org/lance/fragment/DeletionFile.java b/java/src/main/java/org/lance/fragment/DeletionFile.java index 6fa17a37c3f..2eed2fafb0f 100644 --- a/java/src/main/java/org/lance/fragment/DeletionFile.java +++ b/java/src/main/java/org/lance/fragment/DeletionFile.java @@ -95,7 +95,13 @@ public boolean equals(Object o) { return id == that.id && readVersion == that.readVersion && fileType == that.fileType - && Objects.equals(numDeletedRows, that.numDeletedRows); + && Objects.equals(numDeletedRows, that.numDeletedRows) + && Objects.equals(baseId, that.baseId); + } + + @Override + public int hashCode() { + return Objects.hash(id, readVersion, numDeletedRows, fileType, baseId); } @Override diff --git a/java/src/main/java/org/lance/operation/DataOverlay.java b/java/src/main/java/org/lance/operation/DataOverlay.java index 99abf84b797..57f5ed83f55 100644 --- a/java/src/main/java/org/lance/operation/DataOverlay.java +++ b/java/src/main/java/org/lance/operation/DataOverlay.java @@ -161,15 +161,7 @@ public boolean equals(Object o) { @Override public int hashCode() { - int dataFileHash = - Objects.hash( - dataFile.getPath(), - dataFile.getFileMajorVersion(), - dataFile.getFileMinorVersion(), - dataFile.getFileSizeBytes()); - dataFileHash = 31 * dataFileHash + Arrays.hashCode(dataFile.getFields()); - dataFileHash = 31 * dataFileHash + Arrays.hashCode(dataFile.getColumnIndices()); - return Objects.hash(dataFileHash, coverage, committedVersion); + return Objects.hash(dataFile, coverage, committedVersion); } } diff --git a/java/src/test/java/org/lance/fragment/DataFileEqualityTest.java b/java/src/test/java/org/lance/fragment/DataFileEqualityTest.java new file mode 100644 index 00000000000..0e47d06f5f5 --- /dev/null +++ b/java/src/test/java/org/lance/fragment/DataFileEqualityTest.java @@ -0,0 +1,80 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.lance.fragment; + +import org.lance.FragmentMetadata; + +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Equality contract of fragment metadata value classes. */ +public class DataFileEqualityTest { + + private static DataFile dataFile(Integer baseId) { + return new DataFile("data/a.lance", new int[] {0, 1}, new int[] {0, 1}, 2, 1, 1024L, baseId); + } + + private static DeletionFile deletionFile(Integer baseId) { + return new DeletionFile(7L, 3L, 5L, DeletionFileType.BITMAP, baseId); + } + + @Test + void testDataFileEqualObjectsHaveEqualHashCodes() { + DataFile a = dataFile(null); + DataFile b = dataFile(null); + assertEquals(a, b); + assertEquals(a.hashCode(), b.hashCode()); + } + + @Test + void testDataFileEqualsConsidersBaseId() { + // Same relative path under different bases refers to different physical files. + assertNotEquals(dataFile(1), dataFile(2)); + assertNotEquals(dataFile(null), dataFile(1)); + } + + @Test + void testDeletionFileEqualObjectsHaveEqualHashCodes() { + DeletionFile a = deletionFile(null); + DeletionFile b = deletionFile(null); + assertEquals(a, b); + assertEquals(a.hashCode(), b.hashCode()); + } + + @Test + void testDeletionFileEqualsConsidersBaseId() { + assertNotEquals(deletionFile(1), deletionFile(2)); + } + + @Test + void testEqualFragmentMetadataWorksInHashSet() { + FragmentMetadata a = + new FragmentMetadata( + 0, Collections.singletonList(dataFile(null)), 100L, deletionFile(null), null); + FragmentMetadata b = + new FragmentMetadata( + 0, Collections.singletonList(dataFile(null)), 100L, deletionFile(null), null); + assertEquals(a, b); + assertEquals(a.hashCode(), b.hashCode()); + Set set = new HashSet<>(Collections.singletonList(a)); + assertTrue(set.contains(b)); + } +} From 175209305528796610981bc9adc600839e2cfc06 Mon Sep 17 00:00:00 2001 From: Edson Date: Fri, 2 Oct 2026 11:04:03 -0400 Subject: [PATCH 2/2] test(java): merge DataFile/DeletionFile equality tests and cover a non-null baseId --- .../lance/fragment/DataFileEqualityTest.java | 29 +++++++------------ 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/java/src/test/java/org/lance/fragment/DataFileEqualityTest.java b/java/src/test/java/org/lance/fragment/DataFileEqualityTest.java index 0e47d06f5f5..1726cda1727 100644 --- a/java/src/test/java/org/lance/fragment/DataFileEqualityTest.java +++ b/java/src/test/java/org/lance/fragment/DataFileEqualityTest.java @@ -37,31 +37,24 @@ private static DeletionFile deletionFile(Integer baseId) { } @Test - void testDataFileEqualObjectsHaveEqualHashCodes() { - DataFile a = dataFile(null); - DataFile b = dataFile(null); - assertEquals(a, b); - assertEquals(a.hashCode(), b.hashCode()); - } - - @Test - void testDataFileEqualsConsidersBaseId() { + void testDataFileEqualsAndHashCodeIncludeBaseId() { + for (Integer baseId : new Integer[] {null, 1}) { + assertEquals(dataFile(baseId), dataFile(baseId)); + assertEquals(dataFile(baseId).hashCode(), dataFile(baseId).hashCode()); + } // Same relative path under different bases refers to different physical files. assertNotEquals(dataFile(1), dataFile(2)); assertNotEquals(dataFile(null), dataFile(1)); } @Test - void testDeletionFileEqualObjectsHaveEqualHashCodes() { - DeletionFile a = deletionFile(null); - DeletionFile b = deletionFile(null); - assertEquals(a, b); - assertEquals(a.hashCode(), b.hashCode()); - } - - @Test - void testDeletionFileEqualsConsidersBaseId() { + void testDeletionFileEqualsAndHashCodeIncludeBaseId() { + for (Integer baseId : new Integer[] {null, 1}) { + assertEquals(deletionFile(baseId), deletionFile(baseId)); + assertEquals(deletionFile(baseId).hashCode(), deletionFile(baseId).hashCode()); + } assertNotEquals(deletionFile(1), deletionFile(2)); + assertNotEquals(deletionFile(null), deletionFile(1)); } @Test