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 
28 namespace CUDF_EXPORT cudf {
47 
54  bounded_closed(cudf::scalar const& delta) : delta_{delta} {}
59  [[nodiscard]] cudf::scalar const* delta() const noexcept { return &delta_; }
60 };
61 
72 struct bounded_open {
75 
82  bounded_open(cudf::scalar const& delta) : delta_{delta} {}
83 
88  [[nodiscard]] cudf::scalar const* delta() const noexcept { return &delta_; }
89 };
90 
96 struct unbounded {
101  [[nodiscard]] constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
102 };
108 struct current_row {
113  [[nodiscard]] constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
114 };
115 
119 using range_window_type = std::variant<unbounded, current_row, bounded_closed, bounded_open>;
120 
127  std::unique_ptr<rolling_aggregation> aggregation;
128 };
129 
146 std::pair<std::unique_ptr<column>, std::unique_ptr<column>> make_range_windows(
147  table_view const& group_keys,
148  column_view const& orderby,
149  order order,
151  range_window_type preceding,
152  range_window_type following,
155 
194 std::unique_ptr<column> rolling_window(
195  column_view const& input,
196  size_type preceding_window,
197  size_type following_window,
198  size_type min_periods,
199  rolling_aggregation const& agg,
202 
218 std::unique_ptr<column> rolling_window(
219  column_view const& input,
220  column_view const& default_outputs,
221  size_type preceding_window,
222  size_type following_window,
223  size_type min_periods,
224  rolling_aggregation const& agg,
227 
232  public:
239  static window_bounds get(size_type value) { return window_bounds(false, value); }
240 
247  {
248  return window_bounds(true, std::numeric_limits<cudf::size_type>::max());
249  }
250 
257  [[nodiscard]] bool is_unbounded() const { return _is_unbounded; }
258 
264  [[nodiscard]] size_type value() const { return _value; }
265 
266  private:
267  explicit window_bounds(bool is_unbounded_, size_type value_ = 0)
268  : _is_unbounded{is_unbounded_}, _value{value_}
269  {
270  }
271 
272  bool const _is_unbounded;
273  size_type const _value;
274 };
275 
369 std::unique_ptr<column> grouped_rolling_window(
370  table_view const& group_keys,
371  column_view const& input,
372  size_type preceding_window,
373  size_type following_window,
374  size_type min_periods,
375  rolling_aggregation const& aggr,
378 
391 std::unique_ptr<column> grouped_rolling_window(
392  table_view const& group_keys,
393  column_view const& input,
394  window_bounds preceding_window,
395  window_bounds following_window,
396  size_type min_periods,
397  rolling_aggregation const& aggr,
400 
417 std::unique_ptr<column> grouped_rolling_window(
418  table_view const& group_keys,
419  column_view const& input,
420  column_view const& default_outputs,
421  size_type preceding_window,
422  size_type following_window,
423  size_type min_periods,
424  rolling_aggregation const& aggr,
427 
441 std::unique_ptr<column> grouped_rolling_window(
442  table_view const& group_keys,
443  column_view const& input,
444  column_view const& default_outputs,
445  window_bounds preceding_window,
446  window_bounds following_window,
447  size_type min_periods,
448  rolling_aggregation const& aggr,
451 
563 std::unique_ptr<column> grouped_range_rolling_window(
564  table_view const& group_keys,
565  column_view const& orderby_column,
566  cudf::order const& order,
567  column_view const& input,
568  range_window_bounds const& preceding,
569  range_window_bounds const& following,
570  size_type min_periods,
571  rolling_aggregation const& aggr,
574 
590 std::unique_ptr<table> grouped_range_rolling_window(
591  table_view const& group_keys,
592  column_view const& orderby,
593  order order,
595  range_window_type preceding,
596  range_window_type following,
597  std::span<rolling_request const> requests,
600 
623 std::unique_ptr<table> grouped_range_rolling_window(
624  table_view const& group_keys,
625  table_view const& orderby,
626  host_span<order const> orders,
627  host_span<null_order const> null_orders,
628  range_window_type preceding,
629  range_window_type following,
633 
680 std::unique_ptr<column> rolling_window(
681  column_view const& input,
682  column_view const& preceding_window,
683  column_view const& following_window,
684  size_type min_periods,
685  rolling_aggregation const& agg,
688 
696 bool is_valid_rolling_aggregation(data_type source, aggregation::Kind kind); // end of group
698 } // namespace CUDF_EXPORT cudf
Representation for specifying desired aggregations from aggregation-based APIs, e....
Kind
Possible aggregation operations.
Definition: aggregation.hpp:78
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:41
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:206
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:119
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:140
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:76
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:99
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:44
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:59
bounded_closed(cudf::scalar const &delta)
Construct a bounded closed rolling window.
Definition: rolling.hpp:54
cudf::scalar const & delta_
Definition: rolling.hpp:45
Strongly typed wrapper for bounded open rolling windows.
Definition: rolling.hpp:72
cudf::scalar const & delta_
Definition: rolling.hpp:73
bounded_open(cudf::scalar const &delta)
Construct a bounded open rolling window.
Definition: rolling.hpp:82
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:88
Strongly typed wrapper for current_row rolling windows.
Definition: rolling.hpp:108
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:113
Host span, a non-owning view over a contiguous sequence of host-accessible elements.
Definition: span.hpp:65
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:124
std::unique_ptr< rolling_aggregation > aggregation
Desired aggregation.
Definition: rolling.hpp:127
column_view values
Elements to aggregate.
Definition: rolling.hpp:125
size_type min_periods
Minimum number of observations required for the window to be valid.
Definition: rolling.hpp:126
Strongly typed wrapper for unbounded rolling windows.
Definition: rolling.hpp:96
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:101
Abstraction for window boundary sizes.
Definition: rolling.hpp:231
static window_bounds unbounded()
Construct unbounded window boundary.
Definition: rolling.hpp:246
size_type value() const
Gets the row-boundary for this window_bounds.
Definition: rolling.hpp:264
bool is_unbounded() const
Definition: rolling.hpp:257
static window_bounds get(size_type value)
Construct bounded window boundary.
Definition: rolling.hpp:239
Type declarations for libcudf.