scalar.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 #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 <span>
20 #include <string_view>
21 
27 namespace CUDF_EXPORT cudf {
41 class scalar {
42  public:
43  scalar() = delete;
44  virtual ~scalar() = default;
45  scalar& operator=(scalar const& other) = delete;
46  scalar& operator=(scalar&& other) = delete;
47 
53  [[nodiscard]] data_type type() const noexcept;
54 
61  void set_valid_async(bool is_valid, rmm::cuda_stream_view stream = cudf::get_default_stream());
62 
73  [[nodiscard]] bool is_valid(rmm::cuda_stream_view stream = cudf::get_default_stream()) const;
74 
80  bool* validity_data();
81 
87  [[nodiscard]] bool const* validity_data() const;
88 
89  protected:
90  data_type _type{type_id::EMPTY};
91  cudf::detail::device_scalar<bool> _is_valid;
92 
97  scalar(scalar&& other) = default;
98 
106  scalar(scalar const& other,
109 
122  bool is_valid = false,
125 };
126 
127 namespace detail {
133 template <typename T>
134 class fixed_width_scalar : public scalar {
135  static_assert(is_fixed_width<T>(), "Unexpected non-fixed-width type.");
136 
137  public:
138  using value_type = T;
139 
140  fixed_width_scalar() = delete;
141  ~fixed_width_scalar() override = default;
142 
148 
149  fixed_width_scalar& operator=(fixed_width_scalar const& other) = delete;
150  fixed_width_scalar& operator=(fixed_width_scalar&& other) = delete;
151 
162 
170 
177  [[nodiscard]] T value(rmm::cuda_stream_view stream = cudf::get_default_stream()) const;
178 
183  T* data();
184 
189  [[nodiscard]] T const* data() const;
190 
191  protected:
193 
203  bool is_valid = true,
206 
216  bool is_valid = true,
219 };
220 
221 } // namespace detail
222 
228 template <typename T>
230  static_assert(is_numeric<T>(), "Unexpected non-numeric type.");
231 
232  public:
233  numeric_scalar() = delete;
234  ~numeric_scalar() override = default;
235 
240  numeric_scalar(numeric_scalar&& other) = default;
241 
242  numeric_scalar& operator=(numeric_scalar const& other) = delete;
243  numeric_scalar& operator=(numeric_scalar&& other) = delete;
244 
255 
264  numeric_scalar(T value,
265  bool is_valid = true,
268 
278  bool is_valid = true,
281 };
282 
288 template <typename T>
289 class fixed_point_scalar : public scalar {
290  static_assert(is_fixed_point<T>(), "Unexpected non-fixed_point type.");
291 
292  public:
293  using rep_type = typename T::rep;
294  using value_type = T;
295 
296  fixed_point_scalar() = delete;
297  ~fixed_point_scalar() override = default;
298 
304 
305  fixed_point_scalar& operator=(fixed_point_scalar const& other) = delete;
306  fixed_point_scalar& operator=(fixed_point_scalar&& other) = delete;
307 
318 
329  numeric::scale_type scale,
330  bool is_valid = true,
333 
343  bool is_valid = true,
346 
356  bool is_valid = true,
359 
370  numeric::scale_type scale,
371  bool is_valid = true,
374 
382 
389  [[nodiscard]] T fixed_point_value(
391 
397 
402  [[nodiscard]] rep_type const* data() const;
403 
404  protected:
406 };
407 
411 class string_scalar : public scalar {
412  public:
414 
415  string_scalar() = delete;
416  ~string_scalar() override = default;
417 
422  string_scalar(string_scalar&& other) = default;
423 
424  // string_scalar(string_scalar const& other) = delete;
425  string_scalar& operator=(string_scalar const& other) = delete;
426  string_scalar& operator=(string_scalar&& other) = delete;
427 
438 
449  string_scalar(std::string_view string,
450  bool is_valid = true,
453 
464  string_scalar(value_type const& source,
465  bool is_valid = true,
468 
480  bool is_valid = true,
483 
496  bool is_valid = true,
499 
506  [[nodiscard]] std::string to_string(
508 
516 
521  [[nodiscard]] size_type size() const;
522 
527  [[nodiscard]] char const* data() const;
528 
529  protected:
531 };
532 
539 template <typename T>
541  static_assert(is_chrono<T>(), "Unexpected non-chrono type");
542 
543  public:
544  chrono_scalar() = delete;
545  ~chrono_scalar() override = default;
546 
551  chrono_scalar(chrono_scalar&& other) = default;
552 
553  chrono_scalar& operator=(chrono_scalar const& other) = delete;
554  chrono_scalar& operator=(chrono_scalar&& other) = delete;
555 
566 
575  chrono_scalar(T value,
576  bool is_valid = true,
579 
589  bool is_valid = true,
592 };
593 
600 template <typename T>
601 class timestamp_scalar : public chrono_scalar<T> {
602  public:
603  static_assert(is_timestamp<T>(), "Unexpected non-timestamp type");
605  using rep_type = typename T::rep;
606 
607  timestamp_scalar() = delete;
608 
613  timestamp_scalar(timestamp_scalar&& other) = default;
614 
625 
636  template <typename Duration2>
637  timestamp_scalar(Duration2 const& value,
638  bool is_valid,
641 
648 };
649 
656 template <typename T>
657 class duration_scalar : public chrono_scalar<T> {
658  public:
659  static_assert(is_duration<T>(), "Unexpected non-duration type");
661  using rep_type = typename T::rep;
662 
663  duration_scalar() = delete;
664 
669  duration_scalar(duration_scalar&& other) = default;
670 
681 
691  bool is_valid,
694 
701 };
702 
706 class list_scalar : public scalar {
707  public:
708  list_scalar() = delete;
709  ~list_scalar() override = default;
710 
715  list_scalar(list_scalar&& other) = default;
716 
717  list_scalar& operator=(list_scalar const& other) = delete;
718  list_scalar& operator=(list_scalar&& other) = delete;
719 
727  list_scalar(list_scalar const& other,
730 
742  bool is_valid = true,
745 
755  bool is_valid = true,
758 
763  [[nodiscard]] column_view view() const;
764 
765  private:
766  cudf::column _data;
767 };
768 
772 class struct_scalar : public scalar {
773  public:
774  struct_scalar() = delete;
775  ~struct_scalar() override = default;
776 
781  struct_scalar(struct_scalar&& other) = default;
782  struct_scalar& operator=(struct_scalar const& other) = delete;
783  struct_scalar& operator=(struct_scalar&& other) = delete;
784 
795 
807  bool is_valid = true,
810 
821  struct_scalar(std::span<column_view const> data,
822  bool is_valid = true,
825 
838  bool is_valid = true,
841 
846  [[nodiscard]] table_view view() const;
847 
848  private:
849  table _data;
850 
854  void assert_valid_size();
855 
865  static table init_data(table&& data,
866  bool is_valid,
867  rmm::cuda_stream_view stream,
869 };
870  // end of group
872 } // namespace CUDF_EXPORT cudf
An owning class to represent a timestamp/duration value in device memory.
Definition: scalar.hpp:540
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:278
An owning class to represent a fixed-width type value in device memory.
Definition: scalar.hpp:134
rmm::device_scalar< T > _data
device memory containing the value
Definition: scalar.hpp:192
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:138
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:657
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:661
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:289
rmm::device_scalar< rep_type > _data
device memory containing the value
Definition: scalar.hpp:405
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:294
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:293
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:706
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:229
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:41
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:91
An owning class to represent a string in device memory.
Definition: scalar.hpp:411
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:35
An owning class to represent a struct value in device memory.
Definition: scalar.hpp:772
table_view view() const
Returns a non-owning, immutable view to underlying device data.
struct_scalar(std::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 span of column_views.
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:601
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:605
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:35
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
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:85
cuDF interfaces
Definition: host_udf.hpp:26
Class definition for cudf::table.
Type declarations for libcudf.