aggregation.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, 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 #include <cudf/utilities/export.hpp>
21 
22 #include <functional>
23 #include <memory>
24 #include <vector>
25 
35 namespace CUDF_EXPORT cudf {
42 // forward declaration
43 namespace detail {
44 class simple_aggregations_collector;
45 class aggregation_finalizer;
46 } // namespace detail
47 
54 enum class rank_method : int32_t {
55  FIRST,
56  AVERAGE,
57  MIN,
58  MAX,
59  DENSE
60 };
61 
67 enum class rank_percentage : int32_t {
68  NONE,
71 };
72 
81 class aggregation {
82  public:
86  enum Kind {
87  SUM,
89  MIN,
90  MAX,
93  ANY,
94  ALL,
96  MEAN,
97  M2,
99  STD,
112  LAG,
113  PTX,
123  MERGE_HISTOGRAM
124  };
125 
126  aggregation() = delete;
127 
135  virtual ~aggregation() = default;
136 
143  [[nodiscard]] virtual bool is_equal(aggregation const& other) const { return kind == other.kind; }
144 
150  [[nodiscard]] virtual size_t do_hash() const { return std::hash<int>{}(kind); }
151 
157  [[nodiscard]] virtual std::unique_ptr<aggregation> clone() const = 0;
158 
159  // override functions for compound aggregations
167  virtual std::vector<std::unique_ptr<aggregation>> get_simple_aggregations(
168  data_type col_type, cudf::detail::simple_aggregations_collector& collector) const = 0;
169 
176  virtual void finalize(cudf::detail::aggregation_finalizer& finalizer) const = 0;
177 };
178 
186 class rolling_aggregation : public virtual aggregation {
187  public:
188  ~rolling_aggregation() override = default;
189 
190  protected:
193  using aggregation::aggregation;
194 };
195 
199 class groupby_aggregation : public virtual aggregation {
200  public:
201  ~groupby_aggregation() override = default;
202 
203  protected:
205 };
206 
210 class groupby_scan_aggregation : public virtual aggregation {
211  public:
212  ~groupby_scan_aggregation() override = default;
213 
214  protected:
216 };
217 
221 class reduce_aggregation : public virtual aggregation {
222  public:
223  ~reduce_aggregation() override = default;
224 
225  protected:
226  reduce_aggregation() {}
227 };
228 
232 class scan_aggregation : public virtual aggregation {
233  public:
234  ~scan_aggregation() override = default;
235 
236  protected:
237  scan_aggregation() {}
238 };
239 
244  public:
245  ~segmented_reduce_aggregation() override = default;
246 
247  protected:
249 };
250 
252 enum class udf_type : bool { CUDA, PTX };
254 enum class correlation_type : int32_t { PEARSON, KENDALL, SPEARMAN };
256 enum class ewm_history : int32_t { INFINITE, FINITE };
257 
260 template <typename Base = aggregation>
261 std::unique_ptr<Base> make_sum_aggregation();
262 
265 template <typename Base = aggregation>
266 std::unique_ptr<Base> make_product_aggregation();
267 
270 template <typename Base = aggregation>
271 std::unique_ptr<Base> make_min_aggregation();
272 
275 template <typename Base = aggregation>
276 std::unique_ptr<Base> make_max_aggregation();
277 
284 template <typename Base = aggregation>
285 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
286 
289 template <typename Base = aggregation>
290 std::unique_ptr<Base> make_any_aggregation();
291 
294 template <typename Base = aggregation>
295 std::unique_ptr<Base> make_all_aggregation();
296 
299 template <typename Base = aggregation>
300 std::unique_ptr<Base> make_histogram_aggregation();
301 
304 template <typename Base = aggregation>
305 std::unique_ptr<Base> make_sum_of_squares_aggregation();
306 
309 template <typename Base = aggregation>
310 std::unique_ptr<Base> make_mean_aggregation();
311 
324 template <typename Base = aggregation>
325 std::unique_ptr<Base> make_m2_aggregation();
326 
336 template <typename Base = aggregation>
337 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
338 
348 template <typename Base = aggregation>
349 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
350 
353 template <typename Base = aggregation>
354 std::unique_ptr<Base> make_median_aggregation();
355 
363 template <typename Base = aggregation>
364 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
365  interpolation interp = interpolation::LINEAR);
366 
373 template <typename Base = aggregation>
374 std::unique_ptr<Base> make_argmax_aggregation();
375 
382 template <typename Base = aggregation>
383 std::unique_ptr<Base> make_argmin_aggregation();
384 
392 template <typename Base = aggregation>
393 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
394 
409 template <typename Base = aggregation>
410 std::unique_ptr<Base> make_nth_element_aggregation(
411  size_type n, null_policy null_handling = null_policy::INCLUDE);
412 
415 template <typename Base = aggregation>
416 std::unique_ptr<Base> make_row_number_aggregation();
417 
451 template <typename Base = aggregation>
452 std::unique_ptr<Base> make_ewma_aggregation(double const center_of_mass, ewm_history history);
453 
526 template <typename Base = aggregation>
527 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
528  order column_order = order::ASCENDING,
529  null_policy null_handling = null_policy::EXCLUDE,
530  null_order null_precedence = null_order::AFTER,
531  rank_percentage percentage = rank_percentage::NONE);
532 
544 template <typename Base = aggregation>
545 std::unique_ptr<Base> make_collect_list_aggregation(
546  null_policy null_handling = null_policy::INCLUDE);
547 
564 template <typename Base = aggregation>
565 std::unique_ptr<Base> make_collect_set_aggregation(
566  null_policy null_handling = null_policy::INCLUDE,
567  null_equality nulls_equal = null_equality::EQUAL,
568  nan_equality nans_equal = nan_equality::ALL_EQUAL);
569 
576 template <typename Base = aggregation>
577 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
578 
585 template <typename Base = aggregation>
586 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
587 
597 template <typename Base = aggregation>
598 std::unique_ptr<Base> make_udf_aggregation(udf_type type,
599  std::string const& user_defined_aggregator,
600  data_type output_type);
601 
613 template <typename Base = aggregation>
614 std::unique_ptr<Base> make_merge_lists_aggregation();
615 
638 template <typename Base = aggregation>
639 std::unique_ptr<Base> make_merge_sets_aggregation(
640  null_equality nulls_equal = null_equality::EQUAL,
641  nan_equality nans_equal = nan_equality::ALL_EQUAL);
642 
657 template <typename Base = aggregation>
658 std::unique_ptr<Base> make_merge_m2_aggregation();
659 
668 template <typename Base = aggregation>
669 std::unique_ptr<Base> make_merge_histogram_aggregation();
670 
681 template <typename Base = aggregation>
682 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
683 
694 template <typename Base = aggregation>
696  size_type min_periods = 1);
697 
732 template <typename Base>
733 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
734 
770 template <typename Base>
771 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
772  // end of group
774 } // namespace CUDF_EXPORT cudf
Abstract base class for specifying the desired aggregation in an aggregation_request.
Definition: aggregation.hpp:81
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.
virtual void finalize(cudf::detail::aggregation_finalizer &finalizer) const =0
Compute the aggregation after pre-requisite simple aggregations have been computed.
Kind
Possible aggregation operations.
Definition: aggregation.hpp:86
@ PRODUCT
product reduction
Definition: aggregation.hpp:88
@ ALL
all reduction
Definition: aggregation.hpp:94
@ M2
sum of squares of differences from the mean
Definition: aggregation.hpp:97
@ TDIGEST
create a tdigest from a set of input values
@ MEAN
arithmetic mean reduction
Definition: aggregation.hpp:96
@ MERGE_M2
merge partial values of M2 aggregation,
@ PTX
PTX UDF based reduction.
@ MERGE_SETS
merge multiple lists values into one list then drop duplicate entries
@ MEDIAN
median reduction
@ NUNIQUE
count number of unique elements
@ ARGMIN
Index of min element.
@ VARIANCE
variance
Definition: aggregation.hpp:98
@ CORRELATION
correlation between two sets of elements
@ STD
standard deviation
Definition: aggregation.hpp:99
@ QUANTILE
compute specified quantile(s)
@ COVARIANCE
covariance between two sets of elements
@ MAX
max reduction
Definition: aggregation.hpp:90
@ MIN
min reduction
Definition: aggregation.hpp:89
@ COLLECT_SET
collect values into a list without duplicate entries
@ LAG
window function, accesses row at specified offset preceding current row
@ CUDA
CUDA UDF based reduction.
@ LEAD
window function, accesses row at specified offset following current row
@ SUM_OF_SQUARES
sum of squares reduction
Definition: aggregation.hpp:95
@ SUM
sum reduction
Definition: aggregation.hpp:87
@ NTH_ELEMENT
get the nth element
@ EWMA
get exponential weighted moving average at current index
@ MERGE_LISTS
merge multiple lists values into one list
@ MERGE_TDIGEST
create a tdigest by merging multiple tdigests together
@ ANY
any reduction
Definition: aggregation.hpp:93
@ COLLECT_LIST
collect values into a list
@ COUNT_VALID
count number of valid elements
Definition: aggregation.hpp:91
@ ROW_NUMBER
get row-number of current index (relative to rolling window)
@ ARGMAX
Index of max element.
@ HISTOGRAM
compute frequency of each element
@ RANK
get rank of current index
@ COUNT_ALL
count number of elements
Definition: aggregation.hpp:92
virtual bool is_equal(aggregation const &other) const
Compares two aggregation objects for equality.
aggregation(aggregation::Kind a)
Construct a new aggregation object.
virtual size_t do_hash() const
Computes the hash value of the aggregation.
virtual std::unique_ptr< aggregation > clone() const =0
Clones the aggregation object.
Kind kind
The aggregation to perform.
Indicator for the logical data type of an element in a column.
Definition: types.hpp:243
Derived class intended for groupby specific aggregation usage.
Derived class intended for groupby specific scan usage.
Derived class intended for reduction usage.
Derived class intended for rolling_window specific aggregation usage.
Derived class intended for scan usage.
Derived class intended for segmented reduction usage.
std::unique_ptr< Base > make_median_aggregation()
correlation_type
Type of correlation method.
std::unique_ptr< Base > make_lag_aggregation(size_type offset)
Factory to create a LAG aggregation.
std::unique_ptr< Base > make_tdigest_aggregation(int max_centroids=1000)
Factory to create a TDIGEST aggregation.
rank_percentage
Whether returned rank should be percentage or not and mention the type of percentage normalization.
Definition: aggregation.hpp:67
std::unique_ptr< Base > make_covariance_aggregation(size_type min_periods=1, size_type ddof=1)
Factory to create a COVARIANCE aggregation.
std::unique_ptr< Base > make_std_aggregation(size_type ddof=1)
Factory to create a STD aggregation.
std::unique_ptr< Base > make_correlation_aggregation(correlation_type type, size_type min_periods=1)
Factory to create a CORRELATION aggregation.
std::unique_ptr< Base > make_merge_sets_aggregation(null_equality nulls_equal=null_equality::EQUAL, nan_equality nans_equal=nan_equality::ALL_EQUAL)
Factory to create a MERGE_SETS aggregation.
std::unique_ptr< Base > make_variance_aggregation(size_type ddof=1)
Factory to create a VARIANCE aggregation.
std::unique_ptr< Base > make_lead_aggregation(size_type offset)
Factory to create a LEAD aggregation.
std::unique_ptr< Base > make_any_aggregation()
std::unique_ptr< Base > make_nunique_aggregation(null_policy null_handling=null_policy::EXCLUDE)
Factory to create a NUNIQUE aggregation.
std::unique_ptr< Base > make_max_aggregation()
std::unique_ptr< Base > make_histogram_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.
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.
std::unique_ptr< Base > make_row_number_aggregation()
std::unique_ptr< Base > make_merge_histogram_aggregation()
Factory to create a MERGE_HISTOGRAM aggregation.
std::unique_ptr< Base > make_count_aggregation(null_policy null_handling=null_policy::EXCLUDE)
Factory to create a COUNT aggregation.
ewm_history
Type of treatment of EWM input values' first value.
std::unique_ptr< Base > make_collect_list_aggregation(null_policy null_handling=null_policy::INCLUDE)
Factory to create a COLLECT_LIST aggregation.
std::unique_ptr< Base > make_argmax_aggregation()
Factory to create an ARGMAX aggregation.
std::unique_ptr< Base > make_sum_aggregation()
std::unique_ptr< Base > make_all_aggregation()
std::unique_ptr< Base > make_m2_aggregation()
Factory to create a M2 aggregation.
std::unique_ptr< Base > make_merge_m2_aggregation()
Factory to create a MERGE_M2 aggregation.
std::unique_ptr< Base > make_sum_of_squares_aggregation()
std::unique_ptr< Base > make_min_aggregation()
std::unique_ptr< Base > make_product_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.
udf_type
Type of code in the user defined function string.
std::unique_ptr< Base > make_ewma_aggregation(double const center_of_mass, ewm_history history)
Factory to create an EWMA aggregation.
std::unique_ptr< Base > make_merge_lists_aggregation()
Factory to create a MERGE_LISTS 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::ALL_EQUAL)
Factory to create a COLLECT_SET aggregation.
std::unique_ptr< Base > make_argmin_aggregation()
Factory to create an ARGMIN aggregation.
std::unique_ptr< Base > make_quantile_aggregation(std::vector< double > const &quantiles, interpolation interp=interpolation::LINEAR)
Factory to create a QUANTILE aggregation.
std::unique_ptr< Base > make_mean_aggregation()
std::unique_ptr< Base > make_merge_tdigest_aggregation(int max_centroids=1000)
Factory to create a MERGE_TDIGEST aggregation.
@ ONE_NORMALIZED
(rank - 1) / (count - 1)
@ ZERO_NORMALIZED
rank / count
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::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Returns the rows of the input corresponding to the requested quantiles.
rank_method
Tie-breaker method to use for ranking the column.
Definition: aggregation.hpp:54
@ DENSE
rank always increases by 1 between groups
@ AVERAGE
mean of first in the group
@ MAX
max of first in the group
@ FIRST
stable sort order ranking (no ties)
@ MIN
min of first in the group
null_order
Indicates how null values compare against all other values.
Definition: types.hpp:159
null_equality
Enum to consider two nulls as equal or unequal.
Definition: types.hpp:151
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:95
null_policy
Enum to specify whether to include nulls or exclude nulls.
Definition: types.hpp:126
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:118
interpolation
Interpolation method to use when the desired quantile lies between two data points i and j.
Definition: types.hpp:192
nan_equality
Enum to consider different elements (of floating point types) holding NaN value as equal or unequal.
Definition: types.hpp:143
cuDF interfaces
Definition: aggregation.hpp:35
Type declarations for libcudf.