scalar.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2024, 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 
35 namespace CUDF_EXPORT cudf {
49 class scalar {
50  public:
51  scalar() = delete;
52  virtual ~scalar() = default;
53  scalar& operator=(scalar const& other) = delete;
54  scalar& operator=(scalar&& other) = delete;
55 
61  [[nodiscard]] data_type type() const noexcept;
62 
69  void set_valid_async(bool is_valid, rmm::cuda_stream_view stream = cudf::get_default_stream());
70 
81  [[nodiscard]] bool is_valid(rmm::cuda_stream_view stream = cudf::get_default_stream()) const;
82 
88  bool* validity_data();
89 
95  [[nodiscard]] bool const* validity_data() const;
96 
97  protected:
98  data_type _type{type_id::EMPTY};
99  cudf::detail::device_scalar<bool> _is_valid;
100 
105  scalar(scalar&& other) = default;
106 
114  scalar(scalar const& other,
117 
130  bool is_valid = false,
133 };
134 
135 namespace detail {
141 template <typename T>
142 class fixed_width_scalar : public scalar {
143  static_assert(is_fixed_width<T>(), "Unexpected non-fixed-width type.");
144 
145  public:
146  using value_type = T;
147 
148  fixed_width_scalar() = delete;
149  ~fixed_width_scalar() override = default;
150 
156 
157  fixed_width_scalar& operator=(fixed_width_scalar const& other) = delete;
158  fixed_width_scalar& operator=(fixed_width_scalar&& other) = delete;
159 
170 
178 
182  explicit operator value_type() const;
183 
190  [[nodiscard]] T value(rmm::cuda_stream_view stream = cudf::get_default_stream()) const;
191 
196  T* data();
197 
202  [[nodiscard]] T const* data() const;
203 
204  protected:
206 
216  bool is_valid = true,
219 
229  bool is_valid = true,
232 };
233 
234 } // namespace detail
235 
241 template <typename T>
243  static_assert(is_numeric<T>(), "Unexpected non-numeric type.");
244 
245  public:
246  numeric_scalar() = delete;
247  ~numeric_scalar() override = default;
248 
253  numeric_scalar(numeric_scalar&& other) = default;
254 
255  numeric_scalar& operator=(numeric_scalar const& other) = delete;
256  numeric_scalar& operator=(numeric_scalar&& other) = delete;
257 
268 
277  numeric_scalar(T value,
278  bool is_valid = true,
281 
291  bool is_valid = true,
294 };
295 
301 template <typename T>
302 class fixed_point_scalar : public scalar {
303  static_assert(is_fixed_point<T>(), "Unexpected non-fixed_point type.");
304 
305  public:
306  using rep_type = typename T::rep;
307  using value_type = T;
308 
309  fixed_point_scalar() = delete;
310  ~fixed_point_scalar() override = default;
311 
317 
318  fixed_point_scalar& operator=(fixed_point_scalar const& other) = delete;
319  fixed_point_scalar& operator=(fixed_point_scalar&& other) = delete;
320 
331 
342  numeric::scale_type scale,
343  bool is_valid = true,
346 
356  bool is_valid = true,
359 
369  bool is_valid = true,
372 
383  numeric::scale_type scale,
384  bool is_valid = true,
387 
395 
402  [[nodiscard]] T fixed_point_value(
404 
408  explicit operator value_type() const;
409 
415 
420  [[nodiscard]] rep_type const* data() const;
421 
422  protected:
424 };
425 
429 class string_scalar : public scalar {
430  public:
432 
433  string_scalar() = delete;
434  ~string_scalar() override = default;
435 
440  string_scalar(string_scalar&& other) = default;
441 
442  // string_scalar(string_scalar const& other) = delete;
443  string_scalar& operator=(string_scalar const& other) = delete;
444  string_scalar& operator=(string_scalar&& other) = delete;
445 
456 
467  string_scalar(std::string const& string,
468  bool is_valid = true,
471 
482  string_scalar(value_type const& source,
483  bool is_valid = true,
486 
498  bool is_valid = true,
501 
514  bool is_valid = true,
517 
521  explicit operator std::string() const;
522 
529  [[nodiscard]] std::string to_string(
531 
539 
544  [[nodiscard]] size_type size() const;
545 
550  [[nodiscard]] char const* data() const;
551 
552  protected:
554 };
555 
562 template <typename T>
564  static_assert(is_chrono<T>(), "Unexpected non-chrono type");
565 
566  public:
567  chrono_scalar() = delete;
568  ~chrono_scalar() override = default;
569 
574  chrono_scalar(chrono_scalar&& other) = default;
575 
576  chrono_scalar& operator=(chrono_scalar const& other) = delete;
577  chrono_scalar& operator=(chrono_scalar&& other) = delete;
578 
589 
598  chrono_scalar(T value,
599  bool is_valid = true,
602 
612  bool is_valid = true,
615 };
616 
623 template <typename T>
624 class timestamp_scalar : public chrono_scalar<T> {
625  public:
626  static_assert(is_timestamp<T>(), "Unexpected non-timestamp type");
628  using rep_type = typename T::rep;
629 
630  timestamp_scalar() = delete;
631 
636  timestamp_scalar(timestamp_scalar&& other) = default;
637 
648 
659  template <typename Duration2>
660  timestamp_scalar(Duration2 const& value,
661  bool is_valid,
664 
671 };
672 
679 template <typename T>
680 class duration_scalar : public chrono_scalar<T> {
681  public:
682  static_assert(is_duration<T>(), "Unexpected non-duration type");
684  using rep_type = typename T::rep;
685 
686  duration_scalar() = delete;
687 
692  duration_scalar(duration_scalar&& other) = default;
693 
704 
714  bool is_valid,
717 
724 };
725 
729 class list_scalar : public scalar {
730  public:
731  list_scalar() = delete;
732  ~list_scalar() override = default;
733 
738  list_scalar(list_scalar&& other) = default;
739 
740  list_scalar& operator=(list_scalar const& other) = delete;
741  list_scalar& operator=(list_scalar&& other) = delete;
742 
750  list_scalar(list_scalar const& other,
753 
765  bool is_valid = true,
768 
778  bool is_valid = true,
781 
786  [[nodiscard]] column_view view() const;
787 
788  private:
789  cudf::column _data;
790 };
791 
795 class struct_scalar : public scalar {
796  public:
797  struct_scalar() = delete;
798  ~struct_scalar() override = default;
799 
804  struct_scalar(struct_scalar&& other) = default;
805  struct_scalar& operator=(struct_scalar const& other) = delete;
806  struct_scalar& operator=(struct_scalar&& other) = delete;
807 
818 
830  bool is_valid = true,
833 
845  bool is_valid = true,
848 
861  bool is_valid = true,
864 
869  [[nodiscard]] table_view view() const;
870 
871  private:
872  table _data;
873 
877  void assert_valid_size();
878 
888  static table init_data(table&& data,
889  bool is_valid,
890  rmm::cuda_stream_view stream,
892 };
893  // end of group
895 } // namespace CUDF_EXPORT cudf
An owning class to represent a timestamp/duration value in device memory.
Definition: scalar.hpp:563
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:142
rmm::device_scalar< T > _data
device memory containing the value
Definition: scalar.hpp:205
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:146
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:680
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:684
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:302
rmm::device_scalar< rep_type > _data
device memory containing the value
Definition: scalar.hpp:423
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:307
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:306
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:729
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:242
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:49
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:99
An owning class to represent a string in device memory.
Definition: scalar.hpp:429
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.
value_type value(rmm::cuda_stream_view stream=cudf::get_default_stream()) const
Get the value of the scalar as a string_view.
string_scalar(std::string const &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.
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:795
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:624
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:628
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: aggregation.hpp:35
C++20 std::span with reduced feature set.
Definition: span.hpp:219
Class definition for cudf::table.
Type declarations for libcudf.