wgtmac commented on code in PR #39393:
URL: https://github.com/apache/arrow/pull/39393#discussion_r1443777679
##########
cpp/src/arrow/io/buffered.h:
##########
@@ -163,5 +164,156 @@ class ARROW_EXPORT BufferedInputStream
std::unique_ptr<Impl> impl_;
};
+/// \class ChunkBufferedInputStream
+/// \brief An ChunkBufferedInputStream that performs buffered reads from an
+/// unbuffered InputStream, which can mitigate the overhead of many small
+/// reads in some cases.
+///
+/// When an actual io request occurs, read ranges will be coalesced if the
+/// distance between them is less than io_merge_threshold, and the actual size
+/// in one io request will be limited by the buffer_size.
+///
+/// \attention It is important to note that since the data returned by the Read
+/// interface is a reference to the Buffer, the caller must ensure that the
data
+/// returned by the Read interface is processed completely before calling the
+/// Read interface again. Otherwise, fatal errors may occur due to data in the
+/// Buffer being overwritten.
+class ARROW_EXPORT ChunkBufferedInputStream : public InputStream {
+ public:
+ static Result<std::shared_ptr<ChunkBufferedInputStream>> Create(
+ int64_t start, int64_t length, std::shared_ptr<RandomAccessFile> impl,
+ std::vector<ReadRange> read_ranges, int64_t buffer_size, int32_t
io_merge_threshold,
+ MemoryPool* pool_ = ::arrow::default_memory_pool());
+
+ // InputStream interface
+ Status Advance(int64_t nbytes) override;
+
+ /// \brief Peek some data without advancing the read position.
+ Result<std::string_view> Peek(int64_t nbytes) override;
+
+ // Readable interface
+ Result<int64_t> Read(int64_t nbytes, void* out) override;
+
+ Result<std::shared_ptr<Buffer>> Read(int64_t nbytes) override;
+
+ // FileInterface
+ Status Close() override;
+
+ Result<int64_t> Tell() const override;
+
+ bool closed() const override;
+
+ private:
+ explicit ChunkBufferedInputStream(int64_t start, int64_t length,
+ std::shared_ptr<RandomAccessFile> impl,
+ std::vector<ReadRange> read_ranges,
+ int64_t buffer_size, int32_t
io_merge_threshold,
+ std::shared_ptr<ResizableBuffer> buffer);
+
+ inline Status CheckClosed() const {
+ if (ARROW_PREDICT_TRUE(is_open_)) {
+ return Status::OK();
+ }
+
+ return Status::IOError("Operation forbidden on closed file input stream");
+ }
+
+ inline Status CheckReadRange(int64_t nbytes) {
+ // Upon reaching the end of the current read range, if there is a next read
+ // range, current_range_available_bytes_ will be set to the length of the
+ // next read range, and this will be ensured by ConsumeBuffer;
+ if (ARROW_PREDICT_TRUE(current_range_available_bytes_ >= nbytes)) {
+ return Status::OK();
+ }
+
+ if (current_range_available_bytes_ > 0 && current_range_available_bytes_ <
nbytes) {
+ if (read_ranges_[read_ranges_idx_].length > nbytes) {
+ return Status::IOError("Read length is illegal");
+ }
+
+ // At the beginning of a new read range and required bytes is more than
+ // read range length, we think it's ok because there are some tentative
+ // read requests when getting next page data.
+ return Status::OK();
+ }
+
+ if (read_ranges_idx_ != read_ranges_.size()) {
Review Comment:
Why not checking this before line 230?
##########
cpp/src/parquet/properties.cc:
##########
@@ -27,8 +27,44 @@
namespace parquet {
+namespace {
+
+static void CheckReadPosition(int64_t offset, int64_t length,
+ const std::shared_ptr<ArrowInputFile>& source) {
+ PARQUET_ASSIGN_OR_THROW(auto size, source->GetSize());
+
+ if (offset < 0) {
+ throw ParquetException("Invalid read offset ", offset);
+ }
+
+ if (length < 0) {
+ throw ParquetException("Invalid read length ", length);
+ }
+
+ if (offset + length > size) {
+ std::stringstream ss;
+ ss << "Tried reading " << length << " bytes starting at position " <<
offset
+ << " from file but only got " << (size - offset);
+ throw ParquetException(ss.str());
+ }
+}
+
+} // namespace
+
std::shared_ptr<ArrowInputStream> ReaderProperties::GetStream(
- std::shared_ptr<ArrowInputFile> source, int64_t start, int64_t num_bytes) {
+ std::shared_ptr<ArrowInputFile> source, int64_t start, int64_t num_bytes,
+ const ReadRanges* read_ranges) {
+ if (read_ranges != nullptr) {
+ CheckReadPosition(start, num_bytes, source);
+
+ // Prefetch data
+ PARQUET_THROW_NOT_OK(source->WillNeed(*read_ranges));
Review Comment:
What will happen if the read behavior of `source->WillNeed(*read_ranges)` is
different from the IO merging process in the ChunkBufferedInputStream?
##########
cpp/src/parquet/properties.h:
##########
@@ -64,7 +69,8 @@ class PARQUET_EXPORT ReaderProperties {
MemoryPool* memory_pool() const { return pool_; }
std::shared_ptr<ArrowInputStream> GetStream(std::shared_ptr<ArrowInputFile>
source,
- int64_t start, int64_t
num_bytes);
+ int64_t start, int64_t num_bytes,
+ const ReadRanges* read_ranges =
NULLPTR);
Review Comment:
This function now returns two different streams depending on the
availability of the read_ranges input. It would be good to add some comment to
make it clear.
##########
cpp/src/arrow/io/buffered.h:
##########
@@ -163,5 +164,156 @@ class ARROW_EXPORT BufferedInputStream
std::unique_ptr<Impl> impl_;
};
+/// \class ChunkBufferedInputStream
+/// \brief An ChunkBufferedInputStream that performs buffered reads from an
+/// unbuffered InputStream, which can mitigate the overhead of many small
+/// reads in some cases.
+///
+/// When an actual io request occurs, read ranges will be coalesced if the
+/// distance between them is less than io_merge_threshold, and the actual size
+/// in one io request will be limited by the buffer_size.
+///
+/// \attention It is important to note that since the data returned by the Read
Review Comment:
This breaks the contract of the InputStream interface.
##########
cpp/src/arrow/io/buffered.h:
##########
@@ -163,5 +164,156 @@ class ARROW_EXPORT BufferedInputStream
std::unique_ptr<Impl> impl_;
};
+/// \class ChunkBufferedInputStream
+/// \brief An ChunkBufferedInputStream that performs buffered reads from an
+/// unbuffered InputStream, which can mitigate the overhead of many small
+/// reads in some cases.
+///
+/// When an actual io request occurs, read ranges will be coalesced if the
+/// distance between them is less than io_merge_threshold, and the actual size
+/// in one io request will be limited by the buffer_size.
+///
+/// \attention It is important to note that since the data returned by the Read
+/// interface is a reference to the Buffer, the caller must ensure that the
data
+/// returned by the Read interface is processed completely before calling the
+/// Read interface again. Otherwise, fatal errors may occur due to data in the
+/// Buffer being overwritten.
+class ARROW_EXPORT ChunkBufferedInputStream : public InputStream {
+ public:
+ static Result<std::shared_ptr<ChunkBufferedInputStream>> Create(
+ int64_t start, int64_t length, std::shared_ptr<RandomAccessFile> impl,
+ std::vector<ReadRange> read_ranges, int64_t buffer_size, int32_t
io_merge_threshold,
+ MemoryPool* pool_ = ::arrow::default_memory_pool());
+
+ // InputStream interface
+ Status Advance(int64_t nbytes) override;
+
+ /// \brief Peek some data without advancing the read position.
+ Result<std::string_view> Peek(int64_t nbytes) override;
+
+ // Readable interface
+ Result<int64_t> Read(int64_t nbytes, void* out) override;
+
+ Result<std::shared_ptr<Buffer>> Read(int64_t nbytes) override;
+
+ // FileInterface
+ Status Close() override;
+
+ Result<int64_t> Tell() const override;
+
+ bool closed() const override;
+
+ private:
+ explicit ChunkBufferedInputStream(int64_t start, int64_t length,
+ std::shared_ptr<RandomAccessFile> impl,
+ std::vector<ReadRange> read_ranges,
+ int64_t buffer_size, int32_t
io_merge_threshold,
+ std::shared_ptr<ResizableBuffer> buffer);
+
+ inline Status CheckClosed() const {
+ if (ARROW_PREDICT_TRUE(is_open_)) {
+ return Status::OK();
+ }
+
+ return Status::IOError("Operation forbidden on closed file input stream");
+ }
+
+ inline Status CheckReadRange(int64_t nbytes) {
+ // Upon reaching the end of the current read range, if there is a next read
+ // range, current_range_available_bytes_ will be set to the length of the
+ // next read range, and this will be ensured by ConsumeBuffer;
+ if (ARROW_PREDICT_TRUE(current_range_available_bytes_ >= nbytes)) {
+ return Status::OK();
+ }
+
+ if (current_range_available_bytes_ > 0 && current_range_available_bytes_ <
nbytes) {
+ if (read_ranges_[read_ranges_idx_].length > nbytes) {
+ return Status::IOError("Read length is illegal");
+ }
+
+ // At the beginning of a new read range and required bytes is more than
+ // read range length, we think it's ok because there are some tentative
+ // read requests when getting next page data.
Review Comment:
Here is for checking the record boundary of a repeated column?
##########
cpp/src/arrow/io/metrics.h:
##########
@@ -0,0 +1,39 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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.
+
+#pragma once
+
+#include "arrow/metrics.h"
+
+namespace arrow::io {
+
+struct IoMetrics {
+ Metric io_time;
Review Comment:
We may need extra metrics like number of actual I/Os and others to
effectiveness evaluation.
##########
cpp/src/arrow/io/buffered.h:
##########
@@ -163,5 +164,156 @@ class ARROW_EXPORT BufferedInputStream
std::unique_ptr<Impl> impl_;
};
+/// \class ChunkBufferedInputStream
+/// \brief An ChunkBufferedInputStream that performs buffered reads from an
+/// unbuffered InputStream, which can mitigate the overhead of many small
+/// reads in some cases.
+///
+/// When an actual io request occurs, read ranges will be coalesced if the
+/// distance between them is less than io_merge_threshold, and the actual size
+/// in one io request will be limited by the buffer_size.
+///
+/// \attention It is important to note that since the data returned by the Read
+/// interface is a reference to the Buffer, the caller must ensure that the
data
+/// returned by the Read interface is processed completely before calling the
+/// Read interface again. Otherwise, fatal errors may occur due to data in the
+/// Buffer being overwritten.
+class ARROW_EXPORT ChunkBufferedInputStream : public InputStream {
Review Comment:
> When `pre_buffer = true`, `ReadRangeCache` will be used to perform IO
coalescence between columns, only when `pre_buffer = false` and
`buffered_stream_enabled_ = true`, `BufferedInputStream` will be used to do
single column IO. `ChunkBufferedInputStream` does the same thing with
`BufferedInputStream`, while also providing simple IO merging capabilities, and
shielding the upper layer from the discontinuity of IO.
Do you have a plan to implement `prebuffer` feature when ReadRanges are
provided? It seems that we are still uncapable of doing this, which I think it
is worth doing.
##########
cpp/src/parquet/row_ranges.h:
##########
@@ -0,0 +1,201 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you 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.
+
+#pragma once
Review Comment:
@huberylee Do you mind taking a look at this patch:
https://github.com/apache/arrow/pull/38867? It does similar feature but use a
different approach discussed in the doc without optimization I/O and would be
good to consolidate the effort with yours.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]