libcudf  23.12.00
column_wrapper.hpp
1 /*
2  * Copyright (c) 2019-2023, 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 
17 #pragma once
18 
19 #include <cudf/column/column.hpp>
21 #include <cudf/copying.hpp>
22 #include <cudf/detail/concatenate.hpp>
23 #include <cudf/detail/iterator.cuh>
24 #include <cudf/detail/null_mask.hpp>
25 #include <cudf/detail/utilities/vector_factories.hpp>
29 #include <cudf/null_mask.hpp>
30 #include <cudf/types.hpp>
31 #include <cudf/utilities/bit.hpp>
32 #include <cudf/utilities/default_stream.hpp>
35 
36 #include <cudf_test/column_utilities.hpp>
37 #include <cudf_test/cudf_gtest.hpp>
38 #include <cudf_test/default_stream.hpp>
39 
40 #include <rmm/device_buffer.hpp>
42 
43 #include <thrust/copy.h>
44 #include <thrust/functional.h>
45 #include <thrust/host_vector.h>
46 #include <thrust/iterator/constant_iterator.h>
47 #include <thrust/iterator/counting_iterator.h>
48 #include <thrust/iterator/transform_iterator.h>
49 
50 #include <algorithm>
51 #include <iterator>
52 #include <memory>
53 #include <numeric>
54 
55 namespace cudf {
56 namespace test {
57 namespace detail {
67  public:
75  operator column_view() const { return wrapped->view(); }
76 
84  operator mutable_column_view() { return wrapped->mutable_view(); }
85 
91  std::unique_ptr<cudf::column> release() { return std::move(wrapped); }
92 
93  protected:
94  std::unique_ptr<cudf::column> wrapped{};
95 };
96 
100 template <typename From, typename To>
110  template <typename FromT = From,
111  typename ToT = To,
112  std::enable_if_t<std::is_same_v<FromT, ToT>, void>* = nullptr>
113  constexpr ToT operator()(FromT element) const
114  {
115  return element;
116  }
117 
126  template <
127  typename FromT = From,
128  typename ToT = To,
129  std::enable_if_t<!std::is_same_v<FromT, ToT> && (cudf::is_convertible<FromT, ToT>::value ||
130  std::is_constructible_v<ToT, FromT>),
131  void>* = nullptr>
132  constexpr ToT operator()(FromT element) const
133  {
134  return static_cast<ToT>(element);
135  }
136 
145  template <
146  typename FromT = From,
147  typename ToT = To,
148  std::enable_if_t<std::is_integral_v<FromT> && cudf::is_timestamp<ToT>(), void>* = nullptr>
149  constexpr ToT operator()(FromT element) const
150  {
151  return ToT{typename ToT::duration{element}};
152  }
153 };
154 
165 template <typename ElementTo,
166  typename ElementFrom,
167  typename InputIterator,
168  std::enable_if_t<not cudf::is_fixed_point<ElementTo>()>* = nullptr>
169 rmm::device_buffer make_elements(InputIterator begin, InputIterator end)
170 {
171  static_assert(cudf::is_fixed_width<ElementTo>(), "Unexpected non-fixed width type.");
172  auto transformer = fixed_width_type_converter<ElementFrom, ElementTo>{};
173  auto transform_begin = thrust::make_transform_iterator(begin, transformer);
174  auto const size = cudf::distance(begin, end);
175  auto const elements = thrust::host_vector<ElementTo>(transform_begin, transform_begin + size);
176  return rmm::device_buffer{
177  elements.data(), size * sizeof(ElementTo), cudf::test::get_default_stream()};
178 }
179 
191 template <typename ElementTo,
192  typename ElementFrom,
193  typename InputIterator,
194  std::enable_if_t<not cudf::is_fixed_point<ElementFrom>() and
195  cudf::is_fixed_point<ElementTo>()>* = nullptr>
196 rmm::device_buffer make_elements(InputIterator begin, InputIterator end)
197 {
198  using RepType = typename ElementTo::rep;
199  auto transformer = fixed_width_type_converter<ElementFrom, RepType>{};
200  auto transform_begin = thrust::make_transform_iterator(begin, transformer);
201  auto const size = cudf::distance(begin, end);
202  auto const elements = thrust::host_vector<RepType>(transform_begin, transform_begin + size);
203  return rmm::device_buffer{
204  elements.data(), size * sizeof(RepType), cudf::test::get_default_stream()};
205 }
206 
217 template <typename ElementTo,
218  typename ElementFrom,
219  typename InputIterator,
220  std::enable_if_t<cudf::is_fixed_point<ElementFrom>() and
221  cudf::is_fixed_point<ElementTo>()>* = nullptr>
222 rmm::device_buffer make_elements(InputIterator begin, InputIterator end)
223 {
224  using namespace numeric;
225  using RepType = typename ElementTo::rep;
226 
227  auto to_rep = [](ElementTo fp) { return fp.value(); };
228  auto transformer_begin = thrust::make_transform_iterator(begin, to_rep);
229  auto const size = cudf::distance(begin, end);
230  auto const elements = thrust::host_vector<RepType>(transformer_begin, transformer_begin + size);
231  return rmm::device_buffer{
232  elements.data(), size * sizeof(RepType), cudf::test::get_default_stream()};
233 }
234 
248 template <typename ValidityIterator>
249 std::pair<std::vector<bitmask_type>, cudf::size_type> make_null_mask_vector(ValidityIterator begin,
250  ValidityIterator end)
251 {
252  auto const size = cudf::distance(begin, end);
253  auto const num_words = cudf::bitmask_allocation_size_bytes(size) / sizeof(bitmask_type);
254 
255  auto null_mask = std::vector<bitmask_type>(num_words, 0);
256  auto null_count = cudf::size_type{0};
257  for (auto i = 0; i < size; ++i) {
258  if (*(begin + i)) {
259  set_bit_unsafe(null_mask.data(), i);
260  } else {
261  ++null_count;
262  }
263  }
264 
265  return {std::move(null_mask), null_count};
266 }
267 
281 template <typename ValidityIterator>
282 std::pair<rmm::device_buffer, cudf::size_type> make_null_mask(ValidityIterator begin,
283  ValidityIterator end)
284 {
285  auto [null_mask, null_count] = make_null_mask_vector(begin, end);
286  auto d_mask = rmm::device_buffer{null_mask.data(),
288  cudf::test::get_default_stream()};
289  return {std::move(d_mask), null_count};
290 }
291 
306 template <typename StringsIterator, typename ValidityIterator>
307 auto make_chars_and_offsets(StringsIterator begin, StringsIterator end, ValidityIterator v)
308 {
309  std::vector<char> chars{};
310  std::vector<cudf::size_type> offsets(1, 0);
311  for (auto str = begin; str < end; ++str) {
312  std::string tmp = (*v++) ? std::string(*str) : std::string{};
313  chars.insert(chars.end(), std::cbegin(tmp), std::cend(tmp));
314  offsets.push_back(offsets.back() + tmp.length());
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  auto all_valid = thrust::make_constant_iterator(true);
756  auto [chars, offsets] = detail::make_chars_and_offsets(begin, end, all_valid);
757  auto d_chars = cudf::detail::make_device_uvector_sync(
758  chars, cudf::test::get_default_stream(), rmm::mr::get_current_device_resource());
759  auto d_offsets = cudf::detail::make_device_uvector_sync(
760  offsets, cudf::test::get_default_stream(), rmm::mr::get_current_device_resource());
761  wrapped =
762  cudf::make_strings_column(d_chars, d_offsets, {}, 0, cudf::test::get_default_stream());
763  }
764 
793  template <typename StringsIterator, typename ValidityIterator>
794  strings_column_wrapper(StringsIterator begin, StringsIterator end, ValidityIterator v)
795  : column_wrapper{}
796  {
797  size_type num_strings = std::distance(begin, end);
798  auto [chars, offsets] = detail::make_chars_and_offsets(begin, end, v);
799  auto [null_mask, null_count] = detail::make_null_mask_vector(v, v + num_strings);
800  auto d_chars = cudf::detail::make_device_uvector_sync(
801  chars, cudf::test::get_default_stream(), rmm::mr::get_current_device_resource());
802  auto d_offsets = cudf::detail::make_device_uvector_sync(
803  offsets, cudf::test::get_default_stream(), rmm::mr::get_current_device_resource());
804  auto d_bitmask = cudf::detail::make_device_uvector_sync(
805  null_mask, cudf::test::get_default_stream(), rmm::mr::get_current_device_resource());
806  wrapped = cudf::make_strings_column(
807  d_chars, d_offsets, d_bitmask, null_count, cudf::test::get_default_stream());
808  }
809 
822  strings_column_wrapper(std::initializer_list<std::string> strings)
823  : strings_column_wrapper(std::cbegin(strings), std::cend(strings))
824  {
825  }
826 
845  template <typename ValidityIterator>
846  strings_column_wrapper(std::initializer_list<std::string> strings, ValidityIterator v)
847  : strings_column_wrapper(std::cbegin(strings), std::cend(strings), v)
848  {
849  }
850 
866  strings_column_wrapper(std::initializer_list<std::string> strings,
867  std::initializer_list<bool> validity)
868  : strings_column_wrapper(std::cbegin(strings), std::cend(strings), std::cbegin(validity))
869  {
870  }
871 
892  strings_column_wrapper(std::initializer_list<std::pair<std::string, bool>> strings)
893  {
894  auto begin =
895  thrust::make_transform_iterator(strings.begin(), [](auto const& s) { return s.first; });
896  auto end = begin + strings.size();
897  auto v =
898  thrust::make_transform_iterator(strings.begin(), [](auto const& s) { return s.second; });
899  wrapped = strings_column_wrapper(begin, end, v).release();
900  }
901 };
902 
911 template <typename KeyElementTo, typename SourceElementT = KeyElementTo>
913  public:
917  operator dictionary_column_view() const { return cudf::dictionary_column_view{wrapped->view()}; }
918 
922  dictionary_column_wrapper() : column_wrapper{}
923  {
925  }
926 
946  template <typename InputIterator>
947  dictionary_column_wrapper(InputIterator begin, InputIterator end) : column_wrapper{}
948  {
949  wrapped =
952  cudf::test::get_default_stream());
953  }
954 
980  template <typename InputIterator, typename ValidityIterator>
981  dictionary_column_wrapper(InputIterator begin, InputIterator end, ValidityIterator v)
982  : column_wrapper{}
983  {
984  wrapped = cudf::dictionary::encode(
987  cudf::test::get_default_stream());
988  }
989 
1003  template <typename ElementFrom>
1004  dictionary_column_wrapper(std::initializer_list<ElementFrom> elements)
1005  : dictionary_column_wrapper(std::cbegin(elements), std::cend(elements))
1006  {
1007  }
1008 
1027  template <typename ElementFrom>
1028  dictionary_column_wrapper(std::initializer_list<ElementFrom> elements,
1029  std::initializer_list<bool> validity)
1030  : dictionary_column_wrapper(std::cbegin(elements), std::cend(elements), std::cbegin(validity))
1031  {
1032  }
1033 
1052  template <typename ValidityIterator, typename ElementFrom>
1053  dictionary_column_wrapper(std::initializer_list<ElementFrom> element_list, ValidityIterator v)
1054  : dictionary_column_wrapper(std::cbegin(element_list), std::cend(element_list), v)
1055  {
1056  }
1057 
1078  template <typename InputIterator>
1079  dictionary_column_wrapper(InputIterator begin,
1080  InputIterator end,
1081  std::initializer_list<bool> const& validity)
1082  : dictionary_column_wrapper(begin, end, std::cbegin(validity))
1083  {
1084  }
1085 };
1086 
1092 template <>
1094  public:
1099  operator dictionary_column_view() const { return cudf::dictionary_column_view{wrapped->view()}; }
1100 
1106  column_view keys() const { return cudf::dictionary_column_view{wrapped->view()}.keys(); }
1107 
1113  column_view indices() const { return cudf::dictionary_column_view{wrapped->view()}.indices(); }
1114 
1118  dictionary_column_wrapper() : dictionary_column_wrapper(std::initializer_list<std::string>{}) {}
1119 
1140  template <typename StringsIterator>
1141  dictionary_column_wrapper(StringsIterator begin, StringsIterator end) : column_wrapper{}
1142  {
1143  wrapped = cudf::dictionary::encode(strings_column_wrapper(begin, end),
1145  cudf::test::get_default_stream());
1146  }
1147 
1176  template <typename StringsIterator, typename ValidityIterator>
1177  dictionary_column_wrapper(StringsIterator begin, StringsIterator end, ValidityIterator v)
1178  : column_wrapper{}
1179  {
1180  wrapped = cudf::dictionary::encode(strings_column_wrapper(begin, end, v),
1182  cudf::test::get_default_stream());
1183  }
1184 
1197  dictionary_column_wrapper(std::initializer_list<std::string> strings)
1198  : dictionary_column_wrapper(std::cbegin(strings), std::cend(strings))
1199  {
1200  }
1201 
1220  template <typename ValidityIterator>
1221  dictionary_column_wrapper(std::initializer_list<std::string> strings, ValidityIterator v)
1222  : dictionary_column_wrapper(std::cbegin(strings), std::cend(strings), v)
1223  {
1224  }
1225 
1241  dictionary_column_wrapper(std::initializer_list<std::string> strings,
1242  std::initializer_list<bool> validity)
1243  : dictionary_column_wrapper(std::cbegin(strings), std::cend(strings), std::cbegin(validity))
1244  {
1245  }
1246 };
1247 
1283 template <typename T, typename SourceElementT = T>
1285  public:
1289  operator lists_column_view() const { return cudf::lists_column_view{wrapped->view()}; }
1290 
1304  template <typename Element = T, std::enable_if_t<cudf::is_fixed_width<Element>()>* = nullptr>
1305  lists_column_wrapper(std::initializer_list<SourceElementT> elements) : column_wrapper{}
1306  {
1307  build_from_non_nested(
1308  std::move(cudf::test::fixed_width_column_wrapper<T, SourceElementT>(elements).release()));
1309  }
1310 
1326  template <typename Element = T,
1327  typename InputIterator,
1328  std::enable_if_t<cudf::is_fixed_width<Element>()>* = nullptr>
1329  lists_column_wrapper(InputIterator begin, InputIterator end) : column_wrapper{}
1330  {
1331  build_from_non_nested(
1332  std::move(cudf::test::fixed_width_column_wrapper<T, SourceElementT>(begin, end).release()));
1333  }
1334 
1350  template <typename Element = T,
1351  typename ValidityIterator,
1352  std::enable_if_t<cudf::is_fixed_width<Element>()>* = nullptr>
1353  lists_column_wrapper(std::initializer_list<SourceElementT> elements, ValidityIterator v)
1354  : column_wrapper{}
1355  {
1356  build_from_non_nested(
1357  std::move(cudf::test::fixed_width_column_wrapper<T, SourceElementT>(elements, v).release()));
1358  }
1359 
1377  template <typename Element = T,
1378  typename InputIterator,
1379  typename ValidityIterator,
1380  std::enable_if_t<cudf::is_fixed_width<Element>()>* = nullptr>
1381  lists_column_wrapper(InputIterator begin, InputIterator end, ValidityIterator v)
1382  : column_wrapper{}
1383  {
1384  build_from_non_nested(std::move(
1386  }
1387 
1401  template <typename Element = T,
1402  std::enable_if_t<std::is_same_v<Element, cudf::string_view>>* = nullptr>
1403  lists_column_wrapper(std::initializer_list<std::string> elements) : column_wrapper{}
1404  {
1405  build_from_non_nested(
1406  std::move(cudf::test::strings_column_wrapper(elements.begin(), elements.end()).release()));
1407  }
1408 
1424  template <typename Element = T,
1425  typename ValidityIterator,
1426  std::enable_if_t<std::is_same_v<Element, cudf::string_view>>* = nullptr>
1427  lists_column_wrapper(std::initializer_list<std::string> elements, ValidityIterator v)
1428  : column_wrapper{}
1429  {
1430  build_from_non_nested(
1431  std::move(cudf::test::strings_column_wrapper(elements.begin(), elements.end(), v).release()));
1432  }
1433 
1456  : column_wrapper{}
1457  {
1458  std::vector<bool> valids;
1459  build_from_nested(elements, valids);
1460  }
1461 
1473  lists_column_wrapper() : column_wrapper{}
1474  {
1475  build_from_non_nested(make_empty_column(cudf::type_to_id<T>()));
1476  }
1477 
1503  template <typename ValidityIterator>
1505  ValidityIterator v)
1506  : column_wrapper{}
1507  {
1508  std::vector<bool> validity;
1509  std::transform(elements.begin(),
1510  elements.end(),
1511  v,
1512  std::back_inserter(validity),
1513  [](lists_column_wrapper const& l, bool valid) { return valid; });
1514  build_from_nested(elements, validity);
1515  }
1516 
1524  {
1527  return lists_column_wrapper<T>(
1528  1,
1529  offsets.release(),
1530  values.release(),
1531  valid ? 0 : 1,
1533  }
1534 
1535  private:
1546  std::unique_ptr<cudf::column>&& offsets,
1547  std::unique_ptr<cudf::column>&& values,
1549  rmm::device_buffer&& null_mask)
1550  {
1551  // construct the list column
1552  wrapped = make_lists_column(num_rows,
1553  std::move(offsets),
1554  std::move(values),
1555  null_count,
1556  std::move(null_mask),
1557  cudf::test::get_default_stream());
1558  }
1559 
1575  void build_from_nested(std::initializer_list<lists_column_wrapper<T, SourceElementT>> elements,
1576  std::vector<bool> const& v)
1577  {
1578  auto valids = cudf::detail::make_counting_transform_iterator(
1579  0, [&v](auto i) { return v.empty() ? true : v[i]; });
1580 
1581  // compute the expected hierarchy and depth
1582  auto const hierarchy_and_depth =
1583  std::accumulate(elements.begin(),
1584  elements.end(),
1585  std::pair<column_view, int32_t>{{}, -1},
1586  [](auto acc, lists_column_wrapper const& lcw) {
1587  return lcw.depth > acc.second ? std::pair(lcw.get_view(), lcw.depth) : acc;
1588  });
1589  column_view expected_hierarchy = hierarchy_and_depth.first;
1590  int32_t const expected_depth = hierarchy_and_depth.second;
1591 
1592  // preprocess columns so that every column_view in 'cols' is an equivalent hierarchy
1593  auto [cols, stubs] = preprocess_columns(elements, expected_hierarchy, expected_depth);
1594 
1595  // generate offsets
1596  size_type count = 0;
1597  std::vector<size_type> offsetv;
1598  std::transform(cols.cbegin(),
1599  cols.cend(),
1600  valids,
1601  std::back_inserter(offsetv),
1602  [&](cudf::column_view const& col, bool valid) {
1603  // nulls are represented as a repeated offset
1604  size_type ret = count;
1605  if (valid) { count += col.size(); }
1606  return ret;
1607  });
1608  // add the final offset
1609  offsetv.push_back(count);
1610  auto offsets =
1611  cudf::test::fixed_width_column_wrapper<size_type>(offsetv.begin(), offsetv.end()).release();
1612 
1613  // concatenate them together, skipping children that are null.
1614  std::vector<column_view> children;
1615  thrust::copy_if(
1616  std::cbegin(cols), std::cend(cols), valids, std::back_inserter(children), thrust::identity{});
1617 
1618  auto data = children.empty() ? cudf::empty_like(expected_hierarchy)
1619  : cudf::concatenate(children,
1620  cudf::test::get_default_stream(),
1622 
1623  // increment depth
1624  depth = expected_depth + 1;
1625 
1626  auto [null_mask, null_count] = [&] {
1627  if (v.size() <= 0) return std::make_pair(rmm::device_buffer{}, cudf::size_type{0});
1628  return cudf::test::detail::make_null_mask(v.begin(), v.end());
1629  }();
1630 
1631  // construct the list column
1632  wrapped = make_lists_column(cols.size(),
1633  std::move(offsets),
1634  std::move(data),
1635  null_count,
1636  std::move(null_mask),
1637  cudf::test::get_default_stream());
1638  }
1639 
1647  void build_from_non_nested(std::unique_ptr<column> c)
1648  {
1649  CUDF_EXPECTS(c->type().id() == type_id::EMPTY || !cudf::is_nested(c->type()),
1650  "Unexpected type");
1651 
1652  std::vector<size_type> offsetv;
1653  if (c->size() > 0) {
1654  offsetv.push_back(0);
1655  offsetv.push_back(c->size());
1656  }
1657  auto offsets =
1658  cudf::test::fixed_width_column_wrapper<size_type>(offsetv.begin(), offsetv.end()).release();
1659 
1660  // construct the list column. mark this as a root
1661  root = true;
1662  depth = 0;
1663 
1664  size_type num_elements = offsets->size() == 0 ? 0 : offsets->size() - 1;
1665  wrapped = make_lists_column(num_elements,
1666  std::move(offsets),
1667  std::move(c),
1668  0,
1670  cudf::test::get_default_stream());
1671  }
1672 
1707  std::unique_ptr<column> normalize_column(column_view const& col,
1708  column_view const& expected_hierarchy)
1709  {
1710  // if are at the bottom of the short column, it must be empty
1711  if (col.type().id() != type_id::LIST) {
1712  CUDF_EXPECTS(col.is_empty(), "Encountered mismatched column!");
1713 
1714  auto remainder = empty_like(expected_hierarchy);
1715  return remainder;
1716  }
1717 
1718  lists_column_view lcv(col);
1719  return make_lists_column(
1720  col.size(),
1721  std::make_unique<column>(lcv.offsets()),
1722  normalize_column(lists_column_view(col).child(),
1723  lists_column_view(expected_hierarchy).child()),
1724  col.null_count(),
1726  col, cudf::test::get_default_stream(), rmm::mr::get_current_device_resource()),
1727  cudf::test::get_default_stream());
1728  }
1729 
1730  std::pair<std::vector<column_view>, std::vector<std::unique_ptr<column>>> preprocess_columns(
1731  std::initializer_list<lists_column_wrapper<T, SourceElementT>> const& elements,
1732  column_view& expected_hierarchy,
1733  int expected_depth)
1734  {
1735  std::vector<std::unique_ptr<column>> stubs;
1736  std::vector<column_view> cols;
1737 
1738  // preprocess the incoming lists.
1739  // - unwrap any "root" lists
1740  // - handle incomplete hierarchies
1741  std::transform(elements.begin(),
1742  elements.end(),
1743  std::back_inserter(cols),
1744  [&](lists_column_wrapper const& l) -> column_view {
1745  // depth mismatch. attempt to normalize the short column.
1746  // this function will also catch if this is a legitimately broken
1747  // set of input
1748  if (l.depth < expected_depth) {
1749  if (l.root) {
1750  // this exception distinguishes between the following two cases:
1751  //
1752  // { {{{1, 2, 3}}}, {} }
1753  // In this case, row 0 is a List<List<List<int>>>, whereas row 1 is
1754  // just a List<> which is an apparent mismatch. However, because row 1
1755  // is empty we will allow that to semantically mean
1756  // "a List<List<List<int>>> that's empty at the top level"
1757  //
1758  // { {{{1, 2, 3}}}, {4, 5, 6} }
1759  // In this case, row 1 is a concrete List<int> with actual values.
1760  // There is no way to rectify the differences so we will treat it as a
1761  // true column mismatch.
1762  CUDF_EXPECTS(l.wrapped->size() == 0, "Mismatch in column types!");
1763  stubs.push_back(empty_like(expected_hierarchy));
1764  } else {
1765  stubs.push_back(normalize_column(l.get_view(), expected_hierarchy));
1766  }
1767  return *(stubs.back());
1768  }
1769  // the empty hierarchy case
1770  return l.get_view();
1771  });
1772 
1773  return {std::move(cols), std::move(stubs)};
1774  }
1775 
1776  column_view get_view() const { return root ? lists_column_view(*wrapped).child() : *wrapped; }
1777 
1778  int depth = 0;
1779  bool root = false;
1780 };
1781 
1786  public:
1814  structs_column_wrapper(std::vector<std::unique_ptr<cudf::column>>&& child_columns,
1815  std::vector<bool> const& validity = {})
1816  {
1817  init(std::move(child_columns), validity);
1818  }
1819 
1841  std::initializer_list<std::reference_wrapper<detail::column_wrapper>> child_column_wrappers,
1842  std::vector<bool> const& validity = {})
1843  {
1844  std::vector<std::unique_ptr<cudf::column>> child_columns;
1845  child_columns.reserve(child_column_wrappers.size());
1846  std::transform(child_column_wrappers.begin(),
1847  child_column_wrappers.end(),
1848  std::back_inserter(child_columns),
1849  [&](auto const& column_wrapper) {
1850  return std::make_unique<cudf::column>(column_wrapper.get(),
1851  cudf::test::get_default_stream());
1852  });
1853  init(std::move(child_columns), validity);
1854  }
1855 
1876  template <typename V>
1878  std::initializer_list<std::reference_wrapper<detail::column_wrapper>> child_column_wrappers,
1879  V validity_iter)
1880  {
1881  std::vector<std::unique_ptr<cudf::column>> child_columns;
1882  child_columns.reserve(child_column_wrappers.size());
1883  std::transform(child_column_wrappers.begin(),
1884  child_column_wrappers.end(),
1885  std::back_inserter(child_columns),
1886  [&](auto const& column_wrapper) {
1887  return std::make_unique<cudf::column>(column_wrapper.get(),
1888  cudf::test::get_default_stream());
1889  });
1890  init(std::move(child_columns), validity_iter);
1891  }
1892 
1893  private:
1894  void init(std::vector<std::unique_ptr<cudf::column>>&& child_columns,
1895  std::vector<bool> const& validity)
1896  {
1897  size_type num_rows = child_columns.empty() ? 0 : child_columns[0]->size();
1898 
1899  CUDF_EXPECTS(std::all_of(child_columns.begin(),
1900  child_columns.end(),
1901  [&](auto const& p_column) { return p_column->size() == num_rows; }),
1902  "All struct member columns must have the same row count.");
1903 
1904  CUDF_EXPECTS(validity.size() <= 0 || static_cast<size_type>(validity.size()) == num_rows,
1905  "Validity buffer must have as many elements as rows in the struct column.");
1906 
1907  auto [null_mask, null_count] = [&] {
1908  if (validity.size() <= 0) return std::make_pair(rmm::device_buffer{}, cudf::size_type{0});
1909  return cudf::test::detail::make_null_mask(validity.begin(), validity.end());
1910  }();
1911 
1912  wrapped = cudf::make_structs_column(num_rows,
1913  std::move(child_columns),
1914  null_count,
1915  std::move(null_mask),
1916  cudf::test::get_default_stream());
1917  }
1918 
1919  template <typename V>
1920  void init(std::vector<std::unique_ptr<cudf::column>>&& child_columns, V validity_iterator)
1921  {
1922  size_type const num_rows = child_columns.empty() ? 0 : child_columns[0]->size();
1923 
1924  CUDF_EXPECTS(std::all_of(child_columns.begin(),
1925  child_columns.end(),
1926  [&](auto const& p_column) { return p_column->size() == num_rows; }),
1927  "All struct member columns must have the same row count.");
1928 
1929  std::vector<bool> validity(num_rows);
1930  std::copy(validity_iterator, validity_iterator + num_rows, validity.begin());
1931 
1932  init(std::move(child_columns), validity);
1933  }
1934 };
1935 
1936 } // namespace test
1937 } // namespace 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:48
Indicator for the logical data type of an element in a column.
Definition: types.hpp:227
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 > wrapped
The wrapped 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< column > make_strings_column(cudf::device_span< thrust::pair< char const *, size_type > const > strings, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
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, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a LIST type column given offsets column, child column, null mask and null count.
std::unique_ptr< column > make_empty_column(data_type type)
Creates an empty column of the specified type.
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::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a STRUCT column using specified child columns as members.
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,...
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::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
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::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Creates a device_buffer for use as a null value indicator bitmask of a column.
std::unique_ptr< column > concatenate(host_span< column_view const > columns_to_concat, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Concatenates multiple columns into a single column.
std::unique_ptr< column > encode(column_view const &column, data_type indices_type=data_type{type_id::UINT32}, rmm::cuda_stream_view stream=cudf::get_default_stream(), rmm::mr::device_memory_resource *mr=rmm::mr::get_current_device_resource())
Construct a dictionary column by dictionary encoding an existing column.
device_memory_resource * get_current_device_resource()
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_HOST_DEVICE void set_bit_unsafe(bitmask_type *bitmask, size_type bit_index)
Sets the specified bit to 1
Definition: bit.hpp:97
#define CUDF_EXPECTS(...)
Macro for checking (pre-)conditions that throws an exception when a condition is violated.
Definition: error.hpp:170
constexpr bool is_fixed_point()
Indicates whether the type T is a fixed-point type.
Definition: traits.hpp:397
int32_t size_type
Row index type for columns and tables.
Definition: types.hpp:80
uint32_t bitmask_type
Bitmask type stored as 32-bit unsigned integer.
Definition: types.hpp:81
size_type distance(T f, T l)
Similar to std::distance but returns cudf::size_type and performs static_cast
Definition: types.hpp:94
constexpr bool is_nested()
Indicates whether T is a nested type.
Definition: traits.hpp:577
@ ALL_NULL
Null mask allocated, initialized to all elements NULL.
@ UINT32
4 byte unsigned integer
@ LIST
List elements.
@ EMPTY
Always null with no underlying data.
@ DICTIONARY32
Dictionary type using int32 indices.
Class definition for cudf::lists_column_view.
cuDF interfaces
Definition: aggregation.hpp:34
rmm::cuda_stream_view const get_default_stream()
Get the current default stream.
fixed_point and supporting types
Definition: fixed_point.hpp:32
scale_type
The scale type for fixed_point.
Definition: fixed_point.hpp:35
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.