You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/04/08 05:53:32 UTC

[GitHub] [arrow] westonpace commented on a diff in pull request #12672: ARROW-15779: [Python] Create python bindings for Substrait consumer

westonpace commented on code in PR #12672:
URL: https://github.com/apache/arrow/pull/12672#discussion_r845748534


##########
cpp/src/arrow/engine/substrait/serde_test.cc:
##########
@@ -724,5 +728,99 @@ TEST(Substrait, ExtensionSetFromPlan) {
   EXPECT_EQ(decoded_add_func.name, "add");
 }
 
+TEST(Substrait, GetRecordBatchReader) {
+  const auto parquet_root = std::getenv("PARQUET_TEST_DATA");

Review Comment:
   There is `GetEnvVar` in `io_util.cc` that returns `Result<std::string>` which you can `ASSERT_OK_AND_ASSIGN`.  That might lead to slightly clearer errors in case the test data environment variable is not set.  As it stands `getenv` will return `nullptr` and then constructing a string with it will trigger `std::exception`.



##########
cpp/src/arrow/engine/substrait/serde_test.cc:
##########
@@ -724,5 +728,99 @@ TEST(Substrait, ExtensionSetFromPlan) {
   EXPECT_EQ(decoded_add_func.name, "add");
 }
 
+TEST(Substrait, GetRecordBatchReader) {
+  const auto parquet_root = std::getenv("PARQUET_TEST_DATA");
+  std::string dir_string(parquet_root);
+  std::stringstream ss;
+  ss << dir_string << "/binary.parquet";
+  auto file_path = ss.str();

Review Comment:
   `/` isn't a valid directory separator on Windows.  Maybe use something like (not 100% sure this is correct) `PlatformFilename::FromString(dir_string).Join("binary.parquet")`



##########
cpp/src/arrow/engine/substrait/serde_test.cc:
##########
@@ -724,5 +728,99 @@ TEST(Substrait, ExtensionSetFromPlan) {
   EXPECT_EQ(decoded_add_func.name, "add");
 }
 
+TEST(Substrait, GetRecordBatchReader) {
+  const auto parquet_root = std::getenv("PARQUET_TEST_DATA");
+  std::string dir_string(parquet_root);
+  std::stringstream ss;
+  ss << dir_string << "/binary.parquet";
+  auto file_path = ss.str();
+
+  std::string substrait_json = R"({
+    "relations": [
+      {"rel": {
+        "read": {
+          "base_schema": {
+            "struct": {
+              "types": [ 
+                         {"binary": {}}
+                       ]
+            },
+            "names": [
+                      "foo"
+                      ]
+          },
+          "local_files": {
+            "items": [
+              {
+                "uri_file": "file://FILENAME_PLACEHOLDER",
+                "format": "FILE_FORMAT_PARQUET"
+              }
+            ]
+          }
+        }
+      }}
+    ]
+  })";
+
+  std::string filename_placeholder = "FILENAME_PLACEHOLDER";
+  substrait_json.replace(substrait_json.find(filename_placeholder),
+                         filename_placeholder.size(), file_path);
+  AsyncGenerator<util::optional<cp::ExecBatch>> sink_gen;
+  cp::ExecContext exec_context(default_memory_pool(),
+                               arrow::internal::GetCpuThreadPool());
+  ASSERT_OK_AND_ASSIGN(auto plan, cp::ExecPlan::Make());
+  engine::SubstraitExecutor executor(substrait_json, &sink_gen, plan,
+                                     exec_context);
+  ASSERT_OK_AND_ASSIGN(auto reader, executor.Execute());
+  ASSERT_OK(executor.Close());
+  
+  ASSERT_OK_AND_ASSIGN(auto table, Table::FromRecordBatchReader(reader.get()));
+  EXPECT_GT(table->num_rows(), 0);
+}
+
+TEST(Substrait, GetRecordBatchReaderUtil) {
+  const auto parquet_root = std::getenv("PARQUET_TEST_DATA");
+  std::string dir_string(parquet_root);
+  std::stringstream ss;
+  ss << dir_string << "/binary.parquet";
+  auto file_path = ss.str();

Review Comment:
   Same comments here as above.  Also,  you could maybe move this logic into a helper function.  Especially if we can avoid repeating the long substrait_json string.



##########
cpp/src/arrow/engine/substrait/util.h:
##########
@@ -0,0 +1,89 @@
+// 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 <memory>
+#include <string>
+#include <vector>
+#include "arrow/compute/type_fwd.h"
+#include "arrow/engine/api.h"
+#include "arrow/util/async_generator.h"
+#include "arrow/util/iterator.h"
+#include "arrow/util/optional.h"
+
+namespace arrow {
+
+namespace cp = arrow::compute;
+
+namespace engine {
+
+/// \brief A SinkNodeConsumer specialized to output ExecBatches via PushGenerator
+class ARROW_ENGINE_EXPORT SubstraitSinkConsumer : public cp::SinkNodeConsumer {
+ public:
+  explicit SubstraitSinkConsumer(
+      AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* generator,
+      arrow::util::BackpressureOptions backpressure = {})
+      : producer_(MakeProducer(generator, std::move(backpressure))) {}
+
+  Status Consume(cp::ExecBatch batch) override;
+
+  static arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer
+  MakeProducer(AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* out_gen,
+               arrow::util::BackpressureOptions backpressure);
+
+  Future<> Finish() override;
+
+ private:
+  PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer producer_;
+};
+
+/// \brief An executor to run a Substrait Query
+/// This interface is provided as a utility when creating language
+/// bindings for consuming a Substrait plan.
+class ARROW_ENGINE_EXPORT SubstraitExecutor {
+ public:
+  explicit SubstraitExecutor(
+      std::string substrait_json,
+      AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* generator,
+      std::shared_ptr<cp::ExecPlan> plan,

Review Comment:
   Why do I need to provide `generator` and `plan`?  I would think that this class should take care of those things for me.



##########
cpp/src/arrow/engine/substrait/util.cc:
##########
@@ -0,0 +1,100 @@
+// 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/engine/substrait/util.h"
+
+namespace arrow {
+
+namespace engine {
+
+Status SubstraitSinkConsumer::Consume(cp::ExecBatch batch) {
+  // Consume a batch of data
+  bool did_push = producer_.Push(batch);
+  if (!did_push) return Status::ExecutionError("Producer closed already");
+  return Status::OK();
+}
+
+Future<> SubstraitSinkConsumer::Finish() {
+  producer_.Push(IterationEnd<arrow::util::optional<cp::ExecBatch>>());
+  if (producer_.Close()) {
+    return Future<>::MakeFinished();
+  }
+  return Future<>::MakeFinished(
+      Status::ExecutionError("Error occurred in closing the batch producer"));
+}
+
+Status SubstraitExecutor::MakePlan() {
+  ARROW_ASSIGN_OR_RAISE(auto serialized_plan,
+                        engine::internal::SubstraitFromJSON("Plan", substrait_json_));
+  RETURN_NOT_OK(engine::internal::SubstraitToJSON("Plan", *serialized_plan));
+  std::vector<std::shared_ptr<cp::SinkNodeConsumer>> consumers;
+  std::function<std::shared_ptr<cp::SinkNodeConsumer>()> consumer_factory = [&] {
+    consumers.emplace_back(new SubstraitSinkConsumer{generator_});
+    return consumers.back();
+  };

Review Comment:
   ```suggestion
     std::function<std::shared_ptr<cp::SinkNodeConsumer>()> consumer_factory = [&] {
       return std::make_shared<SubstraitSinkConsumer>(generator_);
     };
   ```
   The created `declarations_` will manage ownerhsip of the consumer (I'm pretty sure).  Either way, this vector isn't keeping the consumers alive for very long and I don't think it is needed.



##########
cpp/src/arrow/engine/substrait/util.cc:
##########
@@ -0,0 +1,100 @@
+// 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/engine/substrait/util.h"
+
+namespace arrow {
+
+namespace engine {
+
+Status SubstraitSinkConsumer::Consume(cp::ExecBatch batch) {
+  // Consume a batch of data
+  bool did_push = producer_.Push(batch);
+  if (!did_push) return Status::ExecutionError("Producer closed already");
+  return Status::OK();
+}
+
+Future<> SubstraitSinkConsumer::Finish() {
+  producer_.Push(IterationEnd<arrow::util::optional<cp::ExecBatch>>());
+  if (producer_.Close()) {
+    return Future<>::MakeFinished();
+  }
+  return Future<>::MakeFinished(
+      Status::ExecutionError("Error occurred in closing the batch producer"));
+}
+
+Status SubstraitExecutor::MakePlan() {
+  ARROW_ASSIGN_OR_RAISE(auto serialized_plan,
+                        engine::internal::SubstraitFromJSON("Plan", substrait_json_));
+  RETURN_NOT_OK(engine::internal::SubstraitToJSON("Plan", *serialized_plan));
+  std::vector<std::shared_ptr<cp::SinkNodeConsumer>> consumers;
+  std::function<std::shared_ptr<cp::SinkNodeConsumer>()> consumer_factory = [&] {
+    consumers.emplace_back(new SubstraitSinkConsumer{generator_});
+    return consumers.back();
+  };
+  ARROW_ASSIGN_OR_RAISE(declarations_,
+                        engine::DeserializePlan(*serialized_plan, consumer_factory));
+  return Status::OK();
+}
+
+arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer
+SubstraitSinkConsumer::MakeProducer(
+    AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* out_gen,
+    arrow::util::BackpressureOptions backpressure) {
+  arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>> push_gen(
+      std::move(backpressure));
+  auto out = push_gen.producer();
+  *out_gen = std::move(push_gen);
+  return out;
+}
+
+Result<std::shared_ptr<RecordBatchReader>> SubstraitExecutor::Execute() {
+  RETURN_NOT_OK(SubstraitExecutor::MakePlan());
+  for (const cp::Declaration& decl : declarations_) {
+    RETURN_NOT_OK(decl.AddToPlan(plan_.get()).status());
+  }
+  ARROW_RETURN_NOT_OK(plan_->Validate());
+  ARROW_RETURN_NOT_OK(plan_->StartProducing());
+  auto schema = plan_->sinks()[0]->output_schema();
+  std::shared_ptr<RecordBatchReader> sink_reader = cp::MakeGeneratorReader(
+      schema, std::move(*generator_), exec_context_.memory_pool());
+  return sink_reader;
+}
+
+Status SubstraitExecutor::Close() {
+  ARROW_RETURN_NOT_OK(plan_->finished().status());
+  return Status::OK();
+}

Review Comment:
   ```suggestion
   Status SubstraitExecutor::Close() {
     return plan_->finished().status();
   }
   ```



##########
cpp/src/arrow/engine/substrait/util.cc:
##########
@@ -0,0 +1,100 @@
+// 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/engine/substrait/util.h"
+
+namespace arrow {
+
+namespace engine {
+
+Status SubstraitSinkConsumer::Consume(cp::ExecBatch batch) {
+  // Consume a batch of data
+  bool did_push = producer_.Push(batch);
+  if (!did_push) return Status::ExecutionError("Producer closed already");
+  return Status::OK();
+}
+
+Future<> SubstraitSinkConsumer::Finish() {
+  producer_.Push(IterationEnd<arrow::util::optional<cp::ExecBatch>>());
+  if (producer_.Close()) {
+    return Future<>::MakeFinished();
+  }
+  return Future<>::MakeFinished(
+      Status::ExecutionError("Error occurred in closing the batch producer"));
+}
+
+Status SubstraitExecutor::MakePlan() {
+  ARROW_ASSIGN_OR_RAISE(auto serialized_plan,
+                        engine::internal::SubstraitFromJSON("Plan", substrait_json_));
+  RETURN_NOT_OK(engine::internal::SubstraitToJSON("Plan", *serialized_plan));
+  std::vector<std::shared_ptr<cp::SinkNodeConsumer>> consumers;
+  std::function<std::shared_ptr<cp::SinkNodeConsumer>()> consumer_factory = [&] {
+    consumers.emplace_back(new SubstraitSinkConsumer{generator_});
+    return consumers.back();
+  };
+  ARROW_ASSIGN_OR_RAISE(declarations_,
+                        engine::DeserializePlan(*serialized_plan, consumer_factory));
+  return Status::OK();
+}
+
+arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer
+SubstraitSinkConsumer::MakeProducer(
+    AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* out_gen,
+    arrow::util::BackpressureOptions backpressure) {
+  arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>> push_gen(
+      std::move(backpressure));
+  auto out = push_gen.producer();
+  *out_gen = std::move(push_gen);
+  return out;
+}
+
+Result<std::shared_ptr<RecordBatchReader>> SubstraitExecutor::Execute() {
+  RETURN_NOT_OK(SubstraitExecutor::MakePlan());
+  for (const cp::Declaration& decl : declarations_) {
+    RETURN_NOT_OK(decl.AddToPlan(plan_.get()).status());
+  }
+  ARROW_RETURN_NOT_OK(plan_->Validate());
+  ARROW_RETURN_NOT_OK(plan_->StartProducing());
+  auto schema = plan_->sinks()[0]->output_schema();
+  std::shared_ptr<RecordBatchReader> sink_reader = cp::MakeGeneratorReader(
+      schema, std::move(*generator_), exec_context_.memory_pool());
+  return sink_reader;
+}
+
+Status SubstraitExecutor::Close() {
+  ARROW_RETURN_NOT_OK(plan_->finished().status());
+  return Status::OK();
+}
+
+Result<std::shared_ptr<RecordBatchReader>> SubstraitExecutor::GetRecordBatchReader(
+    std::string& substrait_json) {
+  cp::ExecContext exec_context(arrow::default_memory_pool(),
+                               ::arrow::internal::GetCpuThreadPool());
+
+  arrow::AsyncGenerator<arrow::util::optional<cp::ExecBatch>> sink_gen;
+  ARROW_ASSIGN_OR_RAISE(auto plan, cp::ExecPlan::Make());
+  arrow::engine::SubstraitExecutor executor(substrait_json, &sink_gen, plan,
+                                            exec_context);
+  RETURN_NOT_OK(executor.MakePlan());
+  ARROW_ASSIGN_OR_RAISE(auto sink_reader, executor.Execute());

Review Comment:
   Minor nit: It might be slightly simpler to have `executor.Execute()` make the call to `MakePlan` and `Close`.  That way `SubstraitExecutor` only has one public method there's less coupling between this static method and the class.
   
   On the other hand, if you want to keep it this way, I would change the method `MakePlan` to be named `Init`.



##########
cpp/src/arrow/engine/substrait/util.cc:
##########
@@ -0,0 +1,100 @@
+// 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/engine/substrait/util.h"
+
+namespace arrow {
+
+namespace engine {
+
+Status SubstraitSinkConsumer::Consume(cp::ExecBatch batch) {
+  // Consume a batch of data
+  bool did_push = producer_.Push(batch);
+  if (!did_push) return Status::ExecutionError("Producer closed already");
+  return Status::OK();
+}
+
+Future<> SubstraitSinkConsumer::Finish() {
+  producer_.Push(IterationEnd<arrow::util::optional<cp::ExecBatch>>());
+  if (producer_.Close()) {
+    return Future<>::MakeFinished();
+  }
+  return Future<>::MakeFinished(
+      Status::ExecutionError("Error occurred in closing the batch producer"));
+}
+
+Status SubstraitExecutor::MakePlan() {
+  ARROW_ASSIGN_OR_RAISE(auto serialized_plan,
+                        engine::internal::SubstraitFromJSON("Plan", substrait_json_));
+  RETURN_NOT_OK(engine::internal::SubstraitToJSON("Plan", *serialized_plan));
+  std::vector<std::shared_ptr<cp::SinkNodeConsumer>> consumers;
+  std::function<std::shared_ptr<cp::SinkNodeConsumer>()> consumer_factory = [&] {
+    consumers.emplace_back(new SubstraitSinkConsumer{generator_});
+    return consumers.back();
+  };
+  ARROW_ASSIGN_OR_RAISE(declarations_,
+                        engine::DeserializePlan(*serialized_plan, consumer_factory));
+  return Status::OK();
+}
+
+arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer
+SubstraitSinkConsumer::MakeProducer(
+    AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* out_gen,
+    arrow::util::BackpressureOptions backpressure) {
+  arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>> push_gen(
+      std::move(backpressure));
+  auto out = push_gen.producer();
+  *out_gen = std::move(push_gen);
+  return out;
+}
+
+Result<std::shared_ptr<RecordBatchReader>> SubstraitExecutor::Execute() {
+  RETURN_NOT_OK(SubstraitExecutor::MakePlan());
+  for (const cp::Declaration& decl : declarations_) {
+    RETURN_NOT_OK(decl.AddToPlan(plan_.get()).status());
+  }
+  ARROW_RETURN_NOT_OK(plan_->Validate());
+  ARROW_RETURN_NOT_OK(plan_->StartProducing());
+  auto schema = plan_->sinks()[0]->output_schema();
+  std::shared_ptr<RecordBatchReader> sink_reader = cp::MakeGeneratorReader(
+      schema, std::move(*generator_), exec_context_.memory_pool());

Review Comment:
   We shouldn't have to grab the `output_schema` from the sink.  



##########
cpp/src/arrow/engine/substrait/util.h:
##########
@@ -0,0 +1,89 @@
+// 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 <memory>
+#include <string>
+#include <vector>
+#include "arrow/compute/type_fwd.h"
+#include "arrow/engine/api.h"
+#include "arrow/util/async_generator.h"
+#include "arrow/util/iterator.h"
+#include "arrow/util/optional.h"
+
+namespace arrow {
+
+namespace cp = arrow::compute;
+
+namespace engine {
+
+/// \brief A SinkNodeConsumer specialized to output ExecBatches via PushGenerator
+class ARROW_ENGINE_EXPORT SubstraitSinkConsumer : public cp::SinkNodeConsumer {
+ public:
+  explicit SubstraitSinkConsumer(
+      AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* generator,
+      arrow::util::BackpressureOptions backpressure = {})
+      : producer_(MakeProducer(generator, std::move(backpressure))) {}
+
+  Status Consume(cp::ExecBatch batch) override;
+
+  static arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer
+  MakeProducer(AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* out_gen,
+               arrow::util::BackpressureOptions backpressure);
+
+  Future<> Finish() override;
+
+ private:
+  PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer producer_;
+};
+
+/// \brief An executor to run a Substrait Query
+/// This interface is provided as a utility when creating language
+/// bindings for consuming a Substrait plan.
+class ARROW_ENGINE_EXPORT SubstraitExecutor {

Review Comment:
   Can we move this class into the `.cc` file?  It should be an implementation detail.  Then take the static method `GetRecordBatchReader` and make it a file-scoped function (i.e. global static function).  This way the user never has to know about the `SubstraitExecutor`.



##########
cpp/src/arrow/engine/substrait/util.h:
##########
@@ -0,0 +1,89 @@
+// 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 <memory>
+#include <string>
+#include <vector>
+#include "arrow/compute/type_fwd.h"
+#include "arrow/engine/api.h"
+#include "arrow/util/async_generator.h"
+#include "arrow/util/iterator.h"
+#include "arrow/util/optional.h"
+
+namespace arrow {
+
+namespace cp = arrow::compute;
+
+namespace engine {
+
+/// \brief A SinkNodeConsumer specialized to output ExecBatches via PushGenerator
+class ARROW_ENGINE_EXPORT SubstraitSinkConsumer : public cp::SinkNodeConsumer {
+ public:
+  explicit SubstraitSinkConsumer(
+      AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* generator,
+      arrow::util::BackpressureOptions backpressure = {})
+      : producer_(MakeProducer(generator, std::move(backpressure))) {}
+
+  Status Consume(cp::ExecBatch batch) override;
+
+  static arrow::PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer
+  MakeProducer(AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* out_gen,
+               arrow::util::BackpressureOptions backpressure);
+
+  Future<> Finish() override;
+
+ private:
+  PushGenerator<arrow::util::optional<cp::ExecBatch>>::Producer producer_;
+};
+
+/// \brief An executor to run a Substrait Query
+/// This interface is provided as a utility when creating language
+/// bindings for consuming a Substrait plan.
+class ARROW_ENGINE_EXPORT SubstraitExecutor {
+ public:
+  explicit SubstraitExecutor(
+      std::string substrait_json,
+      AsyncGenerator<arrow::util::optional<cp::ExecBatch>>* generator,
+      std::shared_ptr<cp::ExecPlan> plan,
+      cp::ExecContext exec_context)
+      : substrait_json_(substrait_json),
+        generator_(generator),
+        plan_(std::move(plan)),
+        exec_context_(exec_context) {}
+
+  Result<std::shared_ptr<RecordBatchReader>> Execute();
+
+  Status Close();
+
+  static Result<std::shared_ptr<RecordBatchReader>> GetRecordBatchReader(
+      std::string& substrait_json);

Review Comment:
   We should have another overload that takes a buffer.  We're using json a lot for testing purposes but in real cases we will be passing substrait plans around in protobuf binary form (e.g. buffer).  Then we just skip the "deserialize from json" step.



-- 
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-unsubscribe@arrow.apache.org

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