table_view.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2022, 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 
19 #include <cudf/types.hpp>
20 
21 #include <algorithm>
22 #include <vector>
23 
35 namespace cudf {
36 namespace detail {
49 template <typename ColumnView>
51  static_assert(std::is_same_v<ColumnView, column_view> or
52  std::is_same_v<ColumnView, mutable_column_view>,
53  "table_view_base can only be instantiated with column_view or "
54  "column_view_base.");
55 
56  private:
57  std::vector<ColumnView> _columns{};
58  size_type _num_rows{};
59 
60  public:
61  using iterator = decltype(std::begin(_columns));
62  using const_iterator = decltype(std::cbegin(_columns));
63 
80  explicit table_view_base(std::vector<ColumnView> const& cols);
81 
87  iterator begin() noexcept { return std::begin(_columns); }
88 
94  [[nodiscard]] const_iterator begin() const noexcept { return std::begin(_columns); }
95 
104  iterator end() noexcept { return std::end(_columns); }
105 
114  [[nodiscard]] const_iterator end() const noexcept { return std::end(_columns); }
115 
125  ColumnView const& column(size_type column_index) const;
126 
132  [[nodiscard]] size_type num_columns() const noexcept { return _columns.size(); }
133 
139  [[nodiscard]] size_type num_rows() const noexcept { return _num_rows; }
140 
146  [[nodiscard]] size_type is_empty() const noexcept { return num_columns() == 0; }
147 
148  table_view_base() = default;
149 
150  ~table_view_base() = default;
151 
152  table_view_base(table_view_base const&) = default;
153 
155 
167 };
168 } // namespace detail
169 
178 class table_view : public detail::table_view_base<column_view> {
180 
181  public:
183 
184  table_view() = default;
185 
204  table_view(std::vector<table_view> const& views);
205 
217  template <typename InputIterator>
218  table_view select(InputIterator begin, InputIterator end) const
219  {
220  std::vector<column_view> columns(std::distance(begin, end));
221  std::transform(begin, end, columns.begin(), [this](auto index) { return this->column(index); });
222  return table_view(columns);
223  }
224 
235  [[nodiscard]] table_view select(std::vector<size_type> const& column_indices) const;
236 };
237 
246 class mutable_table_view : public detail::table_view_base<mutable_column_view> {
248 
249  public:
251 
252  mutable_table_view() = default;
253 
260  [[nodiscard]] mutable_column_view& column(size_type column_index) const
261  {
262  return const_cast<mutable_column_view&>(table_view_base::column(column_index));
263  }
267  operator table_view();
268 
287  mutable_table_view(std::vector<mutable_table_view> const& views);
288 };
289 
296 inline bool nullable(table_view const& view)
297 {
298  return std::any_of(view.begin(), view.end(), [](auto const& col) { return col.nullable(); });
299 }
300 
309 inline bool has_nulls(table_view const& view)
310 {
311  return std::any_of(view.begin(), view.end(), [](auto const& col) { return col.has_nulls(); });
312 }
313 
320 inline bool has_nested_nulls(table_view const& input)
321 {
322  return std::any_of(input.begin(), input.end(), [](auto const& col) {
323  return col.has_nulls() ||
324  std::any_of(col.child_begin(), col.child_end(), [](auto const& child_col) {
325  return has_nested_nulls(table_view{{child_col}});
326  });
327  });
328 }
329 
336 std::vector<column_view> get_nullable_columns(table_view const& table);
337 
345 inline bool have_same_types(table_view const& lhs, table_view const& rhs)
346 {
347  return std::equal(
348  lhs.begin(),
349  lhs.end(),
350  rhs.begin(),
351  rhs.end(),
352  [](column_view const& lcol, column_view const& rcol) { return (lcol.type() == rcol.type()); });
353 }
354 
369  std::vector<size_type> const& map,
370  table_view const& target);
371 
372 namespace detail {
381 template <typename TableView>
382 bool is_relationally_comparable(TableView const& lhs, TableView const& rhs);
383 // @cond
384 extern template bool is_relationally_comparable<table_view>(table_view const& lhs,
385  table_view const& rhs);
386 extern template bool is_relationally_comparable<mutable_table_view>(mutable_table_view const& lhs,
387  mutable_table_view const& rhs);
388 // @endcond
389 } // namespace detail
390 } // namespace cudf
cudf::mutable_table_view
A set of mutable_column_views of the same size.
Definition: table_view.hpp:246
cudf::detail::table_view_base::begin
const_iterator begin() const noexcept
Returns an iterator to the first view in the table.
Definition: table_view.hpp:94
cudf::mutable_table_view::mutable_table_view
mutable_table_view(std::vector< mutable_table_view > const &views)
Construct a table from a vector of table views.
cudf::table_view::select
table_view select(std::vector< size_type > const &column_indices) const
Returns a table_view with set of specified columns.
cudf::table_view::select
table_view select(InputIterator begin, InputIterator end) const
Returns a table_view built from a range of column indices.
Definition: table_view.hpp:218
cudf::mutable_table_view::column
mutable_column_view & column(size_type column_index) const
Returns column at specified index.
Definition: table_view.hpp:260
cudf::detail::table_view_base::operator=
table_view_base & operator=(table_view_base const &)=default
Copy assignment operator.
cudf::size_type
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:80
cudf::detail::table_view_base::operator=
table_view_base & operator=(table_view_base &&)=default
Move assignment operator.
cudf::column_view
A non-owning, immutable view of device data as a column of elements, some of which may be null as ind...
Definition: column_view.hpp:322
types.hpp
Type declarations for libcudf.
cudf::detail::table_view_base::table_view_base
table_view_base(table_view_base const &)=default
Copy constructor.
cudf::detail::table_view_base::num_rows
size_type num_rows() const noexcept
Returns the number of rows.
Definition: table_view.hpp:139
cudf::mutable_column_view
A non-owning, mutable view of device data as a column of elements, some of which may be null as indic...
Definition: column_view.hpp:485
cudf::detail::is_relationally_comparable
bool is_relationally_comparable(TableView const &lhs, TableView const &rhs)
Indicates whether respective columns in input tables are relationally comparable.
cudf::table_view::table_view
table_view(std::vector< table_view > const &views)
Construct a table from a vector of table views.
cudf::table
A set of cudf::column's of the same size.
Definition: table.hpp:40
cudf::detail::table_view_base::begin
iterator begin() noexcept
Returns an iterator to the first view in the table.
Definition: table_view.hpp:87
cudf::detail::table_view_base::end
iterator end() noexcept
Returns an iterator one past the last column view in the table.
Definition: table_view.hpp:104
cudf::nullable
bool nullable(table_view const &view)
Returns True if any of the columns in the table is nullable. (not entire hierarchy)
Definition: table_view.hpp:296
cudf::detail::table_view_base::num_columns
size_type num_columns() const noexcept
Returns the number of columns.
Definition: table_view.hpp:132
cudf::detail::table_view_base::table_view_base
table_view_base(std::vector< ColumnView > const &cols)
Construct a table from a vector of column views.
cudf::detail::table_view_base::end
const_iterator end() const noexcept
Returns an iterator one past the last column view in the table.
Definition: table_view.hpp:114
cudf::scatter_columns
table_view scatter_columns(table_view const &source, std::vector< size_type > const &map, table_view const &target)
Copy column_views from a table_view into another table_view according to a column indices map.
cudf::table_view
A set of cudf::column_view's of the same size.
Definition: table_view.hpp:178
cudf::transform
std::unique_ptr< column > transform(column_view const &input, std::string const &unary_udf, data_type output_type, bool is_ptx, rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Creates a new column by applying a unary function against every element of an input column.
cudf::have_same_types
bool have_same_types(table_view const &lhs, table_view const &rhs)
Checks if two table_views have columns of same types.
Definition: table_view.hpp:345
cudf::detail::table_view_base::table_view_base
table_view_base(table_view_base &&)=default
Move constructor.
cudf
cuDF interfaces
Definition: aggregation.hpp:34
cudf::has_nulls
bool has_nulls(table_view const &view)
Returns True if the table has nulls in any of its columns.
Definition: table_view.hpp:309
cudf::has_nested_nulls
bool has_nested_nulls(table_view const &input)
Returns True if the table has nulls in any of its columns hierarchy.
Definition: table_view.hpp:320
cudf::detail::table_view_base< column_view >::iterator
decltype(std::begin(_columns)) iterator
Iterator type for the table.
Definition: table_view.hpp:61
cudf::detail::table_view_base
Base class for a table of ColumnViews.
Definition: table_view.hpp:50
cudf::distance
size_type distance(T f, T l)
Similar to std::distance but returns cudf::size_type and performs static_cast
Definition: types.hpp:95
cudf::detail::table_view_base< column_view >::const_iterator
decltype(std::cbegin(_columns)) const_iterator
const iterator type for the table
Definition: table_view.hpp:62
cudf::detail::table_view_base::column
ColumnView const & column(size_type column_index) const
Returns a reference to the view of the specified column.
cudf::get_nullable_columns
std::vector< column_view > get_nullable_columns(table_view const &table)
The function to collect all nullable columns at all nested levels in a given table.
column_view.hpp
column view class definitions
cudf::detail::table_view_base::is_empty
size_type is_empty() const noexcept
Returns true if num_columns() returns zero, or false otherwise.
Definition: table_view.hpp:146