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



##########
File path: python/pyarrow/tests/test_dataset.py
##########
@@ -2614,6 +2615,49 @@ def test_ipc_format(tempdir, dataset_reader):
         assert result.equals(table)
 
 
[email protected]
+def test_orc_format(tempdir, dataset_reader):
+    from pyarrow import orc
+    table = pa.table({'a': pa.array([1, 2, 3], type="int8"),
+                      'b': pa.array([.1, .2, .3], type="float64")})
+
+    path = str(tempdir / 'test.orc')
+    orc.write_table(table, path)
+
+    dataset = ds.dataset(path, format=ds.OrcFileFormat())
+    result = dataset_reader.to_table(dataset)

Review comment:
       Can you add `result.validate(full=True)` here and in other similar 
places?

##########
File path: python/pyarrow/tests/test_dataset.py
##########
@@ -2614,6 +2615,49 @@ def test_ipc_format(tempdir, dataset_reader):
         assert result.equals(table)
 
 
[email protected]
+def test_orc_format(tempdir, dataset_reader):
+    from pyarrow import orc
+    table = pa.table({'a': pa.array([1, 2, 3], type="int8"),
+                      'b': pa.array([.1, .2, .3], type="float64")})
+
+    path = str(tempdir / 'test.orc')
+    orc.write_table(table, path)
+
+    dataset = ds.dataset(path, format=ds.OrcFileFormat())
+    result = dataset_reader.to_table(dataset)
+    assert result.equals(table)
+
+    dataset = ds.dataset(path, format="orc")
+    result = dataset_reader.to_table(dataset)
+    assert result.equals(table)
+
+    result = dataset_reader.to_table(dataset, columns=["b"])
+    assert result.equals(table.select(["b"]))
+
+    assert dataset_reader.count_rows(dataset) == 3
+    assert dataset_reader.count_rows(dataset, filter=ds.field("a") > 2) == 1
+
+
[email protected]
+def test_orc_scan_options(tempdir, dataset_reader):
+    from pyarrow import orc
+    table = pa.table({'a': pa.array([1, 2, 3], type="int8"),
+                      'b': pa.array([.1, .2, .3], type="float64")})
+
+    path = str(tempdir / 'test.orc')
+    orc.write_table(table, path)
+
+    dataset = ds.dataset(path, format="orc")
+    result = list(dataset_reader.to_batches(dataset))
+    assert len(result) == 1
+    assert result[0].num_rows == 3
+    result = list(dataset_reader.to_batches(dataset, batch_size=2))
+    assert len(result) == 2
+    assert result[0].num_rows == 2
+    assert result[1].num_rows == 1

Review comment:
       Can you also test the actual result values? 

##########
File path: cpp/src/arrow/dataset/file_orc.h
##########
@@ -0,0 +1,79 @@
+// 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.
+
+// This API is EXPERIMENTAL.
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+#include "arrow/adapters/orc/adapter.h"

Review comment:
       I'm not sure this include is actually necessary here?

##########
File path: cpp/src/arrow/dataset/file_orc.cc
##########
@@ -0,0 +1,132 @@
+// 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/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 {
+
+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 {
+    ARROW_ASSIGN_OR_RAISE(auto reader, OpenReader(source_));
+    std::shared_ptr<arrow::RecordBatchReader> batch_reader;
+    // TODO 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;
+    RETURN_NOT_OK(reader->NextStripeReader(options_->batch_size, 
&batch_reader));
+
+    auto batch_it = MakeIteratorFromReader(batch_reader);
+    return batch_it;
+  }
+
+ private:
+  FileSource source_;
+};
+
+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));

Review comment:
       For the record, see also 
https://issues.apache.org/jira/browse/ARROW-13793

##########
File path: cpp/src/arrow/dataset/file_orc.cc
##########
@@ -0,0 +1,132 @@
+// 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/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 {
+
+static inline Result<std::unique_ptr<arrow::adapters::orc::ORCFileReader>> 
OpenReader(

Review comment:
       Can you put all private functions and classes inside the anonymous 
namespace? (that is, `namespace {`)
   
   The anonymous namespace is equivalent to `static` for functions, but also 
applies to classes, variables, etc.

##########
File path: cpp/src/arrow/dataset/file_orc.cc
##########
@@ -0,0 +1,132 @@
+// 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/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 {
+
+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 {
+    ARROW_ASSIGN_OR_RAISE(auto reader, OpenReader(source_));
+    std::shared_ptr<arrow::RecordBatchReader> batch_reader;
+    // TODO determine included fields from options_->MaterializedFields() to

Review comment:
       Can you perhaps open a JIRA for this TODO?

##########
File path: cpp/src/arrow/dataset/file_orc.cc
##########
@@ -0,0 +1,132 @@
+// 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/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 {
+
+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 {
+    ARROW_ASSIGN_OR_RAISE(auto reader, OpenReader(source_));
+    std::shared_ptr<arrow::RecordBatchReader> batch_reader;
+    // TODO 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;
+    RETURN_NOT_OK(reader->NextStripeReader(options_->batch_size, 
&batch_reader));
+
+    auto batch_it = MakeIteratorFromReader(batch_reader);
+    return batch_it;
+  }
+
+ private:
+  FileSource source_;
+};
+
+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 {
+  auto task = std::make_shared<OrcScanTask>(fragment, options);
+
+  return MakeVectorIterator<std::shared_ptr<ScanTask>>({std::move(task)});
+}
+
+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 = 
internal::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
+// //
+
+std::shared_ptr<FileWriteOptions> OrcFileFormat::DefaultWriteOptions() {
+  // TODO
+  return NULLPTR;

Review comment:
       Can use `nullptr` in `.cc` files.

##########
File path: cpp/src/arrow/dataset/file_orc.cc
##########
@@ -0,0 +1,132 @@
+// 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/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 {
+
+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 {
+    ARROW_ASSIGN_OR_RAISE(auto reader, OpenReader(source_));
+    std::shared_ptr<arrow::RecordBatchReader> batch_reader;
+    // TODO 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;
+    RETURN_NOT_OK(reader->NextStripeReader(options_->batch_size, 
&batch_reader));

Review comment:
       Hmm, IIUC, this will only read one stripe from the ORC file, but an ORC 
file may well have several stripes. @iajoiner is my interpretation correct?




-- 
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]


Reply via email to