|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +#include "arrow/dataset/file_orc.h" |
| 19 | + |
| 20 | +#include <memory> |
| 21 | + |
| 22 | +#include "arrow/adapters/orc/adapter.h" |
| 23 | +#include "arrow/dataset/dataset_internal.h" |
| 24 | +#include "arrow/dataset/file_base.h" |
| 25 | +#include "arrow/dataset/scanner.h" |
| 26 | +#include "arrow/util/checked_cast.h" |
| 27 | +#include "arrow/util/iterator.h" |
| 28 | +#include "arrow/util/logging.h" |
| 29 | + |
| 30 | +namespace arrow { |
| 31 | + |
| 32 | +using internal::checked_pointer_cast; |
| 33 | + |
| 34 | +namespace dataset { |
| 35 | + |
| 36 | +namespace { |
| 37 | + |
| 38 | +inline Result<std::unique_ptr<arrow::adapters::orc::ORCFileReader>> OpenReader( |
| 39 | + const FileSource& source, |
| 40 | + const std::shared_ptr<ScanOptions>& scan_options = nullptr) { |
| 41 | + ARROW_ASSIGN_OR_RAISE(auto input, source.Open()); |
| 42 | + |
| 43 | + arrow::MemoryPool* pool; |
| 44 | + if (scan_options) { |
| 45 | + pool = scan_options->pool; |
| 46 | + } else { |
| 47 | + pool = default_memory_pool(); |
| 48 | + } |
| 49 | + |
| 50 | + auto reader = arrow::adapters::orc::ORCFileReader::Open(std::move(input), pool); |
| 51 | + auto status = reader.status(); |
| 52 | + if (!status.ok()) { |
| 53 | + return status.WithMessage("Could not open ORC input source '", source.path(), |
| 54 | + "': ", status.message()); |
| 55 | + } |
| 56 | + return reader; |
| 57 | +} |
| 58 | + |
| 59 | +/// \brief A ScanTask backed by an ORC file. |
| 60 | +class OrcScanTask : public ScanTask { |
| 61 | + public: |
| 62 | + OrcScanTask(std::shared_ptr<FileFragment> fragment, |
| 63 | + std::shared_ptr<ScanOptions> options) |
| 64 | + : ScanTask(std::move(options), fragment), source_(fragment->source()) {} |
| 65 | + |
| 66 | + Result<RecordBatchIterator> Execute() override { |
| 67 | + struct Impl { |
| 68 | + static Result<RecordBatchIterator> Make(const FileSource& source, |
| 69 | + const FileFormat& format, |
| 70 | + const ScanOptions& scan_options) { |
| 71 | + ARROW_ASSIGN_OR_RAISE( |
| 72 | + auto reader, OpenReader(source, std::make_shared<ScanOptions>(scan_options))); |
| 73 | + int num_stripes = reader->NumberOfStripes(); |
| 74 | + return RecordBatchIterator(Impl{std::move(reader), 0, num_stripes}); |
| 75 | + } |
| 76 | + |
| 77 | + Result<std::shared_ptr<RecordBatch>> Next() { |
| 78 | + if (i_ == num_stripes_) { |
| 79 | + return nullptr; |
| 80 | + } |
| 81 | + std::shared_ptr<RecordBatch> batch; |
| 82 | + // TODO (https://issues.apache.org/jira/browse/ARROW-13797) |
| 83 | + // determine included fields from options_->MaterializedFields() to |
| 84 | + // optimize the column selection (see _column_index_lookup in python |
| 85 | + // orc.py for custom logic) |
| 86 | + // std::vector<int> included_fields; |
| 87 | + // TODO (https://issues.apache.org/jira/browse/ARROW-14153) |
| 88 | + // pass scan_options_->batch_size |
| 89 | + return reader_->ReadStripe(i_++); |
| 90 | + } |
| 91 | + |
| 92 | + std::unique_ptr<arrow::adapters::orc::ORCFileReader> reader_; |
| 93 | + int i_; |
| 94 | + int num_stripes_; |
| 95 | + }; |
| 96 | + |
| 97 | + return Impl::Make(source_, *checked_pointer_cast<FileFragment>(fragment_)->format(), |
| 98 | + *options_); |
| 99 | + } |
| 100 | + |
| 101 | + private: |
| 102 | + FileSource source_; |
| 103 | +}; |
| 104 | + |
| 105 | +class OrcScanTaskIterator { |
| 106 | + public: |
| 107 | + static Result<ScanTaskIterator> Make(std::shared_ptr<ScanOptions> options, |
| 108 | + std::shared_ptr<FileFragment> fragment) { |
| 109 | + return ScanTaskIterator(OrcScanTaskIterator(std::move(options), std::move(fragment))); |
| 110 | + } |
| 111 | + |
| 112 | + Result<std::shared_ptr<ScanTask>> Next() { |
| 113 | + if (once_) { |
| 114 | + // Iteration is done. |
| 115 | + return nullptr; |
| 116 | + } |
| 117 | + |
| 118 | + once_ = true; |
| 119 | + return std::shared_ptr<ScanTask>(new OrcScanTask(fragment_, options_)); |
| 120 | + } |
| 121 | + |
| 122 | + private: |
| 123 | + OrcScanTaskIterator(std::shared_ptr<ScanOptions> options, |
| 124 | + std::shared_ptr<FileFragment> fragment) |
| 125 | + : options_(std::move(options)), fragment_(std::move(fragment)) {} |
| 126 | + |
| 127 | + bool once_ = false; |
| 128 | + std::shared_ptr<ScanOptions> options_; |
| 129 | + std::shared_ptr<FileFragment> fragment_; |
| 130 | +}; |
| 131 | + |
| 132 | +} // namespace |
| 133 | + |
| 134 | +Result<bool> OrcFileFormat::IsSupported(const FileSource& source) const { |
| 135 | + RETURN_NOT_OK(source.Open().status()); |
| 136 | + return OpenReader(source).ok(); |
| 137 | +} |
| 138 | + |
| 139 | +Result<std::shared_ptr<Schema>> OrcFileFormat::Inspect(const FileSource& source) const { |
| 140 | + ARROW_ASSIGN_OR_RAISE(auto reader, OpenReader(source)); |
| 141 | + return reader->ReadSchema(); |
| 142 | +} |
| 143 | + |
| 144 | +Result<ScanTaskIterator> OrcFileFormat::ScanFile( |
| 145 | + const std::shared_ptr<ScanOptions>& options, |
| 146 | + const std::shared_ptr<FileFragment>& fragment) const { |
| 147 | + return OrcScanTaskIterator::Make(options, fragment); |
| 148 | +} |
| 149 | + |
| 150 | +Future<util::optional<int64_t>> OrcFileFormat::CountRows( |
| 151 | + const std::shared_ptr<FileFragment>& file, compute::Expression predicate, |
| 152 | + const std::shared_ptr<ScanOptions>& options) { |
| 153 | + if (ExpressionHasFieldRefs(predicate)) { |
| 154 | + return Future<util::optional<int64_t>>::MakeFinished(util::nullopt); |
| 155 | + } |
| 156 | + auto self = checked_pointer_cast<OrcFileFormat>(shared_from_this()); |
| 157 | + return DeferNotOk(options->io_context.executor()->Submit( |
| 158 | + [self, file]() -> Result<util::optional<int64_t>> { |
| 159 | + ARROW_ASSIGN_OR_RAISE(auto reader, OpenReader(file->source())); |
| 160 | + return reader->NumberOfRows(); |
| 161 | + })); |
| 162 | +} |
| 163 | + |
| 164 | +// // |
| 165 | +// // OrcFileWriter, OrcFileWriteOptions |
| 166 | +// // |
| 167 | + |
| 168 | +std::shared_ptr<FileWriteOptions> OrcFileFormat::DefaultWriteOptions() { |
| 169 | + // TODO (https://issues.apache.org/jira/browse/ARROW-13796) |
| 170 | + return nullptr; |
| 171 | +} |
| 172 | + |
| 173 | +Result<std::shared_ptr<FileWriter>> OrcFileFormat::MakeWriter( |
| 174 | + std::shared_ptr<io::OutputStream> destination, std::shared_ptr<Schema> schema, |
| 175 | + std::shared_ptr<FileWriteOptions> options, |
| 176 | + fs::FileLocator destination_locator) const { |
| 177 | + // TODO (https://issues.apache.org/jira/browse/ARROW-13796) |
| 178 | + return Status::NotImplemented("ORC writer not yet implemented."); |
| 179 | +} |
| 180 | + |
| 181 | +} // namespace dataset |
| 182 | +} // namespace arrow |
0 commit comments