-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Read parquet column chunks in small steps #15374
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
raunaqmorarka
merged 6 commits into
trinodb:master
from
starburstdata:ls/054-parquet-small-buffer
Dec 23, 2022
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
74db80d
Implement toString on ReferenceCountedReader
lukasz-stec c13474b
Fix processValues javadoc formating
lukasz-stec cc18eb1
Rename TestHdfsParquetDataSource to TestParquetDataSource
lukasz-stec 5c64bce
Move TestParquetDataSource to trino-parquet
lukasz-stec 7be2201
Read parquet column chunks in small steps
lukasz-stec b581c52
Support reading large parquet column chunks
lukasz-stec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
lib/trino-parquet/src/main/java/io/trino/parquet/reader/ChunkedInputStream.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.trino.parquet.reader; | ||
|
|
||
| import io.airlift.slice.BasicSliceInput; | ||
| import io.airlift.slice.Slice; | ||
| import io.airlift.slice.Slices; | ||
| import io.trino.parquet.ChunkReader; | ||
| import io.trino.parquet.ParquetReaderOptions; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.util.Collection; | ||
| import java.util.Iterator; | ||
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
| import static com.google.common.base.Preconditions.checkPositionIndexes; | ||
| import static com.google.common.io.ByteStreams.readFully; | ||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| /** | ||
| * A single continuous {@link InputStream} over multiple {@link Slice}s read on demand using given collection of {@link ChunkReader}s. | ||
| * It is used to read parquet column chunk in limited (small) byte chunks (8MB by default, controlled by {@link ParquetReaderOptions#getMaxReadBlockSize()}). | ||
| * Column chunks consists of multiple pages. | ||
| * This abstraction is used because the page size is unknown until the page header is read | ||
| * and page header and page data can be split between two or more byte chunks. | ||
| */ | ||
| public final class ChunkedInputStream | ||
| extends InputStream | ||
| { | ||
| private final Iterator<? extends ChunkReader> chunks; | ||
| private ChunkReader currentChunkReader; | ||
| private BasicSliceInput current; | ||
|
|
||
| public ChunkedInputStream(Collection<? extends ChunkReader> chunks) | ||
| { | ||
| requireNonNull(chunks, "chunks is null"); | ||
| checkArgument(!chunks.isEmpty(), "At least one chunk is expected but got none"); | ||
| this.chunks = chunks.iterator(); | ||
| readNextChunk(); | ||
| } | ||
|
|
||
| public Slice getSlice(int length) | ||
| throws IOException | ||
| { | ||
| if (length == 0) { | ||
| return Slices.EMPTY_SLICE; | ||
| } | ||
| ensureOpen(); | ||
| while (!current.isReadable()) { | ||
| checkArgument(chunks.hasNext(), "Requested %s bytes but 0 was available", length); | ||
| readNextChunk(); | ||
| } | ||
| if (current.available() >= length) { | ||
| return current.readSlice(length); | ||
| } | ||
| // requested length crosses the slice boundary | ||
| byte[] bytes = new byte[length]; | ||
| try { | ||
| readFully(this, bytes, 0, bytes.length); | ||
| } | ||
| catch (IOException e) { | ||
| throw new RuntimeException("Failed to read " + length + " bytes", e); | ||
| } | ||
| return Slices.wrappedBuffer(bytes); | ||
| } | ||
|
|
||
| @Override | ||
| public int read(byte[] b, int off, int len) | ||
| throws IOException | ||
| { | ||
| checkPositionIndexes(off, off + len, b.length); | ||
| if (len == 0) { | ||
| return 0; | ||
| } | ||
| ensureOpen(); | ||
| while (!current.isReadable()) { | ||
| if (!chunks.hasNext()) { | ||
| return -1; | ||
| } | ||
| readNextChunk(); | ||
| } | ||
|
|
||
| return current.read(b, off, len); | ||
| } | ||
|
|
||
| @Override | ||
| public int read() | ||
| throws IOException | ||
| { | ||
| ensureOpen(); | ||
| while (!current.isReadable() && chunks.hasNext()) { | ||
| readNextChunk(); | ||
| } | ||
|
|
||
| return current.read(); | ||
| } | ||
|
|
||
|
lukasz-stec marked this conversation as resolved.
Outdated
|
||
| @Override | ||
| public int available() | ||
| throws IOException | ||
| { | ||
|
raunaqmorarka marked this conversation as resolved.
Outdated
|
||
| ensureOpen(); | ||
| return current.available(); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() | ||
| { | ||
|
lukasz-stec marked this conversation as resolved.
Outdated
|
||
| if (current == null) { | ||
| // already closed | ||
| return; | ||
| } | ||
| currentChunkReader.free(); | ||
|
raunaqmorarka marked this conversation as resolved.
|
||
|
|
||
| while (chunks.hasNext()) { | ||
| chunks.next().free(); | ||
|
raunaqmorarka marked this conversation as resolved.
|
||
| } | ||
| current = null; | ||
| } | ||
|
|
||
| private void ensureOpen() | ||
| throws IOException | ||
| { | ||
| if (current == null) { | ||
| throw new IOException("Stream closed"); | ||
| } | ||
| } | ||
|
|
||
| private void readNextChunk() | ||
| { | ||
| if (currentChunkReader != null) { | ||
| currentChunkReader.free(); | ||
|
raunaqmorarka marked this conversation as resolved.
|
||
| } | ||
| currentChunkReader = chunks.next(); | ||
| Slice slice = currentChunkReader.readUnchecked(); | ||
| checkArgument(slice.length() > 0, "all chunks have to be not empty"); | ||
| current = slice.getInput(); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.