mapleFU commented on code in PR #37100:
URL: https://github.com/apache/arrow/pull/37100#discussion_r1299393432
##########
cpp/src/arrow/compute/kernels/aggregate_basic_internal.h:
##########
@@ -891,6 +891,105 @@ struct NullMinMaxImpl : public ScalarAggregator {
}
};
+template <SimdLevel::type SimdLevel>
+struct DictionaryMinMaxImpl : public ScalarAggregator {
+ using ThisType = DictionaryMinMaxImpl<SimdLevel>;
+
+ DictionaryMinMaxImpl(std::shared_ptr<DataType> out_type,
ScalarAggregateOptions options)
+ : options(std::move(options)),
+ out_type(std::move(out_type)),
+ has_nulls(false),
+ count(0),
+ min(nullptr),
+ max(nullptr) {
+ this->options.min_count = std::max<uint32_t>(1, this->options.min_count);
+ }
+
+ Status Consume(KernelContext*, const ExecSpan& batch) override {
+ if (batch[0].is_scalar()) {
+ return Status::NotImplemented("No min/max implemented for
DictionaryScalar");
+ }
+
+ DictionaryArray arr(batch[0].array.ToArrayData());
+ this->has_nulls = arr.null_count() > 0;
+ this->count += arr.length() - arr.null_count();
+
+ Datum dict_values(arr.dictionary());
+ ARROW_ASSIGN_OR_RAISE(Datum result, MinMax(std::move(dict_values)));
+ const StructScalar& struct_result =
+ checked_cast<const StructScalar&>(*std::move(result.scalar()));
+ ARROW_ASSIGN_OR_RAISE(auto min_, struct_result.field(FieldRef("min")));
+ ARROW_ASSIGN_OR_RAISE(auto max_, struct_result.field(FieldRef("max")));
+ ARROW_RETURN_NOT_OK(CompareMinMax(std::move(min_), std::move(max_)));
+ return Status::OK();
+ }
+
+ Status MergeFrom(KernelContext*, KernelState&& src) override {
+ const auto& other = checked_cast<const ThisType&>(src);
+
+ ARROW_RETURN_NOT_OK(CompareMinMax(other.min, other.max));
+ this->has_nulls = this->has_nulls || other.has_nulls;
+ this->count += other.count;
+ return Status::OK();
+ }
+
+ Status Finalize(KernelContext*, Datum* out) override {
+ const auto& struct_type = checked_cast<const StructType&>(*out_type);
+ const auto& child_type = struct_type.field(0)->type();
+
+ std::vector<std::shared_ptr<Scalar>> values;
+ // Physical type != result type
+ if ((this->has_nulls && !options.skip_nulls) || (this->count <
options.min_count) ||
+ this->min == nullptr || this->min->type->id() == Type::NA) {
+ // (null, null)
+ std::shared_ptr<Scalar> null_scalar = MakeNullScalar(child_type);
+ values = {null_scalar, null_scalar};
+ } else {
+ values = {std::move(this->min), std::move(this->max)};
+ }
+
+ out->value = std::make_shared<StructScalar>(std::move(values),
this->out_type);
+ return Status::OK();
+ }
+
+ ScalarAggregateOptions options;
+ std::shared_ptr<DataType> out_type;
+ bool has_nulls;
+ int64_t count;
+ std::shared_ptr<Scalar> min;
+ std::shared_ptr<Scalar> max;
+
+ private:
+ Status CompareMinMax(std::shared_ptr<Scalar> min_, std::shared_ptr<Scalar>
max_) {
Review Comment:
```suggestion
Status CompareMinMax(const std::shared_ptr<Scalar>& min, const
std::shared_ptr<Scalar>& max_) {
```
Can we just use this to avoid unneccessary copy on `std::shared_ptr`?
##########
cpp/src/arrow/compute/kernels/aggregate_basic_internal.h:
##########
@@ -891,6 +891,105 @@ struct NullMinMaxImpl : public ScalarAggregator {
}
};
+template <SimdLevel::type SimdLevel>
+struct DictionaryMinMaxImpl : public ScalarAggregator {
+ using ThisType = DictionaryMinMaxImpl<SimdLevel>;
+
+ DictionaryMinMaxImpl(std::shared_ptr<DataType> out_type,
ScalarAggregateOptions options)
+ : options(std::move(options)),
+ out_type(std::move(out_type)),
+ has_nulls(false),
+ count(0),
+ min(nullptr),
+ max(nullptr) {
+ this->options.min_count = std::max<uint32_t>(1, this->options.min_count);
+ }
+
+ Status Consume(KernelContext*, const ExecSpan& batch) override {
+ if (batch[0].is_scalar()) {
+ return Status::NotImplemented("No min/max implemented for
DictionaryScalar");
+ }
+
+ DictionaryArray arr(batch[0].array.ToArrayData());
+ this->has_nulls = arr.null_count() > 0;
+ this->count += arr.length() - arr.null_count();
+
+ Datum dict_values(arr.dictionary());
+ ARROW_ASSIGN_OR_RAISE(Datum result, MinMax(std::move(dict_values)));
Review Comment:
Would it be possible that a value doesn't exist in any dict value?
##########
cpp/src/arrow/compute/kernels/aggregate_basic_internal.h:
##########
@@ -891,6 +891,105 @@ struct NullMinMaxImpl : public ScalarAggregator {
}
};
+template <SimdLevel::type SimdLevel>
+struct DictionaryMinMaxImpl : public ScalarAggregator {
+ using ThisType = DictionaryMinMaxImpl<SimdLevel>;
+
+ DictionaryMinMaxImpl(std::shared_ptr<DataType> out_type,
ScalarAggregateOptions options)
+ : options(std::move(options)),
+ out_type(std::move(out_type)),
+ has_nulls(false),
+ count(0),
+ min(nullptr),
+ max(nullptr) {
+ this->options.min_count = std::max<uint32_t>(1, this->options.min_count);
+ }
+
+ Status Consume(KernelContext*, const ExecSpan& batch) override {
+ if (batch[0].is_scalar()) {
+ return Status::NotImplemented("No min/max implemented for
DictionaryScalar");
+ }
+
+ DictionaryArray arr(batch[0].array.ToArrayData());
+ this->has_nulls = arr.null_count() > 0;
+ this->count += arr.length() - arr.null_count();
+
+ Datum dict_values(arr.dictionary());
+ ARROW_ASSIGN_OR_RAISE(Datum result, MinMax(std::move(dict_values)));
Review Comment:
Would it be possible that a value doesn't exist in any dict value?
##########
cpp/src/arrow/compute/kernels/aggregate_basic_internal.h:
##########
@@ -891,6 +891,105 @@ struct NullMinMaxImpl : public ScalarAggregator {
}
};
+template <SimdLevel::type SimdLevel>
+struct DictionaryMinMaxImpl : public ScalarAggregator {
+ using ThisType = DictionaryMinMaxImpl<SimdLevel>;
+
+ DictionaryMinMaxImpl(std::shared_ptr<DataType> out_type,
ScalarAggregateOptions options)
+ : options(std::move(options)),
+ out_type(std::move(out_type)),
+ has_nulls(false),
+ count(0),
+ min(nullptr),
+ max(nullptr) {
+ this->options.min_count = std::max<uint32_t>(1, this->options.min_count);
+ }
+
+ Status Consume(KernelContext*, const ExecSpan& batch) override {
+ if (batch[0].is_scalar()) {
+ return Status::NotImplemented("No min/max implemented for
DictionaryScalar");
+ }
+
+ DictionaryArray arr(batch[0].array.ToArrayData());
+ this->has_nulls = arr.null_count() > 0;
+ this->count += arr.length() - arr.null_count();
+
+ Datum dict_values(arr.dictionary());
+ ARROW_ASSIGN_OR_RAISE(Datum result, MinMax(std::move(dict_values)));
+ const StructScalar& struct_result =
+ checked_cast<const StructScalar&>(*std::move(result.scalar()));
Review Comment:
why `std::move` used here?
--
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]