column.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 
8 #include <cudf/null_mask.hpp>
9 #include <cudf/types.hpp>
12 
13 #include <rmm/cuda_stream_view.hpp>
14 #include <rmm/device_buffer.hpp>
15 #include <rmm/device_uvector.hpp>
16 
17 #include <memory>
18 #include <type_traits>
19 #include <utility>
20 #include <vector>
21 
27 namespace CUDF_EXPORT cudf {
28 
36 class column {
37  public:
38  column() = default;
39  ~column() = default;
40  column& operator=(column const& other) = delete;
41  column& operator=(column&& other) = delete;
42 
54  column(column const& other,
57 
65  column(column&& other) noexcept;
66 
74  template <typename T, CUDF_ENABLE_IF(cudf::is_numeric<T>() or cudf::is_chrono<T>())>
75  column(rmm::device_uvector<T>&& other, rmm::device_buffer&& null_mask, size_type null_count)
76  : _type{cudf::data_type{cudf::type_to_id<T>()}},
77  _size{[&]() {
79  other.size() <= static_cast<std::size_t>(std::numeric_limits<size_type>::max()),
80  "The device_uvector size exceeds the column size limit",
81  std::overflow_error);
82  return static_cast<size_type>(other.size());
83  }()},
84  _data{other.release()},
85  _null_mask{std::move(null_mask)},
86  _null_count{null_count}
87  {
88  }
89 
105  template <typename B1, typename B2 = rmm::device_buffer>
107  size_type size,
108  B1&& data,
109  B2&& null_mask,
111  std::vector<std::unique_ptr<column>>&& children = {})
112  : _type{dtype},
113  _size{size},
114  _data{std::forward<B1>(data)},
115  _null_mask{std::forward<B2>(null_mask)},
116  _null_count{null_count},
117  _children{std::move(children)}
118  {
119  CUDF_EXPECTS(size >= 0, "Column size cannot be negative.");
120  }
121 
132  explicit column(column_view view,
135 
141  [[nodiscard]] data_type type() const noexcept { return _type; }
142 
148  [[nodiscard]] size_type size() const noexcept { return _size; }
149 
155  [[nodiscard]] size_type null_count() const { return _null_count; }
156 
168  void set_null_mask(rmm::device_buffer&& new_null_mask, size_type new_null_count);
169 
182  void set_null_mask(rmm::device_buffer const& new_null_mask,
183  size_type new_null_count,
185 
193  void set_null_count(size_type new_null_count);
194 
207  [[nodiscard]] bool nullable() const noexcept { return (_null_mask.size() > 0); }
208 
215  [[nodiscard]] bool has_nulls() const noexcept { return (null_count() > 0); }
216 
222  [[nodiscard]] size_type num_children() const noexcept { return _children.size(); }
223 
230  column& child(size_type child_index) noexcept { return *_children[child_index]; };
231 
238  [[nodiscard]] column const& child(size_type child_index) const noexcept
239  {
240  return *_children[child_index];
241  };
242 
248  struct contents {
249  std::unique_ptr<rmm::device_buffer> data;
250  std::unique_ptr<rmm::device_buffer> null_mask;
251  std::vector<std::unique_ptr<column>> children;
252  };
253 
269  contents release() noexcept;
270 
280  [[nodiscard]] std::size_t alloc_size() const;
281 
288  [[nodiscard]] column_view view() const;
289 
298  operator column_view() const { return this->view(); };
299 
307 
319  operator mutable_column_view() { return this->mutable_view(); };
320 
321  private:
322  cudf::data_type _type{type_id::EMPTY};
323  cudf::size_type _size{};
324  rmm::device_buffer _data{};
326  rmm::device_buffer _null_mask{};
328  mutable cudf::size_type _null_count{};
329  std::vector<std::unique_ptr<column>> _children{};
331 };
332  // end of group
334 } // namespace CUDF_EXPORT cudf
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
data_type type() const noexcept
Returns the column's logical element type.
Definition: column.hpp:141
bool has_nulls() const noexcept
Indicates whether the column contains null elements.
Definition: column.hpp:215
column(column_view view, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a new column by deep copying the contents of a column_view.
size_type null_count() const
Returns the count of null elements.
Definition: column.hpp:155
size_type num_children() const noexcept
Returns the number of child columns.
Definition: column.hpp:222
void set_null_mask(rmm::device_buffer const &new_null_mask, size_type new_null_count, rmm::cuda_stream_view stream=cudf::get_default_stream())
Sets the column's null value indicator bitmask to new_null_mask.
mutable_column_view mutable_view()
Creates a mutable, non-owning view of the column's data, null mask, and children.
column(column &&other) noexcept
Move the contents from other to create a new column.
column const & child(size_type child_index) const noexcept
Returns a const reference to the specified child.
Definition: column.hpp:238
column & child(size_type child_index) noexcept
Returns a reference to the specified child.
Definition: column.hpp:230
column(data_type dtype, size_type size, B1 &&data, B2 &&null_mask, size_type null_count, std::vector< std::unique_ptr< column >> &&children={})
Construct a new column from existing device memory.
Definition: column.hpp:106
void set_null_mask(rmm::device_buffer &&new_null_mask, size_type new_null_count)
Sets the column's null value indicator bitmask to new_null_mask.
column(column 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 column object by deep copying the contents of other.
bool nullable() const noexcept
Indicates whether it is possible for the column to contain null values, i.e., it has an allocated nul...
Definition: column.hpp:207
contents release() noexcept
Releases ownership of the column's contents.
size_type size() const noexcept
Returns the number of elements.
Definition: column.hpp:148
void set_null_count(size_type new_null_count)
Updates the count of null elements.
Indicator for the logical data type of an element in a column.
Definition: types.hpp:238
A non-owning, mutable view of device data as a column of elements, some of which may be null as indic...
column view class definitions
cudf::size_type null_count(bitmask_type const *bitmask, size_type start, size_type stop, rmm::cuda_stream_view stream=cudf::get_default_stream())
Given a validity bitmask, counts the number of null elements (unset bits) in the range [start,...
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.
detail::cccl_async_resource_ref< cuda::mr::resource_ref< cuda::mr::device_accessible > > device_async_resource_ref
#define CUDF_EXPECTS(...)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
Definition: error.hpp:143
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:84
constexpr CUDF_HOST_DEVICE bool is_chrono()
Indicates whether the type T is a chrono type.
Definition: traits.hpp:490
cuDF interfaces
Definition: host_udf.hpp:26
APIs for managing validity bitmasks.
Wrapper for the contents of a column.
Definition: column.hpp:248
std::unique_ptr< rmm::device_buffer > data
data device memory buffer
Definition: column.hpp:249
std::unique_ptr< rmm::device_buffer > null_mask
null mask device memory buffer
Definition: column.hpp:250
std::vector< std::unique_ptr< column > > children
child columns
Definition: column.hpp:251
Type declarations for libcudf.