aggregation.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/types.hpp>
9 #include <cudf/utilities/export.hpp>
10 
11 #include <functional>
12 #include <memory>
13 #include <vector>
14 
24 namespace CUDF_EXPORT cudf {
31 // forward declaration
32 namespace detail {
33 class simple_aggregations_collector;
34 class aggregation_finalizer;
35 } // namespace detail
36 
43 enum class rank_method : int32_t {
44  FIRST,
45  AVERAGE,
46  MIN,
47  MAX,
48  DENSE
49 };
50 
56 enum class rank_percentage : int32_t {
57  NONE,
60 };
61 
65 enum class bitwise_op : int32_t {
66  AND,
67  OR,
68  XOR
69 };
70 
79 class aggregation {
80  public:
84  enum Kind {
85  SUM,
88  MIN,
89  MAX,
92  ANY,
93  ALL,
95  MEAN,
96  M2,
98  STD,
111  LAG,
112  PTX,
124  BITWISE_AGG
125  };
126 
127  aggregation() = delete;
128 
136  virtual ~aggregation() = default;
137 
144  [[nodiscard]] virtual bool is_equal(aggregation const& other) const { return kind == other.kind; }
145 
151  [[nodiscard]] virtual size_t do_hash() const { return std::hash<int>{}(kind); }
152 
158  [[nodiscard]] virtual std::unique_ptr<aggregation> clone() const = 0;
159 
160  // override functions for compound aggregations
168  virtual std::vector<std::unique_ptr<aggregation>> get_simple_aggregations(
169  data_type col_type, cudf::detail::simple_aggregations_collector& collector) const = 0;
170 
177  virtual void finalize(cudf::detail::aggregation_finalizer& finalizer) const = 0;
178 };
179 
187 class rolling_aggregation : public virtual aggregation {
188  public:
189  ~rolling_aggregation() override = default;
190 
191  protected:
194  using aggregation::aggregation;
195 };
196 
200 class groupby_aggregation : public virtual aggregation {
201  public:
202  ~groupby_aggregation() override = default;
203 
204  protected:
206 };
207 
211 class groupby_scan_aggregation : public virtual aggregation {
212  public:
213  ~groupby_scan_aggregation() override = default;
214 
215  protected:
217 };
218 
222 class reduce_aggregation : public virtual aggregation {
223  public:
224  ~reduce_aggregation() override = default;
225 
226  protected:
227  reduce_aggregation() {}
228 };
229 
233 class scan_aggregation : public virtual aggregation {
234  public:
235  ~scan_aggregation() override = default;
236 
237  protected:
238  scan_aggregation() {}
239 };
240 
245  public:
246  ~segmented_reduce_aggregation() override = default;
247 
248  protected:
250 };
251 
253 enum class udf_type : bool { CUDA, PTX };
255 enum class correlation_type : int32_t { PEARSON, KENDALL, SPEARMAN };
257 enum class ewm_history : int32_t { INFINITE, FINITE };
258 
261 template <typename Base = aggregation>
262 std::unique_ptr<Base> make_sum_aggregation();
263 
266 template <typename Base = aggregation>
267 std::unique_ptr<Base> make_sum_with_overflow_aggregation();
268 
271 template <typename Base = aggregation>
272 std::unique_ptr<Base> make_product_aggregation();
273 
276 template <typename Base = aggregation>
277 std::unique_ptr<Base> make_min_aggregation();
278 
281 template <typename Base = aggregation>
282 std::unique_ptr<Base> make_max_aggregation();
283 
290 template <typename Base = aggregation>
291 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
292 
295 template <typename Base = aggregation>
296 std::unique_ptr<Base> make_any_aggregation();
297 
300 template <typename Base = aggregation>
301 std::unique_ptr<Base> make_all_aggregation();
302 
305 template <typename Base = aggregation>
306 std::unique_ptr<Base> make_histogram_aggregation();
307 
310 template <typename Base = aggregation>
311 std::unique_ptr<Base> make_sum_of_squares_aggregation();
312 
315 template <typename Base = aggregation>
316 std::unique_ptr<Base> make_mean_aggregation();
317 
330 template <typename Base = aggregation>
331 std::unique_ptr<Base> make_m2_aggregation();
332 
342 template <typename Base = aggregation>
343 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
344 
354 template <typename Base = aggregation>
355 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
356 
359 template <typename Base = aggregation>
360 std::unique_ptr<Base> make_median_aggregation();
361 
369 template <typename Base = aggregation>
370 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
371  interpolation interp = interpolation::LINEAR);
372 
379 template <typename Base = aggregation>
380 std::unique_ptr<Base> make_argmax_aggregation();
381 
388 template <typename Base = aggregation>
389 std::unique_ptr<Base> make_argmin_aggregation();
390 
398 template <typename Base = aggregation>
399 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
400 
415 template <typename Base = aggregation>
416 std::unique_ptr<Base> make_nth_element_aggregation(
417  size_type n, null_policy null_handling = null_policy::INCLUDE);
418 
421 template <typename Base = aggregation>
422 std::unique_ptr<Base> make_row_number_aggregation();
423 
457 template <typename Base = aggregation>
458 std::unique_ptr<Base> make_ewma_aggregation(double const center_of_mass, ewm_history history);
459 
532 template <typename Base = aggregation>
533 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
534  order column_order = order::ASCENDING,
535  null_policy null_handling = null_policy::EXCLUDE,
536  null_order null_precedence = null_order::AFTER,
537  rank_percentage percentage = rank_percentage::NONE);
538 
550 template <typename Base = aggregation>
551 std::unique_ptr<Base> make_collect_list_aggregation(
552  null_policy null_handling = null_policy::INCLUDE);
553 
570 template <typename Base = aggregation>
571 std::unique_ptr<Base> make_collect_set_aggregation(
572  null_policy null_handling = null_policy::INCLUDE,
573  null_equality nulls_equal = null_equality::EQUAL,
574  nan_equality nans_equal = nan_equality::ALL_EQUAL);
575 
582 template <typename Base = aggregation>
583 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
584 
591 template <typename Base = aggregation>
592 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
593 
603 template <typename Base = aggregation>
604 std::unique_ptr<Base> make_udf_aggregation(udf_type type,
605  std::string const& user_defined_aggregator,
606  data_type output_type);
607 
608 // Forward declaration of `host_udf_base` for the factory function of `HOST_UDF` aggregation.
609 class host_udf_base;
610 
617 template <typename Base = aggregation>
618 std::unique_ptr<Base> make_host_udf_aggregation(std::unique_ptr<host_udf_base> host_udf);
619 
631 template <typename Base = aggregation>
632 std::unique_ptr<Base> make_merge_lists_aggregation();
633 
656 template <typename Base = aggregation>
657 std::unique_ptr<Base> make_merge_sets_aggregation(
658  null_equality nulls_equal = null_equality::EQUAL,
659  nan_equality nans_equal = nan_equality::ALL_EQUAL);
660 
675 template <typename Base = aggregation>
676 std::unique_ptr<Base> make_merge_m2_aggregation();
677 
686 template <typename Base = aggregation>
687 std::unique_ptr<Base> make_merge_histogram_aggregation();
688 
699 template <typename Base = aggregation>
700 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
701 
712 template <typename Base = aggregation>
714  size_type min_periods = 1);
715 
750 template <typename Base>
751 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
752 
788 template <typename Base>
789 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
790 
797 template <typename Base>
798 std::unique_ptr<Base> make_bitwise_aggregation(bitwise_op op);
799 
808  // end of group
810 } // namespace CUDF_EXPORT cudf
Abstract base class for specifying the desired aggregation in an aggregation_request.
Definition: aggregation.hpp:79
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:84
@ PRODUCT
product reduction
Definition: aggregation.hpp:87
@ ALL
all reduction
Definition: aggregation.hpp:93
@ M2
sum of squares of differences from the mean
Definition: aggregation.hpp:96
@ TDIGEST
create a tdigest from a set of input values
@ MEAN
arithmetic mean reduction
Definition: aggregation.hpp:95
@ MERGE_M2
merge partial values of M2 aggregation,
@ PTX
PTX based UDF aggregation.
@ MERGE_SETS
merge multiple lists values into one list then drop duplicate entries
@ MEDIAN
median reduction
Definition: aggregation.hpp:99
@ NUNIQUE
count number of unique elements
@ MERGE_HISTOGRAM
merge partial values of HISTOGRAM aggregation
@ ARGMIN
Index of min element.
@ VARIANCE
variance
Definition: aggregation.hpp:97
@ CORRELATION
correlation between two sets of elements
@ STD
standard deviation
Definition: aggregation.hpp:98
@ QUANTILE
compute specified quantile(s)
@ COVARIANCE
covariance between two sets of elements
@ MAX
max reduction
Definition: aggregation.hpp:89
@ MIN
min reduction
Definition: aggregation.hpp:88
@ COLLECT_SET
collect values into a list without duplicate entries
@ LAG
window function, accesses row at specified offset preceding current row
@ CUDA
CUDA based UDF aggregation.
@ LEAD
window function, accesses row at specified offset following current row
@ SUM_OF_SQUARES
sum of squares reduction
Definition: aggregation.hpp:94
@ SUM
sum reduction
Definition: aggregation.hpp:85
@ 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
@ HOST_UDF
host based UDF aggregation
@ ANY
any reduction
Definition: aggregation.hpp:92
@ COLLECT_LIST
collect values into a list
@ COUNT_VALID
count number of valid elements
Definition: aggregation.hpp:90
@ ROW_NUMBER
get row-number of current index (relative to rolling window)
@ SUM_WITH_OVERFLOW
sum reduction with overflow detection
Definition: aggregation.hpp:86
@ 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:91
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:238
Derived class intended for groupby specific aggregation usage.
Derived class intended for groupby specific scan usage.
The fundamental interface for host-based UDF implementation.
Definition: host_udf.hpp:39
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_bitwise_aggregation(bitwise_op op)
Factory to create a BITWISE_AGG aggregation.
std::unique_ptr< Base > make_median_aggregation()
correlation_type
Type of correlation method.
std::unique_ptr< Base > make_host_udf_aggregation(std::unique_ptr< host_udf_base > host_udf)
Factory to create a HOST_UDF aggregation.
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:56
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_sum_with_overflow_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.
bitwise_op
Bitwise operations to use for BITWISE_AGG aggregations on numeric columns.
Definition: aggregation.hpp:65
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()
bool is_valid_aggregation(data_type source, aggregation::Kind kind)
Indicate if an aggregation is supported for a source datatype.
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
@ OR
bitwise OR operation
@ AND
bitwise AND operation
@ XOR
bitwise XOR operation
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::cuda_stream_view stream=cudf::get_default_stream(), 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:43
@ 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:148
null_equality
Enum to consider two nulls as equal or unequal.
Definition: types.hpp:140
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:84
null_policy
Enum to specify whether to include nulls or exclude nulls.
Definition: types.hpp:115
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:107
interpolation
Interpolation method to use when the desired quantile lies between two data points i and j.
Definition: types.hpp:181
nan_equality
Enum to consider different elements (of floating point types) holding NaN value as equal or unequal.
Definition: types.hpp:132
cuDF interfaces
Definition: host_udf.hpp:26
Type declarations for libcudf.