vibhatha commented on a change in pull request #12590: URL: https://github.com/apache/arrow/pull/12590#discussion_r837628569
########## File path: cpp/examples/arrow/udf_example.cc ########## @@ -0,0 +1,253 @@ +// 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> // ARROW-15263 +#include <arrow/util/async_generator.h> +#include <arrow/util/future.h> +#include <arrow/util/make_unique.h> +#include <arrow/util/vector.h> + +#include <cstdlib> +#include <iostream> +#include <memory> +#include <string> +#include <type_traits> +#include <utility> +#include <vector> + +// Demonstrate registering a 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::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& options, + const cp::FunctionOptions& other) const override { + return true; + } + std::unique_ptr<cp::FunctionOptions> Copy( + const cp::FunctionOptions& options) const override; +}; + +cp::FunctionOptionsType* GetUDFOptionsType() { + static UDFOptionsType options_type; + return &options_type; +} + +class UDFOptions : public cp::FunctionOptions { + public: + UDFOptions() : cp::FunctionOptions(GetUDFOptionsType()) {} +}; + +std::unique_ptr<cp::FunctionOptions> UDFOptionsType::Copy( + const cp::FunctionOptions&) const { + return std::unique_ptr<cp::FunctionOptions>(new UDFOptions()); +} + +class ExampleNodeOptions : public cp::ExecNodeOptions {}; + +// a basic ExecNode which ignores all input batches +class ExampleNode : public cp::ExecNode { Review comment: Not necessarily, it’s just to show case how the function is used with an execution engine application. I see your point, we can be more objective and just stick to a function example. -- 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