vibhatha commented on a change in pull request #12033: URL: https://github.com/apache/arrow/pull/12033#discussion_r789360176
########## File path: cpp/examples/arrow/execution_plan_documentation_examples.cc ########## @@ -0,0 +1,906 @@ +// 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. + +// (Doc section: Execution Plan Documentation Example) + +#include <memory> +#include <utility> + +#include <arrow/array.h> +#include <arrow/builder.h> + +#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/dataset_writer.h> +#include <arrow/dataset/file_base.h> +#include <arrow/dataset/file_parquet.h> +#include <arrow/dataset/plan.h> +#include <arrow/dataset/scanner.h> + +#include <arrow/io/interfaces.h> +#include <arrow/io/memory.h> +#include <arrow/io/slow.h> +#include <arrow/io/stdio.h> +#include <arrow/io/transform.h> + +#include "arrow/json/api.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 + +namespace cp = ::arrow::compute; + +constexpr char kSep[] = "******"; + +void PrintBlock(std::string msg) { + std::cout << "\n\t" << kSep << " " << msg << " " << kSep << "\n" << std::endl; +} + +const char kCsvData[] = R"csv(a,b +1,null +2,true +null,true +3,false +null,true +4,false +5,null +6,false +7,false +8,true +)csv"; + +template <typename TYPE, + typename = typename std::enable_if<arrow::is_number_type<TYPE>::value | + arrow::is_boolean_type<TYPE>::value | + arrow::is_temporal_type<TYPE>::value>::type> +arrow::Result<std::shared_ptr<arrow::Array>> GetArrayDataSample( + const std::vector<typename TYPE::c_type>& values) { + using ARROW_ARRAY_TYPE = typename arrow::TypeTraits<TYPE>::ArrayType; + using ARROW_BUILDER_TYPE = typename arrow::TypeTraits<TYPE>::BuilderType; + ARROW_BUILDER_TYPE builder; + ABORT_NOT_OK(builder.Reserve(values.size())); + std::shared_ptr<ARROW_ARRAY_TYPE> array; + ABORT_NOT_OK(builder.AppendValues(values)); + ABORT_NOT_OK(builder.Finish(&array)); + arrow::Result<std::shared_ptr<ARROW_ARRAY_TYPE>> result(std::move(array)); + return result; +} + +template <class TYPE> +arrow::Result<std::shared_ptr<arrow::Array>> GetBinaryArrayDataSample( + const std::vector<std::string>& values) { + using ARROW_ARRAY_TYPE = typename arrow::TypeTraits<TYPE>::ArrayType; + using ARROW_BUILDER_TYPE = typename arrow::TypeTraits<TYPE>::BuilderType; + ARROW_BUILDER_TYPE builder; + ABORT_NOT_OK(builder.Reserve(values.size())); + std::shared_ptr<ARROW_ARRAY_TYPE> array; + ABORT_NOT_OK(builder.AppendValues(values)); + ABORT_NOT_OK(builder.Finish(&array)); + arrow::Result<std::shared_ptr<ARROW_ARRAY_TYPE>> result(std::move(array)); + return result; +} + +arrow::Result<std::shared_ptr<arrow::RecordBatch>> GetSampleRecordBatch( + const arrow::ArrayVector array_vector, const arrow::FieldVector& field_vector) { + std::shared_ptr<arrow::RecordBatch> record_batch; + ARROW_ASSIGN_OR_RAISE(auto struct_result, + arrow::StructArray::Make(array_vector, field_vector)); + return record_batch->FromStructArray(struct_result); +} + +arrow::Result<std::shared_ptr<arrow::dataset::Dataset>> CreateDataSetFromCSVData() { + arrow::io::IOContext io_context = arrow::io::default_io_context(); + std::shared_ptr<arrow::io::InputStream> input; + input = std::make_shared<arrow::io::BufferReader>(arrow::Buffer::FromString(kCsvData)); + + 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); + arrow::Result<std::shared_ptr<arrow::dataset::InMemoryDataset>> result(std::move(ds)); + return result; +} + +// (Doc section: Materialize) +cp::Expression Materialize(std::vector<std::string> names) { + std::vector<cp::Expression> exprs; + for (const auto& name : names) { + exprs.push_back(cp::field_ref(name)); + } + + return cp::project(exprs, names); Review comment: Of course, I have missed this. I will update it. -- 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