Skip to content

Commit a1575c8

Browse files
Add support for batch_size in the ORC Scanner (Dataset) (apache#47)
1 parent cded559 commit a1575c8

3 files changed

Lines changed: 56 additions & 12 deletions

File tree

cpp/src/arrow/adapters/orc/adapter.cc

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,37 @@ class ORCFileReader::Impl {
438438
return Status::OK();
439439
}
440440

441+
Status NextStripeReader(int64_t batch_size, const std::vector<std::string>& include_names,
442+
std::shared_ptr<RecordBatchReader>* out) {
443+
if (current_row_ >= NumberOfRows()) {
444+
out->reset();
445+
return Status::OK();
446+
}
447+
448+
liborc::RowReaderOptions opts;
449+
if (!include_names.empty()) {
450+
RETURN_NOT_OK(SelectNames(&opts, include_names));
451+
}
452+
StripeInformation stripe_info({0, 0, 0, 0});
453+
RETURN_NOT_OK(SelectStripeWithRowNumber(&opts, current_row_, &stripe_info));
454+
std::shared_ptr<Schema> schema;
455+
RETURN_NOT_OK(ReadSchema(opts, &schema));
456+
std::unique_ptr<liborc::RowReader> row_reader;
457+
458+
ORC_BEGIN_CATCH_NOT_OK
459+
row_reader = reader_->createRowReader(opts);
460+
row_reader->seekToRow(current_row_);
461+
current_row_ = stripe_info.first_row_of_stripe + stripe_info.num_rows;
462+
ORC_END_CATCH_NOT_OK
463+
464+
*out = std::shared_ptr<RecordBatchReader>(
465+
new OrcStripeReader(std::move(row_reader), schema, batch_size, pool_));
466+
return Status::OK();
467+
}
468+
441469
Status NextStripeReader(int64_t batch_size, std::shared_ptr<RecordBatchReader>* out) {
442-
return NextStripeReader(batch_size, {}, out);
470+
std::vector<int> empty_vec;
471+
return NextStripeReader(batch_size, empty_vec, out);
443472
}
444473

445474
private:
@@ -538,6 +567,12 @@ Status ORCFileReader::NextStripeReader(int64_t batch_size,
538567
return impl_->NextStripeReader(batch_size, include_indices, out);
539568
}
540569

570+
Status ORCFileReader::NextStripeReader(int64_t batch_size,
571+
const std::vector<std::string>& include_names,
572+
std::shared_ptr<RecordBatchReader>* out) {
573+
return impl_->NextStripeReader(batch_size, include_names, out);
574+
}
575+
541576
int64_t ORCFileReader::NumberOfStripes() { return impl_->NumberOfStripes(); }
542577

543578
int64_t ORCFileReader::NumberOfRows() { return impl_->NumberOfRows(); }

cpp/src/arrow/adapters/orc/adapter.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,19 @@ class ARROW_EXPORT ORCFileReader {
164164
Status NextStripeReader(int64_t batch_size, const std::vector<int>& include_indices,
165165
std::shared_ptr<RecordBatchReader>* out);
166166

167+
/// \brief Get a stripe level record batch iterator with specified row count
168+
/// in each record batch. NextStripeReader serves as a fine grain
169+
/// alternative to ReadStripe which may cause OOM issue by loading
170+
/// the whole stripes into memory.
171+
///
172+
/// \param[in] batch_size Get a stripe level record batch iterator with specified row
173+
/// count in each record batch.
174+
///
175+
/// \param[in] include_indices the selected field names to read
176+
/// \param[out] out the returned stripe reader
177+
Status NextStripeReader(int64_t batch_size, const std::vector<std::string>& include_names,
178+
std::shared_ptr<RecordBatchReader>* out);
179+
167180
/// \brief The number of stripes in the file
168181
int64_t NumberOfStripes();
169182

cpp/src/arrow/dataset/file_orc.cc

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,20 @@ class OrcScanTask : public ScanTask {
8484
included_fields.push_back(name);
8585
}
8686

87+
std::shared_ptr<RecordBatchReader> recordBatchReader;
88+
reader->NextStripeReader(scan_options.batch_size, included_fields, &recordBatchReader);
89+
8790
return RecordBatchIterator(
88-
Impl{std::move(reader), 0, num_stripes, included_fields});
91+
Impl{std::move(recordBatchReader)});
8992
}
9093

9194
Result<std::shared_ptr<RecordBatch>> Next() {
92-
if (i_ == num_stripes_) {
93-
return nullptr;
94-
}
9595
std::shared_ptr<RecordBatch> batch;
96-
// TODO (https://issues.apache.org/jira/browse/ARROW-14153)
97-
// pass scan_options_->batch_size
98-
return reader_->ReadStripe(i_++, included_fields_);
96+
RETURN_NOT_OK(recordBatchReader_->ReadNext(&batch));
97+
return batch;
9998
}
10099

101-
std::unique_ptr<arrow::adapters::orc::ORCFileReader> reader_;
102-
int i_;
103-
int num_stripes_;
104-
std::vector<std::string> included_fields_;
100+
std::shared_ptr<RecordBatchReader> recordBatchReader_;
105101
};
106102

107103
return Impl::Make(source_, *checked_pointer_cast<FileFragment>(fragment_)->format(),

0 commit comments

Comments
 (0)