Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@ public interface PageReader extends AutoCloseable {
*/
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 {@link org.apache.parquet.ParquetReadOptions#getMaxAllocationSize()}
* does internally) rather than allocating the full reported size in one call.
*/
default long getTotalUncompressedSize() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sum of the uncompressed sizes of the (data) pages this reader will yield

This seems to be inconsistent with similar methods above?

return -1;
}

/**
* @return the next page in that chunk or null if after the last page
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<DataPage> compressedPages;
private final DictionaryPage compressedDictionaryPage;
// null means no page synchronization is required; firstRowIndex will not be returned by the pages
Expand Down Expand Up @@ -98,10 +99,13 @@ static final class ColumnChunkPageReader implements PageReader {
this.compressedPages = new ArrayDeque<DataPage>(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;
Expand Down Expand Up @@ -131,6 +135,11 @@ public long getTotalValueCount() {
return valueCount;
}

@Override
public long getTotalUncompressedSize() {
return uncompressedSize;
}

@Override
public DataPage readPage() {
final DataPage compressedPage = compressedPages.poll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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())) {
Expand Down
Loading