aggregation.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, 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 
76 enum class bitwise_op : int32_t {
77  AND,
78  OR,
79  XOR
80 };
81 
90 class aggregation {
91  public:
95  enum Kind {
96  SUM,
99  MIN,
100  MAX,
103  ANY,
104  ALL,
107  M2,
109  STD,
122  LAG,
123  PTX,
135  BITWISE_AGG
136  };
137 
138  aggregation() = delete;
139 
147  virtual ~aggregation() = default;
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  // override functions for compound aggregations
179  virtual std::vector<std::unique_ptr<aggregation>> get_simple_aggregations(
180  data_type col_type, cudf::detail::simple_aggregations_collector& collector) const = 0;
181 
188  virtual void finalize(cudf::detail::aggregation_finalizer& finalizer) const = 0;
189 };
190 
198 class rolling_aggregation : public virtual aggregation {
199  public:
200  ~rolling_aggregation() override = default;
201 
202  protected:
205  using aggregation::aggregation;
206 };
207 
211 class groupby_aggregation : public virtual aggregation {
212  public:
213  ~groupby_aggregation() override = default;
214 
215  protected:
217 };
218 
222 class groupby_scan_aggregation : public virtual aggregation {
223  public:
224  ~groupby_scan_aggregation() override = default;
225 
226  protected:
228 };
229 
233 class reduce_aggregation : public virtual aggregation {
234  public:
235  ~reduce_aggregation() override = default;
236 
237  protected:
238  reduce_aggregation() {}
239 };
240 
244 class scan_aggregation : public virtual aggregation {
245  public:
246  ~scan_aggregation() override = default;
247 
248  protected:
249  scan_aggregation() {}
250 };
251 
256  public:
257  ~segmented_reduce_aggregation() override = default;
258 
259  protected:
261 };
262 
264 enum class udf_type : bool { CUDA, PTX };
266 enum class correlation_type : int32_t { PEARSON, KENDALL, SPEARMAN };
268 enum class ewm_history : int32_t { INFINITE, FINITE };
269 
272 template <typename Base = aggregation>
273 std::unique_ptr<Base> make_sum_aggregation();
274 
277 template <typename Base = aggregation>
278 std::unique_ptr<Base> make_sum_with_overflow_aggregation();
279 
282 template <typename Base = aggregation>
283 std::unique_ptr<Base> make_product_aggregation();
284 
287 template <typename Base = aggregation>
288 std::unique_ptr<Base> make_min_aggregation();
289 
292 template <typename Base = aggregation>
293 std::unique_ptr<Base> make_max_aggregation();
294 
301 template <typename Base = aggregation>
302 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
303 
306 template <typename Base = aggregation>
307 std::unique_ptr<Base> make_any_aggregation();
308 
311 template <typename Base = aggregation>
312 std::unique_ptr<Base> make_all_aggregation();
313 
316 template <typename Base = aggregation>
317 std::unique_ptr<Base> make_histogram_aggregation();
318 
321 template <typename Base = aggregation>
322 std::unique_ptr<Base> make_sum_of_squares_aggregation();
323 
326 template <typename Base = aggregation>
327 std::unique_ptr<Base> make_mean_aggregation();
328 
341 template <typename Base = aggregation>
342 std::unique_ptr<Base> make_m2_aggregation();
343 
353 template <typename Base = aggregation>
354 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
355 
365 template <typename Base = aggregation>
366 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
367 
370 template <typename Base = aggregation>
371 std::unique_ptr<Base> make_median_aggregation();
372 
380 template <typename Base = aggregation>
381 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
382  interpolation interp = interpolation::LINEAR);
383 
390 template <typename Base = aggregation>
391 std::unique_ptr<Base> make_argmax_aggregation();
392 
399 template <typename Base = aggregation>
400 std::unique_ptr<Base> make_argmin_aggregation();
401 
409 template <typename Base = aggregation>
410 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
411 
426 template <typename Base = aggregation>
427 std::unique_ptr<Base> make_nth_element_aggregation(
428  size_type n, null_policy null_handling = null_policy::INCLUDE);
429 
432 template <typename Base = aggregation>
433 std::unique_ptr<Base> make_row_number_aggregation();
434 
468 template <typename Base = aggregation>
469 std::unique_ptr<Base> make_ewma_aggregation(double const center_of_mass, ewm_history history);
470 
543 template <typename Base = aggregation>
544 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
545  order column_order = order::ASCENDING,
546  null_policy null_handling = null_policy::EXCLUDE,
547  null_order null_precedence = null_order::AFTER,
548  rank_percentage percentage = rank_percentage::NONE);
549 
561 template <typename Base = aggregation>
562 std::unique_ptr<Base> make_collect_list_aggregation(
563  null_policy null_handling = null_policy::INCLUDE);
564 
581 template <typename Base = aggregation>
582 std::unique_ptr<Base> make_collect_set_aggregation(
583  null_policy null_handling = null_policy::INCLUDE,
584  null_equality nulls_equal = null_equality::EQUAL,
585  nan_equality nans_equal = nan_equality::ALL_EQUAL);
586 
593 template <typename Base = aggregation>
594 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
595 
602 template <typename Base = aggregation>
603 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
604 
614 template <typename Base = aggregation>
615 std::unique_ptr<Base> make_udf_aggregation(udf_type type,
616  std::string const& user_defined_aggregator,
617  data_type output_type);
618 
619 // Forward declaration of `host_udf_base` for the factory function of `HOST_UDF` aggregation.
620 class host_udf_base;
621 
628 template <typename Base = aggregation>
629 std::unique_ptr<Base> make_host_udf_aggregation(std::unique_ptr<host_udf_base> host_udf);
630 
642 template <typename Base = aggregation>
643 std::unique_ptr<Base> make_merge_lists_aggregation();
644 
667 template <typename Base = aggregation>
668 std::unique_ptr<Base> make_merge_sets_aggregation(
669  null_equality nulls_equal = null_equality::EQUAL,
670  nan_equality nans_equal = nan_equality::ALL_EQUAL);
671 
686 template <typename Base = aggregation>
687 std::unique_ptr<Base> make_merge_m2_aggregation();
688 
697 template <typename Base = aggregation>
698 std::unique_ptr<Base> make_merge_histogram_aggregation();
699 
710 template <typename Base = aggregation>
711 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
712 
723 template <typename Base = aggregation>
725  size_type min_periods = 1);
726 
761 template <typename Base>
762 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
763 
799 template <typename Base>
800 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
801 
808 template <typename Base>
809 std::unique_ptr<Base> make_bitwise_aggregation(bitwise_op op);
810 
819  // end of group
821 } // namespace CUDF_EXPORT cudf
Abstract base class for specifying the desired aggregation in an aggregation_request.
Definition: aggregation.hpp:90
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:95
@ PRODUCT
product reduction
Definition: aggregation.hpp:98
@ ALL
all reduction
@ M2
sum of squares of differences from the mean
@ TDIGEST
create a tdigest from a set of input values
@ MEAN
arithmetic mean reduction
@ 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
@ NUNIQUE
count number of unique elements
@ MERGE_HISTOGRAM
merge partial values of HISTOGRAM aggregation
@ ARGMIN
Index of min element.
@ CORRELATION
correlation between two sets of elements
@ STD
standard deviation
@ QUANTILE
compute specified quantile(s)
@ COVARIANCE
covariance between two sets of elements
@ MAX
max reduction
@ MIN
min reduction
Definition: aggregation.hpp:99
@ 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
@ SUM
sum reduction
Definition: aggregation.hpp:96
@ 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
@ COLLECT_LIST
collect values into a list
@ COUNT_VALID
count number of valid elements
@ ROW_NUMBER
get row-number of current index (relative to rolling window)
@ SUM_WITH_OVERFLOW
sum reduction with overflow detection
Definition: aggregation.hpp:97
@ ARGMAX
Index of max element.
@ HISTOGRAM
compute frequency of each element
@ RANK
get rank of current index
@ COUNT_ALL
count number of elements
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:249
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:50
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: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_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:76
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: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: host_udf.hpp:37
Type declarations for libcudf.