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 #include <cudf/utilities/span.hpp>
15 
16 #include <rmm/resource_ref.hpp>
17 
18 #include <memory>
19 #include <optional>
20 #include <variant>
21 
22 namespace CUDF_EXPORT cudf {
42 
49  bounded_closed(cudf::scalar const& delta) : delta_{delta} {}
54  [[nodiscard]] cudf::scalar const* delta() const noexcept { return &delta_; }
55 };
56 
67 struct bounded_open {
70 
77  bounded_open(cudf::scalar const& delta) : delta_{delta} {}
78 
83  [[nodiscard]] cudf::scalar const* delta() const noexcept { return &delta_; }
84 };
85 
91 struct unbounded {
96  [[nodiscard]] constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
97 };
103 struct current_row {
108  [[nodiscard]] constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
109 };
110 
114 using range_window_type = std::variant<unbounded, current_row, bounded_closed, bounded_open>;
115 
122  std::unique_ptr<rolling_aggregation> aggregation;
123 };
124 
141 std::pair<std::unique_ptr<column>, std::unique_ptr<column>> make_range_windows(
142  table_view const& group_keys,
143  column_view const& orderby,
144  order order,
146  range_window_type preceding,
147  range_window_type following,
150 
189 std::unique_ptr<column> rolling_window(
190  column_view const& input,
191  size_type preceding_window,
192  size_type following_window,
193  size_type min_periods,
194  rolling_aggregation const& agg,
197 
213 std::unique_ptr<column> rolling_window(
214  column_view const& input,
215  column_view const& default_outputs,
216  size_type preceding_window,
217  size_type following_window,
218  size_type min_periods,
219  rolling_aggregation const& agg,
222 
227  public:
234  static window_bounds get(size_type value) { return window_bounds(false, value); }
235 
242  {
243  return window_bounds(true, std::numeric_limits<cudf::size_type>::max());
244  }
245 
252  [[nodiscard]] bool is_unbounded() const { return _is_unbounded; }
253 
259  [[nodiscard]] size_type value() const { return _value; }
260 
261  private:
262  explicit window_bounds(bool is_unbounded_, size_type value_ = 0)
263  : _is_unbounded{is_unbounded_}, _value{value_}
264  {
265  }
266 
267  bool const _is_unbounded;
268  size_type const _value;
269 };
270 
364 std::unique_ptr<column> grouped_rolling_window(
365  table_view const& group_keys,
366  column_view const& input,
367  size_type preceding_window,
368  size_type following_window,
369  size_type min_periods,
370  rolling_aggregation const& aggr,
373 
386 std::unique_ptr<column> grouped_rolling_window(
387  table_view const& group_keys,
388  column_view const& input,
389  window_bounds preceding_window,
390  window_bounds following_window,
391  size_type min_periods,
392  rolling_aggregation const& aggr,
395 
412 std::unique_ptr<column> grouped_rolling_window(
413  table_view const& group_keys,
414  column_view const& input,
415  column_view const& default_outputs,
416  size_type preceding_window,
417  size_type following_window,
418  size_type min_periods,
419  rolling_aggregation const& aggr,
422 
436 std::unique_ptr<column> grouped_rolling_window(
437  table_view const& group_keys,
438  column_view const& input,
439  column_view const& default_outputs,
440  window_bounds preceding_window,
441  window_bounds following_window,
442  size_type min_periods,
443  rolling_aggregation const& aggr,
446 
558 std::unique_ptr<column> grouped_range_rolling_window(
559  table_view const& group_keys,
560  column_view const& orderby_column,
561  cudf::order const& order,
562  column_view const& input,
563  range_window_bounds const& preceding,
564  range_window_bounds const& following,
565  size_type min_periods,
566  rolling_aggregation const& aggr,
569 
585 std::unique_ptr<table> grouped_range_rolling_window(
586  table_view const& group_keys,
587  column_view const& orderby,
588  order order,
590  range_window_type preceding,
591  range_window_type following,
595 
618 std::unique_ptr<table> grouped_range_rolling_window(
619  table_view const& group_keys,
620  table_view const& orderby,
621  host_span<order const> orders,
622  host_span<null_order const> null_orders,
623  range_window_type preceding,
624  range_window_type following,
628 
675 std::unique_ptr<column> rolling_window(
676  column_view const& input,
677  column_view const& preceding_window,
678  column_view const& following_window,
679  size_type min_periods,
680  rolling_aggregation const& agg,
683 
691 bool is_valid_rolling_aggregation(data_type source, aggregation::Kind kind); // end of group
693 } // 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:278
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< 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:114
std::unique_ptr< table > grouped_range_rolling_window(table_view const &group_keys, table_view const &orderby, host_span< order const > orders, host_span< null_order const > null_orders, 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 > 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.
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:149
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:85
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:108
cuDF interfaces
Definition: host_udf.hpp:26
APIs for spans.
Strongly typed wrapper for bounded closed rolling windows.
Definition: rolling.hpp:39
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:54
bounded_closed(cudf::scalar const &delta)
Construct a bounded closed rolling window.
Definition: rolling.hpp:49
cudf::scalar const & delta_
Definition: rolling.hpp:40
Strongly typed wrapper for bounded open rolling windows.
Definition: rolling.hpp:67
cudf::scalar const & delta_
Definition: rolling.hpp:68
bounded_open(cudf::scalar const &delta)
Construct a bounded open rolling window.
Definition: rolling.hpp:77
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:83
Strongly typed wrapper for current_row rolling windows.
Definition: rolling.hpp:103
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:108
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:119
std::unique_ptr< rolling_aggregation > aggregation
Desired aggregation.
Definition: rolling.hpp:122
column_view values
Elements to aggregate.
Definition: rolling.hpp:120
size_type min_periods
Minimum number of observations required for the window to be valid.
Definition: rolling.hpp:121
Strongly typed wrapper for unbounded rolling windows.
Definition: rolling.hpp:91
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:96
Abstraction for window boundary sizes.
Definition: rolling.hpp:226
static window_bounds unbounded()
Construct unbounded window boundary.
Definition: rolling.hpp:241
size_type value() const
Gets the row-boundary for this window_bounds.
Definition: rolling.hpp:259
bool is_unbounded() const
Definition: rolling.hpp:252
static window_bounds get(size_type value)
Construct bounded window boundary.
Definition: rolling.hpp:234
Type declarations for libcudf.