vibhatha commented on a change in pull request #12590: URL: https://github.com/apache/arrow/pull/12590#discussion_r836068466
########## File path: cpp/examples/arrow/udf_example.cc ########## @@ -0,0 +1,264 @@ +// 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/api.h> +#include <arrow/compute/api.h> +#include <arrow/compute/exec/exec_plan.h> +#include <arrow/compute/exec/expression.h> +#include <arrow/compute/exec/options.h> +#include <arrow/datum.h> +#include <arrow/record_batch.h> +#include <arrow/result.h> +#include <arrow/status.h> +#include <arrow/table.h> +#include <arrow/util/async_generator.h> +#include <arrow/util/future.h> +#include <arrow/util/vector.h> + +#include <cstdlib> +#include <iostream> +#include <memory> + +// Demonstrate registering an user-defined Arrow compute function outside of the Arrow +// source tree + +namespace cp = ::arrow::compute; + +#define ABORT_ON_FAILURE(expr) \ + do { \ + arrow::Status status_ = (expr); \ + if (!status_.ok()) { \ + std::cerr << status_.message() << std::endl; \ + abort(); \ + } \ + } while (0); + +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; + ARROW_RETURN_NOT_OK(builder.Reserve(values.size())); + std::shared_ptr<ARROW_ARRAY_TYPE> array; + ARROW_RETURN_NOT_OK(builder.AppendValues(values)); + ARROW_RETURN_NOT_OK(builder.Finish(&array)); + return array; +} + +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::Table>> GetTable() { + std::shared_ptr<arrow::Table> table; + + auto field_vector = { + arrow::field("a", arrow::int64()), arrow::field("x", arrow::int64()), + arrow::field("y", arrow::int64()), arrow::field("z", arrow::int64()), + arrow::field("b", arrow::boolean())}; + + ARROW_ASSIGN_OR_RAISE(auto int_array, + GetArrayDataSample<arrow::Int64Type>({1, 2, 3, 4, 5, 6})); + ARROW_ASSIGN_OR_RAISE(auto x, + GetArrayDataSample<arrow::Int64Type>({21, 22, 23, 24, 25, 26})); + ARROW_ASSIGN_OR_RAISE(auto y, + GetArrayDataSample<arrow::Int64Type>({31, 32, 33, 34, 35, 36})); + ARROW_ASSIGN_OR_RAISE(auto z, + GetArrayDataSample<arrow::Int64Type>({41, 42, 43, 44, 45, 46})); + ARROW_ASSIGN_OR_RAISE(auto bool_array, GetArrayDataSample<arrow::BooleanType>( + {false, true, false, true, true, false})); + + auto schema = arrow::schema(field_vector); + auto data_vector = {int_array, x, y, z, bool_array}; + + table = arrow::Table::Make(schema, data_vector, 6); + + return table; +} + +class UDFOptionsType : public cp::FunctionOptionsType { + const char* type_name() const override { return "UDFOptionsType"; } + std::string Stringify(const cp::FunctionOptions&) const override { + return "UDFOptionsType"; + } + bool Compare(const cp::FunctionOptions&, const cp::FunctionOptions&) const override { + return true; + } + std::unique_ptr<cp::FunctionOptions> Copy(const cp::FunctionOptions&) const override; Review comment: It is implemented below. ```c++ std::unique_ptr<cp::FunctionOptions> UDFOptionsType::Copy( const cp::FunctionOptions&) const { return std::unique_ptr<cp::FunctionOptions>(new UDFOptions()); } ``` -- 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]
