| 516 | } |
| 517 | |
| 518 | Status RegisterScalarAggregateFunction(PyObject* function, UdfWrapperCallback cb, |
| 519 | const UdfOptions& options, |
| 520 | compute::FunctionRegistry* registry) { |
| 521 | if (!PyCallable_Check(function)) { |
| 522 | return Status::TypeError("Expected a callable Python object."); |
| 523 | } |
| 524 | |
| 525 | if (registry == NULLPTR) { |
| 526 | registry = compute::GetFunctionRegistry(); |
| 527 | } |
| 528 | |
| 529 | static auto default_scalar_aggregate_options = |
| 530 | compute::ScalarAggregateOptions::Defaults(); |
| 531 | auto aggregate_func = std::make_shared<compute::ScalarAggregateFunction>( |
| 532 | options.func_name, options.arity, options.func_doc, |
| 533 | &default_scalar_aggregate_options); |
| 534 | |
| 535 | std::vector<compute::InputType> input_types; |
| 536 | for (const auto& in_dtype : options.input_types) { |
| 537 | input_types.emplace_back(in_dtype); |
| 538 | } |
| 539 | compute::OutputType output_type(options.output_type); |
| 540 | |
| 541 | // Take reference before wrapping with OwnedRefNoGIL |
| 542 | Py_INCREF(function); |
| 543 | auto function_ref = std::make_shared<OwnedRefNoGIL>(function); |
| 544 | |
| 545 | compute::KernelInit init = [cb, function_ref, options]( |
| 546 | compute::KernelContext* ctx, |
| 547 | const compute::KernelInitArgs& args) |
| 548 | -> Result<std::unique_ptr<compute::KernelState>> { |
| 549 | return std::make_unique<PythonUdfScalarAggregatorImpl>( |
| 550 | function_ref, cb, options.input_types, options.output_type); |
| 551 | }; |
| 552 | |
| 553 | auto sig = compute::KernelSignature::Make( |
| 554 | std::move(input_types), std::move(output_type), options.arity.is_varargs); |
| 555 | compute::ScalarAggregateKernel kernel(std::move(sig), std::move(init), |
| 556 | AggregateUdfConsume, AggregateUdfMerge, |
| 557 | AggregateUdfFinalize, /*ordered=*/false); |
| 558 | RETURN_NOT_OK(aggregate_func->AddKernel(std::move(kernel))); |
| 559 | RETURN_NOT_OK(registry->AddFunction(std::move(aggregate_func))); |
| 560 | return Status::OK(); |
| 561 | } |
| 562 | |
| 563 | /// \brief Create a new UdfOptions with adjustment for hash kernel |
| 564 | /// \param options User provided udf options |
no test coverage detected