aggregation.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION.
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 {
38 enum class rank_method : int32_t {
39  FIRST,
40  AVERAGE,
41  MIN,
42  MAX,
43  DENSE
44 };
45 
51 enum class rank_percentage : int32_t {
52  NONE,
55 };
56 
60 enum class bitwise_op : int32_t {
61  AND,
62  OR,
63  XOR
64 };
65 
74 class aggregation {
75  public:
79  enum Kind : int32_t {
80  SUM = 0,
83  MIN,
84  MAX,
87  ANY,
88  ALL,
90  MEAN,
91  M2,
93  STD,
106  LAG,
107  PTX,
121  INVALID
122  };
123 
130  aggregation() : kind{Kind::INVALID}
131  {
132  CUDF_FAIL("No-parameter aggregation constructor should never be called");
133  }
134 
140  aggregation(Kind kind_) : kind{kind_} { CUDF_EXPECTS(is_valid(), "Invalid aggregation kind"); }
142  virtual ~aggregation() = default;
143 
150  [[nodiscard]] bool is_valid() const { return kind >= 0 && kind < Kind::INVALID; }
151 
158  [[nodiscard]] virtual bool is_equal(aggregation const& other) const { return kind == other.kind; }
159 
165  [[nodiscard]] virtual size_t do_hash() const { return std::hash<int>{}(kind); }
166 
172  [[nodiscard]] virtual std::unique_ptr<aggregation> clone() const = 0;
173 };
174 
178 class rolling_aggregation : public virtual aggregation {};
179 
183 class groupby_aggregation : public virtual aggregation {};
184 
188 class groupby_scan_aggregation : public virtual aggregation {};
189 
193 class reduce_aggregation : public virtual aggregation {};
194 
198 class scan_aggregation : public virtual aggregation {};
199 
203 class segmented_reduce_aggregation : public virtual aggregation {};
204 
206 enum class udf_type : bool { CUDA, PTX };
208 enum class correlation_type : int32_t { PEARSON, KENDALL, SPEARMAN };
210 enum class ewm_history : int32_t { INFINITE, FINITE };
211 
214 template <typename Base = aggregation>
215 std::unique_ptr<Base> make_sum_aggregation();
216 
219 template <typename Base = aggregation>
220 std::unique_ptr<Base> make_sum_with_overflow_aggregation();
221 
224 template <typename Base = aggregation>
225 std::unique_ptr<Base> make_product_aggregation();
226 
229 template <typename Base = aggregation>
230 std::unique_ptr<Base> make_min_aggregation();
231 
234 template <typename Base = aggregation>
235 std::unique_ptr<Base> make_max_aggregation();
236 
243 template <typename Base = aggregation>
244 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
245 
248 template <typename Base = aggregation>
249 std::unique_ptr<Base> make_any_aggregation();
250 
253 template <typename Base = aggregation>
254 std::unique_ptr<Base> make_all_aggregation();
255 
258 template <typename Base = aggregation>
259 std::unique_ptr<Base> make_histogram_aggregation();
260 
263 template <typename Base = aggregation>
264 std::unique_ptr<Base> make_sum_of_squares_aggregation();
265 
268 template <typename Base = aggregation>
269 std::unique_ptr<Base> make_mean_aggregation();
270 
283 template <typename Base = aggregation>
284 std::unique_ptr<Base> make_m2_aggregation();
285 
295 template <typename Base = aggregation>
296 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
297 
307 template <typename Base = aggregation>
308 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
309 
312 template <typename Base = aggregation>
313 std::unique_ptr<Base> make_median_aggregation();
314 
322 template <typename Base = aggregation>
323 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
324  interpolation interp = interpolation::LINEAR);
325 
332 template <typename Base = aggregation>
333 std::unique_ptr<Base> make_argmax_aggregation();
334 
341 template <typename Base = aggregation>
342 std::unique_ptr<Base> make_argmin_aggregation();
343 
351 template <typename Base = aggregation>
352 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
353 
368 template <typename Base = aggregation>
369 std::unique_ptr<Base> make_nth_element_aggregation(
370  size_type n, null_policy null_handling = null_policy::INCLUDE);
371 
374 template <typename Base = aggregation>
375 std::unique_ptr<Base> make_row_number_aggregation();
376 
410 template <typename Base = aggregation>
411 std::unique_ptr<Base> make_ewma_aggregation(double const center_of_mass, ewm_history history);
412 
485 template <typename Base = aggregation>
486 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
487  order column_order = order::ASCENDING,
488  null_policy null_handling = null_policy::EXCLUDE,
489  null_order null_precedence = null_order::AFTER,
490  rank_percentage percentage = rank_percentage::NONE);
491 
503 template <typename Base = aggregation>
504 std::unique_ptr<Base> make_collect_list_aggregation(
505  null_policy null_handling = null_policy::INCLUDE);
506 
523 template <typename Base = aggregation>
524 std::unique_ptr<Base> make_collect_set_aggregation(
525  null_policy null_handling = null_policy::INCLUDE,
526  null_equality nulls_equal = null_equality::EQUAL,
527  nan_equality nans_equal = nan_equality::ALL_EQUAL);
528 
535 template <typename Base = aggregation>
536 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
537 
544 template <typename Base = aggregation>
545 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
546 
556 template <typename Base = aggregation>
557 std::unique_ptr<Base> make_udf_aggregation(udf_type type,
558  std::string const& user_defined_aggregator,
559  data_type output_type);
560 
561 // Forward declaration of `host_udf_base` for the factory function of `HOST_UDF` aggregation.
562 class host_udf_base;
563 
570 template <typename Base = aggregation>
571 std::unique_ptr<Base> make_host_udf_aggregation(std::unique_ptr<host_udf_base> host_udf);
572 
584 template <typename Base = aggregation>
585 std::unique_ptr<Base> make_merge_lists_aggregation();
586 
609 template <typename Base = aggregation>
610 std::unique_ptr<Base> make_merge_sets_aggregation(
611  null_equality nulls_equal = null_equality::EQUAL,
612  nan_equality nans_equal = nan_equality::ALL_EQUAL);
613 
628 template <typename Base = aggregation>
629 std::unique_ptr<Base> make_merge_m2_aggregation();
630 
639 template <typename Base = aggregation>
640 std::unique_ptr<Base> make_merge_histogram_aggregation();
641 
652 template <typename Base = aggregation>
653 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
654 
665 template <typename Base = aggregation>
667  size_type min_periods = 1);
668 
703 template <typename Base>
704 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
705 
741 template <typename Base>
742 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
743 
750 template <typename Base>
751 std::unique_ptr<Base> make_bitwise_aggregation(bitwise_op op);
752 
763 template <typename Base = aggregation>
764 std::unique_ptr<Base> make_top_k_aggregation(size_type k, order topk_order = order::DESCENDING);
765 
774  // end of group
776 } // namespace CUDF_EXPORT cudf
Abstract base class for specifying the desired aggregation in an aggregation_request.
Definition: aggregation.hpp:74
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:79
@ PRODUCT
product reduction
Definition: aggregation.hpp:82
@ ALL
all reduction
Definition: aggregation.hpp:88
@ TOP_K
top k elements in a group
@ M2
sum of squares of differences from the mean
Definition: aggregation.hpp:91
@ TDIGEST
create a tdigest from a set of input values
@ MEAN
arithmetic mean reduction
Definition: aggregation.hpp:90
@ 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:94
@ NUNIQUE
count number of unique elements
Definition: aggregation.hpp:98
@ MERGE_HISTOGRAM
merge partial values of HISTOGRAM aggregation
@ ARGMIN
Index of min element.
Definition: aggregation.hpp:97
@ VARIANCE
variance
Definition: aggregation.hpp:92
@ CORRELATION
correlation between two sets of elements
@ STD
standard deviation
Definition: aggregation.hpp:93
@ QUANTILE
compute specified quantile(s)
Definition: aggregation.hpp:95
@ COVARIANCE
covariance between two sets of elements
@ MAX
max reduction
Definition: aggregation.hpp:84
@ MIN
min reduction
Definition: aggregation.hpp:83
@ 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:89
@ NTH_ELEMENT
get the nth element
Definition: aggregation.hpp:99
@ 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:87
@ COLLECT_LIST
collect values into a list
@ COUNT_VALID
count number of valid elements
Definition: aggregation.hpp:85
@ ROW_NUMBER
get row-number of current index (relative to rolling window)
@ SUM_WITH_OVERFLOW
sum reduction with overflow detection
Definition: aggregation.hpp:81
@ ARGMAX
Index of max element.
Definition: aggregation.hpp:96
@ HISTOGRAM
compute frequency of each element
@ RANK
get rank of current index
@ COUNT_ALL
count number of elements
Definition: aggregation.hpp:86
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:51
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:60
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:38
@ 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, rmm::cuda_stream_view 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:143
#define CUDF_FAIL(...)
Indicates that an erroneous code path has been taken.
Definition: error.hpp:182
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.