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,
125  TOP_K
126  };
127 
128  aggregation() = delete;
129 
137  virtual ~aggregation() = default;
138 
145  [[nodiscard]] virtual bool is_equal(aggregation const& other) const { return kind == other.kind; }
146 
152  [[nodiscard]] virtual size_t do_hash() const { return std::hash<int>{}(kind); }
153 
159  [[nodiscard]] virtual std::unique_ptr<aggregation> clone() const = 0;
160 
161  // override functions for compound aggregations
169  virtual std::vector<std::unique_ptr<aggregation>> get_simple_aggregations(
170  data_type col_type, cudf::detail::simple_aggregations_collector& collector) const = 0;
171 
178  virtual void finalize(cudf::detail::aggregation_finalizer& finalizer) const = 0;
179 };
180 
188 class rolling_aggregation : public virtual aggregation {
189  public:
190  ~rolling_aggregation() override = default;
191 
192  protected:
195  using aggregation::aggregation;
196 };
197 
201 class groupby_aggregation : public virtual aggregation {
202  public:
203  ~groupby_aggregation() override = default;
204 
205  protected:
207 };
208 
212 class groupby_scan_aggregation : public virtual aggregation {
213  public:
214  ~groupby_scan_aggregation() override = default;
215 
216  protected:
218 };
219 
223 class reduce_aggregation : public virtual aggregation {
224  public:
225  ~reduce_aggregation() override = default;
226 
227  protected:
228  reduce_aggregation() {}
229 };
230 
234 class scan_aggregation : public virtual aggregation {
235  public:
236  ~scan_aggregation() override = default;
237 
238  protected:
239  scan_aggregation() {}
240 };
241 
246  public:
247  ~segmented_reduce_aggregation() override = default;
248 
249  protected:
251 };
252 
254 enum class udf_type : bool { CUDA, PTX };
256 enum class correlation_type : int32_t { PEARSON, KENDALL, SPEARMAN };
258 enum class ewm_history : int32_t { INFINITE, FINITE };
259 
262 template <typename Base = aggregation>
263 std::unique_ptr<Base> make_sum_aggregation();
264 
267 template <typename Base = aggregation>
268 std::unique_ptr<Base> make_sum_with_overflow_aggregation();
269 
272 template <typename Base = aggregation>
273 std::unique_ptr<Base> make_product_aggregation();
274 
277 template <typename Base = aggregation>
278 std::unique_ptr<Base> make_min_aggregation();
279 
282 template <typename Base = aggregation>
283 std::unique_ptr<Base> make_max_aggregation();
284 
291 template <typename Base = aggregation>
292 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
293 
296 template <typename Base = aggregation>
297 std::unique_ptr<Base> make_any_aggregation();
298 
301 template <typename Base = aggregation>
302 std::unique_ptr<Base> make_all_aggregation();
303 
306 template <typename Base = aggregation>
307 std::unique_ptr<Base> make_histogram_aggregation();
308 
311 template <typename Base = aggregation>
312 std::unique_ptr<Base> make_sum_of_squares_aggregation();
313 
316 template <typename Base = aggregation>
317 std::unique_ptr<Base> make_mean_aggregation();
318 
331 template <typename Base = aggregation>
332 std::unique_ptr<Base> make_m2_aggregation();
333 
343 template <typename Base = aggregation>
344 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
345 
355 template <typename Base = aggregation>
356 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
357 
360 template <typename Base = aggregation>
361 std::unique_ptr<Base> make_median_aggregation();
362 
370 template <typename Base = aggregation>
371 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
372  interpolation interp = interpolation::LINEAR);
373 
380 template <typename Base = aggregation>
381 std::unique_ptr<Base> make_argmax_aggregation();
382 
389 template <typename Base = aggregation>
390 std::unique_ptr<Base> make_argmin_aggregation();
391 
399 template <typename Base = aggregation>
400 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
401 
416 template <typename Base = aggregation>
417 std::unique_ptr<Base> make_nth_element_aggregation(
418  size_type n, null_policy null_handling = null_policy::INCLUDE);
419 
422 template <typename Base = aggregation>
423 std::unique_ptr<Base> make_row_number_aggregation();
424 
458 template <typename Base = aggregation>
459 std::unique_ptr<Base> make_ewma_aggregation(double const center_of_mass, ewm_history history);
460 
533 template <typename Base = aggregation>
534 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
535  order column_order = order::ASCENDING,
536  null_policy null_handling = null_policy::EXCLUDE,
537  null_order null_precedence = null_order::AFTER,
538  rank_percentage percentage = rank_percentage::NONE);
539 
551 template <typename Base = aggregation>
552 std::unique_ptr<Base> make_collect_list_aggregation(
553  null_policy null_handling = null_policy::INCLUDE);
554 
571 template <typename Base = aggregation>
572 std::unique_ptr<Base> make_collect_set_aggregation(
573  null_policy null_handling = null_policy::INCLUDE,
574  null_equality nulls_equal = null_equality::EQUAL,
575  nan_equality nans_equal = nan_equality::ALL_EQUAL);
576 
583 template <typename Base = aggregation>
584 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
585 
592 template <typename Base = aggregation>
593 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
594 
604 template <typename Base = aggregation>
605 std::unique_ptr<Base> make_udf_aggregation(udf_type type,
606  std::string const& user_defined_aggregator,
607  data_type output_type);
608 
609 // Forward declaration of `host_udf_base` for the factory function of `HOST_UDF` aggregation.
610 class host_udf_base;
611 
618 template <typename Base = aggregation>
619 std::unique_ptr<Base> make_host_udf_aggregation(std::unique_ptr<host_udf_base> host_udf);
620 
632 template <typename Base = aggregation>
633 std::unique_ptr<Base> make_merge_lists_aggregation();
634 
657 template <typename Base = aggregation>
658 std::unique_ptr<Base> make_merge_sets_aggregation(
659  null_equality nulls_equal = null_equality::EQUAL,
660  nan_equality nans_equal = nan_equality::ALL_EQUAL);
661 
676 template <typename Base = aggregation>
677 std::unique_ptr<Base> make_merge_m2_aggregation();
678 
687 template <typename Base = aggregation>
688 std::unique_ptr<Base> make_merge_histogram_aggregation();
689 
700 template <typename Base = aggregation>
701 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
702 
713 template <typename Base = aggregation>
715  size_type min_periods = 1);
716 
751 template <typename Base>
752 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
753 
789 template <typename Base>
790 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
791 
798 template <typename Base>
799 std::unique_ptr<Base> make_bitwise_aggregation(bitwise_op op);
800 
811 template <typename Base = aggregation>
812 std::unique_ptr<Base> make_top_k_aggregation(size_type k, order topk_order = order::DESCENDING);
813 
822  // end of group
824 } // 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,
@ BITWISE_AGG
bitwise aggregation on numeric columns
@ 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:269
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_top_k_aggregation(size_type k, order topk_order=order::DESCENDING)
Factory to create a TOP_K 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.