scalar.hpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 #pragma once
6 
7 #include <cudf/column/column.hpp>
8 #include <cudf/detail/device_scalar.hpp>
9 #include <cudf/table/table.hpp>
10 #include <cudf/types.hpp>
14 
15 #include <rmm/cuda_stream_view.hpp>
16 #include <rmm/device_buffer.hpp>
17 #include <rmm/device_scalar.hpp>
18 
19 #include <string_view>
20 
26 namespace CUDF_EXPORT cudf {
40 class scalar {
41  public:
42  scalar() = delete;
43  virtual ~scalar() = default;
44  scalar& operator=(scalar const& other) = delete;
45  scalar& operator=(scalar&& other) = delete;
46 
52  [[nodiscard]] data_type type() const noexcept;
53 
60  void set_valid_async(bool is_valid, rmm::cuda_stream_view stream = cudf::get_default_stream());
61 
72  [[nodiscard]] bool is_valid(rmm::cuda_stream_view stream = cudf::get_default_stream()) const;
73 
79  bool* validity_data();
80 
86  [[nodiscard]] bool const* validity_data() const;
87 
88  protected:
89  data_type _type{type_id::EMPTY};
90  cudf::detail::device_scalar<bool> _is_valid;
91 
96  scalar(scalar&& other) = default;
97 
105  scalar(scalar const& other,
108 
121  bool is_valid = false,
124 };
125 
126 namespace detail {
132 template <typename T>
133 class fixed_width_scalar : public scalar {
134  static_assert(is_fixed_width<T>(), "Unexpected non-fixed-width type.");
135 
136  public:
137  using value_type = T;
138 
139  fixed_width_scalar() = delete;
140  ~fixed_width_scalar() override = default;
141 
147 
148  fixed_width_scalar& operator=(fixed_width_scalar const& other) = delete;
149  fixed_width_scalar& operator=(fixed_width_scalar&& other) = delete;
150 
161 
169 
176  [[nodiscard]] T value(rmm::cuda_stream_view stream = cudf::get_default_stream()) const;
177 
182  T* data();
183 
188  [[nodiscard]] T const* data() const;
189 
190  protected:
192 
202  bool is_valid = true,
205 
215  bool is_valid = true,
218 };
219 
220 } // namespace detail
221 
227 template <typename T>
229  static_assert(is_numeric<T>(), "Unexpected non-numeric type.");
230 
231  public:
232  numeric_scalar() = delete;
233  ~numeric_scalar() override = default;
234 
239  numeric_scalar(numeric_scalar&& other) = default;
240 
241  numeric_scalar& operator=(numeric_scalar const& other) = delete;
242  numeric_scalar& operator=(numeric_scalar&& other) = delete;
243 
254 
263  numeric_scalar(T value,
264  bool is_valid = true,
267 
277  bool is_valid = true,
280 };
281 
287 template <typename T>
288 class fixed_point_scalar : public scalar {
289  static_assert(is_fixed_point<T>(), "Unexpected non-fixed_point type.");
290 
291  public:
292  using rep_type = typename T::rep;
293  using value_type = T;
294 
295  fixed_point_scalar() = delete;
296  ~fixed_point_scalar() override = default;
297 
303 
304  fixed_point_scalar& operator=(fixed_point_scalar const& other) = delete;
305  fixed_point_scalar& operator=(fixed_point_scalar&& other) = delete;
306 
317 
328  numeric::scale_type scale,
329  bool is_valid = true,
332 
342  bool is_valid = true,
345 
355  bool is_valid = true,
358 
369  numeric::scale_type scale,
370  bool is_valid = true,
373 
381 
388  [[nodiscard]] T fixed_point_value(
390 
396 
401  [[nodiscard]] rep_type const* data() const;
402 
403  protected:
405 };
406 
410 class string_scalar : public scalar {
411  public:
413 
414  string_scalar() = delete;
415  ~string_scalar() override = default;
416 
421  string_scalar(string_scalar&& other) = default;
422 
423  // string_scalar(string_scalar const& other) = delete;
424  string_scalar& operator=(string_scalar const& other) = delete;
425  string_scalar& operator=(string_scalar&& other) = delete;
426 
437 
448  string_scalar(std::string_view string,
449  bool is_valid = true,
452 
463  string_scalar(value_type const& source,
464  bool is_valid = true,
467 
479  bool is_valid = true,
482 
495  bool is_valid = true,
498 
505  [[nodiscard]] std::string to_string(
507 
515 
520  [[nodiscard]] size_type size() const;
521 
526  [[nodiscard]] char const* data() const;
527 
528  protected:
530 };
531 
538 template <typename T>
540  static_assert(is_chrono<T>(), "Unexpected non-chrono type");
541 
542  public:
543  chrono_scalar() = delete;
544  ~chrono_scalar() override = default;
545 
550  chrono_scalar(chrono_scalar&& other) = default;
551 
552  chrono_scalar& operator=(chrono_scalar const& other) = delete;
553  chrono_scalar& operator=(chrono_scalar&& other) = delete;
554 
565 
574  chrono_scalar(T value,
575  bool is_valid = true,
578 
588  bool is_valid = true,
591 };
592 
599 template <typename T>
600 class timestamp_scalar : public chrono_scalar<T> {
601  public:
602  static_assert(is_timestamp<T>(), "Unexpected non-timestamp type");
604  using rep_type = typename T::rep;
605 
606  timestamp_scalar() = delete;
607 
612  timestamp_scalar(timestamp_scalar&& other) = default;
613 
624 
635  template <typename Duration2>
636  timestamp_scalar(Duration2 const& value,
637  bool is_valid,
640 
647 };
648 
655 template <typename T>
656 class duration_scalar : public chrono_scalar<T> {
657  public:
658  static_assert(is_duration<T>(), "Unexpected non-duration type");
660  using rep_type = typename T::rep;
661 
662  duration_scalar() = delete;
663 
668  duration_scalar(duration_scalar&& other) = default;
669 
680 
690  bool is_valid,
693 
700 };
701 
705 class list_scalar : public scalar {
706  public:
707  list_scalar() = delete;
708  ~list_scalar() override = default;
709 
714  list_scalar(list_scalar&& other) = default;
715 
716  list_scalar& operator=(list_scalar const& other) = delete;
717  list_scalar& operator=(list_scalar&& other) = delete;
718 
726  list_scalar(list_scalar const& other,
729 
741  bool is_valid = true,
744 
754  bool is_valid = true,
757 
762  [[nodiscard]] column_view view() const;
763 
764  private:
765  cudf::column _data;
766 };
767 
771 class struct_scalar : public scalar {
772  public:
773  struct_scalar() = delete;
774  ~struct_scalar() override = default;
775 
780  struct_scalar(struct_scalar&& other) = default;
781  struct_scalar& operator=(struct_scalar const& other) = delete;
782  struct_scalar& operator=(struct_scalar&& other) = delete;
783 
794 
806  bool is_valid = true,
809 
821  bool is_valid = true,
824 
837  bool is_valid = true,
840 
845  [[nodiscard]] table_view view() const;
846 
847  private:
848  table _data;
849 
853  void assert_valid_size();
854 
864  static table init_data(table&& data,
865  bool is_valid,
866  rmm::cuda_stream_view stream,
868 };
869  // end of group
871 } // namespace CUDF_EXPORT cudf
An owning class to represent a timestamp/duration value in device memory.
Definition: scalar.hpp:539
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:36
Indicator for the logical data type of an element in a column.
Definition: types.hpp:238
An owning class to represent a fixed-width type value in device memory.
Definition: scalar.hpp:133
rmm::device_scalar< T > _data
device memory containing the value
Definition: scalar.hpp:191
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:137
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:656
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:660
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:288
rmm::device_scalar< rep_type > _data
device memory containing the value
Definition: scalar.hpp:404
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:293
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:292
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:705
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:228
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:40
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:90
An owning class to represent a string in device memory.
Definition: scalar.hpp:410
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:33
An owning class to represent a struct value in device memory.
Definition: scalar.hpp:771
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:189
A set of cudf::column's of the same size.
Definition: table.hpp:29
An owning class to represent a timestamp value in device memory.
Definition: scalar.hpp:600
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:604
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:33
rmm::device_async_resource_ref get_current_device_resource_ref()
Get the current device memory resource reference.
detail::cccl_async_resource_ref< cuda::mr::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:84
cuDF interfaces
Definition: host_udf.hpp:26
C++20 std::span with reduced feature set.
Definition: span.hpp:182
Class definition for cudf::table.
Type declarations for libcudf.