rolling.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
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 <span>
21 #include <variant>
22 
23 namespace CUDF_EXPORT cudf {
44 
51  bounded_closed(cudf::scalar const& delta) : delta_{delta} {}
56  [[nodiscard]] cudf::scalar const* delta() const noexcept { return &delta_; }
57 };
58 
69 struct bounded_open {
72 
79  bounded_open(cudf::scalar const& delta) : delta_{delta} {}
80 
85  [[nodiscard]] cudf::scalar const* delta() const noexcept { return &delta_; }
86 };
87 
93 struct unbounded {
98  [[nodiscard]] constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
99 };
105 struct current_row {
110  [[nodiscard]] constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
111 };
112 
116 using range_window_type = std::variant<unbounded, current_row, bounded_closed, bounded_open>;
117 
124  std::unique_ptr<rolling_aggregation> aggregation;
125 };
126 
143 std::pair<std::unique_ptr<column>, std::unique_ptr<column>> make_range_windows(
144  table_view const& group_keys,
145  column_view const& orderby,
146  order order,
148  range_window_type preceding,
149  range_window_type following,
152 
191 std::unique_ptr<column> rolling_window(
192  column_view const& input,
193  size_type preceding_window,
194  size_type following_window,
195  size_type min_periods,
196  rolling_aggregation const& agg,
199 
215 std::unique_ptr<column> rolling_window(
216  column_view const& input,
217  column_view const& default_outputs,
218  size_type preceding_window,
219  size_type following_window,
220  size_type min_periods,
221  rolling_aggregation const& agg,
224 
229  public:
236  static window_bounds get(size_type value) { return window_bounds(false, value); }
237 
244  {
245  return window_bounds(true, std::numeric_limits<cudf::size_type>::max());
246  }
247 
254  [[nodiscard]] bool is_unbounded() const { return _is_unbounded; }
255 
261  [[nodiscard]] size_type value() const { return _value; }
262 
263  private:
264  explicit window_bounds(bool is_unbounded_, size_type value_ = 0)
265  : _is_unbounded{is_unbounded_}, _value{value_}
266  {
267  }
268 
269  bool const _is_unbounded;
270  size_type const _value;
271 };
272 
366 std::unique_ptr<column> grouped_rolling_window(
367  table_view const& group_keys,
368  column_view const& input,
369  size_type preceding_window,
370  size_type following_window,
371  size_type min_periods,
372  rolling_aggregation const& aggr,
375 
388 std::unique_ptr<column> grouped_rolling_window(
389  table_view const& group_keys,
390  column_view const& input,
391  window_bounds preceding_window,
392  window_bounds following_window,
393  size_type min_periods,
394  rolling_aggregation const& aggr,
397 
414 std::unique_ptr<column> grouped_rolling_window(
415  table_view const& group_keys,
416  column_view const& input,
417  column_view const& default_outputs,
418  size_type preceding_window,
419  size_type following_window,
420  size_type min_periods,
421  rolling_aggregation const& aggr,
424 
438 std::unique_ptr<column> grouped_rolling_window(
439  table_view const& group_keys,
440  column_view const& input,
441  column_view const& default_outputs,
442  window_bounds preceding_window,
443  window_bounds following_window,
444  size_type min_periods,
445  rolling_aggregation const& aggr,
448 
560 std::unique_ptr<column> grouped_range_rolling_window(
561  table_view const& group_keys,
562  column_view const& orderby_column,
563  cudf::order const& order,
564  column_view const& input,
565  range_window_bounds const& preceding,
566  range_window_bounds const& following,
567  size_type min_periods,
568  rolling_aggregation const& aggr,
571 
587 std::unique_ptr<table> grouped_range_rolling_window(
588  table_view const& group_keys,
589  column_view const& orderby,
590  order order,
592  range_window_type preceding,
593  range_window_type following,
594  std::span<rolling_request const> requests,
597 
620 std::unique_ptr<table> grouped_range_rolling_window(
621  table_view const& group_keys,
622  table_view const& orderby,
623  host_span<order const> orders,
624  host_span<null_order const> null_orders,
625  range_window_type preceding,
626  range_window_type following,
630 
677 std::unique_ptr<column> rolling_window(
678  column_view const& input,
679  column_view const& preceding_window,
680  column_view const& following_window,
681  size_type min_periods,
682  rolling_aggregation const& agg,
685 
693 bool is_valid_rolling_aggregation(data_type source, aggregation::Kind kind); // end of group
695 } // 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:287
Derived class intended for rolling_window specific aggregation usage.
An owning class to represent a singular value.
Definition: scalar.hpp:41
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:189
APIs for querying the default CUDA stream and per-thread default stream status.
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:116
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
APIs for getting and setting the current device memory resource.
cuDF interfaces
Definition: host_udf.hpp:26
Class defining range-based window boundary sizes for rolling window operations.
APIs for spans.
Strongly typed wrapper for bounded closed rolling windows.
Definition: rolling.hpp:41
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:56
bounded_closed(cudf::scalar const &delta)
Construct a bounded closed rolling window.
Definition: rolling.hpp:51
cudf::scalar const & delta_
Definition: rolling.hpp:42
Strongly typed wrapper for bounded open rolling windows.
Definition: rolling.hpp:69
cudf::scalar const & delta_
Definition: rolling.hpp:70
bounded_open(cudf::scalar const &delta)
Construct a bounded open rolling window.
Definition: rolling.hpp:79
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:85
Strongly typed wrapper for current_row rolling windows.
Definition: rolling.hpp:105
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:110
Host span, a non-owning view over a contiguous sequence of host-accessible elements.
Definition: span.hpp:62
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:121
std::unique_ptr< rolling_aggregation > aggregation
Desired aggregation.
Definition: rolling.hpp:124
column_view values
Elements to aggregate.
Definition: rolling.hpp:122
size_type min_periods
Minimum number of observations required for the window to be valid.
Definition: rolling.hpp:123
Strongly typed wrapper for unbounded rolling windows.
Definition: rolling.hpp:93
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:98
Abstraction for window boundary sizes.
Definition: rolling.hpp:228
static window_bounds unbounded()
Construct unbounded window boundary.
Definition: rolling.hpp:243
size_type value() const
Gets the row-boundary for this window_bounds.
Definition: rolling.hpp:261
bool is_unbounded() const
Definition: rolling.hpp:254
static window_bounds get(size_type value)
Construct bounded window boundary.
Definition: rolling.hpp:236
Type declarations for libcudf.