rolling.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <cudf/aggregation.hpp>
21 #include <cudf/types.hpp>
23 #include <cudf/utilities/export.hpp>
25 
26 #include <rmm/resource_ref.hpp>
27 
28 #include <memory>
29 #include <optional>
30 #include <variant>
31 
32 namespace CUDF_EXPORT cudf {
52 
57  cudf::scalar const* delta() const noexcept { return &delta_; }
58 };
59 
70 struct bounded_open {
75 
80  cudf::scalar const* delta() const noexcept { return &delta_; }
81 };
82 
88 struct unbounded {
93  constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
94 };
100 struct current_row {
105  constexpr cudf::scalar const* delta() const noexcept { return nullptr; }
106 };
107 
111 using range_window_type = std::variant<unbounded, current_row, bounded_closed, bounded_open>;
112 
129 std::pair<std::unique_ptr<column>, std::unique_ptr<column>> make_range_windows(
130  table_view const& group_keys,
131  column_view const& orderby,
132  order order,
134  range_window_type preceding,
135  range_window_type following,
138 
172 std::unique_ptr<column> rolling_window(
173  column_view const& input,
174  size_type preceding_window,
175  size_type following_window,
176  size_type min_periods,
177  rolling_aggregation const& agg,
180 
196 std::unique_ptr<column> rolling_window(
197  column_view const& input,
198  column_view const& default_outputs,
199  size_type preceding_window,
200  size_type following_window,
201  size_type min_periods,
202  rolling_aggregation const& agg,
205 
210  public:
217  static window_bounds get(size_type value) { return window_bounds(false, value); }
218 
225  {
226  return window_bounds(true, std::numeric_limits<cudf::size_type>::max());
227  }
228 
235  [[nodiscard]] bool is_unbounded() const { return _is_unbounded; }
236 
242  [[nodiscard]] size_type value() const { return _value; }
243 
244  private:
245  explicit window_bounds(bool is_unbounded_, size_type value_ = 0)
246  : _is_unbounded{is_unbounded_}, _value{value_}
247  {
248  }
249 
250  bool const _is_unbounded;
251  size_type const _value;
252 };
253 
346 std::unique_ptr<column> grouped_rolling_window(
347  table_view const& group_keys,
348  column_view const& input,
349  size_type preceding_window,
350  size_type following_window,
351  size_type min_periods,
352  rolling_aggregation const& aggr,
355 
368 std::unique_ptr<column> grouped_rolling_window(
369  table_view const& group_keys,
370  column_view const& input,
371  window_bounds preceding_window,
372  window_bounds following_window,
373  size_type min_periods,
374  rolling_aggregation const& aggr,
377 
394 std::unique_ptr<column> grouped_rolling_window(
395  table_view const& group_keys,
396  column_view const& input,
397  column_view const& default_outputs,
398  size_type preceding_window,
399  size_type following_window,
400  size_type min_periods,
401  rolling_aggregation const& aggr,
404 
418 std::unique_ptr<column> grouped_rolling_window(
419  table_view const& group_keys,
420  column_view const& input,
421  column_view const& default_outputs,
422  window_bounds preceding_window,
423  window_bounds following_window,
424  size_type min_periods,
425  rolling_aggregation const& aggr,
428 
539 std::unique_ptr<column> grouped_range_rolling_window(
540  table_view const& group_keys,
541  column_view const& orderby_column,
542  cudf::order const& order,
543  column_view const& input,
544  range_window_bounds const& preceding,
545  range_window_bounds const& following,
546  size_type min_periods,
547  rolling_aggregation const& aggr,
550 
592 std::unique_ptr<column> rolling_window(
593  column_view const& input,
594  column_view const& preceding_window,
595  column_view const& following_window,
596  size_type min_periods,
597  rolling_aggregation const& agg,
600  // end of group
602 } // namespace CUDF_EXPORT cudf
Representation for specifying desired aggregations from aggregation-based APIs, e....
A non-owning, immutable view of device data as a column of elements, some of which may be null as ind...
Derived class intended for rolling_window specific aggregation usage.
An owning class to represent a singular value.
Definition: scalar.hpp:51
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:200
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.
std::variant< unbounded, current_row, bounded_closed, bounded_open > range_window_type
The type of the range-based rolling window endpoint.
Definition: rolling.hpp:111
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.
std::unique_ptr< column > grouped_range_rolling_window(table_view const &group_keys, column_view const &orderby_column, cudf::order const &order, column_view const &input, range_window_bounds const &preceding, range_window_bounds const &following, 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, value range-based 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::async_resource_ref< cuda::mr::device_accessible > device_async_resource_ref
null_order
Indicates how null values compare against all other values.
Definition: types.hpp:159
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:95
order
Indicates the order in which elements should be sorted.
Definition: types.hpp:118
cuDF interfaces
Definition: host_udf.hpp:37
Strongly typed wrapper for bounded closed rolling windows.
Definition: rolling.hpp:49
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:57
cudf::scalar const & delta_
Definition: rolling.hpp:50
Strongly typed wrapper for bounded open rolling windows.
Definition: rolling.hpp:70
cudf::scalar const & delta_
Definition: rolling.hpp:71
cudf::scalar const * delta() const noexcept
Return pointer to the row delta scalar.
Definition: rolling.hpp:80
Strongly typed wrapper for current_row rolling windows.
Definition: rolling.hpp:100
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:105
Abstraction for window boundary sizes, to be used with grouped_range_rolling_window().
Strongly typed wrapper for unbounded rolling windows.
Definition: rolling.hpp:88
constexpr cudf::scalar const * delta() const noexcept
Return a null row delta.
Definition: rolling.hpp:93
Abstraction for window boundary sizes.
Definition: rolling.hpp:209
static window_bounds unbounded()
Construct unbounded window boundary.
Definition: rolling.hpp:224
size_type value() const
Gets the row-boundary for this window_bounds.
Definition: rolling.hpp:242
bool is_unbounded() const
Definition: rolling.hpp:235
static window_bounds get(size_type value)
Construct bounded window boundary.
Definition: rolling.hpp:217
Type declarations for libcudf.