aggregation.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cudf/types.hpp>
20 
21 #include <functional>
22 #include <memory>
23 #include <vector>
24 
34 namespace cudf {
41 // forward declaration
42 namespace detail {
43 class simple_aggregations_collector;
44 class aggregation_finalizer;
45 } // namespace detail
46 
53 enum class rank_method : int32_t {
54  FIRST,
55  AVERAGE,
56  MIN,
57  MAX,
58  DENSE
59 };
60 
66 enum class rank_percentage : int32_t {
67  NONE,
68  ZERO_NORMALIZED,
70 };
71 
80 class aggregation {
81  public:
85  enum Kind {
86  SUM,
88  MIN,
89  MAX,
92  ANY,
93  ALL,
95  MEAN,
96  M2,
98  STD,
110  LAG,
111  PTX,
120  };
121 
122  aggregation() = delete;
123 
131  virtual ~aggregation() = default;
132 
139  [[nodiscard]] virtual bool is_equal(aggregation const& other) const { return kind == other.kind; }
140 
146  [[nodiscard]] virtual size_t do_hash() const { return std::hash<int>{}(kind); }
147 
153  [[nodiscard]] virtual std::unique_ptr<aggregation> clone() const = 0;
154 
155  // override functions for compound aggregations
163  virtual std::vector<std::unique_ptr<aggregation>> get_simple_aggregations(
164  data_type col_type, cudf::detail::simple_aggregations_collector& collector) const = 0;
165 
172  virtual void finalize(cudf::detail::aggregation_finalizer& finalizer) const = 0;
173 };
174 
182 class rolling_aggregation : public virtual aggregation {
183  public:
184  ~rolling_aggregation() override = default;
185 
186  protected:
189  using aggregation::aggregation;
190 };
191 
195 class groupby_aggregation : public virtual aggregation {
196  public:
197  ~groupby_aggregation() override = default;
198 
199  protected:
201 };
202 
206 class groupby_scan_aggregation : public virtual aggregation {
207  public:
208  ~groupby_scan_aggregation() override = default;
209 
210  protected:
212 };
213 
217 class reduce_aggregation : public virtual aggregation {
218  public:
219  ~reduce_aggregation() override = default;
220 
221  protected:
222  reduce_aggregation() {}
223 };
224 
228 class scan_aggregation : public virtual aggregation {
229  public:
230  ~scan_aggregation() override = default;
231 
232  protected:
233  scan_aggregation() {}
234 };
235 
240  public:
241  ~segmented_reduce_aggregation() override = default;
242 
243  protected:
245 };
246 
248 enum class udf_type : bool { CUDA, PTX };
250 enum class correlation_type : int32_t { PEARSON, KENDALL, SPEARMAN };
251 
254 template <typename Base = aggregation>
255 std::unique_ptr<Base> make_sum_aggregation();
256 
259 template <typename Base = aggregation>
260 std::unique_ptr<Base> make_product_aggregation();
261 
264 template <typename Base = aggregation>
265 std::unique_ptr<Base> make_min_aggregation();
266 
269 template <typename Base = aggregation>
270 std::unique_ptr<Base> make_max_aggregation();
271 
278 template <typename Base = aggregation>
279 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
280 
283 template <typename Base = aggregation>
284 std::unique_ptr<Base> make_any_aggregation();
285 
288 template <typename Base = aggregation>
289 std::unique_ptr<Base> make_all_aggregation();
290 
293 template <typename Base = aggregation>
294 std::unique_ptr<Base> make_sum_of_squares_aggregation();
295 
298 template <typename Base = aggregation>
299 std::unique_ptr<Base> make_mean_aggregation();
300 
313 template <typename Base = aggregation>
314 std::unique_ptr<Base> make_m2_aggregation();
315 
325 template <typename Base = aggregation>
326 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
327 
337 template <typename Base = aggregation>
338 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
339 
342 template <typename Base = aggregation>
343 std::unique_ptr<Base> make_median_aggregation();
344 
352 template <typename Base = aggregation>
353 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
354  interpolation interp = interpolation::LINEAR);
355 
362 template <typename Base = aggregation>
363 std::unique_ptr<Base> make_argmax_aggregation();
364 
371 template <typename Base = aggregation>
372 std::unique_ptr<Base> make_argmin_aggregation();
373 
381 template <typename Base = aggregation>
382 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
383 
398 template <typename Base = aggregation>
399 std::unique_ptr<Base> make_nth_element_aggregation(
400  size_type n, null_policy null_handling = null_policy::INCLUDE);
401 
404 template <typename Base = aggregation>
405 std::unique_ptr<Base> make_row_number_aggregation();
406 
479 template <typename Base = aggregation>
480 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
481  order column_order = order::ASCENDING,
482  null_policy null_handling = null_policy::EXCLUDE,
483  null_order null_precedence = null_order::AFTER,
484  rank_percentage percentage = rank_percentage::NONE);
485 
497 template <typename Base = aggregation>
498 std::unique_ptr<Base> make_collect_list_aggregation(
499  null_policy null_handling = null_policy::INCLUDE);
500 
517 template <typename Base = aggregation>
518 std::unique_ptr<Base> make_collect_set_aggregation(null_policy null_handling = null_policy::INCLUDE,
519  null_equality nulls_equal = null_equality::EQUAL,
520  nan_equality nans_equal = nan_equality::UNEQUAL);
521 
528 template <typename Base = aggregation>
529 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
530 
537 template <typename Base = aggregation>
538 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
539 
549 template <typename Base = aggregation>
550 std::unique_ptr<Base> make_udf_aggregation(udf_type type,
551  std::string const& user_defined_aggregator,
552  data_type output_type);
553 
565 template <typename Base = aggregation>
566 std::unique_ptr<Base> make_merge_lists_aggregation();
567 
590 template <typename Base = aggregation>
591 std::unique_ptr<Base> make_merge_sets_aggregation(null_equality nulls_equal = null_equality::EQUAL,
592  nan_equality nans_equal = nan_equality::UNEQUAL);
593 
608 template <typename Base = aggregation>
609 std::unique_ptr<Base> make_merge_m2_aggregation();
610 
621 template <typename Base = aggregation>
622 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
623 
634 template <typename Base = aggregation>
636  size_type min_periods = 1);
637 
672 template <typename Base>
673 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
674 
710 template <typename Base>
711 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
712  // end of group
714 } // namespace cudf
cudf::rank_method
rank_method
Tie-breaker method to use for ranking the column.
Definition: aggregation.hpp:53
cudf::scan_aggregation
Derived class intended for scan usage.
Definition: aggregation.hpp:228
cudf::make_variance_aggregation
std::unique_ptr< Base > make_variance_aggregation(size_type ddof=1)
Factory to create a VARIANCE aggregation.
cudf::aggregation::finalize
virtual void finalize(cudf::detail::aggregation_finalizer &finalizer) const =0
Compute the aggregation after pre-requisite simple aggregations have been computed.
cudf::quantiles
std::unique_ptr< table > quantiles(table_view const &input, std::vector< double > const &q, interpolation interp=interpolation::NEAREST, cudf::sorted is_input_sorted=sorted::NO, std::vector< order > const &column_order={}, std::vector< null_order > const &null_precedence={}, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Returns the rows of the input corresponding to the requested quantiles.
cudf::make_m2_aggregation
std::unique_ptr< Base > make_m2_aggregation()
Factory to create a M2 aggregation.
cudf::aggregation::kind
Kind kind
The aggregation to perform.
Definition: aggregation.hpp:130
cudf::rank_method::MIN
@ MIN
min of first in the group
cudf::size_type
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:80
cudf::reduce_aggregation
Derived class intended for reduction usage.
Definition: aggregation.hpp:217
cudf::null_policy
null_policy
Enum to specify whether to include nulls or exclude nulls.
Definition: types.hpp:119
cudf::aggregation::NTH_ELEMENT
@ NTH_ELEMENT
get the nth element
Definition: aggregation.hpp:104
cudf::aggregation::SUM
@ SUM
sum reduction
Definition: aggregation.hpp:86
cudf::make_argmax_aggregation
std::unique_ptr< Base > make_argmax_aggregation()
Factory to create an ARGMAX aggregation.
cudf::make_min_aggregation
std::unique_ptr< Base > make_min_aggregation()
types.hpp
Type declarations for libcudf.
cudf::interpolation
interpolation
Interpolation method to use when the desired quantile lies between two data points i and j.
Definition: types.hpp:185
cudf::aggregation::MIN
@ MIN
min reduction
Definition: aggregation.hpp:88
cudf::make_row_number_aggregation
std::unique_ptr< Base > make_row_number_aggregation()
cudf::make_merge_lists_aggregation
std::unique_ptr< Base > make_merge_lists_aggregation()
Factory to create a MERGE_LISTS aggregation.
cudf::aggregation::STD
@ STD
standard deviation
Definition: aggregation.hpp:98
cudf::make_sum_aggregation
std::unique_ptr< Base > make_sum_aggregation()
cudf::aggregation::COUNT_VALID
@ COUNT_VALID
count number of valid elements
Definition: aggregation.hpp:90
cudf::aggregation::ARGMAX
@ ARGMAX
Index of max element.
Definition: aggregation.hpp:101
cudf::aggregation::PRODUCT
@ PRODUCT
product reduction
Definition: aggregation.hpp:87
cudf::aggregation::COLLECT_LIST
@ COLLECT_LIST
collect values into a list
Definition: aggregation.hpp:107
cudf::rank_method::MAX
@ MAX
max of first in the group
cudf::segmented_reduce_aggregation
Derived class intended for segmented reduction usage.
Definition: aggregation.hpp:239
cudf::aggregation::VARIANCE
@ VARIANCE
variance
Definition: aggregation.hpp:97
cudf::aggregation::CUDA
@ CUDA
CUDA UDF based reduction.
Definition: aggregation.hpp:112
cudf::aggregation::do_hash
virtual size_t do_hash() const
Computes the hash value of the aggregation.
Definition: aggregation.hpp:146
cudf::make_lag_aggregation
std::unique_ptr< Base > make_lag_aggregation(size_type offset)
Factory to create a LAG aggregation.
cudf::aggregation::ANY
@ ANY
any reduction
Definition: aggregation.hpp:92
cudf::make_any_aggregation
std::unique_ptr< Base > make_any_aggregation()
cudf::groupby_aggregation
Derived class intended for groupby specific aggregation usage.
Definition: aggregation.hpp:195
cudf::make_sum_of_squares_aggregation
std::unique_ptr< Base > make_sum_of_squares_aggregation()
cudf::aggregation::ARGMIN
@ ARGMIN
Index of min element.
Definition: aggregation.hpp:102
cudf::make_product_aggregation
std::unique_ptr< Base > make_product_aggregation()
cudf::correlation_type
correlation_type
Type of correlation method.
Definition: aggregation.hpp:250
cudf::aggregation::RANK
@ RANK
get rank of current index
Definition: aggregation.hpp:106
cudf::null_order
null_order
Indicates how null values compare against all other values.
Definition: types.hpp:152
cudf::groupby_scan_aggregation
Derived class intended for groupby specific scan usage.
Definition: aggregation.hpp:206
cudf::aggregation::CORRELATION
@ CORRELATION
correlation between two sets of elements
Definition: aggregation.hpp:117
cudf::make_covariance_aggregation
std::unique_ptr< Base > make_covariance_aggregation(size_type min_periods=1, size_type ddof=1)
Factory to create a COVARIANCE aggregation.
cudf::aggregation::MERGE_SETS
@ MERGE_SETS
merge multiple lists values into one list then drop duplicate entries
Definition: aggregation.hpp:114
cudf::aggregation::MEAN
@ MEAN
arithmetic mean reduction
Definition: aggregation.hpp:95
cudf::nan_equality
nan_equality
Enum to consider different elements (of floating point types) holding NaN value as equal or unequal.
Definition: types.hpp:136
cudf::aggregation::MEDIAN
@ MEDIAN
median reduction
Definition: aggregation.hpp:99
cudf::make_nunique_aggregation
std::unique_ptr< Base > make_nunique_aggregation(null_policy null_handling=null_policy::EXCLUDE)
Factory to create a NUNIQUE aggregation.
cudf::nan_equality::ALL_EQUAL
@ ALL_EQUAL
All NaNs compare equal, regardless of sign.
cudf::rank_percentage::NONE
@ NONE
rank
cudf::make_merge_m2_aggregation
std::unique_ptr< Base > make_merge_m2_aggregation()
Factory to create a MERGE_M2 aggregation.
cudf::make_merge_tdigest_aggregation
std::unique_ptr< Base > make_merge_tdigest_aggregation(int max_centroids=1000)
Factory to create a MERGE_TDIGEST aggregation.
cudf::aggregation::QUANTILE
@ QUANTILE
compute specified quantile(s)
Definition: aggregation.hpp:100
cudf::aggregation::MERGE_M2
@ MERGE_M2
merge partial values of M2 aggregation,
Definition: aggregation.hpp:115
cudf::aggregation::get_simple_aggregations
virtual std::vector< std::unique_ptr< aggregation > > get_simple_aggregations(data_type col_type, cudf::detail::simple_aggregations_collector &collector) const =0
Get the simple aggregations that this aggregation requires to compute.
cudf::make_all_aggregation
std::unique_ptr< Base > make_all_aggregation()
cudf::aggregation::MERGE_LISTS
@ MERGE_LISTS
merge multiple lists values into one list
Definition: aggregation.hpp:113
cudf::aggregation::LEAD
@ LEAD
window function, accesses row at specified offset following current row
Definition: aggregation.hpp:109
cudf::make_tdigest_aggregation
std::unique_ptr< Base > make_tdigest_aggregation(int max_centroids=1000)
Factory to create a TDIGEST aggregation.
cudf::make_mean_aggregation
std::unique_ptr< Base > make_mean_aggregation()
cudf::make_rank_aggregation
std::unique_ptr< Base > make_rank_aggregation(rank_method method, order column_order=order::ASCENDING, null_policy null_handling=null_policy::EXCLUDE, null_order null_precedence=null_order::AFTER, rank_percentage percentage=rank_percentage::NONE)
Factory to create a RANK aggregation.
cudf::make_merge_sets_aggregation
std::unique_ptr< Base > make_merge_sets_aggregation(null_equality nulls_equal=null_equality::EQUAL, nan_equality nans_equal=nan_equality::UNEQUAL)
Factory to create a MERGE_SETS aggregation.
cudf::aggregation::LAG
@ LAG
window function, accesses row at specified offset preceding current row
Definition: aggregation.hpp:110
cudf::make_count_aggregation
std::unique_ptr< Base > make_count_aggregation(null_policy null_handling=null_policy::EXCLUDE)
Factory to create a COUNT aggregation.
cudf::data_type
Indicator for the logical data type of an element in a column.
Definition: types.hpp:236
cudf::udf_type
udf_type
Type of code in the user defined function string.
Definition: aggregation.hpp:248
cudf::rank_percentage
rank_percentage
Whether returned rank should be percentage or not and mention the type of percentage normalization.
Definition: aggregation.hpp:66
cudf::aggregation::MERGE_TDIGEST
@ MERGE_TDIGEST
create a tdigest by merging multiple tdigests together
Definition: aggregation.hpp:119
cudf::make_nth_element_aggregation
std::unique_ptr< Base > make_nth_element_aggregation(size_type n, null_policy null_handling=null_policy::INCLUDE)
Factory to create a NTH_ELEMENT aggregation.
cudf::make_collect_list_aggregation
std::unique_ptr< Base > make_collect_list_aggregation(null_policy null_handling=null_policy::INCLUDE)
Factory to create a COLLECT_LIST aggregation.
cudf
cuDF interfaces
Definition: aggregation.hpp:34
cudf::rolling_aggregation
Derived class intended for rolling_window specific aggregation usage.
Definition: aggregation.hpp:182
cudf::aggregation::SUM_OF_SQUARES
@ SUM_OF_SQUARES
sum of squares reduction
Definition: aggregation.hpp:94
cudf::make_std_aggregation
std::unique_ptr< Base > make_std_aggregation(size_type ddof=1)
Factory to create a STD aggregation.
cudf::aggregation::M2
@ M2
sum of squares of differences from the mean
Definition: aggregation.hpp:96
cudf::aggregation::clone
virtual std::unique_ptr< aggregation > clone() const =0
Clones the aggregation object.
cudf::aggregation::COLLECT_SET
@ COLLECT_SET
collect values into a list without duplicate entries
Definition: aggregation.hpp:108
cudf::aggregation::PTX
@ PTX
PTX UDF based reduction.
Definition: aggregation.hpp:111
cudf::null_policy::EXCLUDE
@ EXCLUDE
exclude null elements
cudf::aggregation::TDIGEST
@ TDIGEST
create a tdigest from a set of input values
Definition: aggregation.hpp:118
cudf::make_correlation_aggregation
std::unique_ptr< Base > make_correlation_aggregation(correlation_type type, size_type min_periods=1)
Factory to create a CORRELATION aggregation.
cudf::make_max_aggregation
std::unique_ptr< Base > make_max_aggregation()
cudf::rank_method::FIRST
@ FIRST
stable sort order ranking (no ties)
cudf::make_median_aggregation
std::unique_ptr< Base > make_median_aggregation()
cudf::aggregation::is_equal
virtual bool is_equal(aggregation const &other) const
Compares two aggregation objects for equality.
Definition: aggregation.hpp:139
cudf::null_equality
null_equality
Enum to consider two nulls as equal or unequal.
Definition: types.hpp:144
cudf::aggregation::MAX
@ MAX
max reduction
Definition: aggregation.hpp:89
cudf::aggregation::ROW_NUMBER
@ ROW_NUMBER
get row-number of current index (relative to rolling window)
Definition: aggregation.hpp:105
cudf::make_argmin_aggregation
std::unique_ptr< Base > make_argmin_aggregation()
Factory to create an ARGMIN aggregation.
cudf::aggregation
Abstract base class for specifying the desired aggregation in an aggregation_request.
Definition: aggregation.hpp:80
cudf::make_lead_aggregation
std::unique_ptr< Base > make_lead_aggregation(size_type offset)
Factory to create a LEAD aggregation.
cudf::make_quantile_aggregation
std::unique_ptr< Base > make_quantile_aggregation(std::vector< double > const &quantiles, interpolation interp=interpolation::LINEAR)
Factory to create a QUANTILE aggregation.
cudf::aggregation::COUNT_ALL
@ COUNT_ALL
count number of elements
Definition: aggregation.hpp:91
cudf::aggregation::Kind
Kind
Possible aggregation operations.
Definition: aggregation.hpp:85
cudf::make_udf_aggregation
std::unique_ptr< Base > make_udf_aggregation(udf_type type, std::string const &user_defined_aggregator, data_type output_type)
Factory to create an aggregation base on UDF for PTX or CUDA.
cudf::make_collect_set_aggregation
std::unique_ptr< Base > make_collect_set_aggregation(null_policy null_handling=null_policy::INCLUDE, null_equality nulls_equal=null_equality::EQUAL, nan_equality nans_equal=nan_equality::UNEQUAL)
Factory to create a COLLECT_SET aggregation.
cudf::aggregation::NUNIQUE
@ NUNIQUE
count number of unique elements
Definition: aggregation.hpp:103
cudf::aggregation::COVARIANCE
@ COVARIANCE
covariance between two sets of elements
Definition: aggregation.hpp:116
cudf::aggregation::ALL
@ ALL
all reduction
Definition: aggregation.hpp:93
cudf::order
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:111