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..61720e1bb0 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 @@ -29,10 +29,26 @@ public interface PageReader extends AutoCloseable { DictionaryPage readDictionaryPage(); /** - * @return the total number of values in the column chunk + * @return the total number of values this reader will yield - which may be a filtered subset of + * the column chunk's values, e.g. when row-range/column-index filtering is applied */ long getTotalValueCount(); + /** + * @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 {@code org.apache.parquet.ParquetReadOptions#getMaxAllocationSize()} + * does internally) rather than allocating the full reported size in one call. + */ + default long getTotalUncompressedSize() { + 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..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 @@ -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 getTotalUncompressedSize() { + return uncompressedSize; + } + @Override public DataPage readPage() { final DataPage compressedPage = compressedPages.poll(); 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())) {