rolling.hpp
转到此文件的文档。
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  * https://apache.ac.cn/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/aggregation.hpp>
21 #include <cudf/types.hpp>
23 #include <cudf/utilities/export.hpp>
25 
26 #include <rmm/resource_ref.hpp>
27 
28 #include <memory>
29 #include <optional>
30 #include <variant>
31 
32 namespace CUDF_EXPORT cudf {
49 /**
50  * @brief Strongly typed wrapper for bounded closed rolling windows.
51  *
52  */
59 
70 /**
71  * @brief Strongly typed wrapper for bounded open rolling windows.
72  *
73  */
80 struct bounded_open {
82 
88 /**
89  * @brief Strongly typed wrapper for unbounded rolling windows.
90  *
91  */
93 struct unbounded {
94  constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
95 };
100 /**
101  * @brief Strongly typed wrapper for current_row rolling windows.
102  *
103  */
105 struct current_row {
106  constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
107 };
108 
111 /**
112  * @brief The type of the range-based rolling window endpoint.
113  *
114  */
129 using range_window_type = std::variant<unbounded, current_row, bounded_closed, bounded_open>;
130 
172 /**
173  * @brief Applies a fixed-size rolling window function to the values in a column.
174  *
175  */
180 std::unique_ptr<column> make_range_windows(
181  table_view const& group_keys,
182  column_view const& orderby,
183  order order,
185  range_window_type preceding,
186  range_window_type following,
196 /**
197  * @brief Applies a fixed-size rolling window function to the values in a column,
198  * providing default outputs for nulls.
199  *
200  */
205 std::unique_ptr<column> rolling_window(
206  column_view const& input,
207  size_type preceding_window,
208  size_type following_window,
209  size_type min_periods,
210  rolling_aggregation const& agg,
213 
214 /**
215  * @brief Applies a fixed-size rolling window function to the values in a column,
216  * providing default outputs for nulls.
217  *
218  */
228 std::unique_ptr<column> rolling_window(
229  column_view const& input,
230  column_view const& default_outputs,
231  size_type preceding_window,
232  size_type following_window,
233  size_type min_periods,
234  rolling_aggregation const& agg,
237 
238 /**
239  * @brief Abstraction for window boundary sizes.
240  *
241  */
243  public
244  /**
245  * @brief Construct bounded window boundary.
246  *
247  * @param value The boundary size, in number of rows.
248  * @return A bounded window boundary.
249  */
250  static window_bounds get(size_type value) { return window_bounds(false, value); }
251 
252  /**
253  * @brief Construct unbounded window boundary.
254  *
255  * @return An unbounded window boundary.
256  */
258  {
259  return window_bounds(true, std::numeric_limits<cudf::size_type>::max());
260  }
261 
262  /**
263  * @brief Checks if the window boundary is unbounded.
264  *
265  * @return `true` if boundary is unbounded, `false` otherwise.
266  */
267  [[nodiscard]] bool is_unbounded() const { return _is_unbounded; }
268 
269  /**
270  * @brief Gets the row-boundary for this window_bounds.
271  *
272  * @note Undefined behavior if boundary is unbounded.
273  *
274  * @return Row boundary size.
275  */
276  [[nodiscard]] size_type value() const { return _value; }
277 
278  private
279  explicit window_bounds(bool is_unbounded_, size_type value_ = 0)
280  : _is_unbounded{is_unbounded_}, _value{value_}
281  {
282  }
283 
284  bool const _is_unbounded;
285  size_type const _value;
286 };
287 
346 /**
347  * @brief Applies a grouping-aware, fixed-size rolling window function to the values in a column.
348  *
349  */
355 std::unique_ptr<column> grouped_rolling_window(
356  table_view const& group_keys,
357  column_view const& input,
358  size_type preceding_window,
359  size_type following_window,
360  size_type min_periods,
368 /**
369  * @brief Applies a grouping-aware, fixed-size rolling window function to the values in a column.
370  *
371  * @note This overload accepts window boundaries defined via `window_bounds` struct.
372  *
373  */
377 std::unique_ptr<column> grouped_rolling_window(
378  table_view const& group_keys,
379  column_view const& input,
380  window_bounds preceding_window,
381  window_bounds following_window,
382  size_type min_periods,
383  rolling_aggregation const& aggr,
395  * @brief Applies a grouping-aware, fixed-size rolling window function to the values in a column,
396  * providing default outputs for nulls.
397  *
398  */
404 std::unique_ptr<column> grouped_rolling_window(
405  table_view const& group_keys,
406  column_view const& input,
407  column_view const& default_outputs,
408  size_type preceding_window,
409  size_type following_window,
410  size_type min_periods,
411  rolling_aggregation const& aggr,
418 /**
419  * @brief Applies a grouping-aware, fixed-size rolling window function to the values in a column,
420  * providing default outputs for nulls.
421  *
422  * @note This overload accepts window boundaries defined via `window_bounds` struct.
423  *
424  */
428 std::unique_ptr<column> grouped_rolling_window(
429  table_view const& group_keys,
430  column_view const& input,
431  column_view const& default_outputs,
432  window_bounds preceding_window,
433  window_bounds following_window,
434  size_type min_periods,
435  rolling_aggregation const& aggr,
539 /**
540  * @brief Applies a grouping-aware, value range-based rolling window function to the values in a column.
541  *
542  */
550 std::unique_ptr<column> grouped_range_rolling_window(
551  table_view const& group_keys,
552  column_view const& orderby_column,
553  cudf::order const& order,
554  column_view const& input,
555  range_window_bounds const& preceding,
556  range_window_bounds const& following,
592 /**
593  * @brief Applies a variable-size rolling window function to the values in a column.
594  *
595  */
600 std::unique_ptr<column> rolling_window(
601  column_view const& input,
602  column_view const& preceding_window,
593  * @brief Applies a variable-size rolling window function to the values in a column.
603  column_view const& following_window,
604  size_type min_periods,
558  rolling_aggregation const& aggr,
605  rolling_aggregation const& agg,
610 } // namespace CUDF_EXPORT cudf
aggregation.hpp
用于指定基于聚合 API 所需聚合的表示,例如...
cudf::column_view
aggregation.hpp
用于指定基于聚合 API 所需聚合的表示,例如...
定义: scalar.hpp:51
一组大小相同的 cudf::column_view。
default_stream.hpp
cudf::make_range_windows
std::pair< std::unique_ptr< column >, std::unique_ptr< column > > make_range_windows(table_view const &group_keys, column_view const &orderby, order order, null_order null_order, range_window_type preceding, range_window_type following, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
cudf::grouped_rolling_window
std::unique_ptr< column > grouped_rolling_window(table_view const &group_keys, column_view const &input, column_view const &default_outputs, window_bounds preceding_window, window_bounds following_window, size_type min_periods, rolling_aggregation const &aggr, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
将考虑分组的固定大小滚动窗口函数应用于列中的值。
std::variant< unbounded, current_row, bounded_closed, bounded_open > range_window_type
基于范围的滚动窗口端点的类型。
cudf::rolling_window
std::unique_ptr< column > rolling_window(column_view const &input, column_view const &preceding_window, column_view const &following_window, size_type min_periods, rolling_aggregation const &agg, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
将可变大小的滚动窗口函数应用于列中的值。
std::unique_ptr< column > grouped_range_rolling_window(table_view const &group_keys, column_view const &orderby_column, cudf::order const &order, column_view const &input, range_window_bounds const &preceding, range_window_bounds const &following, size_type min_periods, rolling_aggregation const &aggr, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())