rtpsw commented on code in PR #14043:
URL: https://github.com/apache/arrow/pull/14043#discussion_r1016237509


##########
cpp/src/arrow/compute/function_test.cc:
##########
@@ -351,5 +354,87 @@ TEST(ScalarAggregateFunction, DispatchExact) {
   ASSERT_TRUE(selected_kernel->signature->MatchesInputs(dispatch_args));
 }
 
+namespace {
+
+struct TestFunctionOptions : public FunctionOptions {
+  TestFunctionOptions();
+
+  static const char* kTypeName;
+};
+
+static auto kTestFunctionOptionsType =
+    internal::GetFunctionOptionsType<TestFunctionOptions>();
+
+TestFunctionOptions::TestFunctionOptions() : 
FunctionOptions(kTestFunctionOptionsType) {}
+
+const char* TestFunctionOptions::kTypeName = "test_options";
+
+}  // namespace
+
+TEST(FunctionExecutor, Basics) {
+  VectorFunction func("vector_test", Arity::Binary(), 
/*doc=*/FunctionDoc::Empty());
+  bool init_called = false;
+  ExecContext exec_ctx;
+  TestFunctionOptions options;
+  auto init =
+      [&init_called, &exec_ctx, &options](
+          KernelContext* kernel_ctx,
+          const KernelInitArgs& init_args) -> 
Result<std::unique_ptr<KernelState>> {
+    init_called = true;
+    if (&exec_ctx != kernel_ctx->exec_context()) {
+      return Status::Invalid("expected exec context not found in kernel 
context");
+    }
+    if (&options != init_args.options) {
+      return Status::Invalid("expected options not found in kernel init args");
+    }
+    return NULLPTR;
+  };
+  auto exec = [](KernelContext* ctx, const ExecSpan& args, ExecResult* out) {
+    DCHECK_EQ(2, args.values.size());
+    const int32_t* vals[2];
+    for (size_t i = 0; i < 2; i++) {
+      DCHECK(args.values[i].is_array());
+      const ArraySpan& array = args.values[i].array;
+      DCHECK_EQ(*int32(), *array.type);
+      vals[i] = array.GetValues<int32_t>(1);
+    }
+    DCHECK(out->is_array_data());
+    auto out_data = out->array_data();
+    Int32Builder builder;
+    for (int64_t i = 0; i < args.length; i++) {
+      ARROW_RETURN_NOT_OK(builder.Append(vals[0][i] + vals[1][i]));
+    }
+    ARROW_ASSIGN_OR_RAISE(auto array, builder.Finish());
+    *out_data.get() = *array->data();
+    return Status::OK();
+  };
+  std::vector<InputType> in_types = {int32(), int32()};
+  OutputType out_type = int32();
+  ASSERT_OK(func.AddKernel(in_types, out_type, exec, init));
+  ASSERT_OK_AND_ASSIGN(const Kernel* dispatched, func.DispatchExact({int32(), 
int32()}));
+  ASSERT_EQ(exec, static_cast<const ScalarKernel*>(dispatched)->exec);
+  std::vector<TypeHolder> inputs = {int32(), int32()};
+  ASSERT_OK_AND_ASSIGN(auto func_exec, func.GetBestExecutor(inputs));
+  ASSERT_FALSE(init_called);
+  ASSERT_OK(func_exec->Init(&options, &exec_ctx));
+  ASSERT_TRUE(init_called);
+  auto build_array = [](int32_t i) -> Result<Datum> {
+    Int32Builder builder;
+    ARROW_RETURN_NOT_OK(builder.Append(i));
+    ARROW_ASSIGN_OR_RAISE(auto array, builder.Finish());
+    return Datum(array->data());
+  };
+  for (int32_t i = 1; i <= 3; i++) {
+    ASSERT_OK_AND_ASSIGN(auto value0, build_array(i));
+    ASSERT_OK_AND_ASSIGN(auto value1, build_array(i + 1));

Review Comment:
   I may have missed your intention, but the code didn't come out shorter - see 
upcoming commit.



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

Reply via email to