pitrou commented on a change in pull request #10991:
URL: https://github.com/apache/arrow/pull/10991#discussion_r707564352



##########
File path: python/pyarrow/_dataset.pyx
##########
@@ -53,6 +53,26 @@ def _forbid_instantiation(klass, subclasses_instead=True):
     raise TypeError(msg)
 
 
+_orc_fileformat = None
+_orc_imported = False
+
+
+def _get_orc_filesystem():
+    """
+    Import OrcFileSystem on first usage (to avoid circular import issue
+    when `pyarrow._dataset_orc` would be imported first)
+    """
+    global _orc_fileformat
+    global _orc_imported
+    if not _orc_imported:

Review comment:
       `_orc_imported` is never set?

##########
File path: python/pyarrow/_dataset.pyx
##########
@@ -53,6 +53,26 @@ def _forbid_instantiation(klass, subclasses_instead=True):
     raise TypeError(msg)
 
 
+_orc_fileformat = None
+_orc_imported = False
+
+
+def _get_orc_filesystem():

Review comment:
       "file format" rather than "filesystem", no?

##########
File path: cpp/src/arrow/dataset/file_orc.cc
##########
@@ -0,0 +1,185 @@
+// 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.
+
+#include "arrow/dataset/file_orc.h"
+
+#include <memory>
+
+#include "arrow/adapters/orc/adapter.h"
+#include "arrow/dataset/dataset_internal.h"
+#include "arrow/dataset/file_base.h"
+#include "arrow/dataset/scanner.h"
+#include "arrow/util/checked_cast.h"
+#include "arrow/util/iterator.h"
+#include "arrow/util/logging.h"
+
+namespace arrow {
+
+using internal::checked_pointer_cast;
+
+namespace dataset {
+
+namespace {
+
+static inline Result<std::unique_ptr<arrow::adapters::orc::ORCFileReader>> 
OpenReader(
+    const FileSource& source,
+    const std::shared_ptr<ScanOptions>& scan_options = nullptr) {
+  ARROW_ASSIGN_OR_RAISE(auto input, source.Open());
+
+  arrow::MemoryPool* pool;
+  if (scan_options) {
+    pool = scan_options->pool;
+  } else {
+    pool = default_memory_pool();
+  }
+
+  std::unique_ptr<arrow::adapters::orc::ORCFileReader> reader;
+  auto status =
+      arrow::adapters::orc::ORCFileReader::Open(std::move(input), pool, 
&reader);
+  if (!status.ok()) {
+    return status.WithMessage("Could not open ORC input source '", 
source.path(),
+                              "': ", status.message());
+  }
+  return reader;
+}
+
+/// \brief A ScanTask backed by an ORC file.
+class OrcScanTask : public ScanTask {
+ public:
+  OrcScanTask(std::shared_ptr<FileFragment> fragment,
+              std::shared_ptr<ScanOptions> options)
+      : ScanTask(std::move(options), fragment), source_(fragment->source()) {}
+
+  Result<RecordBatchIterator> Execute() override {
+    struct Impl {
+      static Result<RecordBatchIterator> Make(const FileSource& source,
+                                              const FileFormat& format,
+                                              const ScanOptions& scan_options) 
{
+        ARROW_ASSIGN_OR_RAISE(
+            auto reader, OpenReader(source, 
std::make_shared<ScanOptions>(scan_options)));
+        int num_stripes = reader->NumberOfStripes();
+        return RecordBatchIterator(Impl{std::move(reader), 0, num_stripes});
+      }
+
+      Result<std::shared_ptr<RecordBatch>> Next() {
+        if (i_ == num_stripes_) {
+          return nullptr;
+        }
+        std::shared_ptr<RecordBatch> batch;
+        // TODO (https://issues.apache.org/jira/browse/ARROW-13797)
+        // determine included fields from options_->MaterializedFields() to
+        // optimize the column selection (see _column_index_lookup in python
+        // orc.py for custom logic)
+        // std::vector<int> included_fields;
+        // TODO pass scan_options_->batch_size
+        reader_->ReadStripe(i_++, &batch);
+        return batch;
+      }
+
+      std::unique_ptr<arrow::adapters::orc::ORCFileReader> reader_;
+      int i_;
+      int num_stripes_;
+    };
+
+    return Impl::Make(source_, 
*checked_pointer_cast<FileFragment>(fragment_)->format(),
+                      *options_);
+  }
+
+ private:
+  FileSource source_;
+};
+
+class OrcScanTaskIterator {
+ public:
+  static Result<ScanTaskIterator> Make(std::shared_ptr<ScanOptions> options,
+                                       std::shared_ptr<FileFragment> fragment) 
{
+    return ScanTaskIterator(OrcScanTaskIterator(std::move(options), 
std::move(fragment)));
+  }
+
+  Result<std::shared_ptr<ScanTask>> Next() {
+    if (once_) {
+      // Iteration is done.
+      return nullptr;
+    }
+
+    once_ = true;
+    return std::shared_ptr<ScanTask>(new OrcScanTask(fragment_, options_));
+  }
+
+ private:
+  OrcScanTaskIterator(std::shared_ptr<ScanOptions> options,
+                      std::shared_ptr<FileFragment> fragment)
+      : options_(std::move(options)), fragment_(std::move(fragment)) {}
+
+  bool once_ = false;
+  std::shared_ptr<ScanOptions> options_;
+  std::shared_ptr<FileFragment> fragment_;
+};
+
+}  // namespace
+
+Result<bool> OrcFileFormat::IsSupported(const FileSource& source) const {
+  RETURN_NOT_OK(source.Open().status());
+  return OpenReader(source).ok();
+}
+
+Result<std::shared_ptr<Schema>> OrcFileFormat::Inspect(const FileSource& 
source) const {
+  ARROW_ASSIGN_OR_RAISE(auto reader, OpenReader(source));
+  std::shared_ptr<Schema> schema;
+  RETURN_NOT_OK(reader->ReadSchema(&schema));
+  return schema;
+}
+
+Result<ScanTaskIterator> OrcFileFormat::ScanFile(
+    const std::shared_ptr<ScanOptions>& options,
+    const std::shared_ptr<FileFragment>& fragment) const {
+  return OrcScanTaskIterator::Make(options, fragment);
+}
+
+Future<util::optional<int64_t>> OrcFileFormat::CountRows(
+    const std::shared_ptr<FileFragment>& file, compute::Expression predicate,
+    const std::shared_ptr<ScanOptions>& options) {
+  if (ExpressionHasFieldRefs(predicate)) {
+    return Future<util::optional<int64_t>>::MakeFinished(util::nullopt);
+  }
+  auto self = checked_pointer_cast<OrcFileFormat>(shared_from_this());
+  return DeferNotOk(options->io_context.executor()->Submit(
+      [self, file]() -> Result<util::optional<int64_t>> {
+        ARROW_ASSIGN_OR_RAISE(auto reader, OpenReader(file->source()));
+        return reader->NumberOfRows();
+      }));
+}
+
+// //
+// // IpcFileWriter, IpcFileWriteOptions
+// //

Review comment:
       Ping on this :-)




-- 
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: github-unsubscr...@arrow.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to