Skip to content

Commit 689547c

Browse files
GH-3449: Fix BinaryStats int overflow (#3448)
1 parent 64c35db commit 689547c

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

parquet-column/src/main/java/org/apache/parquet/column/statistics/BinaryStatistics.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,16 +105,16 @@ String stringify(Binary value) {
105105

106106
@Override
107107
public boolean isSmallerThan(long size) {
108-
return !hasNonNullValue() || ((min.length() + max.length()) < size);
108+
return !hasNonNullValue() || (((long) min.length() + max.length()) < size);
109109
}
110110

111111
public boolean isSmallerThanWithTruncation(long size, int truncationLength) {
112112
if (!hasNonNullValue()) {
113113
return true;
114114
}
115115

116-
int minTruncateLength = Math.min(min.length(), truncationLength);
117-
int maxTruncateLength = Math.min(max.length(), truncationLength);
116+
long minTruncateLength = Math.min(min.length(), truncationLength);
117+
long maxTruncateLength = Math.min(max.length(), truncationLength);
118118

119119
return minTruncateLength + maxTruncateLength < size;
120120
}

parquet-column/src/test/java/org/apache/parquet/column/statistics/TestStatistics.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,4 +927,15 @@ public void testNoopStatistics() {
927927
assertThrows(UnsupportedOperationException.class, stats::minAsString);
928928
assertThrows(UnsupportedOperationException.class, () -> stats.isSmallerThan(0));
929929
}
930+
931+
@Test
932+
public void testBinaryIsSmallerThanNoOverflowForLargeValues() {
933+
BinaryStatistics stats = new BinaryStatistics();
934+
// Create a Binary whose length() reports 2^30 without allocating 1 GB
935+
Binary fakeLarge = Binary.fromConstantByteArray(new byte[0], 0, 1 << 30);
936+
stats.updateStats(fakeLarge);
937+
938+
// min.length() + max.length() = 2^31, must not overflow int to negative
939+
assertFalse(stats.isSmallerThan(4096));
940+
}
930941
}

0 commit comments

Comments
 (0)