Skip to content

Commit efacf01

Browse files
committed
Add slicing to fix bug with the comparator
1 parent 5eef533 commit efacf01

2 files changed

Lines changed: 38 additions & 13 deletions

File tree

parquet-column/src/main/java/org/apache/parquet/schema/PrimitiveComparator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,8 @@ public String toString() {
215215
static final PrimitiveComparator<Binary> BINARY_AS_INT96_TIMESTAMP_COMPARATOR = new BinaryComparator() {
216216
@Override
217217
int compareBinary(Binary b1, Binary b2) {
218-
ByteBuffer bb1 = b1.toByteBuffer();
219-
ByteBuffer bb2 = b2.toByteBuffer();
218+
ByteBuffer bb1 = b1.toByteBuffer().slice();
219+
ByteBuffer bb2 = b2.toByteBuffer().slice();
220220
bb1.order(java.nio.ByteOrder.LITTLE_ENDIAN);
221221
bb2.order(java.nio.ByteOrder.LITTLE_ENDIAN);
222222
int jd1 = bb1.getInt(8);

parquet-column/src/test/java/org/apache/parquet/schema/TestPrimitiveComparator.java

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -287,22 +287,47 @@ private Binary timestampToInt96(String timestamp) {
287287
@Test
288288
public void testInt96Comparator() {
289289
Binary[] valuesInAscendingOrder = {
290-
timestampToInt96("2020-01-01T00:00:00.000"),
291-
timestampToInt96("2020-02-29T23:59:59.999"),
292-
timestampToInt96("2020-12-31T23:59:59.999"),
293-
timestampToInt96("2021-01-01T00:00:00.000"),
294-
timestampToInt96("2023-06-15T12:30:45.500"),
295-
timestampToInt96("2024-02-29T15:45:30.750"),
296-
timestampToInt96("2024-12-25T07:00:00.000"),
297-
timestampToInt96("2025-01-01T00:00:00.000"),
298-
timestampToInt96("2025-07-04T20:00:00.000"),
299-
timestampToInt96("2025-12-31T23:59:59.999")
290+
timestampToInt96("2020-01-01T00:00:00.000"),
291+
timestampToInt96("2020-01-01T10:00:00.000"),
292+
timestampToInt96("2020-02-29T23:59:59.999"),
293+
timestampToInt96("2020-12-31T23:59:59.999"),
294+
timestampToInt96("2021-01-01T00:00:00.000"),
295+
timestampToInt96("2023-06-15T12:30:45.500"),
296+
timestampToInt96("2024-02-29T15:45:30.750"),
297+
timestampToInt96("2024-12-25T07:00:00.000"),
298+
timestampToInt96("2025-01-01T00:00:00.000"),
299+
timestampToInt96("2025-07-04T20:00:00.000"),
300+
timestampToInt96("2025-07-04T20:50:00.000"),
301+
timestampToInt96("2025-12-31T23:59:59.999")
300302
};
303+
304+
java.util.function.Function<Binary, Binary>[] perturb = new java.util.function.Function[] {
305+
(java.util.function.Function<Binary, Binary>) b -> b,
306+
(java.util.function.Function<Binary, Binary>) b -> Binary.fromReusedByteArray(b.getBytes()),
307+
(java.util.function.Function<Binary, Binary>) b -> Binary.fromConstantByteArray(b.getBytes()),
308+
(java.util.function.Function<Binary, Binary>) b -> {
309+
byte[] originalBytes = b.getBytes();
310+
byte[] paddedBuffer = new byte[originalBytes.length + 20];
311+
int offset = 10;
312+
for (int i = 0; i < paddedBuffer.length; i++) {
313+
paddedBuffer[i] = (byte) (0xAA + (i % 5));
314+
}
315+
System.arraycopy(originalBytes, 0, paddedBuffer, offset, originalBytes.length);
316+
return Binary.fromReusedByteArray(paddedBuffer, offset, originalBytes.length);
317+
}
318+
};
319+
301320
for (int i = 0; i < valuesInAscendingOrder.length; ++i) {
302321
for (int j = 0; j < valuesInAscendingOrder.length; ++j) {
303322
Binary bi = valuesInAscendingOrder[i];
304323
Binary bj = valuesInAscendingOrder[j];
305-
assertEquals(Integer.compare(i, j), BINARY_AS_INT96_TIMESTAMP_COMPARATOR.compare(bi, bj));
324+
for (java.util.function.Function<Binary, Binary> fi : perturb) {
325+
for (java.util.function.Function<Binary, Binary> fj : perturb) {
326+
Binary perturbedBi = fi.apply(bi);
327+
Binary perturbedBj = fj.apply(bj);
328+
assertEquals(Integer.compare(i, j), BINARY_AS_INT96_TIMESTAMP_COMPARATOR.compare(perturbedBi, perturbedBj));
329+
}
330+
}
306331
}
307332
}
308333
}

0 commit comments

Comments
 (0)