scalar.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 #pragma once
17 
18 #include <cudf/column/column.hpp>
19 #include <cudf/detail/device_scalar.hpp>
20 #include <cudf/table/table.hpp>
21 #include <cudf/types.hpp>
25 
26 #include <rmm/cuda_stream_view.hpp>
27 #include <rmm/device_buffer.hpp>
28 #include <rmm/device_scalar.hpp>
29 
30 #include <string_view>
31 
37 namespace CUDF_EXPORT cudf {
51 class scalar {
52  public:
53  scalar() = delete;
54  virtual ~scalar() = default;
55  scalar& operator=(scalar const& other) = delete;
56  scalar& operator=(scalar&& other) = delete;
57 
63  [[nodiscard]] data_type type() const noexcept;
64 
71  void set_valid_async(bool is_valid, rmm::cuda_stream_view stream = cudf::get_default_stream());
72 
83  [[nodiscard]] bool is_valid(rmm::cuda_stream_view stream = cudf::get_default_stream()) const;
84 
90  bool* validity_data();
91 
97  [[nodiscard]] bool const* validity_data() const;
98 
99  protected:
100  data_type _type{type_id::EMPTY};
101  cudf::detail::device_scalar<bool> _is_valid;
102 
107  scalar(scalar&& other) = default;
108 
116  scalar(scalar const& other,
119 
132  bool is_valid = false,
135 };
136 
137 namespace detail {
143 template <typename T>
144 class fixed_width_scalar : public scalar {
145  static_assert(is_fixed_width<T>(), "Unexpected non-fixed-width type.");
146 
147  public:
148  using value_type = T;
149 
150  fixed_width_scalar() = delete;
151  ~fixed_width_scalar() override = default;
152 
158 
159  fixed_width_scalar& operator=(fixed_width_scalar const& other) = delete;
160  fixed_width_scalar& operator=(fixed_width_scalar&& other) = delete;
161 
172 
180 
187  [[nodiscard]] T value(rmm::cuda_stream_view stream = cudf::get_default_stream()) const;
188 
193  T* data();
194 
199  [[nodiscard]] T const* data() const;
200 
201  protected:
203 
213  bool is_valid = true,
216 
226  bool is_valid = true,
229 };
230 
231 } // namespace detail
232 
238 template <typename T>
240  static_assert(is_numeric<T>(), "Unexpected non-numeric type.");
241 
242  public:
243  numeric_scalar() = delete;
244  ~numeric_scalar() override = default;
245 
250  numeric_scalar(numeric_scalar&& other) = default;
251 
252  numeric_scalar& operator=(numeric_scalar const& other) = delete;
253  numeric_scalar& operator=(numeric_scalar&& other) = delete;
254 
265 
274  numeric_scalar(T value,
275  bool is_valid = true,
278 
288  bool is_valid = true,
291 };
292 
298 template <typename T>
299 class fixed_point_scalar : public scalar {
300  static_assert(is_fixed_point<T>(), "Unexpected non-fixed_point type.");
301 
302  public:
303  using rep_type = typename T::rep;
304  using value_type = T;
305 
306  fixed_point_scalar() = delete;
307  ~fixed_point_scalar() override = default;
308 
314 
315  fixed_point_scalar& operator=(fixed_point_scalar const& other) = delete;
316  fixed_point_scalar& operator=(fixed_point_scalar&& other) = delete;
317 
328 
339  numeric::scale_type scale,
340  bool is_valid = true,
343 
353  bool is_valid = true,
356 
366  bool is_valid = true,
369 
380  numeric::scale_type scale,
381  bool is_valid = true,
384 
392 
399  [[nodiscard]] T fixed_point_value(
401 
407 
412  [[nodiscard]] rep_type const* data() const;
413 
414  protected:
416 };
417 
421 class string_scalar : public scalar {
422  public:
424 
425  string_scalar() = delete;
426  ~string_scalar() override = default;
427 
432  string_scalar(string_scalar&& other) = default;
433 
434  // string_scalar(string_scalar const& other) = delete;
435  string_scalar& operator=(string_scalar const& other) = delete;
436  string_scalar& operator=(string_scalar&& other) = delete;
437 
448 
459  string_scalar(std::string_view string,
460  bool is_valid = true,
463 
474  string_scalar(value_type const& source,
475  bool is_valid = true,
478 
490  bool is_valid = true,
493 
506  bool is_valid = true,
509 
516  [[nodiscard]] std::string to_string(
518 
526 
531  [[nodiscard]] size_type size() const;
532 
537  [[nodiscard]] char const* data() const;
538 
539  protected:
541 };
542 
549 template <typename T>
551  static_assert(is_chrono<T>(), "Unexpected non-chrono type");
552 
553  public:
554  chrono_scalar() = delete;
555  ~chrono_scalar() override = default;
556 
561  chrono_scalar(chrono_scalar&& other) = default;
562 
563  chrono_scalar& operator=(chrono_scalar const& other) = delete;
564  chrono_scalar& operator=(chrono_scalar&& other) = delete;
565 
576 
585  chrono_scalar(T value,
586  bool is_valid = true,
589 
599  bool is_valid = true,
602 };
603 
610 template <typename T>
611 class timestamp_scalar : public chrono_scalar<T> {
612  public:
613  static_assert(is_timestamp<T>(), "Unexpected non-timestamp type");
615  using rep_type = typename T::rep;
616 
617  timestamp_scalar() = delete;
618 
623  timestamp_scalar(timestamp_scalar&& other) = default;
624 
635 
646  template <typename Duration2>
647  timestamp_scalar(Duration2 const& value,
648  bool is_valid,
651 
658 };
659 
666 template <typename T>
667 class duration_scalar : public chrono_scalar<T> {
668  public:
669  static_assert(is_duration<T>(), "Unexpected non-duration type");
671  using rep_type = typename T::rep;
672 
673  duration_scalar() = delete;
674 
679  duration_scalar(duration_scalar&& other) = default;
680 
691 
701  bool is_valid,
704 
711 };
712 
716 class list_scalar : public scalar {
717  public:
718  list_scalar() = delete;
719  ~list_scalar() override = default;
720 
725  list_scalar(list_scalar&& other) = default;
726 
727  list_scalar& operator=(list_scalar const& other) = delete;
728  list_scalar& operator=(list_scalar&& other) = delete;
729 
737  list_scalar(list_scalar const& other,
740 
752  bool is_valid = true,
755 
765  bool is_valid = true,
768 
773  [[nodiscard]] column_view view() const;
774 
775  private:
776  cudf::column _data;
777 };
778 
782 class struct_scalar : public scalar {
783  public:
784  struct_scalar() = delete;
785  ~struct_scalar() override = default;
786 
791  struct_scalar(struct_scalar&& other) = default;
792  struct_scalar& operator=(struct_scalar const& other) = delete;
793  struct_scalar& operator=(struct_scalar&& other) = delete;
794 
805 
817  bool is_valid = true,
820 
832  bool is_valid = true,
835 
848  bool is_valid = true,
851 
856  [[nodiscard]] table_view view() const;
857 
858  private:
859  table _data;
860 
864  void assert_valid_size();
865 
875  static table init_data(table&& data,
876  bool is_valid,
877  rmm::cuda_stream_view stream,
879 };
880  // end of group
882 } // namespace CUDF_EXPORT cudf
An owning class to represent a timestamp/duration value in device memory.
Definition: scalar.hpp:550
chrono_scalar(T value, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new chrono scalar object.
chrono_scalar(rmm::device_scalar< T > &&data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new chrono scalar object from existing device memory.
chrono_scalar(chrono_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new chrono scalar object by deep copying another.
chrono_scalar(chrono_scalar &&other)=default
Move constructor for chrono_scalar.
A non-owning, immutable view of device data as a column of elements, some of which may be null as ind...
A container of nullable device data as a column of elements.
Definition: column.hpp:47
Indicator for the logical data type of an element in a column.
Definition: types.hpp:243
An owning class to represent a fixed-width type value in device memory.
Definition: scalar.hpp:144
rmm::device_scalar< T > _data
device memory containing the value
Definition: scalar.hpp:202
fixed_width_scalar(fixed_width_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new fixed-width scalar object by deep copying another.
void set_value(T value, rmm::cuda_stream_view stream=cudf::get_default_stream())
Set the value of the scalar.
T value(rmm::cuda_stream_view stream=cudf::get_default_stream()) const
Get the value of the scalar.
fixed_width_scalar(fixed_width_scalar &&other)=default
Move constructor for fixed_width_scalar.
fixed_width_scalar(T value, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new fixed width scalar object.
fixed_width_scalar(rmm::device_scalar< T > &&data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new fixed width scalar object from existing device memory.
T const * data() const
Returns a const raw pointer to the value in device memory.
T value_type
Type of the value held by the scalar.
Definition: scalar.hpp:148
T * data()
Returns a raw pointer to the value in device memory.
An owning class to represent a duration value in device memory.
Definition: scalar.hpp:667
rep_type count(rmm::cuda_stream_view stream)
Returns the duration in number of ticks.
duration_scalar(duration_scalar &&other)=default
Move constructor for duration_scalar.
typename T::rep rep_type
The duration's underlying representation type.
Definition: scalar.hpp:671
duration_scalar(duration_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new duration scalar object by deep copying another.
duration_scalar(rep_type value, bool is_valid, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new duration scalar object from tick counts.
An owning class to represent a fixed_point number in device memory.
Definition: scalar.hpp:299
rmm::device_scalar< rep_type > _data
device memory containing the value
Definition: scalar.hpp:415
fixed_point_scalar(fixed_point_scalar &&other)=default
Move constructor for fixed_point_scalar.
fixed_point_scalar(T value, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new fixed_point scalar object from a fixed_point number.
fixed_point_scalar(rep_type value, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new fixed_point scalar object from a value and default 0-scale.
fixed_point_scalar(rmm::device_scalar< rep_type > &&data, numeric::scale_type scale, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new fixed_point scalar object from existing device memory.
rep_type value(rmm::cuda_stream_view stream=cudf::get_default_stream()) const
Get the value of the scalar.
T value_type
The value type of the fixed_point number.
Definition: scalar.hpp:304
fixed_point_scalar(rep_type value, numeric::scale_type scale, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new fixed_point scalar object from already shifted value and scale.
rep_type const * data() const
Returns a const raw pointer to the value in device memory.
T fixed_point_value(rmm::cuda_stream_view stream=cudf::get_default_stream()) const
Get the decimal32, decimal64 or decimal128.
rep_type * data()
Returns a raw pointer to the value in device memory.
typename T::rep rep_type
The representation type of the fixed_point number.
Definition: scalar.hpp:303
fixed_point_scalar(fixed_point_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new fixed_point scalar object by deep copying another.
An owning class to represent a list value in device memory.
Definition: scalar.hpp:716
column_view view() const
Returns a non-owning, immutable view to underlying device data.
list_scalar(list_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new list scalar object by deep copying another.
list_scalar(cudf::column_view const &data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new list scalar object from column_view.
list_scalar(list_scalar &&other)=default
Move constructor for list_scalar.
list_scalar(cudf::column &&data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new list scalar object from existing column.
An owning class to represent a numerical value in device memory.
Definition: scalar.hpp:239
numeric_scalar(rmm::device_scalar< T > &&data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new numeric scalar object from existing device memory.
numeric_scalar(T value, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new numeric scalar object.
numeric_scalar(numeric_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new numeric scalar object by deep copying another.
numeric_scalar(numeric_scalar &&other)=default
Move constructor for numeric_scalar.
An owning class to represent a singular value.
Definition: scalar.hpp:51
scalar(scalar &&other)=default
Move constructor for scalar.
data_type type() const noexcept
Returns the scalar's logical value type.
scalar(scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new scalar object by deep copying another.
scalar(data_type type, bool is_valid=false, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new scalar object.
cudf::detail::device_scalar< bool > _is_valid
Device bool signifying validity.
Definition: scalar.hpp:101
An owning class to represent a string in device memory.
Definition: scalar.hpp:421
std::string to_string(rmm::cuda_stream_view stream=cudf::get_default_stream()) const
Get the value of the scalar in a host std::string.
string_scalar(value_type const &source, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new string scalar object from string_view.
string_scalar(rmm::device_scalar< value_type > &data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new string scalar object from string_view in device memory.
size_type size() const
Returns the size of the string in bytes.
string_scalar(string_scalar &&other)=default
Move constructor for string_scalar.
string_scalar(string_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new string scalar object by deep copying another string_scalar.
string_scalar(rmm::device_buffer &&data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new string scalar object by moving an existing string data buffer.
string_scalar(std::string_view string, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new string scalar object.
value_type value(rmm::cuda_stream_view stream=cudf::get_default_stream()) const
Get the value of the scalar as a string_view.
char const * data() const
Returns a raw pointer to the string in device memory.
A non-owning, immutable view of device data that is a variable length char array representing a UTF-8...
Definition: string_view.hpp:44
An owning class to represent a struct value in device memory.
Definition: scalar.hpp:782
struct_scalar(host_span< column_view const > data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new struct scalar object from a host_span of column_views.
table_view view() const
Returns a non-owning, immutable view to underlying device data.
struct_scalar(table &&data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new struct scalar object from an existing table in device memory.
struct_scalar(table_view const &data, bool is_valid=true, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new struct scalar object from table_view.
struct_scalar(struct_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new struct scalar object by deep copying another.
struct_scalar(struct_scalar &&other)=default
Move constructor for struct_scalar.
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:200
A set of cudf::column's of the same size.
Definition: table.hpp:40
An owning class to represent a timestamp value in device memory.
Definition: scalar.hpp:611
timestamp_scalar(timestamp_scalar &&other)=default
Move constructor for timestamp_scalar.
typename T::rep rep_type
The underlying representation type of the timestamp.
Definition: scalar.hpp:615
rep_type ticks_since_epoch(rmm::cuda_stream_view stream)
Returns the duration in number of ticks since the UNIX epoch.
timestamp_scalar(Duration2 const &value, bool is_valid, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new timestamp scalar object from a duration that is convertible to T::duration.
timestamp_scalar(timestamp_scalar const &other, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new timestamp scalar object by deep copying another.
Class definition for cudf::column.
rmm::cuda_stream_view const get_default_stream()
Get the current default stream.
scale_type
The scale type for fixed_point.
Definition: fixed_point.hpp:43
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
std::unique_ptr< cudf::column > is_valid(cudf::column_view const &input, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Creates a column of type_id::BOOL8 elements where for every element in input true indicates the value...
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:95
cuDF interfaces
Definition: host_udf.hpp:37
C++20 std::span with reduced feature set.
Definition: span.hpp:194
Class definition for cudf::table.
Type declarations for libcudf.