-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-3121: [C++] Mean aggregate kernel #3708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
fsaintjacques
wants to merge
7
commits into
apache:master
from
fsaintjacques:ARROW-3121-mean-aggregate
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
8bc293f
Move TypeTraits into sum-internal.h
fsaintjacques 3a1a0cd
Refactor sum implementation
fsaintjacques 7929140
Implement mean aggregate
fsaintjacques c448bcb
Add documentation per review
fsaintjacques d1191fd
Deal with NaN values in sum
fsaintjacques d41db22
Refactor with ternary
fsaintjacques 0d39c1f
reformat
fsaintjacques File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| #include "arrow/compute/kernels/mean.h" | ||
|
|
||
| #include <algorithm> | ||
|
|
||
| #include "arrow/compute/kernels/sum-internal.h" | ||
|
|
||
| namespace arrow { | ||
| namespace compute { | ||
|
|
||
| template <typename ArrowType, | ||
| typename SumType = typename FindAccumulatorType<ArrowType>::Type> | ||
| struct MeanState { | ||
| using ThisType = MeanState<ArrowType, SumType>; | ||
|
|
||
| ThisType operator+(const ThisType& rhs) const { | ||
| return ThisType(this->count + rhs.count, this->sum + rhs.sum); | ||
| } | ||
|
|
||
| ThisType& operator+=(const ThisType& rhs) { | ||
| this->count += rhs.count; | ||
| this->sum += rhs.sum; | ||
|
|
||
| return *this; | ||
| } | ||
|
|
||
| std::shared_ptr<Scalar> Finalize() const { | ||
| using ScalarType = typename TypeTraits<DoubleType>::ScalarType; | ||
|
|
||
| const bool is_valid = count > 0; | ||
| const double divisor = static_cast<double>(is_valid ? count : 1UL); | ||
| const double mean = static_cast<double>(sum) / divisor; | ||
|
|
||
| return std::make_shared<ScalarType>(mean, is_valid); | ||
| } | ||
|
|
||
| static std::shared_ptr<DataType> out_type() { | ||
| return TypeTraits<DoubleType>::type_singleton(); | ||
| } | ||
|
|
||
| size_t count = 0; | ||
| typename SumType::c_type sum = 0; | ||
| }; | ||
|
|
||
| #define MEAN_AGG_FN_CASE(T) \ | ||
| case T::type_id: \ | ||
| return std::static_pointer_cast<AggregateFunction>( \ | ||
| std::make_shared<SumAggregateFunction<T, MeanState<T>>>()); | ||
|
|
||
| std::shared_ptr<AggregateFunction> MakeMeanAggregateFunction(const DataType& type, | ||
| FunctionContext* ctx) { | ||
| switch (type.id()) { | ||
| MEAN_AGG_FN_CASE(UInt8Type); | ||
fsaintjacques marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| MEAN_AGG_FN_CASE(Int8Type); | ||
| MEAN_AGG_FN_CASE(UInt16Type); | ||
| MEAN_AGG_FN_CASE(Int16Type); | ||
| MEAN_AGG_FN_CASE(UInt32Type); | ||
| MEAN_AGG_FN_CASE(Int32Type); | ||
| MEAN_AGG_FN_CASE(UInt64Type); | ||
| MEAN_AGG_FN_CASE(Int64Type); | ||
| MEAN_AGG_FN_CASE(FloatType); | ||
| MEAN_AGG_FN_CASE(DoubleType); | ||
| default: | ||
| return nullptr; | ||
| } | ||
|
|
||
| #undef MEAN_AGG_FN_CASE | ||
| } | ||
|
|
||
| static Status GetMeanKernel(FunctionContext* ctx, const DataType& type, | ||
| std::shared_ptr<AggregateUnaryKernel>& kernel) { | ||
| std::shared_ptr<AggregateFunction> aggregate = MakeMeanAggregateFunction(type, ctx); | ||
| if (!aggregate) return Status::Invalid("No mean for type ", type); | ||
|
|
||
| kernel = std::make_shared<AggregateUnaryKernel>(aggregate); | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status Mean(FunctionContext* ctx, const Datum& value, Datum* out) { | ||
| std::shared_ptr<AggregateUnaryKernel> kernel; | ||
|
|
||
| auto data_type = value.type(); | ||
| if (data_type == nullptr) | ||
| return Status::Invalid("Datum must be array-like"); | ||
| else if (!is_integer(data_type->id()) && !is_floating(data_type->id())) | ||
| return Status::Invalid("Datum must contain a NumericType"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to always use braces |
||
|
|
||
| RETURN_NOT_OK(GetMeanKernel(ctx, *data_type, kernel)); | ||
|
|
||
| return kernel->Call(ctx, value, out); | ||
| } | ||
|
|
||
| Status Mean(FunctionContext* ctx, const Array& array, Datum* out) { | ||
| return Mean(ctx, array.data(), out); | ||
| } | ||
|
|
||
| } // namespace compute | ||
| } // namespace arrow | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For self-documenting code you could do:
Also keep in mind that
Datumhas implicit ctors, so theseDatum(...)are unneeded