column_wrapper.hpp
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include <cudf_test/column_utilities.hpp>
9 #include <cudf_test/default_stream.hpp>
10 
11 #include <cudf/column/column.hpp>
13 #include <cudf/copying.hpp>
14 #include <cudf/detail/concatenate.hpp>
15 #include <cudf/detail/iterator.cuh>
16 #include <cudf/detail/utilities/vector_factories.hpp>
20 #include <cudf/null_mask.hpp>
21 #include <cudf/types.hpp>
22 #include <cudf/utilities/bit.hpp>
24 #include <cudf/utilities/export.hpp>
28 
29 #include <rmm/device_buffer.hpp>
30 
31 #include <cuda/iterator>
32 #include <cuda/std/functional>
33 #include <thrust/copy.h>
34 #include <thrust/host_vector.h>
35 #include <thrust/iterator/counting_iterator.h>
36 #include <thrust/iterator/transform_iterator.h>
37 
38 #include <algorithm>
39 #include <iterator>
40 #include <memory>
41 #include <numeric>
42 
43 namespace CUDF_EXPORT cudf {
44 namespace test {
45 namespace detail {
55  public:
63  operator column_view() const { return wrapped->view(); }
64 
72  operator mutable_column_view() { return wrapped->mutable_view(); }
73 
79  std::unique_ptr<cudf::column> release() { return std::move(wrapped); }
80 
81  protected:
82  std::unique_ptr<cudf::column> wrapped{};
83 };
84 
88 template <typename From, typename To>
98  template <typename FromT = From,
99  typename ToT = To,
100  std::enable_if_t<std::is_same_v<FromT, ToT>, void>* = nullptr>
101  constexpr ToT operator()(FromT element) const
102  {
103  return element;
104  }
105 
114  template <
115  typename FromT = From,
116  typename ToT = To,
117  std::enable_if_t<!std::is_same_v<FromT, ToT> && (cudf::is_convertible<FromT, ToT>::value ||
118  std::is_constructible_v<ToT, FromT>),
119  void>* = nullptr>
120  constexpr ToT operator()(FromT element) const
121  {
122  return static_cast<ToT>(element);
123  }
124 
133  template <
134  typename FromT = From,
135  typename ToT = To,
136  std::enable_if_t<std::is_integral_v<FromT> && cudf::is_timestamp<ToT>(), void>* = nullptr>
137  constexpr ToT operator()(FromT element) const
138  {
139  return ToT{typename ToT::duration{element}};
140  }
141 };
142 
153 template <typename ElementTo,
154  typename ElementFrom,
155  typename InputIterator,
156  std::enable_if_t<not cudf::is_fixed_point<ElementTo>()>* = nullptr>
157 rmm::device_buffer make_elements(InputIterator begin, InputIterator end)
158 {
159  static_assert(cudf::is_fixed_width<ElementTo>(), "Unexpected non-fixed width type.");
160  auto transformer = fixed_width_type_converter<ElementFrom, ElementTo>{};
161  auto transform_begin = thrust::make_transform_iterator(begin, transformer);
162  auto const size = cudf::distance(begin, end);
163  auto const elements = thrust::host_vector<ElementTo>(transform_begin, transform_begin + size);
164  return rmm::device_buffer{
165  elements.data(), size * sizeof(ElementTo), cudf::test::get_default_stream()};
166 }
167 
168 // The two signatures below are identical to the above overload apart from
169 // SFINAE so doxygen sees it as a duplicate.
171 
182 template <typename ElementTo,
183  typename ElementFrom,
184  typename InputIterator,
185  std::enable_if_t<not cudf::is_fixed_point<ElementFrom>() and
186  cudf::is_fixed_point<ElementTo>()>* = nullptr>
187 rmm::device_buffer make_elements(InputIterator begin, InputIterator end)
188 {
189  using RepType = typename ElementTo::rep;
190  auto transformer = fixed_width_type_converter<ElementFrom, RepType>{};
191  auto transform_begin = thrust::make_transform_iterator(begin, transformer);
192  auto const size = cudf::distance(begin, end);
193  auto const elements = thrust::host_vector<RepType>(transform_begin, transform_begin + size);
194  return rmm::device_buffer{
195  elements.data(), size * sizeof(RepType), cudf::test::get_default_stream()};
196 }
197 
208 template <typename ElementTo,
209  typename ElementFrom,
210  typename InputIterator,
211  std::enable_if_t<cudf::is_fixed_point<ElementFrom>() and
212  cudf::is_fixed_point<ElementTo>()>* = nullptr>
213 rmm::device_buffer make_elements(InputIterator begin, InputIterator end)
214 {
215  using namespace numeric;
216  using RepType = typename ElementTo::rep;
217 
218  CUDF_EXPECTS(std::all_of(begin, end, [](ElementFrom v) { return v.scale() == 0; }),
219  "Only zero-scale fixed-point values are supported");
220 
221  auto to_rep = [](ElementTo fp) { return fp.value(); };
222  auto transformer_begin = thrust::make_transform_iterator(begin, to_rep);
223  auto const size = cudf::distance(begin, end);
224  auto const elements = thrust::host_vector<RepType>(transformer_begin, transformer_begin + size);
225  return rmm::device_buffer{
226  elements.data(), size * sizeof(RepType), cudf::test::get_default_stream()};
227 }
229 
243 template <typename ValidityIterator>
244 std::pair<std::vector<bitmask_type>, cudf::size_type> make_null_mask_vector(ValidityIterator begin,
245  ValidityIterator end)
246 {
247  auto const size = cudf::distance(begin, end);
248  auto const num_words = cudf::bitmask_allocation_size_bytes(size) / sizeof(bitmask_type);
249 
250  auto null_mask = std::vector<bitmask_type>(num_words, 0);
251  auto null_count = cudf::size_type{0};
252  for (auto i = 0; i < size; ++i) {
253  if (*(begin + i)) {
254  set_bit_unsafe(null_mask.data(), i);
255  } else {
256  ++null_count;
257  }
258  }
259 
260  return {std::move(null_mask), null_count};
261 }
262 
276 template <typename ValidityIterator>
277 std::pair<rmm::device_buffer, cudf::size_type> make_null_mask(ValidityIterator begin,
278  ValidityIterator end)
279 {
280  auto [null_mask, null_count] = make_null_mask_vector(begin, end);
281  auto d_mask = rmm::device_buffer{null_mask.data(),
284  return {std::move(d_mask), null_count};
285 }
286 
301 template <typename StringsIterator, typename ValidityIterator>
302 auto make_chars_and_offsets(StringsIterator begin, StringsIterator end, ValidityIterator v)
303 {
304  std::vector<char> chars{};
305  std::vector<cudf::size_type> offsets(1, 0);
306  for (auto str = begin; str < end; ++str) {
307  std::string tmp = (*v++) ? std::string(*str) : std::string{};
308  chars.insert(chars.end(), std::cbegin(tmp), std::cend(tmp));
309  auto const last_offset = static_cast<std::size_t>(offsets.back());
310  auto const next_offset = last_offset + tmp.length();
311  CUDF_EXPECTS(
312  next_offset < static_cast<std::size_t>(std::numeric_limits<cudf::size_type>::max()),
313  "Cannot use strings_column_wrapper to build a large strings column");
314  offsets.push_back(static_cast<cudf::size_type>(next_offset));
315  }
316  return std::pair(std::move(chars), std::move(offsets));
317 };
318 } // namespace detail
319 
328 template <typename ElementTo, typename SourceElementT = ElementTo>
330  public:
334  fixed_width_column_wrapper() : column_wrapper{}
335  {
336  std::vector<ElementTo> empty;
337  wrapped.reset(
338  new cudf::column{cudf::data_type{cudf::type_to_id<ElementTo>()},
339  0,
340  detail::make_elements<ElementTo, SourceElementT>(empty.begin(), empty.end()),
342  0});
343  }
344 
363  template <typename InputIterator>
364  fixed_width_column_wrapper(InputIterator begin, InputIterator end) : column_wrapper{}
365  {
366  auto const size = cudf::distance(begin, end);
367  wrapped.reset(new cudf::column{cudf::data_type{cudf::type_to_id<ElementTo>()},
368  size,
369  detail::make_elements<ElementTo, SourceElementT>(begin, end),
371  0});
372  }
373 
397  template <typename InputIterator, typename ValidityIterator>
398  fixed_width_column_wrapper(InputIterator begin, InputIterator end, ValidityIterator v)
399  : column_wrapper{}
400  {
401  auto const size = cudf::distance(begin, end);
402  auto [null_mask, null_count] = detail::make_null_mask(v, v + size);
403  wrapped.reset(new cudf::column{cudf::data_type{cudf::type_to_id<ElementTo>()},
404  size,
405  detail::make_elements<ElementTo, SourceElementT>(begin, end),
406  std::move(null_mask),
407  null_count});
408  }
409 
422  template <typename ElementFrom>
423  fixed_width_column_wrapper(std::initializer_list<ElementFrom> elements)
424  : fixed_width_column_wrapper(std::cbegin(elements), std::cend(elements))
425  {
426  }
427 
445  template <typename ElementFrom>
446  fixed_width_column_wrapper(std::initializer_list<ElementFrom> elements,
447  std::initializer_list<bool> validity)
448  : fixed_width_column_wrapper(std::cbegin(elements), std::cend(elements), std::cbegin(validity))
449  {
450  }
451 
469  template <typename ValidityIterator, typename ElementFrom>
470  fixed_width_column_wrapper(std::initializer_list<ElementFrom> element_list, ValidityIterator v)
471  : fixed_width_column_wrapper(std::cbegin(element_list), std::cend(element_list), v)
472  {
473  }
474 
493  template <typename InputIterator>
494  fixed_width_column_wrapper(InputIterator begin,
495  InputIterator end,
496  std::initializer_list<bool> const& validity)
497  : fixed_width_column_wrapper(begin, end, std::cbegin(validity))
498  {
499  }
500 
518  template <typename ElementFrom>
519  fixed_width_column_wrapper(std::initializer_list<std::pair<ElementFrom, bool>> elements)
520  {
521  auto begin =
522  thrust::make_transform_iterator(elements.begin(), [](auto const& e) { return e.first; });
523  auto end = begin + elements.size();
524  auto v =
525  thrust::make_transform_iterator(elements.begin(), [](auto const& e) { return e.second; });
527  }
528 };
529 
535 template <typename Rep>
537  public:
554  template <typename FixedPointRepIterator>
555  fixed_point_column_wrapper(FixedPointRepIterator begin,
556  FixedPointRepIterator end,
557  numeric::scale_type scale)
558  : column_wrapper{}
559  {
560  CUDF_EXPECTS(numeric::is_supported_representation_type<Rep>(), "not valid representation type");
561 
562  auto const size = cudf::distance(begin, end);
563  auto const elements = thrust::host_vector<Rep>(begin, end);
564  auto const id = type_to_id<numeric::fixed_point<Rep, numeric::Radix::BASE_10>>();
565  auto const data_type = cudf::data_type{id, static_cast<int32_t>(scale)};
566 
567  wrapped.reset(new cudf::column{
568  data_type,
569  size,
570  rmm::device_buffer{elements.data(), size * sizeof(Rep), cudf::test::get_default_stream()},
572  0});
573  }
574 
587  fixed_point_column_wrapper(std::initializer_list<Rep> values, numeric::scale_type scale)
588  : fixed_point_column_wrapper(std::cbegin(values), std::cend(values), scale)
589  {
590  }
591 
619  template <typename FixedPointRepIterator, typename ValidityIterator>
620  fixed_point_column_wrapper(FixedPointRepIterator begin,
621  FixedPointRepIterator end,
622  ValidityIterator v,
623  numeric::scale_type scale)
624  : column_wrapper{}
625  {
626  CUDF_EXPECTS(numeric::is_supported_representation_type<Rep>(), "not valid representation type");
627 
628  auto const size = cudf::distance(begin, end);
629  auto const elements = thrust::host_vector<Rep>(begin, end);
630  auto const id = type_to_id<numeric::fixed_point<Rep, numeric::Radix::BASE_10>>();
631  auto const data_type = cudf::data_type{id, static_cast<int32_t>(scale)};
632  auto [null_mask, null_count] = detail::make_null_mask(v, v + size);
633  wrapped.reset(new cudf::column{
634  data_type,
635  size,
636  rmm::device_buffer{elements.data(), size * sizeof(Rep), cudf::test::get_default_stream()},
637  std::move(null_mask),
638  null_count});
639  }
640 
658  fixed_point_column_wrapper(std::initializer_list<Rep> elements,
659  std::initializer_list<bool> validity,
660  numeric::scale_type scale)
662  std::cbegin(elements), std::cend(elements), std::cbegin(validity), scale)
663  {
664  }
665 
684  template <typename ValidityIterator>
685  fixed_point_column_wrapper(std::initializer_list<Rep> element_list,
686  ValidityIterator v,
687  numeric::scale_type scale)
688  : fixed_point_column_wrapper(std::cbegin(element_list), std::cend(element_list), v, scale)
689  {
690  }
691 
712  template <typename FixedPointRepIterator>
713  fixed_point_column_wrapper(FixedPointRepIterator begin,
714  FixedPointRepIterator end,
715  std::initializer_list<bool> const& validity,
716  numeric::scale_type scale)
717  : fixed_point_column_wrapper(begin, end, std::cbegin(validity), scale)
718  {
719  }
720 };
721 
726  public:
730  strings_column_wrapper() : strings_column_wrapper(std::initializer_list<std::string>{}) {}
731 
752  template <typename StringsIterator>
753  strings_column_wrapper(StringsIterator begin, StringsIterator end) : column_wrapper{}
754  {
755  size_type num_strings = std::distance(begin, end);
756  if (num_strings == 0) {
758  return;
759  }
760  auto all_valid = cuda::make_constant_iterator(true);
761  auto [chars, offsets] = detail::make_chars_and_offsets(begin, end, all_valid);
762  auto d_chars = cudf::detail::make_device_uvector_async(
764  auto d_offsets = std::make_unique<cudf::column>(
765  cudf::detail::make_device_uvector(
768  0);
769  wrapped =
770  cudf::make_strings_column(num_strings, std::move(d_offsets), d_chars.release(), 0, {});
771  }
772 
801  template <typename StringsIterator, typename ValidityIterator>
802  strings_column_wrapper(StringsIterator begin, StringsIterator end, ValidityIterator v)
803  : column_wrapper{}
804  {
805  size_type num_strings = std::distance(begin, end);
806  if (num_strings == 0) {
808  return;
809  }
810  auto [chars, offsets] = detail::make_chars_and_offsets(begin, end, v);
811  auto [null_mask, null_count] = detail::make_null_mask_vector(v, v + num_strings);
812  auto d_chars = cudf::detail::make_device_uvector_async(
814  auto d_offsets = std::make_unique<cudf::column>(
815  cudf::detail::make_device_uvector_async(
818  0);
819  auto d_bitmask = cudf::detail::make_device_uvector(
821  wrapped = cudf::make_strings_column(
822  num_strings, std::move(d_offsets), d_chars.release(), null_count, d_bitmask.release());
823  }
824 
837  strings_column_wrapper(std::initializer_list<std::string> strings)
838  : strings_column_wrapper(std::cbegin(strings), std::cend(strings))
839  {
840  }
841 
860  template <typename ValidityIterator>
861  strings_column_wrapper(std::initializer_list<std::string> strings, ValidityIterator v)
862  : strings_column_wrapper(std::cbegin(strings), std::cend(strings), v)
863  {
864  }
865 
881  strings_column_wrapper(std::initializer_list<std::string> strings,
882  std::initializer_list<bool> validity)
883  : strings_column_wrapper(std::cbegin(strings), std::cend(strings), std::cbegin(validity))
884  {
885  }
886 
907  strings_column_wrapper(std::initializer_list<std::pair<std::string, bool>> strings)
908  {
909  auto begin =
910  thrust::make_transform_iterator(strings.begin(), [](auto const& s) { return s.first; });
911  auto end = begin + strings.size();
912  auto v =
913  thrust::make_transform_iterator(strings.begin(), [](auto const& s) { return s.second; });
914  wrapped = strings_column_wrapper(begin, end, v).release();
915  }
916 };
917 
926 template <typename KeyElementTo, typename SourceElementT = KeyElementTo>
928  public:
932  operator dictionary_column_view() const { return cudf::dictionary_column_view{wrapped->view()}; }
933 
937  dictionary_column_wrapper() : column_wrapper{}
938  {
940  }
941 
961  template <typename InputIterator>
962  dictionary_column_wrapper(InputIterator begin, InputIterator end) : column_wrapper{}
963  {
964  wrapped =
966  cudf::data_type{type_id::INT32},
968  }
969 
995  template <typename InputIterator, typename ValidityIterator>
996  dictionary_column_wrapper(InputIterator begin, InputIterator end, ValidityIterator v)
997  : column_wrapper{}
998  {
999  wrapped = cudf::dictionary::encode(
1001  cudf::data_type{type_id::INT32},
1003  }
1004 
1018  template <typename ElementFrom>
1019  dictionary_column_wrapper(std::initializer_list<ElementFrom> elements)
1020  : dictionary_column_wrapper(std::cbegin(elements), std::cend(elements))
1021  {
1022  }
1023 
1042  template <typename ElementFrom>
1043  dictionary_column_wrapper(std::initializer_list<ElementFrom> elements,
1044  std::initializer_list<bool> validity)
1045  : dictionary_column_wrapper(std::cbegin(elements), std::cend(elements), std::cbegin(validity))
1046  {
1047  }
1048 
1067  template <typename ValidityIterator, typename ElementFrom>
1068  dictionary_column_wrapper(std::initializer_list<ElementFrom> element_list, ValidityIterator v)
1069  : dictionary_column_wrapper(std::cbegin(element_list), std::cend(element_list), v)
1070  {
1071  }
1072 
1093  template <typename InputIterator>
1094  dictionary_column_wrapper(InputIterator begin,
1095  InputIterator end,
1096  std::initializer_list<bool> const& validity)
1097  : dictionary_column_wrapper(begin, end, std::cbegin(validity))
1098  {
1099  }
1100 };
1101 
1107 template <>
1109  public:
1114  operator dictionary_column_view() const { return cudf::dictionary_column_view{wrapped->view()}; }
1115 
1121  [[nodiscard]] column_view keys() const
1122  {
1123  return cudf::dictionary_column_view{wrapped->view()}.keys();
1124  }
1125 
1131  [[nodiscard]] column_view indices() const
1132  {
1133  return cudf::dictionary_column_view{wrapped->view()}.indices();
1134  }
1135 
1139  dictionary_column_wrapper() : dictionary_column_wrapper(std::initializer_list<std::string>{}) {}
1140 
1161  template <typename StringsIterator>
1162  dictionary_column_wrapper(StringsIterator begin, StringsIterator end) : column_wrapper{}
1163  {
1164  wrapped = cudf::dictionary::encode(strings_column_wrapper(begin, end),
1165  cudf::data_type{type_id::INT32},
1167  }
1168 
1197  template <typename StringsIterator, typename ValidityIterator>
1198  dictionary_column_wrapper(StringsIterator begin, StringsIterator end, ValidityIterator v)
1199  : column_wrapper{}
1200  {
1201  wrapped = cudf::dictionary::encode(strings_column_wrapper(begin, end, v),
1202  cudf::data_type{type_id::INT32},
1204  }
1205 
1218  dictionary_column_wrapper(std::initializer_list<std::string> strings)
1219  : dictionary_column_wrapper(std::cbegin(strings), std::cend(strings))
1220  {
1221  }
1222 
1241  template <typename ValidityIterator>
1242  dictionary_column_wrapper(std::initializer_list<std::string> strings, ValidityIterator v)
1243  : dictionary_column_wrapper(std::cbegin(strings), std::cend(strings), v)
1244  {
1245  }
1246 
1262  dictionary_column_wrapper(std::initializer_list<std::string> strings,
1263  std::initializer_list<bool> validity)
1264  : dictionary_column_wrapper(std::cbegin(strings), std::cend(strings), std::cbegin(validity))
1265  {
1266  }
1267 };
1268 
1304 template <typename T, typename SourceElementT = T>
1306  public:
1310  operator lists_column_view() const { return cudf::lists_column_view{wrapped->view()}; }
1311 
1325  template <typename Element = T, std::enable_if_t<cudf::is_fixed_width<Element>()>* = nullptr>
1326  lists_column_wrapper(std::initializer_list<SourceElementT> elements) : column_wrapper{}
1327  {
1328  build_from_non_nested(
1330  }
1331 
1347  template <typename Element = T,
1348  typename InputIterator,
1349  std::enable_if_t<cudf::is_fixed_width<Element>()>* = nullptr>
1350  lists_column_wrapper(InputIterator begin, InputIterator end) : column_wrapper{}
1351  {
1352  build_from_non_nested(
1354  }
1355 
1371  template <typename Element = T,
1372  typename ValidityIterator,
1373  std::enable_if_t<cudf::is_fixed_width<Element>()>* = nullptr>
1374  lists_column_wrapper(std::initializer_list<SourceElementT> elements, ValidityIterator v)
1375  : column_wrapper{}
1376  {
1377  build_from_non_nested(
1379  }
1380 
1398  template <typename Element = T,
1399  typename InputIterator,
1400  typename ValidityIterator,
1401  std::enable_if_t<cudf::is_fixed_width<Element>()>* = nullptr>
1402  lists_column_wrapper(InputIterator begin, InputIterator end, ValidityIterator v)
1403  : column_wrapper{}
1404  {
1405  build_from_non_nested(
1407  }
1408 
1422  template <typename Element = T,
1423  std::enable_if_t<std::is_same_v<Element, cudf::string_view>>* = nullptr>
1424  lists_column_wrapper(std::initializer_list<std::string> elements) : column_wrapper{}
1425  {
1426  build_from_non_nested(
1427  cudf::test::strings_column_wrapper(elements.begin(), elements.end()).release());
1428  }
1429 
1445  template <typename Element = T,
1446  typename ValidityIterator,
1447  std::enable_if_t<std::is_same_v<Element, cudf::string_view>>* = nullptr>
1448  lists_column_wrapper(std::initializer_list<std::string> elements, ValidityIterator v)
1449  : column_wrapper{}
1450  {
1451  build_from_non_nested(
1452  cudf::test::strings_column_wrapper(elements.begin(), elements.end(), v).release());
1453  }
1454 
1477  : column_wrapper{}
1478  {
1479  std::vector<bool> valids;
1480  build_from_nested(elements, valids);
1481  }
1482 
1494  lists_column_wrapper() : column_wrapper{}
1495  {
1496  build_from_non_nested(make_empty_column(cudf::type_to_id<T>()));
1497  }
1498 
1524  template <typename ValidityIterator>
1526  ValidityIterator v)
1527  : column_wrapper{}
1528  {
1529  std::vector<bool> validity;
1530  std::transform(elements.begin(),
1531  elements.end(),
1532  v,
1533  std::back_inserter(validity),
1534  [](lists_column_wrapper const& l, bool valid) { return valid; });
1535  build_from_nested(elements, validity);
1536  }
1537 
1545  {
1548  return lists_column_wrapper<T>(
1549  1,
1550  offsets.release(),
1551  values.release(),
1552  valid ? 0 : 1,
1554  }
1555 
1556  private:
1567  std::unique_ptr<cudf::column>&& offsets,
1568  std::unique_ptr<cudf::column>&& values,
1570  rmm::device_buffer&& null_mask)
1571  {
1572  // construct the list column
1573  wrapped = make_lists_column(
1574  num_rows, std::move(offsets), std::move(values), null_count, std::move(null_mask));
1575  }
1576 
1593  void build_from_nested(std::initializer_list<lists_column_wrapper<T, SourceElementT>> elements,
1594  std::vector<bool> const& v)
1595  {
1596  auto valids = cudf::detail::make_counting_transform_iterator(
1597  0, [&v](auto i) { return v.empty() ? true : v[i]; });
1598 
1599  // compute the expected hierarchy and depth
1600  auto const hierarchy_and_depth =
1601  std::accumulate(elements.begin(),
1602  elements.end(),
1603  std::pair<column_view, int32_t>{{}, -1},
1604  [](auto acc, lists_column_wrapper const& lcw) {
1605  return lcw.depth > acc.second ? std::pair(lcw.get_view(), lcw.depth) : acc;
1606  });
1607  column_view expected_hierarchy = hierarchy_and_depth.first;
1608  int32_t const expected_depth = hierarchy_and_depth.second;
1609 
1610  // preprocess columns so that every column_view in 'cols' is an equivalent hierarchy
1611  auto [cols, stubs] = preprocess_columns(elements, expected_hierarchy, expected_depth);
1612 
1613  // generate offsets
1614  size_type count = 0;
1615  std::vector<size_type> offsetv;
1616  std::transform(cols.cbegin(),
1617  cols.cend(),
1618  valids,
1619  std::back_inserter(offsetv),
1620  [&](cudf::column_view const& col, bool valid) {
1621  // nulls are represented as a repeated offset
1622  size_type ret = count;
1623  if (valid) { count += col.size(); }
1624  return ret;
1625  });
1626  // add the final offset
1627  offsetv.push_back(count);
1628  auto offsets =
1629  cudf::test::fixed_width_column_wrapper<size_type>(offsetv.begin(), offsetv.end()).release();
1630 
1631  // concatenate them together, skipping children that are null.
1632  std::vector<column_view> children;
1633  thrust::copy_if(std::cbegin(cols),
1634  std::cend(cols),
1635  valids,
1636  std::back_inserter(children),
1637  cuda::std::identity{});
1638 
1639  auto data = children.empty() ? cudf::empty_like(expected_hierarchy)
1640  : cudf::concatenate(children,
1641  cudf::test::get_default_stream(),
1643 
1644  // increment depth
1645  depth = expected_depth + 1;
1646 
1647  auto [null_mask, null_count] = [&] {
1648  if (v.size() <= 0) return std::make_pair(rmm::device_buffer{}, cudf::size_type{0});
1649  return cudf::test::detail::make_null_mask(v.begin(), v.end());
1650  }();
1651 
1652  // construct the list column
1653  wrapped = make_lists_column(
1654  cols.size(), std::move(offsets), std::move(data), null_count, std::move(null_mask));
1655  }
1656 
1664  void build_from_non_nested(std::unique_ptr<column> c)
1665  {
1666  CUDF_EXPECTS(c->type().id() == type_id::EMPTY || !cudf::is_nested(c->type()),
1667  "Unexpected type");
1668 
1669  std::vector<size_type> offsetv;
1670  if (c->size() > 0) {
1671  offsetv.push_back(0);
1672  offsetv.push_back(c->size());
1673  }
1674  auto offsets =
1675  cudf::test::fixed_width_column_wrapper<size_type>(offsetv.begin(), offsetv.end()).release();
1676 
1677  // construct the list column. mark this as a root
1678  root = true;
1679  depth = 0;
1680 
1681  size_type num_elements = offsets->size() == 0 ? 0 : offsets->size() - 1;
1682  wrapped =
1683  make_lists_column(num_elements, std::move(offsets), std::move(c), 0, rmm::device_buffer{});
1684  }
1685 
1720  std::unique_ptr<column> normalize_column(column_view const& col,
1721  column_view const& expected_hierarchy)
1722  {
1723  // if are at the bottom of the short column, it must be empty
1724  if (col.type().id() != type_id::LIST) {
1725  CUDF_EXPECTS(col.is_empty(), "Encountered mismatched column!");
1726 
1727  auto remainder = empty_like(expected_hierarchy);
1728  return remainder;
1729  }
1730 
1731  lists_column_view lcv(col);
1732  return make_lists_column(
1733  col.size(),
1734  std::make_unique<column>(lcv.offsets()),
1735  normalize_column(lists_column_view(col).child(),
1736  lists_column_view(expected_hierarchy).child()),
1737  col.null_count(),
1740  }
1741 
1742  std::pair<std::vector<column_view>, std::vector<std::unique_ptr<column>>> preprocess_columns(
1743  std::initializer_list<lists_column_wrapper<T, SourceElementT>> const& elements,
1744  column_view& expected_hierarchy,
1745  int expected_depth)
1746  {
1747  std::vector<std::unique_ptr<column>> stubs;
1748  std::vector<column_view> cols;
1749 
1750  // preprocess the incoming lists.
1751  // - unwrap any "root" lists
1752  // - handle incomplete hierarchies
1753  std::transform(elements.begin(),
1754  elements.end(),
1755  std::back_inserter(cols),
1756  [&](lists_column_wrapper const& l) -> column_view {
1757  // depth mismatch. attempt to normalize the short column.
1758  // this function will also catch if this is a legitimately broken
1759  // set of input
1760  if (l.depth < expected_depth) {
1761  if (l.root) {
1762  // this exception distinguishes between the following two cases:
1763  //
1764  // { {{{1, 2, 3}}}, {} }
1765  // In this case, row 0 is a List<List<List<int>>>, whereas row 1 is
1766  // just a List<> which is an apparent mismatch. However, because row 1
1767  // is empty we will allow that to semantically mean
1768  // "a List<List<List<int>>> that's empty at the top level"
1769  //
1770  // { {{{1, 2, 3}}}, {4, 5, 6} }
1771  // In this case, row 1 is a concrete List<int> with actual values.
1772  // There is no way to rectify the differences so we will treat it as a
1773  // true column mismatch.
1774  CUDF_EXPECTS(l.wrapped->size() == 0, "Mismatch in column types!");
1775  stubs.push_back(empty_like(expected_hierarchy));
1776  } else {
1777  stubs.push_back(normalize_column(l.get_view(), expected_hierarchy));
1778  }
1779  return *(stubs.back());
1780  }
1781  // the empty hierarchy case
1782  return l.get_view();
1783  });
1784 
1785  return {std::move(cols), std::move(stubs)};
1786  }
1787 
1788  [[nodiscard]] column_view get_view() const
1789  {
1790  return root ? lists_column_view(*wrapped).child() : *wrapped;
1791  }
1792 
1793  int depth = 0;
1794  bool root = false;
1795 };
1796 
1801  public:
1829  structs_column_wrapper(std::vector<std::unique_ptr<cudf::column>>&& child_columns,
1830  std::vector<bool> const& validity = {})
1831  {
1832  init(std::move(child_columns), validity);
1833  }
1834 
1856  std::initializer_list<std::reference_wrapper<detail::column_wrapper>> child_column_wrappers,
1857  std::vector<bool> const& validity = {})
1858  {
1859  std::vector<std::unique_ptr<cudf::column>> child_columns;
1860  child_columns.reserve(child_column_wrappers.size());
1861  std::transform(child_column_wrappers.begin(),
1862  child_column_wrappers.end(),
1863  std::back_inserter(child_columns),
1864  [&](auto const& column_wrapper) {
1865  return std::make_unique<cudf::column>(column_wrapper.get(),
1866  cudf::test::get_default_stream());
1867  });
1868  init(std::move(child_columns), validity);
1869  }
1870 
1891  template <typename V>
1893  std::initializer_list<std::reference_wrapper<detail::column_wrapper>> child_column_wrappers,
1894  V validity_iter)
1895  {
1896  std::vector<std::unique_ptr<cudf::column>> child_columns;
1897  child_columns.reserve(child_column_wrappers.size());
1898  std::transform(child_column_wrappers.begin(),
1899  child_column_wrappers.end(),
1900  std::back_inserter(child_columns),
1901  [&](auto const& column_wrapper) {
1902  return std::make_unique<cudf::column>(column_wrapper.get(),
1903  cudf::test::get_default_stream());
1904  });
1905  init(std::move(child_columns), validity_iter);
1906  }
1907 
1908  private:
1909  void init(std::vector<std::unique_ptr<cudf::column>>&& child_columns,
1910  std::vector<bool> const& validity)
1911  {
1912  size_type num_rows = child_columns.empty() ? 0 : child_columns[0]->size();
1913 
1914  CUDF_EXPECTS(std::all_of(child_columns.begin(),
1915  child_columns.end(),
1916  [&](auto const& p_column) { return p_column->size() == num_rows; }),
1917  "All struct member columns must have the same row count.");
1918 
1919  CUDF_EXPECTS(validity.size() <= 0 || static_cast<size_type>(validity.size()) == num_rows,
1920  "Validity buffer must have as many elements as rows in the struct column.");
1921 
1922  auto [null_mask, null_count] = [&] {
1923  if (validity.size() <= 0) return std::make_pair(rmm::device_buffer{}, cudf::size_type{0});
1924  return cudf::test::detail::make_null_mask(validity.begin(), validity.end());
1925  }();
1926 
1927  wrapped = cudf::make_structs_column(num_rows,
1928  std::move(child_columns),
1929  null_count,
1930  std::move(null_mask),
1932  }
1933 
1934  template <typename V>
1935  void init(std::vector<std::unique_ptr<cudf::column>>&& child_columns, V validity_iterator)
1936  {
1937  size_type const num_rows = child_columns.empty() ? 0 : child_columns[0]->size();
1938 
1939  CUDF_EXPECTS(std::all_of(child_columns.begin(),
1940  child_columns.end(),
1941  [&](auto const& p_column) { return p_column->size() == num_rows; }),
1942  "All struct member columns must have the same row count.");
1943 
1944  std::vector<bool> validity(num_rows);
1945  std::copy(validity_iterator, validity_iterator + num_rows, validity.begin());
1946 
1947  init(std::move(child_columns), validity);
1948  }
1949 };
1950 
1951 } // namespace test
1952 } // namespace CUDF_EXPORT cudf
Utilities for bit and bitmask operations.
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:277
A wrapper class for operations on a dictionary column.
column_view indices() const noexcept
Returns the column of indices.
column_view keys() const noexcept
Returns the column of keys.
Given a column-view of lists type, an instance of this class provides a wrapper on this compound colu...
A non-owning, mutable view of device data as a column of elements, some of which may be null as indic...
Base class for a wrapper around a cudf::column.
std::unique_ptr< cudf::column > release()
Releases internal unique_ptr to wrapped column.
dictionary_column_wrapper(StringsIterator begin, StringsIterator end, ValidityIterator v)
Construct a nullable dictionary column of strings from the range [begin,end) using the range [v,...
dictionary_column_wrapper(std::initializer_list< std::string > strings, std::initializer_list< bool > validity)
Construct a nullable dictionary column of strings from a list of strings and a list of booleans to in...
column_view indices() const
Access indices column view.
dictionary_column_wrapper(std::initializer_list< std::string > strings)
Construct a non-nullable dictionary column of strings from a list of strings.
dictionary_column_wrapper(StringsIterator begin, StringsIterator end)
Construct a non-nullable dictionary column of strings from the range [begin,end).
dictionary_column_wrapper(std::initializer_list< std::string > strings, ValidityIterator v)
Construct a nullable dictionary column of strings from a list of strings and the range [v,...
column_view keys() const
Access keys column view.
dictionary_column_wrapper()
Default constructor initializes an empty dictionary column of strings.
column_wrapper derived class for wrapping dictionary columns.
dictionary_column_wrapper(std::initializer_list< ElementFrom > elements)
Construct a non-nullable dictionary column of fixed-width elements from an initializer list.
dictionary_column_wrapper(std::initializer_list< ElementFrom > elements, std::initializer_list< bool > validity)
Construct a nullable dictionary column from a list of fixed-width elements using another list to indi...
dictionary_column_wrapper(std::initializer_list< ElementFrom > element_list, ValidityIterator v)
Construct a nullable dictionary column from a list of fixed-width elements and the range [v,...
dictionary_column_wrapper()
Default constructor initializes an empty column with dictionary type.
dictionary_column_wrapper(InputIterator begin, InputIterator end)
Construct a non-nullable dictionary column of the fixed-width elements in the range [begin,...
dictionary_column_wrapper(InputIterator begin, InputIterator end, ValidityIterator v)
Construct a nullable dictionary column of the fixed-width elements in the range [begin,...
dictionary_column_wrapper(InputIterator begin, InputIterator end, std::initializer_list< bool > const &validity)
Construct a nullable dictionary column of the fixed-width elements in the range [begin,...
A wrapper for a column of fixed-width elements.
fixed_point_column_wrapper(FixedPointRepIterator begin, FixedPointRepIterator end, std::initializer_list< bool > const &validity, numeric::scale_type scale)
Construct a nullable column of the decimal elements in the range [begin,end) using a validity initial...
fixed_point_column_wrapper(std::initializer_list< Rep > elements, std::initializer_list< bool > validity, numeric::scale_type scale)
Construct a nullable column from an initializer list of decimal elements using another list to indica...
fixed_point_column_wrapper(FixedPointRepIterator begin, FixedPointRepIterator end, numeric::scale_type scale)
Construct a non-nullable column of the decimal elements in the range [begin,end).
fixed_point_column_wrapper(std::initializer_list< Rep > element_list, ValidityIterator v, numeric::scale_type scale)
Construct a nullable column from an initializer list of decimal elements and the range [v,...
fixed_point_column_wrapper(std::initializer_list< Rep > values, numeric::scale_type scale)
Construct a non-nullable column of decimal elements from an initializer list.
fixed_point_column_wrapper(FixedPointRepIterator begin, FixedPointRepIterator end, ValidityIterator v, numeric::scale_type scale)
Construct a nullable column of the fixed-point elements from a range.
column_wrapper derived class for wrapping columns of fixed-width elements.
fixed_width_column_wrapper(InputIterator begin, InputIterator end, std::initializer_list< bool > const &validity)
Construct a nullable column of the fixed-width elements in the range [begin,end) using a validity ini...
fixed_width_column_wrapper(std::initializer_list< std::pair< ElementFrom, bool >> elements)
Construct a nullable column from a list of pairs of fixed-width elements and validity booleans of eac...
fixed_width_column_wrapper(std::initializer_list< ElementFrom > elements, std::initializer_list< bool > validity)
Construct a nullable column from a list of fixed-width elements using another list to indicate the va...
fixed_width_column_wrapper(std::initializer_list< ElementFrom > elements)
Construct a non-nullable column of fixed-width elements from an initializer list.
fixed_width_column_wrapper(InputIterator begin, InputIterator end, ValidityIterator v)
Construct a nullable column of the fixed-width elements in the range [begin,end) using the range [v,...
fixed_width_column_wrapper(std::initializer_list< ElementFrom > element_list, ValidityIterator v)
Construct a nullable column from a list of fixed-width elements and the range [v, v + element_list....
fixed_width_column_wrapper(InputIterator begin, InputIterator end)
Construct a non-nullable column of the fixed-width elements in the range [begin,end).
fixed_width_column_wrapper()
Default constructor initializes an empty column with proper dtype.
column_wrapper derived class for wrapping columns of lists.
lists_column_wrapper(InputIterator begin, InputIterator end)
Construct a lists column containing a single list of fixed-width type from an iterator range.
lists_column_wrapper(std::initializer_list< lists_column_wrapper< T, SourceElementT >> elements, ValidityIterator v)
Construct a lists column of nested lists from an initializer list of values and a validity iterator.
static lists_column_wrapper< T > make_one_empty_row_column(bool valid=true)
Construct a list column containing a single empty, optionally null row.
lists_column_wrapper(InputIterator begin, InputIterator end, ValidityIterator v)
Construct a lists column containing a single list of fixed-width type from an iterator range and a va...
lists_column_wrapper()
Construct am empty lists column.
lists_column_wrapper(std::initializer_list< lists_column_wrapper< T, SourceElementT >> elements)
Construct a lists column of nested lists from an initializer list of values.
lists_column_wrapper(std::initializer_list< SourceElementT > elements)
Construct a lists column containing a single list of fixed-width type from an initializer list of val...
lists_column_wrapper(std::initializer_list< std::string > elements, ValidityIterator v)
Construct a lists column containing a single list of strings from an initializer list of values and a...
lists_column_wrapper(std::initializer_list< std::string > elements)
Construct a lists column containing a single list of strings from an initializer list of values.
lists_column_wrapper(std::initializer_list< SourceElementT > elements, ValidityIterator v)
Construct a lists column containing a single list of fixed-width type from an initializer list of val...
column_wrapper derived class for wrapping columns of strings.
strings_column_wrapper(std::initializer_list< std::pair< std::string, bool >> strings)
Construct a nullable column from a list of pairs of strings and validity booleans of each string.
strings_column_wrapper()
Default constructor initializes an empty column of strings.
strings_column_wrapper(std::initializer_list< std::string > strings)
Construct a non-nullable column of strings from a list of strings.
strings_column_wrapper(std::initializer_list< std::string > strings, std::initializer_list< bool > validity)
Construct a nullable column of strings from a list of strings and a list of booleans to indicate the ...
strings_column_wrapper(std::initializer_list< std::string > strings, ValidityIterator v)
Construct a nullable column of strings from a list of strings and the range [v, v + strings....
strings_column_wrapper(StringsIterator begin, StringsIterator end)
Construct a non-nullable column of strings from the range [begin,end).
strings_column_wrapper(StringsIterator begin, StringsIterator end, ValidityIterator v)
Construct a nullable column of strings from the range [begin,end) using the range [v,...
column_wrapper derived class for wrapping columns of structs.
structs_column_wrapper(std::initializer_list< std::reference_wrapper< detail::column_wrapper >> child_column_wrappers, V validity_iter)
Constructs a struct column from the list of column wrappers for child columns.
structs_column_wrapper(std::initializer_list< std::reference_wrapper< detail::column_wrapper >> child_column_wrappers, std::vector< bool > const &validity={})
Constructs a struct column from the list of column wrappers for child columns.
structs_column_wrapper(std::vector< std::unique_ptr< cudf::column >> &&child_columns, std::vector< bool > const &validity={})
Constructs a struct column from the specified list of pre-constructed child columns.
void const * data() const noexcept
Class definition for cudf::column.
Column factory APIs.
Column APIs for gather, scatter, split, slice, etc.
Dictionary column encode and decode APIs.
Class definition for fixed point data type.
std::unique_ptr< column > empty_like(column_view const &input)
Initializes and returns an empty column of the same type as the input.
std::unique_ptr< cudf::column > make_structs_column(size_type num_rows, std::vector< std::unique_ptr< column >> &&child_columns, size_type null_count, rmm::device_buffer &&null_mask, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a STRUCT column using specified child columns as members.
std::unique_ptr< column > make_empty_column(data_type type)
Creates an empty column of the specified type.
std::unique_ptr< column > make_strings_column(cudf::device_span< cuda::std::pair< char const *, size_type > const > strings, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a STRING type column given a device span of pointer/size pairs.
std::unique_ptr< cudf::column > make_lists_column(size_type num_rows, std::unique_ptr< column > offsets_column, std::unique_ptr< column > child_column, size_type null_count, rmm::device_buffer &&null_mask)
Construct a LIST type column given offsets column, child column, null mask and null count.
std::size_t bitmask_allocation_size_bytes(size_type number_of_bits, std::size_t padding_boundary=64)
Computes the required bytes necessary to represent the specified number of bits with a given padding ...
rmm::device_buffer copy_bitmask(bitmask_type const *mask, size_type begin_bit, size_type end_bit, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Creates a device_buffer from a slice of bitmask defined by a range of indices [begin_bit,...
rmm::device_buffer create_null_mask(size_type size, mask_state state, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Creates a device_buffer for use as a null value indicator bitmask of a column
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,...
std::unique_ptr< column > concatenate(host_span< column_view const > columns_to_concat, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Concatenates multiple columns into a single column.
rmm::cuda_stream_view const get_default_stream()
Get the current default stream.
std::unique_ptr< column > encode(column_view const &column, data_type indices_type=data_type{type_id::INT32}, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Construct a dictionary column by dictionary encoding an existing column.
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.
device_async_resource_ref get_current_device_resource_ref()
std::unique_ptr< column > is_fixed_point(strings_column_view const &input, data_type decimal_type=data_type{type_id::DECIMAL64}, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Returns a boolean column identifying strings in which all characters are valid for conversion to fixe...
std::unique_ptr< column > transform(std::vector< column_view > const &inputs, std::string const &transform_udf, data_type output_type, bool is_ptx, std::optional< void * > user_data=std::nullopt, null_aware is_null_aware=null_aware::NO, output_nullability null_policy=output_nullability::PRESERVE, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::device_async_resource_ref mr=cudf::get_current_device_resource_ref())
Creates a new column by applying a transform function against every element of the input columns.
CUDF_HOST_DEVICE void set_bit_unsafe(bitmask_type *bitmask, size_type bit_index)
Sets the specified bit to 1
Definition: bit.hpp:73
#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
uint32_t bitmask_type
Bitmask type stored as 32-bit unsigned integer.
Definition: types.hpp:85
size_type distance(T f, T l)
Similar to std::distance but returns cudf::size_type and performs static_cast
Definition: types.hpp:99
constexpr CUDF_HOST_DEVICE bool is_nested()
Indicates whether T is a nested type.
Definition: traits.hpp:646
@ ALL_NULL
Null mask allocated, initialized to all elements NULL.
@ STRING
String elements.
@ DICTIONARY32
Dictionary type using int32 indices.
Class definition for cudf::lists_column_view.
cuDF interfaces
Definition: host_udf.hpp:26
fixed_point and supporting types
Definition: fixed_point.hpp:23
APIs for managing validity bitmasks.
Convert between source and target types when they differ and where possible.
constexpr ToT operator()(FromT element) const
No conversion necessary: Same type, simply copy element to output.
Defines the mapping between cudf::type_id runtime type information and concrete C++ types.
Type declarations for libcudf.