rolling.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, 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 
188 std::unique_ptr<column> rolling_window(
189  column_view const& input,
190  size_type preceding_window,
191  size_type following_window,
192  size_type min_periods,
193  rolling_aggregation const& agg,
196 
212 std::unique_ptr<column> rolling_window(
213  column_view const& input,
214  column_view const& default_outputs,
215  size_type preceding_window,
216  size_type following_window,
217  size_type min_periods,
218  rolling_aggregation const& agg,
221 
226  public:
233  static window_bounds get(size_type value) { return window_bounds(false, value); }
234 
241  {
242  return window_bounds(true, std::numeric_limits<cudf::size_type>::max());
243  }
244 
251  [[nodiscard]] bool is_unbounded() const { return _is_unbounded; }
252 
258  [[nodiscard]] size_type value() const { return _value; }
259 
260  private:
261  explicit window_bounds(bool is_unbounded_, size_type value_ = 0)
262  : _is_unbounded{is_unbounded_}, _value{value_}
263  {
264  }
265 
266  bool const _is_unbounded;
267  size_type const _value;
268 };
269 
363 std::unique_ptr<column> grouped_rolling_window(
364  table_view const& group_keys,
365  column_view const& input,
366  size_type preceding_window,
367  size_type following_window,
368  size_type min_periods,
369  rolling_aggregation const& aggr,
372 
385 std::unique_ptr<column> grouped_rolling_window(
386  table_view const& group_keys,
387  column_view const& input,
388  window_bounds preceding_window,
389  window_bounds following_window,
390  size_type min_periods,
391  rolling_aggregation const& aggr,
394 
411 std::unique_ptr<column> grouped_rolling_window(
412  table_view const& group_keys,
413  column_view const& input,
414  column_view const& default_outputs,
415  size_type preceding_window,
416  size_type following_window,
417  size_type min_periods,
418  rolling_aggregation const& aggr,
421 
435 std::unique_ptr<column> grouped_rolling_window(
436  table_view const& group_keys,
437  column_view const& input,
438  column_view const& default_outputs,
439  window_bounds preceding_window,
440  window_bounds following_window,
441  size_type min_periods,
442  rolling_aggregation const& aggr,
445 
557 std::unique_ptr<column> grouped_range_rolling_window(
558  table_view const& group_keys,
559  column_view const& orderby_column,
560  cudf::order const& order,
561  column_view const& input,
562  range_window_bounds const& preceding,
563  range_window_bounds const& following,
564  size_type min_periods,
565  rolling_aggregation const& aggr,
568 
584 std::unique_ptr<table> grouped_range_rolling_window(
585  table_view const& group_keys,
586  column_view const& orderby,
587  order order,
589  range_window_type preceding,
590  range_window_type following,
594 
641 std::unique_ptr<column> rolling_window(
642  column_view const& input,
643  column_view const& preceding_window,
644  column_view const& following_window,
645  size_type min_periods,
646  rolling_aggregation const& agg,
649 
657 bool is_valid_rolling_aggregation(data_type source, aggregation::Kind kind); // end of group
659 } // namespace CUDF_EXPORT cudf
Representation for specifying desired aggregations from aggregation-based APIs, e....
Kind
Possible aggregation operations.
Definition: aggregation.hpp:79
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:269
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:184
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:225
static window_bounds unbounded()
Construct unbounded window boundary.
Definition: rolling.hpp:240
size_type value() const
Gets the row-boundary for this window_bounds.
Definition: rolling.hpp:258
bool is_unbounded() const
Definition: rolling.hpp:251
static window_bounds get(size_type value)
Construct bounded window boundary.
Definition: rolling.hpp:233
Type declarations for libcudf.