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 2021/12/27 23:07:54 UTC

[GitHub] [arrow] lidavidm commented on a change in pull request #12033: ARROW-15091: [C++][Doc] Document nodes in C++ streaming execution engine

lidavidm commented on a change in pull request #12033:
URL: https://github.com/apache/arrow/pull/12033#discussion_r775667843



##########
File path: cpp/examples/arrow/execution_plan_documentation_examples.cc
##########
@@ -0,0 +1,1160 @@
+// 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 <memory>
+#include <utility>
+
+#include <arrow/compute/api.h>
+#include <arrow/compute/api_scalar.h>
+#include <arrow/compute/api_vector.h>
+#include <arrow/compute/cast.h>
+#include <arrow/compute/exec/exec_plan.h>
+#include <arrow/compute/exec/ir_consumer.h>
+#include <arrow/compute/exec/test_util.h>
+
+#include <arrow/csv/api.h>
+
+#include <arrow/dataset/dataset.h>
+#include <arrow/dataset/file_parquet.h>
+#include <arrow/dataset/file_base.h>
+#include <arrow/dataset/plan.h>
+#include <arrow/dataset/scanner.h>
+#include <arrow/dataset/dataset_writer.h>
+
+#include <arrow/io/interfaces.h>
+#include <arrow/io/memory.h>
+#include <arrow/io/slow.h>
+#include <arrow/io/transform.h>
+#include <arrow/io/stdio.h>
+
+#include <arrow/result.h>
+#include <arrow/status.h>
+#include <arrow/table.h>
+
+#include <arrow/ipc/api.h>
+
+#include <arrow/util/future.h>
+#include <arrow/util/range.h>
+#include <arrow/util/thread_pool.h>
+#include <arrow/util/vector.h>
+
+// Demonstrate various operators in Arrow Streaming Execution Engine
+
+#define ABORT_ON_FAILURE(expr)                     \
+  do {                                             \
+    arrow::Status status_ = (expr);                \
+    if (!status_.ok()) {                           \
+      std::cerr << status_.message() << std::endl; \
+      abort();                                     \
+    }                                              \
+  } while (0);
+
+#define CHECK_AND_RETURN(expr)                     \
+  do {                                             \
+    arrow::Status status_ = (expr);                \
+    if (!status_.ok()) {                           \
+      std::cerr << status_.message() << std::endl; \
+      return EXIT_FAILURE;                         \
+    } else {                                       \
+      return EXIT_SUCCESS;                         \
+    }                                              \
+  } while (0);
+
+#define CHECK_AND_CONTINUE(expr)                   \
+  do {                                             \
+    arrow::Status status_ = (expr);                \
+    if (!status_.ok()) {                           \
+      std::cerr << status_.message() << std::endl; \
+      return EXIT_FAILURE;                         \
+    }                                              \
+  } while (0);
+
+constexpr char kSep[] = "******";
+
+#define PRINT_BLOCK(msg)                                                     \
+  std::cout << "" << std::endl;                                              \
+  std::cout << "\t" << kSep << " " << msg << " " << kSep << std::endl; \
+  std::cout << "" << std::endl;
+
+#define PRINT_LINE(msg) std::cout << msg << std::endl;
+
+namespace cp = ::arrow::compute;
+
+std::shared_ptr<arrow::Array> GetArrayFromJSON(
+    const std::shared_ptr<arrow::DataType>& type, arrow::util::string_view json) {
+    std::shared_ptr<arrow::Array> out;
+    ABORT_ON_FAILURE(arrow::ipc::internal::json::ArrayFromJSON(type, json, &out));
+  return out;
+}
+
+std::shared_ptr<arrow::RecordBatch> GetRecordBatchFromJSON(
+    const std::shared_ptr<arrow::Schema>& schema, arrow::util::string_view json) {
+  // Parse as a StructArray
+  auto struct_type = struct_(schema->fields());
+  std::shared_ptr<arrow::Array> struct_array = GetArrayFromJSON(struct_type, json);
+
+  // Convert StructArray to RecordBatch
+  return *arrow::RecordBatch::FromStructArray(struct_array);
+}
+
+std::string GetDataAsCsvString() {
+    std::string data_str = "";
+
+    data_str.append("a,b\n");
+    data_str.append("1,null\n");
+    data_str.append("2,true\n");
+    data_str.append("null,true\n");
+    data_str.append("3,false\n");
+    data_str.append("null,true\n");
+    data_str.append("4,false\n");
+    data_str.append("5,null\n");
+    data_str.append("6,false\n");
+    data_str.append("7,false\n");
+    data_str.append("8,true\n");
+
+    return data_str;
+}
+
+arrow::Status CreateDataSetFromCSVData(
+    std::shared_ptr<arrow::dataset::InMemoryDataset> &dataset) {
+    arrow::io::IOContext io_context = arrow::io::default_io_context();
+    std::shared_ptr<arrow::io::InputStream> input;
+    std::string csv_data = GetDataAsCsvString();
+    arrow::util::string_view sv = csv_data;
+    input = std::make_shared<arrow::io::BufferReader>(sv);
+
+    auto read_options = arrow::csv::ReadOptions::Defaults();
+    auto parse_options = arrow::csv::ParseOptions::Defaults();
+    auto convert_options = arrow::csv::ConvertOptions::Defaults();
+
+    // Instantiate TableReader from input stream and options
+    ARROW_ASSIGN_OR_RAISE(std::shared_ptr<arrow::csv::TableReader> table_reader,
+      arrow::csv::TableReader::Make(io_context,
+                                    input,
+                                    read_options,
+                                    parse_options,
+                                    convert_options));
+
+    std::shared_ptr<arrow::csv::TableReader> reader = table_reader;
+
+    // Read table from CSV file
+    ARROW_ASSIGN_OR_RAISE(auto maybe_table,
+      reader->Read());
+    auto ds = std::make_shared<arrow::dataset::InMemoryDataset>(maybe_table);
+    dataset = std::move(ds);
+    return arrow::Status::OK();
+}
+
+std::shared_ptr<arrow::dataset::Dataset> CreateDataset() {
+    std::shared_ptr<arrow::dataset::InMemoryDataset> im_dataset;
+    ABORT_ON_FAILURE(CreateDataSetFromCSVData(im_dataset));
+    return im_dataset;
+}
+
+arrow::Status exec_plan_end_to_end_sample() {
+    cp::ExecContext exec_context(arrow::default_memory_pool(),
+                                ::arrow::internal::GetCpuThreadPool());
+
+    // ensure arrow::dataset node factories are in the registry
+    arrow::dataset::internal::Initialize();

Review comment:
       It is called already when you use a relevant API. Do not call it directly, or if you need to call it directly, then it should be fixed. We should not use internal APIs in an example that is meant for users.




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