pitrou commented on a change in pull request #11152:
URL: https://github.com/apache/arrow/pull/11152#discussion_r709906988



##########
File path: cpp/src/arrow/compute/kernels/aggregate_basic.cc
##########
@@ -282,6 +282,43 @@ Result<std::unique_ptr<KernelState>> 
MinMaxInit(KernelContext* ctx,
   return visitor.Create();
 }
 
+// For "min" and "max" functions: dispatch to the MinMax kernel.
+Result<std::unique_ptr<KernelState>> MinOrMaxInit(KernelContext* ctx,
+                                                  const KernelInitArgs& args) {
+  std::vector<ValueDescr> inputs = args.inputs;
+
+  ARROW_ASSIGN_OR_RAISE(auto func, 
GetFunctionRegistry()->GetFunction("min_max"));

Review comment:
       For the record, it's a bit of a pity to do a second registry lookup when 
arriving here.

##########
File path: cpp/src/arrow/compute/kernels/aggregate_basic.cc
##########
@@ -282,6 +282,43 @@ Result<std::unique_ptr<KernelState>> 
MinMaxInit(KernelContext* ctx,
   return visitor.Create();
 }
 
+// For "min" and "max" functions: dispatch to the MinMax kernel.
+Result<std::unique_ptr<KernelState>> MinOrMaxInit(KernelContext* ctx,
+                                                  const KernelInitArgs& args) {
+  std::vector<ValueDescr> inputs = args.inputs;
+
+  ARROW_ASSIGN_OR_RAISE(auto func, 
GetFunctionRegistry()->GetFunction("min_max"));
+  ARROW_ASSIGN_OR_RAISE(auto kernel, func->DispatchBest(&inputs));
+
+  KernelInitArgs new_args{kernel, inputs, args.options};
+  return kernel->init(ctx, new_args);
+}
+
+// For "min" and "max" functions: override finalize and return the actual value
+template <MinOrMax min_or_max>
+void AddMinOrMaxAggKernel(ScalarAggregateFunction* func) {
+  auto sig = KernelSignature::Make(
+      {InputType(ValueDescr::ANY)},
+      OutputType([](KernelContext*,
+                    const std::vector<ValueDescr>& descrs) -> 
Result<ValueDescr> {
+        // any[T] -> scalar[T]
+        return ValueDescr::Scalar(descrs.front().type);
+      }));
+
+  auto finalize = [](KernelContext* ctx, Datum* out) -> Status {
+    Datum temp;
+    RETURN_NOT_OK(checked_cast<ScalarAggregator*>(ctx->state())->Finalize(ctx, 
&temp));
+    *out = 
temp.scalar_as<StructScalar>().value[static_cast<uint8_t>(min_or_max)];

Review comment:
       DCHECK that the result is valid?

##########
File path: docs/source/cpp/compute.rst
##########
@@ -198,6 +198,10 @@ the input to a single output value.
 
+---------------+-------+-------------+------------------------+----------------------------------+-------+
 | mean          | Unary | Numeric     | Scalar Decimal/Float64 | 
:struct:`ScalarAggregateOptions` |       |
 
+---------------+-------+-------------+------------------------+----------------------------------+-------+
+| max           | Unary | Numeric     | Scalar Struct          | 
:struct:`ScalarAggregateOptions` |       |

Review comment:
       "max" before "mean"




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