| 502 | // For "min" and "max" functions: override finalize and return the actual value |
| 503 | template <MinOrMax min_or_max> |
| 504 | void AddMinOrMaxAggKernel(ScalarAggregateFunction* func, |
| 505 | ScalarAggregateFunction* min_max_func) { |
| 506 | auto sig = KernelSignature::Make({InputType::Any()}, FirstType); |
| 507 | auto init = [min_max_func]( |
| 508 | KernelContext* ctx, |
| 509 | const KernelInitArgs& args) -> Result<std::unique_ptr<KernelState>> { |
| 510 | ARROW_ASSIGN_OR_RAISE(auto kernel, min_max_func->DispatchExact(args.inputs)); |
| 511 | KernelInitArgs new_args{kernel, args.inputs, args.options}; |
| 512 | return kernel->init(ctx, new_args); |
| 513 | }; |
| 514 | |
| 515 | auto finalize = [](KernelContext* ctx, Datum* out) -> Status { |
| 516 | Datum temp; |
| 517 | RETURN_NOT_OK(checked_cast<ScalarAggregator*>(ctx->state())->Finalize(ctx, &temp)); |
| 518 | const auto& result = temp.scalar_as<StructScalar>(); |
| 519 | DCHECK(result.is_valid); |
| 520 | *out = result.value[static_cast<uint8_t>(min_or_max)]; |
| 521 | return Status::OK(); |
| 522 | }; |
| 523 | |
| 524 | // Note SIMD level is always NONE, but the convenience kernel will |
| 525 | // dispatch to an appropriate implementation |
| 526 | AddAggKernel(std::move(sig), std::move(init), std::move(finalize), func); |
| 527 | } |
| 528 | |
| 529 | // ---------------------------------------------------------------------- |
| 530 | // Any implementation |