-
Notifications
You must be signed in to change notification settings - Fork 4k
ARROW-3123: [C++] Implement Count aggregate kernel #3786
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
1
commit into
apache:master
from
fsaintjacques:ARROW-3123-count-aggregate
Closed
Changes from all commits
Commits
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
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/count.h" | ||
|
|
||
| #include "arrow/compute/kernel.h" | ||
| #include "arrow/compute/kernels/aggregate.h" | ||
|
|
||
| namespace arrow { | ||
| namespace compute { | ||
|
|
||
| struct CountState { | ||
| CountState() : non_nulls(0), nulls(0) {} | ||
| CountState(int64_t non_nulls, int64_t nulls) : non_nulls(non_nulls), nulls(nulls) {} | ||
|
|
||
| CountState operator+(const CountState& rhs) const { | ||
| return CountState(this->non_nulls + rhs.non_nulls, this->nulls + rhs.nulls); | ||
| } | ||
|
|
||
| CountState& operator+=(const CountState& rhs) { | ||
| this->non_nulls += rhs.non_nulls; | ||
| this->nulls += rhs.nulls; | ||
| return *this; | ||
| } | ||
|
|
||
| std::shared_ptr<Scalar> NonNullsAsScalar() const { | ||
| using ScalarType = typename CTypeTraits<int64_t>::ScalarType; | ||
| return std::make_shared<ScalarType>(non_nulls); | ||
| } | ||
|
|
||
| std::shared_ptr<Scalar> NullsAsScalar() const { | ||
| using ScalarType = typename CTypeTraits<int64_t>::ScalarType; | ||
| return std::make_shared<ScalarType>(nulls); | ||
| } | ||
|
|
||
| int64_t non_nulls = 0; | ||
| int64_t nulls = 0; | ||
| }; | ||
|
|
||
| class CountAggregateFunction final : public AggregateFunctionStaticState<CountState> { | ||
| public: | ||
| explicit CountAggregateFunction(const CountOptions& options) : options_(options) {} | ||
|
|
||
| Status Consume(const Array& input, CountState* state) const override { | ||
| const int64_t length = input.length(); | ||
| const int64_t nulls = input.null_count(); | ||
|
|
||
| state->nulls = nulls; | ||
| state->non_nulls = length - nulls; | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status Merge(const CountState& src, CountState* dst) const override { | ||
| *dst += src; | ||
| return Status::OK(); | ||
| } | ||
|
|
||
| Status Finalize(const CountState& src, Datum* output) const override { | ||
| switch (options_.count_mode) { | ||
| case CountOptions::COUNT_ALL: | ||
| *output = src.NonNullsAsScalar(); | ||
| break; | ||
| case CountOptions::COUNT_NULL: | ||
| *output = src.NullsAsScalar(); | ||
| break; | ||
| default: | ||
| return Status::Invalid("Unknown CountOptions encountered"); | ||
| } | ||
|
|
||
| return Status::OK(); | ||
| } | ||
|
|
||
| std::shared_ptr<DataType> out_type() const override { return int64(); } | ||
|
|
||
| private: | ||
| CountOptions options_; | ||
| }; | ||
|
|
||
| std::shared_ptr<AggregateFunction> MakeCountAggregateFunction( | ||
| FunctionContext* context, const CountOptions& options) { | ||
| return std::make_shared<CountAggregateFunction>(options); | ||
| } | ||
|
|
||
| Status Count(FunctionContext* context, const CountOptions& options, const Datum& value, | ||
| Datum* out) { | ||
| if (!value.is_array()) return Status::Invalid("Count is expecting an array datum."); | ||
|
|
||
| auto aggregate = MakeCountAggregateFunction(context, options); | ||
| auto kernel = std::make_shared<AggregateUnaryKernel>(aggregate); | ||
|
|
||
| return kernel->Call(context, value, out); | ||
| } | ||
|
|
||
| Status Count(FunctionContext* context, const CountOptions& options, const Array& array, | ||
| Datum* out) { | ||
| return Count(context, options, array.data(), out); | ||
| } | ||
|
|
||
| } // namespace compute | ||
| } // namespace arrow | ||
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,88 @@ | ||
| // 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include <type_traits> | ||
|
|
||
| #include "arrow/status.h" | ||
| #include "arrow/type.h" | ||
| #include "arrow/type_traits.h" | ||
| #include "arrow/util/visibility.h" | ||
|
|
||
| namespace arrow { | ||
|
|
||
| class Array; | ||
| class DataType; | ||
|
|
||
| namespace compute { | ||
|
|
||
| struct Datum; | ||
| class FunctionContext; | ||
| class AggregateFunction; | ||
|
|
||
| /// \class CountOptions | ||
| /// | ||
| /// The user control the Count kernel behavior with this class. By default, the | ||
| /// it will count all non-null values. | ||
| struct ARROW_EXPORT CountOptions { | ||
| enum mode { | ||
| // Count all non-null values. | ||
| COUNT_ALL = 0, | ||
| // Count all null values. | ||
| COUNT_NULL, | ||
| }; | ||
|
|
||
| explicit CountOptions(enum mode count_mode) : count_mode(count_mode) {} | ||
|
|
||
| enum mode count_mode = COUNT_ALL; | ||
| }; | ||
|
|
||
| /// \brief Return Count function aggregate | ||
| ARROW_EXPORT | ||
| std::shared_ptr<AggregateFunction> MakeCount(FunctionContext* context, | ||
| const CountOptions& options); | ||
|
|
||
| /// \brief Count non-null (or null) values in an array. | ||
| /// | ||
| /// \param[in] context the FunctionContext | ||
| /// \param[in] options counting options, see CountOptions for more information | ||
| /// \param[in] datum to count | ||
| /// \param[out] out resulting datum | ||
| /// | ||
| /// \since 0.13.0 | ||
| /// \note API not yet finalized | ||
| ARROW_EXPORT | ||
| Status Count(FunctionContext* context, const CountOptions& options, const Datum& datum, | ||
| Datum* out); | ||
|
|
||
| /// \brief Count non-null (or null) values in an array. | ||
| /// | ||
| /// \param[in] context the FunctionContext | ||
| /// \param[in] options counting options, see CountOptions for more information | ||
| /// \param[in] array to count | ||
| /// \param[out] out resulting datum | ||
| /// | ||
| /// \since 0.13.0 | ||
| /// \note API not yet finalized | ||
| ARROW_EXPORT | ||
| Status Count(FunctionContext* context, const CountOptions& options, const Array& array, | ||
| Datum* out); | ||
|
|
||
| } // namespace compute | ||
| } // namespace arrow |
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.
leave TODO about evaluation on chunked arrays (we need to think about how to address this generally rather than having a bunch of boilerplate)?