aggregation.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/types.hpp>
10 #include <cudf/utilities/export.hpp>
11 
12 #include <functional>
13 #include <memory>
14 #include <vector>
15 
25 namespace CUDF_EXPORT cudf {
37 enum class rank_method : int32_t {
38  FIRST,
39  AVERAGE,
40  MIN,
41  MAX,
42  DENSE
43 };
44 
50 enum class rank_percentage : int32_t {
51  NONE,
54 };
55 
59 enum class bitwise_op : int32_t {
60  AND,
61  OR,
62  XOR
63 };
64 
73 class aggregation {
74  public:
78  enum Kind : int32_t {
79  SUM = 0,
82  MIN,
83  MAX,
86  ANY,
87  ALL,
89  MEAN,
90  M2,
92  STD,
105  LAG,
118  INVALID
119  };
120 
127  aggregation() : kind{Kind::INVALID}
128  {
129  CUDF_FAIL("No-parameter aggregation constructor should never be called");
130  }
131 
137  aggregation(Kind kind_) : kind{kind_} { CUDF_EXPECTS(is_valid(), "Invalid aggregation kind"); }
139  virtual ~aggregation() = default;
140 
147  [[nodiscard]] bool is_valid() const { return kind >= 0 && kind < Kind::INVALID; }
148 
155  [[nodiscard]] virtual bool is_equal(aggregation const& other) const { return kind == other.kind; }
156 
162  [[nodiscard]] virtual size_t do_hash() const { return std::hash<int>{}(kind); }
163 
169  [[nodiscard]] virtual std::unique_ptr<aggregation> clone() const = 0;
170 };
171 
175 class rolling_aggregation : public virtual aggregation {};
176 
180 class groupby_aggregation : public virtual aggregation {};
181 
185 class groupby_scan_aggregation : public virtual aggregation {};
186 
190 class reduce_aggregation : public virtual aggregation {};
191 
195 class scan_aggregation : public virtual aggregation {};
196 
200 class segmented_reduce_aggregation : public virtual aggregation {};
201 
203 enum class correlation_type : int32_t { PEARSON, KENDALL, SPEARMAN };
205 enum class ewm_history : int32_t { INFINITE, FINITE };
206 
209 template <typename Base = aggregation>
210 std::unique_ptr<Base> make_sum_aggregation();
211 
214 template <typename Base = aggregation>
215 std::unique_ptr<Base> make_sum_overflow_aggregation();
216 
219 template <typename Base = aggregation>
220 std::unique_ptr<Base> make_product_aggregation();
221 
224 template <typename Base = aggregation>
225 std::unique_ptr<Base> make_min_aggregation();
226 
229 template <typename Base = aggregation>
230 std::unique_ptr<Base> make_max_aggregation();
231 
238 template <typename Base = aggregation>
239 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
240 
243 template <typename Base = aggregation>
244 std::unique_ptr<Base> make_any_aggregation();
245 
248 template <typename Base = aggregation>
249 std::unique_ptr<Base> make_all_aggregation();
250 
253 template <typename Base = aggregation>
254 std::unique_ptr<Base> make_histogram_aggregation();
255 
258 template <typename Base = aggregation>
259 std::unique_ptr<Base> make_sum_of_squares_aggregation();
260 
263 template <typename Base = aggregation>
264 std::unique_ptr<Base> make_mean_aggregation();
265 
278 template <typename Base = aggregation>
279 std::unique_ptr<Base> make_m2_aggregation();
280 
290 template <typename Base = aggregation>
291 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
292 
302 template <typename Base = aggregation>
303 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
304 
307 template <typename Base = aggregation>
308 std::unique_ptr<Base> make_median_aggregation();
309 
317 template <typename Base = aggregation>
318 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
319  interpolation interp = interpolation::LINEAR);
320 
327 template <typename Base = aggregation>
328 std::unique_ptr<Base> make_argmax_aggregation();
329 
336 template <typename Base = aggregation>
337 std::unique_ptr<Base> make_argmin_aggregation();
338 
346 template <typename Base = aggregation>
347 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
348 
363 template <typename Base = aggregation>
364 std::unique_ptr<Base> make_nth_element_aggregation(
365  size_type n, null_policy null_handling = null_policy::INCLUDE);
366 
369 template <typename Base = aggregation>
370 std::unique_ptr<Base> make_row_number_aggregation();
371 
405 template <typename Base = aggregation>
406 std::unique_ptr<Base> make_ewma_aggregation(double const center_of_mass, ewm_history history);
407 
480 template <typename Base = aggregation>
481 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
482  order column_order = order::ASCENDING,
483  null_policy null_handling = null_policy::EXCLUDE,
484  null_order null_precedence = null_order::AFTER,
485  rank_percentage percentage = rank_percentage::NONE);
486 
498 template <typename Base = aggregation>
499 std::unique_ptr<Base> make_collect_list_aggregation(
500  null_policy null_handling = null_policy::INCLUDE);
501 
518 template <typename Base = aggregation>
519 std::unique_ptr<Base> make_collect_set_aggregation(
520  null_policy null_handling = null_policy::INCLUDE,
521  null_equality nulls_equal = null_equality::EQUAL,
522  nan_equality nans_equal = nan_equality::ALL_EQUAL);
523 
530 template <typename Base = aggregation>
531 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
532 
539 template <typename Base = aggregation>
540 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
541 
542 // Forward declaration of `host_udf_base` for the factory function of `HOST_UDF` aggregation.
543 class host_udf_base;
544 
551 template <typename Base = aggregation>
552 std::unique_ptr<Base> make_host_udf_aggregation(std::unique_ptr<host_udf_base> host_udf);
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(
592  null_equality nulls_equal = null_equality::EQUAL,
593  nan_equality nans_equal = nan_equality::ALL_EQUAL);
594 
609 template <typename Base = aggregation>
610 std::unique_ptr<Base> make_merge_m2_aggregation();
611 
620 template <typename Base = aggregation>
621 std::unique_ptr<Base> make_merge_histogram_aggregation();
622 
633 template <typename Base = aggregation>
634 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
635 
646 template <typename Base = aggregation>
648  size_type min_periods = 1);
649 
684 template <typename Base>
685 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
686 
722 template <typename Base>
723 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
724 
731 template <typename Base>
732 std::unique_ptr<Base> make_bitwise_aggregation(bitwise_op op);
733 
744 template <typename Base = aggregation>
745 std::unique_ptr<Base> make_top_k_aggregation(size_type k, order topk_order = order::DESCENDING);
746 
755  // end of group
757 } // namespace CUDF_EXPORT cudf
Abstract base class for specifying the desired aggregation in an aggregation_request.
Definition: aggregation.hpp:73
bool is_valid() const
Checks if the aggregation is valid, i.e. it was constructed with a valid value for the aggregation ki...
aggregation()
Default constructor.
aggregation(Kind kind_)
Construct a new aggregation object from a given aggregation kind.
virtual bool is_equal(aggregation const &other) const
Compares two aggregation objects for equality.
virtual size_t do_hash() const
Computes the hash value of the aggregation.
Kind
Possible aggregation operations.
Definition: aggregation.hpp:78
@ PRODUCT
product reduction
Definition: aggregation.hpp:81
@ ALL
all reduction
Definition: aggregation.hpp:87
@ TOP_K
top k elements in a group
@ M2
sum of squares of differences from the mean
Definition: aggregation.hpp:90
@ TDIGEST
create a tdigest from a set of input values
@ MEAN
arithmetic mean reduction
Definition: aggregation.hpp:89
@ MERGE_M2
merge partial values of M2 aggregation,
@ BITWISE_AGG
bitwise aggregation on numeric columns
@ MERGE_SETS
merge multiple lists values into one list then drop duplicate entries
@ MEDIAN
median reduction
Definition: aggregation.hpp:93
@ NUNIQUE
count number of unique elements
Definition: aggregation.hpp:97
@ MERGE_HISTOGRAM
merge partial values of HISTOGRAM aggregation
@ ARGMIN
Index of min element.
Definition: aggregation.hpp:96
@ VARIANCE
variance
Definition: aggregation.hpp:91
@ CORRELATION
correlation between two sets of elements
@ STD
standard deviation
Definition: aggregation.hpp:92
@ QUANTILE
compute specified quantile(s)
Definition: aggregation.hpp:94
@ COVARIANCE
covariance between two sets of elements
@ MAX
max reduction
Definition: aggregation.hpp:83
@ MIN
min reduction
Definition: aggregation.hpp:82
@ COLLECT_SET
collect values into a list without duplicate entries
@ LAG
window function, accesses row at specified offset preceding current row
@ LEAD
window function, accesses row at specified offset following current row
@ SUM_OF_SQUARES
sum of squares reduction
Definition: aggregation.hpp:88
@ NTH_ELEMENT
get the nth element
Definition: aggregation.hpp:98
@ 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:86
@ COLLECT_LIST
collect values into a list
@ COUNT_VALID
count number of valid elements
Definition: aggregation.hpp:84
@ ROW_NUMBER
get row-number of current index (relative to rolling window)
Definition: aggregation.hpp:99
@ SUM_OVERFLOW
sum reduction with overflow detection
Definition: aggregation.hpp:80
@ ARGMAX
Index of max element.
Definition: aggregation.hpp:95
@ HISTOGRAM
compute frequency of each element
@ RANK
get rank of current index
@ COUNT_ALL
count number of elements
Definition: aggregation.hpp:85
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:279
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:40
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.
Exception types and error-checking macros used throughout libcudf.
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:50
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_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:59
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_sum_overflow_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.
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={}, cuda::stream_ref 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:37
@ 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
std::unique_ptr< cudf::column > is_valid(cudf::column_view const &input, cuda::stream_ref stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Creates a column of type_id::BOOL8 elements where for every element in input true indicates the value...
#define CUDF_EXPECTS(...)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
Definition: error.hpp:182
#define CUDF_FAIL(...)
Indicates that an erroneous code path has been taken.
Definition: error.hpp:223
null_order
Indicates how null values compare against all other values.
Definition: types.hpp:140
null_equality
Enum to consider two nulls as equal or unequal.
Definition: types.hpp:132
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:76
null_policy
Enum to specify whether to include nulls or exclude nulls.
Definition: types.hpp:107
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:99
interpolation
Interpolation method to use when the desired quantile lies between two data points i and j.
Definition: types.hpp:173
nan_equality
Enum to consider different elements (of floating point types) holding NaN value as equal or unequal.
Definition: types.hpp:124
cuDF interfaces
Definition: host_udf.hpp:27
Type declarations for libcudf.