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 
21 #include <functional>
22 #include <memory>
23 #include <vector>
24 
34 namespace cudf {
41 // forward declaration
42 namespace detail {
43 class simple_aggregations_collector;
44 class aggregation_finalizer;
45 } // namespace detail
46 
53 enum class rank_method : int32_t {
54  FIRST,
55  AVERAGE,
56  MIN,
57  MAX,
58  DENSE
59 };
60 
66 enum class rank_percentage : int32_t {
67  NONE,
70 };
71 
80 class aggregation {
81  public:
85  enum Kind {
86  SUM,
88  MIN,
89  MAX,
92  ANY,
93  ALL,
95  MEAN,
96  M2,
98  STD,
111  LAG,
112  PTX,
123  };
124 
125  aggregation() = delete;
126 
134  virtual ~aggregation() = default;
135 
142  [[nodiscard]] virtual bool is_equal(aggregation const& other) const { return kind == other.kind; }
143 
149  [[nodiscard]] virtual size_t do_hash() const { return std::hash<int>{}(kind); }
150 
156  [[nodiscard]] virtual std::unique_ptr<aggregation> clone() const = 0;
157 
158  // override functions for compound aggregations
166  virtual std::vector<std::unique_ptr<aggregation>> get_simple_aggregations(
167  data_type col_type, cudf::detail::simple_aggregations_collector& collector) const = 0;
168 
175  virtual void finalize(cudf::detail::aggregation_finalizer& finalizer) const = 0;
176 };
177 
185 class rolling_aggregation : public virtual aggregation {
186  public:
187  ~rolling_aggregation() override = default;
188 
189  protected:
192  using aggregation::aggregation;
193 };
194 
198 class groupby_aggregation : public virtual aggregation {
199  public:
200  ~groupby_aggregation() override = default;
201 
202  protected:
204 };
205 
209 class groupby_scan_aggregation : public virtual aggregation {
210  public:
211  ~groupby_scan_aggregation() override = default;
212 
213  protected:
215 };
216 
220 class reduce_aggregation : public virtual aggregation {
221  public:
222  ~reduce_aggregation() override = default;
223 
224  protected:
225  reduce_aggregation() {}
226 };
227 
231 class scan_aggregation : public virtual aggregation {
232  public:
233  ~scan_aggregation() override = default;
234 
235  protected:
236  scan_aggregation() {}
237 };
238 
243  public:
244  ~segmented_reduce_aggregation() override = default;
245 
246  protected:
248 };
249 
251 enum class udf_type : bool { CUDA, PTX };
253 enum class correlation_type : int32_t { PEARSON, KENDALL, SPEARMAN };
255 enum class ewm_history : int32_t { INFINITE, FINITE };
256 
259 template <typename Base = aggregation>
260 std::unique_ptr<Base> make_sum_aggregation();
261 
264 template <typename Base = aggregation>
265 std::unique_ptr<Base> make_product_aggregation();
266 
269 template <typename Base = aggregation>
270 std::unique_ptr<Base> make_min_aggregation();
271 
274 template <typename Base = aggregation>
275 std::unique_ptr<Base> make_max_aggregation();
276 
283 template <typename Base = aggregation>
284 std::unique_ptr<Base> make_count_aggregation(null_policy null_handling = null_policy::EXCLUDE);
285 
288 template <typename Base = aggregation>
289 std::unique_ptr<Base> make_any_aggregation();
290 
293 template <typename Base = aggregation>
294 std::unique_ptr<Base> make_all_aggregation();
295 
298 template <typename Base = aggregation>
299 std::unique_ptr<Base> make_histogram_aggregation();
300 
303 template <typename Base = aggregation>
304 std::unique_ptr<Base> make_sum_of_squares_aggregation();
305 
308 template <typename Base = aggregation>
309 std::unique_ptr<Base> make_mean_aggregation();
310 
323 template <typename Base = aggregation>
324 std::unique_ptr<Base> make_m2_aggregation();
325 
335 template <typename Base = aggregation>
336 std::unique_ptr<Base> make_variance_aggregation(size_type ddof = 1);
337 
347 template <typename Base = aggregation>
348 std::unique_ptr<Base> make_std_aggregation(size_type ddof = 1);
349 
352 template <typename Base = aggregation>
353 std::unique_ptr<Base> make_median_aggregation();
354 
362 template <typename Base = aggregation>
363 std::unique_ptr<Base> make_quantile_aggregation(std::vector<double> const& quantiles,
365 
372 template <typename Base = aggregation>
373 std::unique_ptr<Base> make_argmax_aggregation();
374 
381 template <typename Base = aggregation>
382 std::unique_ptr<Base> make_argmin_aggregation();
383 
391 template <typename Base = aggregation>
392 std::unique_ptr<Base> make_nunique_aggregation(null_policy null_handling = null_policy::EXCLUDE);
393 
408 template <typename Base = aggregation>
409 std::unique_ptr<Base> make_nth_element_aggregation(
410  size_type n, null_policy null_handling = null_policy::INCLUDE);
411 
414 template <typename Base = aggregation>
415 std::unique_ptr<Base> make_row_number_aggregation();
416 
450 template <typename Base = aggregation>
451 std::unique_ptr<Base> make_ewma_aggregation(double const center_of_mass, ewm_history history);
452 
525 template <typename Base = aggregation>
526 std::unique_ptr<Base> make_rank_aggregation(rank_method method,
527  order column_order = order::ASCENDING,
528  null_policy null_handling = null_policy::EXCLUDE,
529  null_order null_precedence = null_order::AFTER,
531 
543 template <typename Base = aggregation>
544 std::unique_ptr<Base> make_collect_list_aggregation(
545  null_policy null_handling = null_policy::INCLUDE);
546 
563 template <typename Base = aggregation>
564 std::unique_ptr<Base> make_collect_set_aggregation(
565  null_policy null_handling = null_policy::INCLUDE,
566  null_equality nulls_equal = null_equality::EQUAL,
568 
575 template <typename Base = aggregation>
576 std::unique_ptr<Base> make_lag_aggregation(size_type offset);
577 
584 template <typename Base = aggregation>
585 std::unique_ptr<Base> make_lead_aggregation(size_type offset);
586 
596 template <typename Base = aggregation>
597 std::unique_ptr<Base> make_udf_aggregation(udf_type type,
598  std::string const& user_defined_aggregator,
599  data_type output_type);
600 
612 template <typename Base = aggregation>
613 std::unique_ptr<Base> make_merge_lists_aggregation();
614 
637 template <typename Base = aggregation>
638 std::unique_ptr<Base> make_merge_sets_aggregation(
639  null_equality nulls_equal = null_equality::EQUAL,
641 
656 template <typename Base = aggregation>
657 std::unique_ptr<Base> make_merge_m2_aggregation();
658 
667 template <typename Base = aggregation>
668 std::unique_ptr<Base> make_merge_histogram_aggregation();
669 
680 template <typename Base = aggregation>
681 std::unique_ptr<Base> make_covariance_aggregation(size_type min_periods = 1, size_type ddof = 1);
682 
693 template <typename Base = aggregation>
695  size_type min_periods = 1);
696 
731 template <typename Base>
732 std::unique_ptr<Base> make_tdigest_aggregation(int max_centroids = 1000);
733 
769 template <typename Base>
770 std::unique_ptr<Base> make_merge_tdigest_aggregation(int max_centroids = 1000);
771  // end of group
773 } // namespace cudf
Abstract base class for specifying the desired aggregation in an aggregation_request.
Definition: aggregation.hpp:80
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:85
@ 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,
@ PTX
PTX UDF based reduction.
@ 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 UDF based reduction.
@ 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:86
@ 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
@ 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)
@ 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:241
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_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:66
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::device_async_resource_ref mr=rmm::mr::get_current_device_resource())
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:53
@ 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:157
null_equality
Enum to consider two nulls as equal or unequal.
Definition: types.hpp:149
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:93
null_policy
Enum to specify whether to include nulls or exclude nulls.
Definition: types.hpp:124
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:116
interpolation
Interpolation method to use when the desired quantile lies between two data points i and j.
Definition: types.hpp:190
nan_equality
Enum to consider different elements (of floating point types) holding NaN value as equal or unequal.
Definition: types.hpp:141
@ AFTER
NULL values ordered after all other values.
@ EQUAL
nulls compare equal
@ INCLUDE
include null elements
@ EXCLUDE
exclude null elements
@ ASCENDING
Elements ordered from small to large.
@ LINEAR
Linear interpolation between i and j.
@ ALL_EQUAL
All NaNs compare equal, regardless of sign.
cuDF interfaces
Definition: aggregation.hpp:34
Type declarations for libcudf.