aggregation.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, 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 
81 class aggregation {
82  public:
86  enum Kind {
87  SUM,
89  MIN,
90  MAX,
93  ANY,
94  ALL,
96  MEAN,
97  M2,
99  STD,
112  LAG,
113  PTX,
124  MERGE_HISTOGRAM
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_product_aggregation();
268 
271 template <typename Base = aggregation>
272 std::unique_ptr<Base> make_min_aggregation();
273 
276 template <typename Base = aggregation>
277 std::unique_ptr<Base> make_max_aggregation();
278 
285 template <typename Base = aggregation>
286 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
287 
290 template <typename Base = aggregation>
291 std::unique_ptr<Base> make_any_aggregation();
292 
295 template <typename Base = aggregation>
296 std::unique_ptr<Base> make_all_aggregation();
297 
300 template <typename Base = aggregation>
301 std::unique_ptr<Base> make_histogram_aggregation();
302 
305 template <typename Base = aggregation>
306 std::unique_ptr<Base> make_sum_of_squares_aggregation();
307 
310 template <typename Base = aggregation>
311 std::unique_ptr<Base> make_mean_aggregation();
312 
325 template <typename Base = aggregation>
326 std::unique_ptr<Base> make_m2_aggregation();
327 
337 template <typename Base = aggregation>
338 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
339 
349 template <typename Base = aggregation>
350 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
351 
354 template <typename Base = aggregation>
355 std::unique_ptr<Base> make_median_aggregation();
356 
364 template <typename Base = aggregation>
365 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
366  interpolation interp = interpolation::LINEAR);
367 
374 template <typename Base = aggregation>
375 std::unique_ptr<Base> make_argmax_aggregation();
376 
383 template <typename Base = aggregation>
384 std::unique_ptr<Base> make_argmin_aggregation();
385 
393 template <typename Base = aggregation>
394 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
395 
410 template <typename Base = aggregation>
411 std::unique_ptr<Base> make_nth_element_aggregation(
412  size_type n, null_policy null_handling = null_policy::INCLUDE);
413 
416 template <typename Base = aggregation>
417 std::unique_ptr<Base> make_row_number_aggregation();
418 
452 template <typename Base = aggregation>
453 std::unique_ptr<Base> make_ewma_aggregation(double const center_of_mass, ewm_history history);
454 
527 template <typename Base = aggregation>
528 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
529  order column_order = order::ASCENDING,
530  null_policy null_handling = null_policy::EXCLUDE,
531  null_order null_precedence = null_order::AFTER,
532  rank_percentage percentage = rank_percentage::NONE);
533 
545 template <typename Base = aggregation>
546 std::unique_ptr<Base> make_collect_list_aggregation(
547  null_policy null_handling = null_policy::INCLUDE);
548 
565 template <typename Base = aggregation>
566 std::unique_ptr<Base> make_collect_set_aggregation(
567  null_policy null_handling = null_policy::INCLUDE,
568  null_equality nulls_equal = null_equality::EQUAL,
569  nan_equality nans_equal = nan_equality::ALL_EQUAL);
570 
577 template <typename Base = aggregation>
578 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
579 
586 template <typename Base = aggregation>
587 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
588 
598 template <typename Base = aggregation>
599 std::unique_ptr<Base> make_udf_aggregation(udf_type type,
600  std::string const& user_defined_aggregator,
601  data_type output_type);
602 
603 // Forward declaration of `host_udf_base` for the factory function of `HOST_UDF` aggregation.
604 struct host_udf_base;
605 
612 template <typename Base = aggregation>
613 std::unique_ptr<Base> make_host_udf_aggregation(std::unique_ptr<host_udf_base> host_udf);
614 
626 template <typename Base = aggregation>
627 std::unique_ptr<Base> make_merge_lists_aggregation();
628 
651 template <typename Base = aggregation>
652 std::unique_ptr<Base> make_merge_sets_aggregation(
653  null_equality nulls_equal = null_equality::EQUAL,
654  nan_equality nans_equal = nan_equality::ALL_EQUAL);
655 
670 template <typename Base = aggregation>
671 std::unique_ptr<Base> make_merge_m2_aggregation();
672 
681 template <typename Base = aggregation>
682 std::unique_ptr<Base> make_merge_histogram_aggregation();
683 
694 template <typename Base = aggregation>
695 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
696 
707 template <typename Base = aggregation>
709  size_type min_periods = 1);
710 
745 template <typename Base>
746 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
747 
783 template <typename Base>
784 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
785  // end of group
787 } // namespace CUDF_EXPORT cudf
Abstract base class for specifying the desired aggregation in an aggregation_request.
Definition: aggregation.hpp:81
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:86
@ PRODUCT
product reduction
Definition: aggregation.hpp:88
@ ALL
all reduction
Definition: aggregation.hpp:94
@ M2
sum of squares of differences from the mean
Definition: aggregation.hpp:97
@ TDIGEST
create a tdigest from a set of input values
@ MEAN
arithmetic mean reduction
Definition: aggregation.hpp:96
@ 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
@ ARGMIN
Index of min element.
@ VARIANCE
variance
Definition: aggregation.hpp:98
@ CORRELATION
correlation between two sets of elements
@ STD
standard deviation
Definition: aggregation.hpp:99
@ QUANTILE
compute specified quantile(s)
@ COVARIANCE
covariance between two sets of elements
@ MAX
max reduction
Definition: aggregation.hpp:90
@ MIN
min reduction
Definition: aggregation.hpp:89
@ 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:95
@ SUM
sum reduction
Definition: aggregation.hpp:87
@ 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:93
@ COLLECT_LIST
collect values into a list
@ COUNT_VALID
count number of valid elements
Definition: aggregation.hpp:91
@ ROW_NUMBER
get row-number of current index (relative to rolling window)
@ 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:92
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:243
Derived class intended for groupby specific aggregation usage.
Derived class intended for groupby specific scan usage.
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_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_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.
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()
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
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:39
The interface for host-based UDF implementation.
Definition: host_udf.hpp:99
Type declarations for libcudf.