From a9f9cc0ad4aa43223e9b0042d852678e51e631a3 Mon Sep 17 00:00:00 2001 From: Yuming Wang Date: Mon, 23 Nov 2026 17:12:07 +0800 Subject: [PATCH 1/2] GH-3988: Expose per-column-chunk uncompressed size via PageReader PageReader exposes getTotalValueCount() but not a column chunk's total uncompressed byte size, even though ColumnChunkPageReadStore already sums page value counts in its constructor and could just as easily sum page uncompressed sizes at the same time. Add a default getUncompressedSize() (-1 if unknown) so existing PageReader implementations are unaffected, and implement it in ColumnChunkPageReadStore. Combined with getTotalValueCount(), this lets callers compute an average uncompressed-bytes-per-value for a whole column chunk before decoding any page, which is useful for pre-sizing per-batch buffers in vectorized readers (e.g. Spark's) ahead of reading binary/string columns with large values. This change has no effect by itself; it only exposes a number ColumnChunkPageReadStore already computes. Fixes #3988 --- .../java/org/apache/parquet/column/page/PageReader.java | 9 +++++++++ .../apache/parquet/hadoop/ColumnChunkPageReadStore.java | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/parquet-column/src/main/java/org/apache/parquet/column/page/PageReader.java b/parquet-column/src/main/java/org/apache/parquet/column/page/PageReader.java index 530fc959a0..ea611c9be7 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/page/PageReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/page/PageReader.java @@ -33,6 +33,15 @@ public interface PageReader extends AutoCloseable { */ long getTotalValueCount(); + /** + * @return the sum of the uncompressed sizes of the (data) pages in this column chunk, or -1 if + * unknown. Callers that use this to pre-size a buffer must treat -1 (or any non-positive value) + * as "no estimate available" and fall back to their default behavior. + */ + default long getUncompressedSize() { + return -1; + } + /** * @return the next page in that chunk or null if after the last page */ diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java index a21fec9ab8..da70f50705 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java @@ -70,6 +70,7 @@ static final class ColumnChunkPageReader implements PageReader { private final BytesInputDecompressor decompressor; private final long valueCount; + private final long uncompressedSize; private final Queue compressedPages; private final DictionaryPage compressedDictionaryPage; // null means no page synchronization is required; firstRowIndex will not be returned by the pages @@ -98,10 +99,13 @@ static final class ColumnChunkPageReader implements PageReader { this.compressedPages = new ArrayDeque(compressedPages); this.compressedDictionaryPage = compressedDictionaryPage; long count = 0; + long uncompressed = 0; for (DataPage p : compressedPages) { count += p.getValueCount(); + uncompressed += p.getUncompressedSize(); } this.valueCount = count; + this.uncompressedSize = uncompressed; this.offsetIndex = offsetIndex; this.rowCount = rowCount; this.options = options; @@ -131,6 +135,11 @@ public long getTotalValueCount() { return valueCount; } + @Override + public long getUncompressedSize() { + return uncompressedSize; + } + @Override public DataPage readPage() { final DataPage compressedPage = compressedPages.poll(); From 165ea3897b369f08e89bc5aaf746cd41997840ef Mon Sep 17 00:00:00 2001 From: Yuming Wang Date: Tue, 24 Nov 2026 13:46:17 +0800 Subject: [PATCH 2/2] GH-3988: Rename getUncompressedSize to getTotalUncompressedSize, add tests and doc fixes Deep review found the new PageReader method name collided in spirit with the per-page Page.getUncompressedSize() (used a dozen times in the very same ColumnChunkPageReadStore.java) and broke the interface's own getTotalValueCount() naming convention, as well as the existing ColumnChunkMetaData.getTotalUncompressedSize(). Renamed now while the method is still unreleased, since this would be a breaking change after a release. Also: - Added unit test coverage: an assertion using the existing known-size DataPageV1 fixture, plus a new multi-page summation test. Verified both catch a regression by temporarily breaking the accumulator and confirming they fail, then restoring it and confirming they pass. - Clarified the Javadoc: the value reflects pages this reader will yield (which may be a filtered subset under row-range/column-index filtering), not unconditionally the whole column chunk; only -1 (not any non-positive value) means "unknown", since 0 is a legitimate answer for an empty chunk; and the value is derived from unvalidated page-header metadata, so callers pre-sizing an allocation from it should bound/chunk it rather than allocating the full reported size in one call. Co-Authored-By: Claude Sonnet 5 --- .../apache/parquet/column/page/PageReader.java | 14 ++++++++++---- .../hadoop/ColumnChunkPageReadStore.java | 2 +- .../hadoop/TestColumnChunkPageReadStore.java | 17 +++++++++++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/parquet-column/src/main/java/org/apache/parquet/column/page/PageReader.java b/parquet-column/src/main/java/org/apache/parquet/column/page/PageReader.java index ea611c9be7..56909ec25b 100644 --- a/parquet-column/src/main/java/org/apache/parquet/column/page/PageReader.java +++ b/parquet-column/src/main/java/org/apache/parquet/column/page/PageReader.java @@ -34,11 +34,17 @@ public interface PageReader extends AutoCloseable { long getTotalValueCount(); /** - * @return the sum of the uncompressed sizes of the (data) pages in this column chunk, or -1 if - * unknown. Callers that use this to pre-size a buffer must treat -1 (or any non-positive value) - * as "no estimate available" and fall back to their default behavior. + * @return the sum of the uncompressed sizes of the (data) pages this reader will yield - which may + * be a filtered subset of the column chunk's pages, e.g. when row-range/column-index filtering is + * applied - or exactly -1 if unknown. A return value of 0 is a legitimate, meaningful result (for + * example an empty column chunk), distinct from "unknown"; callers that use this to pre-size a + * buffer must treat only -1 as "no estimate available" and fall back to their default behavior. + * This value is derived from unvalidated page-header metadata in the Parquet file, not verified + * against the actual bytes read; callers that pre-size an allocation from it should bound or chunk + * that allocation (e.g. as {@link org.apache.parquet.ParquetReadOptions#getMaxAllocationSize()} + * does internally) rather than allocating the full reported size in one call. */ - default long getUncompressedSize() { + default long getTotalUncompressedSize() { return -1; } diff --git a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java index da70f50705..2952821d5a 100644 --- a/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java +++ b/parquet-hadoop/src/main/java/org/apache/parquet/hadoop/ColumnChunkPageReadStore.java @@ -136,7 +136,7 @@ public long getTotalValueCount() { } @Override - public long getUncompressedSize() { + public long getTotalUncompressedSize() { return uncompressedSize; } diff --git a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestColumnChunkPageReadStore.java b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestColumnChunkPageReadStore.java index d83bae46bd..ac5bb3cc77 100644 --- a/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestColumnChunkPageReadStore.java +++ b/parquet-hadoop/src/test/java/org/apache/parquet/hadoop/TestColumnChunkPageReadStore.java @@ -28,6 +28,7 @@ import static org.mockito.Mockito.verify; import java.nio.ByteBuffer; +import java.util.Arrays; import java.util.Collections; import org.apache.parquet.ParquetReadOptions; import org.apache.parquet.bytes.ByteBufferAllocator; @@ -74,12 +75,28 @@ public void heapDataPagesAreNotRetainedUntilRowGroupClose() throws Exception { NOOP_DECOMPRESSOR, Collections.singletonList(page), null, null, 1L, null, null, 0, 0, options); try (ColumnChunkPageReadStore store = new ColumnChunkPageReadStore(1L)) { store.addColumn(COLUMN, reader); + assertThat(reader.getTotalUncompressedSize()).isEqualTo(bytes.length); assertThat(((DataPageV1) reader.readPage()).getBytes().toByteArray()) .isEqualTo(bytes); verify(allocator, never()).allocate(anyInt()); } } + @Test + public void getTotalUncompressedSizeSumsAcrossMultiplePages() { + // Declared (header) uncompressed sizes, not the backing array lengths, are what the accumulator + // sums - distinct values here (4, 7, 10) prove it sums per-page sizes rather than e.g. returning + // just the last page's size. + DataPageV1 page1 = new DataPageV1(BytesInput.from(new byte[4]), 1, 4, null, RLE, RLE, PLAIN); + DataPageV1 page2 = new DataPageV1(BytesInput.from(new byte[4]), 1, 7, null, RLE, RLE, PLAIN); + DataPageV1 page3 = new DataPageV1(BytesInput.from(new byte[4]), 1, 10, null, RLE, RLE, PLAIN); + ParquetReadOptions options = ParquetReadOptions.builder().build(); + ColumnChunkPageReader reader = new ColumnChunkPageReader( + NOOP_DECOMPRESSOR, Arrays.asList(page1, page2, page3), null, null, 3L, null, null, 0, 0, options); + + assertThat(reader.getTotalUncompressedSize()).isEqualTo(21); + } + @Test public void closeWithoutSetReleaserDoesNotThrow() { try (TrackingByteBufferAllocator allocator = TrackingByteBufferAllocator.wrap(new HeapByteBufferAllocator())) {