MCPcopy Create free account
hub / github.com/apache/arrow / RegisterScalarAggregateFunction

Function RegisterScalarAggregateFunction

python/pyarrow/src/arrow/python/udf.cc:518–561  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

516}
517
518Status 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

Callers 1

Calls 8

GetFunctionRegistryFunction · 0.85
emplace_backMethod · 0.80
TypeErrorFunction · 0.50
DefaultsFunction · 0.50
MakeFunction · 0.50
OKFunction · 0.50
AddKernelMethod · 0.45
AddFunctionMethod · 0.45

Tested by

no test coverage detected