Skip to content

Commit 03e05bb

Browse files
committed
specialize minmax for boolean
1 parent 48da080 commit 03e05bb

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

cpp/src/arrow/compute/kernels/aggregate_basic.cc

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,34 @@ struct MinMaxImpl : public ScalarAggregator {
502502
MinMaxState<ArrowType> state;
503503
};
504504

505+
struct BooleanMinMaxImpl : public MinMaxImpl<BooleanType> {
506+
507+
using MinMaxImpl::MinMaxImpl;
508+
509+
void Consume(KernelContext*, const ExecBatch& batch) override {
510+
StateType local;
511+
ArrayType arr(batch[0].array());
512+
513+
const auto arr_length = arr.length();
514+
const auto null_count = arr.null_count();
515+
const auto valid_count = arr_length - null_count;
516+
517+
local.has_nulls = null_count > 0;
518+
local.has_values = valid_count > 0;
519+
if (local.has_nulls && options.null_handling == MinMaxOptions::OUTPUT_NULL) {
520+
this->state = local;
521+
return;
522+
}
523+
524+
const auto true_count = arr.true_count();
525+
const auto false_count = valid_count - true_count;
526+
local.max = true_count > 0;
527+
local.min = false_count == 0;
528+
529+
this->state = local;
530+
}
531+
};
532+
505533
struct MinMaxInitState {
506534
std::unique_ptr<KernelState> state;
507535
KernelContext* ctx;
@@ -522,7 +550,7 @@ struct MinMaxInitState {
522550
}
523551

524552
Status Visit(const BooleanType&) {
525-
state.reset(new MinMaxImpl<BooleanType>(out_type, options));
553+
state.reset(new BooleanMinMaxImpl(out_type, options));
526554
return Status::OK();
527555
}
528556

0 commit comments

Comments
 (0)