rolling.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf/aggregation.hpp>
10 #include <cudf/types.hpp>
12 #include <cudf/utilities/export.hpp>
14 
15 #include <rmm/resource_ref.hpp>
16 
17 #include <memory>
18 #include <optional>
19 #include <variant>
20 
21 namespace CUDF_EXPORT cudf {
41 
48  bounded_closed(cudf::scalar const& delta) : delta_{delta} {}
53  [[nodiscard]] cudf::scalar const* delta() const noexcept { return &delta_; }
54 };
55 
66 struct bounded_open {
69 
76  bounded_open(cudf::scalar const& delta) : delta_{delta} {}
77 
82  [[nodiscard]] cudf::scalar const* delta() const noexcept { return &delta_; }
83 };
84 
90 struct unbounded {
95  [[nodiscard]] constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
96 };
102 struct current_row {
107  [[nodiscard]] constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
108 };
109 
113 using range_window_type = std::variant<unbounded, current_row, bounded_closed, bounded_open>;
114 
121  std::unique_ptr<rolling_aggregation> aggregation;
122 };
123 
140 std::pair<std::unique_ptr<column>, std::unique_ptr<column>> make_range_windows(
141  table_view const& group_keys,
142  column_view const& orderby,
143  order order,
145  range_window_type preceding,
146  range_window_type following,
149 
183 std::unique_ptr<column> rolling_window(
184  column_view const& input,
185  size_type preceding_window,
186  size_type following_window,
187  size_type min_periods,
188  rolling_aggregation const& agg,
191 
207 std::unique_ptr<column> rolling_window(
208  column_view const& input,
209  column_view const& default_outputs,
210  size_type preceding_window,
211  size_type following_window,
212  size_type min_periods,
213  rolling_aggregation const& agg,
216 
221  public:
228  static window_bounds get(size_type value) { return window_bounds(false, value); }
229 
236  {
237  return window_bounds(true, std::numeric_limits<cudf::size_type>::max());
238  }
239 
246  [[nodiscard]] bool is_unbounded() const { return _is_unbounded; }
247 
253  [[nodiscard]] size_type value() const { return _value; }
254 
255  private:
256  explicit window_bounds(bool is_unbounded_, size_type value_ = 0)
257  : _is_unbounded{is_unbounded_}, _value{value_}
258  {
259  }
260 
261  bool const _is_unbounded;
262  size_type const _value;
263 };
264 
357 std::unique_ptr<column> grouped_rolling_window(
358  table_view const& group_keys,
359  column_view const& input,
360  size_type preceding_window,
361  size_type following_window,
362  size_type min_periods,
363  rolling_aggregation const& aggr,
366 
379 std::unique_ptr<column> grouped_rolling_window(
380  table_view const& group_keys,
381  column_view const& input,
382  window_bounds preceding_window,
383  window_bounds following_window,
384  size_type min_periods,
385  rolling_aggregation const& aggr,
388 
405 std::unique_ptr<column> grouped_rolling_window(
406  table_view const& group_keys,
407  column_view const& input,
408  column_view const& default_outputs,
409  size_type preceding_window,
410  size_type following_window,
411  size_type min_periods,
412  rolling_aggregation const& aggr,
415 
429 std::unique_ptr<column> grouped_rolling_window(
430  table_view const& group_keys,
431  column_view const& input,
432  column_view const& default_outputs,
433  window_bounds preceding_window,
434  window_bounds following_window,
435  size_type min_periods,
436  rolling_aggregation const& aggr,
439 
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,
557  size_type min_periods,
558  rolling_aggregation const& aggr,
561 
577 std::unique_ptr<table> grouped_range_rolling_window(
578  table_view const& group_keys,
579  column_view const& orderby,
580  order order,
582  range_window_type preceding,
583  range_window_type following,
587 
629 std::unique_ptr<column> rolling_window(
630  column_view const& input,
631  column_view const& preceding_window,
632  column_view const& following_window,
633  size_type min_periods,
634  rolling_aggregation const& agg,
637 
645 bool is_valid_rolling_aggregation(data_type source, aggregation::Kind kind); // end of group
647 } // namespace CUDF_EXPORT cudf
Representation for specifying desired aggregations from aggregation-based APIs, e....
Kind
Possible aggregation operations.
Definition: aggregation.hpp:84
A non-owning, immutable view of device data as a column of elements, some of which may be null as ind...
Indicator for the logical data type of an element in a column.
Definition: types.hpp:238
Derived class intended for rolling_window specific aggregation usage.
An owning class to represent a singular value.
Definition: scalar.hpp:40
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:189
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())
Constructs preceding and following columns given window range specifications.
std::unique_ptr< table > grouped_range_rolling_window(table_view const &group_keys, column_view const &orderby, order order, null_order null_order, range_window_type preceding, range_window_type following, host_span< rolling_request const > requests, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Apply a grouping-aware range-based rolling window function to a sequence of columns.
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())
Applies a grouping-aware, fixed-size rolling window function to the values in a column.
bool is_valid_rolling_aggregation(data_type source, aggregation::Kind kind)
Indicate if a rolling aggregation is supported for a source datatype.
std::variant< unbounded, current_row, bounded_closed, bounded_open > range_window_type
The type of the range-based rolling window endpoint.
Definition: rolling.hpp:113
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())
Applies a variable-size rolling window function to the values in a column.
rmm::cuda_stream_view const get_default_stream()
Get the current default stream.
rmm::device_async_resource_ref get_current_device_resource_ref()
Get the current device memory resource reference.
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::device_accessible > > device_async_resource_ref
null_order
Indicates how null values compare against all other values.
Definition: types.hpp:148
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:84
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:107
cuDF interfaces
Definition: host_udf.hpp:26
Strongly typed wrapper for bounded closed rolling windows.
Definition: rolling.hpp:38
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:53
bounded_closed(cudf::scalar const &delta)
Construct a bounded closed rolling window.
Definition: rolling.hpp:48
cudf::scalar const & delta_
Definition: rolling.hpp:39
Strongly typed wrapper for bounded open rolling windows.
Definition: rolling.hpp:66
cudf::scalar const & delta_
Definition: rolling.hpp:67
bounded_open(cudf::scalar const &delta)
Construct a bounded open rolling window.
Definition: rolling.hpp:76
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:82
Strongly typed wrapper for current_row rolling windows.
Definition: rolling.hpp:102
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:107
C++20 std::span with reduced feature set.
Definition: span.hpp:182
Abstraction for window boundary sizes, to be used with grouped_range_rolling_window().
A request for a rolling aggregation on a column.
Definition: rolling.hpp:118
std::unique_ptr< rolling_aggregation > aggregation
Desired aggregation.
Definition: rolling.hpp:121
column_view values
Elements to aggregate.
Definition: rolling.hpp:119
size_type min_periods
Minimum number of observations required for the window to be valid.
Definition: rolling.hpp:120
Strongly typed wrapper for unbounded rolling windows.
Definition: rolling.hpp:90
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:95
Abstraction for window boundary sizes.
Definition: rolling.hpp:220
static window_bounds unbounded()
Construct unbounded window boundary.
Definition: rolling.hpp:235
size_type value() const
Gets the row-boundary for this window_bounds.
Definition: rolling.hpp:253
bool is_unbounded() const
Definition: rolling.hpp:246
static window_bounds get(size_type value)
Construct bounded window boundary.
Definition: rolling.hpp:228
Type declarations for libcudf.