aggregation.hpp
转到此文件的文档。
1 /*
2  * Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  *
4  * 根据 Apache 许可,版本 2.0(“许可”)获得许可;除非符合许可,否则不得使用此文件。
5  * 您可以在以下网址获得许可副本:
6  *
7  * https://apache.ac.cn/licenses/LICENSE-2.0
8  *
9  * 除非适用法律要求或书面同意,根据许可分发的软件按“原样”分发,不附带任何明示或暗示的担保或条件。
10  * 有关管理权限和限制的特定语言,请参阅许可。
11  *
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <cudf/types.hpp>
19 #include <cudf/utilities/export.hpp>
20 
21 #include <functional>
22 #include <memory>
23 #include <vector>
24 
35 namespace CUDF_EXPORT cudf {
42 // 前向声明
43 namespace detail {
44 class simple_aggregations_collector;
45 class aggregation_finalizer;
46 } // 命名空间 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,
97  M2,
99  STD,
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  // 覆盖组合聚合函数
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_equality nulls_equal = null_equality::EQUAL,
568  nan_equality nans_equal = nan_equality::ALL_EQUAL);
569 
570 // Forward declaration of `host_udf_base` for the factory function of `HOST_UDF` aggregation.
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 class 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  // 组结束
787 } // namespace CUDF_EXPORT cudf
cudf::aggregation
在计算完前置简单聚合后计算聚合结果。
cudf::aggregation::Kind
可能的聚合操作。
cudf::aggregation::PRODUCT
积归约
cudf::aggregation::ALL
全归约
cudf::aggregation::M2
与均值之差的平方和
cudf::aggregation::TDIGEST
从一组输入值创建 tdigest
cudf::aggregation::MEAN
算术平均归约
cudf::aggregation::MERGE_M2
合并 M2 聚合的部分值,
cudf::aggregation::PTX
基于 PTX 的 UDF 聚合。
cudf::aggregation::MERGE_SETS
将多个列表值合并到一个列表中,然后删除重复条目
cudf::aggregation::MEDIAN
中位数归约
cudf::aggregation::NUNIQUE
计算唯一元素的数量
cudf::aggregation::ARGMIN
最小元素的索引。
cudf::aggregation::VARIANCE
方差
cudf::aggregation::CORRELATION
两组元素之间的相关性
cudf::aggregation::STD
标准差
cudf::aggregation::QUANTILE
计算指定的分位数
cudf::aggregation::COVARIANCE
两组元素之间的协方差
cudf::aggregation::MAX
最大值归约
cudf::aggregation::MIN
最小值归约
cudf::aggregation::COLLECT_SET
将值收集到一个列表中,不包含重复条目
cudf::aggregation::LAG
窗口函数,访问当前行之前指定偏移量的行
cudf::aggregation::CUDA
基于 CUDA 的 UDF 聚合。
cudf::aggregation::LEAD
窗口函数,访问当前行之后指定偏移量的行
cudf::aggregation::SUM_OF_SQUARES
平方和归约
cudf::aggregation::SUM
求和归约
cudf::aggregation::NTH_ELEMENT
获取第 n 个元素
cudf::aggregation::EWMA
获取当前索引的指数加权移动平均值
cudf::aggregation::MERGE_LISTS
将多个列表值合并到一个列表中
cudf::aggregation::MERGE_TDIGEST
通过合并多个 tdigest 创建一个 tdigest
cudf::aggregation::HOST_UDF
基于 host 的 UDF 聚合
cudf::aggregation::ANY
任意归约
cudf::aggregation::COLLECT_LIST
将值收集到一个列表中
cudf::aggregation::COUNT_VALID
计算有效元素的数量
cudf::aggregation::ROW_NUMBER
获取当前索引的行号(相对于滚动窗口)
cudf::aggregation::ARGMAX
最大元素的索引。
cudf::aggregation::HISTOGRAM
计算每个元素的频率
cudf::aggregation::RANK
获取当前索引的排名
cudf::aggregation::COUNT_ALL
计算元素的数量
cudf::aggregation::is_equal
比较两个聚合对象是否相等。
cudf::aggregation::aggregation
构造一个新的聚合对象。
cudf::aggregation::do_hash
计算聚合的哈希值。
cudf::aggregation::clone
克隆聚合对象。
cudf::aggregation::kind
要执行的聚合。
cudf::data_type
cudf::segmented_reduce_aggregation
相关方法类型。
cudf::make_host_udf_aggregation
创建 HOST_UDF 聚合的工厂函数。
cudf::make_lag_aggregation
创建 LAG 聚合的工厂函数。
cudf::make_tdigest_aggregation
创建 TDIGEST 聚合的工厂函数。
cudf::rank_percentage
返回的排名是否应为百分比,并提及百分比归一化类型。
cudf::make_covariance_aggregation
创建 COVARIANCE 聚合的工厂函数。
cudf::make_std_aggregation
创建 STD 聚合的工厂函数。
cudf::make_correlation_aggregation
创建 CORRELATION 聚合的工厂函数。
cudf::make_merge_sets_aggregation
创建 VARIANCE 聚合的工厂函数。
cudf::make_lead_aggregation
创建 LEAD 聚合的工厂函数。
cudf::make_any_aggregation
创建 NUNIQUE 聚合的工厂函数。
cudf::make_max_aggregation
cudf::make_histogram_aggregation
创建基于 PTX 或 CUDA 的 UDF 聚合的工厂函数。
cudf::make_row_number_aggregation
cudf::make_merge_histogram_aggregation
创建 MERGE_HISTOGRAM 聚合的工厂函数。
cudf::make_count_aggregation
EWM 输入值第一个值的处理类型。
cudf::make_collect_list_aggregation
创建 COLLECT_LIST 聚合的工厂函数。
cudf::make_argmax_aggregation
创建 ARGMAX 聚合的工厂函数。
cudf::make_sum_aggregation
创建 M2 聚合的工厂函数。
cudf::make_merge_m2_aggregation
创建 MERGE_M2 聚合的工厂函数。
cudf::make_sum_of_squares_aggregation
cudf::make_nth_element_aggregation
用户定义函数字符串中的代码类型。
cudf::make_ewma_aggregation
创建 EWMA 聚合的工厂函数。
cudf::make_merge_lists_aggregation
创建 MERGE_LISTS 聚合的工厂函数。
cudf::make_collect_set_aggregation
创建 ARGMIN 聚合的工厂函数。
cudf::make_quantile_aggregation
cudf::make_merge_tdigest_aggregation
创建 MERGE_TDIGEST 聚合的工厂函数。
cudf::rank_percentage::ONE_NORMALIZED
(排名 - 1) / (计数 - 1)
cudf::rank_percentage::ZERO_NORMALIZED
排名 / 计数
cudf::quantiles
用于对列进行排名时使用的平局处理方法。
cudf::rank_method::DENSE
组之间排名总是增加 1
cudf::rank_method::AVERAGE
组内第一个值的均值
cudf::rank_method::MAX
最大值归约
cudf::rank_method::FIRST
稳定排序顺序排名(无平局)
cudf::rank_method::MIN
最小值归约
cudf::null_order
指示空值如何与所有其他值进行比较。
定义: types.hpp:159
cudf::null_equality
枚举,用于判断两个空值是否相等。
定义: types.hpp:151
cudf::size_type
列和表的行索引类型。
定义: types.hpp:95
cudf::null_policy
枚举,用于指定是包含空值还是排除空值。
定义: types.hpp:126
cudf::order
指示元素应按什么顺序排序。
定义: types.hpp:118
cudf::interpolation
当所需分位数介于两个数据点 i 和 j 之间时使用的插值方法。
定义: types.hpp:192
cudf::nan_equality
枚举,用于判断持有 NaN 值的不同元素(浮点类型)是否相等。
定义: types.hpp:143
cuDF 接口
定义: host_udf.hpp:37
types.hpp