repo_id
stringlengths 21
96
| file_path
stringlengths 31
155
| content
stringlengths 1
92.9M
| __index_level_0__
int64 0
0
|
---|---|---|---|
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/lists/explode_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/lists/explode.hpp>
using FCW = cudf::test::fixed_width_column_wrapper<int32_t>;
using LCW = cudf::test::lists_column_wrapper<int32_t>;
class ExplodeTest : public cudf::test::BaseFixture {};
class ExplodeOuterTest : public cudf::test::BaseFixture {};
template <typename T>
class ExplodeTypedTest : public cudf::test::BaseFixture {};
template <typename T>
class ExplodeOuterTypedTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(ExplodeTypedTest, cudf::test::FixedPointTypes);
TYPED_TEST_SUITE(ExplodeOuterTypedTest, cudf::test::FixedPointTypes);
TEST_F(ExplodeTest, Empty)
{
cudf::table_view t({LCW{}, FCW{}});
auto ret = cudf::explode(t, 0);
cudf::table_view expected({FCW{}, FCW{}});
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
auto pos_ret = cudf::explode_position(t, 0);
cudf::table_view pos_expected({FCW{}, FCW{}, FCW{}});
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, NonList)
{
cudf::table_view t({FCW{100, 200, 300}, FCW{100, 200, 300}});
EXPECT_THROW(cudf::explode(t, 1), cudf::logic_error);
EXPECT_THROW(cudf::explode_position(t, 1), cudf::logic_error);
}
TEST_F(ExplodeTest, Basics)
{
// a b c
// 100 [1, 2, 7] string0
// 200 [5, 6] string1
// 300 [0, 3] string2
FCW a{100, 200, 300};
LCW b{LCW{1, 2, 7}, LCW{5, 6}, LCW{0, 3}};
cudf::test::strings_column_wrapper c{"string0", "string1", "string2"};
FCW expected_a{100, 100, 100, 200, 200, 300, 300};
FCW expected_b{1, 2, 7, 5, 6, 0, 3};
cudf::test::strings_column_wrapper expected_c{
"string0", "string0", "string0", "string1", "string1", "string2", "string2"};
cudf::table_view t({a, b, c});
cudf::table_view expected({expected_a, expected_b, expected_c});
auto ret = cudf::explode(t, 1);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 2, 0, 1, 0, 1};
cudf::table_view pos_expected({expected_a, expected_pos_col, expected_b, expected_c});
auto pos_ret = cudf::explode_position(t, 1);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, SingleNull)
{
// a b
// null 100
// [5, 6] 200
// [] 300
// [0, 3] 400
constexpr auto null = 0;
auto first_invalid =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i != 0; });
LCW a({LCW{null}, LCW{5, 6}, LCW{}, LCW{0, 3}}, first_invalid);
FCW b({100, 200, 300, 400});
FCW expected_a{5, 6, 0, 3};
FCW expected_b{200, 200, 400, 400};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 1};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, Nulls)
{
// a b
// [1, 2, 7] 100
// null 200
// [0, 3] 300
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
auto always_valid =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return true; });
LCW a({LCW{1, 2, 7}, LCW{null}, LCW{0, 3}}, valids);
FCW b({100, 200, 300}, valids);
FCW expected_a({1, 2, 7, 0, 3});
FCW expected_b({100, 100, 100, 300, 300}, always_valid);
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 2, 0, 1};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, NullsInList)
{
// a b
// [1, null, 7] 100
// [5, null, 0, null] 200
// [] 300
// [0, null, 8] 400
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a{
LCW({1, null, 7}, valids), LCW({5, null, 0, null}, valids), LCW{}, LCW({0, null, 8}, valids)};
FCW b{100, 200, 300, 400};
FCW expected_a({1, null, 7, 5, null, 0, null, 0, null, 8}, {1, 0, 1, 1, 0, 1, 0, 1, 0, 1});
FCW expected_b{100, 100, 100, 200, 200, 200, 200, 400, 400, 400};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 2, 0, 1, 2, 3, 0, 1, 2};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, Nested)
{
// a b
// [[1, 2], [7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[],[5],[2, 1]] 300
LCW a{LCW{LCW{1, 2}, LCW{7, 6, 5}}, LCW{LCW{5, 6}}, LCW{LCW{0, 3}, LCW{}, LCW{5}, LCW{2, 1}}};
FCW b{100, 200, 300};
LCW expected_a{LCW{1, 2}, LCW{7, 6, 5}, LCW{5, 6}, LCW{0, 3}, LCW{}, LCW{5}, LCW{2, 1}};
FCW expected_b{100, 100, 200, 300, 300, 300, 300};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 0, 1, 2, 3};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, NestedNulls)
{
// a b
// [[1, 2], [7, 6, 5]] 100
// null null
// [[0, 3],[5],[2, 1]] 300
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
auto always_valid =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return true; });
LCW a({LCW{LCW{1, 2}, LCW{7, 6, 5}}, LCW{LCW{null}}, LCW{LCW{0, 3}, LCW{5}, LCW{2, 1}}}, valids);
FCW b({100, null, 300}, valids);
LCW expected_a{LCW{1, 2}, LCW{7, 6, 5}, LCW{0, 3}, LCW{5}, LCW{2, 1}};
FCW expected_b({100, 100, 300, 300, 300}, always_valid);
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 1, 2};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, NullsInNested)
{
// a b
// [[1, null], [7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[5],[2, null]] 300
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a({LCW{LCW({1, null}, valids), LCW{7, 6, 5}},
LCW{LCW{5, 6}},
LCW{LCW{0, 3}, LCW{5}, LCW({2, null}, valids)}});
FCW b({100, 200, 300});
LCW expected_a{
LCW({1, null}, valids), LCW{7, 6, 5}, LCW{5, 6}, LCW{0, 3}, LCW{5}, LCW({2, null}, valids)};
FCW expected_b{100, 100, 200, 300, 300, 300};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 0, 1, 2};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, NullsInNestedDoubleExplode)
{
// a b
// [[1, null], [], [7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[5],[2, null]] 300
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a{LCW{LCW({1, null}, valids), LCW{}, LCW{7, 6, 5}},
LCW{LCW{5, 6}},
LCW{LCW{0, 3}, LCW{5}, LCW({2, null}, valids)}};
FCW b{100, 200, 300};
FCW expected_a({1, null, 7, 6, 5, 5, 6, 0, 3, 5, 2, null}, {1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
FCW expected_b{100, 100, 100, 100, 100, 200, 200, 300, 300, 300, 300, 300};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto first_explode_ret = cudf::explode(t, 0);
auto ret = cudf::explode(first_explode_ret->view(), 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 1, 2, 0, 1, 0, 1, 0, 0, 1};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(first_explode_ret->view(), 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, NestedStructs)
{
// a b
// [[1, null], [7, 6, 5]] {100, "100"}
// [[5, 6]] {200, "200"}
// [[0, 3],[5],[2, null]] {300, "300"}
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a({LCW{LCW({1, null}, valids), LCW{7, 6, 5}},
LCW{LCW{5, 6}},
LCW{LCW{0, 3}, LCW{5}, LCW({2, null}, valids)}});
FCW b1({100, 200, 300});
cudf::test::strings_column_wrapper b2{"100", "200", "300"};
cudf::test::structs_column_wrapper b({b1, b2});
LCW expected_a{
LCW({1, null}, valids), LCW{7, 6, 5}, LCW{5, 6}, LCW{0, 3}, LCW{5}, LCW({2, null}, valids)};
FCW expected_b1{100, 100, 200, 300, 300, 300};
cudf::test::strings_column_wrapper expected_b2{"100", "100", "200", "300", "300", "300"};
cudf::test::structs_column_wrapper expected_b({expected_b1, expected_b2});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 0, 1, 2};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, ListOfStructsWithEmpties)
{
// a b
// [{1}}] "a"
// [{null}] "b"
// [null] "c"
// [] "d"
// null "e"
constexpr auto null = 0;
// row 0. 1 struct that contains a single int
cudf::test::fixed_width_column_wrapper<int32_t> i0{1};
std::vector<std::unique_ptr<cudf::column>> s0_cols;
s0_cols.push_back(i0.release());
cudf::test::structs_column_wrapper s0(std::move(s0_cols));
cudf::test::fixed_width_column_wrapper<int32_t> off0{0, 1};
auto row0 = cudf::make_lists_column(1, off0.release(), s0.release(), 0, rmm::device_buffer{});
// row 1. 1 struct that contains a null value
cudf::test::fixed_width_column_wrapper<int32_t> i1{{1}, {false}};
std::vector<std::unique_ptr<cudf::column>> s1_cols;
s1_cols.push_back(i1.release());
cudf::test::structs_column_wrapper s1(std::move(s1_cols));
cudf::test::fixed_width_column_wrapper<int32_t> off1{0, 1};
auto row1 = cudf::make_lists_column(1, off1.release(), s1.release(), 0, rmm::device_buffer{});
// row 2. 1 null struct
cudf::test::fixed_width_column_wrapper<int32_t> i2{0};
std::vector<std::unique_ptr<cudf::column>> s2_cols;
s2_cols.push_back(i2.release());
std::vector<bool> r2_valids{false};
auto [null_mask, null_count] =
cudf::test::detail::make_null_mask(r2_valids.begin(), r2_valids.end());
auto s2 = cudf::make_structs_column(1, std::move(s2_cols), null_count, std::move(null_mask));
cudf::test::fixed_width_column_wrapper<int32_t> off2{0, 1};
auto row2 = cudf::make_lists_column(1, off2.release(), std::move(s2), 0, rmm::device_buffer{});
// row 3. empty list.
cudf::test::fixed_width_column_wrapper<int32_t> i3{};
std::vector<std::unique_ptr<cudf::column>> s3_cols;
s3_cols.push_back(i3.release());
auto s3 = cudf::make_structs_column(0, std::move(s3_cols), 0, rmm::device_buffer{});
cudf::test::fixed_width_column_wrapper<int32_t> off3{0, 0};
auto row3 = cudf::make_lists_column(1, off3.release(), std::move(s3), 0, rmm::device_buffer{});
// row 4. null list
cudf::test::fixed_width_column_wrapper<int32_t> i4{};
std::vector<std::unique_ptr<cudf::column>> s4_cols;
s4_cols.push_back(i4.release());
auto s4 = cudf::make_structs_column(0, std::move(s4_cols), 0, rmm::device_buffer{});
cudf::test::fixed_width_column_wrapper<int32_t> off4{0, 0};
std::vector<bool> r4_valids{false};
std::tie(null_mask, null_count) =
cudf::test::detail::make_null_mask(r4_valids.begin(), r4_valids.end());
auto row4 =
cudf::make_lists_column(1, off4.release(), std::move(s4), null_count, std::move(null_mask));
// concatenated
auto final_col =
cudf::concatenate(std::vector<cudf::column_view>({*row0, *row1, *row2, *row3, *row4}));
auto s = cudf::test::strings_column_wrapper({"a", "b", "c", "d", "e"}).release();
cudf::table_view t({final_col->view(), s->view()});
auto ret = cudf::explode(t, 0);
auto expected_numeric_col =
cudf::test::fixed_width_column_wrapper<int32_t>{{1, null, null}, {1, 0, 0}};
auto expected_a = cudf::test::structs_column_wrapper{{expected_numeric_col}, {1, 1, 0}}.release();
auto expected_b = cudf::test::strings_column_wrapper({"a", "b", "c"}).release();
cudf::table_view expected({expected_a->view(), expected_b->view()});
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 0, 0};
cudf::table_view pos_expected({expected_pos_col, expected_a->view(), expected_b->view()});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TYPED_TEST(ExplodeTypedTest, ListOfStructs)
{
// a b
// [{70, "70"}, {75, "75"}] 100
// [{50, "50"}, {55, "55"}] 200
// [{35, "35"}, {45, "45"}] 300
// [{25, "25"}, {30, "30"}] 400
// [{15, "15"}, {20, "20"}] 500
auto numeric_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>{
{70, 75, 50, 55, 35, 45, 25, 30, 15, 20}};
cudf::test::strings_column_wrapper string_col{
"70", "75", "50", "55", "35", "45", "25", "30", "15", "20"};
auto struct_col = cudf::test::structs_column_wrapper{{numeric_col, string_col}}.release();
auto a =
cudf::make_lists_column(5, FCW{0, 2, 4, 6, 8, 10}.release(), std::move(struct_col), 0, {});
FCW b{100, 200, 300, 400, 500};
cudf::table_view t({a->view(), b});
auto ret = cudf::explode(t, 0);
auto expected_numeric_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>{
{70, 75, 50, 55, 35, 45, 25, 30, 15, 20}};
cudf::test::strings_column_wrapper expected_string_col{
"70", "75", "50", "55", "35", "45", "25", "30", "15", "20"};
auto expected_a =
cudf::test::structs_column_wrapper{{expected_numeric_col, expected_string_col}}.release();
FCW expected_b{100, 100, 200, 200, 300, 300, 400, 400, 500, 500};
cudf::table_view expected({expected_a->view(), expected_b});
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
cudf::table_view pos_expected({expected_pos_col, expected_a->view(), expected_b});
auto pos_ret = cudf::explode_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeTest, SlicedList)
{
// a b
// [[1, null],[7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[5],[2, null]] 300
// [[8, 3],[],[4, null, 1, null]] 400
// [[2, 3, 4],[9, 8]] 500
// slicing the top 2 rows and the bottom row off
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a({LCW{LCW({1, 2}, valids), LCW{7, 6, 5}},
LCW{LCW{5, 6}},
LCW{LCW{0, 3}, LCW{5}, LCW({2, 1}, valids)},
LCW{LCW{8, 3}, LCW{}, LCW({4, 3, 1, 2}, valids)},
LCW{LCW{2, 3, 4}, LCW{9, 8}}});
FCW b({100, 200, 300, 400, 500});
LCW expected_a{
LCW{0, 3}, LCW{5}, LCW({2, null}, valids), LCW{8, 3}, LCW{}, LCW({4, null, 1, null}, valids)};
FCW expected_b{300, 300, 300, 400, 400, 400};
cudf::table_view t({a, b});
auto sliced_t = cudf::slice(t, {2, 4});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode(sliced_t[0], 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 2, 0, 1, 2};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_position(sliced_t[0], 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, Empty)
{
LCW a{};
FCW b{};
cudf::table_view t({LCW{}, FCW{}});
auto ret = cudf::explode_outer(t, 0);
FCW expected_a{};
FCW expected_b{};
cudf::table_view expected({FCW{}, FCW{}});
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
}
TEST_F(ExplodeOuterTest, NonList)
{
cudf::table_view t({FCW{100, 200, 300}, FCW{100, 200, 300}});
EXPECT_THROW(cudf::explode_outer(t, 1), cudf::logic_error);
EXPECT_THROW(cudf::explode_outer_position(t, 1), cudf::logic_error);
}
TEST_F(ExplodeOuterTest, Basics)
{
// a b c
// 100 [1, 2, 7] string0
// 200 [5, 6] string1
// 300 [0, 3] string2
FCW a{100, 200, 300};
LCW b{LCW{1, 2, 7}, LCW{5, 6}, LCW{0, 3}};
cudf::test::strings_column_wrapper c{"string0", "string1", "string2"};
FCW expected_a{100, 100, 100, 200, 200, 300, 300};
FCW expected_b{1, 2, 7, 5, 6, 0, 3};
cudf::test::strings_column_wrapper expected_c{
"string0", "string0", "string0", "string1", "string1", "string2", "string2"};
cudf::table_view t({a, b, c});
cudf::table_view expected({expected_a, expected_b, expected_c});
auto ret = cudf::explode_outer(t, 1);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 2, 0, 1, 0, 1};
cudf::table_view pos_expected({expected_a, expected_pos_col, expected_b, expected_c});
auto pos_ret = cudf::explode_outer_position(t, 1);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, SingleNull)
{
// a b
// null 100
// [5, 6] 200
// [] 300
// [0, 3] 400
constexpr auto null = 0;
auto first_invalid =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i != 0; });
LCW a({LCW{null}, LCW{5, 6}, LCW{}, LCW{0, 3}}, first_invalid);
FCW b({100, 200, 300, 400});
FCW expected_a{{null, 5, 6, 0, 0, 3}, {0, 1, 1, 0, 1, 1}};
FCW expected_b{100, 200, 200, 300, 400, 400};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 0, 1, 0, 0, 1}, {0, 1, 1, 0, 1, 1}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, Nulls)
{
// a b
// [1, 2, 7] 100
// null null
// [0, 3] 300
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a({LCW{1, 2, 7}, LCW{null}, LCW{0, 3}}, valids);
FCW b({100, null, 300}, valids);
FCW expected_a({1, 2, 7, null, 0, 3}, {1, 1, 1, 0, 1, 1});
FCW expected_b({100, 100, 100, null, 300, 300}, {1, 1, 1, 0, 1, 1});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 1, 2, 0, 0, 1}, {1, 1, 1, 0, 1, 1}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, AllNulls)
{
// a b
// null 100
// null 200
// null 300
constexpr auto null = 0;
auto non_valid = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return false; });
LCW a({LCW{null}, LCW{null}, LCW{null}}, non_valid);
FCW b({100, 200, 300});
FCW expected_a({null, null, null}, {0, 0, 0});
FCW expected_b({100, 200, 300});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 0, 0}, {0, 0, 0}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, SequentialNulls)
{
// a b
// [1, 2, null] 100
// [3, 4] 200
// [] 300
// [] 400
// [5, 6, 7] 500
constexpr auto null = 0;
auto third_invalid =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i != 2; });
LCW a{LCW({1, 2, null}, third_invalid), LCW{3, 4}, LCW{}, LCW{}, LCW{5, 6, 7}};
FCW b{100, 200, 300, 400, 500};
FCW expected_a({1, 2, null, 3, 4, null, null, 5, 6, 7}, {1, 1, 0, 1, 1, 0, 0, 1, 1, 1});
FCW expected_b({100, 100, 100, 200, 200, 300, 400, 500, 500, 500});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 1, 2, 0, 1, 0, 0, 0, 1, 2}, {1, 1, 1, 1, 1, 0, 0, 1, 1, 1}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, MoreEmptyThanData)
{
// a b
// [1, 2] 100
// [] 200
// [] 300
// [] 400
// [] 500
// [3] 600
constexpr auto null = 0;
LCW a{LCW{1, 2}, LCW{}, LCW{}, LCW{}, LCW{}, LCW{3}};
FCW b{100, 200, 300, 400, 500, 600};
FCW expected_a({1, 2, null, null, null, null, 3}, {1, 1, 0, 0, 0, 0, 1});
FCW expected_b({100, 100, 200, 300, 400, 500, 600});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 1, 0, 0, 0, 0, 0}, {1, 1, 0, 0, 0, 0, 1}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, TrailingEmptys)
{
// a b
// [1, 2] 100
// [] 200
// [] 300
// [] 400
// [] 500
constexpr auto null = 0;
LCW a{LCW{1, 2}, LCW{}, LCW{}, LCW{}, LCW{}};
FCW b{100, 200, 300, 400, 500};
FCW expected_a({1, 2, null, null, null, null}, {1, 1, 0, 0, 0, 0});
FCW expected_b({100, 100, 200, 300, 400, 500});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 1, 0, 0, 0, 0}, {1, 1, 0, 0, 0, 0}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, LeadingNulls)
{
// a b
// null 100
// null 200
// null 300
// null 400
// [1, 2] 500
constexpr auto null = 0;
auto valids = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i == 4; });
LCW a({LCW{null}, LCW{null}, LCW{null}, LCW{null}, LCW{1, 2}}, valids);
FCW b{100, 200, 300, 400, 500};
FCW expected_a({null, null, null, null, 1, 2}, {0, 0, 0, 0, 1, 1});
FCW expected_b({100, 200, 300, 400, 500, 500});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 1, 1}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, NullsInList)
{
// a b
// [1, null, 7] 100
// [5, null, 0, null] 200
// [] 300
// [0, null, 8] 400
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a{
LCW({1, null, 7}, valids), LCW({5, null, 0, null}, valids), LCW{}, LCW({0, null, 8}, valids)};
FCW b{100, 200, 300, 400};
FCW expected_a({1, null, 7, 5, null, 0, null, null, 0, null, 8},
{1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1});
FCW expected_b{100, 100, 100, 200, 200, 200, 200, 300, 400, 400, 400};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 1, 2, 0, 1, 2, 3, 0, 0, 1, 2}, {1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, Nested)
{
// a b
// [[1, 2], [7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[],[5],[2, 1]] 300
LCW a{LCW{LCW{1, 2}, LCW{7, 6, 5}}, LCW{LCW{5, 6}}, LCW{LCW{0, 3}, LCW{}, LCW{5}, LCW{2, 1}}};
FCW b{100, 200, 300};
LCW expected_a{LCW{1, 2}, LCW{7, 6, 5}, LCW{5, 6}, LCW{0, 3}, LCW{}, LCW{5}, LCW{2, 1}};
FCW expected_b{100, 100, 200, 300, 300, 300, 300};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 0, 1, 2, 3};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, NestedNulls)
{
// a b
// [[1, 2], [7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[5],[2, 1]] 300
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a({LCW{LCW{1, 2}, LCW{7, 6, 5}}, LCW{LCW{null}}, LCW{LCW{0, 3}, LCW{5}, LCW{2, 1}}}, valids);
FCW b({100, 200, 300});
auto expected_valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i != 2; });
LCW expected_a({LCW{1, 2}, LCW{7, 6, 5}, LCW{null}, LCW{0, 3}, LCW{5}, LCW{2, 1}},
expected_valids);
FCW expected_b({100, 100, 200, 300, 300, 300});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 1, 0, 0, 1, 2}, {1, 1, 0, 1, 1, 1}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, NullsInNested)
{
// a b
// [[1, null], [7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[5],[2, null]] 300
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a({LCW{LCW({1, null}, valids), LCW{7, 6, 5}},
LCW{LCW{5, 6}},
LCW{LCW{0, 3}, LCW{5}, LCW({2, null}, valids)}});
FCW b({100, 200, 300});
LCW expected_a{
LCW({1, null}, valids), LCW{7, 6, 5}, LCW{5, 6}, LCW{0, 3}, LCW{5}, LCW({2, null}, valids)};
FCW expected_b{100, 100, 200, 300, 300, 300};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 0, 1, 2};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, NullsInNestedDoubleExplode)
{
// a b
// [[1, null], [], [7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[5],[2, null]] 300
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a{LCW{LCW({1, null}, valids), LCW{}, LCW{7, 6, 5}},
LCW{LCW{5, 6}},
LCW{LCW{0, 3}, LCW{5}, LCW({2, null}, valids)}};
FCW b{100, 200, 300};
FCW expected_a({1, null, null, 7, 6, 5, 5, 6, 0, 3, 5, 2, null},
{1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
FCW expected_b{100, 100, 100, 100, 100, 100, 200, 200, 300, 300, 300, 300, 300};
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto first_explode_ret = cudf::explode_outer(t, 0);
auto ret = cudf::explode_outer(first_explode_ret->view(), 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 1, 0, 0, 1, 2, 0, 1, 0, 1, 0, 0, 1},
{1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(first_explode_ret->view(), 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, NestedStructs)
{
// a b
// [[1, null], [7, 6, 5]] {100, "100"}
// [[5, 6]] {200, "200"}
// [[0, 3],[5],[2, null]] {300, "300"}
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a({LCW{LCW({1, null}, valids), LCW{7, 6, 5}},
LCW{LCW{5, 6}},
LCW{LCW{0, 3}, LCW{5}, LCW({2, null}, valids)}});
FCW b1({100, 200, 300});
cudf::test::strings_column_wrapper b2{"100", "200", "300"};
cudf::test::structs_column_wrapper b({b1, b2});
LCW expected_a{
LCW({1, null}, valids), LCW{7, 6, 5}, LCW{5, 6}, LCW{0, 3}, LCW{5}, LCW({2, null}, valids)};
FCW expected_b1{100, 100, 200, 300, 300, 300};
cudf::test::strings_column_wrapper expected_b2{"100", "100", "200", "300", "300", "300"};
cudf::test::structs_column_wrapper expected_b({expected_b1, expected_b2});
cudf::table_view t({a, b});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 0, 1, 2};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, ListOfStructsWithEmpties)
{
// a b
// [{1}}] "a"
// [{null}] "b"
// [null] "c"
// [] "d"
// null "e"
constexpr auto null = 0;
// row 0. 1 struct that contains a single int
cudf::test::fixed_width_column_wrapper<int32_t> i0{1};
std::vector<std::unique_ptr<cudf::column>> s0_cols;
s0_cols.push_back(i0.release());
cudf::test::structs_column_wrapper s0(std::move(s0_cols));
cudf::test::fixed_width_column_wrapper<int32_t> off0{0, 1};
auto row0 = cudf::make_lists_column(1, off0.release(), s0.release(), 0, rmm::device_buffer{});
// row 1. 1 struct that contains a null value
cudf::test::fixed_width_column_wrapper<int32_t> i1{{1}, {false}};
std::vector<std::unique_ptr<cudf::column>> s1_cols;
s1_cols.push_back(i1.release());
cudf::test::structs_column_wrapper s1(std::move(s1_cols));
cudf::test::fixed_width_column_wrapper<int32_t> off1{0, 1};
auto row1 = cudf::make_lists_column(1, off1.release(), s1.release(), 0, rmm::device_buffer{});
// row 2. 1 null struct
cudf::test::fixed_width_column_wrapper<int32_t> i2{0};
std::vector<std::unique_ptr<cudf::column>> s2_cols;
s2_cols.push_back(i2.release());
std::vector<bool> r2_valids{false};
auto [null_mask, null_count] =
cudf::test::detail::make_null_mask(r2_valids.begin(), r2_valids.end());
auto s2 = cudf::make_structs_column(1, std::move(s2_cols), null_count, std::move(null_mask));
cudf::test::fixed_width_column_wrapper<int32_t> off2{0, 1};
auto row2 = cudf::make_lists_column(1, off2.release(), std::move(s2), 0, rmm::device_buffer{});
// row 3. empty list.
cudf::test::fixed_width_column_wrapper<int32_t> i3{};
std::vector<std::unique_ptr<cudf::column>> s3_cols;
s3_cols.push_back(i3.release());
auto s3 = cudf::make_structs_column(0, std::move(s3_cols), 0, rmm::device_buffer{});
cudf::test::fixed_width_column_wrapper<int32_t> off3{0, 0};
auto row3 = cudf::make_lists_column(1, off3.release(), std::move(s3), 0, rmm::device_buffer{});
// row 4. null list
cudf::test::fixed_width_column_wrapper<int32_t> i4{};
std::vector<std::unique_ptr<cudf::column>> s4_cols;
s4_cols.push_back(i4.release());
auto s4 = cudf::make_structs_column(0, std::move(s4_cols), 0, rmm::device_buffer{});
cudf::test::fixed_width_column_wrapper<int32_t> off4{0, 0};
std::vector<bool> r4_valids{false};
std::tie(null_mask, null_count) =
cudf::test::detail::make_null_mask(r4_valids.begin(), r4_valids.end());
auto row4 =
cudf::make_lists_column(1, off4.release(), std::move(s4), null_count, std::move(null_mask));
// concatenated
auto final_col =
cudf::concatenate(std::vector<cudf::column_view>({*row0, *row1, *row2, *row3, *row4}));
auto s = cudf::test::strings_column_wrapper({"a", "b", "c", "d", "e"}).release();
cudf::table_view t({final_col->view(), s->view()});
auto ret = cudf::explode_outer(t, 0);
auto expected_numeric_col =
cudf::test::fixed_width_column_wrapper<int32_t>{{1, null, null, null, null}, {1, 0, 0, 0, 0}};
auto expected_a =
cudf::test::structs_column_wrapper{{expected_numeric_col}, {1, 1, 0, 0, 0}}.release();
auto expected_b = cudf::test::strings_column_wrapper({"a", "b", "c", "d", "e"}).release();
cudf::table_view expected({expected_a->view(), expected_b->view()});
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{{0, 0, 0, null, null}, {1, 1, 1, 0, 0}};
cudf::table_view pos_expected({expected_pos_col, expected_a->view(), expected_b->view()});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TYPED_TEST(ExplodeOuterTypedTest, ListOfStructs)
{
// a b
// [{70, "70"}, {75, "75"}] 100
// [{50, "50"}, {55, "55"}] 200
// [{35, "35"}, {45, "45"}] 300
// [{25, "25"}, {30, "30"}] 400
// [{15, "15"}, {20, "20"}] 500
auto numeric_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>{
{70, 75, 50, 55, 35, 45, 25, 30, 15, 20}};
cudf::test::strings_column_wrapper string_col{
"70", "75", "50", "55", "35", "45", "25", "30", "15", "20"};
auto struct_col = cudf::test::structs_column_wrapper{{numeric_col, string_col}}.release();
auto a =
cudf::make_lists_column(5, FCW{0, 2, 4, 6, 8, 10}.release(), std::move(struct_col), 0, {});
FCW b{100, 200, 300, 400, 500};
cudf::table_view t({a->view(), b});
auto ret = cudf::explode_outer(t, 0);
auto expected_numeric_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>{
{70, 75, 50, 55, 35, 45, 25, 30, 15, 20}};
cudf::test::strings_column_wrapper expected_string_col{
"70", "75", "50", "55", "35", "45", "25", "30", "15", "20"};
auto expected_a =
cudf::test::structs_column_wrapper{{expected_numeric_col, expected_string_col}}.release();
FCW expected_b{100, 100, 200, 200, 300, 300, 400, 400, 500, 500};
cudf::table_view expected({expected_a->view(), expected_b});
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
cudf::table_view pos_expected({expected_pos_col, expected_a->view(), expected_b});
auto pos_ret = cudf::explode_outer_position(t, 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
TEST_F(ExplodeOuterTest, SlicedList)
{
// a b
// [[1, null],[7, 6, 5]] 100
// [[5, 6]] 200
// [[0, 3],[5],[2, null]] 300
// [[8, 3],[],[4, null, 1, null]] 400
// [[2, 3, 4],[9, 8]] 500
// slicing the top 2 rows and the bottom row off
constexpr auto null = 0;
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
LCW a({LCW{LCW({1, null}, valids), LCW{7, 6, 5}},
LCW{LCW{5, 6}},
LCW{LCW{0, 3}, LCW{5}, LCW({2, null}, valids)},
LCW{LCW{8, 3}, LCW{}, LCW({4, null, 1, null}, valids)},
LCW{LCW{2, 3, 4}, LCW{9, 8}}});
FCW b({100, 200, 300, 400, 500});
LCW expected_a{
LCW{0, 3}, LCW{5}, LCW({2, null}, valids), LCW{8, 3}, LCW{}, LCW({4, null, 1, null}, valids)};
FCW expected_b{300, 300, 300, 400, 400, 400};
cudf::table_view t({a, b});
auto sliced_t = cudf::slice(t, {2, 4});
cudf::table_view expected({expected_a, expected_b});
auto ret = cudf::explode_outer(sliced_t[0], 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(ret->view(), expected);
FCW expected_pos_col{0, 1, 2, 0, 1, 2};
cudf::table_view pos_expected({expected_pos_col, expected_a, expected_b});
auto pos_ret = cudf::explode_outer_position(sliced_t[0], 0);
CUDF_TEST_EXPECT_TABLES_EQUAL(pos_ret->view(), pos_expected);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/lists/sequences_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/lists/filling.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
using namespace cudf::test::iterators;
namespace {
template <typename T, typename U = int32_t>
using ListsCol = cudf::test::lists_column_wrapper<T, U>;
template <typename T, typename U = int32_t>
using FWDCol = cudf::test::fixed_width_column_wrapper<T, U>;
using IntsCol = cudf::test::fixed_width_column_wrapper<int32_t>;
} // namespace
/*-----------------------------------------------------------------------------------------------*/
template <typename T>
class NumericSequencesTypedTest : public cudf::test::BaseFixture {};
using NumericTypes =
cudf::test::Concat<cudf::test::IntegralTypesNotBool, cudf::test::FloatingPointTypes>;
TYPED_TEST_SUITE(NumericSequencesTypedTest, NumericTypes);
TYPED_TEST(NumericSequencesTypedTest, SimpleTestNoNull)
{
using T = TypeParam;
auto const starts = FWDCol<T>{1, 2, 3};
auto const sizes = IntsCol{5, 3, 4};
// Sequences with step == 1.
{
auto const expected =
ListsCol<T>{ListsCol<T>{1, 2, 3, 4, 5}, ListsCol<T>{2, 3, 4}, ListsCol<T>{3, 4, 5, 6}};
auto const result = cudf::lists::sequences(starts, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
// Sequences with various steps.
{
auto const steps = FWDCol<T>{1, 3, 2};
auto const expected =
ListsCol<T>{ListsCol<T>{1, 2, 3, 4, 5}, ListsCol<T>{2, 5, 8}, ListsCol<T>{3, 5, 7, 9}};
auto const result = cudf::lists::sequences(starts, steps, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
TYPED_TEST(NumericSequencesTypedTest, ZeroSizesTest)
{
using T = TypeParam;
auto const starts = FWDCol<T>{1, 2, 3};
auto const sizes = IntsCol{0, 3, 0};
// Sequences with step == 1.
{
auto const expected = ListsCol<T>{ListsCol<T>{}, ListsCol<T>{2, 3, 4}, ListsCol<T>{}};
auto const result = cudf::lists::sequences(starts, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
// Sequences with various steps.
{
auto const steps = FWDCol<T>{1, 3, 2};
auto const expected = ListsCol<T>{ListsCol<T>{}, ListsCol<T>{2, 5, 8}, ListsCol<T>{}};
auto const result = cudf::lists::sequences(starts, steps, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
TYPED_TEST(NumericSequencesTypedTest, SlicedInputTestNoNulls)
{
using T = TypeParam;
constexpr int32_t dont_care{123};
auto const starts_original =
FWDCol<T>{dont_care, dont_care, dont_care, 1, 2, 3, 4, 5, dont_care, dont_care};
auto const sizes_original = IntsCol{dont_care, 5, 3, 4, 1, 2, dont_care, dont_care};
auto const starts = cudf::slice(starts_original, {3, 8})[0];
auto const sizes = cudf::slice(sizes_original, {1, 6})[0];
// Sequences with step == 1.
{
auto const expected = ListsCol<T>{ListsCol<T>{1, 2, 3, 4, 5},
ListsCol<T>{2, 3, 4},
ListsCol<T>{3, 4, 5, 6},
ListsCol<T>{4},
ListsCol<T>{5, 6}
};
auto const result = cudf::lists::sequences(starts, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
// Sequences with various steps.
{
auto const steps_original = FWDCol<T>{dont_care, dont_care, 1, 3, 2, 2, 3, dont_care};
auto const steps = cudf::slice(steps_original, {2, 7})[0];
auto const expected = ListsCol<T>{ListsCol<T>{1, 2, 3, 4, 5},
ListsCol<T>{2, 5, 8},
ListsCol<T>{3, 5, 7, 9},
ListsCol<T>{4},
ListsCol<T>{5, 8}
};
auto const result = cudf::lists::sequences(starts, steps, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
/*-----------------------------------------------------------------------------------------------*/
// Data generated using https://www.epochconverter.com/
template <typename T>
class DurationSequencesTypedTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(DurationSequencesTypedTest, cudf::test::DurationTypes);
// Start time is 1638477473L - Thursday, December 2, 2021 8:37:53 PM.
constexpr int64_t start_time = 1638477473L;
TYPED_TEST(DurationSequencesTypedTest, SequencesNoNull)
{
using T = TypeParam;
auto const starts = FWDCol<T, int64_t>{start_time, start_time, start_time};
auto const sizes = IntsCol{1, 2, 3};
// Sequences with step == 1.
{
auto const expected_h = std::vector<int64_t>{start_time, start_time + 1L, start_time + 2L};
auto const expected =
ListsCol<T, int64_t>{ListsCol<T, int64_t>{expected_h.begin(), expected_h.begin() + 1},
ListsCol<T, int64_t>{expected_h.begin(), expected_h.begin() + 2},
ListsCol<T, int64_t>{expected_h.begin(), expected_h.begin() + 3}};
auto const result = cudf::lists::sequences(starts, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
// Sequences with various steps, including negative.
{
auto const steps = FWDCol<T, int64_t>{10L, -155L, -13L};
auto const expected = ListsCol<T, int64_t>{
ListsCol<T, int64_t>{start_time},
ListsCol<T, int64_t>{start_time, start_time - 155L},
ListsCol<T, int64_t>{start_time, start_time - 13L, start_time - 13L * 2L}};
auto const result = cudf::lists::sequences(starts, steps, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
/*-----------------------------------------------------------------------------------------------*/
class NumericSequencesTest : public cudf::test::BaseFixture {};
TEST_F(NumericSequencesTest, EmptyInput)
{
auto const starts = IntsCol{};
auto const sizes = IntsCol{};
auto const steps = IntsCol{};
auto const expected = ListsCol<int32_t>{};
// Sequences with step == 1.
{
auto const result = cudf::lists::sequences(starts, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
// Sequences with given steps.
{
auto const result = cudf::lists::sequences(starts, steps, sizes);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
TEST_F(NumericSequencesTest, InvalidSizesInput)
{
auto const starts = IntsCol{};
auto const steps = IntsCol{};
auto const sizes = FWDCol<float>{};
EXPECT_THROW(cudf::lists::sequences(starts, sizes), cudf::logic_error);
EXPECT_THROW(cudf::lists::sequences(starts, steps, sizes), cudf::logic_error);
}
TEST_F(NumericSequencesTest, MismatchedColumnSizesInput)
{
auto const starts = IntsCol{1, 2, 3};
auto const steps = IntsCol{1, 2};
auto const sizes = IntsCol{1, 2, 3, 4};
EXPECT_THROW(cudf::lists::sequences(starts, sizes), cudf::logic_error);
EXPECT_THROW(cudf::lists::sequences(starts, steps, sizes), cudf::logic_error);
}
TEST_F(NumericSequencesTest, MismatchedColumnTypesInput)
{
auto const starts = IntsCol{1, 2, 3};
auto const steps = FWDCol<float>{1, 2, 3};
auto const sizes = IntsCol{1, 2, 3};
EXPECT_THROW(cudf::lists::sequences(starts, steps, sizes), cudf::logic_error);
}
TEST_F(NumericSequencesTest, InputHasNulls)
{
constexpr int32_t null{0};
{
auto const starts = IntsCol{{null, 2, 3}, null_at(0)};
auto const sizes = IntsCol{1, 2, 3};
EXPECT_THROW(cudf::lists::sequences(starts, sizes), cudf::logic_error);
}
{
auto const starts = IntsCol{1, 2, 3};
auto const sizes = IntsCol{{null, 2, 3}, null_at(0)};
EXPECT_THROW(cudf::lists::sequences(starts, sizes), cudf::logic_error);
}
{
auto const starts = IntsCol{1, 2, 3};
auto const steps = IntsCol{{null, 2, 3}, null_at(0)};
auto const sizes = IntsCol{1, 2, 3};
EXPECT_THROW(cudf::lists::sequences(starts, steps, sizes), cudf::logic_error);
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/lists/count_elements_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/copying.hpp>
#include <cudf/lists/count_elements.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/transform_iterator.h>
struct ListsElementsTest : public cudf::test::BaseFixture {};
using NumericTypesNotBool =
cudf::test::Concat<cudf::test::IntegralTypesNotBool, cudf::test::FloatingPointTypes>;
template <typename T>
class ListsElementsNumericsTest : public ListsElementsTest {};
TYPED_TEST_SUITE(ListsElementsNumericsTest, NumericTypesNotBool);
TYPED_TEST(ListsElementsNumericsTest, CountElements)
{
auto validity = thrust::make_transform_iterator(
thrust::make_counting_iterator<cudf::size_type>(0), [](auto i) { return i != 1; });
using LCW = cudf::test::lists_column_wrapper<TypeParam>;
LCW input({LCW{3, 2, 1}, LCW{}, LCW{30, 20, 10, 50}, LCW{100, 120}, LCW{0}}, validity);
auto result = cudf::lists::count_elements(cudf::lists_column_view(input));
cudf::test::fixed_width_column_wrapper<int32_t> expected({3, 0, 4, 2, 1}, {1, 0, 1, 1, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(ListsElementsTest, CountElementsStrings)
{
auto validity = thrust::make_transform_iterator(
thrust::make_counting_iterator<cudf::size_type>(0), [](auto i) { return i != 1; });
using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
LCW input(
{LCW{"", "Héllo", "thesé"}, LCW{}, LCW{"are", "some", "", "z"}, LCW{"tést", "String"}, LCW{""}},
validity);
auto result = cudf::lists::count_elements(cudf::lists_column_view(input));
cudf::test::fixed_width_column_wrapper<int32_t> expected({3, 0, 4, 2, 1}, {1, 0, 1, 1, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(ListsElementsTest, CountElementsSliced)
{
auto validity = thrust::make_transform_iterator(
thrust::make_counting_iterator<cudf::size_type>(0), [](auto i) { return i != 1; });
using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
LCW input(
{LCW{"", "Héllo", "thesé"}, LCW{}, LCW{"are", "some", "", "z"}, LCW{"tést", "String"}, LCW{""}},
validity);
auto sliced = cudf::slice(input, {1, 4}).front();
auto result = cudf::lists::count_elements(cudf::lists_column_view(sliced));
cudf::test::fixed_width_column_wrapper<int32_t> expected({0, 4, 2}, {0, 1, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TYPED_TEST(ListsElementsNumericsTest, CountElementsNestedLists)
{
std::vector<int32_t> validity{1, 0, 1, 1};
using LCW = cudf::test::lists_column_wrapper<TypeParam>;
LCW list({LCW{LCW{2, 3}, LCW{4, 5}},
LCW{LCW{}},
LCW{LCW{6, 7, 8}, LCW{9, 10, 11}, LCW({12, 13, 14}, validity.begin())},
LCW{LCW{15, 16}, LCW{17, 18}, LCW{19, 20}, LCW{21, 22}, LCW{23, 24}}},
validity.begin());
auto result = cudf::lists::count_elements(cudf::lists_column_view(list));
cudf::test::fixed_width_column_wrapper<int32_t> expected({2, 1, 3, 5}, {1, 0, 1, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(ListsElementsTest, CountElementsEmpty)
{
using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
LCW empty{};
auto result = cudf::lists::count_elements(cudf::lists_column_view(empty));
EXPECT_EQ(0, result->size());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/lists/extract_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/detail/null_mask.hpp>
#include <cudf/lists/extract.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/iterator/transform_iterator.h>
#include <vector>
struct ListsExtractTest : public cudf::test::BaseFixture {};
using NumericTypesNotBool =
cudf::test::Concat<cudf::test::IntegralTypesNotBool, cudf::test::FloatingPointTypes>;
template <typename T>
class ListsExtractNumericsTest : public ListsExtractTest {};
TYPED_TEST_SUITE(ListsExtractNumericsTest, NumericTypesNotBool);
TYPED_TEST(ListsExtractNumericsTest, ExtractElement)
{
auto validity = thrust::make_transform_iterator(
thrust::make_counting_iterator<cudf::size_type>(0), [](auto i) { return i != 1; });
using LCW = cudf::test::lists_column_wrapper<TypeParam>;
LCW input({LCW{3, 2, 1}, LCW{}, LCW{30, 20, 10, 50}, LCW{100, 120}, LCW{0}}, validity);
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 0);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({3, 0, 30, 100, 0}, {1, 0, 1, 1, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 1);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({2, 0, 20, 120, 0}, {1, 0, 1, 1, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 2);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({1, 0, 10, 0, 0}, {1, 0, 1, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 3);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({0, 0, 50, 0, 0}, {0, 0, 1, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 4);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({0, 0, 0, 0, 0}, {0, 0, 0, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -1);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({1, 0, 50, 120, 0}, {1, 0, 1, 1, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -2);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({2, 0, 10, 100, 0}, {1, 0, 1, 1, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -3);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({3, 0, 20, 0, 0}, {1, 0, 1, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -4);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({0, 0, 30, 0, 0}, {0, 0, 1, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -5);
cudf::test::fixed_width_column_wrapper<TypeParam> expected({0, 0, 0, 0, 0}, {0, 0, 0, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
TEST_F(ListsExtractTest, ExtractElementStrings)
{
auto validity = thrust::make_transform_iterator(
thrust::make_counting_iterator<cudf::size_type>(0), [](auto i) { return i != 1; });
using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
LCW input(
{LCW{"", "Héllo", "thesé"}, LCW{}, LCW{"are", "some", "", "z"}, LCW{"tést", "String"}, LCW{""}},
validity);
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 0);
cudf::test::strings_column_wrapper expected({"", "", "are", "tést", ""}, {1, 0, 1, 1, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 1);
cudf::test::strings_column_wrapper expected({"Héllo", "", "some", "String", ""},
{1, 0, 1, 1, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 2);
cudf::test::strings_column_wrapper expected({"thesé", "", "", "", ""}, {1, 0, 1, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 3);
cudf::test::strings_column_wrapper expected({"", "", "z", "", ""}, {0, 0, 1, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 4);
cudf::test::strings_column_wrapper expected({"", "", "", "", ""}, {0, 0, 0, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -1);
cudf::test::strings_column_wrapper expected({"thesé", "", "z", "String", ""}, {1, 0, 1, 1, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -2);
cudf::test::strings_column_wrapper expected({"Héllo", "", "", "tést", ""}, {1, 0, 1, 1, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -3);
cudf::test::strings_column_wrapper expected({"", "", "some", "", ""}, {1, 0, 1, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -4);
cudf::test::strings_column_wrapper expected({"", "", "are", "", ""}, {0, 0, 1, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -5);
cudf::test::strings_column_wrapper expected({"", "", "", "", ""}, {0, 0, 0, 0, 0});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
TYPED_TEST(ListsExtractNumericsTest, ExtractElementNestedLists)
{
std::vector<int32_t> validity{1, 0, 1, 1};
using LCW = cudf::test::lists_column_wrapper<TypeParam>;
LCW list({LCW{LCW{2, 3}, LCW{4, 5}},
LCW{LCW{}},
LCW{LCW{6, 7, 8}, LCW{9, 10, 11}, LCW{12, 13, 14}},
LCW{LCW{15, 16}, LCW{17, 18}, LCW{19, 20}, LCW{21, 22}, LCW{23, 24}}},
validity.begin());
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(list), 0);
LCW expected({LCW{2, 3}, LCW{}, LCW{6, 7, 8}, LCW{15, 16}}, validity.begin());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(list), 1);
LCW expected({LCW{4, 5}, LCW{}, LCW{9, 10, 11}, LCW{17, 18}}, validity.begin());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(list), 2);
std::vector<int32_t> expected_validity{0, 0, 1, 1};
LCW expected({LCW{}, LCW{}, LCW{12, 13, 14}, LCW{19, 20}}, expected_validity.begin());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(list), 3);
std::vector<int32_t> expected_validity{0, 0, 0, 1};
LCW expected({LCW{}, LCW{}, LCW{}, LCW{21, 22}}, expected_validity.begin());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(list), -1);
LCW expected({LCW{4, 5}, LCW{}, LCW{12, 13, 14}, LCW{23, 24}}, validity.begin());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(list), -2);
LCW expected({LCW{2, 3}, LCW{}, LCW{9, 10, 11}, LCW{21, 22}}, validity.begin());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(list), -3);
std::vector<int32_t> expected_validity{0, 0, 1, 1};
LCW expected({LCW{}, LCW{}, LCW{6, 7, 8}, LCW{19, 20}}, expected_validity.begin());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
TEST_F(ListsExtractTest, ExtractElementEmpty)
{
using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
LCW empty{};
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(empty), 1);
EXPECT_EQ(cudf::data_type{cudf::type_id::STRING}, result->type());
EXPECT_EQ(0, result->size());
LCW empty_strings({LCW{"", "", ""}});
result = cudf::lists::extract_list_element(cudf::lists_column_view(empty_strings), 1);
cudf::test::strings_column_wrapper expected({""});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *result);
LCW null_strings({LCW{"", "", ""}}, thrust::make_constant_iterator<int32_t>(0));
result = cudf::lists::extract_list_element(cudf::lists_column_view(null_strings), 1);
cudf::test::strings_column_wrapper expected_null({""}, {0});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_null, *result);
}
TEST_F(ListsExtractTest, ExtractElementWithNulls)
{
auto validity = thrust::make_transform_iterator(
thrust::make_counting_iterator<cudf::size_type>(0), [](auto i) { return i != 1; });
using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
LCW input{
{{"Héllo", "", "thesé"}, validity}, {"are"}, {{"some", ""}, validity}, {"tést", "strings"}};
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 0);
cudf::test::strings_column_wrapper expected({"Héllo", "are", "some", "tést"});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), 1);
cudf::test::strings_column_wrapper expected({"", "", "", "strings"}, {0, 0, 0, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *result);
}
{
auto result = cudf::lists::extract_list_element(cudf::lists_column_view(input), -1);
cudf::test::strings_column_wrapper expected({"thesé", "are", "", "strings"}, {1, 1, 0, 1});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *result);
}
}
struct ListsExtractColumnIndicesTest : ListsExtractTest {};
template <typename T>
struct ListsExtractColumnIndicesTypedTest : ListsExtractColumnIndicesTest {};
TYPED_TEST_SUITE(ListsExtractColumnIndicesTypedTest, cudf::test::FixedWidthTypes);
TYPED_TEST(ListsExtractColumnIndicesTypedTest, ExtractElement)
{
using LCW = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
using FWCW = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using indices = cudf::test::fixed_width_column_wrapper<cudf::size_type>;
auto input_column = LCW({LCW{3, 2, 1}, LCW{}, LCW{30, 20, 10, 50}, LCW{100, 120}, LCW{0}, LCW{}},
cudf::test::iterators::null_at(1));
auto input = cudf::lists_column_view(input_column);
{
// Test fetching first element.
auto result = cudf::lists::extract_list_element(input, indices{0, 0, 0, 0, 0, 0});
auto expected = FWCW({3, 0, 30, 100, 0, 0}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching last element.
auto result = cudf::lists::extract_list_element(input, indices{2, 0, 3, 1, 0, 0});
auto expected = FWCW({1, 0, 50, 120, 0, 0}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching *all* out of bounds.
auto result = cudf::lists::extract_list_element(input, indices{9, 9, 9, 9, 9, 9});
auto expected = FWCW({0, 0, 0, 0, 0, 0}, cudf::test::iterators::all_nulls());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching first from the end.
auto result = cudf::lists::extract_list_element(input, indices{-1, -1, -1, -1, -1, -1});
auto expected = FWCW({1, 0, 50, 120, 0, 0}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching last from the end.
auto result = cudf::lists::extract_list_element(input, indices{-3, 0, -4, -2, -1, 0});
auto expected = FWCW({3, 0, 30, 100, 0, 0}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching *all* negative out of bounds.
auto result = cudf::lists::extract_list_element(input, indices{-9, -9, -9, -9, -9, -9});
auto expected = FWCW({0, 0, 0, 0, 0, 0}, cudf::test::iterators::all_nulls());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test mixed indices.
auto result = cudf::lists::extract_list_element(input, indices{-2, 0, 3, -1, 0, 0});
auto expected = FWCW({2, 0, 50, 120, 0, 0}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test possibly null indices.
auto result = cudf::lists::extract_list_element(
input, indices{{-2, 0, 3, -1, 0, 0}, cudf::test::iterators::nulls_at({2, 4})});
auto expected = FWCW({2, 0, 50, 120, 0, 0}, cudf::test::iterators::nulls_at({1, 2, 4, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
TYPED_TEST(ListsExtractColumnIndicesTypedTest, FailureCases)
{
using LCW = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
using indices = cudf::test::fixed_width_column_wrapper<cudf::size_type>;
{
// Non-empty input, with mismatched size of indices.
auto input_column =
LCW({LCW{3, 2, 1}, LCW{}, LCW{30, 20, 10, 50}, LCW{100, 120}, LCW{0}, LCW{}},
cudf::test::iterators::null_at(1));
auto input = cudf::lists_column_view(input_column);
EXPECT_THROW(cudf::lists::extract_list_element(input, indices{0, 1, 2}), cudf::logic_error);
}
{
// Non-empty input, with empty indices.
auto input_column =
LCW({LCW{3, 2, 1}, LCW{}, LCW{30, 20, 10, 50}, LCW{100, 120}, LCW{0}, LCW{}},
cudf::test::iterators::null_at(1));
auto input = cudf::lists_column_view(input_column);
EXPECT_THROW(cudf::lists::extract_list_element(input, indices{}), cudf::logic_error);
}
{
// Empty input, with mismatched size of indices.
auto input_column = LCW{};
auto input = cudf::lists_column_view(input_column);
EXPECT_THROW(cudf::lists::extract_list_element(input, indices{0, 1, 2}), cudf::logic_error);
}
}
TEST_F(ListsExtractColumnIndicesTest, ExtractStrings)
{
using LCW = cudf::test::lists_column_wrapper<cudf::string_view>;
using strings = cudf::test::strings_column_wrapper;
using indices = cudf::test::fixed_width_column_wrapper<cudf::size_type>;
auto input_column = LCW(
{LCW{"3", "2", "1"}, LCW{}, LCW{"30", "20", "10", "50"}, LCW{"100", "120"}, LCW{"0"}, LCW{}},
cudf::test::iterators::null_at(1));
auto input = cudf::lists_column_view(input_column);
{
// Test fetching first element.
auto result = cudf::lists::extract_list_element(input, indices{0, 0, 0, 0, 0, 0});
auto expected =
strings({"3", "", "30", "100", "0", ""}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching last element.
auto result = cudf::lists::extract_list_element(input, indices{2, 0, 3, 1, 0, 0});
auto expected =
strings({"1", "", "50", "120", "0", ""}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching *all* out of bounds.
auto result = cudf::lists::extract_list_element(input, indices{9, 9, 9, 9, 9, 9});
auto expected = strings({"", "", "", "", "", ""}, cudf::test::iterators::all_nulls());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching first from the end.
auto result = cudf::lists::extract_list_element(input, indices{-1, -1, -1, -1, -1, -1});
auto expected =
strings({"1", "", "50", "120", "0", ""}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching last from the end.
auto result = cudf::lists::extract_list_element(input, indices{-3, 0, -4, -2, -1, 0});
auto expected =
strings({"3", "", "30", "100", "0", ""}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test fetching *all* negative out of bounds.
auto result = cudf::lists::extract_list_element(input, indices{-9, -9, -9, -9, -9, -9});
auto expected = strings({"", "", "", "", "", ""}, cudf::test::iterators::all_nulls());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test mixed indices.
auto result = cudf::lists::extract_list_element(input, indices{-2, 0, 3, -1, 0, 0});
auto expected =
strings({"2", "", "50", "120", "0", ""}, cudf::test::iterators::nulls_at({1, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
// Test possibly null indices.
auto result = cudf::lists::extract_list_element(
input, indices{{-2, 0, 3, -1, 0, 0}, cudf::test::iterators::nulls_at({2, 4})});
auto expected =
strings({"2", "", "50", "120", "", ""}, cudf::test::iterators::nulls_at({1, 2, 4, 5}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests/lists
|
rapidsai_public_repos/cudf/cpp/tests/lists/set_operations/difference_distinct_tests.cpp
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/lists/set_operations.hpp>
#include <cudf/lists/sorting.hpp>
#include <cudf/null_mask.hpp>
#include <limits>
#include <string>
using float_type = double;
using namespace cudf::test::iterators;
auto constexpr null{0}; // null at current level
auto constexpr XXX{0}; // null pushed down from parent level
auto constexpr neg_NaN = -std::numeric_limits<float_type>::quiet_NaN();
auto constexpr neg_Inf = -std::numeric_limits<float_type>::infinity();
auto constexpr NaN = std::numeric_limits<float_type>::quiet_NaN();
auto constexpr Inf = std::numeric_limits<float_type>::infinity();
auto constexpr NULL_EQUAL = cudf::null_equality::EQUAL;
auto constexpr NULL_UNEQUAL = cudf::null_equality::UNEQUAL;
auto constexpr NAN_EQUAL = cudf::nan_equality::ALL_EQUAL;
auto constexpr NAN_UNEQUAL = cudf::nan_equality::UNEQUAL;
using bools_col = cudf::test::fixed_width_column_wrapper<bool>;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_lists = cudf::test::lists_column_wrapper<float_type>;
using strings_lists = cudf::test::lists_column_wrapper<cudf::string_view>;
using strings_col = cudf::test::strings_column_wrapper;
using structs_col = cudf::test::structs_column_wrapper;
using lists_cv = cudf::lists_column_view;
namespace {
auto set_difference_sorted(cudf::column_view const& lhs,
cudf::column_view const& rhs,
cudf::null_equality nulls_equal = NULL_EQUAL,
cudf::nan_equality nans_equal = NAN_EQUAL)
{
auto const results =
cudf::lists::difference_distinct(lists_cv{lhs}, lists_cv{rhs}, nulls_equal, nans_equal);
return cudf::lists::sort_lists(
lists_cv{*results}, cudf::order::ASCENDING, cudf::null_order::BEFORE);
}
} // namespace
struct SetDifferenceTest : public cudf::test::BaseFixture {};
template <typename T>
struct SetDifferenceTypedTest : public cudf::test::BaseFixture {};
using TestTypes =
cudf::test::Concat<cudf::test::IntegralTypesNotBool, cudf::test::FloatingPointTypes>;
TYPED_TEST_SUITE(SetDifferenceTypedTest, TestTypes);
TEST_F(SetDifferenceTest, TrivialTest)
{
auto const lhs =
floats_lists{{floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 0.0}, null_at(6)},
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)},
{} /*NULL*/,
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)}},
null_at(2)};
auto const rhs =
floats_lists{{floats_lists{{1.0, 0.5, null, 0.0, 0.0, null, NaN}, nulls_at({2, 5})},
floats_lists{{2.0, 1.0, null, 0.0, 0.0, null}, nulls_at({2, 5})},
floats_lists{{2.0, 1.0, null, 0.0, 0.0, null}, nulls_at({2, 5})},
{} /*NULL*/},
null_at(3)};
auto const expected = floats_lists{
{floats_lists{5.0}, floats_lists{5.0, NaN}, floats_lists{} /*NULL*/, floats_lists{} /*NULL*/},
nulls_at({2, 3})};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
TEST_F(SetDifferenceTest, TrivialIdentityTest)
{
auto const input =
floats_lists{{floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 0.0}, null_at(6)},
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)},
{} /*NULL*/,
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)}},
null_at(2)};
auto const expected =
floats_lists{{floats_lists{}, floats_lists{}, {} /*NULL*/, floats_lists{}}, null_at(2)};
auto const results_sorted = set_difference_sorted(input, input);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
TEST_F(SetDifferenceTest, FloatingPointTestsWithSignedZero)
{
// -0.0 and 0.0 should be considered equal.
auto const lhs = floats_lists{{0.0, 0.0, 0.0, 0.0, 0.0}, {-0.0, 1.0}, {0.0}};
auto const rhs = floats_lists{{-0.0, -0.0, -0.0, -0.0, -0.0}, {0.0, 2.0}, {1.0}};
auto const expected = floats_lists{floats_lists{}, floats_lists{1.0}, floats_lists{0.0}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(SetDifferenceTest, FloatingPointTestsWithInf)
{
auto const lhs = floats_lists{{Inf, Inf, Inf}, {Inf, 0.0, neg_Inf}};
auto const rhs = floats_lists{{neg_Inf, neg_Inf}, {0.0}};
auto const expected = floats_lists{floats_lists{Inf}, floats_lists{neg_Inf, Inf}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(SetDifferenceTest, FloatingPointTestsWithNaNs)
{
auto const lhs =
floats_lists{{0, -1, 1, NaN}, {2, 0, neg_NaN}, {1, -2, 2, 0, 1, 2}, {NaN, NaN, NaN, NaN, NaN}};
auto const rhs =
floats_lists{{2, 3, 4, neg_NaN}, {2, 0}, {neg_NaN, 1, -2, 2, 0, 1, 2}, {neg_NaN, neg_NaN}};
// NaNs are equal.
{
auto const expected = floats_lists{{-1, 0, 1}, {neg_NaN}, {}, {}};
auto const results_sorted = set_difference_sorted(lhs, rhs, NULL_EQUAL, NAN_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// NaNs are unequal.
{
auto const expected = floats_lists{{-1, 0, 1, NaN}, {neg_NaN}, {}, {NaN, NaN, NaN, NaN, NaN}};
auto const results_sorted = set_difference_sorted(lhs, rhs, NULL_EQUAL, NAN_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetDifferenceTest, StringTestsNonNull)
{
// Trivial cases - empty input.
{
auto const lhs = strings_lists{};
auto const rhs = strings_lists{};
auto const expected = strings_lists{};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Trivial cases - empty input.
{
auto const lhs = strings_lists{strings_lists{}};
auto const rhs = strings_lists{strings_lists{}};
auto const expected = strings_lists{strings_lists{}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// No overlap.
{
auto const lhs = strings_lists{"this", "is", "a", "string"};
auto const rhs = strings_lists{"aha", "bear", "blow", "heat"};
auto const expected = strings_lists{"a", "is", "string", "this"};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// One list column.
{
auto const lhs = strings_lists{"this", "is", "a", "string"};
auto const rhs = strings_lists{"a", "delicious", "banana"};
auto const expected = strings_lists{"is", "string", "this"};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column.
{
auto const lhs = strings_lists{strings_lists{"one", "two", "three"},
strings_lists{"four", "five", "six"},
strings_lists{"1", "2", "3"}};
auto const rhs = strings_lists{strings_lists{"one", "banana"},
strings_lists{"apple", "kiwi", "cherry"},
strings_lists{"two", "and", "1"}};
auto const expected = strings_lists{
strings_lists{"three", "two"}, strings_lists{"five", "four", "six"}, strings_lists{"2", "3"}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetDifferenceTest, StringTestsWithNullsEqual)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const lhs = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const rhs =
strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})};
auto const expected = strings_lists{"a", "is", "string", "this"};
auto const results_sorted = set_difference_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
// Multiple lists column with null lists and null entries.
{
auto const lhs = strings_lists{
strings_lists{{"this", null, "is", null, "a", null, null, "string"}, nulls_at({1, 3, 5, 6})},
strings_lists{},
strings_lists{"this", "is", "a", "string"}};
auto const rhs = strings_lists{
{strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})},
strings_lists{}, /* NULL */
strings_lists{"aha", "this", "is another", "string???"}},
null_at(1)};
auto const expected = [] {
auto str_lists = strings_lists{{strings_lists{"a", "is", "string", "this"},
strings_lists{} /*NULL*/,
strings_lists{"a", "is", "string"}},
null_at(1)}
.release();
auto& child = str_lists->child(cudf::lists_column_view::child_column_index);
child.set_null_mask(cudf::create_null_mask(child.size(), cudf::mask_state::ALL_VALID), 0);
return str_lists;
}();
auto const results_sorted = set_difference_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results_sorted);
}
}
TEST_F(SetDifferenceTest, StringTestsWithNullsUnequal)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const lhs = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const rhs =
strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})};
auto const expected =
strings_lists{{null, null, null, "a", "is", "string", "this"}, nulls_at({0, 1, 2})};
auto const results_sorted = set_difference_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column with null lists and null entries.
{
auto const lhs = strings_lists{
strings_lists{{"this", null, "is", null, "a", null, null, "string"}, nulls_at({1, 3, 5, 6})},
strings_lists{},
strings_lists{"this", "is", "a", "string"}};
auto const rhs = strings_lists{
{strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})},
strings_lists{}, /* NULL */
strings_lists{"aha", "this", "is another", "string???"}},
null_at(1)};
auto const expected = strings_lists{
{strings_lists{{null, null, null, null, "a", "is", "string", "this"}, nulls_at({0, 1, 2, 3})},
strings_lists{} /*NULL*/,
strings_lists{"a", "is", "string"}},
null_at(1)};
auto const results_sorted = set_difference_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(SetDifferenceTypedTest, TrivialInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
// Empty input.
{
auto const lhs = lists_col{};
auto const rhs = lists_col{};
auto const expected = lists_col{};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// All input lists are empty.
{
auto const lhs = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const rhs = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const expected = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple empty lists.
{
auto const lhs = lists_col{{}, {1, 2}, {}, {5, 4, 3, 2, 1, 0}, {}, {6}, {}};
auto const rhs = lists_col{{}, {}, {0}, {0, 1, 2, 3, 4, 5}, {}, {6, 7}, {}};
auto const expected = lists_col{{}, {1, 2}, {}, {}, {}, {}, {}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(SetDifferenceTypedTest, SlicedNonNullInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto const lhs_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {3, 2, 1, 4, 1}, {5}, {10, 8, 9}, {6, 7}};
auto const rhs_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {5, 6, 7, 8, 7, 5}, {}, {1, 2, 3}, {6, 7}};
{
auto const expected = lists_col{{}, {1, 2, 3, 4}, {5}, {8, 9, 10}, {}};
auto const results_sorted = set_difference_sorted(lhs_original, rhs_original);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {1, 5})[0];
auto const rhs = cudf::slice(rhs_original, {1, 5})[0];
auto const expected = lists_col{{1, 2, 3, 4}, {5}, {8, 9, 10}, {}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {1, 3})[0];
auto const rhs = cudf::slice(rhs_original, {1, 3})[0];
auto const expected = lists_col{{1, 2, 3, 4}, {5}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {0, 3})[0];
auto const rhs = cudf::slice(rhs_original, {0, 3})[0];
auto const expected = lists_col{{}, {1, 2, 3, 4}, {5}};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(SetDifferenceTypedTest, InputHaveNullsTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto constexpr null = TypeParam{0};
// Nullable lists.
{
auto const lhs = lists_col{{{3, 2, 1, 4, 1}, {5}, {} /*NULL*/, {} /*NULL*/, {10, 8, 9}, {6, 7}},
nulls_at({2, 3})};
auto const rhs =
lists_col{{{1, 2}, {} /*NULL*/, {3}, {} /*NULL*/, {10, 11, 12}, {1, 2}}, nulls_at({1, 3})};
auto const expected = lists_col{{{3, 4}, {} /*NULL*/, {} /*NULL*/, {} /*NULL*/, {8, 9}, {6, 7}},
nulls_at({1, 2, 3})};
auto const results_sorted = set_difference_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Nullable child and nulls are equal.
{
auto const lhs = lists_col{lists_col{{null, 1, null, 3}, nulls_at({0, 2})},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, null, 9}, nulls_at({0, 2})}};
auto const rhs = lists_col{lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{5, null}, null_at(1)},
lists_col{7, 8, 9}};
auto const expected = lists_col{lists_col{1, 3}, lists_col{}, lists_col{{null}, null_at(0)}};
auto const results_sorted = set_difference_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Nullable child and nulls are unequal.
{
auto const lhs = lists_col{lists_col{{null, 1, null, 3}, nulls_at({0, 2})},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, null, 9}, nulls_at({0, 2})}};
auto const rhs = lists_col{lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{5, null}, null_at(1)},
lists_col{7, 8, 9}};
auto const expected = lists_col{lists_col{{null, null, 1, 3}, nulls_at({0, 1})},
lists_col{{null}, null_at(0)},
lists_col{{null, null}, nulls_at({0, 1})}};
auto const results_sorted = set_difference_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetDifferenceTest, InputListsOfNestedStructsHaveNull)
{
auto const get_structs_lhs = [] {
auto grandchild1 = int32s_col{{
1, XXX, null, XXX, XXX, 1, 1, 1, // list1
1, 1, 1, 1, 2, 1, null, 2, // list2
null, null, 2, 2, 3, 2, 3, 3 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"YYY", /*NULL*/
"Apple",
"XXX", /*NULL*/
"YYY", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
// Only grandchild1 of rhs is different from lhs'. The rest is exactly the same.
auto const get_structs_rhs = [] {
auto grandchild1 = int32s_col{{
2, XXX, null, XXX, XXX, 2, 2, 2, // list1
3, 3, 3, 3, 3, 3, null, 3, // list2
null, null, 4, 4, 4, 4, 4, 4 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"YYY", /*NULL*/
"Apple",
"XXX", /*NULL*/
"YYY", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
// Nulls are equal.
{
auto const get_structs_expected = [] {
auto grandchild1 = int32s_col{
1,
1,
1, // end list1
1,
1,
1,
1,
2, // end list2
2,
2,
2,
3,
3,
3 // end list3
};
auto grandchild2 = strings_col{{
"Banana",
"Cherry",
"Kiwi", // end list1
"Bear",
"Cat",
"Dog",
"Duck",
"Panda", // end list2
"ÁBC",
"ÁÁÁ",
"ÍÍÍÍÍ",
"" /*NULL*/,
"XYZ",
"ÁBC" // end list3
},
null_at(11)};
auto child1 = structs_col{grandchild1, grandchild2};
return structs_col{{child1}};
};
auto const lhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_lhs().release(), 0, {});
auto const rhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_rhs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 3, 8, 14}.release(), get_structs_expected().release(), 0, {});
auto const results_sorted = set_difference_sorted(*lhs, *rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results_sorted);
}
// Nulls are unequal.
{
auto const get_structs_expected = [] {
auto grandchild1 = int32s_col{{
null, null, null, null, 1, 1, 1, // end list1
null, 1, 1, 1, 1, 2, // end list2
null, null, 2, 2, 2, 3, 3, 3 // end list3
},
nulls_at({0, 1, 2, 3, 7, 13, 14})};
auto grandchild2 = strings_col{{
"" /*NULL*/, "" /*NULL*/, "" /*NULL*/, "Apple", "Banana",
"Cherry", "Kiwi", // end list1
"" /*NULL*/, "Bear", "Cat", "Dog", "Duck",
"Panda", // end list2
"ÁÁÁ", "ÉÉÉÉÉ", "ÁBC", "ÁÁÁ", "ÍÍÍÍÍ",
"" /*NULL*/, "XYZ",
"ÁBC" // end list3
},
nulls_at({0, 1, 2, 7, 18})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({0, 1, 2})};
return structs_col{{child1}};
};
auto const lhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_lhs().release(), 0, {});
auto const rhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_rhs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 7, 13, 21}.release(), get_structs_expected().release(), 0, {});
auto const results_sorted = set_difference_sorted(*lhs, *rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results_sorted);
}
}
TEST_F(SetDifferenceTest, InputListsOfStructsOfLists)
{
auto const lhs = [] {
auto const get_structs = [] {
auto child1 = int32s_col{// begin list1
0,
1,
2, // end list1
// begin list2
3, // end list2
// begin list3
4,
5,
6};
auto child2 = floats_lists{// begin list1
floats_lists{0, 1},
floats_lists{0, 2},
floats_lists{1, 1}, // end list1
// begin list2
floats_lists{3, 4, 5}, // end list2
// begin list3
floats_lists{6, 7},
floats_lists{6, 8},
floats_lists{6, 7, 8}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
3, int32s_col{0, 3, 4, 7}.release(), get_structs().release(), 0, {});
}();
auto const rhs = [] {
auto const get_structs = [] {
auto child1 = int32s_col{// begin list1
0,
1,
2, // end list1
// begin list2
3, // end list2
// begin list3
4,
5,
6};
auto child2 = floats_lists{// begin list1
floats_lists{1, 1},
floats_lists{0, 2},
floats_lists{1, 2}, // end list1
// begin list2
floats_lists{3, 4, 5}, // end list2
// begin list3
floats_lists{6, 7, 8, 9},
floats_lists{6, 8},
floats_lists{3, 4, 5}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
3, int32s_col{0, 3, 4, 7}.release(), get_structs().release(), 0, {});
}();
auto const expected = [] {
auto const get_structs = [] {
auto child1 = int32s_col{0, 2, 4, 6};
auto child2 = floats_lists{
floats_lists{0, 1}, floats_lists{1, 1}, floats_lists{6, 7}, floats_lists{6, 7, 8}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
3, int32s_col{0, 2, 2, 4}.release(), get_structs().release(), 0, {});
}();
auto const results = cudf::lists::difference_distinct(lists_cv{*lhs}, lists_cv{*rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests/lists
|
rapidsai_public_repos/cudf/cpp/tests/lists/set_operations/intersect_distinct_tests.cpp
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/lists/set_operations.hpp>
#include <cudf/lists/sorting.hpp>
#include <cudf/lists/stream_compaction.hpp>
#include <cudf/null_mask.hpp>
#include <limits>
#include <string>
using float_type = double;
using namespace cudf::test::iterators;
auto constexpr null{0}; // null at current level
auto constexpr XXX{0}; // null pushed down from parent level
auto constexpr neg_NaN = -std::numeric_limits<float_type>::quiet_NaN();
auto constexpr neg_Inf = -std::numeric_limits<float_type>::infinity();
auto constexpr NaN = std::numeric_limits<float_type>::quiet_NaN();
auto constexpr Inf = std::numeric_limits<float_type>::infinity();
auto constexpr NULL_EQUAL = cudf::null_equality::EQUAL;
auto constexpr NULL_UNEQUAL = cudf::null_equality::UNEQUAL;
auto constexpr NAN_EQUAL = cudf::nan_equality::ALL_EQUAL;
auto constexpr NAN_UNEQUAL = cudf::nan_equality::UNEQUAL;
using bools_col = cudf::test::fixed_width_column_wrapper<bool>;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_lists = cudf::test::lists_column_wrapper<float_type>;
using strings_lists = cudf::test::lists_column_wrapper<cudf::string_view>;
using strings_col = cudf::test::strings_column_wrapper;
using structs_col = cudf::test::structs_column_wrapper;
using lists_cv = cudf::lists_column_view;
namespace {
auto set_intersect_sorted(cudf::column_view const& lhs,
cudf::column_view const& rhs,
cudf::null_equality nulls_equal = NULL_EQUAL,
cudf::nan_equality nans_equal = NAN_EQUAL)
{
auto const results =
cudf::lists::intersect_distinct(lists_cv{lhs}, lists_cv{rhs}, nulls_equal, nans_equal);
return cudf::lists::sort_lists(
lists_cv{*results}, cudf::order::ASCENDING, cudf::null_order::BEFORE);
}
} // namespace
struct SetIntersectTest : public cudf::test::BaseFixture {};
template <typename T>
struct SetIntersectTypedTest : public cudf::test::BaseFixture {};
using TestTypes =
cudf::test::Concat<cudf::test::IntegralTypesNotBool, cudf::test::FloatingPointTypes>;
TYPED_TEST_SUITE(SetIntersectTypedTest, TestTypes);
TEST_F(SetIntersectTest, TrivialTest)
{
auto const lhs =
floats_lists{{floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 0.0}, null_at(6)},
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)},
{} /*NULL*/,
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)}},
null_at(2)};
auto const rhs =
floats_lists{{floats_lists{{1.0, 0.5, null, 0.0, 0.0, null, NaN}, nulls_at({2, 5})},
floats_lists{{2.0, 1.0, null, 0.0, 0.0, null}, nulls_at({2, 5})},
floats_lists{{2.0, 1.0, null, 0.0, 0.0, null}, nulls_at({2, 5})},
{} /*NULL*/},
null_at(3)};
auto const expected = floats_lists{{floats_lists{{null, 0.0, NaN}, null_at(0)},
floats_lists{{null, 0.0, 1.0}, null_at(0)},
floats_lists{} /*NULL*/,
floats_lists{} /*NULL*/},
nulls_at({2, 3})};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
TEST_F(SetIntersectTest, TrivialIdentityTest)
{
auto const input =
floats_lists{{floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 0.0}, null_at(6)},
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)},
{} /*NULL*/,
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)}},
null_at(2)};
// `intersect_distinct(input, input) <==> lists::distinct(input)`.
auto const input_distinct = cudf::lists::distinct(lists_cv{input});
auto const input_distinct_sorted = cudf::lists::sort_lists(
lists_cv{*input_distinct}, cudf::order::ASCENDING, cudf::null_order::BEFORE);
auto const results_sorted = set_intersect_sorted(input, input);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*input_distinct_sorted, *results_sorted);
}
TEST_F(SetIntersectTest, FloatingPointTestsWithSignedZero)
{
// -0.0 and 0.0 should be considered equal.
auto const lhs = floats_lists{{0.0, 0.0, 0.0, 0.0, 0.0}, {-0.0, 1.0}, {0.0}};
auto const rhs = floats_lists{{-0.0, -0.0, -0.0, -0.0, -0.0}, {0.0, 2.0}, {1.0}};
auto const expected = floats_lists{floats_lists{0}, floats_lists{0}, floats_lists{}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(SetIntersectTest, FloatingPointTestsWithInf)
{
auto const lhs = floats_lists{{Inf, Inf, Inf}, {Inf, 0.0, neg_Inf}};
auto const rhs = floats_lists{{neg_Inf, neg_Inf}, {0.0, Inf}};
auto const expected = floats_lists{floats_lists{}, floats_lists{0.0, Inf}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(SetIntersectTest, FloatingPointTestsWithNaNs)
{
auto const lhs =
floats_lists{{0, -1, 1, NaN}, {2, 0, neg_NaN}, {1, -2, 2, 0, 1, 2}, {NaN, NaN, NaN, NaN, NaN}};
auto const rhs =
floats_lists{{2, 3, 4, neg_NaN}, {2, 0}, {neg_NaN, 1, -2, 2, 0, 1, 2}, {neg_NaN, neg_NaN}};
// NaNs are equal.
{
auto const expected = floats_lists{{NaN}, {0, 2}, {-2, 0, 1, 2}, {NaN}};
auto const results_sorted = set_intersect_sorted(lhs, rhs, NULL_EQUAL, NAN_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// NaNs are unequal.
{
auto const expected = floats_lists{{}, {0, 2}, {-2, 0, 1, 2}, {}};
auto const results_sorted = set_intersect_sorted(lhs, rhs, NULL_EQUAL, NAN_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetIntersectTest, StringTestsNonNull)
{
// Trivial cases - empty input.
{
auto const lhs = strings_lists{};
auto const rhs = strings_lists{};
auto const expected = strings_lists{};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Trivial cases - empty input.
{
auto const lhs = strings_lists{strings_lists{}};
auto const rhs = strings_lists{strings_lists{}};
auto const expected = strings_lists{strings_lists{}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// No overlap.
{
auto const lhs = strings_lists{"this", "is", "a", "string"};
auto const rhs = strings_lists{"aha", "bear", "blow", "heat"};
auto const expected = strings_lists{strings_lists{}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// One list column.
{
auto const lhs = strings_lists{"this", "is", "a", "string"};
auto const rhs = strings_lists{"a", "delicious", "banana"};
auto const expected = strings_lists{"a"};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column.
{
auto const lhs = strings_lists{strings_lists{"one", "two", "three"},
strings_lists{"four", "five", "six"},
strings_lists{"1", "2", "3"}};
auto const rhs = strings_lists{strings_lists{"one", "banana"},
strings_lists{"apple", "kiwi", "cherry"},
strings_lists{"two", "and", "1"}};
auto const expected = strings_lists{strings_lists{"one"}, strings_lists{}, strings_lists{"1"}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetIntersectTest, StringTestsWithNullsEqual)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const lhs = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const rhs =
strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})};
auto const expected = strings_lists{{null}, null_at(0)};
auto const results_sorted = set_intersect_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
// Multiple lists column with null lists and null entries.
{
auto const lhs = strings_lists{
strings_lists{{"this", null, "is", null, "a", null, null, "string"}, nulls_at({1, 3, 5, 6})},
strings_lists{},
strings_lists{"this", "is", "a", "string"}};
auto const rhs = strings_lists{
{strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})},
strings_lists{}, /* NULL */
strings_lists{"aha", "this", "is another", "string???"}},
null_at(1)};
auto const expected = strings_lists{
{strings_lists{{null}, null_at(0)}, strings_lists{} /*NULL*/, strings_lists{"this"}},
null_at(1)};
auto const results_sorted = set_intersect_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetIntersectTest, StringTestsWithNullsUnequal)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const lhs = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const rhs =
strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})};
auto const expected = strings_lists{strings_lists{}};
auto const results_sorted = set_intersect_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column with null lists and null entries.
{
auto const lhs = strings_lists{
strings_lists{{"this", null, "is", null, "a", null, null, "string"}, nulls_at({1, 3, 5, 6})},
strings_lists{},
strings_lists{"this", "is", "a", "string"}};
auto const rhs = strings_lists{
{strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})},
strings_lists{}, /* NULL */
strings_lists{"aha", "this", "is another", "string???"}},
null_at(1)};
auto const expected = [] {
auto str_lists =
strings_lists{{strings_lists{}, strings_lists{} /*NULL*/, strings_lists{"this"}},
null_at(1)}
.release();
auto& child = str_lists->child(cudf::lists_column_view::child_column_index);
child.set_null_mask(cudf::create_null_mask(child.size(), cudf::mask_state::ALL_VALID), 0);
return str_lists;
}();
auto const results_sorted = set_intersect_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results_sorted);
}
}
TYPED_TEST(SetIntersectTypedTest, TrivialInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
// Empty input.
{
auto const lhs = lists_col{};
auto const rhs = lists_col{};
auto const expected = lists_col{};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// All input lists are empty.
{
auto const lhs = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const rhs = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const expected = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple empty lists.
{
auto const lhs = lists_col{{}, {1, 2}, {}, {5, 4, 3, 2, 1, 0}, {}, {6}, {}};
auto const rhs = lists_col{{}, {}, {0}, {0, 1, 2, 3, 4, 5}, {}, {6, 7}, {}};
auto const expected = lists_col{{}, {}, {}, {0, 1, 2, 3, 4, 5}, {}, {6}, {}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(SetIntersectTypedTest, SlicedNonNullInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto const lhs_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {3, 2, 1, 4, 1}, {5}, {10, 8, 9}, {6, 7}};
auto const rhs_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {5, 6, 7, 8, 7, 5}, {}, {1, 2, 3}, {6, 7}};
{
auto const expected = lists_col{{1, 2, 3}, {}, {}, {}, {6, 7}};
auto const results_sorted = set_intersect_sorted(lhs_original, rhs_original);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {1, 5})[0];
auto const rhs = cudf::slice(rhs_original, {1, 5})[0];
auto const expected = lists_col{{}, {}, {}, {6, 7}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {1, 3})[0];
auto const rhs = cudf::slice(rhs_original, {1, 3})[0];
auto const expected = lists_col{lists_col{}, lists_col{}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {0, 3})[0];
auto const rhs = cudf::slice(rhs_original, {0, 3})[0];
auto const expected = lists_col{{1, 2, 3}, {}, {}};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(SetIntersectTypedTest, InputHaveNullsTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto constexpr null = TypeParam{0};
// Nullable lists.
{
auto const lhs = lists_col{{{3, 2, 1, 4, 1}, {5}, {} /*NULL*/, {} /*NULL*/, {10, 8, 9}, {6, 7}},
nulls_at({2, 3})};
auto const rhs =
lists_col{{{1, 2}, {} /*NULL*/, {3}, {} /*NULL*/, {10, 11, 12}, {1, 2}}, nulls_at({1, 3})};
auto const expected =
lists_col{{{1, 2}, {} /*NULL*/, {} /*NULL*/, {} /*NULL*/, {10}, {}}, nulls_at({1, 2, 3})};
auto const results_sorted = set_intersect_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Nullable child and nulls are equal.
{
auto const lhs = lists_col{lists_col{{null, 1, null, 3}, nulls_at({0, 2})},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, null, 9}, nulls_at({0, 2})}};
auto const rhs = lists_col{lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{5, null}, null_at(1)},
lists_col{7, 8, 9}};
auto const expected =
lists_col{lists_col{{null}, null_at(0)}, lists_col{{null, 5}, null_at(0)}, lists_col{7, 9}};
auto const results_sorted = set_intersect_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Nullable child and nulls are unequal.
{
auto const lhs = lists_col{lists_col{{null, 1, null, 3}, nulls_at({0, 2})},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, null, 9}, nulls_at({0, 2})}};
auto const rhs = lists_col{lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{5, null}, null_at(1)},
lists_col{7, 8, 9}};
auto const expected = lists_col{lists_col{}, lists_col{5}, lists_col{7, 9}};
auto const results_sorted = set_intersect_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
}
TEST_F(SetIntersectTest, InputListsOfNestedStructsHaveNull)
{
auto const get_structs_lhs = [] {
auto grandchild1 = int32s_col{{
1, XXX, null, XXX, XXX, 1, 1, 1, // list1
1, 1, 1, 1, 2, 1, null, 2, // list2
null, null, 2, 2, 3, 2, 3, 3 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"YYY", /*NULL*/
"Apple",
"XXX", /*NULL*/
"YYY", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
// Only grandchild1 of rhs is different from lhs'. The rest is exactly the same.
auto const get_structs_rhs = [] {
auto grandchild1 = int32s_col{{
2, XXX, null, XXX, XXX, 2, 2, 2, // list1
3, 3, 3, 3, 3, 3, null, 3, // list2
null, null, 4, 4, 4, 4, 4, 4 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"YYY", /*NULL*/
"Apple",
"XXX", /*NULL*/
"YYY", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
// Nulls are equal.
{
auto const get_structs_expected = [] {
auto grandchild1 = int32s_col{{
null,
null, // end list1
null, // end list2
null,
null // end list3
},
all_nulls()};
auto grandchild2 = strings_col{{
"" /*NULL*/,
"Apple", // end list1
"" /*NULL*/, // end list2
"ÁÁÁ",
"ÉÉÉÉÉ" // end list3
},
nulls_at({0, 2})};
auto child1 = structs_col{{grandchild1, grandchild2}, null_at(0)};
return structs_col{{child1}};
};
auto const lhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_lhs().release(), 0, {});
auto const rhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_rhs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 2, 3, 5}.release(), get_structs_expected().release(), 0, {});
auto const results_sorted = set_intersect_sorted(*lhs, *rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results_sorted);
}
// Nulls are unequal.
{
auto const get_structs_expected = [] {
auto grandchild1 = int32s_col{};
auto grandchild2 = strings_col{};
auto child1 = structs_col{{grandchild1, grandchild2}};
return structs_col{{child1}};
};
auto const lhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_lhs().release(), 0, {});
auto const rhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_rhs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 0, 0, 0}.release(), get_structs_expected().release(), 0, {});
auto const results_sorted = set_intersect_sorted(*lhs, *rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results_sorted);
}
}
TEST_F(SetIntersectTest, InputListsOfStructsOfLists)
{
auto const lhs = [] {
auto const get_structs = [] {
auto child1 = int32s_col{// begin list1
0,
1,
2, // end list1
// begin list2
3, // end list2
// begin list3
4,
5,
6};
auto child2 = floats_lists{// begin list1
floats_lists{0, 1},
floats_lists{0, 2},
floats_lists{1, 1}, // end list1
// begin list2
floats_lists{3, 4, 5}, // end list2
// begin list3
floats_lists{6, 7},
floats_lists{6, 8},
floats_lists{6, 7, 8}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
3, int32s_col{0, 3, 4, 7}.release(), get_structs().release(), 0, {});
}();
auto const rhs = [] {
auto const get_structs = [] {
auto child1 = int32s_col{// begin list1
0,
1,
2, // end list1
// begin list2
3, // end list2
// begin list3
4,
5,
6};
auto child2 = floats_lists{// begin list1
floats_lists{1, 1},
floats_lists{0, 2},
floats_lists{1, 2}, // end list1
// begin list2
floats_lists{3, 4, 5}, // end list2
// begin list3
floats_lists{6, 7, 8, 9},
floats_lists{6, 8},
floats_lists{3, 4, 5}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
3, int32s_col{0, 3, 4, 7}.release(), get_structs().release(), 0, {});
}();
auto const expected = [] {
auto const get_structs = [] {
auto child1 = int32s_col{1, 3, 5};
auto child2 = floats_lists{floats_lists{0, 2}, floats_lists{3, 4, 5}, floats_lists{6, 8}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
3, int32s_col{0, 1, 2, 3}.release(), get_structs().release(), 0, {});
}();
auto const results = cudf::lists::intersect_distinct(lists_cv{*lhs}, lists_cv{*rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests/lists
|
rapidsai_public_repos/cudf/cpp/tests/lists/set_operations/union_distinct_tests.cpp
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/lists/set_operations.hpp>
#include <cudf/lists/sorting.hpp>
#include <cudf/lists/stream_compaction.hpp>
#include <limits>
#include <string>
using float_type = double;
using namespace cudf::test::iterators;
auto constexpr null{0}; // null at current level
auto constexpr XXX{0}; // null pushed down from parent level
auto constexpr neg_NaN = -std::numeric_limits<float_type>::quiet_NaN();
auto constexpr neg_Inf = -std::numeric_limits<float_type>::infinity();
auto constexpr NaN = std::numeric_limits<float_type>::quiet_NaN();
auto constexpr Inf = std::numeric_limits<float_type>::infinity();
auto constexpr NULL_EQUAL = cudf::null_equality::EQUAL;
auto constexpr NULL_UNEQUAL = cudf::null_equality::UNEQUAL;
auto constexpr NAN_EQUAL = cudf::nan_equality::ALL_EQUAL;
auto constexpr NAN_UNEQUAL = cudf::nan_equality::UNEQUAL;
using bools_col = cudf::test::fixed_width_column_wrapper<bool>;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_lists = cudf::test::lists_column_wrapper<float_type>;
using strings_lists = cudf::test::lists_column_wrapper<cudf::string_view>;
using strings_col = cudf::test::strings_column_wrapper;
using structs_col = cudf::test::structs_column_wrapper;
using lists_cv = cudf::lists_column_view;
namespace {
auto set_union_sorted(cudf::column_view const& lhs,
cudf::column_view const& rhs,
cudf::null_equality nulls_equal = NULL_EQUAL,
cudf::nan_equality nans_equal = NAN_EQUAL)
{
auto const results =
cudf::lists::union_distinct(lists_cv{lhs}, lists_cv{rhs}, nulls_equal, nans_equal);
return cudf::lists::sort_lists(
lists_cv{*results}, cudf::order::ASCENDING, cudf::null_order::BEFORE);
}
} // namespace
struct SetUnionTest : public cudf::test::BaseFixture {};
template <typename T>
struct SetUnionTypedTest : public cudf::test::BaseFixture {};
using TestTypes =
cudf::test::Concat<cudf::test::IntegralTypesNotBool, cudf::test::FloatingPointTypes>;
TYPED_TEST_SUITE(SetUnionTypedTest, TestTypes);
TEST_F(SetUnionTest, TrivialTest)
{
auto const lhs =
floats_lists{{floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 0.0}, null_at(6)},
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)},
{} /*NULL*/,
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)}},
null_at(2)};
auto const rhs =
floats_lists{{floats_lists{{1.0, 0.5, null, 0.0, 0.0, null, NaN}, nulls_at({2, 5})},
floats_lists{{2.0, 1.0, null, 0.0, 0.0, null}, nulls_at({2, 5})},
floats_lists{{2.0, 1.0, null, 0.0, 0.0, null}, nulls_at({2, 5})},
{} /*NULL*/},
null_at(3)};
auto const expected = floats_lists{{floats_lists{{null, 0.0, 0.5, 1.0, 5.0, NaN}, null_at(0)},
floats_lists{{null, 0.0, 1.0, 2.0, 5.0, NaN}, null_at(0)},
floats_lists{} /*NULL*/,
floats_lists{} /*NULL*/},
nulls_at({2, 3})};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
TEST_F(SetUnionTest, TrivialIdentityTest)
{
auto const input =
floats_lists{{floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 0.0}, null_at(6)},
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)},
{} /*NULL*/,
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)}},
null_at(2)};
// `union_distinct(input, input) <==> lists::distinct(input)`.
auto const input_distinct = cudf::lists::distinct(lists_cv{input});
auto const input_distinct_sorted = cudf::lists::sort_lists(
lists_cv{*input_distinct}, cudf::order::ASCENDING, cudf::null_order::BEFORE);
auto const results_sorted = set_union_sorted(input, input);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*input_distinct_sorted, *results_sorted);
}
TEST_F(SetUnionTest, FloatingPointTestsWithSignedZero)
{
// -0.0 and 0.0 should be considered equal.
auto const lhs = floats_lists{{0.0, 0.0, 0.0, 0.0, 0.0}, {-0.0, 1.0}, {0.0}};
auto const rhs = floats_lists{{-0.0, -0.0, -0.0, -0.0, -0.0}, {0.0, 2.0}, {1.0}};
auto const expected = floats_lists{floats_lists{0}, floats_lists{0, 1, 2}, floats_lists{0, 1}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(SetUnionTest, FloatingPointTestsWithInf)
{
auto const lhs = floats_lists{{Inf, Inf, Inf}, {Inf, 0.0, neg_Inf}};
auto const rhs = floats_lists{{neg_Inf, neg_Inf}, {0.0, Inf}};
auto const expected = floats_lists{floats_lists{neg_Inf, Inf}, floats_lists{neg_Inf, 0.0, Inf}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(SetUnionTest, FloatingPointTestsWithNaNs)
{
auto const lhs =
floats_lists{{0, -1, 1, NaN}, {2, 0, neg_NaN}, {1, -2, 2, 0, 1, 2}, {NaN, NaN, NaN, NaN, NaN}};
auto const rhs =
floats_lists{{2, 3, 4, neg_NaN}, {2, 0}, {neg_NaN, 1, -2, 2, 0, 1, 2}, {neg_NaN, neg_NaN}};
// NaNs are equal.
{
auto const expected =
floats_lists{{-1, 0, 1, 2, 3, 4, NaN}, {0, 2, neg_NaN}, {-2, 0, 1, 2, neg_NaN}, {NaN}};
auto const results_sorted = set_union_sorted(lhs, rhs, NULL_EQUAL, NAN_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// NaNs are unequal.
{
auto const expected = floats_lists{{-1, 0, 1, 2, 3, 4, NaN, neg_NaN},
{0, 2, neg_NaN},
{-2, 0, 1, 2, neg_NaN},
{NaN, NaN, NaN, NaN, NaN, neg_NaN, neg_NaN}};
auto const results_sorted = set_union_sorted(lhs, rhs, NULL_EQUAL, NAN_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetUnionTest, StringTestsNonNull)
{
// Trivial cases - empty input.
{
auto const lhs = strings_lists{};
auto const rhs = strings_lists{};
auto const expected = strings_lists{};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Trivial cases - empty input.
{
auto const lhs = strings_lists{strings_lists{}};
auto const rhs = strings_lists{strings_lists{}};
auto const expected = strings_lists{strings_lists{}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// No overlap.
{
auto const lhs = strings_lists{"this", "is", "a", "string"};
auto const rhs = strings_lists{"aha", "bear", "blow", "heat"};
auto const expected =
strings_lists{strings_lists{"a", "aha", "bear", "blow", "heat", "is", "string", "this"}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// One list column.
{
auto const lhs = strings_lists{"this", "is", "a", "string"};
auto const rhs = strings_lists{"a", "delicious", "banana"};
auto const expected = strings_lists{"a", "banana", "delicious", "is", "string", "this"};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column.
{
auto const lhs = strings_lists{strings_lists{"one", "two", "three"},
strings_lists{"four", "five", "six"},
strings_lists{"1", "2", "3"}};
auto const rhs = strings_lists{strings_lists{"one", "banana"},
strings_lists{"apple", "kiwi", "cherry"},
strings_lists{"two", "and", "1"}};
auto const expected =
strings_lists{strings_lists{"banana", "one", "three", "two"},
strings_lists{"apple", "cherry", "five", "four", "kiwi", "six"},
strings_lists{"1", "2", "3", "and", "two"}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetUnionTest, StringTestsWithNullsEqual)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const lhs = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const rhs =
strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})};
auto const expected =
strings_lists{{null, "1111", "2222", "a", "abc", "aha", "is", "string", "this"}, null_at(0)};
auto const results_sorted = set_union_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
// Multiple lists column with null lists and null entries.
{
auto const lhs = strings_lists{
strings_lists{{"this", null, "is", null, "a", null, null, "string"}, nulls_at({1, 3, 5, 6})},
strings_lists{},
strings_lists{"this", "is", "a", "string"}};
auto const rhs = strings_lists{
{strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})},
strings_lists{}, /* NULL */
strings_lists{"aha", "this", "is another", "string???"}},
null_at(1)};
auto const expected = strings_lists{
{strings_lists{{null, "1111", "2222", "a", "abc", "aha", "is", "string", "this"}, null_at(0)},
strings_lists{} /*NULL*/,
strings_lists{"a", "aha", "is", "is another", "string", "string???", "this"}},
null_at(1)};
auto const results_sorted = set_union_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(SetUnionTest, StringTestsWithNullsUnequal)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const lhs = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const rhs =
strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})};
auto const expected = strings_lists{{null,
null,
null,
null,
null,
null,
"1111",
"2222",
"a",
"abc",
"aha",
"is",
"string",
"this"},
nulls_at({0, 1, 2, 3, 4, 5})};
auto const results_sorted = set_union_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column with null lists and null entries.
{
auto const lhs = strings_lists{
strings_lists{{"this", null, "is", null, "a", null, null, "string"}, nulls_at({1, 3, 5, 6})},
strings_lists{},
strings_lists{"this", "is", "a", "string"}};
auto const rhs = strings_lists{
{strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})},
strings_lists{}, /* NULL */
strings_lists{"aha", "this", "is another", "string???"}},
null_at(1)};
auto const expected =
strings_lists{{strings_lists{{null,
null,
null,
null,
null,
null,
null,
"1111",
"2222",
"a",
"abc",
"aha",
"is",
"string",
"this"},
nulls_at({0, 1, 2, 3, 4, 5, 6})},
strings_lists{} /*NULL*/,
strings_lists{"a", "aha", "is", "is another", "string", "string???", "this"}},
null_at(1)};
auto const results_sorted = set_union_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(SetUnionTypedTest, TrivialInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
// Empty input.
{
auto const lhs = lists_col{};
auto const rhs = lists_col{};
auto const expected = lists_col{};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// All input lists are empty.
{
auto const lhs = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const rhs = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const expected = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple empty lists.
{
auto const lhs = lists_col{{}, {1, 2}, {}, {5, 4, 3, 2, 1, 0}, {}, {6}, {}};
auto const rhs = lists_col{{}, {}, {0}, {0, 1, 2, 3, 4, 5}, {}, {6, 7}, {}};
auto const expected = lists_col{{}, {1, 2}, {0}, {0, 1, 2, 3, 4, 5}, {}, {6, 7}, {}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(SetUnionTypedTest, SlicedNonNullInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto const lhs_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {3, 2, 1, 4, 1}, {5}, {10, 8, 9}, {6, 7}};
auto const rhs_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {5, 6, 7, 8, 7, 5}, {}, {1, 2, 3}, {6, 7}};
{
auto const expected =
lists_col{{1, 2, 3}, {1, 2, 3, 4, 5, 6, 7, 8}, {5}, {1, 2, 3, 8, 9, 10}, {6, 7}};
auto const results_sorted = set_union_sorted(lhs_original, rhs_original);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {1, 5})[0];
auto const rhs = cudf::slice(rhs_original, {1, 5})[0];
auto const expected = lists_col{{1, 2, 3, 4, 5, 6, 7, 8}, {5}, {1, 2, 3, 8, 9, 10}, {6, 7}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {1, 3})[0];
auto const rhs = cudf::slice(rhs_original, {1, 3})[0];
auto const expected = lists_col{{1, 2, 3, 4, 5, 6, 7, 8}, {5}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const lhs = cudf::slice(lhs_original, {0, 3})[0];
auto const rhs = cudf::slice(rhs_original, {0, 3})[0];
auto const expected = lists_col{{1, 2, 3}, {1, 2, 3, 4, 5, 6, 7, 8}, {5}};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(SetUnionTypedTest, InputHaveNullsTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto constexpr null = TypeParam{0};
// Nullable lists.
{
auto const lhs = lists_col{{{3, 2, 1, 4, 1}, {5}, {} /*NULL*/, {} /*NULL*/, {10, 8, 9}, {6, 7}},
nulls_at({2, 3})};
auto const rhs =
lists_col{{{1, 2}, {} /*NULL*/, {3}, {} /*NULL*/, {10, 11, 12}, {1, 2}}, nulls_at({1, 3})};
auto const expected = lists_col{
{{1, 2, 3, 4}, {} /*NULL*/, {} /*NULL*/, {} /*NULL*/, {8, 9, 10, 11, 12}, {1, 2, 6, 7}},
nulls_at({1, 2, 3})};
auto const results_sorted = set_union_sorted(lhs, rhs);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Nullable child and nulls are equal.
{
auto const lhs = lists_col{lists_col{{null, 1, null, 3}, nulls_at({0, 2})},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, null, 9}, nulls_at({0, 2})}};
auto const rhs = lists_col{lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{5, null}, null_at(1)},
lists_col{7, 8, 9}};
auto const expected = lists_col{lists_col{{null, 1, 3, 5}, null_at(0)},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, 8, 9}, null_at(0)}};
auto const results_sorted = set_union_sorted(lhs, rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Nullable child and nulls are unequal.
{
auto const lhs = lists_col{lists_col{{null, 1, null, 3}, nulls_at({0, 2})},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, null, 9}, nulls_at({0, 2})}};
auto const rhs = lists_col{lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{5, null}, null_at(1)},
lists_col{7, 8, 9}};
auto const expected =
lists_col{lists_col{{null, null, null, null, 1, 3, 5}, nulls_at({0, 1, 2, 3})},
lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{null, null, 7, 8, 9}, nulls_at({0, 1})}};
auto const results_sorted = set_union_sorted(lhs, rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, *results_sorted);
}
}
TEST_F(SetUnionTest, InputListsOfNestedStructsHaveNull)
{
auto const get_structs_lhs = [] {
auto grandchild1 = int32s_col{{
1, XXX, null, XXX, XXX, 1, 1, 1, // list1
1, 1, 1, 1, 2, 1, null, 2, // list2
null, null, 2, 2, 3, 2, 3, 3 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"YYY", /*NULL*/
"Apple",
"XXX", /*NULL*/
"YYY", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
// Only grandchild1 of rhs is different from lhs'. The rest is exactly the same.
auto const get_structs_rhs = [] {
auto grandchild1 = int32s_col{{
2, XXX, null, XXX, XXX, 2, 2, 2, // list1
3, 3, 3, 3, 3, 3, null, 3, // list2
null, null, 4, 4, 4, 4, 4, 4 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"YYY", /*NULL*/
"Apple",
"XXX", /*NULL*/
"YYY", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
// Nulls are equal.
{
auto const get_structs_expected = [] {
auto grandchild1 = int32s_col{{
null, null, 1, 1, 1, 2, 2,
2, // end list1
null, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, // end list2
null, null, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 // end list3
},
nulls_at({0, 1, 8, 19, 20})};
auto grandchild2 =
strings_col{{
"" /*NULL*/, "Apple", "Banana", "Cherry", "Kiwi", "Banana", "Cherry",
"Kiwi", // end list1
"" /*NULL*/, "Bear", "Cat", "Dog", "Duck", "Panda", "Bear",
"Cat", "Dog", "Duck", "Panda", // end list2
"ÁÁÁ", "ÉÉÉÉÉ", "ÁBC", "ÁÁÁ", "ÍÍÍÍÍ", "" /*NULL*/, "XYZ",
"ÁBC", "" /*NULL*/, "XYZ", "ÁBC", "ÁÁÁ", "ÍÍÍÍÍ" // end list3
},
nulls_at({0, 8, 24, 27})};
auto child1 = structs_col{{grandchild1, grandchild2}, null_at(0)};
return structs_col{{child1}};
};
auto const lhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_lhs().release(), 0, {});
auto const rhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_rhs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 8, 19, 32}.release(), get_structs_expected().release(), 0, {});
auto const results_sorted = set_union_sorted(*lhs, *rhs, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results_sorted);
}
// Nulls are unequal.
{
auto const get_structs_expected = [] {
auto grandchild1 = int32s_col{
{
null, null, null, null, null, null, null, null, 1, 1, 1, 2, 2, 2, // end list1
null, null, 1, 1, 1, 1, 2, 3, 3, 3, 3, 3, // end list2
null, null, null, null, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4 // end list3
},
nulls_at({0, 1, 2, 3, 4, 5, 6, 7, 14, 15, 26, 27, 28, 29})};
auto grandchild2 = strings_col{
{
"" /*NULL*/, "" /*NULL*/, "" /*NULL*/, "" /*NULL*/, "" /*NULL*/, "" /*NULL*/, "Apple",
"Apple", "Banana", "Cherry", "Kiwi", "Banana", "Cherry",
"Kiwi", // end list1
"" /*NULL*/, "" /*NULL*/, "Bear", "Cat", "Dog", "Duck", "Panda",
"Bear", "Cat", "Dog", "Duck", "Panda", // end list2
"ÁÁÁ", "ÁÁÁ", "ÉÉÉÉÉ", "ÉÉÉÉÉ", "ÁBC", "ÁÁÁ", "ÍÍÍÍÍ",
"" /*NULL*/, "XYZ", "ÁBC", "" /*NULL*/, "XYZ", "ÁBC", "ÁÁÁ",
"ÍÍÍÍÍ" // end list3
},
nulls_at({0, 1, 2, 3, 4, 5, 14, 15, 33, 36})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({0, 1, 2, 3, 4, 5})};
return structs_col{{child1}};
};
auto const lhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_lhs().release(), 0, {});
auto const rhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_rhs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 14, 26, 41}.release(), get_structs_expected().release(), 0, {});
auto const results_sorted = set_union_sorted(*lhs, *rhs, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results_sorted);
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests/lists
|
rapidsai_public_repos/cudf/cpp/tests/lists/set_operations/have_overlap_tests.cpp
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/lists/set_operations.hpp>
#include <limits>
#include <string>
using float_type = double;
using namespace cudf::test::iterators;
auto constexpr null{0}; // null at current level
auto constexpr XXX{0}; // null pushed down from parent level
auto constexpr neg_NaN = -std::numeric_limits<float_type>::quiet_NaN();
auto constexpr neg_Inf = -std::numeric_limits<float_type>::infinity();
auto constexpr NaN = std::numeric_limits<float_type>::quiet_NaN();
auto constexpr Inf = std::numeric_limits<float_type>::infinity();
auto constexpr NULL_EQUAL = cudf::null_equality::EQUAL;
auto constexpr NULL_UNEQUAL = cudf::null_equality::UNEQUAL;
auto constexpr NAN_EQUAL = cudf::nan_equality::ALL_EQUAL;
auto constexpr NAN_UNEQUAL = cudf::nan_equality::UNEQUAL;
using bools_col = cudf::test::fixed_width_column_wrapper<bool>;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_lists = cudf::test::lists_column_wrapper<float_type>;
using strings_lists = cudf::test::lists_column_wrapper<cudf::string_view>;
using strings_col = cudf::test::strings_column_wrapper;
using structs_col = cudf::test::structs_column_wrapper;
using lists_cv = cudf::lists_column_view;
struct ListOverlapTest : public cudf::test::BaseFixture {};
template <typename T>
struct ListOverlapTypedTest : public cudf::test::BaseFixture {};
using TestTypes =
cudf::test::Concat<cudf::test::IntegralTypesNotBool, cudf::test::FloatingPointTypes>;
TYPED_TEST_SUITE(ListOverlapTypedTest, TestTypes);
TEST_F(ListOverlapTest, TrivialTest)
{
auto const lhs =
floats_lists{{floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 0.0}, null_at(6)},
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)},
{} /*NULL*/,
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)}},
null_at(2)};
auto const rhs =
floats_lists{{floats_lists{{1.0, 0.5, null, 0.0, 0.0, null, NaN}, nulls_at({2, 5})},
floats_lists{{2.0, 1.0, null, 0.0, 0.0, null}, nulls_at({2, 5})},
floats_lists{{2.0, 1.0, null, 0.0, 0.0, null}, nulls_at({2, 5})},
{} /*NULL*/},
null_at(3)};
auto const expected = bools_col{{1, 1, null, null}, nulls_at({2, 3})};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
TEST_F(ListOverlapTest, FloatingPointTestsWithSignedZero)
{
// -0.0 and 0.0 should be considered equal.
auto const lhs = floats_lists{{0.0, 0.0, 0.0, 0.0, 0.0}, {-0.0, 1.0}, {0.0}};
auto const rhs = floats_lists{{-0.0, -0.0, -0.0, -0.0, -0.0}, {0.0, 2.0}, {1.0}};
auto const expected = bools_col{1, 1, 0};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
TEST_F(ListOverlapTest, FloatingPointTestsWithInf)
{
auto const lhs = floats_lists{{Inf, Inf, Inf}, {Inf, 0.0, neg_Inf}};
auto const rhs = floats_lists{{neg_Inf, neg_Inf}, {0.0}};
auto const expected = bools_col{0, 1};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
TEST_F(ListOverlapTest, FloatingPointTestsWithNaNs)
{
auto const lhs =
floats_lists{{0, -1, 1, NaN}, {2, 0, neg_NaN}, {1, -2, 2, 0, 1, 2}, {NaN, NaN, NaN, NaN, NaN}};
auto const rhs =
floats_lists{{2, 3, 4, neg_NaN}, {2, 0}, {neg_NaN, 1, -2, 2, 0, 1, 2}, {neg_NaN, neg_NaN}};
// NaNs are equal.
{
auto const expected = bools_col{1, 1, 1, 1};
auto const results =
cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs}, NULL_EQUAL, NAN_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// NaNs are unequal.
{
auto const expected = bools_col{0, 1, 1, 0};
auto const results =
cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs}, NULL_EQUAL, NAN_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
}
TEST_F(ListOverlapTest, StringTestsNonNull)
{
// Trivial cases - empty input.
{
auto const lhs = strings_lists{};
auto const rhs = strings_lists{};
auto const expected = bools_col{};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// Trivial cases - empty input.
{
auto const lhs = strings_lists{strings_lists{}};
auto const rhs = strings_lists{strings_lists{}};
auto const expected = bools_col{0};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// No overlap.
{
auto const lhs = strings_lists{"this", "is", "a", "string"};
auto const rhs = strings_lists{"aha", "bear", "blow", "heat"};
auto const expected = bools_col{0};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// One list column.
{
auto const lhs = strings_lists{"this", "is", "a", "string"};
auto const rhs = strings_lists{"a", "delicious", "banana"};
auto const expected = bools_col{1};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// Multiple lists column.
{
auto const lhs = strings_lists{strings_lists{"one", "two", "three"},
strings_lists{"four", "five", "six"},
strings_lists{"1", "2", "3"}};
auto const rhs = strings_lists{strings_lists{"one", "banana"},
strings_lists{"apple", "kiwi", "cherry"},
strings_lists{"two", "and", "1"}};
auto const expected = bools_col{1, 0, 1};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
}
TEST_F(ListOverlapTest, StringTestsWithNullsEqual)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const lhs = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const rhs =
strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})};
auto const expected = bools_col{1};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs}, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// Multiple lists column with null lists and null entries.
{
auto const lhs = strings_lists{
strings_lists{{"this", null, "is", null, "a", null, null, "string"}, nulls_at({1, 3, 5, 6})},
strings_lists{},
strings_lists{"this", "is", "a", "string"}};
auto const rhs = strings_lists{
{strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})},
strings_lists{}, /* NULL */
strings_lists{"aha", "this", "is another", "string???"}},
null_at(1)};
auto const expected = bools_col{{1, 0 /*null*/, 1}, null_at(1)};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs}, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
}
TEST_F(ListOverlapTest, StringTestsWithNullsUnequal)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const lhs = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const rhs =
strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})};
auto const expected = bools_col{0};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs}, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// Multiple lists column with null lists and null entries.
{
auto const lhs = strings_lists{
strings_lists{{"this", null, "is", null, "a", null, null, "string"}, nulls_at({1, 3, 5, 6})},
strings_lists{},
strings_lists{"this", "is", "a", "string"}};
auto const rhs = strings_lists{
{strings_lists{{"aha", null, "abc", null, "1111", null, "2222"}, nulls_at({1, 3, 5})},
strings_lists{}, /* NULL */
strings_lists{"aha", "this", "is another", "string???"}},
null_at(1)};
auto const expected = bools_col{{0, 0 /*null*/, 1}, null_at(1)};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs}, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
}
TYPED_TEST(ListOverlapTypedTest, TrivialInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
// Empty input.
{
auto const lhs = lists_col{};
auto const rhs = lists_col{};
auto const expected = bools_col{};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// All input lists are empty.
{
auto const lhs = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const rhs = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const expected = bools_col{0, 0, 0};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// Multiple empty lists.
{
auto const lhs = lists_col{{}, {1, 2}, {}, {5, 4, 3, 2, 1, 0}, {}, {6}, {}};
auto const rhs = lists_col{{}, {}, {0}, {0, 1, 2, 3, 4, 5}, {}, {6, 7}, {}};
auto const expected = bools_col{0, 0, 0, 1, 0, 1, 0};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
}
TYPED_TEST(ListOverlapTypedTest, SlicedNonNullInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto const lhs_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {3, 2, 1, 4, 1}, {5}, {10, 8, 9}, {6, 7}};
auto const rhs_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {5, 6, 7, 8, 7, 5}, {}, {1, 2, 3}, {6, 7}};
{
auto const expected = bools_col{1, 0, 0, 0, 1};
auto const results = cudf::lists::have_overlap(lists_cv{lhs_original}, lists_cv{rhs_original});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
{
auto const lhs = cudf::slice(lhs_original, {1, 5})[0];
auto const rhs = cudf::slice(rhs_original, {1, 5})[0];
auto const expected = bools_col{0, 0, 0, 1};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
{
auto const lhs = cudf::slice(lhs_original, {1, 3})[0];
auto const rhs = cudf::slice(rhs_original, {1, 3})[0];
auto const expected = bools_col{0, 0};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
{
auto const lhs = cudf::slice(lhs_original, {0, 3})[0];
auto const rhs = cudf::slice(rhs_original, {0, 3})[0];
auto const expected = bools_col{1, 0, 0};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
}
TYPED_TEST(ListOverlapTypedTest, InputHaveNullsTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto constexpr null = TypeParam{0};
// Nullable lists.
{
auto const lhs = lists_col{{{3, 2, 1, 4, 1}, {5}, {} /*NULL*/, {} /*NULL*/, {10, 8, 9}, {6, 7}},
nulls_at({2, 3})};
auto const rhs =
lists_col{{{1, 2}, {} /*NULL*/, {3}, {} /*NULL*/, {10, 11, 12}, {1, 2}}, nulls_at({1, 3})};
auto const expected =
bools_col{{1, 0 /*null*/, 0 /*null*/, 0 /*null*/, 1, 0}, nulls_at({1, 2, 3})};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// Nullable child and nulls are equal.
{
auto const lhs = lists_col{lists_col{{null, 1, null, 3}, nulls_at({0, 2})},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, null, 9}, nulls_at({0, 2})}};
auto const rhs = lists_col{lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{5, null}, null_at(1)},
lists_col{7, 8, 9}};
auto const expected = bools_col{1, 1, 1};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs}, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// Nullable child and nulls are unequal.
{
auto const lhs = lists_col{lists_col{{null, 1, null, 3}, nulls_at({0, 2})},
lists_col{{null, 5}, null_at(0)},
lists_col{{null, 7, null, 9}, nulls_at({0, 2})}};
auto const rhs = lists_col{lists_col{{null, null, 5}, nulls_at({0, 1})},
lists_col{{5, null}, null_at(1)},
lists_col{7, 8, 9}};
auto const expected = bools_col{0, 1, 1};
auto const results = cudf::lists::have_overlap(lists_cv{lhs}, lists_cv{rhs}, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
}
TEST_F(ListOverlapTest, InputListsOfNestedStructsHaveNull)
{
auto const get_structs_lhs = [] {
auto grandchild1 = int32s_col{{
1, XXX, null, XXX, XXX, 1, 1, 1, // list1
1, 1, 1, 1, 2, 1, null, 2, // list2
null, null, 2, 2, 3, 2, 3, 3 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"XXX", /*NULL*/
"Apple",
"XXX", /*NULL*/
"XXX", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
// Only grandchild1 of rhs is different from lhs'. The rest is exactly the same.
auto const get_structs_rhs = [] {
auto grandchild1 = int32s_col{{
2, XXX, null, XXX, XXX, 2, 2, 2, // list1
3, 3, 3, 3, 3, 3, null, 3, // list2
null, null, 4, 4, 4, 4, 4, 4 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"XXX", /*NULL*/
"Apple",
"XXX", /*NULL*/
"XXX", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
// Nulls are equal.
{
auto const lhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_lhs().release(), 0, {});
auto const rhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_rhs().release(), 0, {});
auto const expected = bools_col{1, 1, 1};
auto const results = cudf::lists::have_overlap(lists_cv{*lhs}, lists_cv{*rhs}, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
// Nulls are unequal.
{
auto const lhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_lhs().release(), 0, {});
auto const rhs = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs_rhs().release(), 0, {});
auto const expected = bools_col{0, 0, 0};
auto const results = cudf::lists::have_overlap(lists_cv{*lhs}, lists_cv{*rhs}, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
}
TEST_F(ListOverlapTest, InputListsOfStructsOfLists)
{
auto const lhs = [] {
auto const get_structs = [] {
auto child1 = int32s_col{// begin list1
0,
1,
2, // end list1
// begin list2
3, // end list2
// begin list3
4,
5,
6};
auto child2 = floats_lists{// begin list1
floats_lists{0, 1},
floats_lists{0, 2},
floats_lists{1, 1}, // end list1
// begin list2
floats_lists{3, 4, 5}, // end list2
// begin list3
floats_lists{6, 7},
floats_lists{6, 8},
floats_lists{6, 7, 8}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
3, int32s_col{0, 3, 4, 7}.release(), get_structs().release(), 0, {});
}();
auto const rhs = [] {
auto const get_structs = [] {
auto child1 = int32s_col{// begin list1
0,
1,
2, // end list1
// begin list2
3, // end list2
// begin list3
4,
5,
6};
auto child2 = floats_lists{// begin list1
floats_lists{1, 1},
floats_lists{1, 2},
floats_lists{1, 2}, // end list1
// begin list2
floats_lists{3, 4, 5}, // end list2
// begin list3
floats_lists{6, 7, 8, 9},
floats_lists{6, 8},
floats_lists{3, 4, 5}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
3, int32s_col{0, 3, 4, 7}.release(), get_structs().release(), 0, {});
}();
auto const expected = bools_col{0, 1, 1};
auto const results = cudf::lists::have_overlap(lists_cv{*lhs}, lists_cv{*rhs});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests/lists
|
rapidsai_public_repos/cudf/cpp/tests/lists/stream_compaction/distinct_tests.cpp
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/lists/sorting.hpp>
#include <cudf/lists/stream_compaction.hpp>
using float_type = double;
using namespace cudf::test::iterators;
auto constexpr null{0}; // null at current level
auto constexpr XXX{0}; // null pushed down from parent level
auto constexpr neg_NaN = -std::numeric_limits<float_type>::quiet_NaN();
auto constexpr neg_Inf = -std::numeric_limits<float_type>::infinity();
auto constexpr NaN = std::numeric_limits<float_type>::quiet_NaN();
auto constexpr Inf = std::numeric_limits<float_type>::infinity();
auto constexpr NULL_EQUAL = cudf::null_equality::EQUAL;
auto constexpr NULL_UNEQUAL = cudf::null_equality::UNEQUAL;
auto constexpr NAN_EQUAL = cudf::nan_equality::ALL_EQUAL;
auto constexpr NAN_UNEQUAL = cudf::nan_equality::UNEQUAL;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_lists = cudf::test::lists_column_wrapper<float_type>;
using strings_lists = cudf::test::lists_column_wrapper<cudf::string_view>;
using strings_col = cudf::test::strings_column_wrapper;
using structs_col = cudf::test::structs_column_wrapper;
using lists_cv = cudf::lists_column_view;
namespace {
auto distinct_sorted(cudf::column_view const& input,
cudf::null_equality nulls_equal = NULL_EQUAL,
cudf::nan_equality nans_equal = NAN_EQUAL)
{
auto const results = cudf::lists::distinct(lists_cv{input}, nulls_equal, nans_equal);
// The sorted result will have nulls first and NaNs last.
// In addition, row equality comparisons in tests just ignore NaN sign thus the expected values
// can be just NaN while the input can be mixed of NaN and neg_NaN.
return cudf::lists::sort_lists(
lists_cv{*results}, cudf::order::ASCENDING, cudf::null_order::BEFORE);
}
} // namespace
struct ListDistinctTest : public cudf::test::BaseFixture {};
template <typename T>
struct ListDistinctTypedTest : public cudf::test::BaseFixture {};
using TestTypes =
cudf::test::Concat<cudf::test::IntegralTypesNotBool, cudf::test::FloatingPointTypes>;
TYPED_TEST_SUITE(ListDistinctTypedTest, TestTypes);
TEST_F(ListDistinctTest, TrivialTest)
{
auto const input =
floats_lists{{floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 0.0}, null_at(6)},
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)},
{} /*NULL*/,
floats_lists{{NaN, 5.0, 0.0, 0.0, 0.0, 0.0, null, 1.0}, null_at(6)}},
null_at(2)};
auto const expected = floats_lists{{floats_lists{{null, 0.0, 5.0, NaN}, null_at(0)},
floats_lists{{null, 0.0, 1.0, 5.0, NaN}, null_at(0)},
floats_lists{} /*NULL*/,
floats_lists{{null, 0.0, 1.0, 5.0, NaN}, null_at(0)}},
null_at(2)};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(ListDistinctTest, FloatingPointTestsWithSignedZero)
{
// -0.0 and 0.0 should be considered equal.
auto const input = floats_lists{0.0, 1, 2, -0.0, 1, 2, 0.0, 1, 2, -0.0, -0.0, 0.0, 0.0, 3};
auto const expected = floats_lists{0, 1, 2, 3};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(ListDistinctTest, FloatingPointTestsWithInf)
{
auto const input = floats_lists{Inf, 0, neg_Inf, 0, Inf, 0, neg_Inf, 0, Inf, 0, neg_Inf};
auto const expected = floats_lists{neg_Inf, 0, Inf};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
TEST_F(ListDistinctTest, FloatingPointTestsWithNaNs)
{
auto const input =
floats_lists{0, -1, 1, NaN, 2, 0, neg_NaN, 1, -2, 2, 0, 1, 2, neg_NaN, NaN, NaN, NaN, neg_NaN};
// NaNs are equal.
{
auto const expected = floats_lists{-2, -1, 0, 1, 2, NaN};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// NaNs are unequal.
{
auto const expected = floats_lists{-2, -1, 0, 1, 2, NaN, NaN, NaN, NaN, NaN, NaN, NaN};
auto const results_sorted = distinct_sorted(input, NULL_EQUAL, NAN_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(ListDistinctTest, StringTestsNonNull)
{
// Trivial cases - empty input.
{
auto const input = strings_lists{{}};
auto const expected = strings_lists{{}};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// No duplicate.
{
auto const input = strings_lists{"this", "is", "a", "string"};
auto const expected = strings_lists{"a", "is", "string", "this"};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// One list column.
{
auto const input = strings_lists{"this", "is", "is", "is", "a", "string", "string"};
auto const expected = strings_lists{"a", "is", "string", "this"};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column.
{
auto const input = strings_lists{
strings_lists{"this", "is", "a", "no duplicate", "string"},
strings_lists{"this", "is", "is", "a", "one duplicate", "string"},
strings_lists{"this", "is", "is", "is", "a", "two duplicates", "string"},
strings_lists{"this", "is", "is", "is", "is", "a", "three duplicates", "string"}};
auto const expected =
strings_lists{strings_lists{"a", "is", "no duplicate", "string", "this"},
strings_lists{"a", "is", "one duplicate", "string", "this"},
strings_lists{"a", "is", "string", "this", "two duplicates"},
strings_lists{"a", "is", "string", "this", "three duplicates"}};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(ListDistinctTest, StringTestsWithNullsEqual)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const input = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const expected = strings_lists{{null, "a", "is", "string", "this"}, null_at(0)};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column with null lists and null entries.
{
auto const input = strings_lists{
{strings_lists{{"this", null, "is", null, "a", null, "no duplicate", null, "string"},
nulls_at({1, 3, 5, 7})},
strings_lists{}, /* NULL */
strings_lists{"this", "is", "is", "a", "one duplicate", "string"}},
null_at(1)};
auto const expected =
strings_lists{{strings_lists{{null, "a", "is", "no duplicate", "string", "this"}, null_at(0)},
strings_lists{}, /* NULL */
strings_lists{"a", "is", "one duplicate", "string", "this"}},
null_at(1)};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(ListDistinctTest, StringTestsWithNullsUnequal)
{
auto const null = std::string("");
// One list column with null entries.
{
auto const input = strings_lists{
{"this", null, "is", "is", "is", "a", null, "string", null, "string"}, nulls_at({1, 6, 8})};
auto const expected =
strings_lists{{null, null, null, "a", "is", "string", "this"}, nulls_at({0, 1, 2})};
auto const results_sorted = distinct_sorted(input, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple lists column with null lists and null entries.
{
auto const input = strings_lists{
{strings_lists{{"this", null, "is", null, "a", null, "no duplicate", null, "string"},
nulls_at({1, 3, 5, 7})},
strings_lists{}, /* NULL */
strings_lists{"this", "is", "is", "a", "one duplicate", "string"}},
null_at(1)};
auto const expected = strings_lists{
{strings_lists{{null, null, null, null, "a", "is", "no duplicate", "string", "this"},
nulls_at({0, 1, 2, 3})},
strings_lists{}, /* NULL */
strings_lists{"a", "is", "one duplicate", "string", "this"}},
null_at(1)};
auto const results_sorted = distinct_sorted(input, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(ListDistinctTypedTest, TrivialInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
// Empty input.
{
auto const input = lists_col{};
auto const expected = lists_col{};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// All input lists are empty.
{
auto const input = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const expected = lists_col{lists_col{}, lists_col{}, lists_col{}};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Trivial cases.
{
auto const input = lists_col{0, 1, 2, 3, 4, 5};
auto const expected = lists_col{0, 1, 2, 3, 4, 5};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Multiple empty lists.
{
auto const input = lists_col{{}, {}, {5, 4, 3, 2, 1, 0}, {}, {6}, {}};
auto const expected = lists_col{{}, {}, {0, 1, 2, 3, 4, 5}, {}, {6}, {}};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(ListDistinctTypedTest, SlicedNonNullInputTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto const input_original =
lists_col{{1, 2, 3, 2, 3, 2, 3, 2, 3}, {3, 2, 1, 4, 1}, {5}, {10, 8, 9}, {6, 7}};
{
auto const expected = lists_col{{1, 2, 3}, {1, 2, 3, 4}, {5}, {8, 9, 10}, {6, 7}};
auto const results_sorted = distinct_sorted(input_original);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const input = cudf::slice(input_original, {0, 5})[0];
auto const expected = lists_col{{1, 2, 3}, {1, 2, 3, 4}, {5}, {8, 9, 10}, {6, 7}};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const input = cudf::slice(input_original, {1, 5})[0];
auto const expected = lists_col{{1, 2, 3, 4}, {5}, {8, 9, 10}, {6, 7}};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const input = cudf::slice(input_original, {1, 3})[0];
auto const expected = lists_col{{1, 2, 3, 4}, {5}};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
{
auto const input = cudf::slice(input_original, {0, 3})[0];
auto const expected = lists_col{{1, 2, 3}, {1, 2, 3, 4}, {5}};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TYPED_TEST(ListDistinctTypedTest, InputHaveNullsTests)
{
using lists_col = cudf::test::lists_column_wrapper<TypeParam>;
auto constexpr null = TypeParam{0};
// Nullable lists.
{
auto const input = lists_col{
{{3, 2, 1, 4, 1}, {5}, {} /*NULL*/, {} /*NULL*/, {10, 8, 9}, {6, 7}}, nulls_at({2, 3})};
auto const expected = lists_col{
{{1, 2, 3, 4}, {5}, {} /*NULL*/, {} /*NULL*/, {8, 9, 10}, {6, 7}}, nulls_at({2, 3})};
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Nullable child and nulls are equal.
{
auto const input =
lists_col{{null, 1, null, 3, null, 5, null, 7, null, 9}, nulls_at({0, 2, 4, 6, 8})};
auto const expected = lists_col{{null, 1, 3, 5, 7, 9}, null_at(0)};
auto const results_sorted = distinct_sorted(input, NULL_EQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
// Nullable child and nulls are unequal.
{
auto const input = lists_col{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, nulls_at({0, 2, 4, 6, 8})};
auto const expected =
lists_col{{null, null, null, null, null, 1, 3, 5, 7, 9}, nulls_at({0, 1, 2, 3, 4})};
auto const results_sorted = distinct_sorted(input, NULL_UNEQUAL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(ListDistinctTest, InputListsOfStructsNoNull)
{
auto const get_structs = [] {
auto child1 = int32s_col{
1, 1, 1, 1, 1, 1, 1, 1, // list1
1, 1, 1, 1, 2, 1, 2, 2, // list2
2, 2, 2, 2, 3, 2, 3, 3 // list3
};
auto child2 = strings_col{
// begin list1
"Banana",
"Mango",
"Apple",
"Cherry",
"Kiwi",
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"Cat",
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"XYZ",
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
};
return structs_col{{child1, child2}};
};
auto const get_expected = [] {
auto child1 = int32s_col{1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3};
auto child2 = strings_col{
// begin list1
"Apple",
"Banana",
"Cherry",
"Kiwi",
"Mango", // end list1
// begin list2
"Bear",
"Cat",
"Dog",
"Duck",
"Cat",
"Panda", // end list2
// begin list3
"ÁBC",
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"XYZ",
"ÁBC" // end list3
};
return structs_col{{child1, child2}};
};
// Test full columns.
{
auto const input = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 5, 11, 17}.release(), get_expected().release(), 0, {});
auto const results_sorted = distinct_sorted(*input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results_sorted);
}
// Test sliced columns.
{
auto const input_original = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs().release(), 0, {});
auto const expected_original = cudf::make_lists_column(
3, int32s_col{0, 5, 11, 17}.release(), get_expected().release(), 0, {});
auto const input = cudf::slice(*input_original, {1, 3})[0];
auto const expected = cudf::slice(*expected_original, {1, 3})[0];
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(ListDistinctTest, InputListsOfStructsHaveNull)
{
auto const get_structs = [] {
auto child1 = int32s_col{{
1, 1, null, XXX, XXX, 1, 1, 1, // list1
1, 1, 1, 1, 2, 1, null, 2, // list2
null, null, 2, 2, 3, 2, 3, 3 // list3
},
nulls_at({2, 14, 16, 17})};
auto child2 = strings_col{{
// begin list1
"Banana",
"Mango",
"Apple",
"XXX", /*NULL*/
"XXX", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
return structs_col{{child1, child2}, nulls_at({3, 4})};
};
auto const get_expected = [] {
auto child1 = int32s_col{{ // begin list1
XXX, // end list1
null,
1,
1,
1,
1,
// begin list2
null, // end list2
1,
1,
1,
1,
2,
// begin list3
null,
null,
2,
2,
2,
3,
3,
3}, // end list3
nulls_at({1, 6, 12, 13})};
auto child2 = strings_col{{ // begin list1
"XXX", /*NULL*/
"Apple",
"Banana",
"Cherry",
"Kiwi",
"Mango", // end list1
// begin list2
"", /*NULL*/
"Bear",
"Cat",
"Dog",
"Duck",
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÁBC",
"ÁÁÁ",
"ÍÍÍÍÍ",
"", /*NULL*/
"XYZ",
"ÁBC"}, // end list3
nulls_at({6, 17})};
return structs_col{{child1, child2}, null_at(0)};
};
// Test full columns.
{
auto const input = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 6, 12, 20}.release(), get_expected().release(), 0, {});
auto const results_sorted = distinct_sorted(*input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results_sorted);
}
// Test sliced columns.
{
auto const input_original = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs().release(), 0, {});
auto const expected_original = cudf::make_lists_column(
3, int32s_col{0, 6, 12, 20}.release(), get_expected().release(), 0, {});
auto const input = cudf::slice(*input_original, {1, 3})[0];
auto const expected = cudf::slice(*expected_original, {1, 3})[0];
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(ListDistinctTest, InputListsOfNestedStructsHaveNull)
{
auto const get_structs = [] {
auto grandchild1 = int32s_col{{
1, XXX, null, XXX, XXX, 1, 1, 1, // list1
1, 1, 1, 1, 2, 1, null, 2, // list2
null, null, 2, 2, 3, 2, 3, 3 // list3
},
nulls_at({2, 14, 16, 17})};
auto grandchild2 = strings_col{{
// begin list1
"Banana",
"YYY", /*NULL*/
"Apple",
"XXX", /*NULL*/
"YYY", /*NULL*/
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"Bear",
"Duck",
"Cat",
"Dog",
"Panda",
"Bear",
"" /*NULL*/,
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÍÍÍÍÍ",
"ÁBC",
"" /*NULL*/,
"ÁÁÁ",
"ÁBC",
"XYZ" // end list3
},
nulls_at({14, 20})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({1, 3, 4})};
return structs_col{{child1}};
};
auto const get_expected = [] {
auto grandchild1 = int32s_col{{// begin list1
XXX,
null,
1,
1,
1, // end list1
// begin list2
null,
1,
1,
1,
1,
2, // end list2
// begin list3
null,
null,
2,
2,
2,
3,
3,
3},
nulls_at({1, 5, 11, 12})};
auto grandchild2 = strings_col{{
// begin list1
"XXX" /*NULL*/,
"Apple",
"Banana",
"Cherry",
"Kiwi", // end list1
// begin list2
"" /*NULL*/,
"Bear",
"Cat",
"Dog",
"Duck",
"Panda", // end list2
// begin list3
"ÁÁÁ",
"ÉÉÉÉÉ",
"ÁBC",
"ÁÁÁ",
"ÍÍÍÍÍ",
"", /*NULL*/
"XYZ",
"ÁBC" // end list3
},
nulls_at({5, 16})};
auto child1 = structs_col{{grandchild1, grandchild2}, nulls_at({0})};
return structs_col{{child1}};
};
// Test full columns.
{
auto const input = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs().release(), 0, {});
auto const expected = cudf::make_lists_column(
3, int32s_col{0, 5, 11, 19}.release(), get_expected().release(), 0, {});
auto const results_sorted = distinct_sorted(*input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results_sorted);
}
// Test sliced columns.
{
auto const input_original = cudf::make_lists_column(
3, int32s_col{0, 8, 16, 24}.release(), get_structs().release(), 0, {});
auto const expected_original = cudf::make_lists_column(
3, int32s_col{0, 5, 11, 19}.release(), get_expected().release(), 0, {});
auto const input = cudf::slice(*input_original, {1, 3})[0];
auto const expected = cudf::slice(*expected_original, {1, 3})[0];
auto const results_sorted = distinct_sorted(input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results_sorted);
}
}
TEST_F(ListDistinctTest, InputListsOfStructsOfLists)
{
auto const input = [] {
auto const get_structs = [] {
auto child1 = int32s_col{// begin list1
0,
0,
0, // end list1
// begin list2
1, // end list2
// begin list3
2,
2, // end list3
// begin list4
3,
3,
3};
auto child2 = floats_lists{// begin list1
floats_lists{0, 1},
floats_lists{0, 1},
floats_lists{0, 1}, // end list1
// begin list2
floats_lists{3, 4, 5}, // end list2
// begin list3
floats_lists{},
floats_lists{}, // end list3
// begin list4
floats_lists{6, 7},
floats_lists{6, 7},
floats_lists{6, 7}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
4, int32s_col{0, 3, 4, 6, 9}.release(), get_structs().release(), 0, {});
}();
auto const expected = [] {
auto const get_structs = [] {
auto child1 = int32s_col{0, 1, 2, 3};
auto child2 =
floats_lists{floats_lists{0, 1}, floats_lists{3, 4, 5}, floats_lists{}, floats_lists{6, 7}};
return structs_col{{child1, child2}};
};
return cudf::make_lists_column(
4, int32s_col{0, 1, 2, 3, 4}.release(), get_structs().release(), 0, {});
}();
auto const results = cudf::lists::distinct(lists_cv{*input});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests/lists
|
rapidsai_public_repos/cudf/cpp/tests/lists/stream_compaction/apply_boolean_mask_tests.cpp
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/column/column_factories.hpp>
#include <cudf/detail/null_mask.hpp>
#include <cudf/lists/extract.hpp>
#include <cudf/lists/stream_compaction.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
namespace cudf::test {
using namespace iterators;
using cudf::lists_column_view;
using cudf::lists::apply_boolean_mask;
template <typename T>
using lists = lists_column_wrapper<T, int32_t>;
using filter_t = lists_column_wrapper<bool, int32_t>;
template <typename T>
using fwcw = fixed_width_column_wrapper<T, int32_t>;
using offsets = fwcw<int32_t>;
using strings = strings_column_wrapper;
auto constexpr X = int32_t{0}; // Placeholder for NULL.
struct ApplyBooleanMaskTest : public BaseFixture {};
template <typename T>
struct ApplyBooleanMaskTypedTest : ApplyBooleanMaskTest {};
TYPED_TEST_SUITE(ApplyBooleanMaskTypedTest, cudf::test::NumericTypes);
TYPED_TEST(ApplyBooleanMaskTypedTest, StraightLine)
{
using T = TypeParam;
auto input = lists<T>{{0, 1, 2, 3}, {4, 5}, {6, 7, 8, 9}, {0, 1}, {2, 3, 4, 5}, {6, 7}}.release();
auto filter = filter_t{{1, 0, 1, 0}, {1, 0}, {1, 0, 1, 0}, {1, 0}, {1, 0, 1, 0}, {1, 0}};
{
// Unsliced.
auto filtered = apply_boolean_mask(lists_column_view{*input}, lists_column_view{filter});
auto expected = lists<T>{{0, 2}, {4}, {6, 8}, {0}, {2, 4}, {6}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*filtered, expected);
}
{
// Sliced input: Remove the first row.
auto sliced = cudf::slice(*input, {1, input->size()}).front();
// == lists_t {{4, 5}, {6, 7, 8, 9}, {0, 1}, {2, 3, 4, 5}, {6, 7}};
auto filter = filter_t{{0, 1}, {0, 1, 0, 1}, {1, 1}, {0, 1, 0, 1}, {0, 0}};
auto filtered = apply_boolean_mask(lists_column_view{sliced}, lists_column_view{filter});
auto expected = lists<T>{{5}, {7, 9}, {0, 1}, {3, 5}, {}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*filtered, expected);
}
}
TYPED_TEST(ApplyBooleanMaskTypedTest, NullElementsInTheListRows)
{
using T = TypeParam;
auto input =
lists<T>{
{0, 1, 2, 3},
lists<T>{{X, 5}, null_at(0)},
{6, 7, 8, 9},
{0, 1},
lists<T>{{X, 3, 4, X}, nulls_at({0, 3})},
lists<T>{{X, X}, nulls_at({0, 1})},
}
.release();
auto filter = filter_t{{1, 0, 1, 0}, {1, 0}, {1, 0, 1, 0}, {1, 0}, {1, 0, 1, 0}, {1, 0}};
{
// Unsliced.
auto filtered = apply_boolean_mask(lists_column_view{*input}, lists_column_view{filter});
auto expected = lists<T>{{0, 2},
lists<T>{{X}, null_at(0)},
{6, 8},
{0},
lists<T>{{X, 4}, null_at(0)},
lists<T>{{X}, null_at(0)}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*filtered, expected);
}
{
// Sliced input: Remove the first row.
auto sliced = cudf::slice(*input, {1, input->size()}).front();
// == lists_t {{X, 5}, {6, 7, 8, 9}, {0, 1}, {X, 3, 4, X}, {X, X}};
auto filter = filter_t{{0, 1}, {0, 1, 0, 1}, {1, 1}, {0, 1, 0, 1}, {0, 0}};
auto filtered = apply_boolean_mask(lists_column_view{sliced}, lists_column_view{filter});
auto expected = lists<T>{{5}, {7, 9}, {0, 1}, lists<T>{{3, X}, null_at(1)}, {}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*filtered, expected);
}
}
TYPED_TEST(ApplyBooleanMaskTypedTest, NullListRowsInTheInputColumn)
{
using T = TypeParam;
auto input =
lists<T>{{{0, 1, 2, 3}, {}, {6, 7, 8, 9}, {}, {2, 3, 4, 5}, {6, 7}}, nulls_at({1, 3})}
.release();
auto filter = filter_t{{1, 0, 1, 0}, {}, {1, 0, 1, 0}, {}, {1, 0, 1, 0}, {1, 0}};
{
// Unsliced.
auto filtered = apply_boolean_mask(lists_column_view{*input}, lists_column_view{filter});
auto expected = lists<T>{{{0, 2}, {}, {6, 8}, {}, {2, 4}, {6}}, nulls_at({1, 3})};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*filtered, expected);
}
{
// Sliced input: Remove the first row.
auto sliced = cudf::slice(*input, {1, input->size()}).front();
// == lists_t{{{}, {6, 7, 8, 9}, {}, {2, 3, 4, 5}, {6, 7}}, nulls_at({0,2})};
auto filter = filter_t{{}, {0, 1, 0, 1}, {}, {0, 1, 0, 1}, {0, 0}};
auto filtered = apply_boolean_mask(lists_column_view{sliced}, lists_column_view{filter});
auto expected = lists<T>{{{}, {7, 9}, {}, {3, 5}, {}}, nulls_at({0, 2})};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*filtered, expected);
}
{
// Sliced input: Remove the first two rows.
auto sliced = cudf::slice(*input, {2, input->size()}).front();
// == lists_t{{{6, 7, 8, 9}, {}, {2, 3, 4, 5}, {6, 7}}, null_at(1)};
auto filter = filter_t{{0, 1, 0, 1}, {}, {0, 1, 0, 1}, {0, 0}};
auto filtered = apply_boolean_mask(lists_column_view{sliced}, lists_column_view{filter});
auto expected = lists<T>{{{7, 9}, {}, {3, 5}, {}}, null_at(1)};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*filtered, expected);
}
}
TYPED_TEST(ApplyBooleanMaskTypedTest, StructInput)
{
using T = TypeParam;
using fwcw = fwcw<T>;
auto constexpr num_input_rows = 7;
auto const input = [] {
auto child_num = fwcw{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto child_str = strings{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
auto const null_mask_begin = null_at(5);
auto const null_mask_end = null_mask_begin + num_input_rows;
auto [null_mask, null_count] = detail::make_null_mask(null_mask_begin, null_mask_end);
return cudf::make_lists_column(num_input_rows,
offsets{0, 2, 3, 6, 6, 8, 8, 10}.release(),
structs_column_wrapper{{child_num, child_str}}.release(),
null_count,
std::move(null_mask));
}();
{
// Unsliced.
// The input should now look as follows: (String child dropped for brevity.)
// Input: {[0, 1], [2], [3, 4, 5], [], [6, 7], [], [8, 9]}
auto const filter = filter_t{{1, 1}, {0}, {0, 1, 0}, {}, {1, 0}, {}, {0, 1}};
auto const result = apply_boolean_mask(lists_column_view{*input}, lists_column_view{filter});
auto const expected = [] {
auto child_num = fwcw{0, 1, 4, 6, 9};
auto child_str = strings{"0", "1", "4", "6", "9"};
auto const null_mask_begin = null_at(5);
auto const null_mask_end = null_mask_begin + num_input_rows;
auto [null_mask, null_count] = detail::make_null_mask(null_mask_begin, null_mask_end);
return cudf::make_lists_column(num_input_rows,
offsets{0, 2, 2, 3, 3, 4, 4, 5}.release(),
structs_column_wrapper{{child_num, child_str}}.release(),
null_count,
std::move(null_mask));
}();
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
{
// Sliced. Remove the first row.
auto const sliced_input = cudf::slice(*input, {1, input->size()}).front();
// The input should now look as follows: (String child dropped for brevity.)
// Input: {[2], [3, 4, 5], [], [6, 7], [], [8, 9]}
auto const filter = filter_t{{0}, {0, 1, 0}, {}, {1, 0}, {}, {0, 1}};
auto const result =
apply_boolean_mask(lists_column_view{sliced_input}, lists_column_view{filter});
auto const expected = [] {
auto child_num = fwcw{4, 6, 9};
auto child_str = strings{"4", "6", "9"};
auto const null_mask_begin = null_at(4);
auto const null_mask_end = null_mask_begin + num_input_rows;
auto [null_mask, null_count] = detail::make_null_mask(null_mask_begin, null_mask_end);
return cudf::make_lists_column(num_input_rows - 1,
offsets{0, 0, 1, 1, 2, 2, 3}.release(),
structs_column_wrapper{{child_num, child_str}}.release(),
null_count,
std::move(null_mask));
}();
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
}
TEST_F(ApplyBooleanMaskTest, Trivial)
{
auto const input = lists<int32_t>{};
auto const filter = filter_t{};
auto const result = apply_boolean_mask(lists_column_view{input}, lists_column_view{filter});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, lists<int32_t>{});
}
TEST_F(ApplyBooleanMaskTest, Failure)
{
{
// Invalid mask type.
auto const input = lists<int32_t>{{1, 2, 3}, {4, 5, 6}};
auto const filter = lists<int32_t>{{0, 0, 0}};
EXPECT_THROW(apply_boolean_mask(lists_column_view{input}, lists_column_view{filter}),
cudf::logic_error);
}
{
// Mismatched number of rows.
auto const input = lists<int32_t>{{1, 2, 3}, {4, 5, 6}};
auto const filter = filter_t{{0, 0, 0}};
EXPECT_THROW(apply_boolean_mask(lists_column_view{input}, lists_column_view{filter}),
cudf::logic_error);
}
}
} // namespace cudf::test
| 0 |
rapidsai_public_repos/cudf/cpp/tests/lists
|
rapidsai_public_repos/cudf/cpp/tests/lists/combine/concatenate_rows_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/lists/combine.hpp>
using namespace cudf::test::iterators;
namespace {
using StrListsCol = cudf::test::lists_column_wrapper<cudf::string_view>;
using IntListsCol = cudf::test::lists_column_wrapper<int32_t>;
using IntCol = cudf::test::fixed_width_column_wrapper<int32_t>;
using TView = cudf::table_view;
constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::FIRST_ERROR};
constexpr int32_t null{0};
} // namespace
struct ListConcatenateRowsTest : public cudf::test::BaseFixture {};
TEST_F(ListConcatenateRowsTest, InvalidInput)
{
// Empty input table
EXPECT_THROW(cudf::lists::concatenate_rows(TView{}), cudf::logic_error);
// Input table contains non-list column
{
auto const col1 = IntCol{}.release();
auto const col2 = IntListsCol{}.release();
EXPECT_THROW(cudf::lists::concatenate_rows(TView{{col1->view(), col2->view()}}),
cudf::logic_error);
}
// Types mismatch
{
auto const col1 = IntListsCol{}.release();
auto const col2 = StrListsCol{}.release();
EXPECT_THROW(cudf::lists::concatenate_rows(TView{{col1->view(), col2->view()}}),
cudf::logic_error);
}
}
template <typename T>
struct ListConcatenateRowsTypedTest : public cudf::test::BaseFixture {};
using TypesForTest = cudf::test::Concat<cudf::test::IntegralTypesNotBool,
cudf::test::FloatingPointTypes,
cudf::test::FixedPointTypes>;
TYPED_TEST_SUITE(ListConcatenateRowsTypedTest, TypesForTest);
TYPED_TEST(ListConcatenateRowsTypedTest, ConcatenateEmptyColumns)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col = ListsCol{}.release();
auto const results = cudf::lists::concatenate_rows(TView{{col->view(), col->view()}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*col, *results, verbosity);
}
TYPED_TEST(ListConcatenateRowsTypedTest, ConcatenateOneColumnNotNull)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col = ListsCol{{1, 2}, {3, 4}, {5, 6}}.release();
auto const results = cudf::lists::concatenate_rows(TView{{col->view()}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*col, *results, verbosity);
}
TYPED_TEST(ListConcatenateRowsTypedTest, ConcatenateOneColumnWithNulls)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col = ListsCol{{ListsCol{{1, 2, null}, null_at(2)},
ListsCol{} /*NULL*/,
ListsCol{{null, 3, 4, 4, 4, 4}, null_at(0)},
ListsCol{5, 6}},
null_at(1)}
.release();
auto const results = cudf::lists::concatenate_rows(TView{{col->view()}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*col, *results, verbosity);
}
TYPED_TEST(ListConcatenateRowsTypedTest, SimpleInputNoNull)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col1 = ListsCol{{1, 2}, {3, 4}, {5, 6}}.release();
auto const empty_lists = ListsCol{ListsCol{}, ListsCol{}, ListsCol{}}.release();
auto const col2 = ListsCol{{7, 8}, {9, 10}, {11, 12}}.release();
auto const expected = ListsCol{{1, 2, 7, 8}, {3, 4, 9, 10}, {5, 6, 11, 12}}.release();
auto const results =
cudf::lists::concatenate_rows(TView{{col1->view(), empty_lists->view(), col2->view()}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
TYPED_TEST(ListConcatenateRowsTypedTest, SimpleInputWithNullableChild)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col1 = ListsCol{{1, 2}, ListsCol{{null}, null_at(0)}, {5, 6}}.release();
auto const empty_lists = ListsCol{{ListsCol{}, ListsCol{}, ListsCol{}}, null_at(2)}.release();
auto const col2 = ListsCol{{7, 8}, {9, 10}, {11, 12}}.release();
auto const expected =
ListsCol{{1, 2, 7, 8}, ListsCol{{null, 9, 10}, null_at(0)}, {5, 6, 11, 12}}.release();
auto const results =
cudf::lists::concatenate_rows(TView{{col1->view(), empty_lists->view(), col2->view()}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
TEST_F(ListConcatenateRowsTest, SimpleInputStringsColumnsNoNull)
{
auto const col1 = StrListsCol{
StrListsCol{"Tomato", "Apple"},
StrListsCol{"Banana", "Kiwi", "Cherry"},
StrListsCol{
"Coconut"}}.release();
auto const col2 =
StrListsCol{StrListsCol{"Orange"}, StrListsCol{"Lemon", "Peach"}, StrListsCol{}}.release();
auto const expected = StrListsCol{
StrListsCol{"Tomato", "Apple", "Orange"},
StrListsCol{"Banana", "Kiwi", "Cherry", "Lemon", "Peach"},
StrListsCol{
"Coconut"}}.release();
auto const results = cudf::lists::concatenate_rows(TView{{col1->view(), col2->view()}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
TEST_F(ListConcatenateRowsTest, SimpleInputStringsColumnsWithNullableChild)
{
auto const col1 = StrListsCol{
StrListsCol{"Tomato", "Apple"},
StrListsCol{"Banana", "Kiwi", "Cherry"},
StrListsCol{
"Coconut"}}.release();
auto const col2 = StrListsCol{
StrListsCol{"Orange"},
StrListsCol{{"Lemon", "Peach"}, null_at(1)},
StrListsCol{}}.release();
auto const expected = StrListsCol{
StrListsCol{"Tomato", "Apple", "Orange"},
StrListsCol{{"Banana", "Kiwi", "Cherry", "Lemon", "Peach"}, null_at(4)},
StrListsCol{
"Coconut"}}.release();
auto const results = cudf::lists::concatenate_rows(TView{{col1->view(), col2->view()}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
TYPED_TEST(ListConcatenateRowsTypedTest, SimpleInputWithNulls)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col1 = ListsCol{{ListsCol{{1, null, 3, 4}, null_at(1)},
ListsCol{{null, 2, 3, 4}, null_at(0)},
ListsCol{{null, 2, 3, 4}, null_at(0)},
ListsCol{} /*NULL*/,
ListsCol{{1, 2, null, 4}, null_at(2)},
ListsCol{{1, 2, 3, null}, null_at(3)},
ListsCol{} /*NULL*/},
nulls_at({3, 6})}
.release();
auto const col2 = ListsCol{{ListsCol{{10, 11, 12, null}, null_at(3)},
ListsCol{{13, 14, 15, 16, 17, null}, null_at(5)},
ListsCol{} /*NULL*/,
ListsCol{{null, 18}, null_at(0)},
ListsCol{{19, 20, null}, null_at(2)},
ListsCol{{null}, null_at(0)},
ListsCol{} /*NULL*/},
nulls_at({2, 6})}
.release();
auto const col3 = ListsCol{{ListsCol{} /*NULL*/,
ListsCol{{20, null}, null_at(1)},
ListsCol{{null, 21, null, null}, nulls_at({0, 2, 3})},
ListsCol{},
ListsCol{22, 23, 24, 25},
ListsCol{{null, null, null, null, null}, all_nulls()},
ListsCol{} /*NULL*/},
nulls_at({0, 6})}
.release();
// Ignore null list elements
{
auto const results =
cudf::lists::concatenate_rows(TView{{col1->view(), col2->view(), col3->view()}});
auto const expected =
ListsCol{{ListsCol{{1, null, 3, 4, 10, 11, 12, null}, nulls_at({1, 7})},
ListsCol{{null, 2, 3, 4, 13, 14, 15, 16, 17, null, 20, null}, nulls_at({0, 9, 11})},
ListsCol{{null, 2, 3, 4, null, 21, null, null}, nulls_at({0, 4, 6, 7})},
ListsCol{{null, 18}, null_at(0)},
ListsCol{{1, 2, null, 4, 19, 20, null, 22, 23, 24, 25}, nulls_at({2, 6})},
ListsCol{{1, 2, 3, null, null, null, null, null, null, null},
nulls_at({3, 4, 5, 6, 7, 8, 9})},
ListsCol{} /*NULL*/},
null_at(6)}
.release();
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
// Null list rows result in null list rows
{
auto const results =
cudf::lists::concatenate_rows(TView{{col1->view(), col2->view(), col3->view()}},
cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected =
ListsCol{{ListsCol{} /*NULL*/,
ListsCol{{null, 2, 3, 4, 13, 14, 15, 16, 17, null, 20, null}, nulls_at({0, 9, 11})},
ListsCol{} /*NULL*/,
ListsCol{} /*NULL*/,
ListsCol{{1, 2, null, 4, 19, 20, null, 22, 23, 24, 25}, nulls_at({2, 6})},
ListsCol{{1, 2, 3, null, null, null, null, null, null, null},
nulls_at({3, 4, 5, 6, 7, 8, 9})},
ListsCol{} /*NULL*/},
nulls_at({0, 2, 3, 6})}
.release();
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
}
TEST_F(ListConcatenateRowsTest, SimpleInputStringsColumnsWithNulls)
{
auto const col1 = StrListsCol{
StrListsCol{{"Tomato", "Bear" /*NULL*/, "Apple"}, null_at(1)},
StrListsCol{{"Banana", "Pig" /*NULL*/, "Kiwi", "Cherry", "Whale" /*NULL*/}, nulls_at({1, 4})},
StrListsCol{
"Coconut"}}.release();
auto const col2 =
StrListsCol{
{StrListsCol{{"Orange", "Dog" /*NULL*/, "Fox" /*NULL*/, "Duck" /*NULL*/},
nulls_at({1, 2, 3})},
StrListsCol{"Lemon", "Peach"},
StrListsCol{{"Deer" /*NULL*/, "Snake" /*NULL*/, "Horse" /*NULL*/}, all_nulls()}}, /*NULL*/
null_at(2)}
.release();
// Ignore null list elements
{
auto const results = cudf::lists::concatenate_rows(TView{{col1->view(), col2->view()}});
auto const expected = StrListsCol{
StrListsCol{{"Tomato", "" /*NULL*/, "Apple", "Orange", "" /*NULL*/, "" /*NULL*/, "" /*NULL*/},
nulls_at({1, 4, 5, 6})},
StrListsCol{{"Banana", "" /*NULL*/, "Kiwi", "Cherry", "" /*NULL*/, "Lemon", "Peach"},
nulls_at({1, 4})},
StrListsCol{
"Coconut"}}.release();
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
// Null list rows result in null list rows
{
auto const results =
cudf::lists::concatenate_rows(TView{{col1->view(), col2->view()}},
cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected =
StrListsCol{
{StrListsCol{
{"Tomato", "" /*NULL*/, "Apple", "Orange", "" /*NULL*/, "" /*NULL*/, "" /*NULL*/},
nulls_at({1, 4, 5, 6})},
StrListsCol{{"Banana", "" /*NULL*/, "Kiwi", "Cherry", "" /*NULL*/, "Lemon", "Peach"},
nulls_at({1, 4})},
StrListsCol{""} /*NULL*/},
null_at(2)}
.release();
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
}
TEST_F(ListConcatenateRowsTest, SimpleInputStringsColumnsWithEmptyLists)
{
auto const col1 =
StrListsCol{StrListsCol{{"" /*NULL*/}, null_at(0)}, StrListsCol{"One"}}.release();
auto const col2 = StrListsCol{
StrListsCol{{"Tomato", "" /*NULL*/, "Apple"}, null_at(1)},
StrListsCol{
"Two"}}.release();
auto const col3 =
StrListsCol{{StrListsCol{"Lemon", "Peach"}, StrListsCol{"Three"} /*NULL*/}, null_at(1)}
.release();
// Ignore null list elements
{
auto const results =
cudf::lists::concatenate_rows(TView{{col1->view(), col2->view(), col3->view()}});
auto const expected = StrListsCol{
StrListsCol{{"" /*NULL*/, "Tomato", "" /*NULL*/, "Apple", "Lemon", "Peach"},
nulls_at({0, 2})},
StrListsCol{"One",
"Two"}}.release();
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
// Null list rows result in null list rows
{
auto const results =
cudf::lists::concatenate_rows(TView{{col1->view(), col2->view(), col3->view()}},
cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected =
StrListsCol{{StrListsCol{{"" /*NULL*/, "Tomato", "" /*NULL*/, "Apple", "Lemon", "Peach"},
nulls_at({0, 2})},
StrListsCol{""} /*NULL*/},
null_at(1)}
.release();
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
}
TYPED_TEST(ListConcatenateRowsTypedTest, SlicedColumnsInputNoNull)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col_original = ListsCol{{1, 2, 3}, {2, 3}, {3, 4, 5, 6}, {5, 6}, {}, {7}}.release();
auto const col1 = cudf::slice(col_original->view(), {0, 3})[0];
auto const col2 = cudf::slice(col_original->view(), {1, 4})[0];
auto const col3 = cudf::slice(col_original->view(), {2, 5})[0];
auto const col4 = cudf::slice(col_original->view(), {3, 6})[0];
auto const expected = ListsCol{
{1, 2, 3, 2, 3, 3, 4, 5, 6, 5, 6},
{2, 3, 3, 4, 5, 6, 5, 6},
{3, 4, 5, 6, 5, 6, 7}}.release();
auto const results = cudf::lists::concatenate_rows(TView{{col1, col2, col3, col4}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
TYPED_TEST(ListConcatenateRowsTypedTest, SlicedColumnsInputWithNulls)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col_original = ListsCol{{ListsCol{{null, 2, 3}, null_at(0)},
ListsCol{2, 3}, /*NULL*/
ListsCol{{3, null, 5, 6}, null_at(1)},
ListsCol{5, 6}, /*NULL*/
ListsCol{}, /*NULL*/
ListsCol{7},
ListsCol{8, 9, 10}},
nulls_at({1, 3, 4})}
.release();
auto const col1 = cudf::slice(col_original->view(), {0, 3})[0];
auto const col2 = cudf::slice(col_original->view(), {1, 4})[0];
auto const col3 = cudf::slice(col_original->view(), {2, 5})[0];
auto const col4 = cudf::slice(col_original->view(), {3, 6})[0];
auto const col5 = cudf::slice(col_original->view(), {4, 7})[0];
auto const expected = ListsCol{
ListsCol{{null, 2, 3, 3, null, 5, 6}, nulls_at({0, 4})},
ListsCol{{3, null, 5, 6, 7}, null_at(1)},
ListsCol{{3, null, 5, 6, 7, 8, 9, 10},
null_at(1)}}.release();
auto const results = cudf::lists::concatenate_rows(TView{{col1, col2, col3, col4, col5}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
TEST_F(ListConcatenateRowsTest, SlicedStringsColumnsInputWithNulls)
{
auto const col =
StrListsCol{
{StrListsCol{{"Tomato", "Bear" /*NULL*/, "Apple"}, null_at(1)},
StrListsCol{{"Banana", "Pig" /*NULL*/, "Kiwi", "Cherry", "Whale" /*NULL*/},
nulls_at({1, 4})},
StrListsCol{"Coconut"},
StrListsCol{{"Orange", "Dog" /*NULL*/, "Fox" /*NULL*/, "Duck" /*NULL*/},
nulls_at({1, 2, 3})},
StrListsCol{"Lemon", "Peach"},
StrListsCol{{"Deer" /*NULL*/, "Snake" /*NULL*/, "Horse" /*NULL*/}, all_nulls()}}, /*NULL*/
null_at(5)}
.release();
auto const col1 = cudf::slice(col->view(), {0, 3})[0];
auto const col2 = cudf::slice(col->view(), {1, 4})[0];
auto const col3 = cudf::slice(col->view(), {2, 5})[0];
auto const col4 = cudf::slice(col->view(), {3, 6})[0];
{
auto const results = cudf::lists::concatenate_rows(TView{{col1, col2, col3, col4}});
auto const expected = StrListsCol{StrListsCol{{"Tomato",
"" /*NULL*/,
"Apple",
"Banana",
"" /*NULL*/,
"Kiwi",
"Cherry",
"" /*NULL*/,
"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"" /*NULL*/},
nulls_at({1, 4, 7, 10, 11, 12})},
StrListsCol{{"Banana",
"" /*NULL*/,
"Kiwi",
"Cherry",
"" /*NULL*/,
"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"", /*NULL*/
"Lemon",
"Peach"},
nulls_at({1, 4, 7, 8, 9})},
StrListsCol{{
"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"", /*NULL*/
"Lemon",
"Peach",
},
nulls_at({2, 3, 4})}}
.release();
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
{
auto const results = cudf::lists::concatenate_rows(
TView{{col1, col2, col3, col4}}, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected = StrListsCol{{StrListsCol{{"Tomato",
"" /*NULL*/,
"Apple",
"Banana",
"" /*NULL*/,
"Kiwi",
"Cherry",
"" /*NULL*/,
"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"" /*NULL*/},
nulls_at({1, 4, 7, 10, 11, 12})},
StrListsCol{{"Banana",
"" /*NULL*/,
"Kiwi",
"Cherry",
"" /*NULL*/,
"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"", /*NULL*/
"Lemon",
"Peach"},
nulls_at({1, 4, 7, 8, 9})},
StrListsCol{} /*NULL*/},
null_at(2)}
.release();
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
}
TEST_F(ListConcatenateRowsTest, StringsColumnsWithEmptyListTest)
{
auto const col1 = StrListsCol{{"1", "2", "3", "4"}}.release();
auto const col2 = StrListsCol{{"a", "b", "c"}}.release();
auto const col3 = StrListsCol{StrListsCol{}}.release();
auto const col4 = StrListsCol{{"x", "y", "" /*NULL*/, "z"}, null_at(2)}.release();
auto const col5 = StrListsCol{{StrListsCol{}}, null_at(0)}.release();
auto const expected =
StrListsCol{{"1", "2", "3", "4", "a", "b", "c", "x", "y", "" /*NULL*/, "z"}, null_at(9)}
.release();
auto const results = cudf::lists::concatenate_rows(
TView{{col1->view(), col2->view(), col3->view(), col4->view(), col5->view()}});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *results, verbosity);
}
struct ListConcatenateRowsNestedTypesTest : public cudf::test::BaseFixture {};
TEST_F(ListConcatenateRowsNestedTypesTest, Identity)
{
// list<list<string>>
// col 0
cudf::test::lists_column_wrapper<cudf::string_view> l0{
{{{{"whee", "yay", "bananas"}, nulls_at({1})}, {}},
{{}},
{{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"}}, nulls_at({0, 2})},
{{"f", "tesla"}},
{{"phone"}, {"hack", "table", "car"}}},
nulls_at({3, 4})};
// perform the concatenate
cudf::table_view t({l0});
auto result = cudf::lists::concatenate_rows(t);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, l0);
}
TEST_F(ListConcatenateRowsNestedTypesTest, List)
{
// list<list<string>>
// col 0
cudf::test::lists_column_wrapper<cudf::string_view> l0{
{{"whee", "yay", "bananas"}, {}},
{{}},
{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"}},
{{"f", "tesla"}},
{{"phone"}, {"hack", "table", "car"}}};
// col1
cudf::test::lists_column_wrapper<cudf::string_view> l1{
{{}},
{{"arg"}, {"mno", "ampere"}, {"gpu"}, {"def"}},
{{"", "hhh"}},
{{"warp", "donuts", "parking"}, {"", "apply", "twelve", "mouse", "bbb"}, {"bbb", "pom"}, {}},
{{}}};
// perform the concatenate
cudf::table_view t({l0, l1});
auto result = cudf::lists::concatenate_rows(t);
// expected
cudf::test::lists_column_wrapper<cudf::string_view> expected{
{{"whee", "yay", "bananas"}, {}, {}},
{{}, {"arg"}, {"mno", "ampere"}, {"gpu"}, {"def"}},
{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"}, {"", "hhh"}},
{{"f", "tesla"},
{"warp", "donuts", "parking"},
{"", "apply", "twelve", "mouse", "bbb"},
{"bbb", "pom"},
{}},
{{"phone"}, {"hack", "table", "car"}, {}}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected);
}
TEST_F(ListConcatenateRowsNestedTypesTest, ListWithNulls)
{
// list<list<string>>
// clang-format off
// col 0
cudf::test::lists_column_wrapper<cudf::string_view>
l0{ {
{{{"whee", "yay", "bananas"}, nulls_at({1})}, {}},
{{}},
{{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"}}, nulls_at({0, 2})},
{{"f", "tesla"}},
{{"phone"}, {"hack", "table", "car"}}
}, nulls_at({3, 4}) };
// col1
cudf::test::lists_column_wrapper<cudf::string_view>
l1{ {
{{}},
{{"arg"}, {"mno", "ampere"}, {"gpu"}, {"def"}},
{{{{"", "hhh"}, nulls_at({0})}, {"www"}}, nulls_at({1})},
{{"warp", "donuts", "parking"}, { "", "apply", "twelve", "mouse", "bbb"}, {"bbb", "pom"}, {}},
{{}}
}, nulls_at({4}) };
// col2
cudf::test::lists_column_wrapper<cudf::string_view>
l2{ {
{{"monitor", "sugar"}},
{{"spurs", "garlic"}, {"onion", "shallot", "carrot"}},
{{"cars", "trucks", "planes"}, {"abc"}, {"mno", "pqr"}},
{{}, {"ram", "cpu", "disk"}, {}},
{{"round"}, {"square"}}
}, nulls_at({0, 4}) };
// concatenate_policy::IGNORE_NULLS
{
// perform the concatenate
cudf::table_view t({l0, l1, l2});
auto result = cudf::lists::concatenate_rows(t, cudf::lists::concatenate_null_policy::IGNORE);
// expected
cudf::test::lists_column_wrapper<cudf::string_view>
expected{ {
{{{"whee", "yay", "bananas"}, nulls_at({1})}, {}, {}},
{{}, {"arg"}, {"mno", "ampere"}, {"gpu"}, {"def"}, {"spurs", "garlic"}, {"onion", "shallot", "carrot"}},
{{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"},
{{"", "hhh"}, nulls_at({0})}, {"www"}, {"cars", "trucks", "planes"}, {"abc"}, {"mno", "pqr"}},
nulls_at({0, 2, 4}) },
{{"warp", "donuts", "parking"}, { "", "apply", "twelve", "mouse", "bbb"}, {"bbb", "pom"}, {}, {}, {"ram", "cpu", "disk"}, {}},
{{}}
}, nulls_at({4}) };
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected);
}
// concatenate_policy::NULLIFY_OUTPUT_ROW
{
// perform the concatenate
cudf::table_view t({l0, l1, l2});
auto result = cudf::lists::concatenate_rows(t, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
// expected
cudf::test::lists_column_wrapper<cudf::string_view>
expected{ {
{{}},
{{}, {"arg"}, {"mno", "ampere"}, {"gpu"}, {"def"}, {"spurs", "garlic"}, {"onion", "shallot", "carrot"}},
{{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"},
{{"", "hhh"}, nulls_at({0})}, {"www"}, {"cars", "trucks", "planes"}, {"abc"}, {"mno", "pqr"}},
nulls_at({0, 2, 4}) },
{{}},
{{}}
}, nulls_at({0, 3, 4}) };
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected);
}
// clang-format on
}
TEST_F(ListConcatenateRowsNestedTypesTest, ListWithNullsSliced)
{
// list<list<string>>
// clang-format off
// col 0
cudf::test::lists_column_wrapper<cudf::string_view>
unsliced_l0{ {
{{{"whee", "yay", "bananas"}, nulls_at({1})}, {}},
{{}},
{{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"}}, nulls_at({0, 2})},
{{"f", "tesla"}},
{{"phone"}, {"hack", "table", "car"}}
}, nulls_at({3, 4}) };
auto l0 = cudf::split(unsliced_l0, {2})[1];
// col1
cudf::test::lists_column_wrapper<cudf::string_view>
unsliced_l1{ {
{{}},
{{"arg"}, {"mno", "ampere"}, {"gpu"}, {"def"}},
{{{{"", "hhh"}, nulls_at({0})}, {"www"}}, nulls_at({1})},
{{"warp", "donuts", "parking"}, { "", "apply", "twelve", "mouse", "bbb"}, {"bbb", "pom"}, {}},
{{}}
}, nulls_at({4}) };
auto l1 = cudf::split(unsliced_l1, {2})[1];
// concatenate_policy::IGNORE_NULLS
{
// perform the concatenate
cudf::table_view t({l0, l1});
auto result = cudf::lists::concatenate_rows(t, cudf::lists::concatenate_null_policy::IGNORE);
// expected
cudf::test::lists_column_wrapper<cudf::string_view>
expected{ { {{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"},
{{"", "hhh"}, nulls_at({0})}, {"www"}}, nulls_at({0, 2, 4}) },
{{"warp", "donuts", "parking"}, { "", "apply", "twelve", "mouse", "bbb"}, {"bbb", "pom"}, {}},
{{}}
}, nulls_at({2}) };
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected);
}
// concatenate_policy::NULLIFY_OUTPUT_ROW
{
// perform the concatenate
cudf::table_view t({l0, l1});
auto result = cudf::lists::concatenate_rows(t, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
// expected
cudf::test::lists_column_wrapper<cudf::string_view>
expected{ { {{{"abc"}, {"def", "g", "xyw", "ijk"}, {"x", "y", "", "column"},
{{"", "hhh"}, nulls_at({0})}, {"www"}}, nulls_at({0, 2, 4}) },
{{}},
{{}}
}, nulls_at({1, 2}) };
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected);
}
// clang-format on
}
TEST_F(ListConcatenateRowsNestedTypesTest, Struct)
{
// list<struct<int, string>>
// col 0
cudf::test::fixed_width_column_wrapper<int> s0_0{0, 1, 2, 3, 4, 5, 6, 7};
cudf::test::strings_column_wrapper s0_1{
"whee", "yay", "bananas", "abc", "def", "g", "xyw", "ijk"};
std::vector<std::unique_ptr<cudf::column>> s0_children;
s0_children.push_back(s0_0.release());
s0_children.push_back(s0_1.release());
cudf::test::structs_column_wrapper s0(std::move(s0_children));
cudf::test::fixed_width_column_wrapper<int> l0_offsets{0, 2, 2, 5, 6, 8};
auto const l0_size = static_cast<cudf::column_view>(l0_offsets).size() - 1;
auto l0 = cudf::make_lists_column(l0_size, l0_offsets.release(), s0.release(), 0, {});
// col1
cudf::test::fixed_width_column_wrapper<int> s1_0{
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24};
cudf::test::strings_column_wrapper s1_1{"arg",
"mno",
"ampere",
"gpu",
"",
"hhh",
"warp",
"donuts",
"parking",
"",
"apply",
"twelve",
"mouse",
"bbb",
"pom"};
std::vector<std::unique_ptr<cudf::column>> s1_children;
s1_children.push_back(s1_0.release());
s1_children.push_back(s1_1.release());
cudf::test::structs_column_wrapper s1(std::move(s1_children));
cudf::test::fixed_width_column_wrapper<int> l1_offsets{0, 0, 4, 7, 15, 15};
auto const l1_size = static_cast<cudf::column_view>(l1_offsets).size() - 1;
auto l1 = cudf::make_lists_column(l1_size, l1_offsets.release(), s1.release(), 0, {});
// perform the concatenate
cudf::table_view t({*l0, *l1});
auto result = cudf::lists::concatenate_rows(t);
// expected
cudf::test::fixed_width_column_wrapper<int> se_0{0, 1, 10, 11, 12, 13, 2, 3, 4, 14, 15, 16,
5, 17, 18, 19, 20, 21, 22, 23, 24, 6, 7};
cudf::test::strings_column_wrapper se_1{"whee", "yay", "arg", "mno", "ampere", "gpu",
"bananas", "abc", "def", "", "hhh", "warp",
"g", "donuts", "parking", "", "apply", "twelve",
"mouse", "bbb", "pom", "xyw", "ijk"};
std::vector<std::unique_ptr<cudf::column>> se_children;
se_children.push_back(se_0.release());
se_children.push_back(se_1.release());
cudf::test::structs_column_wrapper se(std::move(se_children));
cudf::test::fixed_width_column_wrapper<int> le_offsets{0, 2, 6, 12, 21, 23};
auto const le_size = static_cast<cudf::column_view>(le_offsets).size() - 1;
auto expected = cudf::make_lists_column(le_size, le_offsets.release(), se.release(), 0, {});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
TEST_F(ListConcatenateRowsNestedTypesTest, StructWithNulls)
{
// list<struct<int, string>>
// col 0
cudf::test::fixed_width_column_wrapper<int> s0_0{0, 1, 2, 3, 4, 5, 6, 7};
cudf::test::strings_column_wrapper s0_1{
{"whee", "yay", "bananas", "abc", "def", "g", "xyw", "ijk"}, nulls_at({1, 3, 4})};
std::vector<std::unique_ptr<cudf::column>> s0_children;
s0_children.push_back(s0_0.release());
s0_children.push_back(s0_1.release());
cudf::test::structs_column_wrapper s0(std::move(s0_children));
cudf::test::fixed_width_column_wrapper<int> l0_offsets{0, 2, 2, 5, 6, 8};
auto const l0_size = static_cast<cudf::column_view>(l0_offsets).size() - 1;
std::vector<bool> l0_validity{false, true, true, false, true};
auto [null_mask, null_count] =
cudf::test::detail::make_null_mask(l0_validity.begin(), l0_validity.end());
auto l0 = cudf::make_lists_column(
l0_size, l0_offsets.release(), s0.release(), null_count, std::move(null_mask));
// col1
cudf::test::fixed_width_column_wrapper<int> s1_0{
{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, nulls_at({14})};
cudf::test::strings_column_wrapper s1_1{"arg",
"mno",
"ampere",
"gpu",
"",
"hhh",
"warp",
"donuts",
"parking",
"",
"apply",
"twelve",
"mouse",
"bbb",
"pom"};
std::vector<std::unique_ptr<cudf::column>> s1_children;
s1_children.push_back(s1_0.release());
s1_children.push_back(s1_1.release());
cudf::test::structs_column_wrapper s1(std::move(s1_children));
cudf::test::fixed_width_column_wrapper<int> l1_offsets{0, 0, 4, 7, 15, 15};
auto const l1_size = static_cast<cudf::column_view>(l1_offsets).size() - 1;
std::vector<bool> l1_validity{false, true, true, true, true};
std::tie(null_mask, null_count) =
cudf::test::detail::make_null_mask(l1_validity.begin(), l1_validity.end());
auto l1 = cudf::make_lists_column(
l1_size, l1_offsets.release(), s1.release(), null_count, std::move(null_mask));
// concatenate_policy::IGNORE_NULLS
{
// perform the concatenate
cudf::table_view t({*l0, *l1});
auto result = cudf::lists::concatenate_rows(t, cudf::lists::concatenate_null_policy::IGNORE);
// expected
cudf::test::fixed_width_column_wrapper<int> se_0{
{10, 11, 12, 13, 2, 3, 4, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 6, 7}, nulls_at({17})};
cudf::test::strings_column_wrapper se_1{
{"arg", "mno", "ampere", "gpu", "bananas", "", "", "", "hhh", "warp",
"donuts", "parking", "", "apply", "twelve", "mouse", "bbb", "pom", "xyw", "ijk"},
nulls_at({5, 6})};
std::vector<std::unique_ptr<cudf::column>> se_children;
se_children.push_back(se_0.release());
se_children.push_back(se_1.release());
cudf::test::structs_column_wrapper se(std::move(se_children));
cudf::test::fixed_width_column_wrapper<int> le_offsets{0, 0, 4, 10, 18, 20};
auto const le_size = static_cast<cudf::column_view>(le_offsets).size() - 1;
std::vector<bool> le_validity{false, true, true, true, true};
std::tie(null_mask, null_count) =
cudf::test::detail::make_null_mask(le_validity.begin(), le_validity.end());
auto expected = cudf::make_lists_column(
le_size, le_offsets.release(), se.release(), null_count, std::move(null_mask));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
// concatenate_policy::NULLIFY_OUTPUT_ROW
{
// perform the concatenate
cudf::table_view t({*l0, *l1});
auto result =
cudf::lists::concatenate_rows(t, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
// expected
cudf::test::fixed_width_column_wrapper<int> se_0{{10, 11, 12, 13, 2, 3, 4, 14, 15, 16, 6, 7},
nulls_at({})};
cudf::test::strings_column_wrapper se_1{
{"arg", "mno", "ampere", "gpu", "bananas", "", "", "", "hhh", "warp", "xyw", "ijk"},
nulls_at({5, 6})};
std::vector<std::unique_ptr<cudf::column>> se_children;
se_children.push_back(se_0.release());
se_children.push_back(se_1.release());
cudf::test::structs_column_wrapper se(std::move(se_children));
cudf::test::fixed_width_column_wrapper<int> le_offsets{0, 0, 4, 10, 10, 12};
auto const le_size = static_cast<cudf::column_view>(le_offsets).size() - 1;
std::vector<bool> le_validity{false, true, true, false, true};
std::tie(null_mask, null_count) =
cudf::test::detail::make_null_mask(le_validity.begin(), le_validity.end());
auto expected = cudf::make_lists_column(
le_size, le_offsets.release(), se.release(), null_count, std::move(null_mask));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
}
TEST_F(ListConcatenateRowsNestedTypesTest, StructWithNullsSliced)
{
// list<struct<int, string>>
// col 0
cudf::test::fixed_width_column_wrapper<int> s0_0{0, 1, 2, 3, 4, 5, 6, 7};
cudf::test::strings_column_wrapper s0_1{
{"whee", "yay", "bananas", "abc", "def", "g", "xyw", "ijk"}, nulls_at({1, 3, 4})};
std::vector<std::unique_ptr<cudf::column>> s0_children;
s0_children.push_back(s0_0.release());
s0_children.push_back(s0_1.release());
cudf::test::structs_column_wrapper s0(std::move(s0_children));
cudf::test::fixed_width_column_wrapper<int> l0_offsets{0, 2, 2, 5, 6, 8};
auto const l0_size = static_cast<cudf::column_view>(l0_offsets).size() - 1;
std::vector<bool> l0_validity{false, true, false, false, true};
auto [null_mask, null_count] =
cudf::test::detail::make_null_mask(l0_validity.begin(), l0_validity.end());
auto l0_unsliced = cudf::make_lists_column(
l0_size, l0_offsets.release(), s0.release(), null_count, std::move(null_mask));
auto l0 = cudf::split(*l0_unsliced, {2})[1];
// col1
cudf::test::fixed_width_column_wrapper<int> s1_0{
{10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24}, nulls_at({14})};
cudf::test::strings_column_wrapper s1_1{"arg",
"mno",
"ampere",
"gpu",
"",
"hhh",
"warp",
"donuts",
"parking",
"",
"apply",
"twelve",
"mouse",
"bbb",
"pom"};
std::vector<std::unique_ptr<cudf::column>> s1_children;
s1_children.push_back(s1_0.release());
s1_children.push_back(s1_1.release());
cudf::test::structs_column_wrapper s1(std::move(s1_children));
cudf::test::fixed_width_column_wrapper<int> l1_offsets{0, 0, 4, 7, 15, 15};
auto const l1_size = static_cast<cudf::column_view>(l1_offsets).size() - 1;
std::vector<bool> l1_validity{false, true, false, true, true};
std::tie(null_mask, null_count) =
cudf::test::detail::make_null_mask(l1_validity.begin(), l1_validity.end());
auto l1_unsliced = cudf::make_lists_column(
l1_size, l1_offsets.release(), s1.release(), null_count, std::move(null_mask));
auto l1 = cudf::split(*l1_unsliced, {2})[1];
// concatenate_policy::IGNORE_NULLS
{
// perform the concatenate
cudf::table_view t({l0, l1});
auto result = cudf::lists::concatenate_rows(t, cudf::lists::concatenate_null_policy::IGNORE);
// expected
cudf::test::fixed_width_column_wrapper<int> se_0{{17, 18, 19, 20, 21, 22, 23, 24, 6, 7},
nulls_at({7})};
cudf::test::strings_column_wrapper se_1{
{"donuts", "parking", "", "apply", "twelve", "mouse", "bbb", "pom", "xyw", "ijk"}};
std::vector<std::unique_ptr<cudf::column>> se_children;
se_children.push_back(se_0.release());
se_children.push_back(se_1.release());
cudf::test::structs_column_wrapper se(std::move(se_children));
cudf::test::fixed_width_column_wrapper<int> le_offsets{0, 0, 8, 10};
auto const le_size = static_cast<cudf::column_view>(le_offsets).size() - 1;
std::vector<bool> le_validity{false, true, true};
std::tie(null_mask, null_count) =
cudf::test::detail::make_null_mask(le_validity.begin(), le_validity.end());
auto expected = cudf::make_lists_column(
le_size, le_offsets.release(), se.release(), null_count, std::move(null_mask));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
// concatenate_policy::NULLIFY_OUTPUT_ROW
{
// perform the concatenate
cudf::table_view t({l0, l1});
auto result =
cudf::lists::concatenate_rows(t, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
// expected
cudf::test::fixed_width_column_wrapper<int> se_0{{6, 7}, nulls_at({})};
cudf::test::strings_column_wrapper se_1{"xyw", "ijk"};
std::vector<std::unique_ptr<cudf::column>> se_children;
se_children.push_back(se_0.release());
se_children.push_back(se_1.release());
cudf::test::structs_column_wrapper se(std::move(se_children));
cudf::test::fixed_width_column_wrapper<int> le_offsets{0, 0, 0, 2};
auto const le_size = static_cast<cudf::column_view>(le_offsets).size() - 1;
std::vector<bool> le_validity{false, false, true};
std::tie(null_mask, null_count) =
cudf::test::detail::make_null_mask(le_validity.begin(), le_validity.end());
auto expected = cudf::make_lists_column(
le_size, le_offsets.release(), se.release(), null_count, std::move(null_mask));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests/lists
|
rapidsai_public_repos/cudf/cpp/tests/lists/combine/concatenate_list_elements_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/lists/combine.hpp>
#include <stdexcept>
using namespace cudf::test::iterators;
namespace {
using StrListsCol = cudf::test::lists_column_wrapper<cudf::string_view>;
using IntListsCol = cudf::test::lists_column_wrapper<int32_t>;
using IntCol = cudf::test::fixed_width_column_wrapper<int32_t>;
constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::FIRST_ERROR};
constexpr int32_t null{0};
template <class T, class... Ts>
auto build_lists_col(T& list, Ts&... lists)
{
return T(std::initializer_list<T>{std::move(list), std::move(lists)...});
}
} // namespace
struct ConcatenateListElementsTest : public cudf::test::BaseFixture {};
TEST_F(ConcatenateListElementsTest, InvalidInput)
{
// Input lists is not a 2-level depth lists column.
{
auto const col = IntCol{};
EXPECT_THROW(cudf::lists::concatenate_list_elements(col), std::invalid_argument);
}
// Input lists is not at least 2-level depth lists column.
{
auto const col = IntListsCol{1, 2, 3};
EXPECT_THROW(cudf::lists::concatenate_list_elements(col), std::invalid_argument);
}
}
template <typename T>
struct ConcatenateListElementsTypedTest : public cudf::test::BaseFixture {};
using TypesForTest = cudf::test::Concat<cudf::test::IntegralTypesNotBool,
cudf::test::FloatingPointTypes,
cudf::test::FixedPointTypes>;
TYPED_TEST_SUITE(ConcatenateListElementsTypedTest, TypesForTest);
TYPED_TEST(ConcatenateListElementsTypedTest, SimpleInputNoNull)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto row0 = ListsCol{{1, 2}, {3}, {4, 5, 6}};
auto row1 = ListsCol{ListsCol{}};
auto row2 = ListsCol{{7, 8}, {9, 10}};
auto const col = build_lists_col(row0, row1, row2);
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = ListsCol{{1, 2, 3, 4, 5, 6}, {}, {7, 8, 9, 10}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
TYPED_TEST(ConcatenateListElementsTypedTest, SimpleInputNestedManyLevelsNoNull)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto row00 = ListsCol{{1, 2}, {3}, {4, 5, 6}};
auto row01 = ListsCol{ListsCol{}};
auto row02 = ListsCol{{7, 8}, {9, 10}};
auto row0 = build_lists_col(row00, row01, row02);
auto row10 = ListsCol{{1, 2}, {3}, {4, 5, 6}};
auto row11 = ListsCol{ListsCol{}};
auto row12 = ListsCol{{7, 8}, {9, 10}};
auto row1 = build_lists_col(row10, row11, row12);
auto row20 = ListsCol{{1, 2}, {3}, {4, 5, 6}};
auto row21 = ListsCol{ListsCol{}};
auto row22 = ListsCol{{7, 8}, {9, 10}};
auto row2 = build_lists_col(row20, row21, row22);
auto const col = build_lists_col(row0, row1, row2);
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = ListsCol{ListsCol{{1, 2}, {3}, {4, 5, 6}, {}, {7, 8}, {9, 10}},
ListsCol{{1, 2}, {3}, {4, 5, 6}, {}, {7, 8}, {9, 10}},
ListsCol{{1, 2}, {3}, {4, 5, 6}, {}, {7, 8}, {9, 10}}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
TEST_F(ConcatenateListElementsTest, SimpleInputStringsColumnNoNull)
{
auto row0 = StrListsCol{StrListsCol{"Tomato", "Apple"}, StrListsCol{"Orange"}};
auto row1 = StrListsCol{StrListsCol{"Banana", "Kiwi", "Cherry"}, StrListsCol{"Lemon", "Peach"}};
auto row2 = StrListsCol{StrListsCol{"Coconut"}, StrListsCol{}};
auto const col = build_lists_col(row0, row1, row2);
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = StrListsCol{StrListsCol{"Tomato", "Apple", "Orange"},
StrListsCol{"Banana", "Kiwi", "Cherry", "Lemon", "Peach"},
StrListsCol{"Coconut"}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
TYPED_TEST(ConcatenateListElementsTypedTest, SimpleInputWithNulls)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto row0 = ListsCol{{ListsCol{{1, null, 3, 4}, null_at(1)},
ListsCol{{10, 11, 12, null}, null_at(3)},
ListsCol{} /*NULL*/},
null_at(2)};
auto row1 = ListsCol{ListsCol{{null, 2, 3, 4}, null_at(0)},
ListsCol{{13, 14, 15, 16, 17, null}, null_at(5)},
ListsCol{{20, null}, null_at(1)}};
auto row2 = ListsCol{{ListsCol{{null, 2, 3, 4}, null_at(0)},
ListsCol{} /*NULL*/,
ListsCol{{null, 21, null, null}, nulls_at({0, 2, 3})}},
null_at(1)};
auto row3 = ListsCol{{ListsCol{} /*NULL*/, ListsCol{{null, 18}, null_at(0)}}, null_at(0)};
auto row4 = ListsCol{ListsCol{{1, 2, null, 4}, null_at(2)},
ListsCol{{19, 20, null}, null_at(2)},
ListsCol{22, 23, 24, 25}};
auto row5 = ListsCol{ListsCol{{1, 2, 3, null}, null_at(3)},
ListsCol{{null}, null_at(0)},
ListsCol{{null, null, null, null, null}, all_nulls()}};
auto row6 =
ListsCol{{ListsCol{} /*NULL*/, ListsCol{} /*NULL*/, ListsCol{} /*NULL*/}, all_nulls()};
auto const col = build_lists_col(row0, row1, row2, row3, row4, row5, row6);
// Ignore null list elements.
{
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected =
ListsCol{{ListsCol{{1, null, 3, 4, 10, 11, 12, null}, nulls_at({1, 7})},
ListsCol{{null, 2, 3, 4, 13, 14, 15, 16, 17, null, 20, null}, nulls_at({0, 9, 11})},
ListsCol{{null, 2, 3, 4, null, 21, null, null}, nulls_at({0, 4, 6, 7})},
ListsCol{{null, 18}, null_at(0)},
ListsCol{{1, 2, null, 4, 19, 20, null, 22, 23, 24, 25}, nulls_at({2, 6})},
ListsCol{{1, 2, 3, null, null, null, null, null, null, null},
nulls_at({3, 4, 5, 6, 7, 8, 9})},
ListsCol{} /*NULL*/},
null_at(6)};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
// Null lists result in null rows.
{
auto const results = cudf::lists::concatenate_list_elements(
col, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected =
ListsCol{{ListsCol{} /*NULL*/,
ListsCol{{null, 2, 3, 4, 13, 14, 15, 16, 17, null, 20, null}, nulls_at({0, 9, 11})},
ListsCol{} /*NULL*/,
ListsCol{} /*NULL*/,
ListsCol{{1, 2, null, 4, 19, 20, null, 22, 23, 24, 25}, nulls_at({2, 6})},
ListsCol{{1, 2, 3, null, null, null, null, null, null, null},
nulls_at({3, 4, 5, 6, 7, 8, 9})},
ListsCol{} /*NULL*/},
nulls_at({0, 2, 3, 6})};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
}
TYPED_TEST(ConcatenateListElementsTypedTest, SimpleInputNestedManyLevelsWithNulls)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto row00 = ListsCol{{1, 2}, {3}, {4, 5, 6}};
auto row01 = ListsCol{ListsCol{}}; /*NULL*/
auto row02 = ListsCol{{7, 8}, {9, 10}};
auto row0 = ListsCol{{std::move(row00), std::move(row01), std::move(row02)}, null_at(1)};
auto row10 = ListsCol{{{1, 2}, {3}, {4, 5, 6} /*NULL*/}, null_at(2)};
auto row11 = ListsCol{ListsCol{}};
auto row12 = ListsCol{{7, 8}, {9, 10}};
auto row1 = build_lists_col(row10, row11, row12);
auto row20 = ListsCol{{1, 2}, {3}, {4, 5, 6}};
auto row21 = ListsCol{ListsCol{}};
auto row22 = ListsCol{ListsCol{{null, 8}, null_at(0)}, {9, 10}};
auto row2 = build_lists_col(row20, row21, row22);
auto const col = build_lists_col(row0, row1, row2);
// Ignore null list elements.
{
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected =
ListsCol{ListsCol{{1, 2}, {3}, {4, 5, 6}, {7, 8}, {9, 10}},
ListsCol{{{1, 2}, {3}, {} /*NULL*/, {}, {7, 8}, {9, 10}}, null_at(2)},
ListsCol{{1, 2}, {3}, {4, 5, 6}, {}, ListsCol{{null, 8}, null_at(0)}, {9, 10}}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
// Null lists result in null rows.
{
auto const results = cudf::lists::concatenate_list_elements(
col, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected =
ListsCol{{ListsCol{ListsCol{}}, /*NULL*/
ListsCol{{{1, 2}, {3}, {} /*NULL*/, {}, {7, 8}, {9, 10}}, null_at(2)},
ListsCol{{1, 2}, {3}, {4, 5, 6}, {}, ListsCol{{null, 8}, null_at(0)}, {9, 10}}},
null_at(0)};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
}
TEST_F(ConcatenateListElementsTest, SimpleInputStringsColumnWithNulls)
{
auto row0 = StrListsCol{
StrListsCol{{"Tomato", "Bear" /*NULL*/, "Apple"}, null_at(1)},
StrListsCol{{"Orange", "Dog" /*NULL*/, "Fox" /*NULL*/, "Duck" /*NULL*/}, nulls_at({1, 2, 3})}};
auto row1 = StrListsCol{
StrListsCol{{"Banana", "Pig" /*NULL*/, "Kiwi", "Cherry", "Whale" /*NULL*/}, nulls_at({1, 4})},
StrListsCol{"Lemon", "Peach"}};
auto row2 = StrListsCol{{StrListsCol{"Coconut"}, StrListsCol{} /*NULL*/}, null_at(1)};
auto const col = build_lists_col(row0, row1, row2);
// Ignore null list elements.
{
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = StrListsCol{
StrListsCol{{"Tomato", "" /*NULL*/, "Apple", "Orange", "" /*NULL*/, "" /*NULL*/, ""
/*NULL*/},
nulls_at({1, 4, 5, 6})},
StrListsCol{{"Banana", "" /*NULL*/, "Kiwi", "Cherry", "" /*NULL*/, "Lemon", "Peach"},
nulls_at({1, 4})},
StrListsCol{"Coconut"}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
// Null lists result in null rows.
{
auto const results = cudf::lists::concatenate_list_elements(
col, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected = StrListsCol{
{StrListsCol{
{"Tomato", "" /*NULL*/, "Apple", "Orange", "" /*NULL*/, "" /*NULL*/, "" /*NULL*/},
nulls_at({1, 4, 5, 6})},
StrListsCol{{"Banana", "" /*NULL*/, "Kiwi", "Cherry", "" /*NULL*/, "Lemon", "Peach"},
nulls_at({1, 4})},
StrListsCol{} /*NULL*/},
null_at(2)};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
}
TEST_F(ConcatenateListElementsTest, SimpleInputStringsColumnWithEmptyStringsAndNulls)
{
auto row0 = StrListsCol{
StrListsCol{"", "", ""},
StrListsCol{{"Orange", "" /*NULL*/, "" /*NULL*/, "" /*NULL*/}, nulls_at({1, 2, 3})}};
auto row1 = StrListsCol{
StrListsCol{{"Banana", "" /*NULL*/, "Kiwi", "Cherry", "" /*NULL*/}, nulls_at({1, 4})},
StrListsCol{""}};
auto row2 = StrListsCol{{StrListsCol{"Coconut"}, StrListsCol{} /*NULL*/}, null_at(1)};
auto const col = build_lists_col(row0, row1, row2);
// Ignore null list elements.
{
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = StrListsCol{
StrListsCol{{"", "", "", "Orange", "" /*NULL*/, "" /*NULL*/, "" /*NULL*/},
nulls_at({4, 5, 6})},
StrListsCol{{"Banana", "" /*NULL*/, "Kiwi", "Cherry", "" /*NULL*/, ""}, nulls_at({1, 4})},
StrListsCol{"Coconut"}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
// Null lists result in null rows.
{
auto const results = cudf::lists::concatenate_list_elements(
col, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected = StrListsCol{
{StrListsCol{{"", "", "", "Orange", "" /*NULL*/, "" /*NULL*/, "" /*NULL*/},
nulls_at({4, 5, 6})},
StrListsCol{{"Banana", "" /*NULL*/, "Kiwi", "Cherry", "" /*NULL*/, ""}, nulls_at({1, 4})},
StrListsCol{} /*NULL*/},
null_at(2)};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
}
TYPED_TEST(ConcatenateListElementsTypedTest, SlicedColumnsInputNoNull)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto const col_original = ListsCol{ListsCol{{1, 2, 3}, {2, 3}},
ListsCol{{3, 4, 5, 6}, {5, 6}, {}, {7}},
ListsCol{{7, 7, 7}, {7, 8, 1, 0}, {1}},
ListsCol{{9, 10, 11}},
ListsCol{},
ListsCol{{12, 13, 14, 15}, {16}, {17}}};
{
auto const col = cudf::slice(col_original, {0, 3})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected =
ListsCol{{1, 2, 3, 2, 3}, {3, 4, 5, 6, 5, 6, 7}, {7, 7, 7, 7, 8, 1, 0, 1}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {1, 4})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = ListsCol{{3, 4, 5, 6, 5, 6, 7}, {7, 7, 7, 7, 8, 1, 0, 1}, {9, 10, 11}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {2, 5})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = ListsCol{{7, 7, 7, 7, 8, 1, 0, 1}, {9, 10, 11}, {}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {3, 6})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = ListsCol{{9, 10, 11}, {}, {12, 13, 14, 15, 16, 17}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
}
TYPED_TEST(ConcatenateListElementsTypedTest, SlicedColumnsInputWithNulls)
{
using ListsCol = cudf::test::lists_column_wrapper<TypeParam>;
auto row0 = ListsCol{ListsCol{{null, 2, 3}, null_at(0)}, ListsCol{2, 3}};
auto row1 = ListsCol{ListsCol{{3, null, null, 6}, nulls_at({1, 2})},
ListsCol{{5, 6, null}, null_at(2)},
ListsCol{},
ListsCol{{7, null}, null_at(1)}};
auto row2 = ListsCol{ListsCol{7, 7, 7}, ListsCol{{7, 8, null, 0}, null_at(2)}, ListsCol{1}};
auto row3 = ListsCol{ListsCol{9, 10, 11}};
auto row4 = ListsCol{ListsCol{}};
auto row5 = ListsCol{ListsCol{{12, null, 14, 15}, null_at(1)}, ListsCol{16}, ListsCol{17}};
auto const col_original = build_lists_col(row0, row1, row2, row3, row4, row5);
{
auto const col = cudf::slice(col_original, {0, 3})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected =
ListsCol{ListsCol{{null, 2, 3, 2, 3}, null_at(0)},
ListsCol{{3, null, null, 6, 5, 6, null, 7, null}, nulls_at({1, 2, 6, 8})},
ListsCol{{7, 7, 7, 7, 8, null, 0, 1}, null_at(5)}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {1, 4})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected =
ListsCol{ListsCol{{3, null, null, 6, 5, 6, null, 7, null}, nulls_at({1, 2, 6, 8})},
ListsCol{{7, 7, 7, 7, 8, null, 0, 1}, null_at(5)},
ListsCol{9, 10, 11}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {2, 5})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected =
ListsCol{ListsCol{{7, 7, 7, 7, 8, null, 0, 1}, null_at(5)}, ListsCol{9, 10, 11}, ListsCol{}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {3, 6})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected =
ListsCol{ListsCol{9, 10, 11}, ListsCol{}, ListsCol{{12, null, 14, 15, 16, 17}, null_at(1)}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
}
TEST_F(ConcatenateListElementsTest, SlicedStringsColumnsInputWithNulls)
{
auto row0 = StrListsCol{
StrListsCol{{"Tomato", "Bear" /*NULL*/, "Apple"}, null_at(1)},
StrListsCol{{"Banana", "Pig" /*NULL*/, "Kiwi", "Cherry", "Whale" /*NULL*/}, nulls_at({1, 4})},
StrListsCol{"Coconut"}};
auto row1 = StrListsCol{
StrListsCol{{"Banana", "Pig" /*NULL*/, "Kiwi", "Cherry", "Whale" /*NULL*/}, nulls_at({1, 4})},
StrListsCol{"Coconut"},
StrListsCol{{"Orange", "Dog" /*NULL*/, "Fox" /*NULL*/, "Duck" /*NULL*/}, nulls_at({1, 2, 3})}};
auto row2 = StrListsCol{
StrListsCol{"Coconut"},
StrListsCol{{"Orange", "Dog" /*NULL*/, "Fox" /*NULL*/, "Duck" /*NULL*/}, nulls_at({1, 2, 3})},
StrListsCol{"Lemon", "Peach"}};
auto row3 = StrListsCol{
{StrListsCol{{"Orange", "Dog" /*NULL*/, "Fox" /*NULL*/, "Duck" /*NULL*/}, nulls_at({1, 2, 3})},
StrListsCol{"Lemon", "Peach"},
StrListsCol{} /*NULL*/},
null_at(2)};
auto const col_original = build_lists_col(row0, row1, row2, row3);
{
auto const col = cudf::slice(col_original, {0, 2})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = StrListsCol{StrListsCol{{"Tomato",
"" /*NULL*/,
"Apple",
"Banana",
"" /*NULL*/,
"Kiwi",
"Cherry",
"" /*NULL*/,
"Coconut"},
nulls_at({1, 4, 7})},
StrListsCol{{"Banana",
"" /*NULL*/,
"Kiwi",
"Cherry",
"" /*NULL*/,
"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"" /*NULL*/},
nulls_at({1, 4, 7, 8, 9})}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {1, 3})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = StrListsCol{StrListsCol{{"Banana",
"" /*NULL*/,
"Kiwi",
"Cherry",
"" /*NULL*/,
"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"" /*NULL*/},
nulls_at({1, 4, 7, 8, 9})},
StrListsCol{{"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"", /*NULL*/
"Lemon",
"Peach"},
nulls_at({2, 3, 4})}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {2, 4})[0];
auto const results = cudf::lists::concatenate_list_elements(col);
auto const expected = StrListsCol{StrListsCol{{"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"", /*NULL*/
"Lemon",
"Peach"},
nulls_at({2, 3, 4})},
StrListsCol{{"Orange",
"" /*NULL*/,
"" /*NULL*/,
"", /*NULL*/
"Lemon",
"Peach"},
nulls_at({1, 2, 3})}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
{
auto const col = cudf::slice(col_original, {2, 4})[0];
auto const results = cudf::lists::concatenate_list_elements(
col, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
auto const expected = StrListsCol{{StrListsCol{{"Coconut",
"Orange",
"" /*NULL*/,
"" /*NULL*/,
"", /*NULL*/
"Lemon",
"Peach"},
nulls_at({2, 3, 4})},
StrListsCol{} /*NULL*/},
null_at(1)};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *results, verbosity);
}
}
TEST_F(ConcatenateListElementsTest, ListsOfListsOfStructsNoNull)
{
using structs_col = cudf::test::structs_column_wrapper;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using strings_col = cudf::test::strings_column_wrapper;
// Input:
// [ [{1, 11, "1"}, {2, 12, "2"}], [{3, 13, "3"}], [{4, 14, "4"}, {5, 15, "5"}, {6, 16, "6"}] ]
// [ [] ]
// [ [{7, 17, "7"}, {8, 18, "8"}], [{9, 19, "9"}, {10, 110, "10"}] ]
auto const input = [] {
auto child = [] {
auto child1 = int32s_col{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto child2 = int32s_col{11, 12, 13, 14, 15, 16, 17, 18, 19, 110};
auto child3 = strings_col{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 2, 3, 6, 6, 8, 10};
return cudf::make_lists_column(6, offsets.release(), structs.release(), 0, {});
}();
auto offsets = int32s_col{0, 3, 4, 6};
return cudf::make_lists_column(3, offsets.release(), std::move(child), 0, {});
}();
// Output:
// [{1, 11, "1"}, {2, 12, "2"}, {3, 13, "3"}, {4, 14, "4"}, {5, 15, "5"}, {6, 16, "6"}]
// []
// [{7, 17, "7"}, {8, 18, "8"}, {9, 19, "9"}, {10, 110, "10"}]
auto const expected = [] {
auto child1 = int32s_col{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto child2 = int32s_col{11, 12, 13, 14, 15, 16, 17, 18, 19, 110};
auto child3 = strings_col{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10"};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 6, 6, 10};
return cudf::make_lists_column(3, offsets.release(), structs.release(), 0, {});
}();
auto const results = cudf::lists::concatenate_list_elements(*input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
TEST_F(ConcatenateListElementsTest, ListsOfListsOfStructsWithNull)
{
using structs_col = cudf::test::structs_column_wrapper;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using strings_col = cudf::test::strings_column_wrapper;
// Input:
// [ [{1, 11, "1"}, {null, null, null}], [{3, 13, "3"}], NULL ]
// [ [{4, 14, "4"}, {5, 15, "5"}, {null, null, null}] ]
// [ [{7, 17, "7"}, {null, null, null}], [{9, 19, "9"}, {10, 110, "10"}] ]
auto const input = [] {
auto child = [] {
auto child1 = int32s_col{1, null, 3, 4, 5, null, 7, null, 9, 10};
auto child2 = int32s_col{11, null, 13, 14, 15, null, 17, null, 19, 110};
auto child3 = strings_col{"1", "", "3", "4", "5", "", "7", "", "9", "10"};
auto structs = structs_col{{child1, child2, child3}, nulls_at({1, 5, 7})};
auto offsets = int32s_col{0, 2, 3, 3, 6, 8, 10};
auto const null_it = null_at(2); // null list
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(null_it, null_it + 6);
return cudf::make_lists_column(
6, offsets.release(), structs.release(), null_count, std::move(null_mask));
}();
auto offsets = int32s_col{0, 3, 4, 6};
return cudf::make_lists_column(3, offsets.release(), std::move(child), 0, {});
}();
// Concatenate with ignoring null lists.
{
// Output:
// [{1, 11, "1"}, {null, null, null}, {3, 13, "3"}]
// [{4, 14, "4"}, {5, 15, "5"}, {null, null, null}]
// [{7, 17, "7"}, {null, null, null}, {9, 19, "9"}, {10, 110, "10"}]
auto const expected = [] {
auto child1 = int32s_col{1, null, 3, 4, 5, null, 7, null, 9, 10};
auto child2 = int32s_col{11, null, 13, 14, 15, null, 17, null, 19, 110};
auto child3 = strings_col{"1", "", "3", "4", "5", "", "7", "", "9", "10"};
auto structs = structs_col{{child1, child2, child3}, nulls_at({1, 5, 7})};
auto offsets = int32s_col{0, 3, 6, 10};
return cudf::make_lists_column(3, offsets.release(), structs.release(), 0, {});
}();
auto const results = cudf::lists::concatenate_list_elements(*input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
// Concatenate with ignoring null lists and sliced input.
{
// Output:
// [{4, 14, "4"}, {5, 15, "5"}, {null, null, null}]
auto const expected = [] {
auto child1 = int32s_col{4, 5, null};
auto child2 = int32s_col{14, 15, null};
auto child3 = strings_col{"4", "5", ""};
auto structs = structs_col{{child1, child2, child3}, null_at(2)};
auto offsets = int32s_col{0, 3};
return cudf::make_lists_column(1, offsets.release(), structs.release(), 0, {});
}();
auto const sliced_input = cudf::slice(*input, {1, 2})[0];
auto const results = cudf::lists::concatenate_list_elements(sliced_input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
// Concatenate with `concatenate_null_policy::NULLIFY_OUTPUT_ROW`.
{
// Output:
// NULL
// [{4, 14, "4"}, {5, 15, "5"}, {null, null, null}]
// [{7, 17, "7"}, {null, null, null}, {9, 19, "9"}, {10, 110, "10"}]
auto const expected = [] {
auto child1 = int32s_col{4, 5, null, 7, null, 9, 10};
auto child2 = int32s_col{14, 15, null, 17, null, 19, 110};
auto child3 = strings_col{"4", "5", "", "7", "", "9", "10"};
auto structs = structs_col{{child1, child2, child3}, nulls_at({2, 4})};
auto offsets = int32s_col{0, 0, 3, 7};
auto const null_it = null_at(0); // null row
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(null_it, null_it + 3);
return cudf::make_lists_column(
3, offsets.release(), structs.release(), null_count, std::move(null_mask));
}();
auto const results = cudf::lists::concatenate_list_elements(
*input, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
// Concatenate with `concatenate_null_policy::NULLIFY_OUTPUT_ROW` and sliced input.
{
// Output:
// NULL
// [{4, 14, "4"}, {5, 15, "5"}, {null, null, null}]
auto const expected = [] {
auto child1 = int32s_col{4, 5, null};
auto child2 = int32s_col{14, 15, null};
auto child3 = strings_col{"4", "5", ""};
auto structs = structs_col{{child1, child2, child3}, null_at(2)};
auto offsets = int32s_col{0, 0, 3};
auto const null_it = null_at(0); // null row
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(null_it, null_it + 2);
return cudf::make_lists_column(
2, offsets.release(), structs.release(), null_count, std::move(null_mask));
}();
auto const sliced_input = cudf::slice(*input, {0, 2})[0];
auto const results = cudf::lists::concatenate_list_elements(
sliced_input, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
}
TEST_F(ConcatenateListElementsTest, ListsOfListsOfStructsHavingListsNoNull)
{
using structs_col = cudf::test::structs_column_wrapper;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using lists_col = cudf::test::lists_column_wrapper<int32_t>;
// clang-format off
// Input:
// [ [{1, 11, [1, 1]}, {2, 12, [2]}], [{3, 13, [3, 3]}], [{4, 14, []}, {5, 15, [5, 5, 5]}, {6, 16, [6, 6]}] ]
// [ [] ]
// [ [{7, 17, [7]}, {8, 18, [8]}], [{9, 19, [9, 9]}, {10, 110, [10, 10, 10, 10]}] ]
// clang-format on
auto const input = [] {
auto child = [] {
auto child1 = int32s_col{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto child2 = int32s_col{11, 12, 13, 14, 15, 16, 17, 18, 19, 110};
auto child3 =
lists_col{{1, 1}, {2}, {3, 3}, {}, {5, 5, 5}, {6, 6}, {7}, {8}, {9, 9}, {10, 10, 10, 10}};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 2, 3, 6, 6, 8, 10};
return cudf::make_lists_column(6, offsets.release(), structs.release(), 0, {});
}();
auto offsets = int32s_col{0, 3, 4, 6};
return cudf::make_lists_column(3, offsets.release(), std::move(child), 0, {});
}();
// clang-format off
// Output:
// [{1, 11, [1, 1]}, {2, 12, [2]}, {3, 13, [3, 3]}, {4, 14, []}, {5, 15, [5, 5, 5]}, {6, 16, [6, 6]}]
// []
// [{7, 17, [7]}, {8, 18, [8]}, {9, 19, [9, 9]}, {10, 110, [10, 10, 10, 10]}]
// clang-format on
auto const expected = [] {
auto child1 = int32s_col{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto child2 = int32s_col{11, 12, 13, 14, 15, 16, 17, 18, 19, 110};
auto child3 =
lists_col{{1, 1}, {2}, {3, 3}, {}, {5, 5, 5}, {6, 6}, {7}, {8}, {9, 9}, {10, 10, 10, 10}};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 6, 6, 10};
return cudf::make_lists_column(3, offsets.release(), structs.release(), 0, {});
}();
auto const results = cudf::lists::concatenate_list_elements(*input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
TEST_F(ConcatenateListElementsTest, ListsOfListsOfStructsHavingListsWithNulls)
{
using structs_col = cudf::test::structs_column_wrapper;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using lists_col = cudf::test::lists_column_wrapper<int32_t>;
// Input:
// [ [{1, 11, [1, 1]}, {2, 12, [2]}], [{3, 13, [3, 3]}] ]
// [ [{4, 14, null}, {5, 15, [5, 5, 5]}, {6, 16, [6, 6]}], NULL ]
// [ [{7, 17, [7]}, {8, 18, [8]}], [{9, 19, [9, 9]}, {10, 110, [10, 10, 10, 10]}] ]
auto const input = [] {
auto child = [] {
auto child1 = int32s_col{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto child2 = int32s_col{11, 12, 13, 14, 15, 16, 17, 18, 19, 110};
auto child3 =
lists_col{{{1, 1}, {2}, {3, 3}, {}, {5, 5, 5}, {6, 6}, {7}, {8}, {9, 9}, {10, 10, 10, 10}},
null_at(3)};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 2, 3, 6, 6, 8, 10};
auto const null_it = null_at(3); // null list
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(null_it, null_it + 6);
return cudf::make_lists_column(
6, offsets.release(), structs.release(), null_count, std::move(null_mask));
}();
auto offsets = int32s_col{0, 2, 4, 6};
return cudf::make_lists_column(3, offsets.release(), std::move(child), 0, {});
}();
// Concatenate with ignoring null lists.
{
// Output:
// [{1, 11, [1, 1]}, {2, 12, [2]}, {3, 13, [3, 3]}]
// [{4, 14, null}, {5, 15, [5, 5, 5]}, {6, 16, [6, 6]}]
// [{7, 17, [7]}, {8, 18, [8]}, {9, 19, [9, 9]}, {10, 110, [10, 10, 10, 10]}]
auto const expected = [] {
auto child1 = int32s_col{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto child2 = int32s_col{11, 12, 13, 14, 15, 16, 17, 18, 19, 110};
auto child3 =
lists_col{{{1, 1}, {2}, {3, 3}, {}, {5, 5, 5}, {6, 6}, {7}, {8}, {9, 9}, {10, 10, 10, 10}},
null_at(3)};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 3, 6, 10};
return cudf::make_lists_column(3, offsets.release(), structs.release(), 0, {});
}();
auto const results = cudf::lists::concatenate_list_elements(*input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
// Concatenate with ignoring null lists and sliced input.
{
// Output:
// [{4, 14, null}, {5, 15, [5, 5, 5]}, {6, 16, [6, 6]}]
auto const expected = [] {
auto child1 = int32s_col{4, 5, 6};
auto child2 = int32s_col{14, 15, 16};
auto child3 = lists_col{{{}, {5, 5, 5}, {6, 6}}, null_at(0)};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 3};
return cudf::make_lists_column(1, offsets.release(), structs.release(), 0, {});
}();
auto const sliced_input = cudf::slice(*input, {1, 2})[0];
auto const results = cudf::lists::concatenate_list_elements(sliced_input);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
// Concatenate with `concatenate_null_policy::NULLIFY_OUTPUT_ROW`.
{
// Output:
// [{1, 11, [1, 1]}, {2, 12, [2]}, {3, 13, [3, 3]}]
// NULL
// [{7, 17, [7]}, {8, 18, [8]}, {9, 19, [9, 9]}, {10, 110, [10, 10, 10, 10]}]
auto const expected = [] {
auto child1 = int32s_col{1, 2, 3, 7, 8, 9, 10};
auto child2 = int32s_col{11, 12, 13, 17, 18, 19, 110};
auto child3 =
lists_col{{{1, 1}, {2}, {3, 3}, {7}, {8}, {9, 9}, {10, 10, 10, 10}}, no_nulls()};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 3, 3, 7};
auto const null_it = null_at(1); // null row
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(null_it, null_it + 3);
return cudf::make_lists_column(
3, offsets.release(), structs.release(), null_count, std::move(null_mask));
}();
auto const results = cudf::lists::concatenate_list_elements(
*input, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
// Concatenate with `concatenate_null_policy::NULLIFY_OUTPUT_ROW` and sliced input.
{
// Output:
// NULL
// [{7, 17, [7]}, {8, 18, [8]}, {9, 19, [9, 9]}, {10, 110, [10, 10, 10, 10]}]
auto const expected = [] {
auto child1 = int32s_col{7, 8, 9, 10};
auto child2 = int32s_col{17, 18, 19, 110};
auto child3 = lists_col{{{7}, {8}, {9, 9}, {10, 10, 10, 10}}, no_nulls()};
auto structs = structs_col{{child1, child2, child3}};
auto offsets = int32s_col{0, 0, 4};
auto const null_it = null_at(0); // null row
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(null_it, null_it + 2);
return cudf::make_lists_column(
2, offsets.release(), structs.release(), null_count, std::move(null_mask));
}();
auto const sliced_input = cudf::slice(*input, {1, 3})[0];
auto const results = cudf::lists::concatenate_list_elements(
sliced_input, cudf::lists::concatenate_null_policy::NULLIFY_OUTPUT_ROW);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *results, verbosity);
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/transform/bools_to_mask_test.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/transform.hpp>
#include <cudf/types.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <thrust/host_vector.h>
struct MaskToNullTest : public cudf::test::BaseFixture {
void run_test(std::vector<bool> input, std::vector<bool> val)
{
cudf::test::fixed_width_column_wrapper<bool> input_column(
input.begin(), input.end(), val.begin());
std::transform(val.begin(), val.end(), input.begin(), input.begin(), std::logical_and<bool>());
auto sample = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i; });
cudf::test::fixed_width_column_wrapper<int32_t> expected(
sample, sample + input.size(), input.begin());
auto [null_mask, null_count] = cudf::bools_to_mask(input_column);
cudf::column got_column(expected);
got_column.set_null_mask(std::move(*null_mask), null_count);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got_column.view());
}
void run_test(thrust::host_vector<bool> input)
{
cudf::test::fixed_width_column_wrapper<bool> input_column(input.begin(), input.end());
auto sample = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i; });
cudf::test::fixed_width_column_wrapper<int32_t> expected(
sample, sample + input.size(), input.begin());
auto [null_mask, null_count] = cudf::bools_to_mask(input_column);
cudf::column got_column(expected);
got_column.set_null_mask(std::move(*null_mask), null_count);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got_column.view());
}
};
TEST_F(MaskToNullTest, WithNoNull)
{
std::vector<bool> input({1, 0, 1, 0, 1, 0, 1, 0});
run_test(input);
}
TEST_F(MaskToNullTest, WithNull)
{
std::vector<bool> input({1, 0, 1, 0, 1, 0, 1, 0});
std::vector<bool> val({1, 1, 1, 1, 1, 1, 0, 1});
run_test(input, val);
}
TEST_F(MaskToNullTest, ZeroSize)
{
std::vector<bool> input({});
run_test(input);
}
TEST_F(MaskToNullTest, NonBoolTypeColumn)
{
cudf::test::fixed_width_column_wrapper<int32_t> input_column({1, 2, 3, 4, 5});
EXPECT_THROW(cudf::bools_to_mask(input_column), cudf::logic_error);
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/transform/mask_to_bools_test.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/copying.hpp>
#include <cudf/transform.hpp>
#include <cudf/types.hpp>
struct MaskToBools : public cudf::test::BaseFixture {};
TEST_F(MaskToBools, NullDataWithZeroLength)
{
auto expected = cudf::test::fixed_width_column_wrapper<bool>({});
auto out = cudf::mask_to_bools(nullptr, 0, 0);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, out->view());
}
TEST_F(MaskToBools, NullDataWithNonZeroLength)
{
auto expected = cudf::test::fixed_width_column_wrapper<bool>({});
EXPECT_THROW(cudf::mask_to_bools(nullptr, 0, 2), cudf::logic_error);
}
TEST_F(MaskToBools, ImproperBitRange)
{
auto expected = cudf::test::fixed_width_column_wrapper<bool>({});
EXPECT_THROW(cudf::mask_to_bools(nullptr, 2, 1), cudf::logic_error);
}
struct MaskToBoolsTest
: public MaskToBools,
public ::testing::WithParamInterface<std::tuple<cudf::size_type, cudf::size_type>> {};
TEST_P(MaskToBoolsTest, LargeDataSizeTest)
{
auto data = std::vector<bool>(10000);
auto const [begin_bit, end_bit] = GetParam();
std::transform(
data.cbegin(), data.cend(), data.begin(), [](auto val) { return rand() % 2 == 0; });
auto col = cudf::test::fixed_width_column_wrapper<bool>(data.begin(), data.end());
auto expected = cudf::slice(static_cast<cudf::column_view>(col), {begin_bit, end_bit}).front();
auto mask = cudf::bools_to_mask(col);
auto out = cudf::mask_to_bools(
static_cast<cudf::bitmask_type const*>(mask.first->data()), begin_bit, end_bit);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, out->view());
}
INSTANTIATE_TEST_CASE_P(MaskToBools,
MaskToBoolsTest,
::testing::Values(std::make_tuple(0, 0),
std::make_tuple(0, 500),
std::make_tuple(500, 7456),
std::make_tuple(7456, 10000),
std::make_tuple(0, 10000)));
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/transform/one_hot_encode_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/transform.hpp>
#include <limits>
using lists_col = cudf::test::lists_column_wrapper<int32_t>;
using structs_col = cudf::test::structs_column_wrapper;
using bool_col = cudf::test::fixed_width_column_wrapper<bool>;
using cudf::test::iterators::null_at;
using cudf::test::iterators::nulls_at;
template <typename T>
struct OneHotEncodingTestTyped : public cudf::test::BaseFixture {};
struct OneHotEncodingTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(OneHotEncodingTestTyped, cudf::test::NumericTypes);
TYPED_TEST(OneHotEncodingTestTyped, Basic)
{
auto input = cudf::test::fixed_width_column_wrapper<int32_t>{8, 8, 8, 9, 9};
auto category = cudf::test::fixed_width_column_wrapper<int32_t>{8, 9};
auto col0 = cudf::test::fixed_width_column_wrapper<bool>{1, 1, 1, 0, 0};
auto col1 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 1, 1};
auto expected = cudf::table_view{{col0, col1}};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TYPED_TEST(OneHotEncodingTestTyped, Nulls)
{
auto input = cudf::test::fixed_width_column_wrapper<int32_t>{{8, 8, 8, 9, 9}, {1, 1, 0, 1, 1}};
auto category = cudf::test::fixed_width_column_wrapper<int32_t>({8, 9, -1}, {1, 1, 0});
auto col0 = cudf::test::fixed_width_column_wrapper<bool>{1, 1, 0, 0, 0};
auto col1 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 1, 1};
auto col2 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 1, 0, 0};
auto expected = cudf::table_view{{col0, col1, col2}};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, Diagonal)
{
auto input = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 4, 5};
auto category = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 4, 5};
auto col0 = cudf::test::fixed_width_column_wrapper<bool>{1, 0, 0, 0, 0};
auto col1 = cudf::test::fixed_width_column_wrapper<bool>{0, 1, 0, 0, 0};
auto col2 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 1, 0, 0};
auto col3 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 1, 0};
auto col4 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 0, 1};
auto expected = cudf::table_view{{col0, col1, col2, col3, col4}};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, ZeroInput)
{
auto input = cudf::test::strings_column_wrapper{};
auto category = cudf::test::strings_column_wrapper{"rapids", "cudf"};
auto col0 = cudf::test::fixed_width_column_wrapper<bool>{};
auto col1 = cudf::test::fixed_width_column_wrapper<bool>{};
auto expected = cudf::table_view{{col0, col1}};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, ZeroCat)
{
auto input = cudf::test::strings_column_wrapper{"ji", "ji", "ji"};
auto category = cudf::test::strings_column_wrapper{};
auto expected = cudf::table_view{};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, ZeroInputCat)
{
auto input = cudf::test::strings_column_wrapper{};
auto category = cudf::test::strings_column_wrapper{};
auto expected = cudf::table_view{};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, OneCat)
{
auto input = cudf::test::strings_column_wrapper{"ji", "ji", "ji"};
auto category = cudf::test::strings_column_wrapper{"ji"};
auto col0 = cudf::test::fixed_width_column_wrapper<bool>{1, 1, 1};
auto expected = cudf::table_view{{col0}};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, NaNs)
{
auto const nan = std::numeric_limits<float>::signaling_NaN();
auto input = cudf::test::fixed_width_column_wrapper<float>{8.f, 8.f, 8.f, 9.f, nan};
auto category = cudf::test::fixed_width_column_wrapper<float>{8.f, 9.f, nan};
auto col0 = cudf::test::fixed_width_column_wrapper<bool>{1, 1, 1, 0, 0};
auto col1 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 1, 0};
auto col2 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 0, 1};
auto expected = cudf::table_view{{col0, col1, col2}};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, Strings)
{
auto input = cudf::test::strings_column_wrapper{
{"hello", "rapidsai", "cudf", "hello", "cuspatial", "hello", "world", "!"},
{1, 1, 1, 1, 0, 1, 1, 0}};
auto category = cudf::test::strings_column_wrapper{{"hello", "world", ""}, {1, 1, 0}};
auto col0 = cudf::test::fixed_width_column_wrapper<bool>{1, 0, 0, 1, 0, 1, 0, 0};
auto col1 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 0, 0, 0, 1, 0};
auto col2 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 0, 1, 0, 0, 1};
auto expected = cudf::table_view{{col0, col1, col2}};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, Dictionary)
{
auto input =
cudf::test::dictionary_column_wrapper<std::string>{"aa", "xx", "aa", "aa", "yy", "ef"};
auto category = cudf::test::dictionary_column_wrapper<std::string>{"aa", "ef"};
auto col0 = cudf::test::fixed_width_column_wrapper<bool>{1, 0, 1, 1, 0, 0};
auto col1 = cudf::test::fixed_width_column_wrapper<bool>{0, 0, 0, 0, 0, 1};
auto expected = cudf::table_view{{col0, col1}};
[[maybe_unused]] auto [res_ptr, got] = cudf::one_hot_encode(input, category);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, MismatchTypes)
{
auto input = cudf::test::strings_column_wrapper{"xx", "yy", "xx"};
auto category = cudf::test::fixed_width_column_wrapper<int64_t>{1};
EXPECT_THROW(cudf::one_hot_encode(input, category), cudf::logic_error);
}
TEST_F(OneHotEncodingTest, List)
{
auto const input =
lists_col{{{}, {1}, {2, 2}, {2, 2}, {}, {2} /*NULL*/, {2}, {5} /*NULL*/}, nulls_at({5, 7})};
auto const categories = lists_col{{{}, {1}, {2, 2}, {2}, {-1}}, null_at(4)};
auto const col0 = bool_col{1, 0, 0, 0, 1, 0, 0, 0};
auto const col1 = bool_col{0, 1, 0, 0, 0, 0, 0, 0};
auto const col2 = bool_col{0, 0, 1, 1, 0, 0, 0, 0};
auto const col3 = bool_col{0, 0, 0, 0, 0, 0, 1, 0};
auto const col4 = bool_col{0, 0, 0, 0, 0, 1, 0, 1};
auto const expected = cudf::table_view{{col0, col1, col2, col3, col4}};
[[maybe_unused]] auto const [res_ptr, got] = cudf::one_hot_encode(input, categories);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
TEST_F(OneHotEncodingTest, StructsOfStructs)
{
// +-----------------+
// | s1{s2{a,b}, c} |
// +-----------------+
// 0 | Null |
// 1 | { {1, 2}, 4} |
// 2 | { Null, 4} |
// 3 | Null |
// 4 | { {2, 1}, 5} |
// 5 | { Null, 4} |
// 6 | { {2, 1}, 5} |
auto const input = [&] {
auto a = cudf::test::fixed_width_column_wrapper<int32_t>{-1, 1, -1, -1, 2, -1, 2};
auto b = cudf::test::fixed_width_column_wrapper<int32_t>{-1, 2, -1, -1, 1, -1, 1};
auto s2 = structs_col{{a, b}, nulls_at({2, 5})};
auto c = cudf::test::fixed_width_column_wrapper<int32_t>{-1, 4, 4, -1, 5, 4, 5};
std::vector<std::unique_ptr<cudf::column>> s1_children;
s1_children.emplace_back(s2.release());
s1_children.emplace_back(c.release());
auto const null_it = nulls_at({0, 3});
return structs_col(std::move(s1_children), std::vector<bool>{null_it, null_it + 7});
}();
auto const categories = [&] {
auto a = cudf::test::fixed_width_column_wrapper<int32_t>{-1, 1, -1, 2};
auto b = cudf::test::fixed_width_column_wrapper<int32_t>{-1, 2, -1, 1};
auto s2 = structs_col{{a, b}, null_at(2)};
auto c = cudf::test::fixed_width_column_wrapper<int32_t>{-1, 4, 4, 5};
std::vector<std::unique_ptr<cudf::column>> s1_children;
s1_children.emplace_back(s2.release());
s1_children.emplace_back(c.release());
auto const null_it = null_at(0);
return structs_col(std::move(s1_children), std::vector<bool>{null_it, null_it + 4});
}();
auto const col0 = bool_col{1, 0, 0, 1, 0, 0, 0};
auto const col1 = bool_col{0, 1, 0, 0, 0, 0, 0};
auto const col2 = bool_col{0, 0, 1, 0, 0, 1, 0};
auto const col3 = bool_col{0, 0, 0, 0, 1, 0, 1};
auto const expected = cudf::table_view{{col0, col1, col2, col3}};
[[maybe_unused]] auto const [res_ptr, got] = cudf::one_hot_encode(input, categories);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, got);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/transform/row_bit_count_test.cu
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/column/column.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/transform.hpp>
#include <cudf/types.hpp>
#include <rmm/exec_policy.hpp>
#include <thrust/fill.h>
#include <thrust/functional.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/tabulate.h>
#include <thrust/transform.h>
#include <numeric>
template <typename T>
struct RowBitCountTyped : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(RowBitCountTyped, cudf::test::FixedWidthTypes);
TYPED_TEST(RowBitCountTyped, SimpleTypes)
{
using T = TypeParam;
auto col = cudf::make_fixed_width_column(cudf::data_type{cudf::type_to_id<T>()}, 16);
cudf::table_view t({*col});
auto result = cudf::row_bit_count(t);
// expect size of the type per row
auto expected = make_fixed_width_column(cudf::data_type{cudf::type_id::INT32}, 16);
cudf::mutable_column_view mcv(*expected);
thrust::fill(rmm::exec_policy(cudf::get_default_stream()),
mcv.begin<cudf::size_type>(),
mcv.end<cudf::size_type>(),
sizeof(cudf::device_storage_type_t<T>) * CHAR_BIT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *result);
}
TYPED_TEST(RowBitCountTyped, SimpleTypesWithNulls)
{
using T = TypeParam;
auto iter = thrust::make_counting_iterator(0);
auto valids = cudf::detail::make_counting_transform_iterator(0, [](int i) { return i % 2 == 0; });
cudf::test::fixed_width_column_wrapper<T> col(iter, iter + 16, valids);
cudf::table_view t({col});
auto result = cudf::row_bit_count(t);
// expect size of the type + 1 bit per row
auto expected = make_fixed_width_column(cudf::data_type{cudf::type_id::INT32}, 16);
cudf::mutable_column_view mcv(*expected);
thrust::fill(rmm::exec_policy(cudf::get_default_stream()),
mcv.begin<cudf::size_type>(),
mcv.end<cudf::size_type>(),
(sizeof(cudf::device_storage_type_t<T>) * CHAR_BIT) + 1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *result);
}
template <typename T>
std::pair<std::unique_ptr<cudf::column>, std::unique_ptr<cudf::column>> build_list_column()
{
using LCW = cudf::test::lists_column_wrapper<T, int>;
constexpr cudf::size_type type_size = sizeof(cudf::device_storage_type_t<T>) * CHAR_BIT;
// {
// {{1, 2}, {3, 4, 5}},
// {{}},
// {LCW{10}},
// {{6, 7, 8}, {9}},
// {{-1, -2}, {-3, -4}},
// {{-5, -6, -7}, {-8, -9}}
// }
cudf::test::fixed_width_column_wrapper<T> values{
1, 2, 3, 4, 5, 10, 6, 7, 8, 9, -1, -2, -3, -4, -5, -6, -7, -8, -9};
cudf::test::fixed_width_column_wrapper<cudf::size_type> inner_offsets{
0, 2, 5, 6, 9, 10, 12, 14, 17, 19};
auto inner_list = cudf::make_lists_column(9, inner_offsets.release(), values.release(), 0, {});
cudf::test::fixed_width_column_wrapper<cudf::size_type> outer_offsets{0, 2, 2, 3, 5, 7, 9};
auto list = cudf::make_lists_column(6, outer_offsets.release(), std::move(inner_list), 0, {});
// expected size = (num rows at level 1 + num_rows at level 2) + # values in the leaf
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected{
((4 + 8) * CHAR_BIT) + (type_size * 5),
((4 + 0) * CHAR_BIT) + (type_size * 0),
((4 + 4) * CHAR_BIT) + (type_size * 1),
((4 + 8) * CHAR_BIT) + (type_size * 4),
((4 + 8) * CHAR_BIT) + (type_size * 4),
((4 + 8) * CHAR_BIT) + (type_size * 5)};
return {std::move(list), expected.release()};
}
TYPED_TEST(RowBitCountTyped, Lists)
{
using T = TypeParam;
auto [col, expected_sizes] = build_list_column<T>();
cudf::table_view t({*col});
auto result = cudf::row_bit_count(t);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_sizes, *result);
}
TYPED_TEST(RowBitCountTyped, ListsWithNulls)
{
using T = TypeParam;
using LCW = cudf::test::lists_column_wrapper<T, int>;
constexpr cudf::size_type type_size = sizeof(cudf::device_storage_type_t<T>) * CHAR_BIT;
// {
// {{1, 2}, {3, null, 5}},
// {{}},
// {LCW{10}},
// {{null, 7, null}, null},
// }
cudf::test::fixed_width_column_wrapper<T> values{{1, 2, 3, 4, 5, 10, 6, 7, 8},
{1, 1, 1, 0, 1, 1, 0, 1, 0}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> inner_offsets{0, 2, 5, 6, 9, 9};
std::vector<bool> inner_list_validity{1, 1, 1, 1, 0};
auto [null_mask, null_count] =
cudf::test::detail::make_null_mask(inner_list_validity.begin(), inner_list_validity.end());
auto inner_list = cudf::make_lists_column(
5, inner_offsets.release(), values.release(), null_count, std::move(null_mask));
cudf::test::fixed_width_column_wrapper<cudf::size_type> outer_offsets{0, 2, 2, 3, 5};
auto list = cudf::make_lists_column(4, outer_offsets.release(), std::move(inner_list), 0, {});
cudf::table_view t({*list});
auto result = cudf::row_bit_count(t);
// expected size = (num rows at level 1 + num_rows at level 2) + # values in the leaf + validity
// where applicable
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected{
((4 + 8) * CHAR_BIT) + (type_size * 5) + 7,
((4 + 0) * CHAR_BIT) + (type_size * 0),
((4 + 4) * CHAR_BIT) + (type_size * 1) + 2,
((4 + 8) * CHAR_BIT) + (type_size * 3) + 5};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
struct RowBitCount : public cudf::test::BaseFixture {};
TEST_F(RowBitCount, Strings)
{
std::vector<std::string> strings{"abc", "ï", "", "z", "bananas", "warp", "", "zing"};
cudf::test::strings_column_wrapper col(strings.begin(), strings.end());
cudf::table_view t({col});
auto result = cudf::row_bit_count(t);
// expect 1 offset (4 bytes) + length of string per row
auto size_iter = cudf::detail::make_counting_transform_iterator(0, [&strings](int i) {
return (static_cast<cudf::size_type>(strings[i].size()) + sizeof(cudf::size_type)) * CHAR_BIT;
});
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected(size_iter,
size_iter + strings.size());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(RowBitCount, StringsWithNulls)
{
// clang-format off
std::vector<std::string> strings { "daïs", "def", "", "z", "bananas", "warp", "", "zing" };
std::vector<bool> valids { 1, 0, 0, 1, 0, 1, 1, 1 };
// clang-format on
cudf::test::strings_column_wrapper col(strings.begin(), strings.end(), valids.begin());
cudf::table_view t({col});
auto result = cudf::row_bit_count(t);
// expect 1 offset (4 bytes) + (length of string, or 0 if null) + 1 validity bit per row
auto size_iter = cudf::detail::make_counting_transform_iterator(0, [&strings, &valids](int i) {
return ((static_cast<cudf::size_type>(valids[i] ? strings[i].size() : 0) +
sizeof(cudf::size_type)) *
CHAR_BIT) +
1;
});
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected(size_iter,
size_iter + strings.size());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
namespace {
/**
* @brief __device__ functor to multiply input by 2, defined out of line because __device__ lambdas
* cannot be defined in a TEST_F().
*/
struct times_2 {
int32_t __device__ operator()(int32_t i) const { return i * 2; }
};
} // namespace
TEST_F(RowBitCount, StructsWithLists_RowsExceedingASingleBlock)
{
// Tests that `row_bit_count()` can handle struct<list<int32_t>> with more
// than max_block_size (256) rows.
// With a large number of rows, computation spills to multiple thread-blocks,
// thus exercising the branch-stack computation.
// The contents of the input column aren't as pertinent to this test as the
// column size. For what it's worth, it looks as follows:
// [ struct({0,1}), struct({2,3}), struct({4,5}), ... ]
auto constexpr num_rows = 1024 * 2; // Exceeding a block size.
// List child column = {0, 1, 2, 3, 4, ..., 2*num_rows};
auto ints = cudf::make_numeric_column(cudf::data_type{cudf::type_id::INT32}, num_rows * 2);
auto ints_view = ints->mutable_view();
thrust::tabulate(rmm::exec_policy(cudf::get_default_stream()),
ints_view.begin<int32_t>(),
ints_view.end<int32_t>(),
thrust::identity{});
// List offsets = {0, 2, 4, 6, 8, ..., num_rows*2};
auto list_offsets =
cudf::make_numeric_column(cudf::data_type{cudf::type_id::INT32}, num_rows + 1);
auto list_offsets_view = list_offsets->mutable_view();
thrust::tabulate(rmm::exec_policy(cudf::get_default_stream()),
list_offsets_view.begin<cudf::size_type>(),
list_offsets_view.end<cudf::size_type>(),
times_2{});
// List<int32_t> = {{0,1}, {2,3}, {4,5}, ..., {2*(num_rows-1), 2*num_rows-1}};
auto lists_column =
cudf::make_lists_column(num_rows, std::move(list_offsets), std::move(ints), 0, {});
// Struct<List<int32_t>.
auto struct_members = std::vector<std::unique_ptr<cudf::column>>{};
struct_members.emplace_back(std::move(lists_column));
auto structs_column = cudf::make_structs_column(num_rows, std::move(struct_members), 0, {});
// Compute row_bit_count, and compare.
auto row_bit_counts = cudf::row_bit_count(cudf::table_view{{structs_column->view()}});
auto expected_row_bit_counts =
cudf::make_numeric_column(cudf::data_type{cudf::type_id::INT32}, num_rows);
thrust::fill_n(rmm::exec_policy(cudf::get_default_stream()),
expected_row_bit_counts->mutable_view().begin<int32_t>(),
num_rows,
CHAR_BIT * (2 * sizeof(int32_t) + sizeof(cudf::size_type)));
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(row_bit_counts->view(), expected_row_bit_counts->view());
}
std::pair<std::unique_ptr<cudf::column>, std::unique_ptr<cudf::column>> build_struct_column()
{
std::vector<bool> struct_validity{0, 1, 1, 1, 1, 0};
std::vector<std::string> strings{"abc", "def", "", "z", "bananas", "daïs"};
cudf::test::fixed_width_column_wrapper<float> col0{0, 1, 2, 3, 4, 5};
cudf::test::fixed_width_column_wrapper<int16_t> col1{{8, 9, 10, 11, 12, 13}, {1, 0, 1, 1, 1, 1}};
cudf::test::strings_column_wrapper col2(strings.begin(), strings.end());
// creating a struct column will cause all child columns to be promoted to have validity
cudf::test::structs_column_wrapper struct_col({col0, col1, col2}, struct_validity);
// expect (1 offset (4 bytes) + (length of string if row is valid) + 1 validity bit) +
// (1 float + 1 validity bit) +
// (1 int16_t + 1 validity bit) +
// (1 validity bit)
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected_sizes{84, 108, 84, 92, 140, 84};
return {struct_col.release(), expected_sizes.release()};
}
TEST_F(RowBitCount, StructsNoNulls)
{
std::vector<std::string> strings{"abc", "daïs", "", "z", "bananas", "warp"};
cudf::test::fixed_width_column_wrapper<float> col0{0, 1, 2, 3, 4, 5};
cudf::test::fixed_width_column_wrapper<int16_t> col1{8, 9, 10, 11, 12, 13};
cudf::test::strings_column_wrapper col2(strings.begin(), strings.end());
cudf::test::structs_column_wrapper struct_col({col0, col1, col2});
cudf::table_view t({struct_col});
auto result = cudf::row_bit_count(t);
// expect 1 offset (4 bytes) + (length of string) + 1 float + 1 int16_t
auto size_iter = cudf::detail::make_counting_transform_iterator(0, [&strings](int i) {
return ((sizeof(float) + sizeof(int16_t)) * CHAR_BIT) +
((static_cast<cudf::size_type>(strings[i].size()) + sizeof(cudf::size_type)) * CHAR_BIT);
});
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected(size_iter,
size_iter + t.num_rows());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(RowBitCount, StructsNulls)
{
auto [struct_col, expected_sizes] = build_struct_column();
cudf::table_view t({*struct_col});
auto result = cudf::row_bit_count(t);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_sizes, *result);
}
TEST_F(RowBitCount, StructsNested)
{
// struct<struct<int>, int16>
cudf::test::fixed_width_column_wrapper<int> col0{0, 1, 2, 3, 4, 5};
cudf::test::structs_column_wrapper inner_struct({col0});
cudf::test::fixed_width_column_wrapper<int16_t> col1{8, 9, 10, 11, 12, 13};
cudf::test::structs_column_wrapper struct_col({inner_struct, col1});
cudf::table_view t({struct_col});
auto result = cudf::row_bit_count(t);
// expect num_rows * (4 + 2) bytes
auto size_iter =
cudf::detail::make_counting_transform_iterator(0, [&](int i) { return (4 + 2) * CHAR_BIT; });
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected(size_iter,
size_iter + t.num_rows());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
std::unique_ptr<cudf::column> build_nested_column1(std::vector<bool> const& struct_validity)
{
// tests the "branching" case -> list<struct<list> ...>>>
// List<Struct<List<int>, float, int16>
// Inner list column
cudf::test::lists_column_wrapper<int> list{{1, 2, 3, 4, 5},
{6, 7, 8},
{33, 34, 35, 36, 37, 38, 39},
{-1, -2},
{-10, -11, -1, -20},
{40, 41, 42},
{100, 200, 300},
{-100, -200, -300}};
// floats
std::vector<float> ages{5, 10, 15, 20, 4, 75, 16, -16};
std::vector<bool> ages_validity = {1, 1, 1, 1, 0, 1, 0, 1};
auto ages_column =
cudf::test::fixed_width_column_wrapper<float>(ages.begin(), ages.end(), ages_validity.begin());
// int16 values
std::vector<int16_t> vals{-1, -2, -3, 1, 2, 3, 8, 9};
auto i16_column = cudf::test::fixed_width_column_wrapper<int16_t>(vals.begin(), vals.end());
// Assemble struct column
auto struct_column =
cudf::test::structs_column_wrapper({list, ages_column, i16_column}, struct_validity);
// wrap in a list
std::vector<int> outer_offsets{0, 1, 1, 3, 6, 7, 8};
cudf::test::fixed_width_column_wrapper<int> outer_offsets_col(outer_offsets.begin(),
outer_offsets.end());
auto const size = static_cast<cudf::column_view>(outer_offsets_col).size() - 1;
// Each struct (list child) has size:
// (1 offset (4 bytes) + (list size if row is valid) + 1 validity bit) +
// (1 float + 1 validity bit) +
// (1 int16_t + 1 validity bit) +
// (1 validity bit)
// Each top level list has size:
// 1 offset (4 bytes) + (list size if row is valid).
return cudf::make_lists_column(static_cast<cudf::size_type>(size),
outer_offsets_col.release(),
struct_column.release(),
0,
rmm::device_buffer{});
}
std::unique_ptr<cudf::column> build_nested_column2(std::vector<bool> const& struct_validity)
{
// List<Struct<List<List<int>>, Struct<int16>>>
// Inner list column
// clang-format off
cudf::test::lists_column_wrapper<int> list{
{{1, 2, 3, 4, 5}, {2, 3}},
{{6, 7, 8}, {8, 9}},
{{1, 2}, {3, 4, 5}, {33, 34, 35, 36, 37, 38, 39}}};
// clang-format on
// Inner struct
std::vector<int16_t> vals{-1, -2, -3};
auto i16_column = cudf::test::fixed_width_column_wrapper<int16_t>(vals.begin(), vals.end());
auto inner_struct = cudf::test::structs_column_wrapper({i16_column});
// outer struct
auto outer_struct = cudf::test::structs_column_wrapper({list, inner_struct}, struct_validity);
// wrap in a list
std::vector<int> outer_offsets{0, 1, 1, 3};
cudf::test::fixed_width_column_wrapper<int> outer_offsets_col(outer_offsets.begin(),
outer_offsets.end());
auto const size = static_cast<cudf::column_view>(outer_offsets_col).size() - 1;
return make_lists_column(static_cast<cudf::size_type>(size),
outer_offsets_col.release(),
outer_struct.release(),
0,
rmm::device_buffer{});
}
TEST_F(RowBitCount, NestedTypes)
{
// List<Struct<List<int>, float, List<int>, int16>
{
auto const col_no_nulls = build_nested_column1({1, 1, 1, 1, 1, 1, 1, 1});
auto const expected_sizes_no_nulls =
cudf::test::fixed_width_column_wrapper<cudf::size_type>{276, 32, 520, 572, 212, 212}
.release();
cudf::table_view no_nulls_t({*col_no_nulls});
auto no_nulls_result = cudf::row_bit_count(no_nulls_t);
auto const col_nulls = build_nested_column1({0, 0, 1, 1, 1, 1, 1, 1});
auto const expected_sizes_with_nulls =
cudf::test::fixed_width_column_wrapper<cudf::size_type>{116, 32, 424, 572, 212, 212}
.release();
cudf::table_view nulls_t({*col_nulls});
auto nulls_result = cudf::row_bit_count(nulls_t);
// List<Struct<List<int>, float, int16>
//
// this illustrates the difference between a row_bit_count
// returning a pre-gather result, or a post-gather result.
//
// in a post-gather situation, the nulls in the struct would result in the values
// nested in the list below to be dropped, resulting in smaller row sizes.
//
// however, for performance reasons, row_bit_count simply walks the data that is
// currently there. so list rows that are null, but have a real span of
// offsets (X, Y) instead of (X, X) will end up getting the child data for those
// rows included.
//
// if row_bit_count() is changed to return a post-gather result (which may be desirable),
// the nulls_result case below will start failing and will need to be changed.
//
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_sizes_no_nulls, *no_nulls_result);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_sizes_with_nulls, *nulls_result);
}
// List<Struct<List<List<int>>, Struct<int16>>>
{
auto col_no_nulls = build_nested_column2({1, 1, 1});
cudf::table_view no_nulls_t({*col_no_nulls});
auto no_nulls_result = cudf::row_bit_count(no_nulls_t);
auto col_nulls = build_nested_column2({1, 0, 1});
cudf::table_view nulls_t({*col_nulls});
auto nulls_result = cudf::row_bit_count(nulls_t);
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected_sizes_no_nuls{372, 32, 840};
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected_sizes_with_nuls{372, 32, 616};
// same explanation as above
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_sizes_no_nuls, *no_nulls_result);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_sizes_with_nuls, *nulls_result);
}
// test pushing/popping multiple times within one struct, and branch depth > 1
//
// Struct<int, List<int>, float, List<List<int16>>, Struct<List<int>, List<Struct<List<int>,
// float>>, int8_t>>
{
cudf::test::lists_column_wrapper<int> l0{{1, 2, 3}, {4, 5}, {6, 7, 8, 9}, {5}};
cudf::test::lists_column_wrapper<int16_t> l1{
{{-1, -2}, {3, 4}}, {{4, 5}, {6, 7, 8}}, {{-6, -7}, {2}}, {{-11, -11}, {-12, -12}, {3}}};
cudf::test::lists_column_wrapper<int> l2{{-1, -2}, {4, 5}, {-6, -7}, {1}};
cudf::test::lists_column_wrapper<int> l3{{-1, -2, 0}, {5}, {-1, -6, -7}, {1, 2}};
cudf::test::fixed_width_column_wrapper<int> c0{1, 2, 3, 4};
cudf::test::fixed_width_column_wrapper<float> c1{1, 2, 3, 4};
cudf::test::fixed_width_column_wrapper<int8_t> c2{1, 2, 3, 4};
cudf::test::fixed_width_column_wrapper<float> c3{11, 12, 13, 14};
// innermost List<Struct<List<int>>>
auto innermost_struct = cudf::test::structs_column_wrapper({l3, c3});
std::vector<int> l4_offsets{0, 1, 2, 3, 4};
cudf::test::fixed_width_column_wrapper<int> l4_offsets_col(l4_offsets.begin(),
l4_offsets.end());
auto const l4_size = l4_offsets.size() - 1;
auto l4 = cudf::make_lists_column(static_cast<cudf::size_type>(l4_size),
l4_offsets_col.release(),
innermost_struct.release(),
0,
rmm::device_buffer{});
// inner struct
std::vector<std::unique_ptr<cudf::column>> inner_struct_children;
inner_struct_children.push_back(l2.release());
inner_struct_children.push_back(std::move(l4));
auto inner_struct = cudf::test::structs_column_wrapper(std::move(inner_struct_children));
// outer struct
auto struct_col = cudf::test::structs_column_wrapper({c0, l0, c1, l1, inner_struct, c2});
cudf::table_view t({struct_col});
auto result = cudf::row_bit_count(t);
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected_sizes{648, 568, 664, 568};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_sizes, *result);
}
}
TEST_F(RowBitCount, NullsInStringsList)
{
using offsets_wrapper = cudf::test::fixed_width_column_wrapper<cudf::size_type>;
// clang-format off
auto strings = std::vector<std::string>{ "daïs", "def", "", "z", "bananas", "warp", "", "zing" };
auto valids = std::vector<bool>{ 1, 0, 0, 1, 0, 1, 1, 1 };
// clang-format on
cudf::test::strings_column_wrapper col(strings.begin(), strings.end(), valids.begin());
auto offsets = cudf::test::fixed_width_column_wrapper<int>{0, 2, 4, 6, 8};
auto lists_col = cudf::make_lists_column(
4,
offsets_wrapper{0, 2, 4, 6, 8}.release(),
cudf::test::strings_column_wrapper{strings.begin(), strings.end(), valids.begin()}.release(),
0,
{});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(
cudf::row_bit_count(cudf::table_view{{lists_col->view()}})->view(),
cudf::test::fixed_width_column_wrapper<cudf::size_type>{138, 106, 130, 130});
}
TEST_F(RowBitCount, EmptyChildColumnInListOfStrings)
{
// Test with a list<string> column with 4 empty list rows.
// Note: Since there are no strings in any of the lists,
// the lists column's child can be empty.
auto offsets = cudf::test::fixed_width_column_wrapper<cudf::size_type>{0, 0, 0, 0, 0};
auto lists_col = cudf::make_lists_column(
4, offsets.release(), cudf::make_empty_column(cudf::data_type{cudf::type_id::STRING}), 0, {});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(
cudf::row_bit_count(cudf::table_view{{lists_col->view()}})->view(),
cudf::test::fixed_width_column_wrapper<cudf::size_type>{32, 32, 32, 32});
}
TEST_F(RowBitCount, EmptyChildColumnInListOfLists)
{
// Test with a list<list> column with 4 empty list rows.
// Note: Since there are no elements in any of the lists,
// the lists column's child can be empty.
auto empty_child_lists_column = [] {
auto exemplar = cudf::test::lists_column_wrapper<int32_t>{{0, 1, 2}, {3, 4, 5}};
return cudf::empty_like(exemplar);
};
auto offsets = cudf::test::fixed_width_column_wrapper<cudf::size_type>{0, 0, 0, 0, 0};
auto lists_col = cudf::make_lists_column(4, offsets.release(), empty_child_lists_column(), 0, {});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(
cudf::row_bit_count(cudf::table_view{{lists_col->view()}})->view(),
cudf::test::fixed_width_column_wrapper<cudf::size_type>{32, 32, 32, 32});
}
struct sum_functor {
cudf::size_type const* s0;
cudf::size_type const* s1;
cudf::size_type const* s2;
cudf::size_type operator() __device__(int i) { return s0[i] + s1[i] + s2[i]; }
};
TEST_F(RowBitCount, Table)
{
// complex nested column
auto col0 = build_nested_column1({1, 1, 1, 1, 1, 1, 1, 1});
auto col0_sizes =
cudf::test::fixed_width_column_wrapper<cudf::size_type>{276, 32, 520, 572, 212, 212}.release();
// struct column
auto [col1, col1_sizes] = build_struct_column();
// list column
auto [col2, col2_sizes] = build_list_column<int16_t>();
cudf::table_view t({*col0, *col1, *col2});
auto result = cudf::row_bit_count(t);
// sum all column sizes
cudf::column_view cv0 = static_cast<cudf::column_view>(*col0_sizes);
cudf::column_view cv1 = static_cast<cudf::column_view>(*col1_sizes);
cudf::column_view cv2 = static_cast<cudf::column_view>(*col2_sizes);
auto expected =
cudf::make_fixed_width_column(cudf::data_type{cudf::type_id::INT32}, t.num_rows());
cudf::mutable_column_view mcv(*expected);
thrust::transform(
rmm::exec_policy(cudf::get_default_stream()),
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(0) + t.num_rows(),
mcv.begin<cudf::size_type>(),
sum_functor{
cv0.data<cudf::size_type>(), cv1.data<cudf::size_type>(), cv2.data<cudf::size_type>()});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *result);
}
TEST_F(RowBitCount, DepthJump)
{
// jump more than 1 branch depth.
using T = int;
// struct<list<struct<list<int>>, int>
// the jump occurs from depth 2 (the leafmost int column)
// to depth 0 (the topmost int column)
cudf::test::fixed_width_column_wrapper<T> ____c0{1, 2, 3, 5, 5, 6, 7, 8};
cudf::test::fixed_width_column_wrapper<cudf::size_type> ___offsets{0, 2, 4, 6, 8};
auto ___c0 = cudf::make_lists_column(4, ___offsets.release(), ____c0.release(), 0, {});
std::vector<std::unique_ptr<cudf::column>> __children;
__children.push_back(std::move(___c0));
cudf::test::structs_column_wrapper __c0(std::move(__children));
cudf::test::fixed_width_column_wrapper<cudf::size_type> _offsets{0, 3, 4};
auto _c0 = cudf::make_lists_column(2, _offsets.release(), __c0.release(), 0, {});
cudf::test::fixed_width_column_wrapper<int> _c1{3, 4};
std::vector<std::unique_ptr<cudf::column>> children;
children.push_back(std::move(_c0));
children.push_back(_c1.release());
cudf::test::structs_column_wrapper c0(std::move(children));
cudf::table_view t({c0});
auto result = cudf::row_bit_count(t);
// expected size = (num rows at level 1 + num_rows at level 2) + (# values the leaf int column) +
// 1 (value in topmost int column)
constexpr cudf::size_type offset_size = sizeof(cudf::size_type) * CHAR_BIT;
constexpr cudf::size_type type_size = sizeof(T) * CHAR_BIT;
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected{
((1 + 3) * offset_size) + (6 * type_size) + (1 * type_size),
((1 + 1) * offset_size) + (2 * type_size) + (1 * type_size)};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(RowBitCount, SlicedColumnsFixedWidth)
{
auto const slice_size = 7;
cudf::test::fixed_width_column_wrapper<int16_t> c0_unsliced{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
auto c0 = cudf::slice(c0_unsliced, {2, 2 + slice_size});
cudf::table_view t({c0});
auto result = cudf::row_bit_count(t);
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected{16, 16, 16, 16, 16, 16, 16};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(RowBitCount, SlicedColumnsStrings)
{
auto const slice_size = 7;
std::vector<std::string> strings{
"banana", "metric", "imperial", "abc", "daïs", "", "fire", "def", "cudf", "xyzw"};
cudf::test::strings_column_wrapper c0_unsliced(strings.begin(), strings.end());
auto c0 = cudf::slice(c0_unsliced, {3, 3 + slice_size});
cudf::table_view t({c0});
auto result = cudf::row_bit_count(t);
// expect 1 offset (4 bytes) + length of string per row
auto size_iter = cudf::detail::make_counting_transform_iterator(0, [&strings](int i) {
return (static_cast<cudf::size_type>(strings[i].size()) + sizeof(cudf::size_type)) * CHAR_BIT;
});
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected(size_iter + 3,
size_iter + 3 + slice_size);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(RowBitCount, SlicedColumnsLists)
{
auto const slice_size = 2;
cudf::test::lists_column_wrapper<cudf::string_view> c0_unsliced{
{{"banana", "v"}, {"cats"}},
{{"dogs", "yay"}, {"xyz", ""}, {"daïs"}},
{{"fast", "parrot"}, {"orange"}},
{{"blue"}, {"red", "yellow"}, {"ultraviolet", "", "green"}}};
auto c0 = cudf::slice(c0_unsliced, {1, 1 + slice_size});
cudf::table_view t({c0});
auto result = cudf::row_bit_count(t);
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected{408, 320};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(RowBitCount, SlicedColumnsStructs)
{
auto const slice_size = 7;
cudf::test::fixed_width_column_wrapper<int16_t> c0{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<std::string> strings{
"banana", "metric", "imperial", "abc", "daïs", "", "fire", "def", "cudf", "xyzw"};
cudf::test::strings_column_wrapper c1(strings.begin(), strings.end());
auto struct_col_unsliced = cudf::test::structs_column_wrapper({c0, c1});
auto struct_col = cudf::slice(struct_col_unsliced, {3, 3 + slice_size});
cudf::table_view t({struct_col});
auto result = cudf::row_bit_count(t);
// expect 1 offset (4 bytes) + length of string per row + 1 int16_t per row
auto size_iter = cudf::detail::make_counting_transform_iterator(0, [&strings](int i) {
return (static_cast<cudf::size_type>(strings[i].size()) + sizeof(cudf::size_type) +
sizeof(int16_t)) *
CHAR_BIT;
});
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected(size_iter + 3,
size_iter + 3 + slice_size);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
TEST_F(RowBitCount, EmptyTable)
{
{
cudf::table_view empty;
auto result = cudf::row_bit_count(empty);
EXPECT_TRUE(result != nullptr && result->size() == 0);
}
{
auto strings = cudf::make_empty_column(cudf::type_id::STRING);
auto ints = cudf::make_empty_column(cudf::type_id::INT32);
cudf::table_view empty({*strings, *ints});
auto result = cudf::row_bit_count(empty);
EXPECT_TRUE(result != nullptr && result->size() == 0);
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/transform/nans_to_null_test.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/transform.hpp>
#include <cudf/types.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
template <typename T>
struct NaNsToNullTest : public cudf::test::BaseFixture {
void run_test(cudf::column_view const& input, cudf::column_view const& expected)
{
auto [null_mask, null_count] = cudf::nans_to_nulls(input);
cudf::column got(input);
got.set_null_mask(std::move(*null_mask), null_count);
EXPECT_EQ(expected.null_count(), null_count);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got.view());
}
std::unique_ptr<cudf::column> create_expected(std::vector<T> const& input,
std::vector<bool> const& mask = {})
{
std::vector<T> expected(input);
std::vector<bool> expected_mask;
if (mask.size() > 0) {
std::transform(input.begin(),
input.end(),
mask.begin(),
std::back_inserter(expected_mask),
[](T val, bool validity) { return validity and not std::isnan(val); });
} else {
std::transform(input.begin(), input.end(), std::back_inserter(expected_mask), [](T val) {
return not std::isnan(val);
});
}
return cudf::test::fixed_width_column_wrapper<T>(
expected.begin(), expected.end(), expected_mask.begin())
.release();
}
};
using test_types = ::testing::Types<float, double>;
TYPED_TEST_SUITE(NaNsToNullTest, test_types);
TYPED_TEST(NaNsToNullTest, WithMask)
{
using T = TypeParam;
std::vector<T> input = {1, NAN, 3, NAN, 5, NAN};
std::vector<bool> mask = {1, 1, 1, 1, 0, 0};
auto input_column =
cudf::test::fixed_width_column_wrapper<T>(input.begin(), input.end(), mask.begin());
auto expected_column = this->create_expected(input, mask);
this->run_test(input_column, expected_column->view());
}
TYPED_TEST(NaNsToNullTest, WithNoMask)
{
using T = TypeParam;
std::vector<T> input = {1, NAN, 3, NAN, 5, NAN};
auto input_column = cudf::test::fixed_width_column_wrapper<T>(input.begin(), input.end());
auto expected_column = this->create_expected(input);
this->run_test(input_column, expected_column->view());
}
TYPED_TEST(NaNsToNullTest, NoNANWithMask)
{
using T = TypeParam;
std::vector<T> input = {1, 2, 3, 4, 5, 6};
std::vector<bool> mask = {1, 1, 1, 1, 0, 0};
auto input_column =
cudf::test::fixed_width_column_wrapper<T>(input.begin(), input.end(), mask.begin());
auto expected_column = this->create_expected(input, mask);
this->run_test(input_column, expected_column->view());
}
TYPED_TEST(NaNsToNullTest, NoNANNoMask)
{
using T = TypeParam;
std::vector<T> input = {1, 2, 3, 4, 5, 6};
auto input_column = cudf::test::fixed_width_column_wrapper<T>(input.begin(), input.end());
auto expected_column = this->create_expected(input);
this->run_test(input_column, expected_column->view());
}
TYPED_TEST(NaNsToNullTest, EmptyColumn)
{
using T = TypeParam;
std::vector<T> input = {};
auto input_column = cudf::test::fixed_width_column_wrapper<T>(input.begin(), input.end());
auto expected_column = this->create_expected(input);
this->run_test(input_column, expected_column->view());
}
struct NaNsToNullFailTest : public cudf::test::BaseFixture {};
TEST_F(NaNsToNullFailTest, StringType)
{
std::vector<std::string> strings{
"", "this", "is", "a", "column", "of", "strings", "with", "in", "valid"};
cudf::test::strings_column_wrapper input(strings.begin(), strings.end());
EXPECT_THROW(cudf::nans_to_nulls(input), cudf::logic_error);
}
TEST_F(NaNsToNullFailTest, IntegerType)
{
std::vector<int32_t> input = {1, 2, 3, 4, 5, 6};
auto input_column = cudf::test::fixed_width_column_wrapper<int32_t>(input.begin(), input.end());
EXPECT_THROW(cudf::nans_to_nulls(input_column), cudf::logic_error);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests/transform
|
rapidsai_public_repos/cudf/cpp/tests/transform/integration/assert_unary.h
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/cudf_gtest.hpp>
namespace transformation {
template <typename TypeOut, typename TypeIn, typename TypeOpe>
void ASSERT_UNARY(cudf::column_view const& out, cudf::column_view const& in, TypeOpe&& ope)
{
auto in_h = cudf::test::to_host<TypeIn>(in);
auto in_data = in_h.first;
auto out_h = cudf::test::to_host<TypeOut>(out);
auto out_data = out_h.first;
ASSERT_TRUE(out_data.size() == in_data.size());
auto data_comparator = [ope](TypeIn const& in, TypeOut const& out) {
EXPECT_EQ(out, static_cast<TypeOut>(ope(in)));
return true;
};
std::equal(in_data.begin(), in_data.end(), out_data.begin(), data_comparator);
auto in_valid = in_h.second;
auto out_valid = out_h.second;
ASSERT_TRUE(out_valid.size() == in_valid.size());
auto valid_comparator = [](bool const& in, bool const& out) {
EXPECT_EQ(out, in);
return true;
};
std::equal(in_valid.begin(), in_valid.end(), out_valid.begin(), valid_comparator);
}
} // namespace transformation
| 0 |
rapidsai_public_repos/cudf/cpp/tests/transform
|
rapidsai_public_repos/cudf/cpp/tests/transform/integration/unary_transform_test.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Copyright 2018-2019 BlazingDB, Inc.
* Copyright 2018 Christian Noboa Mardini <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "assert_unary.h"
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/transform.hpp>
namespace transformation {
struct UnaryOperationIntegrationTest : public cudf::test::BaseFixture {};
template <class dtype, class Op, class Data>
void test_udf(char const udf[], Op op, Data data_init, cudf::size_type size, bool is_ptx)
{
auto all_valid = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return true; });
auto data_iter = cudf::detail::make_counting_transform_iterator(0, data_init);
cudf::test::fixed_width_column_wrapper<dtype, typename decltype(data_iter)::value_type> in(
data_iter, data_iter + size, all_valid);
std::unique_ptr<cudf::column> out =
cudf::transform(in, udf, cudf::data_type(cudf::type_to_id<dtype>()), is_ptx);
ASSERT_UNARY<dtype, dtype>(out->view(), in, op);
}
TEST_F(UnaryOperationIntegrationTest, Transform_FP32_FP32)
{
// c = a*a*a*a
char const* cuda =
R"***(
__device__ inline void fdsf (
float* C,
float a
)
{
*C = a*a*a*a;
}
)***";
char const* ptx =
R"***(
//
// Generated by NVIDIA NVVM Compiler
//
// Compiler Build ID: CL-24817639
// Cuda compilation tools, release 10.0, V10.0.130
// Based on LLVM 3.4svn
//
.version 6.3
.target sm_70
.address_size 64
// .globl _ZN8__main__7add$241Ef
.common .global .align 8 .u64 _ZN08NumbaEnv8__main__7add$241Ef;
.common .global .align 8 .u64 _ZN08NumbaEnv5numba7targets7numbers14int_power_impl12$3clocals$3e13int_power$242Efx;
.visible .func (.param .b32 func_retval0) _ZN8__main__7add$241Ef(
.param .b64 _ZN8__main__7add$241Ef_param_0,
.param .b32 _ZN8__main__7add$241Ef_param_1
)
{
.reg .f32 %f<4>;
.reg .b32 %r<2>;
.reg .b64 %rd<2>;
ld.param.u64 %rd1, [_ZN8__main__7add$241Ef_param_0];
ld.param.f32 %f1, [_ZN8__main__7add$241Ef_param_1];
mul.f32 %f2, %f1, %f1;
mul.f32 %f3, %f2, %f2;
st.f32 [%rd1], %f3;
mov.u32 %r1, 0;
st.param.b32 [func_retval0+0], %r1;
ret;
}
)***";
using dtype = float;
auto op = [](dtype a) { return a * a * a * a; };
auto data_init = [](cudf::size_type row) { return row % 3; };
test_udf<dtype>(cuda, op, data_init, 500, false);
test_udf<dtype>(ptx, op, data_init, 500, true);
}
TEST_F(UnaryOperationIntegrationTest, Transform_INT32_INT32)
{
// c = a * a - a
char const cuda[] =
"__device__ inline void f(int* output,int input){*output = input*input - input;}";
char const* ptx =
R"***(
.func _Z1fPii(
.param .b64 _Z1fPii_param_0,
.param .b32 _Z1fPii_param_1
)
{
.reg .b32 %r<4>;
.reg .b64 %rd<3>;
ld.param.u64 %rd1, [_Z1fPii_param_0];
ld.param.u32 %r1, [_Z1fPii_param_1];
cvta.to.global.u64 %rd2, %rd1;
mul.lo.s32 %r2, %r1, %r1;
sub.s32 %r3, %r2, %r1;
st.global.u32 [%rd2], %r3;
ret;
}
)***";
using dtype = int;
auto op = [](dtype a) { return a * a - a; };
auto data_init = [](cudf::size_type row) { return row % 78; };
test_udf<dtype>(cuda, op, data_init, 500, false);
test_udf<dtype>(ptx, op, data_init, 500, true);
}
TEST_F(UnaryOperationIntegrationTest, Transform_INT8_INT8)
{
// Capitalize all the lower case letters
// Assuming ASCII, the PTX code is compiled from the following CUDA code
char const cuda[] =
R"***(
__device__ inline void f(
signed char* output,
signed char input
){
if(input > 96 && input < 123){
*output = input - 32;
}else{
*output = input;
}
}
)***";
char const ptx[] =
R"***(
.func _Z1fPcc(
.param .b64 _Z1fPcc_param_0,
.param .b32 _Z1fPcc_param_1
)
{
.reg .pred %p<2>;
.reg .b16 %rs<6>;
.reg .b32 %r<3>;
.reg .b64 %rd<3>;
ld.param.u64 %rd1, [_Z1fPcc_param_0];
cvta.to.global.u64 %rd2, %rd1;
ld.param.s8 %rs1, [_Z1fPcc_param_1];
add.s16 %rs2, %rs1, -97;
and.b16 %rs3, %rs2, 255;
setp.lt.u16 %p1, %rs3, 26;
cvt.u32.u16 %r1, %rs1;
add.s32 %r2, %r1, 224;
cvt.u16.u32 %rs4, %r2;
selp.b16 %rs5, %rs4, %rs1, %p1;
st.global.u8 [%rd2], %rs5;
ret;
}
)***";
using dtype = int8_t;
auto op = [](dtype a) { return std::toupper(a); };
auto data_init = [](cudf::size_type row) { return 'a' + (row % 26); };
test_udf<dtype>(cuda, op, data_init, 500, false);
test_udf<dtype>(ptx, op, data_init, 500, true);
}
TEST_F(UnaryOperationIntegrationTest, Transform_Datetime)
{
// Add one day to timestamp in microseconds
char const cuda[] =
R"***(
__device__ inline void f(cudf::timestamp_us* output, cudf::timestamp_us input)
{
using dur = cuda::std::chrono::duration<int32_t, cuda::std::ratio<86400>>;
*output = static_cast<cudf::timestamp_us>(input + dur{1});
}
)***";
using dtype = cudf::timestamp_us;
auto op = [](dtype a) {
using dur = cuda::std::chrono::duration<int32_t, cuda::std::ratio<86400>>;
return static_cast<cudf::timestamp_us>(a + dur{1});
};
auto random_eng = cudf::test::UniformRandomGenerator<cudf::timestamp_us::rep>(0, 100000000);
auto data_init = [&random_eng](cudf::size_type row) { return random_eng.generate(); };
test_udf<dtype>(cuda, op, data_init, 500, false);
}
} // namespace transformation
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/filling/fill_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/filling.hpp>
#include <cudf/scalar/scalar.hpp>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/traits.hpp>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/counting_iterator.h>
auto all_valid = [](cudf::size_type row) { return true; };
auto odd_valid = [](cudf::size_type row) { return row % 2 != 0; };
auto all_invalid = [](cudf::size_type row) { return false; };
template <typename T>
class FillTypedTestFixture : public cudf::test::BaseFixture {
public:
static constexpr cudf::size_type column_size{1000};
template <typename BitInitializerType = decltype(all_valid)>
void test(cudf::size_type begin,
cudf::size_type end,
T value,
bool value_is_valid = true,
BitInitializerType destination_validity = all_valid)
{
static_assert(cudf::is_fixed_width<T>(), "this code assumes fixed-width types.");
cudf::size_type size{FillTypedTestFixture<T>::column_size};
cudf::test::fixed_width_column_wrapper<T, int32_t> destination(
thrust::make_counting_iterator(0),
thrust::make_counting_iterator(0) + size,
cudf::detail::make_counting_transform_iterator(0, destination_validity));
std::unique_ptr<cudf::scalar> p_val{nullptr};
cudf::data_type type{cudf::type_to_id<T>()};
if (cudf::is_numeric<T>()) {
p_val = cudf::make_numeric_scalar(type);
} else if (cudf::is_timestamp<T>()) {
p_val = cudf::make_timestamp_scalar(type);
} else if (cudf::is_duration<T>()) {
p_val = cudf::make_duration_scalar(type);
} else {
ASSERT_TRUE(false); // should not be reached
}
using ScalarType = cudf::scalar_type_t<T>;
static_cast<ScalarType*>(p_val.get())->set_value(value);
static_cast<ScalarType*>(p_val.get())->set_valid_async(value_is_valid);
auto expected_elements =
cudf::detail::make_counting_transform_iterator(0, [begin, end, value](auto i) {
return (i >= begin && i < end) ? value : cudf::test::make_type_param_scalar<T>(i);
});
cudf::test::fixed_width_column_wrapper<T> expected(
expected_elements,
expected_elements + size,
cudf::detail::make_counting_transform_iterator(
0, [begin, end, value_is_valid, destination_validity](auto i) {
return (i >= begin && i < end) ? value_is_valid : destination_validity(i);
}));
// test out-of-place version first
auto p_ret = cudf::fill(destination, begin, end, *p_val);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*p_ret, expected);
// test in-place version second
cudf::mutable_column_view mutable_view{destination};
EXPECT_NO_THROW(cudf::fill_in_place(mutable_view, begin, end, *p_val));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(mutable_view, expected);
}
};
TYPED_TEST_SUITE(FillTypedTestFixture, cudf::test::FixedWidthTypesWithoutFixedPoint);
TYPED_TEST(FillTypedTestFixture, SetSingle)
{
using T = TypeParam;
cudf::size_type index{9};
T value = cudf::test::make_type_param_scalar<TypeParam>(1);
// First set it as valid
this->test(index, index + 1, value, true);
// next set it as invalid
this->test(index, index + 1, value, false);
}
TYPED_TEST(FillTypedTestFixture, SetAll)
{
using T = TypeParam;
cudf::size_type size{FillTypedTestFixture<T>::column_size};
T value = cudf::test::make_type_param_scalar<TypeParam>(1);
// First set it as valid
this->test(0, size, value, true);
// next set it as invalid
this->test(0, size, value, false);
}
TYPED_TEST(FillTypedTestFixture, SetRange)
{
using T = TypeParam;
cudf::size_type begin{99};
cudf::size_type end{299};
T value = cudf::test::make_type_param_scalar<TypeParam>(1);
// First set it as valid
this->test(begin, end, value, true);
// Next set it as invalid
this->test(begin, end, value, false);
}
TYPED_TEST(FillTypedTestFixture, SetRangeNullCount)
{
using T = TypeParam;
cudf::size_type size{FillTypedTestFixture<T>::column_size};
cudf::size_type begin{10};
cudf::size_type end{50};
T value = cudf::test::make_type_param_scalar<TypeParam>(1);
// First set it as valid value
this->test(begin, end, value, true, odd_valid);
// Next set it as invalid
this->test(begin, end, value, false, odd_valid);
// All invalid column should have some valid
this->test(begin, end, value, true, all_invalid);
// All should be invalid
this->test(begin, end, value, false, all_invalid);
// All should be valid
this->test(0, size, value, true, odd_valid);
}
class FillStringTestFixture : public cudf::test::BaseFixture {
public:
static constexpr cudf::size_type column_size{100};
template <typename BitInitializerType = decltype(all_valid)>
void test(cudf::size_type begin,
cudf::size_type end,
std::string value,
bool value_is_valid = true,
BitInitializerType destination_validity = all_valid)
{
cudf::size_type size{FillStringTestFixture::column_size};
auto destination_elements = cudf::detail::make_counting_transform_iterator(
0, [](auto i) { return "#" + std::to_string(i); });
auto destination = cudf::test::strings_column_wrapper(
destination_elements,
destination_elements + size,
cudf::detail::make_counting_transform_iterator(0, destination_validity));
auto p_val = cudf::make_string_scalar(value);
using ScalarType = cudf::scalar_type_t<cudf::string_view>;
static_cast<ScalarType*>(p_val.get())->set_valid_async(value_is_valid);
auto p_chars = value.c_str();
auto num_chars = value.length();
auto expected_elements =
cudf::detail::make_counting_transform_iterator(0, [begin, end, p_chars, num_chars](auto i) {
return (i >= begin && i < end) ? std::string(p_chars, num_chars) : "#" + std::to_string(i);
});
auto expected = cudf::test::strings_column_wrapper(
expected_elements,
expected_elements + size,
cudf::detail::make_counting_transform_iterator(
0, [begin, end, value_is_valid, destination_validity](auto i) {
return (i >= begin && i < end) ? value_is_valid : destination_validity(i);
}));
auto p_ret = cudf::fill(destination, begin, end, *p_val);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*p_ret, expected);
}
};
TEST_F(FillStringTestFixture, SetSingle)
{
cudf::size_type size{FillStringTestFixture::column_size};
cudf::size_type index{9};
auto value = "#" + std::to_string(size * 2);
// First set it as valid
this->test(index, index + 1, value, true);
// next set it as invalid
this->test(index, index + 1, value, false);
}
TEST_F(FillStringTestFixture, SetAll)
{
cudf::size_type size{FillStringTestFixture::column_size};
auto value = "#" + std::to_string(size * 2);
// First set it as valid
this->test(0, size, value, true);
// next set it as invalid
this->test(0, size, value, false);
}
TEST_F(FillStringTestFixture, SetRange)
{
cudf::size_type size{FillStringTestFixture::column_size};
cudf::size_type begin{9};
cudf::size_type end{99};
auto value = "#" + std::to_string(size * 2);
// First set it as valid
this->test(begin, end, value, true);
// Next set it as invalid
this->test(begin, end, value, false);
}
TEST_F(FillStringTestFixture, SetRangeNullCount)
{
cudf::size_type size{FillStringTestFixture::column_size};
cudf::size_type begin{10};
cudf::size_type end{50};
auto value = "#" + std::to_string(size * 2);
// First set it as valid value
this->test(begin, end, value, true, odd_valid);
// Next set it as invalid
this->test(begin, end, value, false, odd_valid);
// All invalid column should have some valid
this->test(begin, end, value, true, all_invalid);
// All should be invalid
this->test(begin, end, value, false, all_invalid);
// All should be valid
this->test(0, size, value, true, odd_valid);
}
class FillErrorTestFixture : public cudf::test::BaseFixture {};
TEST_F(FillErrorTestFixture, InvalidInplaceCall)
{
auto p_val_int = cudf::make_numeric_scalar(cudf::data_type(cudf::type_id::INT32));
using T_int = cudf::id_to_type<cudf::type_id::INT32>;
using ScalarType = cudf::scalar_type_t<T_int>;
static_cast<ScalarType*>(p_val_int.get())->set_value(5);
static_cast<ScalarType*>(p_val_int.get())->set_valid_async(false);
auto destination = cudf::test::fixed_width_column_wrapper<int32_t>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0) + 100);
auto destination_view = cudf::mutable_column_view{destination};
EXPECT_THROW(cudf::fill_in_place(destination_view, 0, 100, *p_val_int), cudf::logic_error);
auto p_val_str = cudf::make_string_scalar("five");
std::vector<std::string> strings{"", "this", "is", "a", "column", "of", "strings"};
auto destination_string = cudf::test::strings_column_wrapper(strings.begin(), strings.end());
cudf::mutable_column_view destination_view_string{destination_string};
EXPECT_THROW(cudf::fill_in_place(destination_view_string, 0, 100, *p_val_str), cudf::logic_error);
}
TEST_F(FillErrorTestFixture, InvalidRange)
{
auto p_val = cudf::make_numeric_scalar(cudf::data_type(cudf::type_id::INT32));
using T = cudf::id_to_type<cudf::type_id::INT32>;
using ScalarType = cudf::scalar_type_t<T>;
static_cast<ScalarType*>(p_val.get())->set_value(5);
auto destination =
cudf::test::fixed_width_column_wrapper<int32_t>(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(0) + 100,
thrust::make_constant_iterator(true));
cudf::mutable_column_view destination_view{destination};
// empty range == no-op, this is valid
EXPECT_NO_THROW(cudf::fill_in_place(destination_view, 0, 0, *p_val));
EXPECT_NO_THROW(auto p_ret = cudf::fill(destination, 0, 0, *p_val));
// out_begin is negative
EXPECT_THROW(cudf::fill_in_place(destination_view, -10, 0, *p_val), cudf::logic_error);
EXPECT_THROW(auto p_ret = cudf::fill(destination, -10, 0, *p_val), cudf::logic_error);
// out_begin > out_end
EXPECT_THROW(cudf::fill_in_place(destination_view, 10, 5, *p_val), cudf::logic_error);
EXPECT_THROW(auto p_ret = cudf::fill(destination, 10, 5, *p_val), cudf::logic_error);
// out_begin > destination.size()
EXPECT_THROW(cudf::fill_in_place(destination_view, 101, 100, *p_val), cudf::logic_error);
EXPECT_THROW(auto p_ret = cudf::fill(destination, 101, 100, *p_val), cudf::logic_error);
// out_end > destination.size()
EXPECT_THROW(cudf::fill_in_place(destination_view, 99, 101, *p_val), cudf::logic_error);
EXPECT_THROW(auto p_ret = cudf::fill(destination, 99, 101, *p_val), cudf::logic_error);
// Empty Column
destination = cudf::test::fixed_width_column_wrapper<int32_t>{};
destination_view = destination;
// empty column, this is valid
EXPECT_NO_THROW(cudf::fill_in_place(destination_view, 0, destination_view.size(), *p_val));
EXPECT_NO_THROW(auto p_ret = cudf::fill(destination, 0, destination_view.size(), *p_val));
}
TEST_F(FillErrorTestFixture, DTypeMismatch)
{
cudf::size_type size{100};
auto p_val = cudf::make_numeric_scalar(cudf::data_type(cudf::type_id::INT32));
using T = cudf::id_to_type<cudf::type_id::INT32>;
using ScalarType = cudf::scalar_type_t<T>;
static_cast<ScalarType*>(p_val.get())->set_value(5);
auto destination = cudf::test::fixed_width_column_wrapper<float>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0) + size);
auto destination_view = cudf::mutable_column_view{destination};
EXPECT_THROW(cudf::fill_in_place(destination_view, 0, 10, *p_val), cudf::logic_error);
EXPECT_THROW(auto p_ret = cudf::fill(destination, 0, 10, *p_val), cudf::logic_error);
}
template <typename T>
class FixedPointAllReps : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(FixedPointAllReps, cudf::test::FixedPointTypes);
TYPED_TEST(FixedPointAllReps, OutOfPlaceFill)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
for (auto const i : {0, -1, -2, -3, -4}) {
auto const scale = scale_type{i};
auto const column = fp_wrapper{{4104, 42, 1729, 55}, scale};
auto const expected = fp_wrapper{{42, 42, 42, 42}, scale};
auto const scalar = cudf::make_fixed_point_scalar<decimalXX>(42, scale);
auto const result = cudf::fill(column, 0, 4, *scalar);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected);
}
}
TYPED_TEST(FixedPointAllReps, InPlaceFill)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
for (auto const i : {0, -1, -2, -3, -4}) {
auto const scale = scale_type{i};
auto column = fp_wrapper{{4104, 42, 1729, 55}, scale};
auto const expected = fp_wrapper{{42, 42, 42, 42}, scale};
auto const scalar = cudf::make_fixed_point_scalar<decimalXX>(42, scale);
auto mut_column = cudf::mutable_column_view{column};
cudf::fill_in_place(mut_column, 0, 4, *scalar);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(column, expected);
}
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/filling/repeat_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/filling.hpp>
#include <cudf/scalar/scalar.hpp>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>
#include <thrust/iterator/constant_iterator.h>
#include <thrust/iterator/counting_iterator.h>
#include <algorithm>
#include <numeric>
#include <random>
constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::ALL_ERRORS};
template <typename T>
class RepeatTypedTestFixture : public cudf::test::BaseFixture,
cudf::test::UniformRandomGenerator<cudf::size_type> {
public:
RepeatTypedTestFixture() : cudf::test::UniformRandomGenerator<cudf::size_type>{0, 10} {}
cudf::size_type repeat_count() { return this->generate(); }
};
TYPED_TEST_SUITE(RepeatTypedTestFixture, cudf::test::FixedWidthTypes);
TYPED_TEST(RepeatTypedTestFixture, RepeatScalarCount)
{
using T = TypeParam;
static_assert(cudf::is_fixed_width<T>(), "this code assumes fixed-width types.");
constexpr cudf::size_type num_values{10};
constexpr cudf::size_type repeat_count{10};
auto input = cudf::test::fixed_width_column_wrapper<T, int32_t>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0) + num_values);
static_assert(repeat_count > 0, "repeat_count should be larger than 0.");
auto expected_elements = cudf::detail::make_counting_transform_iterator(
0, [repeat_count](auto i) { return i / repeat_count; });
auto expected =
cudf::test::fixed_width_column_wrapper<T, typename decltype(expected_elements)::value_type>(
expected_elements, expected_elements + num_values * repeat_count);
auto input_table = cudf::table_view{{input}};
auto const p_ret = cudf::repeat(input_table, repeat_count);
EXPECT_EQ(p_ret->num_columns(), 1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(p_ret->view().column(0), expected, verbosity);
}
TYPED_TEST(RepeatTypedTestFixture, RepeatColumnCount)
{
using T = TypeParam;
static_assert(cudf::is_fixed_width<T>(), "this code assumes fixed-width types.");
constexpr cudf::size_type num_values{10};
std::vector<int64_t> inputs(num_values);
std::iota(inputs.begin(), inputs.end(), 0);
std::vector<cudf::size_type> counts(num_values);
std::transform(counts.begin(), counts.end(), counts.begin(), [&](cudf::size_type count) {
return this->repeat_count();
});
std::vector<T> expected_values;
for (size_t i{0}; i < counts.size(); i++) {
for (cudf::size_type j{0}; j < counts[i]; j++) {
expected_values.push_back(cudf::test::make_type_param_scalar<T>(inputs[i]));
}
}
cudf::test::fixed_width_column_wrapper<T, int64_t> input(inputs.begin(), inputs.end());
auto count =
cudf::test::fixed_width_column_wrapper<cudf::size_type>(counts.begin(), counts.end());
cudf::test::fixed_width_column_wrapper<T> expected(expected_values.begin(),
expected_values.end());
cudf::table_view input_table{{input}};
auto p_ret = cudf::repeat(input_table, count);
EXPECT_EQ(p_ret->num_columns(), 1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(p_ret->view().column(0), expected);
}
TYPED_TEST(RepeatTypedTestFixture, RepeatNullable)
{
using T = TypeParam;
static_assert(cudf::is_fixed_width<T>(), "this code assumes fixed-width types.");
constexpr cudf::size_type num_values{10};
std::vector<int64_t> input_values(num_values);
std::iota(input_values.begin(), input_values.end(), 0);
std::vector<bool> input_valids(num_values);
for (size_t i{0}; i < input_valids.size(); i++) {
input_valids[i] = (i % 2) == 0;
}
std::vector<cudf::size_type> counts(num_values);
std::transform(counts.begin(), counts.end(), counts.begin(), [&](cudf::size_type count) {
return this->repeat_count();
});
std::vector<T> expected_values;
std::vector<bool> expected_valids;
for (size_t i{0}; i < counts.size(); i++) {
for (cudf::size_type j{0}; j < counts[i]; j++) {
expected_values.push_back(cudf::test::make_type_param_scalar<T>(input_values[i]));
expected_valids.push_back(input_valids[i]);
}
}
cudf::test::fixed_width_column_wrapper<T, int64_t> input(
input_values.begin(), input_values.end(), input_valids.begin());
auto count =
cudf::test::fixed_width_column_wrapper<cudf::size_type>(counts.begin(), counts.end());
cudf::test::fixed_width_column_wrapper<T> expected(
expected_values.begin(), expected_values.end(), expected_valids.begin());
cudf::table_view input_table{{input}};
auto p_ret = cudf::repeat(input_table, count);
EXPECT_EQ(p_ret->num_columns(), 1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(p_ret->view().column(0), expected);
}
TYPED_TEST(RepeatTypedTestFixture, ZeroSizeInput)
{
using T = TypeParam;
static_assert(cudf::is_fixed_width<T>(), "this code assumes fixed-width types.");
cudf::test::fixed_width_column_wrapper<T, int32_t> input(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(0));
auto count = cudf::test::fixed_width_column_wrapper<cudf::size_type>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0));
cudf::test::fixed_width_column_wrapper<T, int32_t> expected(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(0));
cudf::table_view input_table{{input}};
auto p_ret = cudf::repeat(input_table, count);
EXPECT_EQ(p_ret->num_columns(), 1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(p_ret->view().column(0), expected);
}
TYPED_TEST(RepeatTypedTestFixture, ZeroCount)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T, int32_t> input(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(10));
auto expected = cudf::make_empty_column(cudf::type_to_id<T>());
cudf::table_view input_table{{input}};
auto p_ret = cudf::repeat(input_table, 0);
EXPECT_EQ(p_ret->num_columns(), 1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(p_ret->view().column(0), expected->view());
}
class RepeatStringTestFixture : public cudf::test::BaseFixture,
cudf::test::UniformRandomGenerator<cudf::size_type> {
public:
RepeatStringTestFixture() : cudf::test::UniformRandomGenerator<cudf::size_type>{0, 10} {}
cudf::size_type repeat_count() { return this->generate(); }
};
TEST_F(RepeatStringTestFixture, RepeatNullable)
{
constexpr cudf::size_type num_values{10};
std::vector<std::string> input_values(num_values);
std::vector<bool> input_valids(num_values);
for (size_t i{0}; i < num_values; i++) {
input_values[i] = "#" + std::to_string(i);
input_valids[i] = (i % 2) == 0;
}
std::vector<cudf::size_type> counts(num_values);
std::transform(counts.begin(), counts.end(), counts.begin(), [&](cudf::size_type count) {
return this->repeat_count();
});
std::vector<std::string> expected_values;
std::vector<bool> expected_valids;
for (size_t i{0}; i < counts.size(); i++) {
for (cudf::size_type j{0}; j < counts[i]; j++) {
expected_values.push_back(input_values[i]);
expected_valids.push_back(input_valids[i]);
}
}
auto input = cudf::test::strings_column_wrapper(
input_values.begin(), input_values.end(), input_valids.begin());
auto count =
cudf::test::fixed_width_column_wrapper<cudf::size_type>(counts.begin(), counts.end());
auto expected = cudf::test::strings_column_wrapper(
expected_values.begin(), expected_values.end(), expected_valids.begin());
cudf::table_view input_table{{input}};
auto p_ret = cudf::repeat(input_table, count);
EXPECT_EQ(p_ret->num_columns(), 1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(p_ret->view().column(0), expected);
}
TEST_F(RepeatStringTestFixture, ZeroSizeInput)
{
std::vector<std::string> input_values{};
auto input = cudf::test::strings_column_wrapper(input_values.begin(), input_values.end());
auto count = cudf::test::fixed_width_column_wrapper<cudf::size_type>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0));
auto expected = cudf::test::strings_column_wrapper(input_values.begin(), input_values.end());
cudf::table_view input_table{{input}};
auto p_ret = cudf::repeat(input_table, count);
EXPECT_EQ(p_ret->num_columns(), 1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(p_ret->view().column(0), expected);
}
class RepeatErrorTestFixture : public cudf::test::BaseFixture {};
TEST_F(RepeatErrorTestFixture, LengthMismatch)
{
auto input = cudf::test::fixed_width_column_wrapper<int32_t>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0) + 100);
auto count = cudf::test::fixed_width_column_wrapper<cudf::size_type>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0) + 200);
cudf::table_view input_table{{input}};
// input_table.num_rows() != count.size()
EXPECT_THROW(auto p_ret = cudf::repeat(input_table, count), cudf::logic_error);
}
TEST_F(RepeatErrorTestFixture, CountHasNulls)
{
auto input = cudf::test::fixed_width_column_wrapper<int32_t>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0) + 100);
auto count =
cudf::test::fixed_width_column_wrapper<cudf::size_type>(thrust::make_counting_iterator(0),
thrust::make_counting_iterator(0) + 100,
thrust::make_constant_iterator(false));
cudf::table_view input_table{{input}};
// input_table.has_nulls() == true
EXPECT_THROW(auto ret = cudf::repeat(input_table, count), cudf::logic_error);
}
TEST_F(RepeatErrorTestFixture, Overflow)
{
auto input = cudf::test::fixed_width_column_wrapper<int32_t>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0) + 100);
cudf::table_view input_table{{input}};
// set the count such that (count * num_rows) > max(size_type);
// the extra divide by 2 ensures the max is exceeded despite truncation in integer division
auto count = std::numeric_limits<cudf::size_type>::max() / (input_table.num_rows() / 2);
EXPECT_THROW(cudf::repeat(input_table, count), std::overflow_error);
}
TEST_F(RepeatErrorTestFixture, NegativeCount)
{
auto input = cudf::test::fixed_width_column_wrapper<int32_t>(
thrust::make_counting_iterator(0), thrust::make_counting_iterator(0) + 100);
cudf::table_view input_table{{input}};
EXPECT_THROW(cudf::repeat(input_table, -1), cudf::logic_error);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/filling/sequence_tests.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/filling.hpp>
#include <cudf/scalar/scalar.hpp>
template <typename T>
class SequenceTypedTestFixture : public cudf::test::BaseFixture {};
class SequenceTestFixture : public cudf::test::BaseFixture {};
using NumericTypesNoBool = cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>;
TYPED_TEST_SUITE(SequenceTypedTestFixture, NumericTypesNoBool);
TYPED_TEST(SequenceTypedTestFixture, Incrementing)
{
using T = TypeParam;
cudf::numeric_scalar<T> init(0);
cudf::numeric_scalar<T> step(1);
cudf::size_type num_els = 10;
T expected[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<T> expected_w(expected, expected + num_els);
auto result = cudf::sequence(num_els, init, step);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected_w);
}
TYPED_TEST(SequenceTypedTestFixture, Decrementing)
{
using T = TypeParam;
cudf::numeric_scalar<T> init(0);
cudf::numeric_scalar<T> step(-5);
cudf::size_type num_els = 10;
T expected[] = {0, -5, -10, -15, -20, -25, -30, -35, -40, -45};
cudf::test::fixed_width_column_wrapper<T> expected_w(expected, expected + num_els);
auto result = cudf::sequence(num_els, init, step);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected_w);
}
TYPED_TEST(SequenceTypedTestFixture, EmptyOutput)
{
using T = TypeParam;
cudf::numeric_scalar<T> init(0);
cudf::numeric_scalar<T> step(-5);
cudf::size_type num_els = 0;
T expected[] = {};
cudf::test::fixed_width_column_wrapper<T> expected_w(expected, expected + num_els);
auto result = cudf::sequence(num_els, init, step);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected_w);
}
TEST_F(SequenceTestFixture, BadTypes)
{
cudf::string_scalar string_init("zero");
cudf::string_scalar string_step("???");
EXPECT_THROW(cudf::sequence(10, string_init, string_step), cudf::logic_error);
cudf::numeric_scalar<bool> bool_init(true);
cudf::numeric_scalar<bool> bool_step(false);
EXPECT_THROW(cudf::sequence(10, bool_init, bool_step), cudf::logic_error);
cudf::timestamp_scalar<cudf::timestamp_s> ts_init(cudf::duration_s{10}, true);
cudf::timestamp_scalar<cudf::timestamp_s> ts_step(cudf::duration_s{10}, true);
EXPECT_THROW(cudf::sequence(10, ts_init, ts_step), cudf::logic_error);
}
TEST_F(SequenceTestFixture, MismatchedInputs)
{
cudf::numeric_scalar<int> init(0);
cudf::numeric_scalar<float> step(-5);
EXPECT_THROW(cudf::sequence(10, init, step), cudf::logic_error);
cudf::numeric_scalar<int> init2(0);
cudf::numeric_scalar<int8_t> step2(-5);
EXPECT_THROW(cudf::sequence(10, init2, step2), cudf::logic_error);
cudf::numeric_scalar<float> init3(0);
cudf::numeric_scalar<double> step3(-5);
EXPECT_THROW(cudf::sequence(10, init3, step3), cudf::logic_error);
}
TYPED_TEST(SequenceTypedTestFixture, DefaultStep)
{
using T = TypeParam;
cudf::numeric_scalar<T> init(0);
cudf::size_type num_els = 10;
T expected[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<T> expected_w(expected, expected + num_els);
auto result = cudf::sequence(num_els, init);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected_w);
}
TEST_F(SequenceTestFixture, DateSequenceBasic)
{
// Timestamp generated using https://www.epochconverter.com/
cudf::timestamp_scalar<cudf::timestamp_s> init(1629852896L, true); // 2021-08-25 00:54:56 GMT
cudf::size_type size{5};
cudf::size_type months{1};
cudf::test::fixed_width_column_wrapper<cudf::timestamp_s, int64_t> expected{
1629852896L, // 2021-08-25 00:54:56 GMT
1632531296L, // 2021-09-25 00:54:56 GMT
1635123296L, // 2021-10-25 00:54:56 GMT
1637801696L, // 2021-11-25 00:54:56 GMT
1640393696L, // 2021-12-25 00:54:56 GMT
};
auto got = calendrical_month_sequence(size, init, months);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *got);
}
TEST_F(SequenceTestFixture, DateSequenceLeapYear)
{
// Timestamp generated using https://www.epochconverter.com/
cudf::timestamp_scalar<cudf::timestamp_s> init(951876379L, true); // 2000-02-29 02:06:19 GMT
cudf::size_type size{5};
cudf::size_type months{12};
cudf::test::fixed_width_column_wrapper<cudf::timestamp_s, int64_t> expected{
951876379L, // 2000-02-29 02:06:19 GMT Leap Year
983412379L, // 2001-02-28 02:06:19 GMT
1014948379L, // 2002-02-28 02:06:19 GMT
1046484379L, // 2003-02-28 02:06:19 GMT
1078106779L, // 2004-02-29 02:06:19 GMT Leap Year
};
auto got = calendrical_month_sequence(size, init, months);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *got);
}
TEST_F(SequenceTestFixture, DateSequenceBadTypes)
{
cudf::numeric_scalar<int64_t> init(951876379, true);
cudf::size_type size = 5;
cudf::size_type months = 12;
EXPECT_THROW(calendrical_month_sequence(size, init, months), cudf::logic_error);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/identify_stream_usage/test_default_stream_identification.cu
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdexcept>
__global__ void kernel() { printf("The kernel ran!\n"); }
void test_cudaLaunchKernel()
{
cudaStream_t stream;
cudaStreamCreate(&stream);
kernel<<<1, 1, 0, stream>>>();
cudaError_t err{cudaDeviceSynchronize()};
if (err != cudaSuccess) { throw std::runtime_error("Kernel failed on non-default stream!"); }
err = cudaGetLastError();
if (err != cudaSuccess) { throw std::runtime_error("Kernel failed on non-default stream!"); }
try {
kernel<<<1, 1>>>();
} catch (std::runtime_error&) {
return;
}
throw std::runtime_error("No exception raised for kernel on default stream!");
}
int main() { test_cudaLaunchKernel(); }
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/hash_map/map_test.cu
|
/*
* Copyright (c) 2018-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <hash/concurrent_unordered_map.cuh>
#include <cudf/types.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <rmm/device_uvector.hpp>
#include <rmm/exec_policy.hpp>
#include <thrust/logical.h>
#include <thrust/pair.h>
#include <thrust/tabulate.h>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <random>
#include <unordered_map>
#include <vector>
template <typename K, typename V>
struct key_value_types {
using key_type = K;
using value_type = V;
using pair_type = thrust::pair<K, V>;
using map_type = concurrent_unordered_map<key_type, value_type>;
};
template <typename T>
struct InsertTest : public cudf::test::BaseFixture {
using key_type = typename T::key_type;
using value_type = typename T::value_type;
using pair_type = typename T::pair_type;
using map_type = typename T::map_type;
InsertTest()
{
// prevent overflow of small types
const size_t input_size =
std::min(static_cast<key_type>(size), std::numeric_limits<key_type>::max());
pairs.resize(input_size, cudf::get_default_stream());
map = std::move(map_type::create(compute_hash_table_size(size), cudf::get_default_stream()));
cudf::get_default_stream().synchronize();
}
const cudf::size_type size{10000};
rmm::device_uvector<pair_type> pairs{static_cast<std::size_t>(size), cudf::get_default_stream()};
std::unique_ptr<map_type, std::function<void(map_type*)>> map;
};
using TestTypes = ::testing::Types<key_value_types<int32_t, int32_t>,
key_value_types<int64_t, int64_t>,
key_value_types<int16_t, int16_t>,
key_value_types<int32_t, float>,
key_value_types<int64_t, double>>;
TYPED_TEST_SUITE(InsertTest, TestTypes);
template <typename map_type, typename pair_type>
struct insert_pair {
insert_pair(map_type _map) : map{_map} {}
__device__ bool operator()(pair_type const& pair)
{
auto result = map.insert(pair);
if (result.first == map.end()) { return false; }
return result.second;
}
map_type map;
};
template <typename map_type, typename pair_type>
struct find_pair {
find_pair(map_type _map) : map{_map} {}
__device__ bool operator()(pair_type const& pair)
{
auto result = map.find(pair.first);
if (result == map.end()) { return false; }
return *result == pair;
}
map_type map;
};
template <typename pair_type,
typename key_type = typename pair_type::first_type,
typename value_type = typename pair_type::second_type>
struct unique_pair_generator {
__device__ pair_type operator()(cudf::size_type i)
{
return thrust::make_pair(key_type(i), value_type(i));
}
};
template <typename pair_type,
typename key_type = typename pair_type::first_type,
typename value_type = typename pair_type::second_type>
struct identical_pair_generator {
identical_pair_generator(key_type k = 42, value_type v = 42) : key{k}, value{v} {}
__device__ pair_type operator()(cudf::size_type i) { return thrust::make_pair(key, value); }
key_type key;
value_type value;
};
template <typename pair_type,
typename key_type = typename pair_type::first_type,
typename value_type = typename pair_type::second_type>
struct identical_key_generator {
identical_key_generator(key_type k = 42) : key{k} {}
__device__ pair_type operator()(cudf::size_type i)
{
return thrust::make_pair(key, value_type(i));
}
key_type key;
};
TYPED_TEST(InsertTest, UniqueKeysUniqueValues)
{
using map_type = typename TypeParam::map_type;
using pair_type = typename TypeParam::pair_type;
thrust::tabulate(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.end(),
unique_pair_generator<pair_type>{});
// All pairs should be new inserts
EXPECT_TRUE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.end(),
insert_pair<map_type, pair_type>{*this->map}));
// All pairs should be present in the map
EXPECT_TRUE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.end(),
find_pair<map_type, pair_type>{*this->map}));
}
TYPED_TEST(InsertTest, IdenticalKeysIdenticalValues)
{
using map_type = typename TypeParam::map_type;
using pair_type = typename TypeParam::pair_type;
thrust::tabulate(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.end(),
identical_pair_generator<pair_type>{});
// Insert a single pair
EXPECT_TRUE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.begin() + 1,
insert_pair<map_type, pair_type>{*this->map}));
// Identical inserts should all return false (no new insert)
EXPECT_FALSE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.end(),
insert_pair<map_type, pair_type>{*this->map}));
// All pairs should be present in the map
EXPECT_TRUE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.end(),
find_pair<map_type, pair_type>{*this->map}));
}
TYPED_TEST(InsertTest, IdenticalKeysUniqueValues)
{
using map_type = typename TypeParam::map_type;
using pair_type = typename TypeParam::pair_type;
thrust::tabulate(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.end(),
identical_key_generator<pair_type>{});
// Insert a single pair
EXPECT_TRUE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.begin() + 1,
insert_pair<map_type, pair_type>{*this->map}));
// Identical key inserts should all return false (no new insert)
EXPECT_FALSE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin() + 1,
this->pairs.end(),
insert_pair<map_type, pair_type>{*this->map}));
// Only first pair is present in map
EXPECT_TRUE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin(),
this->pairs.begin() + 1,
find_pair<map_type, pair_type>{*this->map}));
EXPECT_FALSE(thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
this->pairs.begin() + 1,
this->pairs.end(),
find_pair<map_type, pair_type>{*this->map}));
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/value_iterator.cpp
|
/*
* Copyright (c) 2020-2021, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <cudf_test/base_fixture.hpp>
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/offsetalator_test.cu
|
/*
* Copyright (c) 2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <tests/iterator/iterator_tests.cuh>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/offsets_iterator_factory.cuh>
#include <thrust/binary_search.h>
#include <thrust/gather.h>
#include <thrust/host_vector.h>
#include <thrust/optional.h>
#include <thrust/pair.h>
#include <thrust/scatter.h>
#include <thrust/sequence.h>
#include <thrust/transform.h>
using TestingTypes = cudf::test::Types<int32_t, int64_t>;
template <typename T>
struct OffsetalatorTest : public IteratorTest<T> {};
TYPED_TEST_SUITE(OffsetalatorTest, TestingTypes);
TYPED_TEST(OffsetalatorTest, input_iterator)
{
using T = TypeParam;
auto host_values = cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -20, 45});
auto d_col = cudf::test::fixed_width_column_wrapper<T>(host_values.begin(), host_values.end());
auto expected_values = thrust::host_vector<cudf::size_type>(host_values.size());
std::transform(host_values.begin(), host_values.end(), expected_values.begin(), [](auto v) {
return static_cast<cudf::size_type>(v);
});
auto it_dev = cudf::detail::offsetalator_factory::make_input_iterator(d_col);
this->iterator_test_thrust(expected_values, it_dev, host_values.size());
}
TYPED_TEST(OffsetalatorTest, output_iterator)
{
using T = TypeParam;
auto d_col1 = cudf::test::fixed_width_column_wrapper<int64_t>({0, 6, 7, 14, 23, 33, 43, 45, 63});
auto d_col2 = cudf::test::fixed_width_column_wrapper<T>({0, 0, 0, 0, 0, 0, 0, 0, 0});
auto itr = cudf::detail::offsetalator_factory::make_output_iterator(d_col2);
auto input = cudf::column_view(d_col1);
auto stream = cudf::get_default_stream();
auto map = cudf::test::fixed_width_column_wrapper<int>({0, 2, 4, 6, 8, 1, 3, 5, 7});
auto d_map = cudf::column_view(map);
thrust::gather(rmm::exec_policy_nosync(stream),
d_map.begin<int>(),
d_map.end<int>(),
input.begin<int64_t>(),
itr);
auto expected = cudf::test::fixed_width_column_wrapper<T>({0, 7, 23, 43, 63, 6, 14, 33, 45});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
thrust::scatter(rmm::exec_policy_nosync(stream),
input.begin<int64_t>(),
input.end<int64_t>(),
d_map.begin<int>(),
itr);
expected = cudf::test::fixed_width_column_wrapper<T>({0, 33, 6, 43, 7, 45, 14, 63, 23});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
thrust::fill(rmm::exec_policy(stream), itr, itr + input.size(), 77);
expected = cudf::test::fixed_width_column_wrapper<T>({77, 77, 77, 77, 77, 77, 77, 77, 77});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
thrust::sequence(rmm::exec_policy(stream), itr, itr + input.size());
expected = cudf::test::fixed_width_column_wrapper<T>({0, 1, 2, 3, 4, 5, 6, 7, 8});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
auto offsets =
cudf::test::fixed_width_column_wrapper<int64_t>({0, 10, 20, 30, 40, 50, 60, 70, 80});
auto d_offsets = cudf::column_view(offsets);
thrust::lower_bound(rmm::exec_policy(stream),
d_offsets.begin<int64_t>(),
d_offsets.end<int64_t>(),
input.begin<int64_t>(),
input.end<int64_t>(),
itr);
expected = cudf::test::fixed_width_column_wrapper<T>({0, 1, 1, 2, 3, 4, 5, 5, 7});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
}
namespace {
/**
* For testing creating and using the offsetalator in device code.
*/
struct device_functor_fn {
cudf::column_device_view const d_col;
__device__ int32_t operator()(int idx)
{
auto const itr = cudf::detail::input_offsetalator(d_col.head(), d_col.type());
return static_cast<int32_t>(itr[idx] * 3);
}
};
} // namespace
TYPED_TEST(OffsetalatorTest, device_offsetalator)
{
using T = TypeParam;
auto d_col1 = cudf::test::fixed_width_column_wrapper<T>({0, 6, 7, 14, 23, 33, 43, 45, 63});
auto d_col2 = cudf::test::fixed_width_column_wrapper<int32_t>({0, 0, 0, 0, 0, 0, 0, 0, 0});
auto input = cudf::column_view(d_col1);
auto output = cudf::mutable_column_view(d_col2);
auto stream = cudf::get_default_stream();
auto d_input = cudf::column_device_view::create(input, stream);
thrust::transform(rmm::exec_policy(stream),
thrust::counting_iterator<int>(0),
thrust::counting_iterator<int>(input.size()),
output.begin<int32_t>(),
device_functor_fn{*d_input});
auto expected =
cudf::test::fixed_width_column_wrapper<int32_t>({0, 18, 21, 42, 69, 99, 129, 135, 189});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/optional_iterator_test.cuh
|
/*
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#pragma once
#include <tests/iterator/iterator_tests.cuh>
#include <thrust/host_vector.h>
#include <thrust/optional.h>
template <typename T>
void nonull_optional_iterator(IteratorTest<T>& testFixture)
{
// data and valid arrays
auto host_values_std =
cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -20, 45});
thrust::host_vector<T> host_values(host_values_std);
// create a column
cudf::test::fixed_width_column_wrapper<T> w_col(host_values.begin(), host_values.end());
auto d_col = cudf::column_device_view::create(w_col);
// calculate the expected value by CPU.
thrust::host_vector<thrust::optional<T>> replaced_array(host_values.size());
std::transform(host_values.begin(), host_values.end(), replaced_array.begin(), [](auto s) {
return thrust::optional<T>{s};
});
// GPU test
testFixture.iterator_test_thrust(
replaced_array,
cudf::detail::make_optional_iterator<T>(*d_col, cudf::nullate::DYNAMIC{false}),
host_values.size());
testFixture.iterator_test_thrust(
replaced_array,
cudf::detail::make_optional_iterator<T>(*d_col, cudf::nullate::NO{}),
host_values.size());
}
template <typename T>
void null_optional_iterator(IteratorTest<T>& testFixture)
{
// data and valid arrays
auto host_values = cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -20, 45});
thrust::host_vector<bool> host_bools(std::vector<bool>({1, 1, 0, 1, 1, 1, 0, 1, 1}));
// create a column with bool vector
cudf::test::fixed_width_column_wrapper<T> w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// calculate the expected value by CPU.
thrust::host_vector<thrust::optional<T>> optional_values(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
optional_values.begin(),
[](auto s, bool b) { return b ? thrust::optional<T>{s} : thrust::optional<T>{}; });
thrust::host_vector<thrust::optional<T>> value_all_valid(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
value_all_valid.begin(),
[](auto s, bool b) { return thrust::optional<T>{s}; });
// GPU test for correct null mapping
testFixture.iterator_test_thrust(
optional_values, d_col->optional_begin<T>(cudf::nullate::DYNAMIC{true}), host_values.size());
testFixture.iterator_test_thrust(
optional_values, d_col->optional_begin<T>(cudf::nullate::YES{}), host_values.size());
testFixture.iterator_test_thrust(
optional_values, d_col->optional_begin<T>(cudf::nullate::YES{}), host_values.size());
// GPU test for ignoring null mapping
testFixture.iterator_test_thrust(
value_all_valid, d_col->optional_begin<T>(cudf::nullate::DYNAMIC{false}), host_values.size());
testFixture.iterator_test_thrust(
value_all_valid, d_col->optional_begin<T>(cudf::nullate::NO{}), host_values.size());
testFixture.iterator_test_thrust(
value_all_valid, d_col->optional_begin<T>(cudf::nullate::NO{}), host_values.size());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/pair_iterator_test_numeric.cu
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <tests/iterator/pair_iterator_test.cuh>
#include <rmm/exec_policy.hpp>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/pair.h>
#include <thrust/reduce.h>
using TestingTypes = cudf::test::NumericTypes;
namespace cudf {
// To print meanvar for debug.
// Needs to be in the cudf namespace for ADL
template <typename T>
std::ostream& operator<<(std::ostream& os, cudf::meanvar<T> const& rhs)
{
return os << "[" << rhs.value << ", " << rhs.value_squared << ", " << rhs.count << "] ";
};
} // namespace cudf
template <typename T>
struct NumericPairIteratorTest : public IteratorTest<T> {};
TYPED_TEST_SUITE(NumericPairIteratorTest, TestingTypes);
TYPED_TEST(NumericPairIteratorTest, nonull_pair_iterator) { nonull_pair_iterator(*this); }
TYPED_TEST(NumericPairIteratorTest, null_pair_iterator) { null_pair_iterator(*this); }
// Transformers and Operators for pair_iterator test
template <typename ElementType>
struct transformer_pair_meanvar {
using ResultType = thrust::pair<cudf::meanvar<ElementType>, bool>;
CUDF_HOST_DEVICE inline ResultType operator()(thrust::pair<ElementType, bool> const& pair)
{
ElementType v = pair.first;
return {{v, static_cast<ElementType>(v * v), (pair.second) ? 1 : 0}, pair.second};
};
};
struct sum_if_not_null {
template <typename T>
CUDF_HOST_DEVICE inline thrust::pair<T, bool> operator()(thrust::pair<T, bool> const& lhs,
thrust::pair<T, bool> const& rhs)
{
if (lhs.second & rhs.second)
return {lhs.first + rhs.first, true};
else if (lhs.second)
return {lhs};
else
return {rhs};
}
};
// TODO: enable this test also at __CUDACC_DEBUG__
// This test causes fatal compilation error only at device debug mode.
// Workaround: exclude this test only at device debug mode.
#if !defined(__CUDACC_DEBUG__)
// This test computes `count`, `sum`, `sum_of_squares` at a single reduction call.
// It would be useful for `var`, `std` operation
TYPED_TEST(NumericPairIteratorTest, mean_var_output)
{
using T = TypeParam;
using T_output = cudf::meanvar<T>;
transformer_pair_meanvar<T> transformer{};
int const column_size{5000};
const T init{0};
// data and valid arrays
std::vector<T> host_values(column_size);
std::vector<bool> host_bools(column_size);
if constexpr (std::is_floating_point<T>()) {
cudf::test::UniformRandomGenerator<int32_t> rng;
std::generate(host_values.begin(), host_values.end(), [&rng]() {
return static_cast<T>(rng.generate() % 10); // reduces float-op errors
});
} else {
cudf::test::UniformRandomGenerator<T> rng;
std::generate(host_values.begin(), host_values.end(), [&rng]() { return rng.generate(); });
}
cudf::test::UniformRandomGenerator<bool> rbg;
std::generate(host_bools.begin(), host_bools.end(), [&rbg]() { return rbg.generate(); });
cudf::test::fixed_width_column_wrapper<TypeParam> w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// calculate expected values by CPU
T_output expected_value;
expected_value.count = d_col->size() - static_cast<cudf::column_view>(w_col).null_count();
std::vector<T> replaced_array(d_col->size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
replaced_array.begin(),
[&](T x, bool b) { return (b) ? static_cast<T>(x) : init; });
expected_value.count = d_col->size() - static_cast<cudf::column_view>(w_col).null_count();
expected_value.value = std::accumulate(replaced_array.begin(), replaced_array.end(), T{0});
expected_value.value_squared = std::accumulate(
replaced_array.begin(), replaced_array.end(), T{0}, [](T acc, T i) { return acc + i * i; });
// GPU test
auto it_dev = d_col->pair_begin<T, true>();
auto it_dev_squared = thrust::make_transform_iterator(it_dev, transformer);
auto result = thrust::reduce(rmm::exec_policy(cudf::get_default_stream()),
it_dev_squared,
it_dev_squared + d_col->size(),
thrust::make_pair(T_output{}, true),
sum_if_not_null{});
if constexpr (not std::is_floating_point<T>()) {
EXPECT_EQ(expected_value, result.first) << "pair iterator reduction sum";
} else {
EXPECT_NEAR(expected_value.value, result.first.value, 1e-3) << "pair iterator reduction sum";
EXPECT_NEAR(expected_value.value_squared, result.first.value_squared, 1e-3)
<< "pair iterator reduction sum squared";
EXPECT_EQ(expected_value.count, result.first.count) << "pair iterator reduction count";
}
}
#endif
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/sizes_to_offsets_iterator_test.cu
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/sizes_to_offsets_iterator.cuh>
#include <cudf/utilities/default_stream.hpp>
#include <rmm/device_scalar.hpp>
#include <rmm/device_uvector.hpp>
#include <rmm/exec_policy.hpp>
#include <thrust/host_vector.h>
#include <thrust/scan.h>
#include <algorithm>
using TestingTypes = cudf::test::IntegralTypesNotBool;
template <typename T>
struct SizesToOffsetsIteratorTestTyped : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(SizesToOffsetsIteratorTestTyped, TestingTypes);
TYPED_TEST(SizesToOffsetsIteratorTestTyped, ExclusiveScan)
{
using T = TypeParam;
using LastType = int64_t;
auto stream = cudf::get_default_stream();
auto sizes = std::vector<T>({0, 6, 0, 14, 13, 64, 10, 20, 41});
auto d_col = cudf::test::fixed_width_column_wrapper<T>(sizes.begin(), sizes.end());
auto d_view = cudf::column_view(d_col);
auto last = rmm::device_scalar<LastType>(0, stream);
auto result = rmm::device_uvector<T>(d_view.size(), stream);
auto output_itr =
cudf::detail::make_sizes_to_offsets_iterator(result.begin(), result.end(), last.data());
thrust::exclusive_scan(
rmm::exec_policy(stream), d_view.begin<T>(), d_view.end<T>(), output_itr, LastType{0});
auto expected_values = std::vector<T>(sizes.size());
std::exclusive_scan(sizes.begin(), sizes.end(), expected_values.begin(), T{0});
auto expected_reduce =
static_cast<LastType>(std::reduce(sizes.begin(), sizes.begin() + sizes.size() - 1, T{0}));
auto expected =
cudf::test::fixed_width_column_wrapper<T>(expected_values.begin(), expected_values.end());
auto result_col = cudf::column_view(
cudf::data_type(cudf::type_to_id<T>()), d_view.size(), result.data(), nullptr, 0);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(result_col, expected);
EXPECT_EQ(last.value(stream), expected_reduce);
}
struct SizesToOffsetsIteratorTest : public cudf::test::BaseFixture {};
TEST_F(SizesToOffsetsIteratorTest, ScanWithOverflow)
{
auto stream = cudf::get_default_stream();
std::vector<int32_t> values(30000, 100000);
auto d_col = cudf::test::fixed_width_column_wrapper<int32_t>(values.begin(), values.end());
auto d_view = cudf::column_view(d_col);
auto last = rmm::device_scalar<int64_t>(0, stream);
auto result = rmm::device_uvector<int32_t>(d_view.size(), stream);
auto output_itr =
cudf::detail::make_sizes_to_offsets_iterator(result.begin(), result.end(), last.data());
thrust::exclusive_scan(rmm::exec_policy(stream),
d_view.begin<int32_t>(),
d_view.end<int32_t>(),
output_itr,
int64_t{0});
auto expected = static_cast<int64_t>(
std::reduce(values.begin(), values.begin() + values.size() - 1, int64_t{0}));
EXPECT_EQ(last.value(stream), expected);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/optional_iterator_test_chrono.cu
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <tests/iterator/optional_iterator_test.cuh>
using TestingTypes = cudf::test::ChronoTypes;
template <typename T>
struct ChronoOptionalIteratorTest : public IteratorTest<T> {};
TYPED_TEST_SUITE(ChronoOptionalIteratorTest, TestingTypes);
TYPED_TEST(ChronoOptionalIteratorTest, nonull_optional_iterator)
{
nonull_optional_iterator(*this);
}
TYPED_TEST(ChronoOptionalIteratorTest, null_optional_iterator) { null_optional_iterator(*this); }
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/scalar_iterator_test.cu
|
/*
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <tests/iterator/iterator_tests.cuh>
#include <thrust/host_vector.h>
#include <thrust/pair.h>
using TestingTypes = cudf::test::FixedWidthTypesWithoutFixedPoint;
TYPED_TEST_SUITE(IteratorTest, TestingTypes);
TYPED_TEST(IteratorTest, scalar_iterator)
{
using T = TypeParam;
T init = cudf::test::make_type_param_scalar<T>(
cudf::test::UniformRandomGenerator<int>(-128, 128).generate());
// data and valid arrays
thrust::host_vector<T> host_values(100, init);
std::vector<bool> host_bools(100, true);
// create a scalar
using ScalarType = cudf::scalar_type_t<T>;
std::unique_ptr<cudf::scalar> s(new ScalarType{init, true});
// calculate the expected value by CPU.
thrust::host_vector<thrust::pair<T, bool>> value_and_validity(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
value_and_validity.begin(),
[](auto v, auto b) {
return thrust::pair<T, bool>{v, b};
});
// GPU test
auto it_dev = cudf::detail::make_scalar_iterator<T>(*s);
this->iterator_test_thrust(host_values, it_dev, host_values.size());
auto it_pair_dev = cudf::detail::make_pair_iterator<T>(*s);
this->iterator_test_thrust(value_and_validity, it_pair_dev, host_values.size());
}
TYPED_TEST(IteratorTest, null_scalar_iterator)
{
using T = TypeParam;
T init = cudf::test::make_type_param_scalar<T>(
cudf::test::UniformRandomGenerator<int>(-128, 128).generate());
// data and valid arrays
std::vector<T> host_values(100, init);
std::vector<bool> host_bools(100, true);
// create a scalar
using ScalarType = cudf::scalar_type_t<T>;
std::unique_ptr<cudf::scalar> s(new ScalarType{init, true});
// calculate the expected value by CPU.
thrust::host_vector<thrust::pair<T, bool>> value_and_validity(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
value_and_validity.begin(),
[](auto v, auto b) {
return thrust::pair<T, bool>{v, b};
});
// GPU test
auto it_pair_dev = cudf::detail::make_pair_iterator<T>(*s);
this->iterator_test_thrust(value_and_validity, it_pair_dev, host_values.size());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/README.md
|
# Iterator Test decomposition
The Iterator tests have been decomposed across different types to
make sure that no single test file takes too long to compile.
The decomposition is that each of the following
categorizes of types should be placed in a separate file:
- numeric
- chrono ( timestamp, duration )
- fixed point ( numeric::decimal32, numeric::decimal64 )
- string
The `numeric` and `chrono` type lists have roughly the same
number of entries allowing for a balanced compile time between
those two. We follow the same pattern for `fixed point` and
`string` so it is clear where to test those types, even though
they have a smaller set of entries and will compile quickly.
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/value_iterator_test_strings.cu
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include "iterator_tests.cuh"
#include <cudf/detail/utilities/vector_factories.hpp>
#include <rmm/cuda_stream_view.hpp>
#include <rmm/device_uvector.hpp>
#include <thrust/host_vector.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/pair.h>
auto strings_to_string_views(std::vector<std::string>& input_strings)
{
auto all_valid = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return true; });
std::vector<char> chars;
std::vector<int32_t> offsets;
std::tie(chars, offsets) = cudf::test::detail::make_chars_and_offsets(
input_strings.begin(), input_strings.end(), all_valid);
auto dev_chars = cudf::detail::make_device_uvector_sync(
chars, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
// calculate the expected value by CPU. (but contains device pointers)
thrust::host_vector<cudf::string_view> replaced_array(input_strings.size());
std::transform(thrust::counting_iterator<size_t>(0),
thrust::counting_iterator<size_t>(replaced_array.size()),
replaced_array.begin(),
[c_start = dev_chars.begin(), offsets](auto i) {
return cudf::string_view(c_start + offsets[i], offsets[i + 1] - offsets[i]);
});
return std::make_tuple(std::move(dev_chars), replaced_array);
}
struct StringIteratorTest : public IteratorTest<cudf::string_view> {};
TEST_F(StringIteratorTest, string_view_null_iterator)
{
using T = cudf::string_view;
std::string zero("zero");
// the char data has to be in GPU
auto initmsg = cudf::detail::make_device_uvector_sync(
zero, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
T init = T{initmsg.data(), int(initmsg.size())};
// data and valid arrays
std::vector<std::string> host_values(
{"one", "two", "three", "four", "five", "six", "eight", "nine"});
std::vector<bool> host_bools({1, 1, 0, 1, 1, 1, 0, 1, 1});
// replace nulls in CPU
std::vector<std::string> replaced_strings(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
replaced_strings.begin(),
[zero](auto s, auto b) { return b ? s : zero; });
auto [dev_chars, replaced_array] = strings_to_string_views(replaced_strings);
// create a column with bool vector
cudf::test::strings_column_wrapper w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// GPU test
auto it_dev = cudf::detail::make_null_replacement_iterator(*d_col, init);
this->iterator_test_thrust(replaced_array, it_dev, host_values.size());
// this->values_equal_test(replaced_array, *d_col); //string_view{0} is invalid
}
TEST_F(StringIteratorTest, string_view_no_null_iterator)
{
using T = cudf::string_view;
// T init = T{"", 0};
std::string zero("zero");
// the char data has to be in GPU
auto initmsg = cudf::detail::make_device_uvector_sync(
zero, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
T init = T{initmsg.data(), int(initmsg.size())};
// data array
std::vector<std::string> host_values(
{"one", "two", "three", "four", "five", "six", "eight", "nine"});
auto [dev_chars, all_array] = strings_to_string_views(host_values);
// create a column with bool vector
cudf::test::strings_column_wrapper w_col(host_values.begin(), host_values.end());
auto d_col = cudf::column_device_view::create(w_col);
// GPU test
auto it_dev = d_col->begin<T>();
this->iterator_test_thrust(all_array, it_dev, host_values.size());
}
TEST_F(StringIteratorTest, string_scalar_iterator)
{
using T = cudf::string_view;
// T init = T{"", 0};
std::string zero("zero");
// the char data has to be in GPU
auto initmsg = cudf::detail::make_device_uvector_sync(
zero, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
T init = T{initmsg.data(), int(initmsg.size())};
// data array
std::vector<std::string> host_values(100, zero);
auto [dev_chars, all_array] = strings_to_string_views(host_values);
// calculate the expected value by CPU.
thrust::host_vector<thrust::pair<T, bool>> value_and_validity(host_values.size());
std::transform(all_array.begin(), all_array.end(), value_and_validity.begin(), [](auto v) {
return thrust::pair<T, bool>{v, true};
});
// create a scalar
using ScalarType = cudf::scalar_type_t<T>;
std::unique_ptr<cudf::scalar> s(new ScalarType{zero, true});
// GPU test
auto it_dev = cudf::detail::make_scalar_iterator<T>(*s);
this->iterator_test_thrust(all_array, it_dev, host_values.size());
auto it_pair_dev = cudf::detail::make_pair_iterator<T>(*s);
this->iterator_test_thrust(value_and_validity, it_pair_dev, host_values.size());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/iterator_tests.cuh
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#pragma once
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/detail/utilities/transform_unary_functions.cuh> // for meanvar
#include <cudf/detail/utilities/vector_factories.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <rmm/device_uvector.hpp>
#include <rmm/exec_policy.hpp>
#include <thrust/distance.h>
#include <thrust/equal.h>
#include <thrust/execution_policy.h>
#include <thrust/functional.h>
#include <thrust/host_vector.h>
#include <thrust/logical.h>
#include <thrust/transform.h>
#include <cub/device/device_reduce.cuh>
#include <bitset>
#include <cstdint>
#include <iostream>
#include <numeric>
#include <random>
// Base Typed test fixture for iterator test
template <typename T>
struct IteratorTest : public cudf::test::BaseFixture {
// iterator test case which uses cub
template <typename InputIterator, typename T_output>
void iterator_test_cub(T_output expected, InputIterator d_in, int num_items)
{
T_output init = cudf::test::make_type_param_scalar<T_output>(0);
rmm::device_uvector<T_output> dev_result(1, cudf::get_default_stream());
// Get temporary storage size
size_t temp_storage_bytes = 0;
cub::DeviceReduce::Reduce(nullptr,
temp_storage_bytes,
d_in,
dev_result.begin(),
num_items,
thrust::minimum{},
init,
cudf::get_default_stream().value());
// Allocate temporary storage
rmm::device_buffer d_temp_storage(temp_storage_bytes, cudf::get_default_stream());
// Run reduction
cub::DeviceReduce::Reduce(d_temp_storage.data(),
temp_storage_bytes,
d_in,
dev_result.begin(),
num_items,
thrust::minimum{},
init,
cudf::get_default_stream().value());
evaluate(expected, dev_result, "cub test");
}
// iterator test case which uses thrust
template <typename InputIterator, typename T_output>
void iterator_test_thrust(thrust::host_vector<T_output> const& expected,
InputIterator d_in,
int num_items)
{
InputIterator d_in_last = d_in + num_items;
EXPECT_EQ(thrust::distance(d_in, d_in_last), num_items);
auto dev_expected = cudf::detail::make_device_uvector_sync(
expected, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
// using a temporary vector and calling transform and all_of separately is
// equivalent to thrust::equal but compiles ~3x faster
auto dev_results = rmm::device_uvector<bool>(num_items, cudf::get_default_stream());
thrust::transform(rmm::exec_policy(cudf::get_default_stream()),
d_in,
d_in_last,
dev_expected.begin(),
dev_results.begin(),
thrust::equal_to{});
auto result = thrust::all_of(rmm::exec_policy(cudf::get_default_stream()),
dev_results.begin(),
dev_results.end(),
thrust::identity<bool>{});
EXPECT_TRUE(result) << "thrust test";
}
template <typename T_output>
void evaluate(T_output expected,
rmm::device_uvector<T_output> const& dev_result,
char const* msg = nullptr)
{
auto host_result = cudf::detail::make_host_vector_sync(dev_result, cudf::get_default_stream());
EXPECT_EQ(expected, host_result[0]) << msg;
}
template <typename T_output>
void values_equal_test(thrust::host_vector<T_output> const& expected,
cudf::column_device_view const& col)
{
if (col.nullable()) {
auto it_dev = cudf::detail::make_null_replacement_iterator(
col, cudf::test::make_type_param_scalar<T_output>(0));
iterator_test_thrust(expected, it_dev, col.size());
} else {
auto it_dev = col.begin<T_output>();
iterator_test_thrust(expected, it_dev, col.size());
}
}
};
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/pair_iterator_test_chrono.cu
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <tests/iterator/pair_iterator_test.cuh>
using TestingTypes = cudf::test::ChronoTypes;
template <typename T>
struct ChronoPairIteratorTest : public IteratorTest<T> {};
TYPED_TEST_SUITE(ChronoPairIteratorTest, TestingTypes);
TYPED_TEST(ChronoPairIteratorTest, nonull_pair_iterator) { nonull_pair_iterator(*this); }
TYPED_TEST(ChronoPairIteratorTest, null_pair_iterator) { null_pair_iterator(*this); }
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/pair_iterator_test.cuh
|
/*
* Copyright (c) 2020-2022, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#pragma once
#include <tests/iterator/iterator_tests.cuh>
#include <thrust/host_vector.h>
#include <thrust/pair.h>
template <typename T>
void nonull_pair_iterator(IteratorTest<T>& testFixture)
{
// data and valid arrays
auto host_values_std =
cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -20, 45});
thrust::host_vector<T> host_values(host_values_std);
// create a column
cudf::test::fixed_width_column_wrapper<T> w_col(host_values.begin(), host_values.end());
auto d_col = cudf::column_device_view::create(w_col);
// calculate the expected value by CPU.
thrust::host_vector<thrust::pair<T, bool>> replaced_array(host_values.size());
std::transform(host_values.begin(), host_values.end(), replaced_array.begin(), [](auto s) {
return thrust::make_pair(s, true);
});
// GPU test
auto it_dev = d_col->pair_begin<T, false>();
testFixture.iterator_test_thrust(replaced_array, it_dev, host_values.size());
}
template <typename T>
void null_pair_iterator(IteratorTest<T>& testFixture)
{
// data and valid arrays
auto host_values = cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -20, 45});
thrust::host_vector<bool> host_bools(std::vector<bool>({1, 1, 0, 1, 1, 1, 0, 1, 1}));
// create a column with bool vector
cudf::test::fixed_width_column_wrapper<T> w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// calculate the expected value by CPU.
thrust::host_vector<thrust::pair<T, bool>> value_and_validity(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
value_and_validity.begin(),
[](auto s, auto b) {
return thrust::pair<T, bool>{s, b};
});
thrust::host_vector<thrust::pair<T, bool>> value_all_valid(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
value_all_valid.begin(),
[](auto s, auto b) {
return thrust::pair<T, bool>{s, true};
});
// GPU test
auto it_dev = d_col->pair_begin<T, true>();
testFixture.iterator_test_thrust(value_and_validity, it_dev, host_values.size());
auto it_hasnonull_dev = d_col->pair_begin<T, false>();
testFixture.iterator_test_thrust(value_all_valid, it_hasnonull_dev, host_values.size());
auto itb_dev = cudf::detail::make_validity_iterator(*d_col);
testFixture.iterator_test_thrust(host_bools, itb_dev, host_values.size());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/indexalator_test.cu
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <tests/iterator/iterator_tests.cuh>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/indexalator.cuh>
#include <thrust/binary_search.h>
#include <thrust/gather.h>
#include <thrust/host_vector.h>
#include <thrust/optional.h>
#include <thrust/pair.h>
#include <thrust/scatter.h>
#include <thrust/sequence.h>
using TestingTypes = cudf::test::IntegralTypesNotBool;
template <typename T>
struct IndexalatorTest : public IteratorTest<T> {};
TYPED_TEST_SUITE(IndexalatorTest, TestingTypes);
TYPED_TEST(IndexalatorTest, input_iterator)
{
using T = TypeParam;
auto host_values = cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -20, 45});
auto d_col = cudf::test::fixed_width_column_wrapper<T>(host_values.begin(), host_values.end());
auto expected_values = thrust::host_vector<cudf::size_type>(host_values.size());
std::transform(host_values.begin(), host_values.end(), expected_values.begin(), [](auto v) {
return static_cast<cudf::size_type>(v);
});
auto it_dev = cudf::detail::indexalator_factory::make_input_iterator(d_col);
this->iterator_test_thrust(expected_values, it_dev, host_values.size());
}
TYPED_TEST(IndexalatorTest, pair_iterator)
{
using T = TypeParam;
auto host_values = cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -120, 115});
auto validity = std::vector<bool>({0, 1, 1, 1, 1, 1, 0, 1, 1});
auto d_col = cudf::test::fixed_width_column_wrapper<T>(
host_values.begin(), host_values.end(), validity.begin());
auto expected_values =
thrust::host_vector<thrust::pair<cudf::size_type, bool>>(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
validity.begin(),
expected_values.begin(),
[](T v, bool b) { return thrust::make_pair(static_cast<cudf::size_type>(v), b); });
auto it_dev = cudf::detail::indexalator_factory::make_input_pair_iterator(d_col);
this->iterator_test_thrust(expected_values, it_dev, host_values.size());
}
TYPED_TEST(IndexalatorTest, optional_iterator)
{
using T = TypeParam;
auto host_values = cudf::test::make_type_param_vector<T>({0, 6, 0, -104, 103, 64, -13, -20, 45});
auto validity = std::vector<bool>({0, 1, 1, 1, 1, 1, 0, 1, 1});
auto d_col = cudf::test::fixed_width_column_wrapper<T>(
host_values.begin(), host_values.end(), validity.begin());
auto expected_values = thrust::host_vector<thrust::optional<cudf::size_type>>(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
validity.begin(),
expected_values.begin(),
[](T v, bool b) {
return (b) ? thrust::make_optional(static_cast<cudf::size_type>(v))
: thrust::nullopt;
});
auto it_dev = cudf::detail::indexalator_factory::make_input_optional_iterator(d_col);
this->iterator_test_thrust(expected_values, it_dev, host_values.size());
}
template <typename Integer>
struct transform_fn {
__device__ cudf::size_type operator()(Integer v)
{
return static_cast<cudf::size_type>(v) + static_cast<cudf::size_type>(v);
}
};
TYPED_TEST(IndexalatorTest, output_iterator)
{
using T = TypeParam;
auto d_col1 =
cudf::test::fixed_width_column_wrapper<T, int32_t>({0, 6, 7, 14, 23, 33, 43, 45, 63});
auto d_col2 =
cudf::test::fixed_width_column_wrapper<cudf::size_type>({0, 0, 0, 0, 0, 0, 0, 0, 0});
auto itr = cudf::detail::indexalator_factory::make_output_iterator(d_col2);
auto input = cudf::column_view(d_col1);
auto stream = cudf::get_default_stream();
auto map = cudf::test::fixed_width_column_wrapper<int>({0, 2, 4, 6, 8, 1, 3, 5, 7});
auto d_map = cudf::column_view(map);
thrust::gather(
rmm::exec_policy_nosync(stream), d_map.begin<int>(), d_map.end<int>(), input.begin<T>(), itr);
auto expected =
cudf::test::fixed_width_column_wrapper<cudf::size_type>({0, 7, 23, 43, 63, 6, 14, 33, 45});
thrust::scatter(
rmm::exec_policy_nosync(stream), input.begin<T>(), input.end<T>(), d_map.begin<int>(), itr);
expected =
cudf::test::fixed_width_column_wrapper<cudf::size_type>({0, 33, 6, 43, 7, 45, 14, 63, 23});
thrust::transform(
rmm::exec_policy(stream), input.begin<T>(), input.end<T>(), itr, transform_fn<T>{});
expected =
cudf::test::fixed_width_column_wrapper<cudf::size_type>({0, 12, 14, 28, 46, 66, 86, 90, 126});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
thrust::fill(rmm::exec_policy(stream), itr, itr + input.size(), 77);
expected =
cudf::test::fixed_width_column_wrapper<cudf::size_type>({77, 77, 77, 77, 77, 77, 77, 77, 77});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
thrust::sequence(rmm::exec_policy(stream), itr, itr + input.size());
expected = cudf::test::fixed_width_column_wrapper<cudf::size_type>({0, 1, 2, 3, 4, 5, 6, 7, 8});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
auto indices =
cudf::test::fixed_width_column_wrapper<T, int32_t>({0, 10, 20, 30, 40, 50, 60, 70, 80});
auto d_indices = cudf::column_view(indices);
thrust::lower_bound(rmm::exec_policy(stream),
d_indices.begin<T>(),
d_indices.end<T>(),
input.begin<T>(),
input.end<T>(),
itr);
expected = cudf::test::fixed_width_column_wrapper<cudf::size_type>({0, 1, 1, 2, 3, 4, 5, 5, 7});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(d_col2, expected);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/value_iterator_test_chrono.cu
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/type_lists.hpp>
#include <tests/iterator/value_iterator_test.cuh>
using TestingTypes = cudf::test::ChronoTypes;
template <typename T>
struct ChronoValueIteratorTest : public IteratorTest<T> {};
TYPED_TEST_SUITE(ChronoValueIteratorTest, TestingTypes);
TYPED_TEST(ChronoValueIteratorTest, non_null_iterator) { non_null_iterator(*this); }
TYPED_TEST(ChronoValueIteratorTest, null_iterator) { null_iterator(*this); }
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/value_iterator_test_numeric.cu
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/type_lists.hpp>
#include <tests/iterator/value_iterator_test.cuh>
using TestingTypes = cudf::test::NumericTypes;
template <typename T>
struct NumericValueIteratorTest : public IteratorTest<T> {};
TYPED_TEST_SUITE(NumericValueIteratorTest, TestingTypes);
TYPED_TEST(NumericValueIteratorTest, non_null_iterator) { non_null_iterator(*this); }
TYPED_TEST(NumericValueIteratorTest, null_iterator) { null_iterator(*this); }
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/optional_iterator_test_numeric.cu
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <tests/iterator/optional_iterator_test.cuh>
#include <cudf/utilities/default_stream.hpp>
#include <thrust/execution_policy.h>
#include <thrust/iterator/transform_iterator.h>
#include <thrust/optional.h>
#include <thrust/reduce.h>
#include <thrust/transform.h>
using TestingTypes = cudf::test::NumericTypes;
namespace cudf {
// To print meanvar for debug.
// Needs to be in the cudf namespace for ADL
template <typename T>
std::ostream& operator<<(std::ostream& os, cudf::meanvar<T> const& rhs)
{
return os << "[" << rhs.value << ", " << rhs.value_squared << ", " << rhs.count << "] ";
};
} // namespace cudf
template <typename T>
struct NumericOptionalIteratorTest : public IteratorTest<T> {};
TYPED_TEST_SUITE(NumericOptionalIteratorTest, TestingTypes);
TYPED_TEST(NumericOptionalIteratorTest, nonull_optional_iterator)
{
nonull_optional_iterator(*this);
}
TYPED_TEST(NumericOptionalIteratorTest, null_optional_iterator) { null_optional_iterator(*this); }
// Transformers and Operators for optional_iterator test
template <typename ElementType>
struct transformer_optional_meanvar {
using ResultType = thrust::optional<cudf::meanvar<ElementType>>;
CUDF_HOST_DEVICE inline ResultType operator()(thrust::optional<ElementType> const& optional)
{
if (optional.has_value()) {
auto v = *optional;
return cudf::meanvar<ElementType>{v, static_cast<ElementType>(v * v), 1};
}
return thrust::nullopt;
}
};
template <typename T>
struct optional_to_meanvar {
CUDF_HOST_DEVICE inline T operator()(thrust::optional<T> const& v) { return v.value_or(T{0}); }
};
// TODO: enable this test also at __CUDACC_DEBUG__
// This test causes fatal compilation error only at device debug mode.
// Workaround: exclude this test only at device debug mode.
#if !defined(__CUDACC_DEBUG__)
TYPED_TEST(NumericOptionalIteratorTest, mean_var_output)
{
using T = TypeParam;
using T_output = cudf::meanvar<T>;
transformer_optional_meanvar<T> transformer{};
int const column_size{50};
const T init{0};
// data and valid arrays
std::vector<T> host_values(column_size);
std::vector<bool> host_bools(column_size);
cudf::test::UniformRandomGenerator<T> rng;
cudf::test::UniformRandomGenerator<bool> rbg;
std::generate(host_values.begin(), host_values.end(), [&rng]() { return rng.generate(); });
std::generate(host_bools.begin(), host_bools.end(), [&rbg]() { return rbg.generate(); });
cudf::test::fixed_width_column_wrapper<TypeParam> w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// calculate expected values by CPU
T_output expected_value;
expected_value.count = d_col->size() - static_cast<cudf::column_view>(w_col).null_count();
std::vector<T> replaced_array(d_col->size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
replaced_array.begin(),
[&](T x, bool b) { return (b) ? static_cast<T>(x) : init; });
expected_value.count = d_col->size() - static_cast<cudf::column_view>(w_col).null_count();
expected_value.value = std::accumulate(replaced_array.begin(), replaced_array.end(), T{0});
expected_value.value_squared = std::accumulate(
replaced_array.begin(), replaced_array.end(), T{0}, [](T acc, T i) { return acc + i * i; });
// GPU test
auto it_dev = d_col->optional_begin<T>(cudf::nullate::YES{});
auto it_dev_squared = thrust::make_transform_iterator(it_dev, transformer);
// this can be computed with a single reduce and without a temporary output vector
// but the approach increases the compile time by ~2x
auto results = rmm::device_uvector<T_output>(d_col->size(), cudf::get_default_stream());
thrust::transform(rmm::exec_policy(cudf::get_default_stream()),
it_dev_squared,
it_dev_squared + d_col->size(),
results.begin(),
optional_to_meanvar<T_output>{});
auto result = thrust::reduce(
rmm::exec_policy(cudf::get_default_stream()), results.begin(), results.end(), T_output{});
if (not std::is_floating_point<T>()) {
EXPECT_EQ(expected_value, result) << "optional iterator reduction sum";
} else {
EXPECT_NEAR(expected_value.value, result.value, 1e-3) << "optional iterator reduction sum";
EXPECT_NEAR(expected_value.value_squared, result.value_squared, 1e-3)
<< "optional iterator reduction sum squared";
EXPECT_EQ(expected_value.count, result.count) << "optional iterator reduction count";
}
}
#endif
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/value_iterator_test.cuh
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#pragma once
#include <tests/iterator/iterator_tests.cuh>
#include <cudf/detail/utilities/vector_factories.hpp>
#include <thrust/host_vector.h>
// tests for non-null iterator (pointer of device array)
template <typename T>
void non_null_iterator(IteratorTest<T>& testFixture)
{
auto host_array = cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -20, 45});
auto dev_array = cudf::detail::make_device_uvector_sync(
host_array, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
// calculate the expected value by CPU.
thrust::host_vector<T> replaced_array(host_array);
// driven by iterator as a pointer of device array.
auto it_dev = dev_array.begin();
T expected_value = *std::min_element(replaced_array.begin(), replaced_array.end());
testFixture.iterator_test_thrust(replaced_array, it_dev, dev_array.size());
testFixture.iterator_test_cub(expected_value, it_dev, dev_array.size());
// test column input
cudf::test::fixed_width_column_wrapper<T> w_col(host_array.begin(), host_array.end());
testFixture.values_equal_test(replaced_array, *cudf::column_device_view::create(w_col));
}
// Tests for null input iterator (column with null bitmap)
// Actually, we can use cub for reduction with nulls without creating custom
// kernel or multiple steps. We may accelerate the reduction for a column using
// cub
template <typename T>
void null_iterator(IteratorTest<T>& testFixture)
{
T init = cudf::test::make_type_param_scalar<T>(0);
// data and valid arrays
auto host_values = cudf::test::make_type_param_vector<T>({0, 6, 0, -14, 13, 64, -13, -20, 45});
std::vector<bool> host_bools({1, 1, 0, 1, 1, 1, 0, 1, 1});
// create a column with bool vector
cudf::test::fixed_width_column_wrapper<T> w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// calculate the expected value by CPU.
thrust::host_vector<T> replaced_array(host_values.size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
replaced_array.begin(),
[&](T x, bool b) { return (b) ? x : init; });
T expected_value = *std::min_element(replaced_array.begin(), replaced_array.end());
// TODO uncomment after time_point ostream operator<<
// std::cout << "expected <null_iterator> = " << expected_value << std::endl;
// GPU test
auto it_dev =
cudf::detail::make_null_replacement_iterator(*d_col, cudf::test::make_type_param_scalar<T>(0));
testFixture.iterator_test_cub(expected_value, it_dev, d_col->size());
testFixture.values_equal_test(replaced_array, *d_col);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/iterator/value_iterator_test_transform.cu
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
#include <tests/iterator/iterator_tests.cuh>
#include <thrust/functional.h>
#include <thrust/host_vector.h>
#include <thrust/iterator/transform_iterator.h>
struct TransformedIteratorTest : public IteratorTest<int8_t> {};
// Tests up cast reduction with null iterator.
// The up cast iterator will be created by transform_iterator and
// cudf::detail::make_null_replacement_iterator(col, T{0})
TEST_F(TransformedIteratorTest, null_iterator_upcast)
{
int const column_size{1000};
using T = int8_t;
using T_upcast = int64_t;
T init{0};
// data and valid arrays
std::vector<T> host_values(column_size);
std::vector<bool> host_bools(column_size);
cudf::test::UniformRandomGenerator<T> rng(-128, 127);
cudf::test::UniformRandomGenerator<bool> rbg;
std::generate(host_values.begin(), host_values.end(), [&rng]() { return rng.generate(); });
std::generate(host_bools.begin(), host_bools.end(), [&rbg]() { return rbg.generate(); });
cudf::test::fixed_width_column_wrapper<T> w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// calculate the expected value by CPU.
thrust::host_vector<T> replaced_array(d_col->size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
replaced_array.begin(),
[&](T x, bool b) { return (b) ? x : init; });
T_upcast expected_value = *std::min_element(replaced_array.begin(), replaced_array.end());
// std::cout << "expected <null_iterator> = " << expected_value << std::endl;
// GPU test
auto it_dev = cudf::detail::make_null_replacement_iterator(*d_col, T{0});
auto it_dev_upcast = thrust::make_transform_iterator(it_dev, thrust::identity<T_upcast>());
this->iterator_test_thrust(replaced_array, it_dev_upcast, d_col->size());
this->iterator_test_cub(expected_value, it_dev, d_col->size());
}
// Tests for square input iterator using helper strcut
// `cudf::transformer_squared<T, T_upcast>` The up cast iterator will be created
// by make_transform_iterator(
// cudf::detail::make_null_replacement_iterator(col, T{0}),
// cudf::detail::transformer_squared<T_upcast>)
TEST_F(TransformedIteratorTest, null_iterator_square)
{
int const column_size{1000};
using T = int8_t;
using T_upcast = int64_t;
T init{0};
cudf::transformer_squared<T_upcast> transformer{};
// data and valid arrays
std::vector<T> host_values(column_size);
std::vector<bool> host_bools(column_size);
cudf::test::UniformRandomGenerator<T> rng(-128, 127);
cudf::test::UniformRandomGenerator<bool> rbg;
std::generate(host_values.begin(), host_values.end(), [&rng]() { return rng.generate(); });
std::generate(host_bools.begin(), host_bools.end(), [&rbg]() { return rbg.generate(); });
cudf::test::fixed_width_column_wrapper<T> w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// calculate the expected value by CPU.
thrust::host_vector<T_upcast> replaced_array(d_col->size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
replaced_array.begin(),
[&](T x, bool b) { return (b) ? x * x : init; });
T_upcast expected_value = *std::min_element(replaced_array.begin(), replaced_array.end());
// std::cout << "expected <null_iterator> = " << expected_value << std::endl;
// GPU test
auto it_dev = cudf::detail::make_null_replacement_iterator(*d_col, T{0});
auto it_dev_upcast = thrust::make_transform_iterator(it_dev, thrust::identity<T_upcast>());
auto it_dev_squared = thrust::make_transform_iterator(it_dev_upcast, transformer);
this->iterator_test_thrust(replaced_array, it_dev_squared, d_col->size());
this->iterator_test_cub(expected_value, it_dev_squared, d_col->size());
}
// TODO only few types
TEST_F(TransformedIteratorTest, large_size_reduction)
{
using T = int64_t;
int const column_size{1000000};
const T init{0};
// data and valid arrays
std::vector<T> host_values(column_size);
std::vector<bool> host_bools(column_size);
cudf::test::UniformRandomGenerator<T> rng(-128, 128);
cudf::test::UniformRandomGenerator<bool> rbg;
std::generate(host_values.begin(), host_values.end(), [&rng]() { return rng.generate(); });
std::generate(host_bools.begin(), host_bools.end(), [&rbg]() { return rbg.generate(); });
cudf::test::fixed_width_column_wrapper<T> w_col(
host_values.begin(), host_values.end(), host_bools.begin());
auto d_col = cudf::column_device_view::create(w_col);
// calculate by cudf::reduce
thrust::host_vector<T> replaced_array(d_col->size());
std::transform(host_values.begin(),
host_values.end(),
host_bools.begin(),
replaced_array.begin(),
[&](T x, bool b) { return (b) ? x : init; });
T expected_value = *std::min_element(replaced_array.begin(), replaced_array.end());
// std::cout << "expected <null_iterator> = " << expected_value << std::endl;
// GPU test
auto it_dev = cudf::detail::make_null_replacement_iterator(*d_col, init);
this->iterator_test_thrust(replaced_array, it_dev, d_col->size());
this->iterator_test_cub(expected_value, it_dev, d_col->size());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/sort/sort_test.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/copying.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <thrust/host_vector.h>
#include <thrust/sort.h>
#include <type_traits>
#include <vector>
void run_sort_test(cudf::table_view input,
cudf::column_view expected_sorted_indices,
std::vector<cudf::order> column_order = {},
std::vector<cudf::null_order> null_precedence = {})
{
// Sorted table
auto got_sorted_table = cudf::sort(input, column_order, null_precedence);
auto expected_sorted_table = cudf::gather(input, expected_sorted_indices);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected_sorted_table->view(), got_sorted_table->view());
// Sorted by key
auto got_sort_by_key_table = cudf::sort_by_key(input, input, column_order, null_precedence);
auto expected_sort_by_key_table = cudf::gather(input, expected_sorted_indices);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected_sort_by_key_table->view(), got_sort_by_key_table->view());
}
using TestTypes = cudf::test::Concat<cudf::test::NumericTypes, // include integers, floats and bool
cudf::test::ChronoTypes>; // include timestamps and durations
template <typename T>
struct Sort : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(Sort, TestTypes);
TYPED_TEST(Sort, WithNullMax)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8, 5}, {1, 1, 0, 1, 1, 1}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k", "d"}, {1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2, 10}, {1, 1, 0, 1, 1, 1}};
cudf::table_view input{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{1, 0, 5, 3, 4, 2}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::ASCENDING, cudf::order::DESCENDING};
std::vector<cudf::null_order> null_precedence{
cudf::null_order::AFTER, cudf::null_order::AFTER, cudf::null_order::AFTER};
// Sorted order
auto got = cudf::sorted_order(input, column_order, null_precedence);
if (!std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order, null_precedence);
} else {
// for bools only validate that the null element landed at the back, since
// the rest of the values are equivalent and yields random sorted order.
auto to_host = [](cudf::column_view const& col) {
thrust::host_vector<int32_t> h_data(col.size());
CUDF_CUDA_TRY(cudaMemcpy(
h_data.data(), col.data<int32_t>(), h_data.size() * sizeof(int32_t), cudaMemcpyDefault));
return h_data;
};
thrust::host_vector<int32_t> h_exp = to_host(expected);
thrust::host_vector<int32_t> h_got = to_host(got->view());
EXPECT_EQ(h_exp[h_exp.size() - 1], h_got[h_got.size() - 1]);
// Run test for sort and sort_by_key
cudf::test::fixed_width_column_wrapper<int32_t> expected_for_bool{{0, 3, 5, 1, 4, 2}};
run_sort_test(input, expected_for_bool, column_order, null_precedence);
}
}
TYPED_TEST(Sort, WithNullMin)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}, {1, 1, 0, 1, 1}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"}, {1, 1, 0, 1, 1});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}, {1, 1, 0, 1, 1}};
cudf::table_view input{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 1, 0, 3, 4}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::ASCENDING, cudf::order::DESCENDING};
auto got = cudf::sorted_order(input, column_order);
if (!std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order);
} else {
// for bools only validate that the null element landed at the front, since
// the rest of the values are equivalent and yields random sorted order.
auto to_host = [](cudf::column_view const& col) {
thrust::host_vector<int32_t> h_data(col.size());
CUDF_CUDA_TRY(cudaMemcpy(
h_data.data(), col.data<int32_t>(), h_data.size() * sizeof(int32_t), cudaMemcpyDefault));
return h_data;
};
thrust::host_vector<int32_t> h_exp = to_host(expected);
thrust::host_vector<int32_t> h_got = to_host(got->view());
EXPECT_EQ(h_exp.front(), h_got.front());
// Run test for sort and sort_by_key
cudf::test::fixed_width_column_wrapper<int32_t> expected_for_bool{{2, 0, 3, 1, 4}};
run_sort_test(input, expected_for_bool, column_order);
}
}
TYPED_TEST(Sort, WithMixedNullOrder)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}, {0, 0, 1, 1, 0}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"}, {0, 1, 0, 0, 1});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}, {1, 0, 1, 0, 1}};
cudf::table_view input{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 3, 0, 1, 4}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::ASCENDING, cudf::order::ASCENDING};
std::vector<cudf::null_order> null_precedence{
cudf::null_order::AFTER, cudf::null_order::BEFORE, cudf::null_order::AFTER};
auto got = cudf::sorted_order(input, column_order, null_precedence);
if (!std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
} else {
// for bools only validate that the null element landed at the front, since
// the rest of the values are equivalent and yields random sorted order.
auto to_host = [](cudf::column_view const& col) {
thrust::host_vector<int32_t> h_data(col.size());
CUDF_CUDA_TRY(cudaMemcpy(
h_data.data(), col.data<int32_t>(), h_data.size() * sizeof(int32_t), cudaMemcpyDefault));
return h_data;
};
thrust::host_vector<int32_t> h_exp = to_host(expected);
thrust::host_vector<int32_t> h_got = to_host(got->view());
EXPECT_EQ(h_exp.front(), h_got.front());
}
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order, null_precedence);
}
TYPED_TEST(Sort, WithAllValid)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}};
cudf::table_view input{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 1, 0, 3, 4}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::ASCENDING, cudf::order::DESCENDING};
auto got = cudf::sorted_order(input, column_order);
// Skip validating bools order. Valid true bools are all
// equivalent, and yield random order after thrust::sort
if (!std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order);
} else {
// Run test for sort and sort_by_key
cudf::test::fixed_width_column_wrapper<int32_t> expected_for_bool{{2, 0, 3, 1, 4}};
run_sort_test(input, expected_for_bool, column_order);
}
}
TYPED_TEST(Sort, WithStructColumn)
{
using T = TypeParam;
std::initializer_list<std::string> names = {"Samuel Vimes",
"Carrot Ironfoundersson",
"Angua von Überwald",
"Cheery Littlebottom",
"Detritus",
"Mr Slant"};
auto num_rows{std::distance(names.begin(), names.end())};
auto names_col = cudf::test::strings_column_wrapper{names.begin(), names.end()};
auto ages_col = cudf::test::fixed_width_column_wrapper<T, int32_t>{{48, 27, 25, 31, 351, 351}};
auto is_human_col = cudf::test::fixed_width_column_wrapper<bool>{
{true, true, false, false, false, false}, {1, 1, 0, 1, 1, 0}};
auto struct_col =
cudf::test::structs_column_wrapper{{names_col, ages_col, is_human_col}}.release();
auto struct_col_view{struct_col->view()};
EXPECT_EQ(num_rows, struct_col->size());
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8, 9}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k", "a"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2, 20}};
cudf::table_view input{{col1, col2, col3, struct_col_view}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 1, 0, 3, 4, 5}};
std::vector<cudf::order> column_order{cudf::order::ASCENDING,
cudf::order::ASCENDING,
cudf::order::DESCENDING,
cudf::order::ASCENDING};
auto got = cudf::sorted_order(input, column_order);
// Skip validating bools order. Valid true bools are all
// equivalent, and yield random order after thrust::sort
if (!std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order);
} else {
// Run test for sort and sort_by_key
cudf::test::fixed_width_column_wrapper<int32_t> expected_for_bool{{2, 5, 3, 0, 1, 4}};
run_sort_test(input, expected_for_bool, column_order);
}
}
TYPED_TEST(Sort, WithNestedStructColumn)
{
using T = TypeParam;
std::initializer_list<std::string> names = {"Samuel Vimes",
"Carrot Ironfoundersson",
"Angua von Überwald",
"Cheery Littlebottom",
"Detritus",
"Mr Slant"};
std::vector<bool> v{1, 1, 0, 1, 1, 0};
auto names_col = cudf::test::strings_column_wrapper{names.begin(), names.end()};
auto ages_col = cudf::test::fixed_width_column_wrapper<T, int32_t>{{48, 27, 25, 31, 351, 351}};
auto is_human_col = cudf::test::fixed_width_column_wrapper<bool>{
{true, true, false, false, false, false}, {1, 1, 0, 1, 1, 0}};
auto struct_col1 = cudf::test::structs_column_wrapper{{names_col, ages_col, is_human_col}, v};
auto ages_col2 = cudf::test::fixed_width_column_wrapper<T, int32_t>{{48, 27, 25, 31, 351, 351}};
auto struct_col2 = cudf::test::structs_column_wrapper{{ages_col2, struct_col1}}.release();
auto struct_col_view{struct_col2->view()};
cudf::test::fixed_width_column_wrapper<T> col1{{6, 6, 6, 6, 6, 6}};
cudf::test::fixed_width_column_wrapper<T> col2{{1, 1, 1, 2, 2, 2}};
cudf::table_view input{{col1, col2, struct_col_view}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{3, 5, 4, 2, 1, 0}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::DESCENDING, cudf::order::ASCENDING};
auto got = cudf::sorted_order(input, column_order);
// Skip validating bools order. Valid true bools are all
// equivalent, and yield random order after thrust::sort
if (!std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order);
} else {
// Run test for sort and sort_by_key
cudf::test::fixed_width_column_wrapper<int32_t> expected_for_bool{{2, 5, 1, 3, 4, 0}};
run_sort_test(input, expected_for_bool, column_order);
}
}
TYPED_TEST(Sort, WithNullableStructColumn)
{
// Test for a struct column that has nulls on struct layer but not pushed down on the child
using T = int;
using fwcw = cudf::test::fixed_width_column_wrapper<T>;
using mask = std::vector<bool>;
auto make_struct = [&](std::vector<std::unique_ptr<cudf::column>> child_cols,
std::vector<bool> nulls) {
cudf::test::structs_column_wrapper struct_col(std::move(child_cols));
auto struct_ = struct_col.release();
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(nulls.begin(), nulls.end());
struct_->set_null_mask(std::move(null_mask), null_count);
return struct_;
};
{
/*
/+-------------+
|s1{s2{a,b}, c}|
+--------------+
0 | { {1, 1}, 5}|
1 | { {1, 2}, 4}|
2 | {@{2, 1}, 6}|
3 | {@{2, 2}, 5}|
4 | @{ {2, 2}, 3}|
5 | @{ {1, 1}, 3}|
6 | { {1, 2}, 3}|
7 | {@{1, 1}, 4}|
8 | { {2, 1}, 5}|
+--------------+
Intermediate representation:
s1{s2{a}}, b, c
*/
auto col_a = fwcw{1, 1, 2, 2, 2, 1, 1, 1, 2};
auto col_b = fwcw{1, 2, 1, 2, 2, 1, 2, 1, 1};
auto s2_mask = mask{1, 1, 0, 0, 1, 1, 1, 0, 1};
auto col_c = fwcw{5, 4, 6, 5, 3, 3, 3, 4, 5};
auto s1_mask = mask{1, 1, 1, 1, 0, 0, 1, 1, 1};
std::vector<std::unique_ptr<cudf::column>> s2_children;
s2_children.push_back(col_a.release());
s2_children.push_back(col_b.release());
auto s2 = make_struct(std::move(s2_children), s2_mask);
std::vector<std::unique_ptr<cudf::column>> s1_children;
s1_children.push_back(std::move(s2));
s1_children.push_back(col_c.release());
auto s1 = make_struct(std::move(s1_children), s1_mask);
auto expect = fwcw{4, 5, 7, 3, 2, 0, 6, 1, 8};
run_sort_test(cudf::table_view({s1->view()}), expect);
}
{ /*
/+-------------+
|s1{a,s2{b, c}}|
+--------------+
0 | {1, {1, 5}}|
1 | {1, {2, 4}}|
2 | {2, @{2, 6}}|
3 | {2, @{1, 5}}|
4 | @{2, {2, 3}}|
5 | @{1, {1, 3}}|
6 | {1, {2, 3}}|
7 | {1, @{1, 4}}|
8 | {2, {1, 5}}|
+--------------+
Intermediate representation:
s1{a}, s2{b}, c
*/
auto s1_mask = mask{1, 1, 1, 1, 0, 0, 1, 1, 1};
auto col_a = fwcw{1, 1, 2, 2, 2, 1, 1, 1, 2};
auto s2_mask = mask{1, 1, 0, 0, 1, 1, 1, 0, 1};
auto col_b = fwcw{1, 2, 1, 2, 2, 1, 2, 1, 1};
auto col_c = fwcw{5, 4, 6, 5, 3, 3, 3, 4, 5};
std::vector<std::unique_ptr<cudf::column>> s22_children;
s22_children.push_back(col_b.release());
s22_children.push_back(col_c.release());
auto s22 = make_struct(std::move(s22_children), s2_mask);
std::vector<std::unique_ptr<cudf::column>> s12_children;
s12_children.push_back(col_a.release());
s12_children.push_back(std::move(s22));
auto s12 = make_struct(std::move(s12_children), s1_mask);
auto expect = fwcw{4, 5, 7, 0, 6, 1, 2, 3, 8};
run_sort_test(cudf::table_view({s12->view()}), expect);
}
}
TYPED_TEST(Sort, WithSingleStructColumn)
{
using T = TypeParam;
std::initializer_list<std::string> names = {"Samuel Vimes",
"Carrot Ironfoundersson",
"Angua von Überwald",
"Cheery Littlebottom",
"Detritus",
"Mr Slant"};
std::vector<bool> v{1, 1, 0, 1, 1, 0};
auto names_col = cudf::test::strings_column_wrapper{names.begin(), names.end()};
auto ages_col = cudf::test::fixed_width_column_wrapper<T, int32_t>{{48, 27, 25, 31, 351, 351}};
auto is_human_col = cudf::test::fixed_width_column_wrapper<bool>{
{true, true, false, false, false, false}, {1, 1, 0, 1, 1, 0}};
auto struct_col =
cudf::test::structs_column_wrapper{{names_col, ages_col, is_human_col}, v}.release();
auto struct_col_view{struct_col->view()};
cudf::table_view input{{struct_col_view}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 5, 1, 3, 4, 0}};
std::vector<cudf::order> column_order{cudf::order::ASCENDING};
auto got = cudf::sorted_order(input, column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order);
}
TYPED_TEST(Sort, WithSlicedStructColumn)
{
using T = TypeParam;
/*
/+-------------+
| s|
+--------------+
0 | {"bbe", 1, 7}|
1 | {"bbe", 1, 8}|
2 | {"aaa", 0, 1}|
3 | {"abc", 0, 1}|
4 | {"ab", 0, 9}|
5 | {"za", 2, 5}|
6 | {"b", 1, 7}|
7 | { @, 3, 3}|
+--------------+
*/
// clang-format off
using FWCW = cudf::test::fixed_width_column_wrapper<T, int32_t>;
std::vector<bool> string_valids{ 1, 1, 1, 1, 1, 1, 1, 0};
std::initializer_list<std::string> names = {"bbe", "bbe", "aaa", "abc", "ab", "za", "b", "x"};
auto col2 = FWCW{{ 1, 1, 0, 0, 0, 2, 1, 3}};
auto col3 = FWCW{{ 7, 8, 1, 1, 9, 5, 7, 3}};
auto col1 = cudf::test::strings_column_wrapper{names.begin(), names.end(), string_valids.begin()};
auto struct_col = cudf::test::structs_column_wrapper{{col1, col2, col3}}.release();
// clang-format on
auto struct_col_view{struct_col->view()};
cudf::table_view input{{struct_col_view}};
auto sliced_columns = cudf::split(struct_col_view, std::vector<cudf::size_type>{3});
auto sliced_tables = cudf::split(input, std::vector<cudf::size_type>{3});
std::vector<cudf::order> column_order{cudf::order::ASCENDING};
/*
asce_null_first sliced[3:]
/+-------------+
| s|
+--------------+
7 | { @, 3, 3}| 7=4
2 | {"aaa", 0, 1}|
4 | {"ab", 0, 9}| 4=1
3 | {"abc", 0, 1}| 3=0
6 | {"b", 1, 7}| 6=3
0 | {"bbe", 1, 7}|
1 | {"bbe", 1, 8}|
5 | {"za", 2, 5}| 5=2
+--------------+
*/
// normal
cudf::test::fixed_width_column_wrapper<int32_t> expected{{7, 2, 4, 3, 6, 0, 1, 5}};
auto got = cudf::sorted_order(input, column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order);
// table with sliced column
cudf::table_view input2{{sliced_columns[1]}};
cudf::test::fixed_width_column_wrapper<int32_t> expected2{{4, 1, 0, 3, 2}};
got = cudf::sorted_order(input2, column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected2, got->view());
// Run test for sort and sort_by_key
run_sort_test(input2, expected2, column_order);
// sliced table[1]
cudf::test::fixed_width_column_wrapper<int32_t> expected3{{4, 1, 0, 3, 2}};
got = cudf::sorted_order(sliced_tables[1], column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected3, got->view());
// Run test for sort and sort_by_key
run_sort_test(sliced_tables[1], expected3, column_order);
// sliced table[0]
cudf::test::fixed_width_column_wrapper<int32_t> expected4{{2, 0, 1}};
got = cudf::sorted_order(sliced_tables[0], column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected4, got->view());
// Run test for sort and sort_by_key
run_sort_test(sliced_tables[0], expected4, column_order);
}
TYPED_TEST(Sort, SlicedColumns)
{
using T = TypeParam;
using FWCW = cudf::test::fixed_width_column_wrapper<T, int32_t>;
// clang-format off
std::vector<bool> string_valids{ 1, 1, 1, 1, 1, 1, 1, 0};
std::initializer_list<std::string> names = {"bbe", "bbe", "aaa", "abc", "ab", "za", "b", "x"};
auto col2 = FWCW{{ 7, 8, 1, 1, 9, 5, 7, 3}};
auto col1 = cudf::test::strings_column_wrapper{names.begin(), names.end(), string_valids.begin()};
// clang-format on
cudf::table_view input{{col1, col2}};
auto sliced_columns1 = cudf::split(col1, std::vector<cudf::size_type>{3});
auto sliced_columns2 = cudf::split(col1, std::vector<cudf::size_type>{3});
auto sliced_tables = cudf::split(input, std::vector<cudf::size_type>{3});
std::vector<cudf::order> column_order{cudf::order::ASCENDING, cudf::order::ASCENDING};
// normal
// cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 3, 7, 5, 0, 6, 1, 4}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{7, 2, 4, 3, 6, 0, 1, 5}};
auto got = cudf::sorted_order(input, column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order);
// table with sliced column
cudf::table_view input2{{sliced_columns1[1], sliced_columns2[1]}};
// cudf::test::fixed_width_column_wrapper<int32_t> expected2{{0, 4, 2, 3, 1}};
cudf::test::fixed_width_column_wrapper<int32_t> expected2{{4, 1, 0, 3, 2}};
got = cudf::sorted_order(input2, column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected2, got->view());
// Run test for sort and sort_by_key
run_sort_test(input2, expected2, column_order);
}
TYPED_TEST(Sort, WithStructColumnCombinations)
{
using T = TypeParam;
using FWCW = cudf::test::fixed_width_column_wrapper<T, int32_t>;
// clang-format off
/*
+------------+
| s|
+------------+
0 | {0, null}|
1 | {1, null}|
2 | null|
3 |{null, null}|
4 | null|
5 |{null, null}|
6 | {null, 1}|
7 | {null, 0}|
+------------+
*/
std::vector<bool> struct_valids{1, 1, 0, 1, 0, 1, 1, 1};
auto col1 = FWCW{{ 0, 1, 9, -1, 9, -1, -1, -1}, {1, 1, 1, 0, 1, 0, 0, 0}};
auto col2 = FWCW{{-1, -1, 9, -1, 9, -1, 1, 0}, {0, 0, 1, 0, 1, 0, 1, 1}};
auto struct_col = cudf::test::structs_column_wrapper{{col1, col2}, struct_valids}.release();
/*
desc_nulls_first desc_nulls_last asce_nulls_first asce_nulls_last
+------------+ +------------+ +------------+ +------------+
| s| | s| | s| | s|
+------------+ +------------+ +------------+ +------------+
2 | null| 1 | {1, null}| 2 | null| 0 | {0, null}|
4 | null| 0 | {0, null}| 4 | null| 1 | {1, null}|
3 |{null, null}| 6 | {null, 1}| 3 |{null, null}| 7 | {null, 0}|
5 |{null, null}| 7 | {null, 0}| 5 |{null, null}| 6 | {null, 1}|
6 | {null, 1}| 3 |{null, null}| 7 | {null, 0}| 3 |{null, null}|
7 | {null, 0}| 5 |{null, null}| 6 | {null, 1}| 5 |{null, null}|
1 | {1, null}| 2 | null| 0 | {0, null}| 2 | null|
0 | {0, null}| 4 | null| 1 | {1, null}| 4 | null|
+------------+ +------------+ +------------+ +------------+
*/
// clang-format on
auto struct_col_view{struct_col->view()};
cudf::table_view input{{struct_col_view}};
std::vector<cudf::order> column_order1{cudf::order::DESCENDING};
// desc_nulls_first
cudf::test::fixed_width_column_wrapper<int32_t> expected1{{2, 4, 3, 5, 6, 7, 1, 0}};
auto got = cudf::sorted_order(input, column_order1, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected1, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected1, column_order1, {cudf::null_order::AFTER});
// desc_nulls_last
cudf::test::fixed_width_column_wrapper<int32_t> expected2{{1, 0, 6, 7, 3, 5, 2, 4}};
got = cudf::sorted_order(input, column_order1, {cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected2, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected2, column_order1, {cudf::null_order::BEFORE});
// asce_nulls_first
std::vector<cudf::order> column_order2{cudf::order::ASCENDING};
cudf::test::fixed_width_column_wrapper<int32_t> expected3{{2, 4, 3, 5, 7, 6, 0, 1}};
got = cudf::sorted_order(input, column_order2, {cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected3, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected3, column_order2, {cudf::null_order::BEFORE});
// asce_nulls_last
cudf::test::fixed_width_column_wrapper<int32_t> expected4{{0, 1, 7, 6, 3, 5, 2, 4}};
got = cudf::sorted_order(input, column_order2, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected4, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected4, column_order2, {cudf::null_order::AFTER});
}
TYPED_TEST(Sort, WithStructColumnCombinationsWithoutNulls)
{
using T = TypeParam;
using FWCW = cudf::test::fixed_width_column_wrapper<T, int32_t>;
// clang-format off
/*
+------------+
| s|
+------------+
0 | {0, null}|
1 | {1, null}|
2 | {9, 9}|
3 |{null, null}|
4 | {9, 9}|
5 |{null, null}|
6 | {null, 1}|
7 | {null, 0}|
+------------+
*/
auto col1 = FWCW{{ 0, 1, 9, -1, 9, -1, -1, -1}, {1, 1, 1, 0, 1, 0, 0, 0}};
auto col2 = FWCW{{-1, -1, 9, -1, 9, -1, 1, 0}, {0, 0, 1, 0, 1, 0, 1, 1}};
auto struct_col = cudf::test::structs_column_wrapper{{col1, col2}}.release();
/* (nested columns are always nulls_first, spark requirement)
desc_nulls_* asce_nulls_*
+------------+ +------------+
| s| | s|
+------------+ +------------+
3 |{null, null}| 0 | {0, null}|
5 |{null, null}| 1 | {1, null}|
6 | {null, 1}| 2 | {9, 9}|
7 | {null, 0}| 4 | {9, 9}|
2 | {9, 9}| 7 | {null, 0}|
4 | {9, 9}| 6 | {null, 1}|
1 | {1, null}| 3 |{null, null}|
0 | {0, null}| 5 |{null, null}|
+------------+ +------------+
*/
// clang-format on
auto struct_col_view{struct_col->view()};
cudf::table_view input{{struct_col_view}};
std::vector<cudf::order> column_order{cudf::order::DESCENDING};
// desc_nulls_first
auto const expected1 = []() {
if constexpr (std::is_same_v<T, bool>) {
return cudf::test::fixed_width_column_wrapper<int32_t>{{3, 5, 6, 7, 1, 2, 4, 0}};
}
return cudf::test::fixed_width_column_wrapper<int32_t>{{3, 5, 6, 7, 2, 4, 1, 0}};
}();
auto got = cudf::sorted_order(input, column_order, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected1, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected1, column_order, {cudf::null_order::AFTER});
// desc_nulls_last
cudf::test::fixed_width_column_wrapper<int32_t> expected2{{2, 4, 1, 0, 6, 7, 3, 5}};
got = cudf::sorted_order(input, column_order, {cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected2, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected2, column_order, {cudf::null_order::BEFORE});
// asce_nulls_first
std::vector<cudf::order> column_order2{cudf::order::ASCENDING};
cudf::test::fixed_width_column_wrapper<int32_t> expected3{{3, 5, 7, 6, 0, 1, 2, 4}};
got = cudf::sorted_order(input, column_order2, {cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected3, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected3, column_order2, {cudf::null_order::BEFORE});
// asce_nulls_last
auto const expected4 = []() {
if constexpr (std::is_same_v<T, bool>) {
return cudf::test::fixed_width_column_wrapper<int32_t>{{0, 2, 4, 1, 7, 6, 3, 5}};
}
return cudf::test::fixed_width_column_wrapper<int32_t>{{0, 1, 2, 4, 7, 6, 3, 5}};
}();
got = cudf::sorted_order(input, column_order2, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected4, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected4, column_order2, {cudf::null_order::AFTER});
}
TYPED_TEST(Sort, MismatchInColumnOrderSize)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}};
cudf::table_view input{{col1, col2, col3}};
std::vector<cudf::order> column_order{cudf::order::ASCENDING, cudf::order::DESCENDING};
EXPECT_THROW(cudf::sorted_order(input, column_order), cudf::logic_error);
EXPECT_THROW(cudf::sort(input, column_order), cudf::logic_error);
EXPECT_THROW(cudf::sort_by_key(input, input, column_order), cudf::logic_error);
}
TYPED_TEST(Sort, MismatchInNullPrecedenceSize)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}};
cudf::table_view input{{col1, col2, col3}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::DESCENDING, cudf::order::DESCENDING};
std::vector<cudf::null_order> null_precedence{cudf::null_order::AFTER, cudf::null_order::BEFORE};
EXPECT_THROW(cudf::sorted_order(input, column_order, null_precedence), cudf::logic_error);
EXPECT_THROW(cudf::sort(input, column_order, null_precedence), cudf::logic_error);
EXPECT_THROW(cudf::sort_by_key(input, input, column_order, null_precedence), cudf::logic_error);
}
TYPED_TEST(Sort, ZeroSizedColumns)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{};
cudf::table_view input{{col1}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{};
std::vector<cudf::order> column_order{cudf::order::ASCENDING};
auto got = cudf::sorted_order(input, column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// Run test for sort and sort_by_key
run_sort_test(input, expected, column_order);
}
TYPED_TEST(Sort, WithListColumn)
{
using T = TypeParam;
if (std::is_same_v<T, bool>) { GTEST_SKIP(); }
/*
[
[[1, 2, 3], [], [4, 5], [], [0, 6, 0]], 0
[[1, 2, 3], [], [4, 5], [], [0, 6, 0]], 1
[[1, 2, 3], [], [4, 5], [0, 6, 0]], 2
[[1, 2], [3], [4, 5], [0, 6, 0]], 3
[[7, 8], []], 4
[[], [], []], 5
[[]], 6
[[10]], 7
[] 8
]
*/
using lcw = cudf::test::lists_column_wrapper<T, int32_t>;
lcw col{{{1, 2, 3}, {}, {4, 5}, {}, {0, 6, 0}},
{{1, 2, 3}, {}, {4, 5}, {}, {0, 6, 0}},
{{1, 2, 3}, {}, {4, 5}, {0, 6, 0}},
{{1, 2}, {3}, {4, 5}, {0, 6, 0}},
{{7, 8}, {}},
lcw{lcw{}, lcw{}, lcw{}},
lcw{lcw{}},
{lcw{10}},
lcw{}};
auto expect = cudf::test::fixed_width_column_wrapper<cudf::size_type>{8, 6, 5, 3, 0, 1, 2, 4, 7};
auto result = cudf::sorted_order(cudf::table_view({col}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expect, *result);
}
TYPED_TEST(Sort, WithNullableListColumn)
{
using T = TypeParam;
if (std::is_same_v<T, bool>) { GTEST_SKIP(); }
using lcw = cudf::test::lists_column_wrapper<T, int32_t>;
using cudf::test::iterators::nulls_at;
/*
[
[[1, 2, 3], [], [4, 5], [], [0, 6, 0]], 0
[[1, 2, 3], [], [4, 5], NULL, [0, 6, 0]], 1
[[1, 2, 3], [], [4, 5], [0, 6, 0]], 2
[[1, 2], [3], [4, 5], [0, 6, 0]], 3
[[1, 2], [3], [4, 5], [NULL, 6, 0]], 4
[[7, 8], []], 5
[[], [], []], 6
[[]], 7
[[10]], 8
[], 9
[[1, 2], [3], [4, 5], [NULL, 6, NULL]], 10
[[1, 2], [3], [4, 5], [NULL, 7]] 11
]
*/
lcw col{
{{1, 2, 3}, {}, {4, 5}, {}, {0, 6, 0}}, // 0
{{{1, 2, 3}, {}, {4, 5}, {}, {0, 6, 0}}, nulls_at({3})}, // 1
{{1, 2, 3}, {}, {4, 5}, {0, 6, 0}}, // 2
{{1, 2}, {3}, {4, 5}, {0, 6, 0}}, // 3
{{1, 2}, {3}, {4, 5}, {{0, 6, 0}, nulls_at({0})}}, // 4
{{7, 8}, {}}, // 5
lcw{lcw{}, lcw{}, lcw{}}, // 6
lcw{lcw{}}, // 7
{lcw{10}}, // 8
lcw{}, // 9
{{1, 2}, {3}, {4, 5}, {{0, 6, 0}, nulls_at({0, 2})}}, // 10
{{1, 2}, {3}, {4, 5}, {{0, 7}, nulls_at({0})}}, // 11
};
auto expect =
cudf::test::fixed_width_column_wrapper<cudf::size_type>{9, 7, 6, 10, 4, 11, 3, 1, 0, 2, 5, 8};
auto result = cudf::sorted_order(cudf::table_view({col}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expect, *result);
}
TYPED_TEST(Sort, MoreLists)
{
using T = TypeParam;
if (std::is_same_v<T, bool>) { GTEST_SKIP(); }
using lcw = cudf::test::lists_column_wrapper<T, int32_t>;
using cudf::test::iterators::null_at;
using cudf::test::iterators::nulls_at;
{
/*
[
[[NULL], [-21827]], 0
[[NULL, NULL]] 1
]
*/
lcw col{
lcw{lcw{{0}, nulls_at({0})}, lcw{-21827}}, // 0
lcw{lcw{{0, 0}, nulls_at({0, 1})}} // 1
};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{0, 1}};
auto result = cudf::sorted_order(cudf::table_view({col}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
/*
[
[[1], [2]], 0
[[1, NULL], [2, 3]] 1
]
*/
auto const col = lcw{lcw{lcw{1}, lcw{2}}, lcw{lcw{{1, 0}, nulls_at({1})}, lcw{2, 3}}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{0, 1}};
auto result = cudf::sorted_order(cudf::table_view({col}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
/*
List<List<List<int>
[
[[[0, 0, 0]]], 0
[[[0], [0], [0]]], 1
[[[0]], [[0]], [[0]]], 2
[[[0, 0, 0]], [[0]], [[0]]], 3
[[[0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0]], [[0]]] 4
]
*/
lcw col{lcw{lcw{lcw{0, 0, 0}}},
lcw{lcw{lcw{0}, lcw{0}, lcw{0}}},
lcw{lcw{lcw{0}}, lcw{lcw{0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0, 0, 0}}, lcw{lcw{0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0, 0}}, lcw{lcw{0, 0, 0, 0, 0, 0, 0, 0}}, lcw{lcw{0}}}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 1, 4, 0, 3}};
auto result = cudf::sorted_order(cudf::table_view({col}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
{
/*
[
[], 0
[1], 1
[2, 2] 2
[2, 3] 3
[] 4
NULL 5
[2] 6
NULL 7
[NULL] 8
]
*/
lcw col{{{}, {1}, {2, 2}, {2, 3}, {}, {} /*NULL*/, {2}, {} /*NULL*/, {{0}, null_at(0)}},
nulls_at({5, 7})};
// ASCENDING, null_order::BEFORE
{
cudf::test::fixed_width_column_wrapper<int32_t> expected{{5, 7, 0, 4, 8, 1, 6, 2, 3}};
auto result = cudf::sorted_order(cudf::table_view({col}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
// ASCENDING, null_order::AFTER
{
cudf::test::fixed_width_column_wrapper<int32_t> expected{{0, 4, 8, 1, 6, 2, 3, 5, 7}};
auto result = cudf::sorted_order(cudf::table_view({col}), {}, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
// DESCENDING, null_order::BEFORE
{
cudf::test::fixed_width_column_wrapper<int32_t> expected{{3, 2, 6, 1, 8, 0, 4, 5, 7}};
auto result = cudf::sorted_order(cudf::table_view({col}), {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
// DESCENDING, null_order::AFTER
{
cudf::test::fixed_width_column_wrapper<int32_t> expected{{5, 7, 3, 2, 6, 1, 8, 0, 4}};
auto result = cudf::sorted_order(
cudf::table_view({col}), {cudf::order::DESCENDING}, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
{
lcw col{lcw{lcw{}, lcw{-729297378, -627961465}},
lcw{lcw{{0}, null_at(0)}, lcw{881899016, -1415270016}}};
{
cudf::test::fixed_width_column_wrapper<int32_t> expected{{0, 1}};
auto result = cudf::sorted_order(cudf::table_view({col}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, *result);
}
}
}
TYPED_TEST(Sort, WithSlicedListColumn)
{
using T = TypeParam;
if (std::is_same_v<T, bool>) { GTEST_SKIP(); }
using lcw = cudf::test::lists_column_wrapper<T, int32_t>;
using cudf::test::iterators::nulls_at;
lcw col{
{{1, 2, 3}, {}, {4, 5}, {}, {0, 6, 0}}, //
{{{1, 2, 3}, {}, {4, 5}, {}, {0, 6, 0}}, nulls_at({3})}, // 0
{{1, 2, 3}, {}, {4, 5}, {0, 6, 0}}, // 1
{{1, 2}, {3}, {4, 5}, {0, 6, 0}}, // 2
{{1, 2}, {3}, {4, 5}, {{0, 6, 0}, nulls_at({0})}}, // 3
{{7, 8}, {}}, // 4
lcw{lcw{}, lcw{}, lcw{}}, // 5
lcw{lcw{}}, // 6
{lcw{10}}, // 7
lcw{}, // 8
{{1, 2}, {3}, {4, 5}, {{0, 6, 0}, nulls_at({0, 2})}}, // 9
{{1, 2}, {3}, {4, 5}, {{0, 7}, nulls_at({0})}}, //
};
auto sliced_col = cudf::slice(col, {1, 10});
auto expect = cudf::test::fixed_width_column_wrapper<cudf::size_type>{8, 6, 5, 3, 2, 0, 1, 4, 7};
auto result = cudf::sorted_order(cudf::table_view({sliced_col}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expect, *result);
}
TYPED_TEST(Sort, WithEmptyListColumn)
{
using T = TypeParam;
if (std::is_same_v<T, bool>) { GTEST_SKIP(); }
auto L1 = cudf::make_lists_column(0,
cudf::make_empty_column(cudf::data_type(cudf::type_id::INT32)),
cudf::make_empty_column(cudf::data_type{cudf::type_id::INT64}),
0,
{});
auto L0 = cudf::make_lists_column(
3, cudf::test::fixed_width_column_wrapper<int32_t>{0, 0, 0, 0}.release(), std::move(L1), 0, {});
auto expect = cudf::test::fixed_width_column_wrapper<cudf::size_type>{0, 1, 2};
auto result = cudf::sorted_order(cudf::table_view({*L0}));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expect, *result);
}
struct SortByKey : public cudf::test::BaseFixture {};
TEST_F(SortByKey, ValueKeysSizeMismatch)
{
using T = int64_t;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}};
cudf::table_view values{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<T> key_col{{5, 4, 3, 5}};
cudf::table_view keys{{key_col}};
EXPECT_THROW(cudf::sort_by_key(values, keys), cudf::logic_error);
}
template <typename T>
struct SortFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(SortFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(SortFixedPointTest, SortedOrderGather)
{
using namespace numeric;
using decimalXX = TypeParam;
auto const ZERO = decimalXX{0, scale_type{0}};
auto const ONE = decimalXX{1, scale_type{0}};
auto const TWO = decimalXX{2, scale_type{0}};
auto const THREE = decimalXX{3, scale_type{0}};
auto const FOUR = decimalXX{4, scale_type{0}};
auto const input_vec = std::vector<decimalXX>{TWO, ONE, ZERO, FOUR, THREE};
auto const index_vec = std::vector<cudf::size_type>{2, 1, 0, 4, 3};
auto const sorted_vec = std::vector<decimalXX>{ZERO, ONE, TWO, THREE, FOUR};
auto const input_col =
cudf::test::fixed_width_column_wrapper<decimalXX>(input_vec.begin(), input_vec.end());
auto const index_col =
cudf::test::fixed_width_column_wrapper<cudf::size_type>(index_vec.begin(), index_vec.end());
auto const sorted_col =
cudf::test::fixed_width_column_wrapper<decimalXX>(sorted_vec.begin(), sorted_vec.end());
auto const sorted_table = cudf::table_view{{sorted_col}};
auto const input_table = cudf::table_view{{input_col}};
auto const indices = cudf::sorted_order(input_table);
auto const sorted = cudf::gather(input_table, indices->view());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(index_col, indices->view());
CUDF_TEST_EXPECT_TABLES_EQUAL(sorted_table, sorted->view());
}
struct SortCornerTest : public cudf::test::BaseFixture {};
TEST_F(SortCornerTest, WithEmptyStructColumn)
{
using int_col = cudf::test::fixed_width_column_wrapper<int32_t>;
// struct{}, int, int
int_col col_for_mask{{0, 0, 0, 0, 0, 0}, {1, 0, 1, 1, 1, 1}};
auto null_mask = cudf::copy_bitmask(col_for_mask);
auto struct_col = cudf::make_structs_column(
6, {}, cudf::column_view(col_for_mask).null_count(), std::move(null_mask));
int_col col1{{1, 2, 3, 1, 2, 3}};
int_col col2{{1, 1, 1, 2, 2, 2}};
cudf::table_view input{{struct_col->view(), col1, col2}};
int_col expected{{1, 0, 3, 4, 2, 5}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::ASCENDING, cudf::order::ASCENDING};
auto got = cudf::sorted_order(input, column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
// struct{struct{}, int}
int_col col3{{0, 1, 2, 3, 4, 5}};
std::vector<std::unique_ptr<cudf::column>> child_columns;
child_columns.push_back(std::move(struct_col));
child_columns.push_back(col3.release());
auto struct_col2 =
cudf::make_structs_column(6, std::move(child_columns), 0, rmm::device_buffer{});
cudf::table_view input2{{struct_col2->view()}};
int_col expected2{{5, 4, 3, 2, 0, 1}};
auto got2 = cudf::sorted_order(input2, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected2, got2->view());
// struct{struct{}, struct{int}}
int_col col_for_mask2{{0, 0, 0, 0, 0, 0}, {1, 0, 1, 1, 0, 1}};
auto null_mask2 = cudf::copy_bitmask(col_for_mask2);
std::vector<std::unique_ptr<cudf::column>> child_columns2;
auto child_col_1 = cudf::make_structs_column(
6, {}, cudf::column_view(col_for_mask2).null_count(), std::move(null_mask2));
child_columns2.push_back(std::move(child_col_1));
int_col col4{{5, 4, 3, 2, 1, 0}};
std::vector<std::unique_ptr<cudf::column>> grand_child;
grand_child.push_back(std::move(col4.release()));
auto child_col_2 = cudf::make_structs_column(6, std::move(grand_child), 0, rmm::device_buffer{});
child_columns2.push_back(std::move(child_col_2));
auto struct_col3 =
cudf::make_structs_column(6, std::move(child_columns2), 0, rmm::device_buffer{});
cudf::table_view input3{{struct_col3->view()}};
int_col expected3{{4, 1, 5, 3, 2, 0}};
auto got3 = cudf::sorted_order(input3, {cudf::order::ASCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected3, got3->view());
};
using SortDouble = Sort<double>;
TEST_F(SortDouble, InfinityAndNan)
{
auto constexpr NaN = std::numeric_limits<double>::quiet_NaN();
auto constexpr Inf = std::numeric_limits<double>::infinity();
auto input = cudf::test::fixed_width_column_wrapper<double>(
{-0.0, -NaN, -NaN, NaN, Inf, -Inf, 7.0, 5.0, 6.0, NaN, Inf, -Inf, -NaN, -NaN, -0.0});
auto expected = // -inf,-inf,-0,-0,5,6,7,inf,inf,-nan,-nan,nan,nan,-nan,-nan
cudf::test::fixed_width_column_wrapper<cudf::size_type>(
{5, 11, 0, 14, 7, 8, 6, 4, 10, 1, 2, 3, 9, 12, 13});
auto results = cudf::sorted_order(cudf::table_view({input}));
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(results->view(), expected);
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/sort/stable_sort_tests.cpp
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/copying.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <thrust/host_vector.h>
#include <thrust/sort.h>
#include <type_traits>
#include <vector>
void run_stable_sort_test(cudf::table_view input,
cudf::column_view expected_sorted_indices,
std::vector<cudf::order> column_order = {},
std::vector<cudf::null_order> null_precedence = {})
{
auto got_sort_by_key_table = cudf::sort_by_key(input, input, column_order, null_precedence);
auto expected_sort_by_key_table = cudf::gather(input, expected_sorted_indices);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected_sort_by_key_table->view(), got_sort_by_key_table->view());
}
using TestTypes = cudf::test::Concat<cudf::test::NumericTypes, // include integers, floats and bool
cudf::test::ChronoTypes>; // include timestamps and durations
template <typename T>
struct StableSort : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(StableSort, TestTypes);
TYPED_TEST(StableSort, MixedNullOrder)
{
using T = TypeParam;
using R = int32_t;
cudf::test::fixed_width_column_wrapper<T> col1({0, 1, 1, 0, 0, 1, 0, 1},
{0, 1, 1, 1, 1, 1, 1, 1});
cudf::test::strings_column_wrapper col2({"2", "a", "b", "x", "k", "a", "x", "a"},
{1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<R> expected{{4, 3, 6, 1, 5, 7, 2, 0}};
auto got = cudf::stable_sorted_order(cudf::table_view({col1, col2}),
{cudf::order::ASCENDING, cudf::order::ASCENDING},
{cudf::null_order::AFTER, cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(StableSort, WithNullMax)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8, 5}, {1, 1, 0, 1, 1, 1}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k", "d"}, {1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 10, 2, 10}, {1, 1, 0, 1, 1, 1}};
cudf::table_view input{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{1, 0, 3, 5, 4, 2}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::ASCENDING, cudf::order::DESCENDING};
std::vector<cudf::null_order> null_precedence{
cudf::null_order::AFTER, cudf::null_order::AFTER, cudf::null_order::AFTER};
auto got = cudf::stable_sorted_order(input, column_order, null_precedence);
if (not std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
run_stable_sort_test(input, expected, column_order, null_precedence);
} else {
// for bools only validate that the null element landed at the back, since
// the rest of the values are equivalent and yields random sorted order.
auto to_host = [](cudf::column_view const& col) {
thrust::host_vector<int32_t> h_data(col.size());
CUDF_CUDA_TRY(cudaMemcpy(
h_data.data(), col.data<int32_t>(), h_data.size() * sizeof(int32_t), cudaMemcpyDefault));
return h_data;
};
thrust::host_vector<int32_t> h_exp = to_host(expected);
thrust::host_vector<int32_t> h_got = to_host(got->view());
EXPECT_EQ(h_exp[h_exp.size() - 1], h_got[h_got.size() - 1]);
cudf::test::fixed_width_column_wrapper<int32_t> expected_for_bool{{0, 3, 5, 1, 4, 2}};
run_stable_sort_test(input, expected_for_bool, column_order, null_precedence);
}
}
TYPED_TEST(StableSort, WithNullMin)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}, {1, 1, 0, 1, 1}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"}, {1, 1, 0, 1, 1});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 10, 2}, {1, 1, 0, 1, 1}};
cudf::table_view input{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 1, 0, 3, 4}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::ASCENDING, cudf::order::DESCENDING};
auto got = cudf::stable_sorted_order(input, column_order);
if (!std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
run_stable_sort_test(input, expected, column_order);
} else {
// for bools only validate that the null element landed at the front, since
// the rest of the values are equivalent and yields random sorted order.
auto to_host = [](cudf::column_view const& col) {
thrust::host_vector<int32_t> h_data(col.size());
CUDF_CUDA_TRY(cudaMemcpy(
h_data.data(), col.data<int32_t>(), h_data.size() * sizeof(int32_t), cudaMemcpyDefault));
return h_data;
};
thrust::host_vector<int32_t> h_exp = to_host(expected);
thrust::host_vector<int32_t> h_got = to_host(got->view());
EXPECT_EQ(h_exp.front(), h_got.front());
cudf::test::fixed_width_column_wrapper<int32_t> expected_for_bool{{2, 0, 3, 1, 4}};
run_stable_sort_test(input, expected_for_bool, column_order);
}
}
TYPED_TEST(StableSort, WithAllValid)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 10, 2}};
cudf::table_view input{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{{2, 1, 0, 3, 4}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::ASCENDING, cudf::order::DESCENDING};
auto got = cudf::stable_sorted_order(input, column_order);
// Skip validating bools order. Valid true bools are all
// equivalent, and yield random order after thrust::sort
if (!std::is_same_v<T, bool>) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
run_stable_sort_test(input, expected, column_order);
} else {
cudf::test::fixed_width_column_wrapper<int32_t> expected_for_bool{{2, 0, 3, 1, 4}};
run_stable_sort_test(input, expected_for_bool, column_order);
}
}
TYPED_TEST(StableSort, MisMatchInColumnOrderSize)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}};
cudf::table_view input{{col1, col2, col3}};
std::vector<cudf::order> column_order{cudf::order::ASCENDING, cudf::order::DESCENDING};
EXPECT_THROW(cudf::stable_sorted_order(input, column_order), cudf::logic_error);
EXPECT_THROW(cudf::stable_sort_by_key(input, input, column_order), cudf::logic_error);
}
TYPED_TEST(StableSort, MisMatchInNullPrecedenceSize)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}};
cudf::table_view input{{col1, col2, col3}};
std::vector<cudf::order> column_order{
cudf::order::ASCENDING, cudf::order::DESCENDING, cudf::order::DESCENDING};
std::vector<cudf::null_order> null_precedence{cudf::null_order::AFTER, cudf::null_order::BEFORE};
EXPECT_THROW(cudf::stable_sorted_order(input, column_order, null_precedence), cudf::logic_error);
EXPECT_THROW(cudf::stable_sort_by_key(input, input, column_order, null_precedence),
cudf::logic_error);
}
TYPED_TEST(StableSort, ZeroSizedColumns)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col1{};
cudf::table_view input{{col1}};
cudf::test::fixed_width_column_wrapper<int32_t> expected{};
std::vector<cudf::order> column_order{cudf::order::ASCENDING};
auto got = cudf::stable_sorted_order(input, column_order);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
run_stable_sort_test(input, expected, column_order);
}
struct StableSortByKey : public cudf::test::BaseFixture {};
TEST_F(StableSortByKey, ValueKeysSizeMismatch)
{
using T = int64_t;
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8}};
cudf::test::strings_column_wrapper col2({"d", "e", "a", "d", "k"});
cudf::test::fixed_width_column_wrapper<T> col3{{10, 40, 70, 5, 2}};
cudf::table_view values{{col1, col2, col3}};
cudf::test::fixed_width_column_wrapper<T> key_col{{5, 4, 3, 5}};
cudf::table_view keys{{key_col}};
EXPECT_THROW(cudf::stable_sort_by_key(values, keys), cudf::logic_error);
}
template <typename T>
struct StableSortFixedPoint : public cudf::test::BaseFixture {};
template <typename T>
using wrapper = cudf::test::fixed_width_column_wrapper<T>;
TYPED_TEST_SUITE(StableSortFixedPoint, cudf::test::FixedPointTypes);
TYPED_TEST(StableSortFixedPoint, FixedPointSortedOrderGather)
{
using namespace numeric;
using decimalXX = TypeParam;
auto const ZERO = decimalXX{0, scale_type{0}};
auto const ONE = decimalXX{1, scale_type{0}};
auto const TWO = decimalXX{2, scale_type{0}};
auto const THREE = decimalXX{3, scale_type{0}};
auto const FOUR = decimalXX{4, scale_type{0}};
auto const input_vec = std::vector<decimalXX>{THREE, TWO, ONE, ZERO, FOUR, THREE};
auto const index_vec = std::vector<cudf::size_type>{3, 2, 1, 0, 5, 4};
auto const sorted_vec = std::vector<decimalXX>{ZERO, ONE, TWO, THREE, THREE, FOUR};
auto const input_col = wrapper<decimalXX>(input_vec.begin(), input_vec.end());
auto const index_col = wrapper<cudf::size_type>(index_vec.begin(), index_vec.end());
auto const sorted_col = wrapper<decimalXX>(sorted_vec.begin(), sorted_vec.end());
auto const sorted_table = cudf::table_view{{sorted_col}};
auto const input_table = cudf::table_view{{input_col}};
auto const indices = cudf::sorted_order(input_table);
auto const sorted = cudf::gather(input_table, indices->view());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(index_col, indices->view());
CUDF_TEST_EXPECT_TABLES_EQUAL(sorted_table, sorted->view());
}
using StableSortDouble = StableSort<double>;
TEST_F(StableSortDouble, InfinityAndNaN)
{
auto constexpr NaN = std::numeric_limits<double>::quiet_NaN();
auto constexpr Inf = std::numeric_limits<double>::infinity();
auto input = cudf::test::fixed_width_column_wrapper<double>(
{-0.0, -NaN, -NaN, NaN, Inf, -Inf, 7.0, 5.0, 6.0, NaN, Inf, -Inf, -NaN, -NaN, -0.0});
auto expected = // -inf,-inf,-0,-0,5,6,7,inf,inf,-nan,-nan,nan,nan,-nan,-nan
cudf::test::fixed_width_column_wrapper<cudf::size_type>(
{5, 11, 0, 14, 7, 8, 6, 4, 10, 1, 2, 3, 9, 12, 13});
auto results = stable_sorted_order(cudf::table_view({input}));
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(results->view(), expected);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/sort/sort_nested_types_tests.cpp
|
/*
* Copyright (c) 2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/copying.hpp>
#include <cudf/sorting.hpp>
using int32s_lists = cudf::test::lists_column_wrapper<int32_t>;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using strings_col = cudf::test::strings_column_wrapper;
using structs_col = cudf::test::structs_column_wrapper;
using namespace cudf::test::iterators;
constexpr auto null{0};
struct NestedStructTest : public cudf::test::BaseFixture {};
TEST_F(NestedStructTest, SimpleStructsOfListsNoNulls)
{
auto const input = [] {
auto child = int32s_lists{{4, 2, 0}, {2}, {0, 5}, {1, 5}, {4, 1}};
return structs_col{{child}};
}();
{
auto const expected_order = int32s_col{2, 3, 1, 4, 0};
auto const order = cudf::sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{0, 4, 1, 3, 2};
auto const order = cudf::sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedStructTest, SimpleStructsOfListsWithNulls)
{
auto const input = [] {
auto child =
int32s_lists{{{4, 2, null}, null_at(2)}, {2}, {{null, 5}, null_at(0)}, {0, 5}, {4, 1}};
return structs_col{{child}};
}();
{
auto const expected_order = int32s_col{2, 3, 1, 4, 0};
auto const order = cudf::sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{0, 4, 1, 3, 2};
auto const order = cudf::sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedStructTest, StructsHaveListsNoNulls)
{
// Input has equal elements, thus needs to be tested by stable sort.
auto const input = [] {
auto child0 = int32s_lists{{4, 2, 0}, {}, {5}, {4, 1}, {4, 0}, {}, {}};
auto child1 = int32s_col{1, 2, 5, 0, 3, 3, 4};
return structs_col{{child0, child1}};
}();
{
auto const expected_order = int32s_col{1, 5, 6, 4, 3, 0, 2};
auto const order = cudf::stable_sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{2, 0, 3, 4, 6, 5, 1};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedStructTest, StructsHaveListsWithNulls)
{
// Input has equal elements, thus needs to be tested by stable sort.
auto const input = [] {
auto child0 =
int32s_lists{{{4, 2, null}, null_at(2)}, {}, {} /*NULL*/, {5}, {4, 1}, {4, 0}, {}, {}};
auto child1 = int32s_col{{1, 2, null, 5, null, 3, 3, 4}, nulls_at({2, 4})};
return structs_col{{child0, child1}, null_at(2)};
}();
{
auto const expected_order = int32s_col{2, 1, 6, 7, 5, 4, 0, 3};
auto const order = cudf::stable_sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{3, 0, 4, 5, 7, 6, 1, 2};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedStructTest, SlicedStructsHaveListsNoNulls)
{
// Input has equal elements, thus needs to be tested by stable sort.
// The original input has 3 first elements repeated at the beginning and the end.
auto const input_original = [] {
auto child0 = int32s_lists{
{4, 2, 0}, {}, {5}, {4, 2, 0}, {}, {5}, {4, 1}, {4, 0}, {}, {}, {4, 2, 0}, {}, {5}};
auto child1 = int32s_col{1, 2, 5, 1, 2, 5, 0, 3, 3, 4, 1, 2, 5};
return structs_col{{child0, child1}};
}();
auto const input = cudf::slice(input_original, {3, 10})[0];
{
auto const expected_order = int32s_col{1, 5, 6, 4, 3, 0, 2};
auto const order = cudf::stable_sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{2, 0, 3, 4, 6, 5, 1};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedStructTest, SlicedStructsHaveListsWithNulls)
{
// Input has equal elements, thus needs to be tested by stable sort.
// The original input has 2 first elements repeated at the beginning and the end.
auto const input_original = [] {
auto child0 = int32s_lists{{{4, 2, null}, null_at(2)},
{},
{{4, 2, null}, null_at(2)},
{},
{} /*NULL*/,
{5},
{4, 1},
{4, 0},
{},
{},
{{4, 2, null}, null_at(2)},
{}};
auto child1 = int32s_col{{1, 2, 1, 2, null, 5, null, 3, 3, 4, 1, 2}, nulls_at({4, 6})};
return structs_col{{child0, child1}, null_at(4)};
}();
auto const input = cudf::slice(input_original, {2, 10})[0];
{
auto const expected_order = int32s_col{2, 1, 6, 7, 5, 4, 0, 3};
auto const order = cudf::stable_sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{3, 0, 4, 5, 7, 6, 1, 2};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedStructTest, StructsOfStructsHaveListsNoNulls)
{
// Input has equal elements, thus needs to be tested by stable sort.
auto const input = [] {
auto child0 = [] {
auto child0 = int32s_lists{{4, 2, 0}, {}, {5}, {4, 1}, {4, 0}, {}, {}};
auto child1 = int32s_col{1, 2, 5, 0, 3, 3, 4};
return structs_col{{child0, child1}};
}();
auto child1 = int32s_lists{{4, 2, 0}, {}, {5}, {4, 1}, {4, 0}, {}, {}};
auto child2 = int32s_col{1, 2, 5, 0, 3, 3, 4};
return structs_col{{child0, child1, child2}};
}();
{
auto const expected_order = int32s_col{1, 5, 6, 4, 3, 0, 2};
auto const order = cudf::stable_sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{2, 0, 3, 4, 6, 5, 1};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedStructTest, StructsOfStructsHaveListsWithNulls)
{
// Input has equal elements, thus needs to be tested by stable sort.
auto const input = [] {
auto child0 = [] {
auto child0 =
int32s_lists{{{4, 2, null}, null_at(2)}, {}, {} /*NULL*/, {5}, {4, 1}, {4, 0}, {}, {}};
auto child1 = int32s_col{{1, 2, null, 5, null, 3, 3, 4}, nulls_at({2, 4})};
return structs_col{{child0, child1}, null_at(2)};
}();
auto child1 =
int32s_lists{{{4, 2, null}, null_at(2)}, {}, {} /*NULL*/, {5}, {4, 1}, {4, 0}, {}, {}};
auto child2 = int32s_col{{1, 2, null, 5, null, 3, 3, 4}, nulls_at({2, 4})};
return structs_col{{child0, child1, child2}, null_at(2)};
}();
{
auto const expected_order = int32s_col{2, 1, 6, 7, 5, 4, 0, 3};
auto const order = cudf::stable_sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{3, 0, 4, 5, 7, 6, 1, 2};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedStructTest, SimpleStructsOfListsOfStructsNoNulls)
{
auto const input = [] {
auto const make_lists_of_structs = [] {
auto const get_structs = [] {
auto child0 = int32s_col{3, 2, 3, 3, 4, 2, 4, 4, 1, 0, 3, 0, 2, 5, 4};
auto child1 = int32s_col{0, 4, 3, 2, 1, 1, 5, 1, 5, 5, 4, 2, 4, 1, 3};
return structs_col{{child0, child1}};
};
return cudf::make_lists_column(
8, int32s_col{0, 3, 5, 6, 6, 8, 10, 12, 15}.release(), get_structs().release(), 0, {});
};
std::vector<std::unique_ptr<cudf::column>> children;
children.emplace_back(make_lists_of_structs());
children.emplace_back(make_lists_of_structs());
return cudf::make_structs_column(8, std::move(children), 0, {});
}();
{
auto const expected_order = int32s_col{3, 5, 2, 7, 0, 1, 6, 4};
auto const order = cudf::stable_sorted_order(cudf::table_view{{*input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{4, 6, 1, 0, 7, 2, 5, 3};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{*input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
struct NestedListTest : public cudf::test::BaseFixture {};
TEST_F(NestedListTest, SimpleListsOfStructsNoNulls)
{
auto const input = [] {
auto const get_structs = [] {
auto child0 = int32s_col{3, 2, 3, 3, 4, 2, 4, 4, 1, 0, 3, 0, 2, 5, 4};
auto child1 = int32s_col{0, 4, 3, 2, 1, 1, 5, 1, 5, 5, 4, 2, 4, 1, 3};
return structs_col{{child0, child1}};
};
return cudf::make_lists_column(
8, int32s_col{0, 3, 5, 6, 6, 8, 10, 12, 15}.release(), get_structs().release(), 0, {});
}();
{
auto const expected_order = int32s_col{3, 5, 2, 7, 0, 1, 6, 4};
auto const order = cudf::stable_sorted_order(cudf::table_view{{*input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{4, 6, 1, 0, 7, 2, 5, 3};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{*input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedListTest, SlicedListsOfStructsNoNulls)
{
auto const input_original = [] {
auto const get_structs = [] {
auto child0 = int32s_col{0, 0, 3, 2, 3, 3, 4, 2, 4, 4, 1, 0, 3, 0, 2, 5, 4, 0};
auto child1 = int32s_col{0, 0, 0, 4, 3, 2, 1, 1, 5, 1, 5, 5, 4, 2, 4, 1, 3, 0};
return structs_col{{child0, child1}};
};
return cudf::make_lists_column(11,
int32s_col{0, 1, 2, 5, 7, 8, 8, 10, 12, 14, 17, 18}.release(),
get_structs().release(),
0,
{});
}();
auto const input = cudf::slice(*input_original, {2, 10})[0];
{
auto const expected_order = int32s_col{3, 5, 2, 7, 0, 1, 6, 4};
auto const order = cudf::stable_sorted_order(cudf::table_view{{input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{4, 6, 1, 0, 7, 2, 5, 3};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedListTest, ListsOfEqualStructsNoNulls)
{
auto const input = [] {
auto const get_structs = [] {
auto child0 = int32s_col{0, 3, 0, 1};
auto child1 = strings_col{"a", "c", "a", "b"};
return structs_col{{child0, child1}};
};
return cudf::make_lists_column(
2, int32s_col{0, 2, 4}.release(), get_structs().release(), 0, {});
}();
{
auto const expected_order = int32s_col{1, 0};
auto const order = cudf::sorted_order(cudf::table_view{{*input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{0, 1};
auto const order = cudf::sorted_order(cudf::table_view{{*input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedListTest, SimpleListsOfStructsWithNulls)
{
// [ {null, 2}, {null, null}, {1, 2} ] | 0
// [] | 1
// [ {null, null}, {4, 2} ] | 2
// [] | 3
// [ {3, 5}, {null, 4} ] | 4
// [] | 5
// [ {5, 3}, {5, 0}, {1, 1} ] | 6
// [ {null, 3}, {5, 2}, {4, 2} ] | 7
auto const input = [] {
auto const get_structs = [] {
auto child0 = int32s_col{{null, null, 1, null, 4, 3, null, 5, 5, 1, null, 5, 4},
nulls_at({0, 1, 3, 6, 10})};
auto child1 = int32s_col{{2, null, 2, null, 2, 5, 4, 3, 0, 1, 3, 2, 2}, nulls_at({1, 3})};
return structs_col{{child0, child1}, nulls_at({1, 3})};
};
return cudf::make_lists_column(
8, int32s_col{0, 3, 3, 5, 5, 7, 7, 10, 13}.release(), get_structs().release(), 0, {});
}();
{
auto const expected_order = int32s_col{1, 3, 5, 2, 0, 7, 4, 6};
auto const order = cudf::stable_sorted_order(
cudf::table_view{{*input}}, {cudf::order::ASCENDING}, {cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{6, 4, 7, 0, 2, 1, 3, 5};
auto const order = cudf::stable_sorted_order(
cudf::table_view{{*input}}, {cudf::order::DESCENDING}, {cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{1, 3, 5, 2, 4, 6, 0, 7};
auto const order = cudf::stable_sorted_order(
cudf::table_view{{*input}}, {cudf::order::ASCENDING}, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{7, 0, 6, 4, 2, 1, 3, 5};
auto const order = cudf::stable_sorted_order(
cudf::table_view{{*input}}, {cudf::order::DESCENDING}, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedListTest, ListsOfListsOfStructsNoNulls)
{
auto const input = [] {
auto const get_structs = [] {
auto child0 = int32s_col{0, 7, 4, 9, 2, 9, 4, 1, 5, 5, 3, 7, 0, 6, 3, 1, 9};
auto child1 = int32s_col{4, 6, 7, 3, 1, 2, 1, 10, 7, 9, 8, 7, 1, 10, 5, 3, 3};
return structs_col{{child0, child1}};
};
auto lists_of_structs =
cudf::make_lists_column(13,
int32s_col{0, 1, 3, 4, 5, 7, 9, 10, 12, 12, 14, 15, 17, 17}.release(),
get_structs().release(),
0,
{});
return cudf::make_lists_column(
8, int32s_col{0, 3, 4, 6, 6, 8, 10, 10, 13}.release(), std::move(lists_of_structs), 0, {});
}();
{
auto const expected_order = int32s_col{3, 6, 5, 0, 1, 7, 4, 2};
auto const order = cudf::stable_sorted_order(cudf::table_view{{*input}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
{
auto const expected_order = int32s_col{2, 4, 7, 1, 0, 5, 3, 6};
auto const order =
cudf::stable_sorted_order(cudf::table_view{{*input}}, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
}
TEST_F(NestedListTest, MultipleListsColumnsWithNulls)
{
// A STRUCT<LIST<INT>> column with all nulls.
auto const col0 = [] {
auto child = int32s_lists{{int32s_lists{}, int32s_lists{}}, all_nulls()};
return structs_col{{child}, all_nulls()};
}();
auto const col1 = [] {
auto child = int32s_lists{{0, 1, 2}, {10, 11, 12}};
return structs_col{{child}};
}();
auto const col2 = int32s_col{1, 0};
auto const expected_order = int32s_col{0, 1};
auto const order = cudf::sorted_order(cudf::table_view{{col0, col1, col2}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_order, order->view());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/sort/segmented_sort_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/copying.hpp>
#include <cudf/sorting.hpp>
#include <type_traits>
#include <vector>
template <typename T>
using column_wrapper = cudf::test::fixed_width_column_wrapper<T, int>;
template <typename T>
struct SegmentedSort : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(SegmentedSort, cudf::test::NumericTypes);
using SegmentedSortInt = SegmentedSort<int>;
TEST_F(SegmentedSortInt, Empty)
{
using T = int;
column_wrapper<T> col_empty{};
// clang-format off
column_wrapper<T> col1{{8, 9, 2, 3, 2, 2, 4, 1, 7, 5, 6}};
column_wrapper<int> segments{{0, 2, 5, 8, 11}};
// clang-format on
cudf::table_view table_empty{{col_empty}};
cudf::table_view table_valid{{col1}};
CUDF_EXPECT_NO_THROW(cudf::segmented_sort_by_key(table_valid, table_valid, segments));
CUDF_EXPECT_NO_THROW(cudf::segmented_sort_by_key(table_valid, table_valid, col_empty));
CUDF_EXPECT_NO_THROW(cudf::segmented_sort_by_key(table_empty, table_empty, segments));
CUDF_EXPECT_NO_THROW(cudf::segmented_sort_by_key(table_empty, table_empty, col_empty));
// Swapping "empty" and "valid" tables is invalid because the keys and values will be of different
// sizes.
EXPECT_THROW(cudf::segmented_sort_by_key(table_empty, table_valid, segments), cudf::logic_error);
EXPECT_THROW(cudf::segmented_sort_by_key(table_empty, table_valid, col_empty), cudf::logic_error);
EXPECT_THROW(cudf::segmented_sort_by_key(table_valid, table_empty, segments), cudf::logic_error);
EXPECT_THROW(cudf::segmented_sort_by_key(table_valid, table_empty, col_empty), cudf::logic_error);
}
TEST_F(SegmentedSortInt, Single)
{
using T = int;
column_wrapper<T> col1{{1}};
column_wrapper<T> col3{{8, 9, 2}};
column_wrapper<int> segments1{{0}};
column_wrapper<int> segments2{{0, 3}};
cudf::table_view table_1elem{{col1}};
cudf::table_view table_1segm{{col3}};
CUDF_EXPECT_NO_THROW(cudf::segmented_sort_by_key(table_1elem, table_1elem, segments1));
CUDF_EXPECT_NO_THROW(cudf::segmented_sort_by_key(table_1segm, table_1segm, segments2));
CUDF_EXPECT_NO_THROW(cudf::segmented_sort_by_key(table_1segm, table_1segm, segments1));
}
TYPED_TEST(SegmentedSort, NoNull)
{
using T = TypeParam;
// segments {0 1 2} {3 4} {5} {6 7 8 9 10}{11 12}{13}{14 15}
column_wrapper<T> col1{{10, 36, 14, 32, 49, 23, 10, 34, 12, 45, 12, 37, 43, 26, 21, 16}};
column_wrapper<T> col2{{10, 63, 41, 23, 94, 32, 10, 43, 21, 54, 22, 73, 34, 62, 12, 61}};
// segment sorted order {0 2 1} {3 4} {5} {6 8 10 7 9}{11 12}{13}{15 16}
column_wrapper<int> segments{0, 3, 5, 5, 5, 6, 11, 13, 14, 16};
cudf::table_view input1{{col1}};
cudf::table_view input2{{col1, col2}};
// Ascending
column_wrapper<T> col1_asc{{10, 14, 36, 32, 49, 23, 10, 12, 12, 34, 45, 37, 43, 26, 16, 21}};
auto results = cudf::segmented_sort_by_key(input1, input1, segments, {cudf::order::ASCENDING});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col1_asc}});
column_wrapper<T> col1_des{{36, 14, 10, 49, 32, 23, 45, 34, 12, 12, 10, 43, 37, 26, 21, 16}};
results = cudf::segmented_sort_by_key(input1, input1, segments, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col1_des}});
column_wrapper<T> col1_12_asc{{10, 14, 36, 32, 49, 23, 10, 12, 12, 34, 45, 37, 43, 26, 16, 21}};
column_wrapper<T> col2_12_asc{{10, 41, 63, 23, 94, 32, 10, 21, 22, 43, 54, 73, 34, 62, 61, 12}};
column_wrapper<T> col2_12_des{{10, 41, 63, 23, 94, 32, 10, 22, 21, 43, 54, 73, 34, 62, 61, 12}};
cudf::table_view expected12_aa{{col1_12_asc, col2_12_asc}};
results = cudf::segmented_sort_by_key(input2, input2, segments, {});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), expected12_aa);
cudf::table_view expected12_ad{{col1_12_asc, col2_12_des}};
results = cudf::segmented_sort_by_key(
input2, input2, segments, {cudf::order::ASCENDING, cudf::order::DESCENDING});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), expected12_ad);
}
TYPED_TEST(SegmentedSort, Null)
{
using T = TypeParam;
if (std::is_same_v<T, bool>) return;
// segments {0 1 2}{3 4} {5}{6 7 8 9 10}{11 12}{13}{14 15}
column_wrapper<T> col1{{1, 3, 2, 4, 5, 23, 6, 8, 7, 9, 7, 37, 43, 26, 21, 16},
{1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1}};
column_wrapper<T> col2{{0, 0, 0, 1, 1, 4, 5, 5, 21, 5, 22, 6, 6, 7, 8, 8},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1}};
column_wrapper<int> segments{0, 3, 5, 5, 5, 6, 11, 13, 14, 16};
cudf::table_view input1{{col1}};
cudf::table_view input2{{col1, col2}};
// Ascending
column_wrapper<T> col1_aa{{1, 3, 2, 4, 5, 23, 6, 7, 7, 8, 9, 37, 43, 26, 16, 21},
{1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1}};
column_wrapper<T> col1_ab{{2, 1, 3, 4, 5, 23, 9, 6, 7, 7, 8, 37, 43, 26, 16, 21},
{0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
auto results =
cudf::segmented_sort_by_key(input1, input1, segments, {}, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col1_aa}});
results = cudf::segmented_sort_by_key(input1, input1, segments, {}, {cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col1_ab}});
// Descending
column_wrapper<T> col1_da{{2, 3, 1, 5, 4, 23, 9, 8, 7, 7, 6, 43, 37, 26, 21, 16},
{0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
column_wrapper<T> col1_db{{3, 1, 2, 5, 4, 23, 8, 7, 7, 6, 9, 43, 37, 26, 21, 16},
{1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1}};
results = cudf::segmented_sort_by_key(
input1, input1, segments, {cudf::order::DESCENDING}, {cudf::null_order::AFTER});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col1_da}});
results = cudf::segmented_sort_by_key(
input1, input1, segments, {cudf::order::DESCENDING}, {cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col1_db}});
// second row null order.
column_wrapper<T> col2_12_aa{{0, 0, 0, 1, 1, 4, 5, 22, 21, 5, 5, 6, 6, 7, 8, 8},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1}};
column_wrapper<T> col2_12_ab{{0, 0, 0, 1, 1, 4, 5, 5, 21, 22, 5, 6, 6, 7, 8, 8},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1}};
cudf::table_view expected12_aa{{col1_aa, col2_12_aa}};
cudf::table_view expected12_ab{{col1_ab, col2_12_ab}};
results = cudf::segmented_sort_by_key(
input2, input2, segments, {}, {cudf::null_order::AFTER, cudf::null_order::AFTER});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), expected12_aa);
results = cudf::segmented_sort_by_key(
input2, input2, segments, {}, {cudf::null_order::BEFORE, cudf::null_order::BEFORE});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), expected12_ab);
}
TYPED_TEST(SegmentedSort, StableNoNulls)
{
using T = TypeParam;
// segments {0 1 2} {3 4} {5} {6 7 8 9 10}{11 12}{13}{14 15}
column_wrapper<T> col1{{10, 36, 14, 32, 49, 23, 10, 34, 12, 45, 11, 37, 43, 26, 21, 16}};
column_wrapper<T> col2{{10, 63, 10, 23, 94, 32, 10, 43, 22, 43, 22, 34, 34, 62, 62, 61}};
// stable sorted order {0 2 1} {3 4} {5} {6 8 10 7 9}{11 12}{13}{16 15}
column_wrapper<int> segments{0, 3, 5, 5, 5, 6, 11, 13, 14, 16};
auto values = cudf::table_view{{col1}};
auto keys = cudf::table_view{{col2}};
// Ascending
column_wrapper<T> col_asc{{10, 14, 36, 32, 49, 23, 10, 12, 11, 34, 45, 37, 43, 26, 16, 21}};
auto results =
cudf::stable_segmented_sort_by_key(values, keys, segments, {cudf::order::ASCENDING});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col_asc}});
// Descending
column_wrapper<T> col_des{{36, 10, 14, 49, 32, 23, 34, 45, 12, 11, 10, 37, 43, 26, 21, 16}};
results = cudf::stable_segmented_sort_by_key(values, keys, segments, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col_des}});
}
TYPED_TEST(SegmentedSort, StableWithNulls)
{
using T = TypeParam;
// segments {0 1 2} {3 4} {5} {6 7 8 9 10}{11 12}{13}{14 15}
column_wrapper<T> col1{{10, 36, 0, 32, 49, 23, 10, 0, 12, 45, 11, 37, 43, 0, 21, 16},
{1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1}};
column_wrapper<T> col2{{10, 0, 10, 23, 94, 32, 0, 43, 0, 43, 0, 34, 34, 62, 62, 61},
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1}};
// stable sorted order {0 2 1} {3 4} {5} {6 8 10 7 9}{11 12}{13}{16 15}
column_wrapper<int> segments{0, 3, 5, 5, 5, 6, 11, 13, 14, 16};
auto values = cudf::table_view{{col1}};
auto keys = cudf::table_view{{col2}};
// Ascending
column_wrapper<T> col_asc{{36, 10, 0, 32, 49, 23, 10, 12, 11, 0, 45, 37, 43, 0, 16, 21},
{1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1}};
auto results =
cudf::stable_segmented_sort_by_key(values, keys, segments, {cudf::order::ASCENDING});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col_asc}});
// Descending
column_wrapper<T> col_des{{10, 0, 36, 49, 32, 23, 0, 45, 12, 11, 10, 37, 43, 0, 21, 16},
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1}};
results = cudf::stable_segmented_sort_by_key(values, keys, segments, {cudf::order::DESCENDING});
CUDF_TEST_EXPECT_TABLES_EQUAL(results->view(), cudf::table_view{{col_des}});
}
TEST_F(SegmentedSortInt, NonZeroSegmentsStart)
{
using T = int;
// clang-format off
column_wrapper<T> col1{{8, 9, 2, 3, 2, 2, 4, 1, 7, 5, 6}};
column_wrapper<int> segments1{{0, 2, 5, 8, 11}};
column_wrapper<int> segments2{{ 2, 5, 8, 11}};
column_wrapper<int> segments3{{ 6, 8, 11}};
column_wrapper<int> segments4{{ 6, 8}};
column_wrapper<int> segments5{{0, 3, 6}};
column_wrapper<int> expected1{{0, 1, 2, 4, 3, 7, 5, 6, 9, 10, 8}};
column_wrapper<int> expected2{{0, 1, 2, 4, 3, 7, 5, 6, 9, 10, 8}};
column_wrapper<int> expected3{{0, 1, 2, 3, 4, 5, 7, 6, 9, 10, 8}};
column_wrapper<int> expected4{{0, 1, 2, 3, 4, 5, 7, 6, 8, 9, 10}};
column_wrapper<int> expected5{{2, 0, 1, 4, 5, 3, 6, 7, 8, 9, 10}};
// clang-format on
cudf::table_view input{{col1}};
auto results = cudf::segmented_sorted_order(input, segments1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected1);
results = cudf::stable_segmented_sorted_order(input, segments1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected1);
results = cudf::segmented_sorted_order(input, segments2);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected2);
results = cudf::stable_segmented_sorted_order(input, segments2);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected2);
results = cudf::segmented_sorted_order(input, segments3);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected3);
results = cudf::stable_segmented_sorted_order(input, segments3);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected3);
results = cudf::segmented_sorted_order(input, segments4);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected4);
results = cudf::stable_segmented_sorted_order(input, segments4);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected4);
results = cudf::segmented_sorted_order(input, segments5);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected5);
results = cudf::stable_segmented_sorted_order(input, segments5);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected5);
}
TEST_F(SegmentedSortInt, Sliced)
{
using T = int;
// clang-format off
column_wrapper<T> col1{{8, 9, 2, 3, 2, 2, 4, 1, 7, 5, 6}};
// sliced 2, 2, 4, 1, 7, 5, 6
column_wrapper<int> segments1{{0, 2, 5}};
column_wrapper<int> segments2{{-4, 0, 2, 5}};
column_wrapper<int> segments3{{ 7}};
column_wrapper<int> expected1{{0, 1, 3, 2, 4, 5, 6}};
column_wrapper<int> expected2{{0, 1, 3, 2, 4, 5, 6}};
column_wrapper<int> expected3{{0, 1, 2, 3, 4, 5, 6}};
// clang-format on
auto slice = cudf::slice(col1, {4, 11})[0]; // 7 elements
cudf::table_view input{{slice}};
auto seg_slice = cudf::slice(segments2, {2, 4})[0]; // 2 elements
// sliced input
auto results = cudf::segmented_sorted_order(input, segments1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected1);
results = cudf::stable_segmented_sorted_order(input, segments1);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected1);
// sliced input and sliced segment
results = cudf::segmented_sorted_order(input, seg_slice);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected2);
results = cudf::stable_segmented_sorted_order(input, seg_slice);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected2);
// sliced input, segment end.
results = cudf::segmented_sorted_order(input, segments3);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected3);
results = cudf::stable_segmented_sorted_order(input, segments3);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(results->view(), expected3);
}
TEST_F(SegmentedSortInt, ErrorsMismatchArgSizes)
{
using T = int;
column_wrapper<T> col1{{5, 6, 7, 8, 9}};
column_wrapper<T> segments{{1, 2, 3, 4}};
cudf::table_view input1{{col1}};
std::vector<cudf::order> order{cudf::order::ASCENDING, cudf::order::ASCENDING};
std::vector<cudf::null_order> null_order{cudf::null_order::AFTER, cudf::null_order::AFTER};
// Mismatch order sizes
EXPECT_THROW(cudf::segmented_sort_by_key(input1, input1, segments, order, {}), cudf::logic_error);
EXPECT_THROW(cudf::stable_segmented_sorted_order(input1, segments, order, {}), cudf::logic_error);
// Mismatch null precedence sizes
EXPECT_THROW(cudf::segmented_sorted_order(input1, segments, {}, null_order), cudf::logic_error);
EXPECT_THROW(cudf::stable_segmented_sort_by_key(input1, input1, segments, {}, null_order),
cudf::logic_error);
// Both
EXPECT_THROW(cudf::segmented_sort_by_key(input1, input1, segments, order, null_order),
cudf::logic_error);
EXPECT_THROW(cudf::stable_segmented_sort_by_key(input1, input1, segments, order, null_order),
cudf::logic_error);
// segmented_offsets beyond num_rows - undefined behavior, no throw.
CUDF_EXPECT_NO_THROW(cudf::segmented_sort_by_key(input1, input1, segments));
CUDF_EXPECT_NO_THROW(cudf::stable_segmented_sort_by_key(input1, input1, segments));
}
// Test specifically verifies the patch added in https://github.com/rapidsai/cudf/pull/12234
// This test will fail if the CUB bug fix is not available or the patch has not been applied.
TEST_F(SegmentedSortInt, Bool)
{
cudf::test::fixed_width_column_wrapper<bool> col1{
{true, false, false, true, true, true, true, true, true, true, true, true, true, false,
false, false, false, true, false, false, true, true, true, true, true, true, true, false,
true, false, true, true, true, true, true, true, false, true, false, false}};
cudf::test::fixed_width_column_wrapper<int> segments{{0, 5, 10, 15, 20, 25, 30, 40}};
cudf::test::fixed_width_column_wrapper<int> expected(
{1, 2, 0, 3, 4, 5, 6, 7, 8, 9, 13, 14, 10, 11, 12, 15, 16, 18, 19, 17,
20, 21, 22, 23, 24, 27, 29, 25, 26, 28, 36, 38, 39, 30, 31, 32, 33, 34, 35, 37});
auto test_col = cudf::column_view{col1};
auto result = cudf::segmented_sorted_order(cudf::table_view({test_col}), segments);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(result->view(), expected);
result = cudf::stable_segmented_sorted_order(cudf::table_view({test_col}), segments);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(result->view(), expected);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/sort/rank_test.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/copying.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <thrust/iterator/counting_iterator.h>
#include <tuple>
#include <vector>
template <typename T>
using lists_col = cudf::test::lists_column_wrapper<T, int32_t>;
using structs_col = cudf::test::structs_column_wrapper;
using cudf::test::iterators::null_at;
using cudf::test::iterators::nulls_at;
namespace {
void run_rank_test(cudf::table_view input,
cudf::table_view expected,
cudf::rank_method method,
cudf::order column_order,
cudf::null_policy null_handling,
cudf::null_order null_precedence,
bool percentage)
{
int i = 0;
for (auto&& input_column : input) {
// Rank
auto got_rank_column =
cudf::rank(input_column, method, column_order, null_handling, null_precedence, percentage);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected.column(i), got_rank_column->view());
i++;
}
}
using input_arg_t = std::tuple<cudf::order, cudf::null_policy, cudf::null_order>;
input_arg_t asc_keep{cudf::order::ASCENDING, cudf::null_policy::EXCLUDE, cudf::null_order::AFTER};
input_arg_t asc_top{cudf::order::ASCENDING, cudf::null_policy::INCLUDE, cudf::null_order::BEFORE};
input_arg_t asc_bottom{cudf::order::ASCENDING, cudf::null_policy::INCLUDE, cudf::null_order::AFTER};
input_arg_t desc_keep{
cudf::order::DESCENDING, cudf::null_policy::EXCLUDE, cudf::null_order::BEFORE};
input_arg_t desc_top{cudf::order::DESCENDING, cudf::null_policy::INCLUDE, cudf::null_order::AFTER};
input_arg_t desc_bottom{
cudf::order::DESCENDING, cudf::null_policy::INCLUDE, cudf::null_order::BEFORE};
using test_case_t = std::tuple<cudf::table_view, cudf::table_view>;
} // namespace
template <typename T>
struct Rank : public cudf::test::BaseFixture {
cudf::test::fixed_width_column_wrapper<T> col1{{5, 4, 3, 5, 8, 5}};
cudf::test::fixed_width_column_wrapper<T> col2{{5, 4, 3, 5, 8, 5}, {1, 1, 0, 1, 1, 1}};
cudf::test::strings_column_wrapper col3{{"d", "e", "a", "d", "k", "d"}, {1, 1, 1, 1, 1, 1}};
void run_all_tests(cudf::rank_method method,
input_arg_t input_arg,
cudf::column_view const col1_rank,
cudf::column_view const col2_rank,
cudf::column_view const col3_rank,
bool percentage = false)
{
if (std::is_same_v<T, bool>) return;
for (auto const& test_case : {
// Non-null column
test_case_t{cudf::table_view{{col1}}, cudf::table_view{{col1_rank}}},
// Null column
test_case_t{cudf::table_view{{col2}}, cudf::table_view{{col2_rank}}},
// Table
test_case_t{cudf::table_view{{col1, col2}}, cudf::table_view{{col1_rank, col2_rank}}},
// Table with String column
test_case_t{cudf::table_view{{col1, col2, col3}},
cudf::table_view{{col1_rank, col2_rank, col3_rank}}},
}) {
auto [input, output] = test_case;
run_rank_test(input,
output,
method,
std::get<0>(input_arg),
std::get<1>(input_arg),
std::get<2>(input_arg),
percentage);
}
}
};
TYPED_TEST_SUITE(Rank, cudf::test::NumericTypes);
// fixed_width_column_wrapper<T> col1{{ 5, 4, 3, 5, 8, 5}};
// 3, 2, 1, 4, 6, 5
TYPED_TEST(Rank, first_asc_keep)
{
// ASCENDING
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 4, 6, 5}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 1, -1, 3, 5, 4},
{1, 1, 0, 1, 1, 1}}; // KEEP
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 5, 1, 3, 6, 4},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::FIRST, asc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, first_asc_top)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 4, 6, 5}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{
{3, 2, 1, 4, 6, 5}}; // BEFORE = TOP
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 5, 1, 3, 6, 4}};
this->run_all_tests(cudf::rank_method::FIRST, asc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, first_asc_bottom)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 4, 6, 5}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{
{2, 1, 6, 3, 5, 4}}; // AFTER = BOTTOM
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 5, 1, 3, 6, 4}};
this->run_all_tests(cudf::rank_method::FIRST, asc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, first_desc_keep)
{
// DESCENDING
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 5, 6, 3, 1, 4}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 5, -1, 3, 1, 4},
{1, 1, 0, 1, 1, 1}}; // KEEP
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 6, 4, 1, 5},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::FIRST, desc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, first_desc_top)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 5, 6, 3, 1, 4}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{
{3, 6, 1, 4, 2, 5}}; // AFTER = TOP
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 6, 4, 1, 5}};
this->run_all_tests(cudf::rank_method::FIRST, desc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, first_desc_bottom)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 5, 6, 3, 1, 4}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{
{2, 5, 6, 3, 1, 4}}; // BEFORE = BOTTOM
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 6, 4, 1, 5}};
this->run_all_tests(cudf::rank_method::FIRST, desc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, dense_asc_keep)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 3, 4, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 1, -1, 2, 3, 2},
{1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 3, 1, 2, 4, 2},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::DENSE, asc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, dense_asc_top)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 3, 4, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{3, 2, 1, 3, 4, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 3, 1, 2, 4, 2}};
this->run_all_tests(cudf::rank_method::DENSE, asc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, dense_asc_bottom)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 3, 4, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 1, 4, 2, 3, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 3, 1, 2, 4, 2}};
this->run_all_tests(cudf::rank_method::DENSE, asc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, dense_desc_keep)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 3, 4, 2, 1, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 3, -1, 2, 1, 2},
{1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 4, 3, 1, 3},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::DENSE, desc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, dense_desc_top)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 3, 4, 2, 1, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{3, 4, 1, 3, 2, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 4, 3, 1, 3}};
this->run_all_tests(cudf::rank_method::DENSE, desc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, dense_desc_bottom)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 3, 4, 2, 1, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 3, 4, 2, 1, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 4, 3, 1, 3}};
this->run_all_tests(cudf::rank_method::DENSE, desc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, min_asc_keep)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 3, 6, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 1, -1, 2, 5, 2},
{1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 5, 1, 2, 6, 2},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::MIN, asc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, min_asc_top)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 3, 6, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{3, 2, 1, 3, 6, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 5, 1, 2, 6, 2}};
this->run_all_tests(cudf::rank_method::MIN, asc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, min_asc_bottom)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{3, 2, 1, 3, 6, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 1, 6, 2, 5, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{2, 5, 1, 2, 6, 2}};
this->run_all_tests(cudf::rank_method::MIN, asc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, min_desc_keep)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 5, 6, 2, 1, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 5, -1, 2, 1, 2},
{1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 6, 3, 1, 3},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::MIN, desc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, min_desc_top)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 5, 6, 2, 1, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{3, 6, 1, 3, 2, 3}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 6, 3, 1, 3}};
this->run_all_tests(cudf::rank_method::MIN, desc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, min_desc_bottom)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{2, 5, 6, 2, 1, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{2, 5, 6, 2, 1, 2}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{3, 2, 6, 3, 1, 3}};
this->run_all_tests(cudf::rank_method::MIN, desc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, max_asc_keep)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{5, 2, 1, 5, 6, 5}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{4, 1, -1, 4, 5, 4},
{1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{4, 5, 1, 4, 6, 4},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::MAX, asc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, max_asc_top)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{5, 2, 1, 5, 6, 5}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{5, 2, 1, 5, 6, 5}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{4, 5, 1, 4, 6, 4}};
this->run_all_tests(cudf::rank_method::MAX, asc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, max_asc_bottom)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{5, 2, 1, 5, 6, 5}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{4, 1, 6, 4, 5, 4}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{4, 5, 1, 4, 6, 4}};
this->run_all_tests(cudf::rank_method::MAX, asc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, max_desc_keep)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{4, 5, 6, 4, 1, 4}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{4, 5, -1, 4, 1, 4},
{1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{5, 2, 6, 5, 1, 5},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::MAX, desc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, max_desc_top)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{4, 5, 6, 4, 1, 4}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{5, 6, 1, 5, 2, 5}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{5, 2, 6, 5, 1, 5}};
this->run_all_tests(cudf::rank_method::MAX, desc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, max_desc_bottom)
{
cudf::test::fixed_width_column_wrapper<cudf::size_type> col1_rank{{4, 5, 6, 4, 1, 4}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col2_rank{{4, 5, 6, 4, 1, 4}};
cudf::test::fixed_width_column_wrapper<cudf::size_type> col3_rank{{5, 2, 6, 5, 1, 5}};
this->run_all_tests(cudf::rank_method::MAX, desc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, average_asc_keep)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{4, 2, 1, 4, 6, 4}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{3, 1, -1, 3, 5, 3}, {1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{3, 5, 1, 3, 6, 3}, {1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::AVERAGE, asc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, average_asc_top)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{4, 2, 1, 4, 6, 4}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{4, 2, 1, 4, 6, 4}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{3, 5, 1, 3, 6, 3}};
this->run_all_tests(cudf::rank_method::AVERAGE, asc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, average_asc_bottom)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{4, 2, 1, 4, 6, 4}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{3, 1, 6, 3, 5, 3}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{3, 5, 1, 3, 6, 3}};
this->run_all_tests(cudf::rank_method::AVERAGE, asc_bottom, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, average_desc_keep)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{3, 5, 6, 3, 1, 3}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{3, 5, -1, 3, 1, 3}, {1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{4, 2, 6, 4, 1, 4}, {1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::AVERAGE, desc_keep, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, average_desc_top)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{3, 5, 6, 3, 1, 3}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{4, 6, 1, 4, 2, 4}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{4, 2, 6, 4, 1, 4}};
this->run_all_tests(cudf::rank_method::AVERAGE, desc_top, col1_rank, col2_rank, col3_rank);
}
TYPED_TEST(Rank, average_desc_bottom)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{3, 5, 6, 3, 1, 3}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{3, 5, 6, 3, 1, 3}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{4, 2, 6, 4, 1, 4}};
this->run_all_tests(cudf::rank_method::AVERAGE, desc_bottom, col1_rank, col2_rank, col3_rank);
}
// percentage==true (dense, not-dense)
TYPED_TEST(Rank, dense_asc_keep_pct)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{0.75, 0.5, 0.25, 0.75, 1., 0.75}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{
{2.0 / 3.0, 1.0 / 3.0, -1., 2.0 / 3.0, 1., 2.0 / 3.0}, {1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{0.5, 0.75, 0.25, 0.5, 1., 0.5},
{1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::DENSE, asc_keep, col1_rank, col2_rank, col3_rank, true);
}
TYPED_TEST(Rank, dense_asc_top_pct)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{0.75, 0.5, 0.25, 0.75, 1., 0.75}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{0.75, 0.5, 0.25, 0.75, 1., 0.75}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{0.5, 0.75, 0.25, 0.5, 1., 0.5}};
this->run_all_tests(cudf::rank_method::DENSE, asc_top, col1_rank, col2_rank, col3_rank, true);
}
TYPED_TEST(Rank, dense_asc_bottom_pct)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{{0.75, 0.5, 0.25, 0.75, 1., 0.75}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{0.5, 0.25, 1., 0.5, 0.75, 0.5}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{{0.5, 0.75, 0.25, 0.5, 1., 0.5}};
this->run_all_tests(cudf::rank_method::DENSE, asc_bottom, col1_rank, col2_rank, col3_rank, true);
}
TYPED_TEST(Rank, min_desc_keep_pct)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{
{1.0 / 3.0, 5.0 / 6.0, 1., 1.0 / 3.0, 1.0 / 6.0, 1.0 / 3.0}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{{0.4, 1., -1., 0.4, 0.2, 0.4},
{1, 1, 0, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{
{0.5, 1.0 / 3.0, 1., 0.5, 1.0 / 6.0, 0.5}, {1, 1, 1, 1, 1, 1}};
this->run_all_tests(cudf::rank_method::MIN, desc_keep, col1_rank, col2_rank, col3_rank, true);
}
TYPED_TEST(Rank, min_desc_top_pct)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{
{1.0 / 3.0, 5.0 / 6.0, 1., 1.0 / 3.0, 1.0 / 6.0, 1.0 / 3.0}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{
{0.5, 1., 1.0 / 6.0, 0.5, 1.0 / 3.0, 0.5}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{
{0.5, 1.0 / 3.0, 1., 0.5, 1.0 / 6.0, 0.5}};
this->run_all_tests(cudf::rank_method::MIN, desc_top, col1_rank, col2_rank, col3_rank, true);
}
TYPED_TEST(Rank, min_desc_bottom_pct)
{
cudf::test::fixed_width_column_wrapper<double> col1_rank{
{1.0 / 3.0, 5.0 / 6.0, 1., 1.0 / 3.0, 1.0 / 6.0, 1.0 / 3.0}};
cudf::test::fixed_width_column_wrapper<double> col2_rank{
{1.0 / 3.0, 5.0 / 6.0, 1., 1.0 / 3.0, 1.0 / 6.0, 1.0 / 3.0}};
cudf::test::fixed_width_column_wrapper<double> col3_rank{
{0.5, 1.0 / 3.0, 1., 0.5, 1.0 / 6.0, 0.5}};
this->run_all_tests(cudf::rank_method::MIN, desc_bottom, col1_rank, col2_rank, col3_rank, true);
}
struct RankLarge : public cudf::test::BaseFixture {};
TEST_F(RankLarge, average_large)
{
// testcase of https://github.com/rapidsai/cudf/issues/9703
auto iter = thrust::counting_iterator<int64_t>(0);
cudf::test::fixed_width_column_wrapper<int64_t> col1(iter, iter + 10558);
auto result = cudf::rank(col1,
cudf::rank_method::AVERAGE,
{},
cudf::null_policy::EXCLUDE,
cudf::null_order::AFTER,
false);
cudf::test::fixed_width_column_wrapper<double, int> expected(iter + 1, iter + 10559);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(result->view(), expected);
}
template <typename T>
struct RankListAndStruct : public cudf::test::BaseFixture {
void run_all_tests(cudf::rank_method method,
input_arg_t input_arg,
cudf::column_view const list_rank,
cudf::column_view const struct_rank,
bool percentage = false)
{
if constexpr (std::is_same_v<T, bool>) { return; }
/*
[
[],
[1],
[2, 2],
[2, 3],
[2, 2],
[1],
[],
NULL
[2],
NULL,
[1]
]
*/
auto list_col =
lists_col<T>{{{}, {1}, {2, 2}, {2, 3}, {2, 2}, {1}, {}, {} /*NULL*/, {2}, {} /*NULL*/, {1}},
nulls_at({7, 9})};
// clang-format off
/*
+------------+
| s|
+------------+
0 | {0, null}|
1 | {1, null}|
2 | null|
3 |{null, null}|
4 | null|
5 |{null, null}|
6 | {null, 1}|
7 | {null, 0}|
+------------+
*/
std::vector<bool> struct_valids{1, 1, 0, 1, 0, 1, 1, 1};
auto col1 = cudf::test::fixed_width_column_wrapper<T>{{ 0, 1, 9, -1, 9, -1, -1, -1}, {1, 1, 1, 0, 1, 0, 0, 0}};
auto col2 = cudf::test::fixed_width_column_wrapper<T>{{-1, -1, 9, -1, 9, -1, 1, 0}, {0, 0, 1, 0, 1, 0, 1, 1}};
auto struct_col = cudf::test::structs_column_wrapper{{col1, col2}, struct_valids}.release();
// clang-format on
for (auto const& test_case : {
// Non-null column
test_case_t{cudf::table_view{{list_col}}, cudf::table_view{{list_rank}}},
// Null column
test_case_t{cudf::table_view{{struct_col->view()}}, cudf::table_view{{struct_rank}}},
}) {
auto [input, output] = test_case;
run_rank_test(input,
output,
method,
std::get<0>(input_arg),
std::get<1>(input_arg),
std::get<2>(input_arg),
percentage);
}
}
};
TYPED_TEST_SUITE(RankListAndStruct, cudf::test::NumericTypes);
TYPED_TEST(RankListAndStruct, first_asc_keep)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> list_rank{
{1, 3, 7, 9, 8, 4, 2, -1, 6, -1, 5}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{{1, 2, -1, 5, -1, 6, 4, 3},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::FIRST, asc_keep, list_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, first_asc_top)
{
// ASCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
3, 5, 9, 11, 10, 6, 4, 1, 8, 2, 7};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{7, 8, 1, 3, 2, 4, 6, 5};
this->run_all_tests(cudf::rank_method::FIRST, asc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, first_asc_bottom)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
1, 3, 7, 9, 8, 4, 2, 10, 6, 11, 5};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{1, 2, 7, 5, 8, 6, 4, 3};
this->run_all_tests(cudf::rank_method::FIRST, asc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, first_desc_keep)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
{8, 5, 2, 1, 3, 6, 9, -1, 4, -1, 7}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{{2, 1, -1, 5, -1, 6, 3, 4},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::FIRST, desc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, first_desc_top)
{
// DESCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
10, 7, 4, 3, 5, 8, 11, 1, 6, 2, 9};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{8, 7, 1, 3, 2, 4, 5, 6};
this->run_all_tests(cudf::rank_method::FIRST, desc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, first_desc_bottom)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
8, 5, 2, 1, 3, 6, 9, 10, 4, 11, 7};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{2, 1, 7, 5, 8, 6, 3, 4};
this->run_all_tests(cudf::rank_method::FIRST, desc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, dense_asc_keep)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
{1, 2, 4, 5, 4, 2, 1, -1, 3, -1, 2}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{{1, 2, -1, 5, -1, 5, 4, 3},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::DENSE, asc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, dense_asc_top)
{
// ASCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{2, 3, 5, 6, 5, 3, 2, 1, 4, 1, 3};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{5, 6, 1, 2, 1, 2, 4, 3};
this->run_all_tests(cudf::rank_method::DENSE, asc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, dense_asc_bottom)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{1, 2, 4, 5, 4, 2, 1, 6, 3, 6, 2};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{1, 2, 6, 5, 6, 5, 4, 3};
this->run_all_tests(cudf::rank_method::DENSE, asc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, dense_desc_keep)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
{5, 4, 2, 1, 2, 4, 5, -1, 3, -1, 4}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{{2, 1, -1, 5, -1, 5, 3, 4},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::DENSE, desc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, dense_desc_top)
{
// DESCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{6, 5, 3, 2, 3, 5, 6, 1, 4, 1, 5};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{6, 5, 1, 2, 1, 2, 3, 4};
this->run_all_tests(cudf::rank_method::DENSE, desc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, dense_desc_bottom)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{5, 4, 2, 1, 2, 4, 5, 6, 3, 6, 4};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{2, 1, 6, 5, 6, 5, 3, 4};
this->run_all_tests(cudf::rank_method::DENSE, desc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, min_asc_keep)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
{1, 3, 7, 9, 7, 3, 1, -1, 6, -1, 3}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{{1, 2, -1, 5, -1, 5, 4, 3},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::MIN, asc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, min_asc_top)
{
// ASCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
3, 5, 9, 11, 9, 5, 3, 1, 8, 1, 5};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{7, 8, 1, 3, 1, 3, 6, 5};
this->run_all_tests(cudf::rank_method::MIN, asc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, min_asc_bottom)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
1, 3, 7, 9, 7, 3, 1, 10, 6, 10, 3};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{1, 2, 7, 5, 7, 5, 4, 3};
this->run_all_tests(cudf::rank_method::MIN, asc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, min_desc_keep)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
{8, 5, 2, 1, 2, 5, 8, -1, 4, -1, 5}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{{2, 1, -1, 5, -1, 5, 3, 4},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::MIN, desc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, min_desc_top)
{
// DESCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
10, 7, 4, 3, 4, 7, 10, 1, 6, 1, 7};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{8, 7, 1, 3, 1, 3, 5, 6};
this->run_all_tests(cudf::rank_method::MIN, desc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, min_desc_bottom)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
8, 5, 2, 1, 2, 5, 8, 10, 4, 10, 5};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{2, 1, 7, 5, 7, 5, 3, 4};
this->run_all_tests(cudf::rank_method::MIN, desc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, max_asc_keep)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
{2, 5, 8, 9, 8, 5, 2, -1, 6, -1, 5}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{{1, 2, -1, 6, -1, 6, 4, 3},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::MAX, asc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, max_asc_top)
{
// ASCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
4, 7, 10, 11, 10, 7, 4, 2, 8, 2, 7};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{7, 8, 2, 4, 2, 4, 6, 5};
this->run_all_tests(cudf::rank_method::MAX, asc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, max_asc_bottom)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
2, 5, 8, 9, 8, 5, 2, 11, 6, 11, 5};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{1, 2, 8, 6, 8, 6, 4, 3};
this->run_all_tests(cudf::rank_method::MAX, asc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, max_desc_keep)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
{9, 7, 3, 1, 3, 7, 9, -1, 4, -1, 7}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{{2, 1, -1, 6, -1, 6, 3, 4},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::MAX, desc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, max_desc_top)
{
// DESCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
11, 9, 5, 3, 5, 9, 11, 2, 6, 2, 9};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{8, 7, 2, 4, 2, 4, 5, 6};
this->run_all_tests(cudf::rank_method::MAX, desc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, max_desc_bottom)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<cudf::size_type> col_rank{
9, 7, 3, 1, 3, 7, 9, 11, 4, 11, 7};
cudf::test::fixed_width_column_wrapper<cudf::size_type> struct_rank{2, 1, 8, 6, 8, 6, 3, 4};
this->run_all_tests(cudf::rank_method::MAX, desc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, average_asc_keep)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<double> col_rank{
{1.5, 4.0, 7.5, 9.0, 7.5, 4.0, 1.5, -1.0, 6.0, -1.0, 4.0}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
{1.0, 2.0, -1.0, 5.5, -1.0, 5.5, 4.0, 3.0}, nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::AVERAGE, asc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, average_asc_top)
{
// ASCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<double> col_rank{
3.5, 6.0, 9.5, 11.0, 9.5, 6.0, 3.5, 1.5, 8.0, 1.5, 6.0};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
7.0, 8.0, 1.5, 3.5, 1.5, 3.5, 6.0, 5.0};
this->run_all_tests(cudf::rank_method::AVERAGE, asc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, average_asc_bottom)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<double> col_rank{
1.5, 4.0, 7.5, 9.0, 7.5, 4.0, 1.5, 10.5, 6.0, 10.5, 4.0};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
1.0, 2.0, 7.5, 5.5, 7.5, 5.5, 4.0, 3.0};
this->run_all_tests(cudf::rank_method::AVERAGE, asc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, average_desc_keep)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<double> col_rank{
{8.5, 6.0, 2.5, 1.0, 2.5, 6.0, 8.5, -1.0, 4.0, -1.0, 6.0}, nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
{2.0, 1.0, -1.0, 5.5, -1.0, 5.5, 3.0, 4.0}, nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::AVERAGE, desc_keep, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, average_desc_top)
{
// DESCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<double> col_rank{
10.5, 8.0, 4.5, 3.0, 4.5, 8.0, 10.5, 1.5, 6.0, 1.5, 8.0};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
8.0, 7.0, 1.5, 3.5, 1.5, 3.5, 5.0, 6.0};
this->run_all_tests(cudf::rank_method::AVERAGE, desc_top, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, average_desc_bottom)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<double> col_rank{
8.5, 6.0, 2.5, 1.0, 2.5, 6.0, 8.5, 10.5, 4.0, 10.5, 6.0};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
2.0, 1.0, 7.5, 5.5, 7.5, 5.5, 3.0, 4.0};
this->run_all_tests(cudf::rank_method::AVERAGE, desc_bottom, col_rank, struct_rank);
}
TYPED_TEST(RankListAndStruct, dense_asc_keep_pct)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<double> col_rank{{1.0 / 5.0,
2.0 / 5.0,
4.0 / 5.0,
1.0,
4.0 / 5.0,
2.0 / 5.0,
1.0 / 5.0,
-1.0,
3.0 / 5.0,
-1.0,
2.0 / 5.0},
nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
{1.0 / 5.0, 2.0 / 5.0, -1.0, 1.0, -1.0, 1.0, 4.0 / 5.0, 3.0 / 5.0}, nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::DENSE, asc_keep, col_rank, struct_rank, true);
}
TYPED_TEST(RankListAndStruct, dense_asc_top_pct)
{
// ASCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<double> col_rank{1.0 / 3.0,
1.0 / 2.0,
5.0 / 6.0,
1.0,
5.0 / 6.0,
1.0 / 2.0,
1.0 / 3.0,
1.0 / 6.0,
2.0 / 3.0,
1.0 / 6.0,
1.0 / 2.0};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
5.0 / 6.0, 1.0, 1.0 / 6.0, 2.0 / 6.0, 1.0 / 6.0, 2.0 / 6.0, 4.0 / 6.0, 3.0 / 6.0};
this->run_all_tests(cudf::rank_method::DENSE, asc_top, col_rank, struct_rank, true);
}
TYPED_TEST(RankListAndStruct, dense_asc_bottom_pct)
{
// ASCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<double> col_rank{1.0 / 6.0,
1.0 / 3.0,
2.0 / 3.0,
5.0 / 6.0,
2.0 / 3.0,
1.0 / 3.0,
1.0 / 6.0,
1.0,
1.0 / 2.0,
1.0,
1.0 / 3.0};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
1.0 / 6.0, 2.0 / 6.0, 1.0, 5.0 / 6.0, 1.0, 5.0 / 6.0, 4.0 / 6.0, 3.0 / 6.0};
this->run_all_tests(cudf::rank_method::DENSE, asc_bottom, col_rank, struct_rank, true);
}
TYPED_TEST(RankListAndStruct, min_desc_keep_pct)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<double> col_rank{{8.0 / 9.0,
5.0 / 9.0,
2.0 / 9.0,
1.0 / 9.0,
2.0 / 9.0,
5.0 / 9.0,
8.0 / 9.0,
-1.0,
4.0 / 9.0,
-1.0,
5.0 / 9.0},
nulls_at({7, 9})};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
{2.0 / 6.0, 1.0 / 6.0, -1.0, 5.0 / 6.0, -1.0, 5.0 / 6.0, 3.0 / 6.0, 4.0 / 6.0},
nulls_at({2, 4})};
this->run_all_tests(cudf::rank_method::MIN, desc_keep, col_rank, struct_rank, true);
}
TYPED_TEST(RankListAndStruct, min_desc_top_pct)
{
// DESCENDING and null_order::AFTER
cudf::test::fixed_width_column_wrapper<double> col_rank{10.0 / 11.0,
7.0 / 11.0,
4.0 / 11.0,
3.0 / 11.0,
4.0 / 11.0,
7.0 / 11.0,
10.0 / 11.0,
1.0 / 11.0,
6.0 / 11.0,
1.0 / 11.0,
7.0 / 11.0};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
1.0, 7.0 / 8.0, 1.0 / 8.0, 3.0 / 8.0, 1.0 / 8.0, 3.0 / 8.0, 5.0 / 8.0, 6.0 / 8.0};
this->run_all_tests(cudf::rank_method::MIN, desc_top, col_rank, struct_rank, true);
}
TYPED_TEST(RankListAndStruct, min_desc_bottom_pct)
{
// DESCENDING and null_order::BEFORE
cudf::test::fixed_width_column_wrapper<double> col_rank{8.0 / 11.0,
5.0 / 11.0,
2.0 / 11.0,
1.0 / 11.0,
2.0 / 11.0,
5.0 / 11.0,
8.0 / 11.0,
10.0 / 11.0,
4.0 / 11.0,
10.0 / 11.0,
5.0 / 11.0};
cudf::test::fixed_width_column_wrapper<double> struct_rank{
2.0 / 8.0, 1.0 / 8.0, 7.0 / 8.0, 5.0 / 8.0, 7.0 / 8.0, 5.0 / 8.0, 3.0 / 8.0, 4.0 / 8.0};
this->run_all_tests(cudf::rank_method::MIN, desc_bottom, col_rank, struct_rank, true);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/sort/is_sorted_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <vector>
namespace testdata {
// ----- most numerics
template <typename T>
std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, bool>,
cudf::test::fixed_width_column_wrapper<T>>
ascending()
{
return std::is_signed_v<T>
? cudf::test::fixed_width_column_wrapper<T>({std::numeric_limits<T>::lowest(),
T(-100),
T(-10),
T(-1),
T(0),
T(1),
T(10),
T(100),
std::numeric_limits<T>::max()})
: cudf::test::fixed_width_column_wrapper<T>({std::numeric_limits<T>::lowest(),
T(0),
T(1),
T(10),
T(100),
std::numeric_limits<T>::max()});
}
template <typename T>
std::enable_if_t<std::is_arithmetic_v<T> && !std::is_same_v<T, bool>,
cudf::test::fixed_width_column_wrapper<T>>
descending()
{
return std::is_signed_v<T>
? cudf::test::fixed_width_column_wrapper<T>({std::numeric_limits<T>::max(),
T(100),
T(10),
T(1),
T(0),
T(-1),
T(-10),
T(-100),
std::numeric_limits<T>::lowest()})
: cudf::test::fixed_width_column_wrapper<T>({std::numeric_limits<T>::max(),
T(100),
T(10),
T(1),
T(0),
std::numeric_limits<T>::lowest()});
}
template <typename T>
auto empty()
{
return cudf::test::fixed_width_column_wrapper<T>();
}
template <typename T>
auto nulls_after()
{
return cudf::test::fixed_width_column_wrapper<T, int32_t>({0, 0}, {1, 0});
}
template <typename T>
auto nulls_before()
{
return cudf::test::fixed_width_column_wrapper<T, int32_t>({0, 0}, {0, 1});
}
// ----- bool
template <typename T>
std::enable_if_t<std::is_same_v<T, bool>, cudf::test::fixed_width_column_wrapper<bool>> ascending()
{
return cudf::test::fixed_width_column_wrapper<bool>({false, false, true, true});
}
template <typename T>
std::enable_if_t<std::is_same_v<T, bool>, cudf::test::fixed_width_column_wrapper<bool>> descending()
{
return cudf::test::fixed_width_column_wrapper<bool>({true, true, false, false});
}
// ----- chrono types
template <typename T>
std::enable_if_t<cudf::is_chrono<T>(), cudf::test::fixed_width_column_wrapper<T>> ascending()
{
return cudf::test::fixed_width_column_wrapper<T>({T::min(), T::max()});
}
template <typename T>
std::enable_if_t<cudf::is_chrono<T>(), cudf::test::fixed_width_column_wrapper<T>> descending()
{
return cudf::test::fixed_width_column_wrapper<T>({T::max(), T::min()});
}
// ----- string_view
template <typename T>
std::enable_if_t<std::is_same_v<T, cudf::string_view>, cudf::test::strings_column_wrapper>
ascending()
{
return cudf::test::strings_column_wrapper({"A", "B"});
}
template <typename T>
std::enable_if_t<std::is_same_v<T, cudf::string_view>, cudf::test::strings_column_wrapper>
descending()
{
return cudf::test::strings_column_wrapper({"B", "A"});
}
template <>
auto empty<cudf::string_view>()
{
return cudf::test::strings_column_wrapper();
}
template <>
auto nulls_after<cudf::string_view>()
{
return cudf::test::strings_column_wrapper({"identical", "identical"}, {1, 0});
}
template <>
auto nulls_before<cudf::string_view>()
{
return cudf::test::strings_column_wrapper({"identical", "identical"}, {0, 1});
}
// ----- struct_view {"nestedInt" : {"Int" : 0 }, "float" : 1}
template <typename T>
std::enable_if_t<std::is_same_v<T, cudf::struct_view>, cudf::test::structs_column_wrapper>
ascending()
{
using T1 = int32_t;
auto int_col = cudf::test::fixed_width_column_wrapper<int32_t>({std::numeric_limits<T1>::lowest(),
T1(-100),
T1(-10),
T1(-10),
T1(0),
T1(10),
T1(10),
T1(100),
std::numeric_limits<T1>::max()});
auto nestedInt_col = cudf::test::structs_column_wrapper{{int_col}};
auto float_col = ascending<float>();
return cudf::test::structs_column_wrapper{{nestedInt_col, float_col}};
}
template <typename T>
std::enable_if_t<std::is_same_v<T, cudf::struct_view>, cudf::test::structs_column_wrapper>
descending()
{
using T1 = int32_t;
auto int_col =
cudf::test::fixed_width_column_wrapper<int32_t>({std::numeric_limits<T1>::max(),
T1(100),
T1(10),
T1(10),
T1(0),
T1(-10),
T1(-10),
T1(-100),
std::numeric_limits<T1>::lowest()});
auto nestedInt_col = cudf::test::structs_column_wrapper{{int_col}};
auto float_col = descending<float>();
return cudf::test::structs_column_wrapper{{nestedInt_col, float_col}};
}
template <>
auto empty<cudf::struct_view>()
{
auto int_col = cudf::test::fixed_width_column_wrapper<int32_t>();
auto col1 = cudf::test::structs_column_wrapper{{int_col}};
auto col2 = cudf::test::fixed_width_column_wrapper<float>();
return cudf::test::structs_column_wrapper{{col1, col2}};
}
template <>
auto nulls_after<cudf::struct_view>()
{
auto int_col = cudf::test::fixed_width_column_wrapper<int32_t>({1, 1});
auto col1 = cudf::test::structs_column_wrapper{{int_col}};
auto col2 = cudf::test::fixed_width_column_wrapper<float>({1, 1});
return cudf::test::structs_column_wrapper{{col1, col2}, {1, 0}};
}
template <>
auto nulls_before<cudf::struct_view>()
{
auto int_col = cudf::test::fixed_width_column_wrapper<int32_t>({1, 1});
auto col1 = cudf::test::structs_column_wrapper{{int_col}};
auto col2 = cudf::test::fixed_width_column_wrapper<float>({1, 1});
return cudf::test::structs_column_wrapper{{col1, col2}, {0, 1}};
}
using lcw = cudf::test::lists_column_wrapper<int32_t>;
using cudf::test::iterators::null_at;
/*
List<List<List<int>
[
[[[0]], [[0]], [[0]]], 0
[[[0], [0], [0]]], 1
[[[0, 0]], [[0, 0, 0, 0, 0, 0, 0, 0]], [[0]]] 2
[[[0, 0, 0]]], 3
[[[0, 0, 0]], [[0]], [[0]]], 4
]
*/
template <typename T>
std::enable_if_t<std::is_same_v<T, cudf::list_view>, lcw> ascending()
{
return lcw{lcw{lcw{lcw{0}}, lcw{lcw{0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0}, lcw{0}, lcw{0}}},
lcw{lcw{lcw{0, 0}}, lcw{lcw{0, 0, 0, 0, 0, 0, 0, 0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0, 0, 0}}},
lcw{lcw{lcw{0, 0, 0}}, lcw{lcw{0}}, lcw{lcw{0}}}};
}
template <typename T>
std::enable_if_t<std::is_same_v<T, cudf::list_view>, lcw> descending()
{
return lcw{lcw{lcw{lcw{0, 0, 0}}, lcw{lcw{0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0, 0, 0}}},
lcw{lcw{lcw{0, 0}}, lcw{lcw{0, 0, 0, 0, 0, 0, 0, 0}}, lcw{lcw{0}}},
lcw{lcw{lcw{0}, lcw{0}, lcw{0}}},
lcw{lcw{lcw{0}}, lcw{lcw{0}}, lcw{lcw{0}}}};
}
template <>
auto empty<cudf::list_view>()
{
return lcw{};
}
template <>
auto nulls_after<cudf::list_view>()
{
return lcw{{{1}, {2, 2}, {0}}, null_at(2)};
}
template <>
auto nulls_before<cudf::list_view>()
{
return lcw{{{0}, {1}, {2, 2}}, null_at(0)};
}
} // namespace testdata
// =============================================================================
// ---- tests ------------------------------------------------------------------
template <typename T>
struct IsSortedTest : public cudf::test::BaseFixture {};
using SupportedTypes = cudf::test::
Concat<cudf::test::ComparableTypes, cudf::test::Types<cudf::struct_view>, cudf::test::ListTypes>;
TYPED_TEST_SUITE(IsSortedTest, SupportedTypes);
TYPED_TEST(IsSortedTest, NoColumns)
{
cudf::table_view in{std::vector<cudf::table_view>{}};
std::vector<cudf::order> order{};
std::vector<cudf::null_order> null_precedence{};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(true, actual);
}
TYPED_TEST(IsSortedTest, NoRows)
{
using T = TypeParam;
if (std::is_same_v<T, cudf::string_view>) {
// cudf::test::strings_column_wrapper does not yet support empty columns.
return;
} else {
auto col1 = testdata::empty<T>();
auto col2 = testdata::empty<T>();
cudf::table_view in{{col1, col2}};
std::vector<cudf::order> order{cudf::order::ASCENDING, cudf::order::DESCENDING};
std::vector<cudf::null_order> null_precedence{};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(true, actual);
}
}
TYPED_TEST(IsSortedTest, Ascending)
{
using T = TypeParam;
auto col1 = testdata::ascending<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{cudf::order::ASCENDING};
std::vector<cudf::null_order> null_precedence{};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(true, actual);
}
TYPED_TEST(IsSortedTest, AscendingFalse)
{
using T = TypeParam;
auto col1 = testdata::descending<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{cudf::order::ASCENDING};
std::vector<cudf::null_order> null_precedence{};
auto actual = cudf::is_sorted(in, order, {});
EXPECT_EQ(false, actual);
}
TYPED_TEST(IsSortedTest, Descending)
{
using T = TypeParam;
auto col1 = testdata::descending<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{cudf::order::DESCENDING};
std::vector<cudf::null_order> null_precedence{};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(true, actual);
}
TYPED_TEST(IsSortedTest, DescendingFalse)
{
using T = TypeParam;
auto col1 = testdata::ascending<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{cudf::order::DESCENDING};
std::vector<cudf::null_order> null_precedence{};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(false, actual);
}
TYPED_TEST(IsSortedTest, NullsAfter)
{
using T = TypeParam;
auto col1 = testdata::nulls_after<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{};
std::vector<cudf::null_order> null_precedence{cudf::null_order::AFTER};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(true, actual);
}
TYPED_TEST(IsSortedTest, NullsAfterFalse)
{
using T = TypeParam;
auto col1 = testdata::nulls_before<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{};
std::vector<cudf::null_order> null_precedence{cudf::null_order::AFTER};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(false, actual);
}
TYPED_TEST(IsSortedTest, NullsBefore)
{
using T = TypeParam;
auto col1 = testdata::nulls_before<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{};
std::vector<cudf::null_order> null_precedence{cudf::null_order::BEFORE};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(true, actual);
}
TYPED_TEST(IsSortedTest, NullsBeforeFalse)
{
using T = TypeParam;
auto col1 = testdata::nulls_after<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{};
std::vector<cudf::null_order> null_precedence{cudf::null_order::BEFORE};
auto actual = cudf::is_sorted(in, order, null_precedence);
EXPECT_EQ(false, actual);
}
TYPED_TEST(IsSortedTest, OrderArgsTooFew)
{
using T = TypeParam;
auto col1 = testdata::ascending<T>();
auto col2 = testdata::ascending<T>();
cudf::table_view in{{col1, col2}};
std::vector<cudf::order> order{cudf::order::ASCENDING};
std::vector<cudf::null_order> null_precedence{};
EXPECT_THROW(cudf::is_sorted(in, order, null_precedence), cudf::logic_error);
}
TYPED_TEST(IsSortedTest, OrderArgsTooMany)
{
using T = TypeParam;
auto col1 = testdata::ascending<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{cudf::order::ASCENDING, cudf::order::ASCENDING};
std::vector<cudf::null_order> null_precedence{};
EXPECT_THROW(cudf::is_sorted(in, order, null_precedence), cudf::logic_error);
}
TYPED_TEST(IsSortedTest, NullOrderArgsTooFew)
{
using T = TypeParam;
auto col1 = testdata::nulls_before<T>();
auto col2 = testdata::nulls_before<T>();
cudf::table_view in{{col1, col2}};
std::vector<cudf::order> order{};
std::vector<cudf::null_order> null_precedence{cudf::null_order::BEFORE};
EXPECT_THROW(cudf::is_sorted(in, order, null_precedence), cudf::logic_error);
}
TYPED_TEST(IsSortedTest, NullOrderArgsTooMany)
{
using T = TypeParam;
auto col1 = testdata::nulls_before<T>();
cudf::table_view in{{col1}};
std::vector<cudf::order> order{};
std::vector<cudf::null_order> null_precedence{cudf::null_order::BEFORE, cudf::null_order::BEFORE};
EXPECT_THROW(cudf::is_sorted(in, order, null_precedence), cudf::logic_error);
}
template <typename T>
struct IsSortedFixedWidthOnly : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(IsSortedFixedWidthOnly, cudf::test::FixedWidthTypes);
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/unary/unary_ops_test.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/unary.hpp>
#include <thrust/iterator/counting_iterator.h>
#include <cuda/std/limits>
template <typename T>
cudf::test::fixed_width_column_wrapper<T> create_fixed_columns(cudf::size_type start,
cudf::size_type size,
bool nullable)
{
auto iter = cudf::detail::make_counting_transform_iterator(start, [](auto i) { return T(i); });
if (not nullable) {
return cudf::test::fixed_width_column_wrapper<T>(iter, iter + size);
} else {
auto valids =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
return cudf::test::fixed_width_column_wrapper<T>(iter, iter + size, valids);
}
}
template <typename T>
cudf::test::fixed_width_column_wrapper<T> create_expected_columns(cudf::size_type size,
bool nullable,
bool nulls_to_be)
{
if (not nullable) {
auto iter = cudf::detail::make_counting_transform_iterator(
0, [nulls_to_be](auto i) { return not nulls_to_be; });
return cudf::test::fixed_width_column_wrapper<T>(iter, iter + size);
} else {
auto iter = cudf::detail::make_counting_transform_iterator(
0, [nulls_to_be](auto i) { return i % 2 == 0 ? not nulls_to_be : nulls_to_be; });
return cudf::test::fixed_width_column_wrapper<T>(iter, iter + size);
}
}
template <typename T>
struct IsNull : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(IsNull, cudf::test::NumericTypes);
TYPED_TEST(IsNull, AllValid)
{
using T = TypeParam;
cudf::size_type start = 0;
cudf::size_type size = 10;
cudf::test::fixed_width_column_wrapper<T> col = create_fixed_columns<T>(start, size, false);
cudf::test::fixed_width_column_wrapper<bool> expected =
create_expected_columns<bool>(size, false, true);
std::unique_ptr<cudf::column> got = cudf::is_null(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNull, WithInvalids)
{
using T = TypeParam;
cudf::size_type start = 0;
cudf::size_type size = 10;
cudf::test::fixed_width_column_wrapper<T> col = create_fixed_columns<T>(start, size, true);
cudf::test::fixed_width_column_wrapper<bool> expected =
create_expected_columns<bool>(size, true, true);
std::unique_ptr<cudf::column> got = cudf::is_null(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNull, EmptyColumns)
{
using T = TypeParam;
cudf::size_type start = 0;
cudf::size_type size = 0;
cudf::test::fixed_width_column_wrapper<T> col = create_fixed_columns<T>(start, size, true);
cudf::test::fixed_width_column_wrapper<bool> expected =
create_expected_columns<bool>(size, true, true);
std::unique_ptr<cudf::column> got = cudf::is_null(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
template <typename T>
struct IsNotNull : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(IsNotNull, cudf::test::NumericTypes);
TYPED_TEST(IsNotNull, AllValid)
{
using T = TypeParam;
cudf::size_type start = 0;
cudf::size_type size = 10;
cudf::test::fixed_width_column_wrapper<T> col = create_fixed_columns<T>(start, size, false);
cudf::test::fixed_width_column_wrapper<bool> expected =
create_expected_columns<bool>(size, false, false);
std::unique_ptr<cudf::column> got = cudf::is_valid(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNotNull, WithInvalids)
{
using T = TypeParam;
cudf::size_type start = 0;
cudf::size_type size = 10;
cudf::test::fixed_width_column_wrapper<T> col = create_fixed_columns<T>(start, size, true);
cudf::test::fixed_width_column_wrapper<bool> expected =
create_expected_columns<bool>(size, true, false);
std::unique_ptr<cudf::column> got = cudf::is_valid(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNotNull, EmptyColumns)
{
using T = TypeParam;
cudf::size_type start = 0;
cudf::size_type size = 0;
cudf::test::fixed_width_column_wrapper<T> col = create_fixed_columns<T>(start, size, true);
cudf::test::fixed_width_column_wrapper<bool> expected =
create_expected_columns<bool>(size, true, false);
std::unique_ptr<cudf::column> got = cudf::is_valid(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
template <typename T>
struct IsNAN : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(IsNAN, cudf::test::FloatingPointTypes);
TYPED_TEST(IsNAN, AllValid)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col{{T(1), T(2), T(NAN), T(4), T(NAN), T(6), T(7)}};
cudf::test::fixed_width_column_wrapper<bool> expected = {
false, false, true, false, true, false, false};
std::unique_ptr<cudf::column> got = cudf::is_nan(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNAN, WithNull)
{
using T = TypeParam;
// The last NAN is null
cudf::test::fixed_width_column_wrapper<T> col{{T(1), T(2), T(NAN), T(4), T(NAN), T(6), T(7)},
{1, 0, 1, 1, 0, 1, 1}};
cudf::test::fixed_width_column_wrapper<bool> expected = {
false, false, true, false, false, false, false};
std::unique_ptr<cudf::column> got = cudf::is_nan(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNAN, EmptyColumn)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col{};
cudf::test::fixed_width_column_wrapper<bool> expected = {};
std::unique_ptr<cudf::column> got = cudf::is_nan(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNAN, NonFloatingColumn)
{
cudf::test::fixed_width_column_wrapper<int32_t> col{{1, 2, 5, 3, 5, 6, 7}, {1, 0, 1, 1, 0, 1, 1}};
EXPECT_THROW(std::unique_ptr<cudf::column> got = cudf::is_nan(col), cudf::logic_error);
}
template <typename T>
struct IsNotNAN : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(IsNotNAN, cudf::test::FloatingPointTypes);
TYPED_TEST(IsNotNAN, AllValid)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col{{T(1), T(2), T(NAN), T(4), T(NAN), T(6), T(7)}};
cudf::test::fixed_width_column_wrapper<bool> expected = {
true, true, false, true, false, true, true};
std::unique_ptr<cudf::column> got = cudf::is_not_nan(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNotNAN, WithNull)
{
using T = TypeParam;
// The last NAN is null
cudf::test::fixed_width_column_wrapper<T> col{{T(1), T(2), T(NAN), T(4), T(NAN), T(6), T(7)},
{1, 0, 1, 1, 0, 1, 1}};
cudf::test::fixed_width_column_wrapper<bool> expected = {
true, true, false, true, true, true, true};
std::unique_ptr<cudf::column> got = cudf::is_not_nan(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNotNAN, EmptyColumn)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> col{};
cudf::test::fixed_width_column_wrapper<bool> expected = {};
std::unique_ptr<cudf::column> got = cudf::is_not_nan(col);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, got->view());
}
TYPED_TEST(IsNotNAN, NonFloatingColumn)
{
cudf::test::fixed_width_column_wrapper<int64_t> col{{1, 2, 5, 3, 5, 6, 7}, {1, 0, 1, 1, 0, 1, 1}};
EXPECT_THROW(std::unique_ptr<cudf::column> got = cudf::is_not_nan(col), cudf::logic_error);
}
template <typename T>
struct FixedPointUnaryTests : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(FixedPointUnaryTests, cudf::test::FixedPointTypes);
TYPED_TEST(FixedPointUnaryTests, FixedPointUnaryAbs)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{-1234, -3456, -6789, 1234, 3456, 6789}, scale_type{-3}};
auto const expected = fp_wrapper{{1234, 3456, 6789, 1234, 3456, 6789}, scale_type{-3}};
auto const result = cudf::unary_operation(input, cudf::unary_operator::ABS);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointUnaryTests, FixedPointUnaryAbsPositiveScale)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{-1234, -3456, -6789, 1234, 3456, 6789}, scale_type{1}};
auto const expected = fp_wrapper{{1234, 3456, 6789, 1234, 3456, 6789}, scale_type{1}};
auto const result = cudf::unary_operation(input, cudf::unary_operator::ABS);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointUnaryTests, FixedPointUnaryAbsLarge)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto a = thrust::make_counting_iterator(-2000);
auto b =
cudf::detail::make_counting_transform_iterator(-2000, [](auto e) { return std::abs(e); });
auto const input = fp_wrapper{a, a + 4000, scale_type{-1}};
auto const expected = fp_wrapper{b, b + 4000, scale_type{-1}};
auto const result = cudf::unary_operation(input, cudf::unary_operator::ABS);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointUnaryTests, FixedPointUnaryCeil)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{-1234, -3456, -6789, 1234, 3456, 7000, 0}, scale_type{-3}};
auto const expected = fp_wrapper{{-1000, -3000, -6000, 2000, 4000, 7000, 0}, scale_type{-3}};
auto const result = cudf::unary_operation(input, cudf::unary_operator::CEIL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointUnaryTests, FixedPointUnaryCeilLarge)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto a = thrust::make_counting_iterator(-5000);
auto b =
cudf::detail::make_counting_transform_iterator(-5000, [](int e) { return (e / 10) * 10; });
auto const input = fp_wrapper{a, a + 4000, scale_type{-1}};
auto const expected = fp_wrapper{b, b + 4000, scale_type{-1}};
auto const result = cudf::unary_operation(input, cudf::unary_operator::CEIL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointUnaryTests, FixedPointUnaryFloor)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{-1234, -3456, -6789, 1234, 3456, 6000, 0}, scale_type{-3}};
auto const expected = fp_wrapper{{-2000, -4000, -7000, 1000, 3000, 6000, 0}, scale_type{-3}};
auto const result = cudf::unary_operation(input, cudf::unary_operator::FLOOR);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointUnaryTests, FixedPointUnaryFloorLarge)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto a = thrust::make_counting_iterator(100);
auto b =
cudf::detail::make_counting_transform_iterator(100, [](auto e) { return (e / 10) * 10; });
auto const input = fp_wrapper{a, a + 4000, scale_type{-1}};
auto const expected = fp_wrapper{b, b + 4000, scale_type{-1}};
auto const result = cudf::unary_operation(input, cudf::unary_operator::FLOOR);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointUnaryTests, ValidateCeilFloorPrecision)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
// This test is designed to protect against floating point conversion
// introducing errors in fixed-point arithmetic. The rounding that occurs
// during ceil/floor should only use fixed-precision math. Realistically,
// we are only able to show precision failures due to floating conversion in
// a few very specific circumstances where dividing by specific powers of 10
// works against us. Some examples: 10^23, 10^25, 10^26, 10^27, 10^30,
// 10^32, 10^36. See https://godbolt.org/z/cP1MddP8P for a derivation. For
// completeness and to ensure that we are not missing any other cases, we
// test all scales representable in the range of each decimal type.
constexpr auto min_scale = -cuda::std::numeric_limits<RepType>::digits10;
for (int input_scale = 0; input_scale >= min_scale; --input_scale) {
RepType input_value = 1;
for (int k = 0; k > input_scale; --k) {
input_value *= 10;
}
auto const input = fp_wrapper{{input_value}, scale_type{input_scale}};
auto const expected = fp_wrapper{{input_value}, scale_type{input_scale}};
auto const ceil_result = cudf::unary_operation(input, cudf::unary_operator::CEIL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, ceil_result->view());
auto const floor_result = cudf::unary_operation(input, cudf::unary_operator::FLOOR);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, floor_result->view());
}
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/unary/cast_tests.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/iterator.cuh>
#include <cudf/detail/utilities/integer_utils.hpp>
#include <cudf/fixed_point/fixed_point.hpp>
#include <cudf/unary.hpp>
#include <cudf/utilities/bit.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/wrappers/timestamps.hpp>
#include <thrust/host_vector.h>
#include <thrust/iterator/counting_iterator.h>
#include <cuda/std/limits>
#include <type_traits>
#include <vector>
static auto const test_timestamps_D = std::vector<int32_t>{
-1528, // 1965-10-26 GMT
17716, // 2018-07-04 GMT
19382, // 2023-01-25 GMT
};
static auto const test_timestamps_s = std::vector<int64_t>{
-131968728, // 1965-10-26 14:01:12 GMT
1530705600, // 2018-07-04 12:00:00 GMT
1674631932, // 2023-01-25 07:32:12 GMT
};
static auto const test_timestamps_ms = std::vector<int64_t>{
-131968727238, // 1965-10-26 14:01:12.762 GMT
1530705600000, // 2018-07-04 12:00:00.000 GMT
1674631932929, // 2023-01-25 07:32:12.929 GMT
};
static auto const test_timestamps_us = std::vector<int64_t>{
-131968727238000, // 1965-10-26 14:01:12.762000000 GMT
1530705600000000, // 2018-07-04 12:00:00.000000000 GMT
1674631932929000, // 2023-01-25 07:32:12.929000000 GMT
};
static auto const test_timestamps_ns = std::vector<int64_t>{
-131968727238000000, // 1965-10-26 14:01:12.762000000 GMT
1530705600000000000, // 2018-07-04 12:00:00.000000000 GMT
1674631932929000000, // 2023-01-25 07:32:12.929000000 GMT
};
static auto const test_durations_D = test_timestamps_D;
static auto const test_durations_s = test_timestamps_s;
static auto const test_durations_ms = test_timestamps_ms;
static auto const test_durations_us = test_timestamps_us;
static auto const test_durations_ns = test_timestamps_ns;
template <typename T, typename R>
inline auto make_column(std::vector<R> data)
{
return cudf::test::fixed_width_column_wrapper<T, R>(data.begin(), data.end());
}
template <typename T, typename R>
inline auto make_column(std::vector<R> data, std::vector<bool> mask)
{
return cudf::test::fixed_width_column_wrapper<T, R>(data.begin(), data.end(), mask.begin());
}
inline cudf::column make_exp_chrono_column(cudf::type_id type_id)
{
switch (type_id) {
case cudf::type_id::TIMESTAMP_DAYS:
return cudf::column(
cudf::data_type{type_id},
test_timestamps_D.size(),
rmm::device_buffer{test_timestamps_D.data(),
test_timestamps_D.size() * sizeof(test_timestamps_D.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::TIMESTAMP_SECONDS:
return cudf::column(
cudf::data_type{type_id},
test_timestamps_s.size(),
rmm::device_buffer{test_timestamps_s.data(),
test_timestamps_s.size() * sizeof(test_timestamps_s.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::TIMESTAMP_MILLISECONDS:
return cudf::column(
cudf::data_type{type_id},
test_timestamps_ms.size(),
rmm::device_buffer{test_timestamps_ms.data(),
test_timestamps_ms.size() * sizeof(test_timestamps_ms.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::TIMESTAMP_MICROSECONDS:
return cudf::column(
cudf::data_type{type_id},
test_timestamps_us.size(),
rmm::device_buffer{test_timestamps_us.data(),
test_timestamps_us.size() * sizeof(test_timestamps_us.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::TIMESTAMP_NANOSECONDS:
return cudf::column(
cudf::data_type{type_id},
test_timestamps_ns.size(),
rmm::device_buffer{test_timestamps_ns.data(),
test_timestamps_ns.size() * sizeof(test_timestamps_ns.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::DURATION_DAYS:
return cudf::column(
cudf::data_type{type_id},
test_durations_D.size(),
rmm::device_buffer{test_durations_D.data(),
test_durations_D.size() * sizeof(test_durations_D.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::DURATION_SECONDS:
return cudf::column(
cudf::data_type{type_id},
test_durations_s.size(),
rmm::device_buffer{test_durations_s.data(),
test_durations_s.size() * sizeof(test_durations_s.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::DURATION_MILLISECONDS:
return cudf::column(
cudf::data_type{type_id},
test_durations_ms.size(),
rmm::device_buffer{test_durations_ms.data(),
test_durations_ms.size() * sizeof(test_durations_ms.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::DURATION_MICROSECONDS:
return cudf::column(
cudf::data_type{type_id},
test_durations_us.size(),
rmm::device_buffer{test_durations_us.data(),
test_durations_us.size() * sizeof(test_durations_us.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
case cudf::type_id::DURATION_NANOSECONDS:
return cudf::column(
cudf::data_type{type_id},
test_durations_ns.size(),
rmm::device_buffer{test_durations_ns.data(),
test_durations_ns.size() * sizeof(test_durations_ns.front()),
cudf::get_default_stream()},
rmm::device_buffer{},
0);
default: CUDF_FAIL("Unsupported type_id");
}
};
template <typename T, typename R>
inline auto make_column(thrust::host_vector<R> data)
{
return cudf::test::fixed_width_column_wrapper<T, R>(data.begin(), data.end());
}
template <typename T, typename R>
inline auto make_column(thrust::host_vector<R> data, thrust::host_vector<bool> mask)
{
return cudf::test::fixed_width_column_wrapper<T, R>(data.begin(), data.end(), mask.begin());
}
template <typename T, typename R>
void validate_cast_result(cudf::column_view expected, cudf::column_view actual)
{
using namespace cudf::test;
// round-trip through the host because sizeof(T) may not equal sizeof(R)
auto [h_data, null_mask] = to_host<T>(expected);
if (null_mask.empty()) {
CUDF_TEST_EXPECT_COLUMNS_EQUAL(make_column<R, T>(h_data), actual);
} else {
thrust::host_vector<bool> h_null_mask(expected.size());
for (cudf::size_type i = 0; i < expected.size(); ++i) {
h_null_mask[i] = cudf::bit_is_set(null_mask.data(), i);
}
CUDF_TEST_EXPECT_COLUMNS_EQUAL(make_column<R, T>(h_data, h_null_mask), actual);
}
}
template <typename T>
inline auto make_data_type()
{
return cudf::data_type{cudf::type_to_id<T>()};
}
struct CastTimestampsSimple : public cudf::test::BaseFixture {};
TEST_F(CastTimestampsSimple, IsIdempotent)
{
using namespace cudf::test;
auto timestamps_D = make_column<cudf::timestamp_D>(test_timestamps_D);
auto timestamps_s = make_column<cudf::timestamp_s>(test_timestamps_s);
auto timestamps_ms = make_column<cudf::timestamp_ms>(test_timestamps_ms);
auto timestamps_us = make_column<cudf::timestamp_us>(test_timestamps_us);
auto timestamps_ns = make_column<cudf::timestamp_ns>(test_timestamps_ns);
// Timestamps to duration
auto timestamps_D_dur = cudf::cast(timestamps_D, make_data_type<cudf::timestamp_D::duration>());
auto timestamps_s_dur = cudf::cast(timestamps_s, make_data_type<cudf::timestamp_s::duration>());
auto timestamps_ms_dur =
cudf::cast(timestamps_ms, make_data_type<cudf::timestamp_ms::duration>());
auto timestamps_us_dur =
cudf::cast(timestamps_us, make_data_type<cudf::timestamp_us::duration>());
auto timestamps_ns_dur =
cudf::cast(timestamps_ns, make_data_type<cudf::timestamp_ns::duration>());
// Duration back to timestamp
auto timestamps_D_got =
cudf::cast(*timestamps_D_dur, cudf::data_type{cudf::type_id::TIMESTAMP_DAYS});
auto timestamps_s_got =
cudf::cast(*timestamps_s_dur, cudf::data_type{cudf::type_id::TIMESTAMP_SECONDS});
auto timestamps_ms_got =
cudf::cast(*timestamps_ms_dur, cudf::data_type{cudf::type_id::TIMESTAMP_MILLISECONDS});
auto timestamps_us_got =
cudf::cast(*timestamps_us_dur, cudf::data_type{cudf::type_id::TIMESTAMP_MICROSECONDS});
auto timestamps_ns_got =
cudf::cast(*timestamps_ns_dur, cudf::data_type{cudf::type_id::TIMESTAMP_NANOSECONDS});
validate_cast_result<cudf::timestamp_D, cudf::timestamp_D>(timestamps_D, *timestamps_D_got);
validate_cast_result<cudf::timestamp_s, cudf::timestamp_s>(timestamps_s, *timestamps_s_got);
validate_cast_result<cudf::timestamp_ms, cudf::timestamp_ms>(timestamps_ms, *timestamps_ms_got);
validate_cast_result<cudf::timestamp_us, cudf::timestamp_us>(timestamps_us, *timestamps_us_got);
validate_cast_result<cudf::timestamp_ns, cudf::timestamp_ns>(timestamps_ns, *timestamps_ns_got);
}
struct CastDurationsSimple : public cudf::test::BaseFixture {};
TEST_F(CastDurationsSimple, IsIdempotent)
{
using namespace cudf::test;
auto durations_D = make_column<cudf::duration_D>(test_durations_D);
auto durations_s = make_column<cudf::duration_s>(test_durations_s);
auto durations_ms = make_column<cudf::duration_ms>(test_durations_ms);
auto durations_us = make_column<cudf::duration_us>(test_durations_us);
auto durations_ns = make_column<cudf::duration_ns>(test_durations_ns);
auto durations_D_rep = cudf::cast(durations_D, make_data_type<cudf::duration_D::rep>());
auto durations_s_rep = cudf::cast(durations_s, make_data_type<cudf::duration_s::rep>());
auto durations_ms_rep = cudf::cast(durations_ms, make_data_type<cudf::duration_ms::rep>());
auto durations_us_rep = cudf::cast(durations_us, make_data_type<cudf::duration_us::rep>());
auto durations_ns_rep = cudf::cast(durations_ns, make_data_type<cudf::duration_ns::rep>());
auto durations_D_got =
cudf::cast(*durations_D_rep, cudf::data_type{cudf::type_id::DURATION_DAYS});
auto durations_s_got =
cudf::cast(*durations_s_rep, cudf::data_type{cudf::type_id::DURATION_SECONDS});
auto durations_ms_got =
cudf::cast(*durations_ms_rep, cudf::data_type{cudf::type_id::DURATION_MILLISECONDS});
auto durations_us_got =
cudf::cast(*durations_us_rep, cudf::data_type{cudf::type_id::DURATION_MICROSECONDS});
auto durations_ns_got =
cudf::cast(*durations_ns_rep, cudf::data_type{cudf::type_id::DURATION_NANOSECONDS});
validate_cast_result<cudf::duration_D, cudf::duration_D>(durations_D, *durations_D_got);
validate_cast_result<cudf::duration_s, cudf::duration_s>(durations_s, *durations_s_got);
validate_cast_result<cudf::duration_ms, cudf::duration_ms>(durations_ms, *durations_ms_got);
validate_cast_result<cudf::duration_us, cudf::duration_us>(durations_us, *durations_us_got);
validate_cast_result<cudf::duration_ns, cudf::duration_ns>(durations_ns, *durations_ns_got);
}
template <typename T>
struct CastChronosTyped : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(CastChronosTyped, cudf::test::ChronoTypes);
// Return a list of chrono type ids whose precision is greater than or equal
// to the input type id
std::vector<cudf::type_id> get_higher_precision_chrono_type_ids(cudf::type_id search)
{
size_t idx = 0;
std::vector<cudf::type_id> gte_ids{};
// Arranged such that for every pair of types, the types that precede them have a lower precision
std::vector<cudf::type_id> timestamp_ids{cudf::type_id::TIMESTAMP_DAYS,
cudf::type_id::DURATION_DAYS,
cudf::type_id::TIMESTAMP_SECONDS,
cudf::type_id::DURATION_SECONDS,
cudf::type_id::TIMESTAMP_MILLISECONDS,
cudf::type_id::DURATION_MILLISECONDS,
cudf::type_id::TIMESTAMP_MICROSECONDS,
cudf::type_id::DURATION_MICROSECONDS,
cudf::type_id::TIMESTAMP_NANOSECONDS,
cudf::type_id::DURATION_NANOSECONDS};
for (cudf::type_id type_id : timestamp_ids) {
if (type_id == search) break;
idx++;
}
for (auto i = idx - idx % 2; i < timestamp_ids.size(); ++i)
gte_ids.emplace_back(timestamp_ids[i]);
return gte_ids;
}
// Test that all chrono types whose precision is >= to the TypeParam
// down-casts appropriately to the lower-precision TypeParam
TYPED_TEST(CastChronosTyped, DownCastingFloorsValues)
{
using T = TypeParam;
using namespace cudf::test;
auto dtype_exp = make_data_type<T>();
auto chrono_exp = make_exp_chrono_column(dtype_exp.id());
// Construct a list of the chrono type_ids whose precision is
// greater than or equal to the precision of TypeParam's, e.g:
// timestamp_ms -> {timestamp_ms, duration_ms, timestamp_us, duration_us, timestamp_ns,
// duration_ns}; duration_us -> {timestamp_us, duration_us, timestamp_ns, duration_ns}; etc.
auto higher_precision_type_ids = get_higher_precision_chrono_type_ids(cudf::type_to_id<T>());
// For each higher-precision type, down-cast to TypeParam and validate
// that the values were floored.
for (cudf::type_id higher_precision_type_id : higher_precision_type_ids) {
auto chrono_src = make_exp_chrono_column(higher_precision_type_id);
auto chrono_got = cudf::cast(chrono_src, dtype_exp);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*chrono_got, chrono_exp);
}
}
// Specific test to ensure down-casting to days happens correctly
TYPED_TEST(CastChronosTyped, DownCastingToDaysFloorsValues)
{
using T = TypeParam;
using namespace cudf::test;
auto dtype_src = make_data_type<T>();
auto chrono_src = make_exp_chrono_column(dtype_src.id());
// Convert {timestamp|duration}_X => timestamp_D
auto timestamp_dtype_out = make_data_type<cudf::timestamp_D>();
auto timestamps_got = cudf::cast(chrono_src, timestamp_dtype_out);
auto timestamp_exp = make_column<cudf::timestamp_D>(test_timestamps_D);
validate_cast_result<cudf::timestamp_D, cudf::timestamp_D>(timestamp_exp, *timestamps_got);
// Convert {timestamp|duration}_X => duration_D
auto duration_dtype_out = make_data_type<cudf::duration_D>();
auto duration_got = cudf::cast(chrono_src, duration_dtype_out);
auto duration_exp = make_column<cudf::duration_D>(test_durations_D);
validate_cast_result<cudf::duration_D, cudf::duration_D>(duration_exp, *duration_got);
}
struct CastToTimestamps : public cudf::test::BaseFixture {};
// Cast duration types to timestamps (as integral types can't be converted)
TEST_F(CastToTimestamps, AllValid)
{
using namespace cudf::test;
auto durations_D = make_column<cudf::duration_D>(test_durations_D);
auto durations_s = make_column<cudf::duration_s>(test_durations_s);
auto durations_ms = make_column<cudf::duration_ms>(test_durations_ms);
auto durations_us = make_column<cudf::duration_us>(test_durations_us);
auto durations_ns = make_column<cudf::duration_ns>(test_durations_ns);
auto timestamps_D_got = cudf::cast(durations_D, cudf::data_type{cudf::type_id::TIMESTAMP_DAYS});
auto timestamps_s_got =
cudf::cast(durations_s, cudf::data_type{cudf::type_id::TIMESTAMP_SECONDS});
auto timestamps_ms_got =
cudf::cast(durations_ms, cudf::data_type{cudf::type_id::TIMESTAMP_MILLISECONDS});
auto timestamps_us_got =
cudf::cast(durations_us, cudf::data_type{cudf::type_id::TIMESTAMP_MICROSECONDS});
auto timestamps_ns_got =
cudf::cast(durations_ns, cudf::data_type{cudf::type_id::TIMESTAMP_NANOSECONDS});
validate_cast_result<cudf::duration_D, cudf::timestamp_D>(durations_D, *timestamps_D_got);
validate_cast_result<cudf::duration_s, cudf::timestamp_s>(durations_s, *timestamps_s_got);
validate_cast_result<cudf::duration_ms, cudf::timestamp_ms>(durations_ms, *timestamps_ms_got);
validate_cast_result<cudf::duration_us, cudf::timestamp_us>(durations_us, *timestamps_us_got);
validate_cast_result<cudf::duration_ns, cudf::timestamp_ns>(durations_ns, *timestamps_ns_got);
}
struct CastFromTimestamps : public cudf::test::BaseFixture {};
// Convert timestamps to duration types
TEST_F(CastFromTimestamps, AllValid)
{
using namespace cudf::test;
auto timestamps_D = make_column<cudf::timestamp_D>(test_timestamps_D);
auto timestamps_s = make_column<cudf::timestamp_s>(test_timestamps_s);
auto timestamps_ms = make_column<cudf::timestamp_ms>(test_timestamps_ms);
auto timestamps_us = make_column<cudf::timestamp_us>(test_timestamps_us);
auto timestamps_ns = make_column<cudf::timestamp_ns>(test_timestamps_ns);
auto duration_D_exp = make_column<cudf::duration_D>(test_durations_D);
auto duration_s_exp = make_column<cudf::duration_s>(test_durations_s);
auto duration_ms_exp = make_column<cudf::duration_us>(test_durations_ms);
auto duration_us_exp = make_column<cudf::duration_ms>(test_durations_us);
auto duration_ns_exp = make_column<cudf::duration_ns>(test_durations_ns);
auto durations_D_got = cudf::cast(timestamps_D, make_data_type<cudf::duration_D>());
auto durations_s_got = cudf::cast(timestamps_s, make_data_type<cudf::duration_s>());
auto durations_ms_got = cudf::cast(timestamps_ms, make_data_type<cudf::duration_ms>());
auto durations_us_got = cudf::cast(timestamps_us, make_data_type<cudf::duration_us>());
auto durations_ns_got = cudf::cast(timestamps_ns, make_data_type<cudf::duration_ns>());
validate_cast_result<cudf::duration_D, cudf::duration_D>(duration_D_exp, *durations_D_got);
validate_cast_result<cudf::duration_s, cudf::duration_s>(duration_s_exp, *durations_s_got);
validate_cast_result<cudf::duration_ms, cudf::duration_ms>(duration_ms_exp, *durations_ms_got);
validate_cast_result<cudf::duration_us, cudf::duration_us>(duration_us_exp, *durations_us_got);
validate_cast_result<cudf::duration_ns, cudf::duration_ns>(duration_ns_exp, *durations_ns_got);
}
TEST_F(CastFromTimestamps, WithNulls)
{
using namespace cudf::test;
auto timestamps_D = make_column<cudf::timestamp_D>(test_timestamps_D, {true, false, true});
auto timestamps_s = make_column<cudf::timestamp_s>(test_timestamps_s, {true, false, true});
auto timestamps_ms = make_column<cudf::timestamp_ms>(test_timestamps_ms, {true, false, true});
auto timestamps_us = make_column<cudf::timestamp_us>(test_timestamps_us, {true, false, true});
auto timestamps_ns = make_column<cudf::timestamp_ns>(test_timestamps_ns, {true, false, true});
auto duration_D_exp = make_column<cudf::duration_D>(test_durations_D, {true, false, true});
auto duration_s_exp = make_column<cudf::duration_s>(test_durations_s, {true, false, true});
auto duration_ms_exp = make_column<cudf::duration_us>(test_durations_ms, {true, false, true});
auto duration_us_exp = make_column<cudf::duration_ms>(test_durations_us, {true, false, true});
auto duration_ns_exp = make_column<cudf::duration_ns>(test_durations_ns, {true, false, true});
auto durations_D_got = cudf::cast(timestamps_D, make_data_type<cudf::duration_D>());
auto durations_s_got = cudf::cast(timestamps_s, make_data_type<cudf::duration_s>());
auto durations_ms_got = cudf::cast(timestamps_ms, make_data_type<cudf::duration_ms>());
auto durations_us_got = cudf::cast(timestamps_us, make_data_type<cudf::duration_us>());
auto durations_ns_got = cudf::cast(timestamps_ns, make_data_type<cudf::duration_ns>());
validate_cast_result<cudf::duration_D, cudf::duration_D>(duration_D_exp, *durations_D_got);
validate_cast_result<cudf::duration_s, cudf::duration_s>(duration_s_exp, *durations_s_got);
validate_cast_result<cudf::duration_ms, cudf::duration_ms>(duration_ms_exp, *durations_ms_got);
validate_cast_result<cudf::duration_us, cudf::duration_us>(duration_us_exp, *durations_us_got);
validate_cast_result<cudf::duration_ns, cudf::duration_ns>(duration_ns_exp, *durations_ns_got);
}
template <typename T>
struct CastToDurations : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(CastToDurations, cudf::test::IntegralTypes);
TYPED_TEST(CastToDurations, AllValid)
{
using T = TypeParam;
using namespace cudf::test;
auto durations_D = make_column<T>(test_durations_D);
auto durations_s = make_column<T>(test_durations_s);
auto durations_ms = make_column<T>(test_durations_ms);
auto durations_us = make_column<T>(test_durations_us);
auto durations_ns = make_column<T>(test_durations_ns);
auto durations_D_got = cudf::cast(durations_D, cudf::data_type{cudf::type_id::DURATION_DAYS});
auto durations_s_got = cudf::cast(durations_s, cudf::data_type{cudf::type_id::DURATION_SECONDS});
auto durations_ms_got =
cudf::cast(durations_ms, cudf::data_type{cudf::type_id::DURATION_MILLISECONDS});
auto durations_us_got =
cudf::cast(durations_us, cudf::data_type{cudf::type_id::DURATION_MICROSECONDS});
auto durations_ns_got =
cudf::cast(durations_ns, cudf::data_type{cudf::type_id::DURATION_NANOSECONDS});
validate_cast_result<T, cudf::duration_D>(durations_D, *durations_D_got);
validate_cast_result<T, cudf::duration_s>(durations_s, *durations_s_got);
validate_cast_result<T, cudf::duration_ms>(durations_ms, *durations_ms_got);
validate_cast_result<T, cudf::duration_us>(durations_us, *durations_us_got);
validate_cast_result<T, cudf::duration_ns>(durations_ns, *durations_ns_got);
}
template <typename T>
struct CastFromDurations : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(CastFromDurations, cudf::test::NumericTypes);
TYPED_TEST(CastFromDurations, AllValid)
{
using T = TypeParam;
using namespace cudf::test;
auto durations_D = make_column<cudf::duration_D>(test_durations_D);
auto durations_s = make_column<cudf::duration_s>(test_durations_s);
auto durations_ms = make_column<cudf::duration_ms>(test_durations_ms);
auto durations_us = make_column<cudf::duration_us>(test_durations_us);
auto durations_ns = make_column<cudf::duration_ns>(test_durations_ns);
auto durations_D_exp = make_column<T>(test_durations_D);
auto durations_s_exp = make_column<T>(test_durations_s);
auto durations_ms_exp = make_column<T>(test_durations_ms);
auto durations_us_exp = make_column<T>(test_durations_us);
auto durations_ns_exp = make_column<T>(test_durations_ns);
auto durations_D_got = cudf::cast(durations_D, make_data_type<T>());
auto durations_s_got = cudf::cast(durations_s, make_data_type<T>());
auto durations_ms_got = cudf::cast(durations_ms, make_data_type<T>());
auto durations_us_got = cudf::cast(durations_us, make_data_type<T>());
auto durations_ns_got = cudf::cast(durations_ns, make_data_type<T>());
validate_cast_result<T, T>(durations_D_exp, *durations_D_got);
validate_cast_result<T, T>(durations_s_exp, *durations_s_got);
validate_cast_result<T, T>(durations_ms_exp, *durations_ms_got);
validate_cast_result<T, T>(durations_us_exp, *durations_us_got);
validate_cast_result<T, T>(durations_ns_exp, *durations_ns_got);
}
TYPED_TEST(CastFromDurations, WithNulls)
{
using T = TypeParam;
using namespace cudf::test;
auto durations_D = make_column<cudf::duration_D>(test_durations_D, {true, false, true});
auto durations_s = make_column<cudf::duration_s>(test_durations_s, {true, false, true});
auto durations_ms = make_column<cudf::duration_ms>(test_durations_ms, {true, false, true});
auto durations_us = make_column<cudf::duration_us>(test_durations_us, {true, false, true});
auto durations_ns = make_column<cudf::duration_ns>(test_durations_ns, {true, false, true});
auto durations_D_exp = make_column<T>(test_durations_D, {true, false, true});
auto durations_s_exp = make_column<T>(test_durations_s, {true, false, true});
auto durations_ms_exp = make_column<T>(test_durations_ms, {true, false, true});
auto durations_us_exp = make_column<T>(test_durations_us, {true, false, true});
auto durations_ns_exp = make_column<T>(test_durations_ns, {true, false, true});
auto durations_D_got = cudf::cast(durations_D, make_data_type<T>());
auto durations_s_got = cudf::cast(durations_s, make_data_type<T>());
auto durations_ms_got = cudf::cast(durations_ms, make_data_type<T>());
auto durations_us_got = cudf::cast(durations_us, make_data_type<T>());
auto durations_ns_got = cudf::cast(durations_ns, make_data_type<T>());
validate_cast_result<T, T>(durations_D_exp, *durations_D_got);
validate_cast_result<T, T>(durations_s_exp, *durations_s_got);
validate_cast_result<T, T>(durations_ms_exp, *durations_ms_got);
validate_cast_result<T, T>(durations_us_exp, *durations_us_got);
validate_cast_result<T, T>(durations_ns_exp, *durations_ns_got);
}
template <typename T>
inline auto make_fixed_point_data_type(int32_t scale)
{
return cudf::data_type{cudf::type_to_id<T>(), scale};
}
struct FixedPointTestSingleType : public cudf::test::BaseFixture {};
template <typename T>
struct FixedPointTests : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(FixedPointTests, cudf::test::FixedPointTypes);
TYPED_TEST(FixedPointTests, CastToDouble)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<double>;
auto const input = fp_wrapper{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fw_wrapper{1.729, 17.29, 172.9, 1729.0};
auto const result = cudf::cast(input, make_data_type<double>());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, CastToDoubleLarge)
{
using namespace numeric;
using namespace cudf::test;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<double>;
auto begin =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return 10 * (i + 0.5); });
auto begin2 = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i + 0.5; });
auto const input = fp_wrapper{begin, begin + 2000, scale_type{-1}};
auto const expected = fw_wrapper(begin2, begin2 + 2000);
auto const result = cudf::cast(input, make_data_type<double>());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, CastToInt32)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
auto const input = fp_wrapper{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fw_wrapper{1, 17, 172, 1729};
auto const result = cudf::cast(input, make_data_type<int32_t>());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TEST_F(FixedPointTestSingleType, CastDecimal64ToInt32)
{
using fp_wrapper = cudf::test::fixed_point_column_wrapper<int64_t>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
auto const input = fp_wrapper{{7246212000}, numeric::scale_type{-5}};
auto const expected = fw_wrapper{72462};
auto const result = cudf::cast(input, make_data_type<int32_t>());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, CastToIntLarge)
{
using namespace numeric;
using namespace cudf::test;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
auto begin = thrust::make_counting_iterator(0);
auto begin2 = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return 10 * i; });
auto const input = fp_wrapper{begin, begin + 2000, scale_type{1}};
auto const expected = fw_wrapper(begin2, begin2 + 2000);
auto const result = cudf::cast(input, make_data_type<int32_t>());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, CastFromDouble)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<double>;
auto const input = fw_wrapper{1.729, 17.29, 172.9, 1729.0};
auto const expected = fp_wrapper{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-3));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, CastFromDoubleLarge)
{
using namespace numeric;
using namespace cudf::test;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<double>;
auto begin = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i + 0.5; });
auto begin2 =
cudf::detail::make_counting_transform_iterator(0, [](auto i) { return 10 * (i + 0.5); });
auto const input = fw_wrapper(begin, begin + 2000);
auto const expected = fp_wrapper{begin2, begin2 + 2000, scale_type{-1}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-1));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, CastFromInt)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
auto const input = fw_wrapper{1729, 172, 17, 1};
auto const expected = fp_wrapper{{17, 1, 0, 0}, scale_type{2}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(2));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TEST_F(FixedPointTestSingleType, CastInt32ToDecimal64)
{
using fp_wrapper = cudf::test::fixed_point_column_wrapper<int64_t>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
auto const input = fw_wrapper{-48938};
auto const expected = fp_wrapper{{-4893800000LL}, numeric::scale_type{-5}};
auto const result = cudf::cast(input, make_fixed_point_data_type<numeric::decimal64>(-5));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, CastFromIntLarge)
{
using namespace numeric;
using namespace cudf::test;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using fw_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
auto begin = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return 1000 * i; });
auto begin2 = thrust::make_counting_iterator(0);
auto const input = fw_wrapper(begin, begin + 2000);
auto const expected = fp_wrapper{begin2, begin2 + 2000, scale_type{3}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(3));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, FixedPointToFixedPointSameTypeidUp)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapper{{172, 1729, 17290, 172900}, scale_type{-2}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-2));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, FixedPointToFixedPointSameTypeidDown)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapper{{17290, 172900, 1729000, 17290000}, scale_type{-4}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-4));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, FixedPointToFixedPointSameTypeidUpPositive)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{1, 12, 123, 1234, 12345, 123456}, scale_type{1}};
auto const expected = fp_wrapper{{0, 1, 12, 123, 1234, 12345}, scale_type{2}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(2));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, FixedPointToFixedPointSameTypeidEmpty)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{}, scale_type{1}};
auto const expected = fp_wrapper{{}, scale_type{2}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(2));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, FixedPointToFixedPointSameTypeidDownPositive)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const input = fp_wrapper{{0, 1, 12, 123, 1234}, scale_type{2}};
auto const expected = fp_wrapper{{0, 1000, 12000, 123000, 1234000}, scale_type{-1}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-1));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal32ToDecimalXX)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = int32_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-3));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal64ToDecimalXX)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = int64_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-3));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal128ToDecimalXX)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = __int128_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-3));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal32ToDecimalXXWithSmallerScale)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = int32_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{172900, 1729000, 17290000, 172900000}, scale_type{-5}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-5));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal64ToDecimalXXWithSmallerScale)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = int64_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{172900, 1729000, 17290000, 172900000}, scale_type{-5}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-5));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal128ToDecimalXXWithSmallerScale)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = __int128_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{172900, 1729000, 17290000, 172900000}, scale_type{-5}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(-5));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal32ToDecimalXXWithLargerScale)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = int32_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1, 17, 172, 1729}, scale_type{0}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal64ToDecimalXXWithLargerScale)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = int64_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1, 17, 172, 1729}, scale_type{0}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TEST_F(FixedPointTestSingleType, AvoidOverflowDecimal32ToDecimal64)
{
using namespace numeric;
using fp_wrapper32 = cudf::test::fixed_point_column_wrapper<int32_t>;
using fp_wrapper64 = cudf::test::fixed_point_column_wrapper<int64_t>;
auto const input = fp_wrapper32{{9999999}, scale_type{3}};
auto const expected = fp_wrapper64{{9999999}, scale_type{3}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimal64>(3));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TEST_F(FixedPointTestSingleType, AvoidOverflowDecimal32ToInt64)
{
using namespace numeric;
using fp_wrapper32 = cudf::test::fixed_point_column_wrapper<int32_t>;
using fw_wrapper64 = cudf::test::fixed_width_column_wrapper<int64_t>;
auto const input = fp_wrapper32{{9999999}, scale_type{3}};
auto const expected = fw_wrapper64{{9999999000}};
auto const result = cudf::cast(input, cudf::data_type{cudf::type_id::INT64});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal128ToDecimalXXWithLargerScale)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = __int128_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const input = fp_wrapperFrom{{1729, 17290, 172900, 1729000}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1, 17, 172, 1729}, scale_type{0}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, ValidateCastRescalePrecision)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
// This test is designed to protect against floating point conversion
// introducing errors in fixed-point arithmetic. The rescaling that occurs
// during casting to different scales should only use fixed-precision math.
// Realistically, we are only able to show precision failures due to floating
// conversion in a few very specific circumstances where dividing by specific
// powers of 10 works against us. Some examples: 10^23, 10^25, 10^26, 10^27,
// 10^30, 10^32, 10^36. See https://godbolt.org/z/cP1MddP8P for a derivation.
// For completeness and to ensure that we are not missing any other cases, we
// test casting to/from all scales in the range of each decimal type. Values
// that are powers of ten show this error more readily than non-powers of 10
// because the rescaling factor is a power of 10, meaning that errors in
// division are more visible.
constexpr auto min_scale = -cuda::std::numeric_limits<RepType>::digits10;
for (int input_scale = 0; input_scale >= min_scale; --input_scale) {
for (int result_scale = 0; result_scale >= min_scale; --result_scale) {
RepType input_value = 1;
for (int k = 0; k > input_scale; --k) {
input_value *= 10;
}
RepType result_value = 1;
for (int k = 0; k > result_scale; --k) {
result_value *= 10;
}
auto const input = fp_wrapper{{input_value}, scale_type{input_scale}};
auto const expected = fp_wrapper{{result_value}, scale_type{result_scale}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(result_scale));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
}
}
TYPED_TEST(FixedPointTests, Decimal32ToDecimalXXWithLargerScaleAndNullMask)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = int32_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const vec = std::vector{1729, 17290, 172900, 1729000};
auto const input = fp_wrapperFrom{vec.cbegin(), vec.cend(), {1, 1, 1, 0}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1, 17, 172, 1729000}, {1, 1, 1, 0}, scale_type{0}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal64ToDecimalXXWithLargerScaleAndNullMask)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = int64_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const vec = std::vector{1729, 17290, 172900, 1729000};
auto const input = fp_wrapperFrom{vec.cbegin(), vec.cend(), {1, 1, 1, 0}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1, 17, 172, 1729000}, {1, 1, 1, 0}, scale_type{0}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, Decimal128ToDecimalXXWithLargerScaleAndNullMask)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepTypeFrom = __int128_t;
using RepTypeTo = cudf::device_storage_type_t<decimalXX>;
using fp_wrapperFrom = cudf::test::fixed_point_column_wrapper<RepTypeFrom>;
using fp_wrapperTo = cudf::test::fixed_point_column_wrapper<RepTypeTo>;
auto const vec = std::vector{1729, 17290, 172900, 1729000};
auto const input = fp_wrapperFrom{vec.cbegin(), vec.cend(), {1, 1, 1, 0}, scale_type{-3}};
auto const expected = fp_wrapperTo{{1, 17, 172, 1729000}, {1, 1, 1, 0}, scale_type{0}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TYPED_TEST(FixedPointTests, DecimalRescaleOverflowAndNullMask)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
auto const vec = std::vector{1729, 17290, 172900, 1729000};
auto const scale = cuda::std::numeric_limits<RepType>::digits10 + 1;
auto const input = fp_wrapper{vec.cbegin(), vec.cend(), {1, 0, 0, 1}, scale_type{0}};
auto const expected = fp_wrapper{{0, 0, 0, 0}, {1, 0, 0, 1}, scale_type{scale}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimalXX>(scale));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
TEST_F(FixedPointTestSingleType, Int32ToInt64Convert)
{
using namespace numeric;
using fp_wrapperA = cudf::test::fixed_point_column_wrapper<int32_t>;
using fp_wrapperB = cudf::test::fixed_point_column_wrapper<int64_t>;
auto const input = fp_wrapperB{{141230900000L}, scale_type{-10}};
auto const expected = fp_wrapperA{{14123}, scale_type{-3}};
auto const result = cudf::cast(input, make_fixed_point_data_type<decimal32>(-3));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result->view());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/unary/math_ops_test.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/detail/utilities/integer_utils.hpp>
#include <cudf/dictionary/encode.hpp>
#include <cudf/unary.hpp>
#include <cudf/utilities/bit.hpp>
#include <cudf/wrappers/timestamps.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <cuda/std/climits>
#include <vector>
template <typename T>
struct UnaryLogicalOpsTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(UnaryLogicalOpsTest, cudf::test::NumericTypes);
TYPED_TEST(UnaryLogicalOpsTest, LogicalNot)
{
cudf::size_type colSize = 1000;
std::vector<TypeParam> h_input_v(colSize, false);
std::vector<bool> h_expect_v(colSize);
std::transform(
std::cbegin(h_input_v), std::cend(h_input_v), std::begin(h_expect_v), [](TypeParam e) -> bool {
return static_cast<bool>(!e);
});
cudf::test::fixed_width_column_wrapper<TypeParam> input(std::cbegin(h_input_v),
std::cend(h_input_v));
cudf::test::fixed_width_column_wrapper<bool> expected(std::cbegin(h_expect_v),
std::cend(h_expect_v));
auto output = cudf::unary_operation(input, cudf::unary_operator::NOT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryLogicalOpsTest, SimpleLogicalNot)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{true, true, true, true}};
cudf::test::fixed_width_column_wrapper<bool> expected{{false, false, false, false}};
auto output = cudf::unary_operation(input, cudf::unary_operator::NOT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
auto encoded = cudf::dictionary::encode(input);
output = cudf::unary_operation(encoded->view(), cudf::unary_operator::NOT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryLogicalOpsTest, SimpleLogicalNotWithNullMask)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{true, true, true, true}, {1, 0, 1, 1}};
cudf::test::fixed_width_column_wrapper<bool> expected{{false, true, false, false}, {1, 0, 1, 1}};
auto output = cudf::unary_operation(input, cudf::unary_operator::NOT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
auto encoded = cudf::dictionary::encode(input);
output = cudf::unary_operation(encoded->view(), cudf::unary_operator::NOT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryLogicalOpsTest, EmptyLogicalNot)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{};
cudf::test::fixed_width_column_wrapper<bool> expected{};
auto output = cudf::unary_operation(input, cudf::unary_operator::NOT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
template <typename T>
struct UnaryMathOpsTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(UnaryMathOpsTest, cudf::test::NumericTypes);
TYPED_TEST(UnaryMathOpsTest, ABS)
{
using T = TypeParam;
cudf::size_type const colSize = 100;
std::vector<T> h_input_v(colSize);
std::vector<T> h_expect_v(colSize);
std::iota(
std::begin(h_input_v), std::end(h_input_v), std::is_unsigned_v<T> ? colSize : -1 * colSize);
std::transform(std::cbegin(h_input_v), std::cend(h_input_v), std::begin(h_expect_v), [](auto e) {
return cudf::util::absolute_value(e);
});
cudf::test::fixed_width_column_wrapper<T> const input(std::cbegin(h_input_v),
std::cend(h_input_v));
cudf::test::fixed_width_column_wrapper<T> const expected(std::cbegin(h_expect_v),
std::cend(h_expect_v));
auto const output = cudf::unary_operation(input, cudf::unary_operator::ABS);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, SQRT)
{
using T = TypeParam;
cudf::size_type const colSize = 1000;
std::vector<T> h_input_v(colSize);
std::vector<T> h_expect_v(colSize);
std::generate(std::begin(h_input_v), std::end(h_input_v), [i = 0]() mutable {
++i;
return i * i;
});
std::transform(std::cbegin(h_input_v), std::cend(h_input_v), std::begin(h_expect_v), [](auto e) {
return std::sqrt(static_cast<float>(e));
});
cudf::test::fixed_width_column_wrapper<T> const input(std::cbegin(h_input_v),
std::cend(h_input_v));
cudf::test::fixed_width_column_wrapper<T> const expected(std::cbegin(h_expect_v),
std::cend(h_expect_v));
auto const output = cudf::unary_operation(input, cudf::unary_operator::SQRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, SimpleABS)
{
auto const v = cudf::test::make_type_param_vector<TypeParam>({-2, -1, 1, 2});
cudf::test::fixed_width_column_wrapper<TypeParam> input(v.begin(), v.end());
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{2, 1, 1, 2}};
auto output = cudf::unary_operation(input, cudf::unary_operator::ABS);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, SimpleSQRT)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{1, 4, 9, 16}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{1, 2, 3, 4}};
auto output = cudf::unary_operation(input, cudf::unary_operator::SQRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, SimpleCBRT)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{1, 27, 125}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{1, 3, 5}};
auto output = cudf::unary_operation(input, cudf::unary_operator::CBRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, SimpleSQRTWithNullMask)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{1, 4, 9, 16}, {1, 1, 0, 1}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{1, 2, 9, 4}, {1, 1, 0, 1}};
auto output = cudf::unary_operation(input, cudf::unary_operator::SQRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, SimpleCBRTWithNullMask)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{1, 27, 125}, {1, 1, 0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{1, 3, 125}, {1, 1, 0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::CBRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, EmptyABS)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{};
auto output = cudf::unary_operation(input, cudf::unary_operator::ABS);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, EmptySQRT)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{};
auto output = cudf::unary_operation(input, cudf::unary_operator::SQRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathOpsTest, DictionaryABS)
{
auto const v = cudf::test::make_type_param_vector<TypeParam>({-2, -1, 1, 2, -1, 2, 0});
cudf::test::fixed_width_column_wrapper<TypeParam> input_w(v.begin(), v.end());
auto input = cudf::dictionary::encode(input_w);
cudf::test::fixed_width_column_wrapper<TypeParam> expected_w{{2, 1, 1, 2, 1, 2, 0}};
auto expected = cudf::dictionary::encode(expected_w);
auto output = cudf::unary_operation(input->view(), cudf::unary_operator::ABS);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected->view(), output->view());
}
TYPED_TEST(UnaryMathOpsTest, DictionarySQRT)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input_w{{1, 4, 0, 16}, {1, 1, 0, 1}};
auto input = cudf::dictionary::encode(input_w);
cudf::test::fixed_width_column_wrapper<TypeParam> expected_w{{1, 2, 0, 4}, {1, 1, 0, 1}};
auto expected = cudf::dictionary::encode(expected_w);
auto output = cudf::unary_operation(input->view(), cudf::unary_operator::SQRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected->view(), output->view());
}
TYPED_TEST(UnaryMathOpsTest, DictionaryCBRT)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input_w{{1, 27, 0, 125}, {1, 1, 0, 1}};
auto input = cudf::dictionary::encode(input_w);
cudf::test::fixed_width_column_wrapper<TypeParam> expected_w{{1, 3, 0, 5}, {1, 1, 0, 1}};
auto expected = cudf::dictionary::encode(expected_w);
auto output = cudf::unary_operation(input->view(), cudf::unary_operator::CBRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected->view(), output->view());
}
template <typename T>
struct UnaryMathFloatOpsTest : public cudf::test::BaseFixture {};
using floating_point_type_list = ::testing::Types<float, double>;
TYPED_TEST_SUITE(UnaryMathFloatOpsTest, floating_point_type_list);
TYPED_TEST(UnaryMathFloatOpsTest, SimpleSIN)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{0.0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{0.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::SIN);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleCOS)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{0.0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{1.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::COS);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleSINH)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{0.0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{0.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::SINH);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleCOSH)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{0.0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{1.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::COSH);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleTANH)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{0.0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{0.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::TANH);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleiASINH)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{0.0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{0.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::ARCSINH);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleACOSH)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{1.0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{0.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::ARCCOSH);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleATANH)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{0.0}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{0.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::ARCTANH);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleFLOOR)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{1.1, 3.3, 5.5, 7.7}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{1.0, 3.0, 5.0, 7.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::FLOOR);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleCEIL)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{1.1, 3.3, 5.5, 7.7}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{2.0, 4.0, 6.0, 8.0}};
auto output = cudf::unary_operation(input, cudf::unary_operator::CEIL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleRINT)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<TypeParam> input{
T(1.5), T(3.5), T(-1.5), T(-3.5), T(0.0), T(NAN)};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{
T(2.0), T(4.0), T(-2.0), T(-4.0), T(0.0), T(NAN)};
auto output = cudf::unary_operation(input, cudf::unary_operator::RINT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleEXP)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> input{T(1.5), T(3.5), T(-1.5), T(-3.5), T(0.0), T(NAN)};
cudf::test::fixed_width_column_wrapper<T> expected{T(std::exp(1.5)),
T(std::exp(3.5)),
T(std::exp(-1.5)),
T(std::exp(-3.5)),
T(std::exp(0.0)),
T(NAN)};
auto output = cudf::unary_operation(input, cudf::unary_operator::EXP);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleLOG)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> input{
T(1.5), T(3.5), T(1.0), T(INFINITY), T(0.0), T(NAN), T(-1.0)};
cudf::test::fixed_width_column_wrapper<T> expected{
T(std::log(1.5)), T(std::log(3.5)), T(+0.0), T(INFINITY), T(-INFINITY), T(NAN), T(NAN)};
auto output = cudf::unary_operation(input, cudf::unary_operator::LOG);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected, output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, DictionaryFLOOR)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input_w{{1.1, 3.3, 5.5, 7.7}};
auto input = cudf::dictionary::encode(input_w);
cudf::test::fixed_width_column_wrapper<TypeParam> expected_w{{1.0, 3.0, 5.0, 7.0}};
auto expected = cudf::dictionary::encode(expected_w);
auto output = cudf::unary_operation(input->view(), cudf::unary_operator::FLOOR);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected->view(), output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, DictionaryCEIL)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input_w{{1.1, 3.3, 5.5, 7.7}};
auto input = cudf::dictionary::encode(input_w);
cudf::test::fixed_width_column_wrapper<TypeParam> expected_w{{2.0, 4.0, 6.0, 8.0}};
auto expected = cudf::dictionary::encode(expected_w);
auto output = cudf::unary_operation(input->view(), cudf::unary_operator::CEIL);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected->view(), output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, DictionaryEXP)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> input_w{
T(1.5), T(3.5), T(-1.5), T(-3.5), T(0.0), T(NAN)};
auto input = cudf::dictionary::encode(input_w);
auto output = cudf::unary_operation(input->view(), cudf::unary_operator::EXP);
auto expect = cudf::unary_operation(input_w, cudf::unary_operator::EXP);
auto expected = cudf::dictionary::encode(expect->view());
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected->view(), output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, DictionaryLOG)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> input_w{
T(1.5), T(3.5), T(1.0), T(INFINITY), T(0.0), T(NAN), T(-1.0)};
auto input = cudf::dictionary::encode(input_w);
auto output = cudf::unary_operation(input->view(), cudf::unary_operator::LOG);
auto expect = cudf::unary_operation(input_w, cudf::unary_operator::LOG);
auto expected = cudf::dictionary::encode(expect->view());
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected->view(), output->view());
}
TYPED_TEST(UnaryMathFloatOpsTest, RINTNonFloatingFail)
{
cudf::test::fixed_width_column_wrapper<int64_t> input{{1, 2, 3, 4, 5}};
EXPECT_THROW(cudf::unary_operation(input, cudf::unary_operator::RINT), cudf::logic_error);
}
TYPED_TEST(UnaryMathFloatOpsTest, IntegralTypeFail)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{1.0};
EXPECT_THROW(cudf::unary_operation(input, cudf::unary_operator::BIT_INVERT), cudf::logic_error);
auto d = cudf::dictionary::encode(input);
EXPECT_THROW(cudf::unary_operation(d->view(), cudf::unary_operator::BIT_INVERT),
cudf::logic_error);
}
TYPED_TEST(UnaryMathFloatOpsTest, SimpleCBRT)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input{{1, 27, 343, 4913}};
cudf::test::fixed_width_column_wrapper<TypeParam> expected{{1, 3, 7, 17}};
auto output = cudf::unary_operation(input, cudf::unary_operator::CBRT);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, output->view());
}
struct UnaryMathOpsErrorTest : public cudf::test::BaseFixture {};
TEST_F(UnaryMathOpsErrorTest, ArithmeticTypeFail)
{
cudf::test::strings_column_wrapper input{"c"};
EXPECT_THROW(cudf::unary_operation(input, cudf::unary_operator::SQRT), cudf::logic_error);
auto d = cudf::dictionary::encode(input);
EXPECT_THROW(cudf::unary_operation(d->view(), cudf::unary_operator::SQRT), cudf::logic_error);
}
TEST_F(UnaryMathOpsErrorTest, LogicalOpTypeFail)
{
cudf::test::strings_column_wrapper input{"h"};
EXPECT_THROW(cudf::unary_operation(input, cudf::unary_operator::NOT), cudf::logic_error);
auto d = cudf::dictionary::encode(input);
EXPECT_THROW(cudf::unary_operation(d->view(), cudf::unary_operator::NOT), cudf::logic_error);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/quantiles/percentile_approx_test.cpp
|
/*
* Copyright (c) 2022-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/tdigest_utilities.cuh>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/tdigest/tdigest.hpp>
#include <cudf/groupby.hpp>
#include <cudf/quantiles.hpp>
#include <cudf/reduction.hpp>
#include <cudf/sorting.hpp>
#include <cudf/tdigest/tdigest_column_view.hpp>
#include <cudf/transform.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/error.hpp>
#include <arrow/util/tdigest.h>
std::unique_ptr<cudf::column> arrow_percentile_approx(cudf::column_view const& _values,
int delta,
std::vector<double> const& percentages)
{
// sort the incoming values using the same settings that groupby does.
// this is a little weak because null_order::AFTER is hardcoded internally to groupby.
cudf::table_view t({_values});
auto sorted_t = cudf::sort(t, {}, {cudf::null_order::AFTER});
auto sorted_values = sorted_t->get_column(0).view();
std::vector<double> h_values(sorted_values.size());
CUDF_CUDA_TRY(cudaMemcpy(h_values.data(),
sorted_values.data<double>(),
sizeof(double) * sorted_values.size(),
cudaMemcpyDefault));
std::vector<char> h_validity(sorted_values.size());
if (sorted_values.null_mask() != nullptr) {
auto validity = cudf::mask_to_bools(sorted_values.null_mask(), 0, sorted_values.size());
CUDF_CUDA_TRY(cudaMemcpy(h_validity.data(),
(validity->view().data<char>()),
sizeof(char) * sorted_values.size(),
cudaMemcpyDefault));
}
// generate the tdigest
arrow::internal::TDigest atd(delta, sorted_values.size() * 2);
for (size_t idx = 0; idx < h_values.size(); idx++) {
if (sorted_values.null_mask() == nullptr || h_validity[idx]) { atd.Add(h_values[idx]); }
}
// generate the percentiles and stuff them into a list column
std::vector<double> h_result;
h_result.reserve(percentages.size());
std::transform(
percentages.begin(), percentages.end(), std::back_inserter(h_result), [&atd](double p) {
return atd.Quantile(p);
});
cudf::test::fixed_width_column_wrapper<double> result(h_result.begin(), h_result.end());
cudf::test::fixed_width_column_wrapper<cudf::size_type> offsets{
0, static_cast<cudf::size_type>(percentages.size())};
return cudf::make_lists_column(1, offsets.release(), result.release(), 0, {});
}
struct percentile_approx_dispatch {
template <
typename T,
typename Func,
typename std::enable_if_t<cudf::is_numeric<T>() || cudf::is_fixed_point<T>()>* = nullptr>
std::unique_ptr<cudf::column> operator()(Func op,
cudf::column_view const& values,
int delta,
std::vector<double> const& percentages,
cudf::size_type ulps)
{
// arrow implementation.
auto expected = [&]() {
// we're explicitly casting back to doubles here but this is ok because that is
// exactly what happens inside of the cudf implementation as values are processed as well. so
// this should not affect results.
auto as_doubles = cudf::cast(values, cudf::data_type{cudf::type_id::FLOAT64});
return arrow_percentile_approx(*as_doubles, delta, percentages);
}();
// gpu implementation
auto agg_result = op(values, delta);
cudf::test::fixed_width_column_wrapper<double> g_percentages(percentages.begin(),
percentages.end());
cudf::tdigest::tdigest_column_view tdv(*agg_result);
auto result = cudf::percentile_approx(tdv, g_percentages);
cudf::test::detail::expect_columns_equivalent(
*expected, *result, cudf::test::debug_output_level::FIRST_ERROR, ulps);
return result;
}
template <
typename T,
typename Func,
typename std::enable_if_t<!cudf::is_numeric<T>() && !cudf::is_fixed_point<T>()>* = nullptr>
std::unique_ptr<cudf::column> operator()(Func op,
cudf::column_view const& values,
int delta,
std::vector<double> const& percentages,
cudf::size_type ulps)
{
CUDF_FAIL("Invalid input type for percentile_approx test");
}
};
void percentile_approx_test(cudf::column_view const& _keys,
cudf::column_view const& _values,
int delta,
std::vector<double> const& percentages,
cudf::size_type ulps)
{
// first pass: validate the actual percentages we get per group.
// produce the groups.
cudf::table_view k({_keys});
cudf::groupby::groupby pass1_gb(k);
cudf::table_view v({_values});
auto groups = pass1_gb.get_groups(v);
// slice it all up so we have keys/columns for everything.
std::vector<cudf::column_view> keys;
std::vector<cudf::column_view> values;
for (size_t idx = 0; idx < groups.offsets.size() - 1; idx++) {
auto k =
cudf::slice(groups.keys->get_column(0), {groups.offsets[idx], groups.offsets[idx + 1]});
keys.push_back(k[0]);
auto v =
cudf::slice(groups.values->get_column(0), {groups.offsets[idx], groups.offsets[idx + 1]});
values.push_back(v[0]);
}
std::vector<std::unique_ptr<cudf::column>> groupby_parts;
std::vector<std::unique_ptr<cudf::column>> reduce_parts;
for (size_t idx = 0; idx < values.size(); idx++) {
// via groupby
auto groupby = [&](cudf::column_view const& values, int delta) {
cudf::table_view t({keys[idx]});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({values, std::move(aggregations)});
return std::move(gb.aggregate(requests).second[0].results[0]);
};
groupby_parts.push_back(cudf::type_dispatcher(values[idx].type(),
percentile_approx_dispatch{},
groupby,
values[idx],
delta,
percentages,
ulps));
// via reduce
auto reduce = [](cudf::column_view const& values, int delta) {
// result is a scalar, but we want to extract out the underlying column
auto scalar_result =
cudf::reduce(values,
*cudf::make_tdigest_aggregation<cudf::reduce_aggregation>(delta),
cudf::data_type{cudf::type_id::STRUCT});
auto tbl = static_cast<cudf::struct_scalar const*>(scalar_result.get())->view();
std::vector<std::unique_ptr<cudf::column>> cols;
std::transform(
tbl.begin(), tbl.end(), std::back_inserter(cols), [](cudf::column_view const& col) {
return std::make_unique<cudf::column>(col);
});
return cudf::make_structs_column(tbl.num_rows(), std::move(cols), 0, rmm::device_buffer());
};
// groupby path
reduce_parts.push_back(cudf::type_dispatcher(values[idx].type(),
percentile_approx_dispatch{},
reduce,
values[idx],
delta,
percentages,
ulps));
}
// second pass. run the percentile_approx with all the keys in one pass and make sure we get the
// same results as the concatenated by-key results.
std::vector<cudf::column_view> part_views;
std::transform(groupby_parts.begin(),
groupby_parts.end(),
std::back_inserter(part_views),
[](std::unique_ptr<cudf::column> const& c) { return c->view(); });
auto expected = cudf::concatenate(part_views);
cudf::groupby::groupby gb(k);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({_values, std::move(aggregations)});
auto gb_result = gb.aggregate(requests);
cudf::test::fixed_width_column_wrapper<double> g_percentages(percentages.begin(),
percentages.end());
cudf::tdigest::tdigest_column_view tdv(*(gb_result.second[0].results[0]));
auto result = cudf::percentile_approx(tdv, g_percentages);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*expected, *result);
}
void simple_test(cudf::data_type input_type, std::vector<std::pair<int, int>> params)
{
auto values = cudf::test::generate_standardized_percentile_distribution(input_type);
// all in the same group
auto keys = cudf::make_fixed_width_column(
cudf::data_type{cudf::type_id::INT32}, values->size(), cudf::mask_state::UNALLOCATED);
CUDF_CUDA_TRY(
cudaMemset(keys->mutable_view().data<int32_t>(), 0, values->size() * sizeof(int32_t)));
// runs both groupby and reduce paths
std::for_each(params.begin(), params.end(), [&](std::pair<int, int> const& params) {
percentile_approx_test(
*keys, *values, params.first, {0.0, 0.05, 0.25, 0.5, 0.75, 0.95, 1.0}, params.second);
});
}
struct group_index {
int32_t operator()(int32_t i) { return i / 150000; }
};
void grouped_test(cudf::data_type input_type, std::vector<std::pair<int, int>> params)
{
auto values = cudf::test::generate_standardized_percentile_distribution(input_type);
// all in the same group
auto keys = cudf::make_fixed_width_column(
cudf::data_type{cudf::type_id::INT32}, values->size(), cudf::mask_state::UNALLOCATED);
auto i = thrust::make_counting_iterator(0);
auto h_keys = std::vector<int32_t>(values->size());
std::transform(i, i + values->size(), h_keys.begin(), group_index{});
CUDF_CUDA_TRY(cudaMemcpy(keys->mutable_view().data<int32_t>(),
h_keys.data(),
h_keys.size() * sizeof(int32_t),
cudaMemcpyDefault));
std::for_each(params.begin(), params.end(), [&](std::pair<int, int> const& params) {
percentile_approx_test(
*keys, *values, params.first, {0.0, 0.05, 0.25, 0.5, 0.75, 0.95, 1.0}, params.second);
});
}
std::pair<rmm::device_buffer, cudf::size_type> make_null_mask(cudf::column_view const& col)
{
auto itr = cudf::detail::make_counting_transform_iterator(0, [](auto i) { return i % 2 == 0; });
return cudf::test::detail::make_null_mask(itr, itr + col.size());
}
void simple_with_nulls_test(cudf::data_type input_type, std::vector<std::pair<int, int>> params)
{
auto values = cudf::test::generate_standardized_percentile_distribution(input_type);
// all in the same group
auto keys = cudf::make_fixed_width_column(
cudf::data_type{cudf::type_id::INT32}, values->size(), cudf::mask_state::UNALLOCATED);
CUDF_CUDA_TRY(
cudaMemset(keys->mutable_view().data<int32_t>(), 0, values->size() * sizeof(int32_t)));
// add a null mask
auto mask = make_null_mask(*values);
values->set_null_mask(mask.first, mask.second);
std::for_each(params.begin(), params.end(), [&](std::pair<int, int> const& params) {
percentile_approx_test(
*keys, *values, params.first, {0.0, 0.05, 0.25, 0.5, 0.75, 0.95, 1.0}, params.second);
});
}
void grouped_with_nulls_test(cudf::data_type input_type, std::vector<std::pair<int, int>> params)
{
auto values = cudf::test::generate_standardized_percentile_distribution(input_type);
// all in the same group
auto keys = cudf::make_fixed_width_column(
cudf::data_type{cudf::type_id::INT32}, values->size(), cudf::mask_state::UNALLOCATED);
auto i = thrust::make_counting_iterator(0);
auto h_keys = std::vector<int32_t>(values->size());
std::transform(i, i + values->size(), h_keys.begin(), group_index{});
CUDF_CUDA_TRY(cudaMemcpy(keys->mutable_view().data<int32_t>(),
h_keys.data(),
h_keys.size() * sizeof(int32_t),
cudaMemcpyDefault));
// add a null mask
auto mask = make_null_mask(*values);
values->set_null_mask(mask.first, mask.second);
std::for_each(params.begin(), params.end(), [&](std::pair<int, int> const& params) {
percentile_approx_test(
*keys, *values, params.first, {0.0, 0.05, 0.25, 0.5, 0.75, 0.95, 1.0}, params.second);
});
}
template <typename T>
cudf::data_type get_appropriate_type()
{
if constexpr (cudf::is_fixed_point<T>()) { return cudf::data_type{cudf::type_to_id<T>(), -7}; }
return cudf::data_type{cudf::type_to_id<T>()};
}
using PercentileApproxTypes =
cudf::test::Concat<cudf::test::NumericTypes, cudf::test::FixedPointTypes>;
template <typename T>
struct PercentileApproxInputTypesTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(PercentileApproxInputTypesTest, PercentileApproxTypes);
TYPED_TEST(PercentileApproxInputTypesTest, Simple)
{
using T = TypeParam;
auto const input_type = get_appropriate_type<T>();
simple_test(input_type,
{{1000, cudf::test::default_ulp},
{100, cudf::test::default_ulp * 4},
{10, cudf::test::default_ulp * 11}});
}
TYPED_TEST(PercentileApproxInputTypesTest, Grouped)
{
using T = TypeParam;
auto const input_type = get_appropriate_type<T>();
grouped_test(input_type,
{{1000, cudf::test::default_ulp},
{100, cudf::test::default_ulp * 2},
{10, cudf::test::default_ulp * 10}});
}
TYPED_TEST(PercentileApproxInputTypesTest, SimpleWithNulls)
{
using T = TypeParam;
auto const input_type = get_appropriate_type<T>();
simple_with_nulls_test(input_type,
{{1000, cudf::test::default_ulp},
{100, cudf::test::default_ulp * 2},
{10, cudf::test::default_ulp * 11}});
}
TYPED_TEST(PercentileApproxInputTypesTest, GroupedWithNulls)
{
using T = TypeParam;
auto const input_type = get_appropriate_type<T>();
grouped_with_nulls_test(input_type,
{{1000, cudf::test::default_ulp},
{100, cudf::test::default_ulp * 2},
{10, cudf::test::default_ulp * 6}});
}
struct PercentileApproxTest : public cudf::test::BaseFixture {};
TEST_F(PercentileApproxTest, EmptyInput)
{
auto empty_ = cudf::tdigest::detail::make_empty_tdigest_column(
cudf::get_default_stream(), rmm::mr::get_current_device_resource());
cudf::test::fixed_width_column_wrapper<double> percentiles{0.0, 0.25, 0.3};
std::vector<cudf::column_view> input;
input.push_back(*empty_);
input.push_back(*empty_);
input.push_back(*empty_);
auto empty = cudf::concatenate(input);
cudf::tdigest::tdigest_column_view tdv(*empty);
auto result = cudf::percentile_approx(tdv, percentiles);
cudf::test::fixed_width_column_wrapper<cudf::size_type> offsets{0, 0, 0, 0};
std::vector<bool> nulls{0, 0, 0};
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(nulls.begin(), nulls.end());
auto expected = cudf::make_lists_column(3,
offsets.release(),
cudf::make_empty_column(cudf::type_id::FLOAT64),
null_count,
std::move(null_mask));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
TEST_F(PercentileApproxTest, EmptyPercentiles)
{
auto const delta = 1000;
cudf::test::fixed_width_column_wrapper<double> values{0, 1, 2, 3, 4, 5};
cudf::test::fixed_width_column_wrapper<int> keys{0, 0, 0, 1, 1, 1};
cudf::table_view t({keys});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({values, std::move(aggregations)});
auto tdigest_column = gb.aggregate(requests);
cudf::test::fixed_width_column_wrapper<double> percentiles{};
cudf::tdigest::tdigest_column_view tdv(*tdigest_column.second[0].results[0]);
auto result = cudf::percentile_approx(tdv, percentiles);
cudf::test::fixed_width_column_wrapper<cudf::size_type> offsets{0, 0, 0};
std::vector<bool> nulls{0, 0};
auto [null_mask, null_count] = cudf::test::detail::make_null_mask(nulls.begin(), nulls.end());
auto expected = cudf::make_lists_column(2,
offsets.release(),
cudf::make_empty_column(cudf::type_id::FLOAT64),
null_count,
std::move(null_mask));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, *expected);
}
TEST_F(PercentileApproxTest, NullPercentiles)
{
auto const delta = 1000;
cudf::test::fixed_width_column_wrapper<double> values{1, 1, 2, 3, 4, 5, 6, 7, 8};
cudf::test::fixed_width_column_wrapper<int> keys{0, 0, 0, 0, 0, 1, 1, 1, 1};
cudf::table_view t({keys});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({values, std::move(aggregations)});
auto tdigest_column = gb.aggregate(requests);
cudf::tdigest::tdigest_column_view tdv(*tdigest_column.second[0].results[0]);
cudf::test::fixed_width_column_wrapper<double> npercentiles{{0.5, 0.5, 1.0, 1.0}, {0, 0, 1, 1}};
auto result = cudf::percentile_approx(tdv, npercentiles);
std::vector<bool> valids{0, 0, 1, 1};
cudf::test::lists_column_wrapper<double> expected{{{99, 99, 4, 4}, valids.begin()},
{{99, 99, 8, 8}, valids.begin()}};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result, expected);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/quantiles/quantiles_test.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/quantiles.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
template <typename T>
struct QuantilesTest : public cudf::test::BaseFixture {};
using TestTypes = cudf::test::AllTypes;
TYPED_TEST_SUITE(QuantilesTest, TestTypes);
TYPED_TEST(QuantilesTest, TestZeroColumns)
{
auto input = cudf::table_view(std::vector<cudf::column_view>{});
EXPECT_THROW(cudf::quantiles(input, {0.0f}), cudf::logic_error);
}
TYPED_TEST(QuantilesTest, TestMultiColumnZeroRows)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> input_a({});
auto input = cudf::table_view({input_a});
EXPECT_THROW(cudf::quantiles(input, {0.0f}), cudf::logic_error);
}
TYPED_TEST(QuantilesTest, TestZeroRequestedQuantiles)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T, int32_t> input_a({1}, {1});
auto input = cudf::table_view(std::vector<cudf::column_view>{input_a});
auto actual = cudf::quantiles(input, {});
auto expected = cudf::empty_like(input);
CUDF_TEST_EXPECT_TABLES_EQUAL(expected->view(), actual->view());
}
TYPED_TEST(QuantilesTest, TestMultiColumnOrderCountMismatch)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> input_a({});
cudf::test::fixed_width_column_wrapper<T> input_b({});
auto input = cudf::table_view({input_a});
EXPECT_THROW(cudf::quantiles(input,
{0.0f},
cudf::interpolation::NEAREST,
cudf::sorted::NO,
{cudf::order::ASCENDING},
{cudf::null_order::AFTER, cudf::null_order::AFTER}),
cudf::logic_error);
}
TYPED_TEST(QuantilesTest, TestMultiColumnNullOrderCountMismatch)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> input_a({});
cudf::test::fixed_width_column_wrapper<T> input_b({});
auto input = cudf::table_view({input_a});
EXPECT_THROW(cudf::quantiles(input,
{0.0f},
cudf::interpolation::NEAREST,
cudf::sorted::NO,
{cudf::order::ASCENDING, cudf::order::ASCENDING},
{cudf::null_order::AFTER}),
cudf::logic_error);
}
TYPED_TEST(QuantilesTest, TestMultiColumnArithmeticInterpolation)
{
using T = TypeParam;
cudf::test::fixed_width_column_wrapper<T> input_a({});
cudf::test::fixed_width_column_wrapper<T> input_b({});
auto input = cudf::table_view({input_a});
EXPECT_THROW(cudf::quantiles(input, {0.0f}, cudf::interpolation::LINEAR), cudf::logic_error);
EXPECT_THROW(cudf::quantiles(input, {0.0f}, cudf::interpolation::MIDPOINT), cudf::logic_error);
}
TYPED_TEST(QuantilesTest, TestMultiColumnUnsorted)
{
using T = TypeParam;
auto input_a = cudf::test::strings_column_wrapper(
{"C", "B", "A", "A", "D", "B", "D", "B", "D", "C", "C", "C",
"D", "B", "D", "B", "C", "C", "A", "D", "B", "A", "A", "A"},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<T, int32_t> input_b(
{4, 3, 5, 0, 1, 0, 4, 1, 5, 3, 0, 5, 2, 4, 3, 2, 1, 2, 3, 0, 5, 1, 4, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
auto input = cudf::table_view({input_a, input_b});
auto actual = cudf::quantiles(input,
{0.0f, 0.5f, 0.7f, 0.25f, 1.0f},
cudf::interpolation::NEAREST,
cudf::sorted::NO,
{cudf::order::ASCENDING, cudf::order::DESCENDING});
auto expected_a = cudf::test::strings_column_wrapper({"A", "C", "C", "B", "D"}, {1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<T, int32_t> expected_b({5, 5, 1, 5, 0}, {1, 1, 1, 1, 1});
auto expected = cudf::table_view({expected_a, expected_b});
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, actual->view());
}
TYPED_TEST(QuantilesTest, TestMultiColumnAssumedSorted)
{
using T = TypeParam;
auto input_a = cudf::test::strings_column_wrapper(
{"C", "B", "A", "A", "D", "B", "D", "B", "D", "C", "C", "C",
"D", "B", "D", "B", "C", "C", "A", "D", "B", "A", "A", "A"},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<T, int32_t> input_b(
{4, 3, 5, 0, 1, 0, 4, 1, 5, 3, 0, 5, 2, 4, 3, 2, 1, 2, 3, 0, 5, 1, 4, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
auto input = cudf::table_view({input_a, input_b});
auto actual = cudf::quantiles(
input, {0.0f, 0.5f, 0.7f, 0.25f, 1.0f}, cudf::interpolation::NEAREST, cudf::sorted::YES);
auto expected_a = cudf::test::strings_column_wrapper({"C", "D", "C", "D", "A"}, {1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<T, int32_t> expected_b({4, 2, 1, 4, 2}, {1, 1, 1, 1, 1});
auto expected = cudf::table_view({expected_a, expected_b});
CUDF_TEST_EXPECT_TABLES_EQUAL(expected, actual->view());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/quantiles/quantile_test.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/quantiles.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <limits>
#include <memory>
#include <type_traits>
#include <vector>
namespace {
struct q_res {
q_res(double value, bool is_valid = true) : is_valid(is_valid), value(value) {}
bool is_valid;
double value;
};
// ----- test data -------------------------------------------------------------
namespace testdata {
struct q_expect {
q_expect(double quantile)
: quantile(quantile),
higher(0, false),
lower(0, false),
linear(0, false),
midpoint(0, false),
nearest(0, false)
{
}
q_expect(
double quantile, double higher, double lower, double linear, double midpoint, double nearest)
: quantile(quantile),
higher(higher),
lower(lower),
linear(linear),
midpoint(midpoint),
nearest(nearest)
{
}
double quantile;
q_res higher;
q_res lower;
q_res linear;
q_res midpoint;
q_res nearest;
};
template <typename T>
struct test_case {
cudf::test::fixed_width_column_wrapper<T> column;
std::vector<q_expect> expectations;
cudf::test::fixed_width_column_wrapper<cudf::size_type> ordered_indices;
};
// interpolate_center
template <typename T>
test_case<T> interpolate_center()
{
auto low = std::numeric_limits<T>::lowest();
auto max = std::numeric_limits<T>::max();
double mid_d = [] {
if (std::is_floating_point_v<T>) return 0.0;
if (std::is_signed_v<T>) return -0.5;
return static_cast<double>(std::numeric_limits<T>::max()) / 2.0;
}();
// int64_t is internally casted to a double, meaning the lerp center point
// is float-like.
double lin_d = [] {
if (std::is_floating_point_v<T> || std::is_same_v<T, int64_t>) return 0.0;
if (std::is_signed_v<T>) return -0.5;
return static_cast<double>(std::numeric_limits<T>::max()) / 2.0;
}();
auto max_d = static_cast<double>(max);
auto low_d = static_cast<double>(low);
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({low, max}),
{q_expect{0.50, max_d, low_d, lin_d, mid_d, low_d}}};
}
template <>
test_case<bool> interpolate_center()
{
auto low = std::numeric_limits<bool>::lowest();
auto max = std::numeric_limits<bool>::max();
auto mid_d = 0.5;
auto low_d = static_cast<double>(low);
auto max_d = static_cast<double>(max);
return test_case<bool>{cudf::test::fixed_width_column_wrapper<bool>({low, max}),
{q_expect{0.5, max_d, low_d, mid_d, mid_d, low_d}}};
}
// interpolate_extrema_high
template <typename T>
test_case<T> interpolate_extrema_high()
{
T max = std::numeric_limits<T>::max();
T low = max - 2;
auto low_d = static_cast<double>(low);
auto max_d = static_cast<double>(max);
auto exact_d = static_cast<double>(max - 1);
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({low, max}),
{q_expect{0.50, max_d, low_d, exact_d, exact_d, low_d}}};
}
template <>
test_case<bool> interpolate_extrema_high<bool>()
{
return interpolate_center<bool>();
}
// interpolate_extrema_low
template <typename T>
test_case<T> interpolate_extrema_low()
{
T lowest = std::numeric_limits<T>::lowest();
T a = lowest;
T b = lowest + 2;
auto a_d = static_cast<double>(a);
auto b_d = static_cast<double>(b);
auto exact_d = static_cast<double>(a + 1);
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({a, b}),
{q_expect{0.50, b_d, a_d, exact_d, exact_d, a_d}}};
}
template <>
test_case<bool> interpolate_extrema_low<bool>()
{
return interpolate_center<bool>();
}
// single
template <typename T>
std::enable_if_t<std::is_floating_point_v<T>, test_case<T>> single()
{
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({7.309999942779541}),
{
q_expect{
-1.0,
7.309999942779541,
7.309999942779541,
7.309999942779541,
7.309999942779541,
7.309999942779541,
},
q_expect{
0.0,
7.309999942779541,
7.309999942779541,
7.309999942779541,
7.309999942779541,
7.309999942779541,
},
q_expect{
1.0,
7.309999942779541,
7.309999942779541,
7.309999942779541,
7.309999942779541,
7.309999942779541,
},
}};
}
template <typename T>
std::enable_if_t<std::is_integral_v<T> and not cudf::is_boolean<T>(), test_case<T>> single()
{
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({1}),
{q_expect{0.7, 1, 1, 1, 1, 1}}};
}
template <typename T>
std::enable_if_t<cudf::is_boolean<T>(), test_case<T>> single()
{
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({1}),
{q_expect{0.7, 1.0, 1.0, 1.0, 1.0, 1.0}}};
}
// all_invalid
template <typename T>
std::enable_if_t<std::is_floating_point_v<T>, test_case<T>> all_invalid()
{
return test_case<T>{
cudf::test::fixed_width_column_wrapper<T>({6.8, 0.15, 3.4, 4.17, 2.13, 1.11, -1.01, 0.8, 5.7},
{0, 0, 0, 0, 0, 0, 0, 0, 0}),
{q_expect{-1.0}, q_expect{0.0}, q_expect{0.5}, q_expect{1.0}, q_expect{2.0}}};
}
template <typename T>
std::enable_if_t<std::is_integral_v<T> and not cudf::is_boolean<T>(), test_case<T>> all_invalid()
{
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({6, 0, 3, 4, 2, 1, -1, 1, 6},
{0, 0, 0, 0, 0, 0, 0, 0, 0}),
{q_expect{0.7}}};
}
template <typename T>
std::enable_if_t<cudf::is_boolean<T>(), test_case<T>> all_invalid()
{
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({1, 0, 1, 1, 0, 1, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 0}),
{q_expect{0.7}}};
}
// some invalid
template <typename T>
std::enable_if_t<std::is_same_v<T, double>, test_case<T>> some_invalid()
{
T high = 0.16;
T low = -1.024;
T mid = -0.432;
T lin = -0.432;
return test_case<T>{
cudf::test::fixed_width_column_wrapper<T>({6.8, high, 3.4, 4.17, 2.13, 1.11, low, 0.8, 5.7},
{0, 1, 0, 0, 0, 0, 1, 0, 0}),
{q_expect{-1.0, low, low, low, low, low},
q_expect{0.0, low, low, low, low, low},
q_expect{0.5, high, low, lin, mid, low},
q_expect{1.0, high, high, high, high, high},
q_expect{2.0, high, high, high, high, high}},
cudf::test::fixed_width_column_wrapper<cudf::size_type>({6, 1})};
}
template <typename T>
std::enable_if_t<std::is_same_v<T, float>, test_case<T>> some_invalid()
{
T high = 0.16;
T low = -1.024;
double mid = -0.43200002610683441;
double lin = -0.43200002610683441;
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>(
{T(6.8), high, T(3.4), T(4.17), T(2.13), T(1.11), low, T(0.8), T(5.7)},
{0, 1, 0, 0, 0, 0, 1, 0, 0}),
{q_expect{-1.0, low, low, low, low, low},
q_expect{0.0, low, low, low, low, low},
q_expect{0.5, high, low, lin, mid, low},
q_expect{1.0, high, high, high, high, high},
q_expect{2.0, high, high, high, high, high}},
cudf::test::fixed_width_column_wrapper<cudf::size_type>({6, 1})};
}
template <typename T>
std::enable_if_t<std::is_integral_v<T> and not cudf::is_boolean<T>(), test_case<T>> some_invalid()
{
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({6, 0, 3, 4, 2, 1, -1, 1, 6},
{0, 0, 1, 0, 0, 0, 0, 0, 1}),
{q_expect{0.0, 3.0, 3.0, 3.0, 3.0, 3.0},
q_expect{0.5, 6.0, 3.0, 4.5, 4.5, 3.0},
q_expect{1.0, 6.0, 6.0, 6.0, 6.0, 6.0}},
cudf::test::fixed_width_column_wrapper<cudf::size_type>({2, 8})};
}
template <typename T>
std::enable_if_t<cudf::is_boolean<T>(), test_case<T>> some_invalid()
{
return test_case<T>{cudf::test::fixed_width_column_wrapper<T>({1, 0, 1, 1, 0, 1, 0, 1, 1},
{0, 0, 1, 0, 1, 0, 0, 0, 0}),
{q_expect{0.0, 0.0, 0.0, 0.0, 0.0, 0.0},
q_expect{0.5, 1.0, 0.0, 0.5, 0.5, 0.0},
q_expect{1.0, 1.0, 1.0, 1.0, 1.0, 1.0}},
cudf::test::fixed_width_column_wrapper<cudf::size_type>({4, 2})};
}
// unsorted
template <typename T>
std::enable_if_t<std::is_floating_point_v<T>, test_case<T>> unsorted()
{
return test_case<T>{
cudf::test::fixed_width_column_wrapper<T>({6.8, 0.15, 3.4, 4.17, 2.13, 1.11, -1.00, 0.8, 5.7}),
{
q_expect{0.0, -1.00, -1.00, -1.00, -1.00, -1.00},
},
cudf::test::fixed_width_column_wrapper<cudf::size_type>({6, 1, 7, 5, 4, 2, 3, 8, 0})};
}
template <typename T>
std::enable_if_t<std::is_integral_v<T> and not cudf::is_boolean<T>(), test_case<T>> unsorted()
{
return std::is_signed<T>()
? test_case<T>{cudf::test::fixed_width_column_wrapper<T>({6, 0, 3, 4, 2, 1, -1, 1, 6}),
{q_expect{0.0, -1, -1, -1, -1, -1}},
cudf::test::fixed_width_column_wrapper<cudf::size_type>(
{6, 1, 7, 5, 4, 2, 3, 8, 0})}
: test_case<T>{cudf::test::fixed_width_column_wrapper<T>({6, 0, 3, 4, 2, 1, 1, 1, 6}),
{q_expect{0.0, 1, 1, 1, 1, 1}},
cudf::test::fixed_width_column_wrapper<cudf::size_type>(
{6, 1, 7, 5, 4, 2, 3, 8, 0})};
}
template <typename T>
std::enable_if_t<cudf::is_boolean<T>(), test_case<T>> unsorted()
{
return test_case<T>{
cudf::test::fixed_width_column_wrapper<T>({0, 0, 1, 1, 0, 1, 1, 0, 1}),
{q_expect{
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
}},
cudf::test::fixed_width_column_wrapper<cudf::size_type>({0, 1, 4, 7, 2, 3, 5, 6, 9})};
}
} // namespace testdata
// =============================================================================
// ----- helper functions ------------------------------------------------------
template <typename T>
void test(testdata::test_case<T> test_case)
{
for (auto& expected : test_case.expectations) {
auto q = std::vector<double>{expected.quantile};
auto nullable = static_cast<cudf::column_view>(test_case.column).nullable();
auto make_expected_column = [nullable](q_res expected) {
return nullable ? cudf::test::fixed_width_column_wrapper<double>({expected.value},
{expected.is_valid})
: cudf::test::fixed_width_column_wrapper<double>({expected.value});
};
auto actual_higher =
cudf::quantile(test_case.column, q, cudf::interpolation::HIGHER, test_case.ordered_indices);
auto expected_higher_col = make_expected_column(expected.higher);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_higher_col, actual_higher->view());
auto actual_lower =
cudf::quantile(test_case.column, q, cudf::interpolation::LOWER, test_case.ordered_indices);
auto expected_lower_col = make_expected_column(expected.lower);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lower_col, actual_lower->view());
auto actual_linear =
cudf::quantile(test_case.column, q, cudf::interpolation::LINEAR, test_case.ordered_indices);
auto expected_linear_col = make_expected_column(expected.linear);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_linear_col, actual_linear->view());
auto actual_midpoint =
cudf::quantile(test_case.column, q, cudf::interpolation::MIDPOINT, test_case.ordered_indices);
auto expected_midpoint_col = make_expected_column(expected.midpoint);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_midpoint_col, actual_midpoint->view());
auto actual_nearest =
cudf::quantile(test_case.column, q, cudf::interpolation::NEAREST, test_case.ordered_indices);
auto expected_nearest_col = make_expected_column(expected.nearest);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_nearest_col, actual_nearest->view());
}
}
// =============================================================================
// ----- tests -----------------------------------------------------------------
template <typename T>
struct QuantileTest : public cudf::test::BaseFixture {};
using TestTypes = cudf::test::NumericTypes;
TYPED_TEST_SUITE(QuantileTest, TestTypes);
TYPED_TEST(QuantileTest, TestSingle) { test(testdata::single<TypeParam>()); }
TYPED_TEST(QuantileTest, TestSomeElementsInvalid) { test(testdata::some_invalid<TypeParam>()); }
TYPED_TEST(QuantileTest, TestAllElementsInvalid) { test(testdata::all_invalid<TypeParam>()); }
TYPED_TEST(QuantileTest, TestUnsorted) { test(testdata::unsorted<TypeParam>()); }
TYPED_TEST(QuantileTest, TestInterpolateCenter) { test(testdata::interpolate_center<TypeParam>()); }
TYPED_TEST(QuantileTest, TestInterpolateExtremaHigh)
{
test(testdata::interpolate_extrema_high<TypeParam>());
}
TYPED_TEST(QuantileTest, TestInterpolateExtremaLow)
{
test(testdata::interpolate_extrema_low<TypeParam>());
}
TYPED_TEST(QuantileTest, TestEmpty)
{
auto input = cudf::test::fixed_width_column_wrapper<TypeParam>({});
auto expected = cudf::test::fixed_width_column_wrapper<double>({0, 0}, {0, 0});
auto actual = cudf::quantile(input, {0.5, 0.25});
}
template <typename T>
struct QuantileUnsupportedTypesTest : public cudf::test::BaseFixture {};
// TODO add tests for FixedPointTypes
using UnsupportedTestTypes = cudf::test::RemoveIf<
cudf::test::ContainedIn<cudf::test::Concat<TestTypes, cudf::test::FixedPointTypes>>,
cudf::test::AllTypes>;
TYPED_TEST_SUITE(QuantileUnsupportedTypesTest, UnsupportedTestTypes);
TYPED_TEST(QuantileUnsupportedTypesTest, TestZeroElements)
{
cudf::test::fixed_width_column_wrapper<TypeParam> input({});
EXPECT_THROW(cudf::quantile(input, {0}), cudf::logic_error);
}
TYPED_TEST(QuantileUnsupportedTypesTest, TestOneElements)
{
cudf::test::fixed_width_column_wrapper<TypeParam, int32_t> input({0});
EXPECT_THROW(cudf::quantile(input, {0}), cudf::logic_error);
}
TYPED_TEST(QuantileUnsupportedTypesTest, TestMultipleElements)
{
cudf::test::fixed_width_column_wrapper<TypeParam, int32_t> input({0, 1, 2});
EXPECT_THROW(cudf::quantile(input, {0}), cudf::logic_error);
}
struct QuantileDictionaryTest : public cudf::test::BaseFixture {};
TEST_F(QuantileDictionaryTest, TestValid)
{
cudf::test::dictionary_column_wrapper<int32_t> col{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cudf::test::fixed_width_column_wrapper<int32_t> indices{0, 2, 4, 6, 8, 1, 3, 5, 7, 9};
auto result = cudf::quantile(col, {0.5}, cudf::interpolation::LINEAR);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(result->view(),
cudf::test::fixed_width_column_wrapper<double>{5.5});
result = cudf::quantile(col, {0.5}, cudf::interpolation::LINEAR, indices);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(result->view(),
cudf::test::fixed_width_column_wrapper<double>{5.5});
result = cudf::quantile(col, {0.1, 0.2}, cudf::interpolation::HIGHER);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(result->view(),
cudf::test::fixed_width_column_wrapper<double>{2.0, 3.0});
result = cudf::quantile(col, {0.25, 0.5, 0.75}, cudf::interpolation::MIDPOINT);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(
result->view(), cudf::test::fixed_width_column_wrapper<double>{3.5, 5.5, 7.5});
};
} // anonymous namespace
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/types/traits_test.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/utilities/traits.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/type_lists.hpp>
#include <gtest/gtest.h>
#include <algorithm>
#include <tuple>
template <typename Tuple, typename F, std::size_t... Indices>
void tuple_for_each_impl(Tuple&& tuple, F&& f, std::index_sequence<Indices...>)
{
(void)std::initializer_list<int>{
((void)(f(std::get<Indices>(std::forward<Tuple>(tuple)))), int{})...};
}
template <typename F, typename... Args>
void tuple_for_each(std::tuple<Args...> const& tuple, F&& f)
{
tuple_for_each_impl(tuple, std::forward<F>(f), std::index_sequence_for<Args...>{});
}
class TraitsTest : public ::testing::Test {};
template <typename T>
class TypedTraitsTest : public TraitsTest {};
TYPED_TEST_SUITE(TypedTraitsTest, cudf::test::AllTypes);
TEST_F(TraitsTest, NumericDataTypesAreNumeric)
{
EXPECT_TRUE(
std::all_of(cudf::test::numeric_type_ids.begin(),
cudf::test::numeric_type_ids.end(),
[](cudf::type_id type) { return cudf::is_numeric(cudf::data_type{type}); }));
}
TEST_F(TraitsTest, TimestampDataTypesAreNotNumeric)
{
EXPECT_TRUE(
std::none_of(cudf::test::timestamp_type_ids.begin(),
cudf::test::timestamp_type_ids.end(),
[](cudf::type_id type) { return cudf::is_numeric(cudf::data_type{type}); }));
}
TEST_F(TraitsTest, NumericDataTypesAreNotTimestamps)
{
EXPECT_TRUE(
std::none_of(cudf::test::numeric_type_ids.begin(),
cudf::test::numeric_type_ids.end(),
[](cudf::type_id type) { return cudf::is_timestamp(cudf::data_type{type}); }));
}
TEST_F(TraitsTest, TimestampDataTypesAreTimestamps)
{
EXPECT_TRUE(
std::all_of(cudf::test::timestamp_type_ids.begin(),
cudf::test::timestamp_type_ids.end(),
[](cudf::type_id type) { return cudf::is_timestamp(cudf::data_type{type}); }));
}
TYPED_TEST(TypedTraitsTest, RelationallyComparable)
{
// All the test types should be comparable with themselves
bool comparable = cudf::is_relationally_comparable<TypeParam, TypeParam>();
EXPECT_TRUE(comparable);
}
TYPED_TEST(TypedTraitsTest, NotRelationallyComparable)
{
// No type should be comparable with an empty dummy type
struct foo {};
bool comparable = cudf::is_relationally_comparable<foo, TypeParam>();
EXPECT_FALSE(comparable);
comparable = cudf::is_relationally_comparable<TypeParam, foo>();
EXPECT_FALSE(comparable);
}
TYPED_TEST(TypedTraitsTest, NotRelationallyComparableWithList)
{
bool comparable = cudf::is_relationally_comparable<TypeParam, cudf::list_view>();
EXPECT_FALSE(comparable);
comparable = cudf::is_relationally_comparable<cudf::list_view, cudf::list_view>();
EXPECT_FALSE(comparable);
}
TYPED_TEST(TypedTraitsTest, EqualityComparable)
{
// All the test types should be comparable with themselves
bool comparable = cudf::is_equality_comparable<TypeParam, TypeParam>();
EXPECT_TRUE(comparable);
}
TYPED_TEST(TypedTraitsTest, NotEqualityComparable)
{
// No type should be comparable with an empty dummy type
struct foo {};
bool comparable = cudf::is_equality_comparable<foo, TypeParam>();
EXPECT_FALSE(comparable);
comparable = cudf::is_equality_comparable<TypeParam, foo>();
EXPECT_FALSE(comparable);
}
TYPED_TEST(TypedTraitsTest, NotEqualityComparableWithList)
{
bool comparable = cudf::is_equality_comparable<TypeParam, cudf::list_view>();
EXPECT_FALSE(comparable);
cudf::is_equality_comparable<cudf::list_view, cudf::list_view>();
EXPECT_FALSE(comparable);
}
// TODO: Tests for is_compound, is_fixed_width
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/types/type_dispatcher_test.cu
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/detail/utilities/vector_factories.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/type_dispatcher.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <rmm/device_uvector.hpp>
struct DispatcherTest : public cudf::test::BaseFixture {};
template <typename T>
struct TypedDispatcherTest : public DispatcherTest {};
TYPED_TEST_SUITE(TypedDispatcherTest, cudf::test::AllTypes);
namespace {
template <typename Expected>
struct type_tester {
template <typename Dispatched>
bool operator()()
{
return std::is_same_v<Expected, Dispatched>;
}
};
} // namespace
TYPED_TEST(TypedDispatcherTest, TypeToId)
{
EXPECT_TRUE(cudf::type_dispatcher(cudf::data_type{cudf::type_to_id<TypeParam>()},
type_tester<TypeParam>{}));
}
namespace {
struct verify_dispatched_type {
template <typename T>
__host__ __device__ bool operator()(cudf::type_id id)
{
return id == cudf::type_to_id<T>();
}
};
__global__ void dispatch_test_kernel(cudf::type_id id, bool* d_result)
{
if (0 == threadIdx.x + blockIdx.x * blockDim.x)
*d_result = cudf::type_dispatcher(cudf::data_type{id}, verify_dispatched_type{}, id);
}
} // namespace
TYPED_TEST(TypedDispatcherTest, DeviceDispatch)
{
auto result = cudf::detail::make_zeroed_device_uvector_sync<bool>(
1, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
dispatch_test_kernel<<<1, 1, 0, cudf::get_default_stream().value()>>>(
cudf::type_to_id<TypeParam>(), result.data());
CUDF_CUDA_TRY(cudaDeviceSynchronize());
EXPECT_EQ(true, result.front_element(cudf::get_default_stream()));
}
struct IdDispatcherTest : public DispatcherTest,
public testing::WithParamInterface<cudf::type_id> {};
INSTANTIATE_TEST_CASE_P(TestAllIds, IdDispatcherTest, testing::ValuesIn(cudf::test::all_type_ids));
TEST_P(IdDispatcherTest, IdToType)
{
auto t = GetParam();
EXPECT_TRUE(cudf::type_dispatcher(cudf::data_type{t}, verify_dispatched_type{}, t));
}
template <typename T>
struct TypedDoubleDispatcherTest : public DispatcherTest {};
TYPED_TEST_SUITE(TypedDoubleDispatcherTest, cudf::test::AllTypes);
namespace {
template <typename Expected1, typename Expected2>
struct two_type_tester {
template <typename Dispatched1, typename Dispatched2>
bool operator()()
{
return std::is_same_v<Expected1, Dispatched1> && std::is_same_v<Expected2, Dispatched2>;
}
};
} // namespace
TYPED_TEST(TypedDoubleDispatcherTest, TypeToId)
{
EXPECT_TRUE(cudf::double_type_dispatcher(cudf::data_type{cudf::type_to_id<TypeParam>()},
cudf::data_type{cudf::type_to_id<TypeParam>()},
two_type_tester<TypeParam, TypeParam>{}));
}
namespace {
struct verify_double_dispatched_type {
template <typename T1, typename T2>
__host__ __device__ bool operator()(cudf::type_id id1, cudf::type_id id2)
{
return id1 == cudf::type_to_id<T1>() && id2 == cudf::type_to_id<T2>();
}
};
__global__ void double_dispatch_test_kernel(cudf::type_id id1, cudf::type_id id2, bool* d_result)
{
if (0 == threadIdx.x + blockIdx.x * blockDim.x)
*d_result = cudf::double_type_dispatcher(
cudf::data_type{id1}, cudf::data_type{id2}, verify_double_dispatched_type{}, id1, id2);
}
} // namespace
TYPED_TEST(TypedDoubleDispatcherTest, DeviceDoubleDispatch)
{
auto result = cudf::detail::make_zeroed_device_uvector_sync<bool>(
1, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
double_dispatch_test_kernel<<<1, 1, 0, cudf::get_default_stream().value()>>>(
cudf::type_to_id<TypeParam>(), cudf::type_to_id<TypeParam>(), result.data());
CUDF_CUDA_TRY(cudaDeviceSynchronize());
EXPECT_EQ(true, result.front_element(cudf::get_default_stream()));
}
struct IdDoubleDispatcherTest : public DispatcherTest,
public testing::WithParamInterface<cudf::type_id> {};
INSTANTIATE_TEST_CASE_P(TestAllIds,
IdDoubleDispatcherTest,
testing::ValuesIn(cudf::test::all_type_ids));
TEST_P(IdDoubleDispatcherTest, IdToType)
{
// Test double-dispatch of all types using the same type for both dispatches
auto t = GetParam();
EXPECT_TRUE(cudf::double_type_dispatcher(
cudf::data_type{t}, cudf::data_type{t}, verify_double_dispatched_type{}, t, t));
}
struct IdFixedDoubleDispatcherTest : public DispatcherTest,
public testing::WithParamInterface<cudf::type_id> {};
INSTANTIATE_TEST_CASE_P(TestAllIds,
IdFixedDoubleDispatcherTest,
testing::ValuesIn(cudf::test::all_type_ids));
TEST_P(IdFixedDoubleDispatcherTest, IdToType)
{
// Test double-dispatch of all types against one fixed type, in each direction
auto t = GetParam();
EXPECT_TRUE(cudf::double_type_dispatcher(cudf::data_type{t},
cudf::data_type{cudf::type_to_id<float>()},
verify_double_dispatched_type{},
t,
cudf::type_to_id<float>()));
EXPECT_TRUE(cudf::double_type_dispatcher(cudf::data_type{cudf::type_to_id<float>()},
cudf::data_type{t},
verify_double_dispatched_type{},
cudf::type_to_id<float>(),
t));
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/keys_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/aggregation.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_keys_test : public cudf::test::BaseFixture {};
using supported_types = cudf::test::
Types<int8_t, int16_t, int32_t, int64_t, float, double, numeric::decimal32, numeric::decimal64>;
TYPED_TEST_SUITE(groupby_keys_test, supported_types);
TYPED_TEST(groupby_keys_test, basic)
{
using K = TypeParam;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys { 1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<K> expect_keys { 1, 2, 3 };
cudf::test::fixed_width_column_wrapper<R> expect_vals { 3, 4, 3 };
// clang-format on
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_keys_test, zero_valid_keys)
{
using K = TypeParam;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys ( { 1, 2, 3}, all_nulls() );
cudf::test::fixed_width_column_wrapper<V> vals { 3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys { };
cudf::test::fixed_width_column_wrapper<R> expect_vals { };
// clang-format on
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_keys_test, some_null_keys)
{
using K = TypeParam;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys( { 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{ 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4};
// { 1, 1, 1, 2, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 3, 4}, no_nulls() );
// { 0, 3, 6, 1, 4, 5, 9, 2, 8, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals { 3, 4, 2, 1};
// clang-format on
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_keys_test, include_null_keys)
{
using K = TypeParam;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys( { 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{ 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4};
// { 1, 1, 1, 2, 2, 2, 2, 3, 3, 4, -}
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 3, 4, 3},
{ 1, 1, 1, 1, 0});
// { 0, 3, 6, 1, 4, 5, 9, 2, 8, -, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals { 9, 19, 10, 4, 7};
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
force_use_sort_impl::NO,
cudf::null_policy::INCLUDE);
}
TYPED_TEST(groupby_keys_test, pre_sorted_keys)
{
using K = TypeParam;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys { 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4};
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4};
cudf::test::fixed_width_column_wrapper<K> expect_keys { 1, 2, 3, 4};
cudf::test::fixed_width_column_wrapper<R> expect_vals { 3, 18, 24, 4};
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
force_use_sort_impl::YES,
cudf::null_policy::EXCLUDE,
cudf::sorted::YES);
}
TYPED_TEST(groupby_keys_test, pre_sorted_keys_descending)
{
using K = TypeParam;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys { 4, 3, 3, 3, 2, 2, 2, 2, 1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4};
cudf::test::fixed_width_column_wrapper<K> expect_keys { 4, 3, 2, 1 };
cudf::test::fixed_width_column_wrapper<R> expect_vals { 0, 6, 22, 21 };
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
force_use_sort_impl::YES,
cudf::null_policy::EXCLUDE,
cudf::sorted::YES,
{cudf::order::DESCENDING});
}
TYPED_TEST(groupby_keys_test, pre_sorted_keys_nullable)
{
using K = TypeParam;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys( { 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4},
{ 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4};
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 3, 4}, no_nulls() );
cudf::test::fixed_width_column_wrapper<R> expect_vals { 3, 15, 17, 4};
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
force_use_sort_impl::YES,
cudf::null_policy::EXCLUDE,
cudf::sorted::YES);
}
TYPED_TEST(groupby_keys_test, pre_sorted_keys_nulls_before_include_nulls)
{
using K = TypeParam;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys( { 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4},
{ 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4};
// { 1, 1, 1, -, -, 2, 2, -, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 2, 3, 3, 4},
{ 1, 0, 1, 0, 1, 1});
cudf::test::fixed_width_column_wrapper<R> expect_vals { 3, 7, 11, 7, 17, 4};
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
force_use_sort_impl::YES,
cudf::null_policy::INCLUDE,
cudf::sorted::YES);
}
TYPED_TEST(groupby_keys_test, mismatch_num_rows)
{
using K = TypeParam;
using V = int32_t;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4};
// Verify that scan throws an error when given data of mismatched sizes.
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
EXPECT_THROW(test_single_agg(keys, vals, keys, vals, std::move(agg)), cudf::logic_error);
auto agg2 = cudf::make_count_aggregation<cudf::groupby_scan_aggregation>();
EXPECT_THROW(test_single_scan(keys, vals, keys, vals, std::move(agg2)), cudf::logic_error);
}
template <typename T>
using FWCW = cudf::test::fixed_width_column_wrapper<T>;
TYPED_TEST(groupby_keys_test, structs)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<int, cudf::aggregation::ARGMAX>;
using STRINGS = cudf::test::strings_column_wrapper;
using STRUCTS = cudf::test::structs_column_wrapper;
if (std::is_same_v<V, bool>) return;
/*
`@` indicates null
keys: values:
/+----------------+
|s1{s2{a,b}, c}|
+-----------------+
0 | { { 1, 1}, "a"}| 1
1 | { { 1, 2}, "b"}| 2
2 | {@{ 2, 1}, "c"}| 3
3 | {@{ 2, 1}, "c"}| 4
4 | @{ { 2, 2}, "d"}| 5
5 | @{ { 2, 2}, "d"}| 6
6 | { { 1, 1}, "a"}| 7
7 | {@{ 2, 1}, "c"}| 8
8 | { {@1, 1}, "a"}| 9
+-----------------+
*/
// clang-format off
auto col_a = FWCW<V>{{ 1, 1, 2, 2, 2, 2, 1, 2, 1 }, null_at(8)};
auto col_b = FWCW<V> { 1, 2, 1, 1, 2, 2, 1, 1, 1 };
auto col_c = STRINGS {"a", "b", "c", "c", "d", "d", "a", "c", "a"};
// clang-format on
auto s2 = STRUCTS{{col_a, col_b}, nulls_at({2, 3, 7})};
auto keys = STRUCTS{{s2, col_c}, nulls_at({4, 5})};
auto vals = FWCW<int>{1, 2, 3, 4, 5, 6, 7, 8, 9};
// clang-format off
auto expected_col_a = FWCW<V>{{1, 1, 1, 2 }, null_at(2)};
auto expected_col_b = FWCW<V>{ 1, 2, 1, 1 };
auto expected_col_c = STRINGS{"a", "b", "a", "c"};
// clang-format on
auto expected_s2 = STRUCTS{{expected_col_a, expected_col_b}, null_at(3)};
auto expect_keys = STRUCTS{{expected_s2, expected_col_c}, no_nulls()};
auto expect_vals = FWCW<R>{6, 1, 8, 7};
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
template <typename T>
using LCW = cudf::test::lists_column_wrapper<T, int32_t>;
TYPED_TEST(groupby_keys_test, lists)
{
using R = cudf::detail::target_type_t<int32_t, cudf::aggregation::SUM>;
// clang-format off
auto keys = LCW<TypeParam> { {1,1}, {2,2}, {3,3}, {1,1}, {2,2} };
auto values = FWCW<int32_t> { 0, 1, 2, 3, 4 };
auto expected_keys = LCW<TypeParam> { {1,1}, {2,2}, {3,3} };
auto expected_values = FWCW<R> { 3, 5, 2 };
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, values, expected_keys, expected_values, std::move(agg));
}
struct groupby_string_keys_test : public cudf::test::BaseFixture {};
TEST_F(groupby_string_keys_test, basic)
{
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
cudf::test::strings_column_wrapper keys { "aaa", "año", "₹1", "aaa", "año", "año", "aaa", "₹1", "₹1", "año"};
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::strings_column_wrapper expect_keys({ "aaa", "año", "₹1" });
cudf::test::fixed_width_column_wrapper<R> expect_vals { 9, 19, 17 };
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
// clang-format on
struct groupby_dictionary_keys_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_keys_test, basic)
{
using K = std::string;
using V = int32_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
cudf::test::dictionary_column_wrapper<K> keys { "aaa", "año", "₹1", "aaa", "año", "año", "aaa", "₹1", "₹1", "año"};
cudf::test::fixed_width_column_wrapper<V> vals{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::dictionary_column_wrapper<K>expect_keys ({ "aaa", "año", "₹1" });
cudf::test::fixed_width_column_wrapper<R> expect_vals({ 9, 19, 17 });
// clang-format on
test_single_agg(
keys, vals, expect_keys, expect_vals, cudf::make_sum_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_sum_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
struct groupby_cache_test : public cudf::test::BaseFixture {};
// To check if the cache doesn't insert multiple times to cache for the same aggregation on a
// column in the same request. If this test fails, then insert happened and the key stored in the
// cache map becomes a dangling reference. Any comparison with the same aggregation as the key will
// fail.
TEST_F(groupby_cache_test, duplicate_agggregations)
{
using K = int32_t;
using V = int32_t;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::groupby::groupby gb_obj(cudf::table_view({keys}));
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = vals;
requests[0].aggregations.push_back(cudf::make_sum_aggregation<cudf::groupby_aggregation>());
requests[0].aggregations.push_back(cudf::make_sum_aggregation<cudf::groupby_aggregation>());
// hash groupby
EXPECT_NO_THROW(gb_obj.aggregate(requests));
// sort groupby
// WAR to force groupby to use sort implementation
requests[0].aggregations.push_back(
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0));
EXPECT_NO_THROW(gb_obj.aggregate(requests));
}
// To check if the cache doesn't insert multiple times to cache for the same aggregation on the same
// column but in different requests. If this test fails, then insert happened and the key stored in
// the cache map becomes a dangling reference. Any comparison with the same aggregation as the key
// will fail.
TEST_F(groupby_cache_test, duplicate_columns)
{
using K = int32_t;
using V = int32_t;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::groupby::groupby gb_obj(cudf::table_view({keys}));
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = vals;
requests[0].aggregations.push_back(cudf::make_sum_aggregation<cudf::groupby_aggregation>());
requests.emplace_back(cudf::groupby::aggregation_request());
requests[1].values = vals;
requests[1].aggregations.push_back(cudf::make_sum_aggregation<cudf::groupby_aggregation>());
// hash groupby
EXPECT_NO_THROW(gb_obj.aggregate(requests));
// sort groupby
// WAR to force groupby to use sort implementation
requests[0].aggregations.push_back(
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0));
EXPECT_NO_THROW(gb_obj.aggregate(requests));
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/max_scan_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/dictionary/update_keys.hpp>
using namespace cudf::test::iterators;
using key_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
template <typename T>
struct groupby_max_scan_test : public cudf::test::BaseFixture {
using V = T;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MAX>;
using value_wrapper = cudf::test::fixed_width_column_wrapper<V, int32_t>;
using result_wrapper = cudf::test::fixed_width_column_wrapper<R, int32_t>;
};
TYPED_TEST_SUITE(groupby_max_scan_test, cudf::test::FixedWidthTypesWithoutFixedPoint);
TYPED_TEST(groupby_max_scan_test, basic)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys {1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
value_wrapper vals({5, 6, 7, 8, 9, 0, 1, 2, 3, 4});
key_wrapper expect_keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
// {5, 8, 1, 6, 9, 0, 4, 7, 2, 3}
result_wrapper expect_vals({5, 8, 8, 6, 9, 9, 9, 7, 7, 7});
// clang-format on
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_max_scan_test, pre_sorted)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
value_wrapper vals({5, 8, 1, 6, 9, 0, 4, 7, 2, 3});
key_wrapper expect_keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
result_wrapper expect_vals({5, 8, 8, 6, 9, 9, 9, 7, 7, 7});
// clang-format on
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
cudf::null_policy::EXCLUDE,
cudf::sorted::YES);
}
TYPED_TEST(groupby_max_scan_test, empty_cols)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys{};
value_wrapper vals{};
key_wrapper expect_keys{};
result_wrapper expect_vals{};
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_max_scan_test, zero_valid_keys)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys({1, 2, 3}, all_nulls());
value_wrapper vals({3, 4, 5});
key_wrapper expect_keys{};
result_wrapper expect_vals{};
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_max_scan_test, zero_valid_values)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys{1, 1, 1};
value_wrapper vals({3, 4, 5}, all_nulls());
key_wrapper expect_keys{1, 1, 1};
result_wrapper expect_vals({-1, -1, -1}, all_nulls());
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_max_scan_test, null_keys_and_values)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys( {1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4}, {1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
value_wrapper vals({5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 4}, {0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// {1, 1, 1, 2, 2, 2, 2, 3, _, 3, 4}
key_wrapper expect_keys( {1, 1, 1, 2, 2, 2, 2, 3, 3, 4}, no_nulls() );
// { -, 3, 6, 1, 4, -, 9, 2, _, 8, -}
result_wrapper expect_vals({-1, 8, 8, 6, 9, -1, 9, 7, 7, -1},
{ 0, 1, 1, 1, 1, 0, 1, 1, 1, 0});
// clang-format on
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
struct groupby_max_scan_string_test : public cudf::test::BaseFixture {};
TEST_F(groupby_max_scan_string_test, basic)
{
key_wrapper keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::strings_column_wrapper vals{
"año", "bit", "₹1", "aaa", "zit", "bat", "aaa", "$1", "₹1", "wut"};
key_wrapper expect_keys{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
cudf::test::strings_column_wrapper expect_vals(
{"año", "año", "año", "bit", "zit", "zit", "zit", "₹1", "₹1", "₹1"});
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
template <typename T>
struct GroupByMaxScanFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupByMaxScanFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(GroupByMaxScanFixedPointTest, GroupBySortMaxScanDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = key_wrapper{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{{5, 6, 7, 8, 9, 0, 1, 2, 3, 4}, scale};
// {5, 8, 1, 6, 9, 0, 4, 7, 2, 3}
auto const expect_keys = key_wrapper{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
auto const expect_vals_max = fp_wrapper{{5, 8, 8, 6, 9, 9, 9, 7, 7, 7}, scale};
// clang-format on
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals_max, std::move(agg));
}
}
struct groupby_max_scan_struct_test : public cudf::test::BaseFixture {};
TEST_F(groupby_max_scan_struct_test, basic)
{
auto const keys = key_wrapper{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const expect_keys = key_wrapper{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "año", "año", "bit", "zit", "zit", "zit", "₹1", "₹1", "₹1"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 1, 1, 2, 5, 5, 5, 3, 3, 3};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_max_scan_struct_test, slice_input)
{
constexpr int32_t dont_care{1};
auto const keys_original =
key_wrapper{dont_care, dont_care, 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, dont_care};
auto const vals_original = [] {
auto child1 = cudf::test::strings_column_wrapper{"dont_care",
"dont_care",
"año",
"bit",
"₹1",
"aaa",
"zit",
"bat",
"aab",
"$1",
"€1",
"wut",
"dont_care"};
auto child2 = key_wrapper{dont_care, dont_care, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, dont_care};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const keys = cudf::slice(keys_original, {2, 12})[0];
auto const vals = cudf::slice(vals_original, {2, 12})[0];
auto const expect_keys = key_wrapper{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "año", "año", "bit", "zit", "zit", "zit", "₹1", "₹1", "₹1"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 1, 1, 2, 5, 5, 5, 3, 3, 3};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_max_scan_struct_test, null_keys_and_values)
{
constexpr int32_t null{0};
auto const keys = key_wrapper{{1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4}, null_at(7)};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "" /*NULL*/, "" /*NULL*/, "$1", "€1", "wut", "" /*NULL*/};
auto child2 =
cudf::test::fixed_width_column_wrapper<int32_t>{9, 8, 7, 6, 5, null, null, 2, 1, 0, null};
return cudf::test::structs_column_wrapper{{child1, child2}, nulls_at({5, 6, 10})};
}();
auto const expect_keys = key_wrapper{{1, 1, 1, 2, 2, 2, 2, 3, 3, 4}, no_nulls()};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "año", "" /*NULL*/, "bit", "zit", "" /*NULL*/, "zit", "₹1", "₹1", "" /*NULL*/};
auto child2 =
cudf::test::fixed_width_column_wrapper<int32_t>{9, 9, null, 8, 5, null, 5, 7, 7, null};
return cudf::test::structs_column_wrapper{{child1, child2}, nulls_at({2, 5, 9})};
}();
auto agg = cudf::make_max_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/lists_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
template <typename V>
struct groupby_lists_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_lists_test, cudf::test::FixedWidthTypes);
using namespace cudf::test::iterators;
// Type of aggregation result.
using agg_result_t = cudf::detail::target_type_t<int32_t, cudf::aggregation::SUM>;
template <typename T>
using fwcw = cudf::test::fixed_width_column_wrapper<T>;
template <typename T>
using lcw = cudf::test::lists_column_wrapper<T, int32_t>;
namespace {
static constexpr auto null = -1;
// Checking with a single aggregation, and aggregation column.
// This test is orthogonal to the aggregation type; it focuses on testing the grouping
// with LISTS keys.
} // namespace
TYPED_TEST(groupby_lists_test, basic)
{
if (std::is_same_v<TypeParam, bool>) { return; }
// clang-format off
auto keys = lcw<TypeParam> { {1,1}, {2,2}, {3,3}, {1,1}, {2,2} };
auto values = fwcw<int32_t> { 0, 1, 2, 3, 4 };
auto expected_keys = lcw<TypeParam> { {1,1}, {2,2}, {3,3} };
auto expected_values = fwcw<agg_result_t>{ 3, 5, 2 };
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_lists_test, all_null_input)
{
// clang-format off
auto keys = lcw<TypeParam> { {{1,1}, {2,2}, {3,3}, {1,1}, {2,2}}, all_nulls()};
auto values = fwcw<int32_t> { 0, 1, 2, 3, 4 };
auto expected_keys = lcw<TypeParam> { {{null,null}}, all_nulls()};
auto expected_values = fwcw<agg_result_t>{ 10 };
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_lists_test, lists_with_nulls)
{
// clang-format off
auto keys = lcw<TypeParam> { {{1,1}, {2,2}, {3,3}, {1,1}, {2,2}}, nulls_at({1,2,4})};
auto values = fwcw<int32_t> { 0, 1, 2, 3, 4 };
auto expected_keys = lcw<TypeParam> { {{null,null}, {1,1}}, null_at(0)};
auto expected_values = fwcw<agg_result_t>{ 7, 3 };
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_lists_test, lists_with_null_elements)
{
auto keys =
lcw<TypeParam>{{lcw<TypeParam>{{{1, 2, 3}, {}, {4, 5}, {}, {6, 0}}, nulls_at({1, 3})},
lcw<TypeParam>{{{1, 2, 3}, {}, {4, 5}, {}, {6, 0}}, nulls_at({1, 3})},
lcw<TypeParam>{{{1, 2, 3}, {}, {4, 5}, {}, {6, 0}}, nulls_at({1, 3})},
lcw<TypeParam>{{{1, 2, 3}, {}, {4, 5}, {}, {6, 0}}, nulls_at({1, 3})}},
nulls_at({2, 3})};
auto values = fwcw<int32_t>{1, 2, 4, 5};
auto expected_keys = lcw<TypeParam>{
{{}, lcw<TypeParam>{{{1, 2, 3}, {}, {4, 5}, {}, {6, 0}}, nulls_at({1, 3})}}, null_at(0)};
auto expected_values = fwcw<agg_result_t>{9, 3};
test_sum_agg(keys, values, expected_keys, expected_values);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/groupby_test_util.hpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <cudf/column/column_view.hpp>
#include <cudf/groupby.hpp>
#include <cudf/sorting.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/default_stream.hpp>
enum class force_use_sort_impl : bool { NO, YES };
void test_single_agg(cudf::column_view const& keys,
cudf::column_view const& values,
cudf::column_view const& expect_keys,
cudf::column_view const& expect_vals,
std::unique_ptr<cudf::groupby_aggregation>&& agg,
force_use_sort_impl use_sort = force_use_sort_impl::NO,
cudf::null_policy include_null_keys = cudf::null_policy::EXCLUDE,
cudf::sorted keys_are_sorted = cudf::sorted::NO,
std::vector<cudf::order> const& column_order = {},
std::vector<cudf::null_order> const& null_precedence = {},
cudf::sorted reference_keys_are_sorted = cudf::sorted::NO);
void test_sum_agg(cudf::column_view const& keys,
cudf::column_view const& values,
cudf::column_view const& expected_keys,
cudf::column_view const& expected_values);
void test_single_scan(cudf::column_view const& keys,
cudf::column_view const& values,
cudf::column_view const& expect_keys,
cudf::column_view const& expect_vals,
std::unique_ptr<cudf::groupby_scan_aggregation>&& agg,
cudf::null_policy include_null_keys = cudf::null_policy::EXCLUDE,
cudf::sorted keys_are_sorted = cudf::sorted::NO,
std::vector<cudf::order> const& column_order = {},
std::vector<cudf::null_order> const& null_precedence = {});
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/groupby_test_util.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "groupby_test_util.hpp"
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/copying.hpp>
#include <cudf/groupby.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table.hpp>
#include <cudf/types.hpp>
#include <cudf/unary.hpp>
#include <random>
void test_single_agg(cudf::column_view const& keys,
cudf::column_view const& values,
cudf::column_view const& expect_keys,
cudf::column_view const& expect_vals,
std::unique_ptr<cudf::groupby_aggregation>&& agg,
force_use_sort_impl use_sort,
cudf::null_policy include_null_keys,
cudf::sorted keys_are_sorted,
std::vector<cudf::order> const& column_order,
std::vector<cudf::null_order> const& null_precedence,
cudf::sorted reference_keys_are_sorted)
{
auto const [sorted_expect_keys, sorted_expect_vals] = [&]() {
if (reference_keys_are_sorted == cudf::sorted::NO) {
auto const sort_expect_order =
cudf::sorted_order(cudf::table_view{{expect_keys}}, column_order, null_precedence);
auto sorted_expect_keys = cudf::gather(cudf::table_view{{expect_keys}}, *sort_expect_order);
auto sorted_expect_vals = cudf::gather(cudf::table_view{{expect_vals}}, *sort_expect_order);
return std::make_pair(std::move(sorted_expect_keys), std::move(sorted_expect_vals));
} else {
auto sorted_expect_keys = std::make_unique<cudf::table>(cudf::table_view{{expect_keys}});
auto sorted_expect_vals = std::make_unique<cudf::table>(cudf::table_view{{expect_vals}});
return std::make_pair(std::move(sorted_expect_keys), std::move(sorted_expect_vals));
}
}();
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = values;
requests[0].aggregations.push_back(std::move(agg));
if (use_sort == force_use_sort_impl::YES) {
// WAR to force cudf::groupby to use sort implementation
requests[0].aggregations.push_back(
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0));
}
// since the default behavior of cudf::groupby(...) for an empty null_precedence vector is
// null_order::AFTER whereas for cudf::sorted_order(...) it's null_order::BEFORE
auto const precedence = null_precedence.empty()
? std::vector<cudf::null_order>(1, cudf::null_order::BEFORE)
: null_precedence;
cudf::groupby::groupby gb_obj(
cudf::table_view({keys}), include_null_keys, keys_are_sorted, column_order, precedence);
auto result = gb_obj.aggregate(requests, cudf::test::get_default_stream());
if (use_sort == force_use_sort_impl::YES && keys_are_sorted == cudf::sorted::NO) {
CUDF_TEST_EXPECT_TABLES_EQUAL(*sorted_expect_keys, result.first->view());
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(sorted_expect_vals->get_column(0),
*result.second[0].results[0]);
} else {
auto const sort_order = cudf::sorted_order(result.first->view(), column_order, precedence);
auto const sorted_keys = cudf::gather(result.first->view(), *sort_order);
auto const sorted_vals =
cudf::gather(cudf::table_view({result.second[0].results[0]->view()}), *sort_order);
CUDF_TEST_EXPECT_TABLES_EQUAL(*sorted_expect_keys, *sorted_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(sorted_expect_vals->get_column(0),
sorted_vals->get_column(0));
}
}
void test_sum_agg(cudf::column_view const& keys,
cudf::column_view const& values,
cudf::column_view const& expected_keys,
cudf::column_view const& expected_values)
{
auto const do_test = [&](auto const use_sort_option) {
test_single_agg(keys,
values,
expected_keys,
expected_values,
cudf::make_sum_aggregation<cudf::groupby_aggregation>(),
use_sort_option,
cudf::null_policy::INCLUDE);
};
do_test(force_use_sort_impl::YES);
do_test(force_use_sort_impl::NO);
}
void test_single_scan(cudf::column_view const& keys,
cudf::column_view const& values,
cudf::column_view const& expect_keys,
cudf::column_view const& expect_vals,
std::unique_ptr<cudf::groupby_scan_aggregation>&& agg,
cudf::null_policy include_null_keys,
cudf::sorted keys_are_sorted,
std::vector<cudf::order> const& column_order,
std::vector<cudf::null_order> const& null_precedence)
{
std::vector<cudf::groupby::scan_request> requests;
requests.emplace_back(cudf::groupby::scan_request());
requests[0].values = values;
requests[0].aggregations.push_back(std::move(agg));
cudf::groupby::groupby gb_obj(
cudf::table_view({keys}), include_null_keys, keys_are_sorted, column_order, null_precedence);
// cudf::groupby scan uses sort implementation
auto result = gb_obj.scan(requests);
CUDF_TEST_EXPECT_TABLES_EQUAL(cudf::table_view({expect_keys}), result.first->view());
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expect_vals, *result.second[0].results[0]);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/collect_list_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
template <typename V>
struct groupby_collect_list_test : public cudf::test::BaseFixture {};
using FixedWidthTypesNotBool = cudf::test::Concat<cudf::test::IntegralTypesNotBool,
cudf::test::FloatingPointTypes,
cudf::test::TimestampTypes>;
TYPED_TEST_SUITE(groupby_collect_list_test, FixedWidthTypesNotBool);
TYPED_TEST(groupby_collect_list_test, CollectWithoutNulls)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{1, 1, 1, 2, 2, 2};
cudf::test::fixed_width_column_wrapper<V, int32_t> values{1, 2, 3, 4, 5, 6};
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{1, 2};
cudf::test::lists_column_wrapper<V, int32_t> expect_vals{{1, 2, 3}, {4, 5, 6}};
auto agg = cudf::make_collect_list_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, values, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_collect_list_test, CollectWithNulls)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{1, 1, 2, 2, 3, 3};
cudf::test::fixed_width_column_wrapper<V, int32_t> values{
{1, 2, 3, 4, 5, 6}, {true, false, true, false, true, false}};
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{1, 2, 3};
std::vector<int32_t> validity({true, false});
cudf::test::lists_column_wrapper<V, int32_t> expect_vals{
{{1, 2}, validity.begin()}, {{3, 4}, validity.begin()}, {{5, 6}, validity.begin()}};
auto agg = cudf::make_collect_list_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, values, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_collect_list_test, CollectWithNullExclusion)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{1, 1, 1, 2, 2, 3, 3, 4, 4};
cudf::test::fixed_width_column_wrapper<V, int32_t> values{
{1, 2, 3, 4, 5, 6, 7, 8, 9}, {false, true, false, true, false, false, false, true, true}};
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{1, 2, 3, 4};
cudf::test::lists_column_wrapper<V, int32_t> expect_vals{{2}, {4}, {}, {8, 9}};
auto agg =
cudf::make_collect_list_aggregation<cudf::groupby_aggregation>(cudf::null_policy::EXCLUDE);
test_single_agg(keys, values, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_collect_list_test, CollectOnEmptyInput)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{};
cudf::test::fixed_width_column_wrapper<V, int32_t> values{};
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{};
cudf::test::lists_column_wrapper<V, int32_t> expect_vals{};
auto agg =
cudf::make_collect_list_aggregation<cudf::groupby_aggregation>(cudf::null_policy::EXCLUDE);
test_single_agg(keys, values, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_collect_list_test, CollectLists)
{
using K = int32_t;
using V = TypeParam;
using LCW = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{1, 1, 2, 2, 3, 3};
cudf::test::lists_column_wrapper<V, int32_t> values{
{1, 2}, {3, 4}, {5, 6, 7}, LCW{}, {9, 10}, {11}};
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{1, 2, 3};
cudf::test::lists_column_wrapper<V, int32_t> expect_vals{
{{1, 2}, {3, 4}}, {{5, 6, 7}, LCW{}}, {{9, 10}, {11}}};
auto agg = cudf::make_collect_list_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, values, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_collect_list_test, CollectListsWithNullExclusion)
{
using K = int32_t;
using V = TypeParam;
using LCW = cudf::test::lists_column_wrapper<V, int32_t>;
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{1, 1, 2, 2, 3, 3, 4, 4};
bool const validity_mask[] = {true, false, false, true, true, true, false, false};
LCW values{{{1, 2}, {3, 4}, {5, 6, 7}, LCW{}, {9, 10}, {11}, {20, 30, 40}, LCW{}}, validity_mask};
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{1, 2, 3, 4};
LCW expect_vals{{{1, 2}}, {LCW{}}, {{9, 10}, {11}}, {}};
auto agg =
cudf::make_collect_list_aggregation<cudf::groupby_aggregation>(cudf::null_policy::EXCLUDE);
test_single_agg(keys, values, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_collect_list_test, CollectOnEmptyInputLists)
{
using K = int32_t;
using V = TypeParam;
using LCW = cudf::test::lists_column_wrapper<V, int32_t>;
auto offsets = cudf::data_type{cudf::type_to_id<cudf::size_type>()};
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{};
auto values =
cudf::make_lists_column(0, cudf::make_empty_column(offsets), LCW{}.release(), 0, {});
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{};
auto expect_child =
cudf::make_lists_column(0, cudf::make_empty_column(offsets), LCW{}.release(), 0, {});
auto expect_values =
cudf::make_lists_column(0, cudf::make_empty_column(offsets), std::move(expect_child), 0, {});
auto agg = cudf::make_collect_list_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, values->view(), expect_keys, expect_values->view(), std::move(agg));
}
TYPED_TEST(groupby_collect_list_test, CollectOnEmptyInputListsOfStructs)
{
using K = int32_t;
using V = TypeParam;
using LCW = cudf::test::lists_column_wrapper<V, int32_t>;
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{};
auto struct_child = LCW{};
auto struct_column = cudf::test::structs_column_wrapper{{struct_child}};
auto values =
cudf::make_lists_column(0,
cudf::make_empty_column(cudf::type_to_id<cudf::size_type>()),
struct_column.release(),
0,
{});
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{};
auto expect_struct_child = LCW{};
auto expect_struct_column = cudf::test::structs_column_wrapper{{expect_struct_child}};
auto expect_child =
cudf::make_lists_column(0,
cudf::make_empty_column(cudf::type_to_id<cudf::size_type>()),
expect_struct_column.release(),
0,
{});
auto expect_values =
cudf::make_lists_column(0,
cudf::make_empty_column(cudf::type_to_id<cudf::size_type>()),
std::move(expect_child),
0,
{});
auto agg = cudf::make_collect_list_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, values->view(), expect_keys, expect_values->view(), std::move(agg));
}
TYPED_TEST(groupby_collect_list_test, dictionary)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K, int32_t> keys{1, 1, 1, 2, 2, 2};
cudf::test::dictionary_column_wrapper<V, int32_t> vals{1, 2, 3, 4, 5, 6};
cudf::test::fixed_width_column_wrapper<K, int32_t> expect_keys{1, 2};
cudf::test::lists_column_wrapper<V, int32_t> expect_vals_w{{1, 2, 3}, {4, 5, 6}};
cudf::test::fixed_width_column_wrapper<int32_t> offsets({0, 3, 6});
auto expect_vals = cudf::make_lists_column(cudf::column_view(offsets).size() - 1,
std::make_unique<cudf::column>(offsets),
std::make_unique<cudf::column>(vals),
0,
rmm::device_buffer{});
test_single_agg(keys,
vals,
expect_keys,
expect_vals->view(),
cudf::make_collect_list_aggregation<cudf::groupby_aggregation>());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/merge_m2_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/aggregation.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/groupby.hpp>
#include <cudf/table/table_view.hpp>
using namespace cudf::test::iterators;
namespace {
constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::FIRST_ERROR};
constexpr int32_t null{0}; // Mark for null elements
constexpr double NaN{std::numeric_limits<double>::quiet_NaN()}; // Mark for NaN double elements
template <class T>
using keys_col = cudf::test::fixed_width_column_wrapper<T, int32_t>;
template <class T>
using vals_col = cudf::test::fixed_width_column_wrapper<T>;
using counts_col = cudf::test::fixed_width_column_wrapper<int32_t>;
template <class T>
using means_col = cudf::test::fixed_width_column_wrapper<T>;
template <class T>
using M2s_col = cudf::test::fixed_width_column_wrapper<T>;
using structs_col = cudf::test::structs_column_wrapper;
using vcol_views = std::vector<cudf::column_view>;
/**
* @brief Compute `COUNT_VALID`, `MEAN`, `M2` aggregations for the given values columns.
* @return A pair of unique keys column and a structs column containing the computed values of
* (`COUNT_VALID`, `MEAN`, `M2`).
*/
auto compute_partial_results(cudf::column_view const& keys, cudf::column_view const& values)
{
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = values;
requests[0].aggregations.emplace_back(cudf::make_count_aggregation<cudf::groupby_aggregation>());
requests[0].aggregations.emplace_back(cudf::make_mean_aggregation<cudf::groupby_aggregation>());
requests[0].aggregations.emplace_back(cudf::make_m2_aggregation<cudf::groupby_aggregation>());
auto gb_obj = cudf::groupby::groupby(cudf::table_view({keys}));
auto [out_keys, out_results] = gb_obj.aggregate(requests);
auto const num_output_rows = out_keys->num_rows();
return std::pair(std::move(out_keys->release()[0]),
cudf::make_structs_column(
num_output_rows, std::move(out_results[0].results), 0, rmm::device_buffer{}));
}
/**
* @brief Perform merging for partial results of M2 aggregations.
*
* @return A pair of unique keys column and a structs column containing the merged values of
* (`COUNT_VALID`, `MEAN`, `M2`).
*/
auto merge_M2(vcol_views const& keys_cols, vcol_views const& values_cols)
{
// Append all the keys and values together.
auto const keys = cudf::concatenate(keys_cols);
auto const values = cudf::concatenate(values_cols);
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = *values;
requests[0].aggregations.emplace_back(
cudf::make_merge_m2_aggregation<cudf::groupby_aggregation>());
auto gb_obj = cudf::groupby::groupby(cudf::table_view({*keys}));
auto result = gb_obj.aggregate(requests);
return std::pair(std::move(result.first->release()[0]), std::move(result.second[0].results[0]));
}
} // namespace
template <class T>
struct GroupbyMergeM2TypedTest : public cudf::test::BaseFixture {};
using TestTypes = cudf::test::Concat<cudf::test::Types<int8_t, int16_t, int32_t, int64_t>,
cudf::test::FloatingPointTypes>;
TYPED_TEST_SUITE(GroupbyMergeM2TypedTest, TestTypes);
TYPED_TEST(GroupbyMergeM2TypedTest, InvalidInput)
{
using T = TypeParam;
auto const keys = keys_col<T>{1, 2, 3};
// The input column must be a structs column.
{
auto const values = keys_col<T>{1, 2, 3};
EXPECT_THROW(merge_M2({keys}, {values}), cudf::logic_error);
}
// The input column must be a structs column having 3 children.
{
auto vals1 = keys_col<T>{1, 2, 3};
auto vals2 = vals_col<double>{1.0, 2.0, 3.0};
auto const vals = structs_col{vals1, vals2};
EXPECT_THROW(merge_M2({keys}, {vals}), cudf::logic_error);
}
// The input column must be a structs column having types (int32_t, double, double).
{
auto vals1 = keys_col<T>{1, 2, 3};
auto vals2 = keys_col<T>{1, 2, 3};
auto vals3 = keys_col<T>{1, 2, 3};
auto const vals = structs_col{vals1, vals2, vals3};
EXPECT_THROW(merge_M2({keys}, {vals}), cudf::logic_error);
}
}
TYPED_TEST(GroupbyMergeM2TypedTest, EmptyInput)
{
using T = TypeParam;
using M2_t = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
using mean_t = cudf::detail::target_type_t<T, cudf::aggregation::MEAN>;
auto const keys = keys_col<T>{};
auto vals_count = counts_col{};
auto vals_mean = means_col<mean_t>{};
auto vals_M2 = M2s_col<M2_t>{};
auto const vals = structs_col{vals_count, vals_mean, vals_M2};
auto const [out_keys, out_vals] = merge_M2({keys}, {vals});
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(vals, *out_vals, verbosity);
}
TYPED_TEST(GroupbyMergeM2TypedTest, SimpleInput)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// Full dataset:
//
// keys = [1, 2, 3, 1, 2, 2, 1, 3, 3, 2]
// vals = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
//
// key = 1: vals = [0, 3, 6]
// key = 2: vals = [1, 4, 5, 9]
// key = 3: vals = [2, 7, 8]
// Partitioned datasets:
auto const keys1 = keys_col<T>{1, 2, 3};
auto const keys2 = keys_col<T>{1, 2, 2};
auto const keys3 = keys_col<T>{1, 3, 3, 2};
auto const vals1 = vals_col<T>{0, 1, 2};
auto const vals2 = vals_col<T>{3, 4, 5};
auto const vals3 = vals_col<T>{6, 7, 8, 9};
// The expected results to validate.
auto const expected_keys = keys_col<T>{1, 2, 3};
auto const expected_M2s = M2s_col<R>{18.0, 32.75, 20.0 + 2.0 / 3.0};
// Compute partial results (`COUNT_VALID`, `MEAN`, `M2`) of each dataset.
// The partial results are also assembled into a structs column.
auto const [out1_keys, out1_vals] = compute_partial_results(keys1, vals1);
auto const [out2_keys, out2_vals] = compute_partial_results(keys2, vals2);
auto const [out3_keys, out3_vals] = compute_partial_results(keys3, vals3);
// Merge the partial results to the final results.
// Merging can be done in just one merge step, or in multiple steps.
// Multiple steps merging:
{
auto const [out4_keys, out4_vals] =
merge_M2(vcol_views{*out1_keys, *out2_keys}, vcol_views{*out1_vals, *out2_vals});
auto const [final_keys, final_vals] =
merge_M2(vcol_views{*out3_keys, *out4_keys}, vcol_views{*out3_vals, *out4_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
// One step merging:
{
auto const [final_keys, final_vals] = merge_M2(vcol_views{*out1_keys, *out2_keys, *out3_keys},
vcol_views{*out1_vals, *out2_vals, *out3_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
}
TYPED_TEST(GroupbyMergeM2TypedTest, SimpleInputHavingNegativeValues)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// Full dataset:
//
// keys = [1, 2, 3, 1, 2, 2, 1, 3, 3, 2]
// vals = [0, 1, -2, 3, -4, -5, -6, 7, -8, 9]
//
// key = 1: vals = [0, 3, -6]
// key = 2: vals = [1, -4, -5, 9]
// key = 3: vals = [-2, 7, -8]
// Partitioned datasets:
auto const keys1 = keys_col<T>{1, 2, 3};
auto const keys2 = keys_col<T>{1, 2, 2};
auto const keys3 = keys_col<T>{1, 3, 3, 2};
auto const vals1 = vals_col<T>{0, 1, -2};
auto const vals2 = vals_col<T>{3, -4, -5};
auto const vals3 = vals_col<T>{-6, 7, -8, 9};
// The expected results to validate.
auto const expected_keys = keys_col<T>{1, 2, 3};
auto const expected_M2s = M2s_col<R>{42.0, 122.75, 114.0};
// Compute partial results (`COUNT_VALID`, `MEAN`, `M2`) of each dataset.
// The partial results are also assembled into a structs column.
auto const [out1_keys, out1_vals] = compute_partial_results(keys1, vals1);
auto const [out2_keys, out2_vals] = compute_partial_results(keys2, vals2);
auto const [out3_keys, out3_vals] = compute_partial_results(keys3, vals3);
// Merge the partial results to the final results.
// Merging can be done in just one merge step, or in multiple steps.
// Multiple steps merging:
{
auto const [out4_keys, out4_vals] =
merge_M2(vcol_views{*out1_keys, *out2_keys}, vcol_views{*out1_vals, *out2_vals});
auto const [final_keys, final_vals] =
merge_M2(vcol_views{*out3_keys, *out4_keys}, vcol_views{*out3_vals, *out4_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
// One step merging:
{
auto const [final_keys, final_vals] = merge_M2(vcol_views{*out1_keys, *out2_keys, *out3_keys},
vcol_views{*out1_vals, *out2_vals, *out3_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
}
TYPED_TEST(GroupbyMergeM2TypedTest, InputHasNulls)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// Full dataset:
//
// keys = [1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4]
// vals = [null, 1, 2, 3, 4, null, 6, 7, 8, 9, null]
//
// key = 1: vals = [null, 3, 6]
// key = 2: vals = [1, 4, null, 9]
// key = 3: vals = [2, 8]
// key = 4: vals = [null]
// Partitioned datasets:
auto const keys1 = keys_col<T>{1, 2, 3, 1};
auto const keys2 = keys_col<T>{{2, 2, 1, null}, null_at(3)};
auto const keys3 = keys_col<T>{3, 2, 4};
auto const vals1 = vals_col<T>{{null, 1, 2, 3}, null_at(0)};
auto const vals2 = vals_col<T>{{4, null, 6, 7}, null_at(1)};
auto const vals3 = vals_col<T>{{8, 9, null}, null_at(2)};
// The expected results to validate.
auto const expected_keys = keys_col<T>{1, 2, 3, 4};
auto const expected_M2s = M2s_col<R>{{4.5, 32.0 + 2.0 / 3.0, 18.0, 0.0 /*NULL*/}, null_at(3)};
// Compute partial results (`COUNT_VALID`, `MEAN`, `M2`) of each dataset.
// The partial results are also assembled into a structs column.
auto const [out1_keys, out1_vals] = compute_partial_results(keys1, vals1);
auto const [out2_keys, out2_vals] = compute_partial_results(keys2, vals2);
auto const [out3_keys, out3_vals] = compute_partial_results(keys3, vals3);
// Merge the partial results to the final results.
// Merging can be done in just one merge step, or in multiple steps.
// Multiple steps merging:
{
auto const [out4_keys, out4_vals] =
merge_M2(vcol_views{*out1_keys, *out2_keys}, vcol_views{*out1_vals, *out2_vals});
auto const [final_keys, final_vals] =
merge_M2(vcol_views{*out3_keys, *out4_keys}, vcol_views{*out3_vals, *out4_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
// One step merging:
{
auto const [final_keys, final_vals] = merge_M2(vcol_views{*out1_keys, *out2_keys, *out3_keys},
vcol_views{*out1_vals, *out2_vals, *out3_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
}
TYPED_TEST(GroupbyMergeM2TypedTest, InputHaveNullsAndNaNs)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// Full dataset:
//
// keys = [4, 3, 1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4, 4]
// vals = [null, null, 0.0, 1.0, 2.0, 3.0, 4.0, NaN, 6.0, 7.0, 8.0, 9.0, 10.0, NaN]
//
// key = 1: vals = [0, 3, 6]
// key = 2: vals = [1, 4, NaN, 9]
// key = 3: vals = [null, 2, 8]
// key = 4: vals = [null, 10, NaN]
// Partitioned datasets:
auto const keys1 = keys_col<T>{4, 3, 1, 2};
auto const keys2 = keys_col<T>{3, 1, 2};
auto const keys3 = keys_col<T>{{2, 1, null}, null_at(2)};
auto const keys4 = keys_col<T>{3, 2, 4, 4};
auto const vals1 = vals_col<double>{{0.0 /*NULL*/, 0.0 /*NULL*/, 0.0, 1.0}, nulls_at({0, 1})};
auto const vals2 = vals_col<double>{2.0, 3.0, 4.0};
auto const vals3 = vals_col<double>{NaN, 6.0, 7.0};
auto const vals4 = vals_col<double>{8.0, 9.0, 10.0, NaN};
// The expected results to validate.
auto const expected_keys = keys_col<T>{1, 2, 3, 4};
auto const expected_M2s = M2s_col<R>{18.0, NaN, 18.0, NaN};
// Compute partial results (`COUNT_VALID`, `MEAN`, `M2`) of each dataset.
// The partial results are also assembled into a structs column.
auto const [out1_keys, out1_vals] = compute_partial_results(keys1, vals1);
auto const [out2_keys, out2_vals] = compute_partial_results(keys2, vals2);
auto const [out3_keys, out3_vals] = compute_partial_results(keys3, vals3);
auto const [out4_keys, out4_vals] = compute_partial_results(keys4, vals4);
// Merge the partial results to the final results.
// Merging can be done in just one merge step, or in multiple steps.
// Multiple steps merging:
{
auto const [out5_keys, out5_vals] =
merge_M2(vcol_views{*out1_keys, *out2_keys}, vcol_views{*out1_vals, *out2_vals});
auto const [out6_keys, out6_vals] =
merge_M2(vcol_views{*out3_keys, *out4_keys}, vcol_views{*out3_vals, *out4_vals});
auto const [final_keys, final_vals] =
merge_M2(vcol_views{*out5_keys, *out6_keys}, vcol_views{*out5_vals, *out6_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
// One step merging:
{
auto const [final_keys, final_vals] =
merge_M2(vcol_views{*out1_keys, *out2_keys, *out3_keys, *out4_keys},
vcol_views{*out1_vals, *out2_vals, *out3_vals, *out4_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
}
TYPED_TEST(GroupbyMergeM2TypedTest, SlicedColumnsInput)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// This test should compute M2 aggregation on the same dataset as the InputHaveNullsAndNaNs test.
// i.e.:
//
// keys = [4, 3, 1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4, 4]
// vals = [null, null, 0.0, 1.0, 2.0, 3.0, 4.0, NaN, 6.0, 7.0, 8.0, 9.0, 10.0, NaN]
//
// key = 1: vals = [0, 3, 6]
// key = 2: vals = [1, 4, NaN, 9]
// key = 3: vals = [null, 2, 8]
// key = 4: vals = [null, 10, NaN]
auto const keys_original =
keys_col<T>{{
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, // will not use, don't care
4, 3, 1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4, 4, // use this
1, 2, 3, 4, 5, 1, 2, 3, 4, 5 // will not use, don't care
},
null_at(19)};
auto const vals_original = vals_col<double>{
{
3.0, 2.0, 5.0, 4.0, 6.0, 9.0, 1.0, 0.0, 1.0, 7.0, // will not use, don't care
0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, NaN, 6.0, 7.0, 8.0, 9.0, 10.0, NaN, // use this
9.0, 10.0, 11.0, 12.0, 0.0, 5.0, 1.0, 20.0, 19.0, 15.0 // will not use, don't care
},
nulls_at({10, 11})};
// Partitioned datasets, taken from the original dataset in the range [10, 24).
auto const keys1 = cudf::slice(keys_original, {10, 14})[0]; // {4, 3, 1, 2}
auto const keys2 = cudf::slice(keys_original, {14, 17})[0]; // {3, 1, 2}
auto const keys3 = cudf::slice(keys_original, {17, 20})[0]; // {2, 1, null}
auto const keys4 = cudf::slice(keys_original, {20, 24})[0]; // {3, 2, 4, 4}
auto const vals1 = cudf::slice(vals_original, {10, 14})[0]; // {null, null, 0.0, 1.0}
auto const vals2 = cudf::slice(vals_original, {14, 17})[0]; // {2.0, 3.0, 4.0}
auto const vals3 = cudf::slice(vals_original, {17, 20})[0]; // {NaN, 6.0, 7.0}
auto const vals4 = cudf::slice(vals_original, {20, 24})[0]; // {8.0, 9.0, 10.0, NaN}
// The expected results to validate.
auto const expected_keys = keys_col<T>{1, 2, 3, 4};
auto const expected_M2s = M2s_col<R>{18.0, NaN, 18.0, NaN};
// Compute partial results (`COUNT_VALID`, `MEAN`, `M2`) of each dataset.
// The partial results are also assembled into a structs column.
auto const [out1_keys, out1_vals] = compute_partial_results(keys1, vals1);
auto const [out2_keys, out2_vals] = compute_partial_results(keys2, vals2);
auto const [out3_keys, out3_vals] = compute_partial_results(keys3, vals3);
auto const [out4_keys, out4_vals] = compute_partial_results(keys4, vals4);
// Merge the partial results to the final results.
// Merging can be done in just one merge step, or in multiple steps.
// Multiple steps merging:
{
auto const [out5_keys, out5_vals] =
merge_M2(vcol_views{*out1_keys, *out2_keys}, vcol_views{*out1_vals, *out2_vals});
auto const [out6_keys, out6_vals] =
merge_M2(vcol_views{*out3_keys, *out4_keys}, vcol_views{*out3_vals, *out4_vals});
auto const [final_keys, final_vals] =
merge_M2(vcol_views{*out5_keys, *out6_keys}, vcol_views{*out5_vals, *out6_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
// One step merging:
{
auto const [final_keys, final_vals] =
merge_M2(vcol_views{*out1_keys, *out2_keys, *out3_keys, *out4_keys},
vcol_views{*out1_vals, *out2_vals, *out3_vals, *out4_vals});
auto const out_M2s = final_vals->child(2);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *final_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, out_M2s, verbosity);
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/sum_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_sum_test : public cudf::test::BaseFixture {};
using K = int32_t;
using supported_types =
cudf::test::Concat<cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>,
cudf::test::DurationTypes>;
TYPED_TEST_SUITE(groupby_sum_test, supported_types);
TYPED_TEST(groupby_sum_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals{9, 19, 17};
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_sum_test, empty_cols)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_sum_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_sum_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, cudf::test::iterators::all_nulls());
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_sum_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4},
cudf::test::iterators::no_nulls());
// { 3, 6, 1, 4, 9, 2, 8, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals({9, 14, 10, 0}, {1, 1, 1, 0});
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_sum_test, dictionary)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals{9, 19, 17};
test_single_agg(
keys, vals, expect_keys, expect_vals, cudf::make_sum_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_sum_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
struct overflow_test : public cudf::test::BaseFixture {};
TEST_F(overflow_test, overflow_integer)
{
using int32_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using int64_col = cudf::test::fixed_width_column_wrapper<int64_t>;
auto const keys = int32_col{0, 0};
auto const vals = int32_col{-2147483648, -2147483648};
auto const expect_keys = int32_col{0};
auto const expect_vals = int64_col{-4294967296L};
auto test_sum = [&](auto const use_sort) {
auto agg = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), use_sort);
};
test_sum(force_use_sort_impl::NO);
test_sum(force_use_sort_impl::YES);
}
template <typename T>
struct GroupBySumFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupBySumFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(GroupBySumFixedPointTest, GroupBySortSumDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using K = int32_t;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
// clang-format on
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals_sum = fp_wrapper{{9, 19, 17}, scale};
auto agg1 = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(
keys, vals, expect_keys, expect_vals_sum, std::move(agg1), force_use_sort_impl::YES);
auto agg4 = cudf::make_product_aggregation<cudf::groupby_aggregation>();
EXPECT_THROW(
test_single_agg(keys, vals, expect_keys, {}, std::move(agg4), force_use_sort_impl::YES),
cudf::logic_error);
}
}
TYPED_TEST(GroupBySumFixedPointTest, GroupByHashSumDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using K = int32_t;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
// clang-format on
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals_sum = fp_wrapper{{9, 19, 17}, scale};
auto agg5 = cudf::make_sum_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals_sum, std::move(agg5));
auto agg8 = cudf::make_product_aggregation<cudf::groupby_aggregation>();
EXPECT_THROW(test_single_agg(keys, vals, expect_keys, {}, std::move(agg8)), cudf::logic_error);
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/collect_set_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/groupby.hpp>
#include <cudf/lists/sorting.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table_view.hpp>
namespace {
constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::FIRST_ERROR};
using keys_col = cudf::test::fixed_width_column_wrapper<int32_t, int32_t>;
using strings_col = cudf::test::strings_column_wrapper;
using strings_lists = cudf::test::lists_column_wrapper<cudf::string_view>;
using validity_col = std::initializer_list<bool>;
auto groupby_collect_set(cudf::column_view const& keys,
cudf::column_view const& values,
std::unique_ptr<cudf::groupby_aggregation>&& agg)
{
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = values;
requests[0].aggregations.emplace_back(std::move(agg));
auto const result = cudf::groupby::groupby(cudf::table_view({keys})).aggregate(requests);
auto const result_keys = result.first->view(); // <== table_view of 1 column
auto const result_vals = result.second[0].results[0]->view(); // <== column_view
// Sort the output columns based on the output keys.
// This is to facilitate comparison of the output with the expected columns.
auto keys_vals_sorted = cudf::sort_by_key(cudf::table_view{{result_keys.column(0), result_vals}},
result_keys,
{},
{cudf::null_order::AFTER})
->release();
// After the columns were reordered, individual rows of the output values column (which are lists)
// also need to be sorted.
auto out_values =
cudf::lists::sort_lists(cudf::lists_column_view{keys_vals_sorted.back()->view()},
cudf::order::ASCENDING,
cudf::null_order::AFTER);
return std::pair(std::move(keys_vals_sorted.front()), std::move(out_values));
}
} // namespace
struct CollectSetTest : public cudf::test::BaseFixture {
static auto collect_set()
{
return cudf::make_collect_set_aggregation<cudf::groupby_aggregation>();
}
static auto collect_set_null_unequal()
{
return cudf::make_collect_set_aggregation<cudf::groupby_aggregation>(
cudf::null_policy::INCLUDE, cudf::null_equality::UNEQUAL);
}
static auto collect_set_null_exclude()
{
return cudf::make_collect_set_aggregation<cudf::groupby_aggregation>(
cudf::null_policy::EXCLUDE);
}
};
template <typename V>
struct CollectSetTypedTest : public cudf::test::BaseFixture {};
using FixedWidthTypesNotBool = cudf::test::Concat<cudf::test::IntegralTypesNotBool,
cudf::test::FloatingPointTypes,
cudf::test::TimestampTypes>;
TYPED_TEST_SUITE(CollectSetTypedTest, FixedWidthTypesNotBool);
TYPED_TEST(CollectSetTypedTest, TrivialInput)
{
using vals_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
// Empty input
{
keys_col keys{};
vals_col vals{};
keys_col keys_expected{};
lists_col vals_expected{};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// Single key input
{
keys_col keys{1};
vals_col vals{10};
keys_col keys_expected{1};
lists_col vals_expected{lists_col{10}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// Non-repeated keys
{
keys_col keys{2, 1};
vals_col vals{20, 10};
keys_col keys_expected{1, 2};
lists_col vals_expected{lists_col{10}, lists_col{20}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
}
TYPED_TEST(CollectSetTypedTest, TypicalInput)
{
using vals_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
// Pre-sorted keys
{
keys_col keys{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3};
vals_col vals{10, 11, 10, 10, 20, 21, 21, 20, 30, 33, 32, 31};
keys_col keys_expected{1, 2, 3};
lists_col vals_expected{{10, 11}, {20, 21}, {30, 31, 32, 33}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// Expect the result keys to be sorted by sort-based groupby
{
keys_col keys{4, 1, 2, 4, 3, 3, 2, 1};
vals_col vals{40, 10, 20, 40, 30, 30, 20, 11};
keys_col keys_expected{1, 2, 3, 4};
lists_col vals_expected{{10, 11}, {20}, {30}, {40}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
}
// Keys and values columns are sliced columns
TYPED_TEST(CollectSetTypedTest, SlicedColumnsInput)
{
using vals_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
keys_col keys_original{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3};
vals_col vals_original{10, 11, 10, 10, 20, 21, 21, 20, 30, 33, 32, 31};
{
auto const keys = cudf::slice(keys_original, {0, 4})[0]; // { 1, 1, 1, 1 }
auto const vals = cudf::slice(vals_original, {0, 4})[0]; // { 10, 11, 10, 10 }
auto const keys_expected = keys_col{1};
auto const vals_expected = lists_col{{10, 11}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
{
auto const keys = cudf::slice(keys_original, {2, 10})[0]; // { 1, 1, 2, 2, 2, 2, 3, 3 }
auto const vals = cudf::slice(vals_original, {2, 10})[0]; // { 10, 10, 20, 21, 21, 20, 30, 33 }
auto const keys_expected = keys_col{1, 2, 3};
auto const vals_expected = lists_col{{10}, {20, 21}, {30, 33}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
}
TEST_F(CollectSetTest, StringInput)
{
keys_col keys{1, 2, 3, 3, 2, 1, 2, 1, 2, 1, 1, 1, 1};
strings_col vals{
"String 1, first",
"String 2, first",
"String 3, first",
"String 3, second",
"String 2, second",
"String 1, second",
"String 2, second", // repeated
"String 1, second", // repeated
"String 2, second", // repeated
"String 1, second", // repeated
"String 1, second", // repeated
"String 1, second", // repeated
"String 1, second" // repeated
};
keys_col keys_expected{1, 2, 3};
strings_lists vals_expected{{"String 1, first", "String 1, second"},
{"String 2, first", "String 2, second"},
{"String 3, first", "String 3, second"}};
auto const [out_keys, out_lists] = groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
TEST_F(CollectSetTest, FloatsWithNaN)
{
keys_col keys{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
cudf::test::fixed_width_column_wrapper<float> vals{
{1.0f, 1.0f, -2.3e-5f, -2.3e-5f, 2.3e5f, 2.3e5f, -NAN, -NAN, NAN, NAN, 0.0f, 0.0f},
{true, true, true, true, true, true, true, true, true, true, false, false}};
keys_col keys_expected{1};
cudf::test::lists_column_wrapper<float> vals_expected;
// null equal with nan unequal
{
vals_expected = {{{-2.3e-5f, 1.0f, 2.3e5f, -NAN, -NAN, NAN, NAN, 0.0f},
validity_col{true, true, true, true, true, true, true, false}}};
auto const [out_keys, out_lists] = groupby_collect_set(
keys,
vals,
cudf::make_collect_set_aggregation<cudf::groupby_aggregation>(
cudf::null_policy::INCLUDE, cudf::null_equality::EQUAL, cudf::nan_equality::UNEQUAL));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// null unequal with nan unequal
{
vals_expected = {{{-2.3e-5f, 1.0f, 2.3e5f, -NAN, -NAN, NAN, NAN, 0.0f, 0.0f},
validity_col{true, true, true, true, true, true, true, false, false}}};
auto const [out_keys, out_lists] = groupby_collect_set(
keys,
vals,
cudf::make_collect_set_aggregation<cudf::groupby_aggregation>(
cudf::null_policy::INCLUDE, cudf::null_equality::UNEQUAL, cudf::nan_equality::UNEQUAL));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// null exclude with nan unequal
{
vals_expected = {{-2.3e-5f, 1.0f, 2.3e5f, -NAN, -NAN, NAN, NAN}};
auto const [out_keys, out_lists] = groupby_collect_set(
keys,
vals,
cudf::make_collect_set_aggregation<cudf::groupby_aggregation>(
cudf::null_policy::EXCLUDE, cudf::null_equality::EQUAL, cudf::nan_equality::UNEQUAL));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// null equal with nan equal
{
vals_expected = {
{{-2.3e-5f, 1.0f, 2.3e5f, NAN, 0.0f}, validity_col{true, true, true, true, false}}};
auto const [out_keys, out_lists] = groupby_collect_set(
keys,
vals,
cudf::make_collect_set_aggregation<cudf::groupby_aggregation>(
cudf::null_policy::INCLUDE, cudf::null_equality::EQUAL, cudf::nan_equality::ALL_EQUAL));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// null unequal with nan equal
{
vals_expected = {{{-2.3e-5f, 1.0f, 2.3e5f, -NAN, 0.0f, 0.0f},
validity_col{true, true, true, true, false, false}}};
auto const [out_keys, out_lists] = groupby_collect_set(
keys,
vals,
cudf::make_collect_set_aggregation<cudf::groupby_aggregation>(
cudf::null_policy::INCLUDE, cudf::null_equality::UNEQUAL, cudf::nan_equality::ALL_EQUAL));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
}
TYPED_TEST(CollectSetTypedTest, CollectWithNulls)
{
using vals_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
// Just use an arbitrary value to store null entries
// Using this alias variable will make the code look cleaner
constexpr int32_t null = 0;
// Pre-sorted keys
{
keys_col keys{1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3};
vals_col vals{{10, 10, null, null, 20, null, null, null, 30, 31, 30, 31},
{true, true, false, false, true, false, false, false, true, true, true, true}};
keys_col keys_expected{1, 2, 3};
lists_col vals_expected;
// By default, nulls are consider equals, thus only one null is kept per key
{
vals_expected = {{{10, null}, validity_col{true, false}},
{{20, null}, validity_col{true, false}},
{{30, 31}, validity_col{true, true}}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// All nulls per key are kept (nulls are put at the end of each list)
{
vals_expected = lists_col{{{10, null, null}, validity_col{true, false, false}},
{{20, null, null, null}, validity_col{true, false, false, false}},
{{30, 31}, validity_col{true, true}}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set_null_unequal());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// All nulls per key are excluded
{
vals_expected = lists_col{{10}, {20}, {30, 31}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set_null_exclude());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
}
// Expect the result keys to be sorted by sort-based groupby
{
keys_col keys{4, 1, 2, 4, 3, 3, 3, 3, 2, 1};
vals_col vals{{40, 10, 20, 40, null, null, null, null, 21, null},
{true, true, true, true, false, false, false, false, true, false}};
keys_col keys_expected{1, 2, 3, 4};
lists_col vals_expected;
// By default, nulls are consider equals, thus only one null is kept per key
{
vals_expected = {{{10, null}, validity_col{true, false}},
{{20, 21}, validity_col{true, true}},
{{null}, validity_col{false}},
{{40}, validity_col{true}}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// All nulls per key are kept (nulls are put at the end of each list)
{
vals_expected =
lists_col{{{10, null}, validity_col{true, false}},
{{20, 21}, validity_col{true, true}},
{{null, null, null, null}, validity_col{false, false, false, false}},
{{40}, validity_col{true}}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set_null_unequal());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
// All nulls per key are excluded
{
vals_expected = lists_col{{10}, {20, 21}, {}, {40}};
auto const [out_keys, out_lists] =
groupby_collect_set(keys, vals, CollectSetTest::collect_set_null_exclude());
CUDF_TEST_EXPECT_COLUMNS_EQUAL(keys_expected, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(vals_expected, *out_lists, verbosity);
}
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/histogram_tests.cpp
|
/*
* Copyright (c) 2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/groupby.hpp>
#include <cudf/lists/sorting.hpp>
#include <cudf/sorting.hpp>
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using int64s_col = cudf::test::fixed_width_column_wrapper<int64_t>;
using structs_col = cudf::test::structs_column_wrapper;
auto groupby_histogram(cudf::column_view const& keys,
cudf::column_view const& values,
cudf::aggregation::Kind agg_kind)
{
CUDF_EXPECTS(
agg_kind == cudf::aggregation::HISTOGRAM || agg_kind == cudf::aggregation::MERGE_HISTOGRAM,
"Aggregation must be either HISTOGRAM or MERGE_HISTOGRAM.");
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back();
requests[0].values = values;
if (agg_kind == cudf::aggregation::HISTOGRAM) {
requests[0].aggregations.push_back(
cudf::make_histogram_aggregation<cudf::groupby_aggregation>());
} else {
requests[0].aggregations.push_back(
cudf::make_merge_histogram_aggregation<cudf::groupby_aggregation>());
}
auto gb_obj = cudf::groupby::groupby(cudf::table_view({keys}));
auto const agg_results = gb_obj.aggregate(requests, cudf::test::get_default_stream());
auto const agg_histogram = agg_results.second[0].results[0]->view();
EXPECT_EQ(agg_histogram.type().id(), cudf::type_id::LIST);
EXPECT_EQ(agg_histogram.null_count(), 0);
auto const histograms = cudf::lists_column_view{agg_histogram}.child();
EXPECT_EQ(histograms.num_children(), 2);
EXPECT_EQ(histograms.null_count(), 0);
EXPECT_EQ(histograms.child(1).null_count(), 0);
auto const key_sort_order = cudf::sorted_order(agg_results.first->view(), {}, {});
auto sorted_keys =
std::move(cudf::gather(agg_results.first->view(), *key_sort_order)->release().front());
auto const sorted_vals =
std::move(cudf::gather(cudf::table_view{{agg_histogram}}, *key_sort_order)->release().front());
auto sorted_histograms = cudf::lists::sort_lists(cudf::lists_column_view{*sorted_vals},
cudf::order::ASCENDING,
cudf::null_order::BEFORE,
cudf::get_default_stream(),
rmm::mr::get_current_device_resource());
return std::pair{std::move(sorted_keys), std::move(sorted_histograms)};
}
template <typename T>
struct GroupbyHistogramTest : public cudf::test::BaseFixture {};
template <typename T>
struct GroupbyMergeHistogramTest : public cudf::test::BaseFixture {};
// Avoid unsigned types, as the tests below have negative values in their input.
using HistogramTestTypes = cudf::test::Concat<cudf::test::Types<int8_t, int16_t, int32_t, int64_t>,
cudf::test::FloatingPointTypes,
cudf::test::FixedPointTypes,
cudf::test::ChronoTypes>;
TYPED_TEST_SUITE(GroupbyHistogramTest, HistogramTestTypes);
TYPED_TEST_SUITE(GroupbyMergeHistogramTest, HistogramTestTypes);
TYPED_TEST(GroupbyHistogramTest, EmptyInput)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
auto const keys = int32s_col{};
auto const values = col_data{};
auto const [res_keys, res_histogram] =
groupby_histogram(keys, values, cudf::aggregation::HISTOGRAM);
// The structure of the output is already verified in the function `groupby_histogram`.
ASSERT_EQ(res_histogram->size(), 0);
}
TYPED_TEST(GroupbyHistogramTest, SimpleInputNoNull)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
// key = 0: values = [2, 2, -3, -2, 2]
// key = 1: values = [2, 0, 5, 2, 1]
// key = 2: values = [-3, 1, 1, 2, 2]
auto const keys = int32s_col{2, 0, 2, 1, 1, 1, 0, 0, 0, 1, 2, 2, 1, 0, 2};
auto const values = col_data{-3, 2, 1, 2, 0, 5, 2, -3, -2, 2, 1, 2, 1, 2, 2};
auto const expected_keys = int32s_col{0, 1, 2};
auto const expected_histogram = [] {
auto structs = [] {
auto values = col_data{-3, -2, 2, 0, 1, 2, 5, -3, 1, 2};
auto counts = int64s_col{1, 1, 3, 1, 1, 2, 1, 1, 2, 2};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
3, int32s_col{0, 3, 7, 10}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, values, cudf::aggregation::HISTOGRAM);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *res_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_histogram, *res_histogram);
}
TYPED_TEST(GroupbyHistogramTest, SlicedInputNoNull)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
auto const keys_original = int32s_col{2, 0, 2, 1, 0, 2, 0, 2, 1, 1, 1, 0, 0, 0, 1, 2, 2, 1, 0, 2};
auto const values_original =
col_data{1, 2, 0, 2, 1, -3, 2, 1, 2, 0, 5, 2, -3, -2, 2, 1, 2, 1, 2, 2};
// key = 0: values = [2, 2, -3, -2, 2]
// key = 1: values = [2, 0, 5, 2, 1]
// key = 2: values = [-3, 1, 1, 2, 2]
auto const keys = cudf::slice(keys_original, {5, 20})[0];
auto const values = cudf::slice(values_original, {5, 20})[0];
auto const expected_keys = int32s_col{0, 1, 2};
auto const expected_histogram = [] {
auto structs = [] {
auto values = col_data{-3, -2, 2, 0, 1, 2, 5, -3, 1, 2};
auto counts = int64s_col{1, 1, 3, 1, 1, 2, 1, 1, 2, 2};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
3, int32s_col{0, 3, 7, 10}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, values, cudf::aggregation::HISTOGRAM);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *res_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_histogram, *res_histogram);
}
TYPED_TEST(GroupbyHistogramTest, InputWithNulls)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
using namespace cudf::test::iterators;
auto constexpr null{0};
// key = 0: values = [-3, null, 2, null, 2]
// key = 1: values = [1, 2, null, 5, 2, -3, 1, 1]
// key = 2: values = [null, 2, 0, -2, 2, null, 2]
auto const keys = int32s_col{2, 0, 2, 1, 1, 1, 2, 1, 1, 0, 1, 2, 0, 0, 1, 2, 2, 1, 0, 2};
auto const values =
col_data{{null, -3, 2, 1, 2, null, 0, 5, 2, null, -3, -2, 2, null, 1, 2, null, 1, 2, 2},
nulls_at({0, 5, 9, 13, 16})};
auto const expected_keys = int32s_col{0, 1, 2};
auto const expected_histogram = [] {
auto structs = [] {
auto values = col_data{{null, -3, 2, null, -3, 1, 2, 5, null, -2, 0, 2}, nulls_at({0, 3, 8})};
auto counts = int64s_col{2, 1, 2, 1, 1, 3, 2, 1, 2, 1, 1, 3};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
3, int32s_col{0, 3, 8, 12}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, values, cudf::aggregation::HISTOGRAM);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *res_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_histogram, *res_histogram);
}
TYPED_TEST(GroupbyHistogramTest, SlicedInputWithNulls)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
using namespace cudf::test::iterators;
auto constexpr null{0};
auto const keys_original =
int32s_col{1, 0, 2, 2, 0, 2, 0, 2, 1, 1, 1, 2, 1, 1, 0, 1, 2, 0, 0, 1, 2, 2, 1, 0, 2, 0, 1, 2};
auto const values_original =
col_data{{null, 1, 1, 2, 1, null, -3, 2, 1, 2, null, 0, 5, 2,
null, -3, -2, 2, null, 1, 2, null, 1, 2, 2, null, 1, 2},
nulls_at({0, 5, 10, 14, 18, 21, 25})};
// key = 0: values = [-3, null, 2, null, 2]
// key = 1: values = [1, 2, null, 5, 2, -3, 1, 1]
// key = 2: values = [null, 2, 0, -2, 2, null, 2]
auto const keys = cudf::slice(keys_original, {5, 25})[0];
auto const values = cudf::slice(values_original, {5, 25})[0];
auto const expected_keys = int32s_col{0, 1, 2};
auto const expected_histogram = [] {
auto structs = [] {
auto values = col_data{{null, -3, 2, null, -3, 1, 2, 5, null, -2, 0, 2}, nulls_at({0, 3, 8})};
auto counts = int64s_col{2, 1, 2, 1, 1, 3, 2, 1, 2, 1, 1, 3};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
3, int32s_col{0, 3, 8, 12}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, values, cudf::aggregation::HISTOGRAM);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *res_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_histogram, *res_histogram);
}
TYPED_TEST(GroupbyMergeHistogramTest, EmptyInput)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
auto const keys = int32s_col{};
auto const values = [] {
auto structs = [] {
auto values = col_data{};
auto counts = int64s_col{};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
0, int32s_col{}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, *values, cudf::aggregation::MERGE_HISTOGRAM);
// The structure of the output is already verified in the function `groupby_histogram`.
ASSERT_EQ(res_histogram->size(), 0);
}
TYPED_TEST(GroupbyMergeHistogramTest, SimpleInputNoNull)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
// key = 0: histograms = [[<-3, 1>, <-2, 1>, <2, 3>], [<0, 1>, <1, 1>], [<-3, 3>, <0, 1>, <1, 2>]]
// key = 1: histograms = [[<-2, 1>, <1, 3>, <2, 2>], [<0, 2>, <1, 1>, <2, 2>]]
auto const keys = int32s_col{0, 1, 0, 1, 0};
auto const values = [] {
auto structs = [] {
auto values = col_data{-3, -2, 2, -2, 1, 2, 0, 1, 0, 1, 2, -3, 0, 1};
auto counts = int64s_col{1, 1, 3, 1, 3, 2, 1, 1, 2, 1, 2, 3, 1, 2};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
5, int32s_col{0, 3, 6, 8, 11, 14}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const expected_keys = int32s_col{0, 1};
auto const expected_histogram = [] {
auto structs = [] {
auto values = col_data{-3, -2, 0, 1, 2, -2, 0, 1, 2};
auto counts = int64s_col{4, 1, 2, 3, 3, 1, 2, 4, 4};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
2, int32s_col{0, 5, 9}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, *values, cudf::aggregation::MERGE_HISTOGRAM);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *res_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_histogram, *res_histogram);
}
TYPED_TEST(GroupbyMergeHistogramTest, SlicedInputNoNull)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
// key = 0: histograms = [[<-3, 1>, <-2, 1>, <2, 3>], [<0, 1>, <1, 1>], [<-3, 3>, <0, 1>, <1, 2>]]
// key = 1: histograms = [[<-2, 1>, <1, 3>, <2, 2>], [<0, 2>, <1, 1>, <2, 2>]]
auto const keys_original = int32s_col{0, 1, 0, 1, 0, 1, 0};
auto const values_original = [] {
auto structs = [] {
auto values = col_data{0, 2, -3, 1, -3, -2, 2, -2, 1, 2, 0, 1, 0, 1, 2, -3, 0, 1};
auto counts = int64s_col{1, 2, 3, 1, 1, 1, 3, 1, 3, 2, 1, 1, 2, 1, 2, 3, 1, 2};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(7,
int32s_col{0, 2, 4, 7, 10, 12, 15, 18}.release(),
structs.release(),
0,
rmm::device_buffer{});
}();
auto const keys = cudf::slice(keys_original, {2, 7})[0];
auto const values = cudf::slice(*values_original, {2, 7})[0];
auto const expected_keys = int32s_col{0, 1};
auto const expected_histogram = [] {
auto structs = [] {
auto values = col_data{-3, -2, 0, 1, 2, -2, 0, 1, 2};
auto counts = int64s_col{4, 1, 2, 3, 3, 1, 2, 4, 4};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
2, int32s_col{0, 5, 9}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, values, cudf::aggregation::MERGE_HISTOGRAM);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *res_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_histogram, *res_histogram);
}
TYPED_TEST(GroupbyMergeHistogramTest, InputWithNulls)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
using namespace cudf::test::iterators;
auto constexpr null{0};
// key = 0: histograms = [[<null, 1>, <2, 3>], [<null, 2>, <1, 1>], [<0, 1>, <1, 2>]]
// key = 1: histograms = [[<null, 1>, <1, 3>, <2, 2>], [<0, 2>, <1, 1>, <2, 2>]]
auto const keys = int32s_col{0, 1, 1, 0, 0};
auto const values = [] {
auto structs = [] {
auto values = col_data{{null, 2, null, 1, 2, 0, 1, 2, null, 1, 0, 1}, nulls_at({0, 2, 8})};
auto counts = int64s_col{1, 3, 1, 3, 2, 2, 1, 2, 2, 1, 1, 2};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
5, int32s_col{0, 2, 5, 8, 10, 12}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const expected_keys = int32s_col{0, 1};
auto const expected_histogram = [] {
auto structs = [] {
auto values = col_data{{null, 0, 1, 2, null, 0, 1, 2}, nulls_at({0, 4})};
auto counts = int64s_col{3, 1, 3, 3, 1, 2, 4, 4};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
2, int32s_col{0, 4, 8}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, *values, cudf::aggregation::MERGE_HISTOGRAM);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *res_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_histogram, *res_histogram);
}
TYPED_TEST(GroupbyMergeHistogramTest, SlicedInputWithNulls)
{
using col_data = cudf::test::fixed_width_column_wrapper<TypeParam, int>;
using namespace cudf::test::iterators;
auto constexpr null{0};
// key = 0: histograms = [[<null, 1>, <2, 3>], [<null, 2>, <1, 1>], [<0, 1>, <1, 2>]]
// key = 1: histograms = [[<null, 1>, <1, 3>, <2, 2>], [<0, 2>, <1, 1>, <2, 2>]]
auto const keys_original = int32s_col{0, 1, 0, 1, 1, 0, 0};
auto const values_original = [] {
auto structs = [] {
auto values = col_data{{null, 2, null, 1, null, 2, null, 1, 2, 0, 1, 2, null, 1, 0, 1},
nulls_at({0, 2, 4, 6, 12})};
auto counts = int64s_col{1, 3, 2, 1, 1, 3, 1, 3, 2, 2, 1, 2, 2, 1, 1, 2};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(7,
int32s_col{0, 2, 4, 6, 9, 12, 14, 16}.release(),
structs.release(),
0,
rmm::device_buffer{});
}();
auto const keys = cudf::slice(keys_original, {2, 7})[0];
auto const values = cudf::slice(*values_original, {2, 7})[0];
auto const expected_keys = int32s_col{0, 1};
auto const expected_histogram = [] {
auto structs = [] {
auto values = col_data{{null, 0, 1, 2, null, 0, 1, 2}, nulls_at({0, 4})};
auto counts = int64s_col{3, 1, 3, 3, 1, 2, 4, 4};
return structs_col{{values, counts}};
}();
return cudf::make_lists_column(
2, int32s_col{0, 4, 8}.release(), structs.release(), 0, rmm::device_buffer{});
}();
auto const [res_keys, res_histogram] =
groupby_histogram(keys, values, cudf::aggregation::MERGE_HISTOGRAM);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *res_keys);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_histogram, *res_histogram);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/sum_of_squares_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_sum_of_squares_test : public cudf::test::BaseFixture {};
using supported_types = cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>;
TYPED_TEST_SUITE(groupby_sum_of_squares_test, supported_types);
TYPED_TEST(groupby_sum_of_squares_test, basic)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM_OF_SQUARES>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// { 1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
// { 0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({45., 123., 117.}, no_nulls());
auto agg = cudf::make_sum_of_squares_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_of_squares_test, empty_cols)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM_OF_SQUARES>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_sum_of_squares_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_of_squares_test, zero_valid_keys)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM_OF_SQUARES>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_sum_of_squares_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_of_squares_test, zero_valid_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM_OF_SQUARES>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_sum_of_squares_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_of_squares_test, null_keys_and_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM_OF_SQUARES>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 3, 6, 1, 4, 9, 2, 8, 3}
cudf::test::fixed_width_column_wrapper<R> expect_vals({45., 98., 68., 9.}, {1, 1, 1, 0});
auto agg = cudf::make_sum_of_squares_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_of_squares_test, dictionary)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM_OF_SQUARES>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3 });
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({45., 123., 117. }, no_nulls());
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_sum_of_squares_aggregation<cudf::groupby_aggregation>());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/groups_tests.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/groupby.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
void test_groups(cudf::column_view const& keys,
cudf::column_view const& expect_grouped_keys,
std::vector<cudf::size_type> const& expect_group_offsets,
cudf::column_view const& values = {},
cudf::column_view const& expect_grouped_values = {})
{
cudf::groupby::groupby gb(cudf::table_view({keys}));
cudf::groupby::groupby::groups gb_groups;
if (values.size()) {
gb_groups = gb.get_groups(cudf::table_view({values}));
} else {
gb_groups = gb.get_groups();
}
CUDF_TEST_EXPECT_TABLES_EQUAL(cudf::table_view({expect_grouped_keys}), gb_groups.keys->view());
auto got_offsets = gb_groups.offsets;
EXPECT_EQ(expect_group_offsets.size(), got_offsets.size());
for (auto i = 0u; i != expect_group_offsets.size(); ++i) {
EXPECT_EQ(expect_group_offsets[i], got_offsets[i]);
}
if (values.size()) {
CUDF_TEST_EXPECT_TABLES_EQUAL(cudf::table_view({expect_grouped_values}),
gb_groups.values->view());
}
}
struct groupby_group_keys_test : public cudf::test::BaseFixture {};
template <typename V>
struct groupby_group_keys_and_values_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_group_keys_and_values_test, cudf::test::NumericTypes);
TEST_F(groupby_group_keys_test, basic)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 2, 1, 2, 3};
cudf::test::fixed_width_column_wrapper<K> expect_grouped_keys{1, 1, 1, 2, 2, 3};
std::vector<cudf::size_type> expect_group_offsets = {0, 3, 5, 6};
test_groups(keys, expect_grouped_keys, expect_group_offsets);
}
TEST_F(groupby_group_keys_test, empty_keys)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<K> expect_grouped_keys{};
std::vector<cudf::size_type> expect_group_offsets = {0};
test_groups(keys, expect_grouped_keys, expect_group_offsets);
}
TEST_F(groupby_group_keys_test, all_null_keys)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> keys({1, 1, 2, 3, 1, 2},
cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_grouped_keys{};
std::vector<cudf::size_type> expect_group_offsets = {0};
test_groups(keys, expect_grouped_keys, expect_group_offsets);
}
TYPED_TEST(groupby_group_keys_and_values_test, basic_with_values)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> keys({5, 4, 3, 2, 1, 0});
cudf::test::fixed_width_column_wrapper<K> expect_grouped_keys{0, 1, 2, 3, 4, 5};
cudf::test::fixed_width_column_wrapper<V> values({0, 0, 1, 1, 2, 2});
cudf::test::fixed_width_column_wrapper<V> expect_grouped_values{2, 2, 1, 1, 0, 0};
std::vector<cudf::size_type> expect_group_offsets = {0, 1, 2, 3, 4, 5, 6};
test_groups(keys, expect_grouped_keys, expect_group_offsets, values, expect_grouped_values);
}
TYPED_TEST(groupby_group_keys_and_values_test, some_nulls)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> keys({1, 1, 3, 2, 1, 2}, {1, 0, 1, 0, 0, 1});
cudf::test::fixed_width_column_wrapper<K> expect_grouped_keys({1, 2, 3},
cudf::test::iterators::no_nulls());
cudf::test::fixed_width_column_wrapper<V> values({1, 2, 3, 4, 5, 6});
cudf::test::fixed_width_column_wrapper<V> expect_grouped_values({1, 6, 3});
std::vector<cudf::size_type> expect_group_offsets = {0, 1, 2, 3};
test_groups(keys, expect_grouped_keys, expect_group_offsets, values, expect_grouped_values);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/mean_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <initializer_list>
#include <vector>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_mean_test : public cudf::test::BaseFixture {};
template <typename Target, typename Source>
std::vector<Target> convert(std::initializer_list<Source> in)
{
std::vector<Target> out(std::cbegin(in), std::cend(in));
return out;
}
using supported_types =
cudf::test::Concat<cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>,
cudf::test::DurationTypes>;
TYPED_TEST_SUITE(groupby_mean_test, supported_types);
using K = int32_t;
TYPED_TEST(groupby_mean_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEAN>;
using RT = typename std::conditional<cudf::is_duration<R>(), int, double>::type;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// clang-format off
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
std::vector<RT> expect_v = convert<RT>( {3., 19. / 4, 17. / 3});
cudf::test::fixed_width_column_wrapper<R, RT> expect_vals(expect_v.cbegin(), expect_v.cend());
// clang-format on
auto agg = cudf::make_mean_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_mean_test, empty_cols)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEAN>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_mean_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_mean_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEAN>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_mean_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_mean_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEAN>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_mean_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_mean_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEAN>;
using RT = typename std::conditional<cudf::is_duration<R>(), int, double>::type;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// clang-format off
// {1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// {3, 6, 1, 4, 9, 2, 8, -}
std::vector<RT> expect_v = convert<RT>( {4.5, 14. / 3, 5., 0.});
// clang-format on
cudf::test::fixed_width_column_wrapper<R, RT> expect_vals(
expect_v.cbegin(), expect_v.cend(), {1, 1, 1, 0});
auto agg = cudf::make_mean_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
// clang-format on
struct groupby_dictionary_mean_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_mean_test, basic)
{
using V = int16_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEAN>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<K> expect_keys( {1, 2, 3});
cudf::test::fixed_width_column_wrapper<R, double> expect_vals({9. / 3, 19. / 4, 17. / 3});
// clang-format on
test_single_agg(
keys, vals, expect_keys, expect_vals, cudf::make_mean_aggregation<cudf::groupby_aggregation>());
}
template <typename T>
struct FixedPointTestBothReps : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(FixedPointTestBothReps, cudf::test::FixedPointTypes);
TYPED_TEST(FixedPointTestBothReps, GroupBySortMeanDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
// clang-format on
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals_min = fp_wrapper{{3, 4, 5}, scale};
auto agg = cudf::make_mean_aggregation<cudf::groupby_aggregation>();
test_single_agg(
keys, vals, expect_keys, expect_vals_min, std::move(agg), force_use_sort_impl::YES);
}
}
TYPED_TEST(FixedPointTestBothReps, GroupByHashMeanDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using K = int32_t;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
// clang-format on
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals_min = fp_wrapper{{3, 4, 5}, scale};
auto agg = cudf::make_mean_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals_min, std::move(agg));
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/shift_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/fixed_point/fixed_point.hpp>
#include <cudf/groupby.hpp>
#include <cudf/scalar/scalar.hpp>
#include <cudf/scalar/scalar_factories.hpp>
template <typename T>
struct groupby_shift_fixed_width_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_shift_fixed_width_test, cudf::test::FixedWidthTypes);
template <typename K, typename V>
void test_groupby_shift_fixed_width_single(
cudf::test::fixed_width_column_wrapper<K> const& key,
cudf::test::fixed_width_column_wrapper<V> const& value,
cudf::size_type offset,
cudf::scalar const& fill_value,
cudf::test::fixed_width_column_wrapper<V> const& expected)
{
cudf::groupby::groupby gb_obj(cudf::table_view({key}));
std::vector<cudf::size_type> offsets{offset};
auto got = gb_obj.shift(cudf::table_view{{value}}, offsets, {fill_value});
CUDF_TEST_EXPECT_COLUMNS_EQUAL((*got.second).view().column(0), expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, ForwardShiftWithoutNull_NullScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::fixed_width_column_wrapper<V> val{3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<V> expected({-1, -1, 3, 5, -1, -1, 4},
{0, 0, 1, 1, 0, 0, 1});
cudf::size_type offset = 2;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, *slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, ForwardShiftWithNull_NullScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::fixed_width_column_wrapper<V> val({3, 4, 5, 6, 7, 8, 9}, {0, 0, 0, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> expected({-1, -1, -1, -1, -1, -1, -1},
{0, 0, 0, 0, 0, 0, 0});
cudf::size_type offset = 2;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, *slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, ForwardShiftWithoutNull_ValidScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1};
cudf::test::fixed_width_column_wrapper<V> val({3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5});
cudf::test::fixed_width_column_wrapper<V> expected({42, 42, 42, 3, 5, 8, 9, 42, 42, 42, 4, 6, 7});
cudf::size_type offset = 3;
auto slr =
cudf::scalar_type_t<TypeParam>(cudf::test::make_type_param_scalar<TypeParam>(42), true);
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, ForwardShiftWithNull_ValidScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1};
cudf::test::fixed_width_column_wrapper<V> val({3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5},
{1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1});
cudf::test::fixed_width_column_wrapper<V> expected(
{42, 42, 42, 3, 5, -1, -1, 42, 42, 42, -1, -1, 7}, {1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1});
cudf::size_type offset = 3;
auto slr =
cudf::scalar_type_t<TypeParam>(cudf::test::make_type_param_scalar<TypeParam>(42), true);
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, BackwardShiftWithoutNull_NullScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::fixed_width_column_wrapper<V> val{3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<V> expected({5, 8, 9, -1, 6, 7, -1},
{1, 1, 1, 0, 1, 1, 0});
cudf::size_type offset = -1;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, *slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, BackwardShiftWithNull_NullScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::fixed_width_column_wrapper<V> val({3, 4, 5, 6, 7, 8, 9}, {0, 0, 0, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> expected({-1, 8, 9, -1, 6, 7, -1},
{0, 1, 1, 0, 1, 1, 0});
cudf::size_type offset = -1;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, *slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, BackwardShiftWithoutNull_ValidScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1};
cudf::test::fixed_width_column_wrapper<V> val{3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
cudf::test::fixed_width_column_wrapper<V> expected(
{3, 5, 42, 42, 42, 42, 42, 4, 42, 42, 42, 42, 42});
cudf::size_type offset = -5;
auto slr =
cudf::scalar_type_t<TypeParam>(cudf::test::make_type_param_scalar<TypeParam>(42), true);
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, BackwardShiftWithNull_ValidScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1};
cudf::test::fixed_width_column_wrapper<V> val({3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5},
{1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 1});
cudf::test::fixed_width_column_wrapper<V> expected({5, -1, -1, -1, 3, 5, 42, -1, 7, 0, 2, -1, 42},
{1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1});
cudf::size_type offset = -1;
auto slr =
cudf::scalar_type_t<TypeParam>(cudf::test::make_type_param_scalar<TypeParam>(42), true);
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, ZeroShiftNullScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::fixed_width_column_wrapper<V> val{3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<V> expected({3, 5, 8, 9, 4, 6, 7});
cudf::size_type offset = 0;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, *slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, ZeroShiftValidScalar)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1};
cudf::test::fixed_width_column_wrapper<V> val{3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
cudf::test::fixed_width_column_wrapper<V> expected({3, 5, 8, 9, 1, 3, 5, 4, 6, 7, 0, 2, 4});
cudf::size_type offset = 0;
auto slr =
cudf::scalar_type_t<TypeParam>(cudf::test::make_type_param_scalar<TypeParam>(42), true);
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, VeryLargeForwardOffset)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1};
cudf::test::fixed_width_column_wrapper<V> val{3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
cudf::test::fixed_width_column_wrapper<V> expected(
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
cudf::size_type offset = 1024;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, *slr, expected);
}
TYPED_TEST(groupby_shift_fixed_width_test, VeryLargeBackwardOffset)
{
using K = int32_t;
using V = TypeParam;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1, 2, 1, 2, 1, 2, 1};
cudf::test::fixed_width_column_wrapper<V> val{3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5};
cudf::test::fixed_width_column_wrapper<V> expected(
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
cudf::size_type offset = -1024;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_fixed_width_single<K, V>(key, val, offset, *slr, expected);
}
struct groupby_shift_string_test : public cudf::test::BaseFixture {};
template <typename K>
void test_groupby_shift_string_single(cudf::test::fixed_width_column_wrapper<K> const& key,
cudf::test::strings_column_wrapper const& value,
cudf::size_type offset,
cudf::scalar const& fill_value,
cudf::test::strings_column_wrapper const& expected)
{
cudf::groupby::groupby gb_obj(cudf::table_view({key}));
std::vector<cudf::size_type> offsets{offset};
auto got = gb_obj.shift(cudf::table_view{{value}}, offsets, {fill_value});
CUDF_TEST_EXPECT_COLUMNS_EQUAL((*got.second).view().column(0), expected);
}
TEST_F(groupby_shift_string_test, ForwardShiftWithoutNull_NullScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::strings_column_wrapper expected({"", "a", "cc", "f", "", "bb", "d"},
{0, 1, 1, 1, 0, 1, 1});
cudf::size_type offset = 1;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, ForwardShiftWithNull_NullScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val({"a", "bb", "cc", "d", "eee", "f", "gg"},
{1, 0, 1, 1, 0, 0, 0});
cudf::test::strings_column_wrapper expected({"", "", "a", "cc", "", "", ""},
{0, 0, 1, 1, 0, 0, 0});
cudf::size_type offset = 2;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, ForwardShiftWithoutNull_ValidScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::strings_column_wrapper expected({"42", "42", "a", "cc", "42", "42", "bb"});
cudf::size_type offset = 2;
auto slr = cudf::make_string_scalar("42");
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, ForwardShiftWithNull_ValidScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val({"a", "bb", "cc", "d", "eee", "f", "gg"},
{1, 1, 0, 0, 1, 0, 1});
cudf::test::strings_column_wrapper expected({"42", "a", "", "", "42", "bb", ""},
{1, 1, 0, 0, 1, 1, 0});
cudf::size_type offset = 1;
auto slr = cudf::make_string_scalar("42");
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, BackwardShiftWithoutNull_NullScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::strings_column_wrapper expected({"gg", "", "", "", "", "", ""},
{1, 0, 0, 0, 0, 0, 0});
cudf::size_type offset = -3;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, BackwardShiftWithNull_NullScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val({"a", "bb", "cc", "d", "eee", "f", "gg"},
{1, 0, 1, 1, 0, 0, 0});
cudf::test::strings_column_wrapper expected({"cc", "", "", "", "d", "", ""},
{1, 0, 0, 0, 1, 0, 0});
cudf::size_type offset = -1;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, BackwardShiftWithoutNull_ValidScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::strings_column_wrapper expected({"42", "42", "42", "42", "42", "42", "42"});
cudf::size_type offset = -4;
auto slr = cudf::make_string_scalar("42");
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, BackwardShiftWithNull_ValidScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val({"a", "bb", "cc", "d", "eee", "f", "gg"},
{1, 1, 0, 0, 1, 0, 1});
cudf::test::strings_column_wrapper expected({"", "gg", "42", "42", "eee", "42", "42"},
{0, 1, 1, 1, 1, 1, 1});
cudf::size_type offset = -2;
auto slr = cudf::make_string_scalar("42");
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, ZeroShiftNullScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::strings_column_wrapper expected({"a", "cc", "f", "gg", "bb", "d", "eee"});
cudf::size_type offset = 0;
auto slr = cudf::make_default_constructed_scalar(cudf::column_view(val).type());
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, ZeroShiftValidScalar)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::strings_column_wrapper expected({"a", "cc", "f", "gg", "bb", "d", "eee"});
cudf::size_type offset = 0;
auto slr = cudf::make_string_scalar("42");
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, VeryLargeForwardOffset)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::strings_column_wrapper expected({"42", "42", "42", "42", "42", "42", "42"});
cudf::size_type offset = 1024;
auto slr = cudf::make_string_scalar("42");
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
TEST_F(groupby_shift_string_test, VeryLargeBackwardOffset)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper val{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::strings_column_wrapper expected({"42", "42", "42", "42", "42", "42", "42"});
cudf::size_type offset = -1024;
auto slr = cudf::make_string_scalar("42");
test_groupby_shift_string_single(key, val, offset, *slr, expected);
}
template <typename T>
struct groupby_shift_mixed_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_shift_mixed_test, cudf::test::FixedWidthTypes);
template <typename K>
void test_groupby_shift_multi(cudf::test::fixed_width_column_wrapper<K> const& key,
cudf::table_view const& value,
std::vector<cudf::size_type> offsets,
std::vector<std::reference_wrapper<const cudf::scalar>> fill_values,
cudf::table_view const& expected)
{
cudf::groupby::groupby gb_obj(cudf::table_view({key}));
auto got = gb_obj.shift(value, offsets, fill_values);
CUDF_TEST_EXPECT_TABLES_EQUAL((*got.second).view(), expected);
}
TYPED_TEST(groupby_shift_mixed_test, NoFill)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper v1{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::fixed_width_column_wrapper<TypeParam> v2{1, 2, 3, 4, 5, 6, 7};
cudf::table_view value{{v1, v2}};
cudf::test::strings_column_wrapper e1({"", "", "a", "cc", "", "", "bb"}, {0, 0, 1, 1, 0, 0, 1});
cudf::test::fixed_width_column_wrapper<TypeParam> e2({-1, 1, 3, 6, -1, 2, 4},
{0, 1, 1, 1, 0, 1, 1});
cudf::table_view expected{{e1, e2}};
std::vector<cudf::size_type> offset{2, 1};
auto slr1 = cudf::make_default_constructed_scalar(cudf::column_view(v1).type());
auto slr2 = cudf::make_default_constructed_scalar(cudf::column_view(v2).type());
std::vector<std::reference_wrapper<const cudf::scalar>> fill_values{*slr1, *slr2};
test_groupby_shift_multi(key, value, offset, fill_values, expected);
}
TYPED_TEST(groupby_shift_mixed_test, Fill)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{1, 2, 1, 2, 2, 1, 1};
cudf::test::strings_column_wrapper v1{"a", "bb", "cc", "d", "eee", "f", "gg"};
cudf::test::fixed_width_column_wrapper<TypeParam> v2{1, 2, 3, 4, 5, 6, 7};
cudf::table_view value{{v1, v2}};
cudf::test::strings_column_wrapper e1({"cc", "f", "gg", "42", "d", "eee", "42"});
cudf::test::fixed_width_column_wrapper<TypeParam> e2({6, 7, 42, 42, 5, 42, 42});
cudf::table_view expected{{e1, e2}};
std::vector<cudf::size_type> offset{-1, -2};
auto slr1 = cudf::make_string_scalar("42");
auto slr2 =
cudf::scalar_type_t<TypeParam>(cudf::test::make_type_param_scalar<TypeParam>(42), true);
std::vector<std::reference_wrapper<const cudf::scalar>> fill_values{*slr1, slr2};
test_groupby_shift_multi(key, value, offset, fill_values, expected);
}
struct groupby_shift_fixed_point_type_test : public cudf::test::BaseFixture {};
TEST_F(groupby_shift_fixed_point_type_test, Matching)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{2, 3, 4, 4, 3, 2, 2, 4};
cudf::test::fixed_point_column_wrapper<int32_t> v1{{10, 10, 40, 40, 20, 20, 30, 40},
numeric::scale_type{-1}};
cudf::test::fixed_point_column_wrapper<int64_t> v2{{5, 5, 8, 8, 6, 7, 9, 7},
numeric::scale_type{3}};
cudf::table_view value{{v1, v2}};
std::vector<cudf::size_type> offset{-3, 1};
auto slr1 = cudf::make_fixed_point_scalar<numeric::decimal32>(-42, numeric::scale_type{-1});
auto slr2 = cudf::make_fixed_point_scalar<numeric::decimal64>(42, numeric::scale_type{3});
std::vector<std::reference_wrapper<const cudf::scalar>> fill_values{*slr1, *slr2};
cudf::test::fixed_point_column_wrapper<int32_t> e1{{-42, -42, -42, -42, -42, -42, -42, -42},
numeric::scale_type{-1}};
cudf::test::fixed_point_column_wrapper<int64_t> e2{{42, 5, 7, 42, 5, 42, 8, 8},
numeric::scale_type{3}};
cudf::table_view expected{{e1, e2}};
test_groupby_shift_multi(key, value, offset, fill_values, expected);
}
TEST_F(groupby_shift_fixed_point_type_test, MismatchScaleType)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{2, 3, 4, 4, 3, 2, 2, 4};
cudf::test::fixed_point_column_wrapper<int32_t> v1{{10, 10, 40, 40, 20, 20, 30, 40},
numeric::scale_type{-1}};
std::vector<cudf::size_type> offset{-3};
auto slr1 = cudf::make_fixed_point_scalar<numeric::decimal32>(-42, numeric::scale_type{-4});
cudf::test::fixed_point_column_wrapper<int32_t> stub{{-42, -42, -42, -42, -42, -42, -42, -42},
numeric::scale_type{-1}};
EXPECT_THROW(test_groupby_shift_multi(
key, cudf::table_view{{v1}}, offset, {*slr1}, cudf::table_view{{stub}}),
cudf::logic_error);
}
TEST_F(groupby_shift_fixed_point_type_test, MismatchRepType)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> key{2, 3, 4, 4, 3, 2, 2, 4};
cudf::test::fixed_point_column_wrapper<int64_t> v1{{10, 10, 40, 40, 20, 20, 30, 40},
numeric::scale_type{-1}};
std::vector<cudf::size_type> offset{-3};
auto slr1 = cudf::make_fixed_point_scalar<numeric::decimal32>(-42, numeric::scale_type{-1});
cudf::test::fixed_point_column_wrapper<int32_t> stub{{-42, -42, -42, -42, -42, -42, -42, -42},
numeric::scale_type{-1}};
EXPECT_THROW(test_groupby_shift_multi(
key, cudf::table_view{{v1}}, offset, {*slr1}, cudf::table_view{{stub}}),
cudf::logic_error);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/argmin_tests.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_argmin_test : public cudf::test::BaseFixture {};
using K = int32_t;
TYPED_TEST_SUITE(groupby_argmin_test, cudf::test::FixedWidthTypes);
TYPED_TEST(groupby_argmin_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMIN>;
if (std::is_same_v<V, bool>) return;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals{6, 9, 8};
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_argmin_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMIN>;
if (std::is_same_v<V, bool>) return;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5});
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_argmin_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMIN>;
if (std::is_same_v<V, bool>) return;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_argmin_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMIN>;
if (std::is_same_v<V, bool>) return;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 4},
{1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 9, 6, 8, 5, 0, 7, 1, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3, 9, 8, 0}, {1, 1, 1, 0});
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
// TODO: explore making this a gtest parameter
auto agg2 = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
struct groupby_argmin_string_test : public cudf::test::BaseFixture {};
TEST_F(groupby_argmin_string_test, basic)
{
using R = cudf::detail::target_type_t<cudf::string_view, cudf::aggregation::ARGMIN>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::strings_column_wrapper vals{
"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals({3, 5, 7});
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TEST_F(groupby_argmin_string_test, zero_valid_values)
{
using R = cudf::detail::target_type_t<cudf::string_view, cudf::aggregation::ARGMIN>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::strings_column_wrapper vals({"año", "bit", "₹1"}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
struct groupby_dictionary_argmin_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_argmin_test, basic)
{
using V = std::string;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMIN>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2 };
cudf::test::dictionary_column_wrapper<V> vals{"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 3 });
cudf::test::fixed_width_column_wrapper<R> expect_vals({ 3, 5, 7 });
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_argmin_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_argmin_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
struct groupby_argmin_struct_test : public cudf::test::BaseFixture {};
TEST_F(groupby_argmin_struct_test, basic)
{
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3};
auto const expect_indices = cudf::test::fixed_width_column_wrapper<int32_t>{3, 5, 7};
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_indices, std::move(agg));
}
TEST_F(groupby_argmin_struct_test, slice_input)
{
constexpr int32_t dont_care{1};
auto const keys_original = cudf::test::fixed_width_column_wrapper<int32_t>{
dont_care, dont_care, 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, dont_care};
auto const vals_original = [] {
auto child1 = cudf::test::strings_column_wrapper{"dont_care",
"dont_care",
"año",
"bit",
"₹1",
"aaa",
"zit",
"bat",
"aab",
"$1",
"€1",
"wut",
"dont_care"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{
dont_care, dont_care, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, dont_care};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const keys = cudf::slice(keys_original, {2, 12})[0];
auto const vals = cudf::slice(vals_original, {2, 12})[0];
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3};
auto const expect_indices = cudf::test::fixed_width_column_wrapper<int32_t>{3, 5, 7};
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_indices, std::move(agg));
}
TEST_F(groupby_argmin_struct_test, null_keys_and_values)
{
constexpr int32_t null{0};
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{
{1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4}, null_at(7)};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "" /*NULL*/, "" /*NULL*/, "$1", "€1", "wut", "" /*NULL*/};
auto child2 =
cudf::test::fixed_width_column_wrapper<int32_t>{9, 8, 7, 6, 5, null, null, 2, 1, 0, null};
return cudf::test::structs_column_wrapper{{child1, child2}, nulls_at({5, 6, 10})};
}();
auto const expect_keys =
cudf::test::fixed_width_column_wrapper<int32_t>{{1, 2, 3, 4}, no_nulls()};
auto const expect_indices =
cudf::test::fixed_width_column_wrapper<int32_t>{{3, 1, 8, null}, null_at(3)};
auto agg = cudf::make_argmin_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_indices, std::move(agg));
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/correlation_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <limits>
#include <vector>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_correlation_test : public cudf::test::BaseFixture {};
using supported_types =
cudf::test::RemoveIf<cudf::test::ContainedIn<cudf::test::Types<bool>>, cudf::test::NumericTypes>;
TYPED_TEST_SUITE(groupby_correlation_test, supported_types);
using K = int32_t;
TYPED_TEST(groupby_correlation_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
auto keys = cudf::test::fixed_width_column_wrapper<K>{{1, 2, 3, 1, 2, 2, 1, 3, 3, 2}};
auto member_0 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 2, 3, 3, 1, 1, 4}};
auto member_1 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 0, 3, 3, 1, 1, 2}};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R, double> expect_vals{{1.0, 0.6, nan}};
auto agg =
cudf::make_correlation_aggregation<cudf::groupby_aggregation>(cudf::correlation_type::PEARSON);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_correlation_test, empty_cols)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> member_0{}, member_1{};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg =
cudf::make_correlation_aggregation<cudf::groupby_aggregation>(cudf::correlation_type::PEARSON);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_correlation_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> member_0{3, 4, 5}, member_1{6, 7, 8};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg =
cudf::make_correlation_aggregation<cudf::groupby_aggregation>(cudf::correlation_type::PEARSON);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_correlation_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> member_0({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> member_1({3, 4, 5}, all_nulls());
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg =
cudf::make_correlation_aggregation<cudf::groupby_aggregation>(cudf::correlation_type::PEARSON);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_correlation_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val0({9, 1, 1, 2, 2, 3, 3,-1, 1, 4, 4},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val1({1, 1, 1, 2, 0, 3, 3,-1, 0, 2, 2});
// clang-format on
auto vals = cudf::test::structs_column_wrapper{{val0, val1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
cudf::test::fixed_width_column_wrapper<R> expect_vals({1.0, 0.6, nan, 0.}, {1, 1, 1, 0});
auto agg =
cudf::make_correlation_aggregation<cudf::groupby_aggregation>(cudf::correlation_type::PEARSON);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_correlation_test, null_values_same)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val0({9, 1, 1, 2, 2, 3, 3,-1, 1, 4, 4},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
cudf::test::fixed_width_column_wrapper<V> val1({1, 1, 1, 2, 0, 3, 3,-1, 0, 2, 2},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
// clang-format on
auto vals = cudf::test::structs_column_wrapper{{val0, val1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
cudf::test::fixed_width_column_wrapper<R> expect_vals({1.0, 0.6, nan, 0.}, {1, 1, 1, 0});
auto agg =
cudf::make_correlation_aggregation<cudf::groupby_aggregation>(cudf::correlation_type::PEARSON);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
// keys=[1, 1, 1, 2, 2, 2, 2, 3, N, 3, 4]
// val0=[N, 2, 3, 1, N, 3, 4, 1,-1, 1, 4]
// val1=[N, 2, 3, 2,-1, 6,-6/1, 1,-1, 0, N]
// corr=[ 1.0, -0.5/0, NAN, NAN]
TYPED_TEST(groupby_correlation_test, null_values_different)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val0({9, 1, 1, 2, 2, 3, 3,-1, 1, 4, 4},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val1({1, 2, 1, 2,-1, 6, 3,-1, 0, 1, 2},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
// clang-format on
auto vals = cudf::test::structs_column_wrapper{{val0, val1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
cudf::test::fixed_width_column_wrapper<R> expect_vals({1.0, 0., nan, 0.}, {1, 1, 1, 0});
auto agg =
cudf::make_correlation_aggregation<cudf::groupby_aggregation>(cudf::correlation_type::PEARSON);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_correlation_test, min_periods)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
auto keys = cudf::test::fixed_width_column_wrapper<K>{{1, 2, 3, 1, 2, 2, 1, 3, 3, 2}};
auto member_0 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 2, 3, 3, 1, 1, 4}};
auto member_1 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 0, 3, 3, 1, 1, 2}};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R, double> expect_vals1{{1.0, 0.6, nan}};
auto agg1 = cudf::make_correlation_aggregation<cudf::groupby_aggregation>(
cudf::correlation_type::PEARSON, 3);
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg1), force_use_sort_impl::YES);
cudf::test::fixed_width_column_wrapper<R, double> expect_vals2{{1.0, 0.6, nan}, {0, 1, 0}};
auto agg2 = cudf::make_correlation_aggregation<cudf::groupby_aggregation>(
cudf::correlation_type::PEARSON, 4);
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg2), force_use_sort_impl::YES);
cudf::test::fixed_width_column_wrapper<R, double> expect_vals3{{1.0, 0.6, nan}, {0, 0, 0}};
auto agg3 = cudf::make_correlation_aggregation<cudf::groupby_aggregation>(
cudf::correlation_type::PEARSON, 5);
test_single_agg(keys, vals, expect_keys, expect_vals3, std::move(agg3), force_use_sort_impl::YES);
}
struct groupby_dictionary_correlation_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_correlation_test, basic)
{
using V = int16_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::CORRELATION>;
constexpr auto nan = std::numeric_limits<double>::quiet_NaN();
auto keys = cudf::test::fixed_width_column_wrapper<K>{{1, 2, 3, 1, 2, 2, 1, 3, 3, 2}};
auto member_0 = cudf::test::dictionary_column_wrapper<V>{{1, 1, 1, 2, 2, 3, 3, 1, 1, 4}};
auto member_1 = cudf::test::dictionary_column_wrapper<V>{{1, 1, 1, 2, 0, 3, 3, 1, 1, 2}};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R, double> expect_vals{{1.0, 0.6, nan}};
auto agg =
cudf::make_correlation_aggregation<cudf::groupby_aggregation>(cudf::correlation_type::PEARSON);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/merge_sets_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/concatenate.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/groupby.hpp>
#include <cudf/lists/sorting.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table_view.hpp>
using namespace cudf::test::iterators;
namespace {
constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::FIRST_ERROR};
constexpr int32_t null{0}; // Mark for null elements
using vcol_views = std::vector<cudf::column_view>;
auto merge_sets(vcol_views const& keys_cols, vcol_views const& values_cols)
{
// Append all the keys and lists together.
auto const keys = cudf::concatenate(keys_cols);
auto const values = cudf::concatenate(values_cols);
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = *values;
requests[0].aggregations.emplace_back(
cudf::make_merge_sets_aggregation<cudf::groupby_aggregation>());
auto const result = cudf::groupby::groupby(cudf::table_view({*keys})).aggregate(requests);
auto const result_keys = result.first->view(); // <== table_view of 1 column
auto const result_vals = result.second[0].results[0]->view(); // <== column_view
// Sort the output columns based on the output keys.
// This is to facilitate comparison of the output with the expected columns.
auto keys_vals_sorted = cudf::sort_by_key(cudf::table_view{{result_keys.column(0), result_vals}},
result_keys,
{},
{cudf::null_order::AFTER})
->release();
// After the columns were reordered, individual rows of the output values column (which are lists)
// also need to be sorted.
auto out_values =
cudf::lists::sort_lists(cudf::lists_column_view{keys_vals_sorted.back()->view()},
cudf::order::ASCENDING,
cudf::null_order::AFTER);
return std::pair(std::move(keys_vals_sorted.front()), std::move(out_values));
}
} // namespace
template <typename V>
struct GroupbyMergeSetsTypedTest : public cudf::test::BaseFixture {};
using FixedWidthTypesNotBool = cudf::test::Concat<cudf::test::IntegralTypesNotBool,
cudf::test::FloatingPointTypes,
cudf::test::TimestampTypes>;
TYPED_TEST_SUITE(GroupbyMergeSetsTypedTest, FixedWidthTypesNotBool);
TYPED_TEST(GroupbyMergeSetsTypedTest, InvalidInput)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys = keys_col{1, 2, 3};
// The input lists column must NOT be nullable.
auto const lists = lists_col{{lists_col{1}, lists_col{} /*NULL*/, lists_col{2}}, null_at(1)};
EXPECT_THROW(merge_sets({keys}, {lists}), cudf::logic_error);
// The input column must be a lists column.
auto const non_lists = keys_col{1, 2, 3, 4, 5};
EXPECT_THROW(merge_sets({keys}, {non_lists}), cudf::logic_error);
}
TYPED_TEST(GroupbyMergeSetsTypedTest, EmptyInput)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
// Keys and lists columns are all empty.
auto const keys = keys_col{};
auto const lists0 = lists_col{{1, 2, 3}, {4, 5, 6}};
auto const lists = cudf::empty_like(lists0);
auto const [out_keys, out_lists] = merge_sets(vcol_views{keys}, vcol_views{*lists});
auto const expected_keys = keys_col{};
auto const expected_lists = cudf::empty_like(lists0);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeSetsTypedTest, InputWithoutNull)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2};
auto const keys2 = keys_col{1, 3};
auto const keys3 = keys_col{2, 3, 4};
auto const lists1 = lists_col{
{1, 2, 3, 4, 5, 6}, // key = 1
{10, 11, 12, 13, 14, 15} // key = 2
};
auto const lists2 = lists_col{
{4, 5, 6, 7, 8, 9}, // key = 1
{20, 21, 22, 23, 24, 25} // key = 3
};
auto const lists3 = lists_col{
{11, 12}, // key = 2
{23, 24, 25, 26, 27, 28}, // key = 3
{30, 31, 32} // key = 4
};
auto const [out_keys, out_lists] =
merge_sets(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
{1, 2, 3, 4, 5, 6, 7, 8, 9}, // key = 1
{10, 11, 12, 13, 14, 15}, // key = 2
{20, 21, 22, 23, 24, 25, 26, 27, 28}, // key = 3
{30, 31, 32} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeSetsTypedTest, InputHasNulls)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2};
auto const keys2 = keys_col{1, 3};
auto const keys3 = keys_col{2, 3, 4};
auto const lists1 = lists_col{
lists_col{{1, null, null, null, 5, 6}, nulls_at({1, 2, 3})}, // key = 1
lists_col{10, 11, 12, 13, 14, 15} // key = 2
};
auto const lists2 = lists_col{
lists_col{{null, null, 6, 7, 8, 9}, nulls_at({0, 1})}, // key = 1
lists_col{{null, 21, 22, 23, 24, 25}, null_at(0)} // key = 3
};
auto const lists3 = lists_col{
lists_col{11, 12}, // key = 2
lists_col{23, 24, 25, 26, 27, 28}, // key = 3
lists_col{{30, null, 32}, null_at(1)} // key = 4
};
auto const [out_keys, out_lists] =
merge_sets(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
lists_col{{1, 5, 6, 7, 8, 9, null}, null_at(6)}, // key = 1
lists_col{10, 11, 12, 13, 14, 15}, // key = 2
lists_col{{21, 22, 23, 24, 25, 26, 27, 28, null}, null_at(8)}, // key = 3
lists_col{{30, 32, null}, null_at(2)} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeSetsTypedTest, InputHasEmptyLists)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2};
auto const keys2 = keys_col{1, 3};
auto const keys3 = keys_col{2, 3, 4};
auto const lists1 = lists_col{
{1, 2, 3}, // key = 1
{} // key = 2
};
auto const lists2 = lists_col{
{0, 1, 2, 3, 4, 5}, // key = 1
{11, 12, 12, 12, 12, 12} // key = 3
};
auto const lists3 = lists_col{
{}, // key = 2
{}, // key = 3
{24, 25, 26} // key = 4
};
auto const [out_keys, out_lists] =
merge_sets(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
{0, 1, 2, 3, 4, 5}, // key = 1
{}, // key = 2
{11, 12}, // key = 3
{24, 25, 26} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeSetsTypedTest, InputHasNullsAndEmptyLists)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2, 3};
auto const keys2 = keys_col{1, 3, 4};
auto const keys3 = keys_col{2, 3, 4};
auto const lists1 = lists_col{
lists_col{{null, 1, 2, 3}, null_at(0)}, // key = 1
lists_col{}, // key = 2
lists_col{} // key = 3
};
auto const lists2 = lists_col{
lists_col{0, 1, 2, 3, 4, 5}, // key = 1
lists_col{{null, 11, null, 12, 12, 12, 12, 12}, nulls_at({0, 2})}, // key = 3
lists_col{20} // key = 4
};
auto const lists3 = lists_col{
lists_col{}, // key = 2
lists_col{}, // key = 3
lists_col{{24, 25, null, null, null, 26}, nulls_at({2, 3, 4})} // key = 4
};
auto const [out_keys, out_lists] =
merge_sets(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
lists_col{{0, 1, 2, 3, 4, 5, null}, null_at(6)}, // key = 1
lists_col{}, // key = 2
lists_col{{11, 12, null}, null_at(2)}, // key = 3
lists_col{{20, 24, 25, 26, null}, null_at(4)} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeSetsTypedTest, SlicedColumnsInput)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1_original = keys_col{1, 2, 4, 5, 6, 7, 8, 9, 10};
auto const keys2_original = keys_col{0, 0, 1, 1, 1, 3, 4, 5, 6};
auto const keys3_original = keys_col{0, 1, 2, 3, 4, 5, 6, 7, 8};
auto const keys1 = cudf::slice(keys1_original, {0, 2})[0]; // { 1, 2 }
auto const keys2 = cudf::slice(keys2_original, {4, 6})[0]; // { 1, 3 }
auto const keys3 = cudf::slice(keys3_original, {2, 5})[0]; // { 2, 3, 4 }
auto const lists1_original = lists_col{
{10, 11, 12, 10, 11, 12, 10, 11, 12},
{12, 13, 12, 13, 12, 13, 12, 13, 14},
{1, 2, 3, 1, 2, 3, 1, 2, 3}, // key = 1
{4, 5, 6, 4, 5, 6, 4, 5, 6} // key = 2
};
auto const lists2_original = lists_col{{1, 1, 1, 1, 1, 1, 1, 2},
{10, 11, 11, 11, 11, 11, 12}, // key = 1
{11, 12, 13, 12, 13, 12, 13, 12, 13, 14, 15}, // key = 3
{13, 14, 15},
{14, 15, 16},
{15, 16}};
auto const lists3_original = lists_col{{20, 21, 20, 21, 20, 21, 20, 21, 22}, // key = 2
{23, 24, 25, 23, 24, 25}, // key = 3
{24, 25, 26}, // key = 4
{1, 2, 3, 4, 5}};
auto const lists1 = cudf::slice(lists1_original, {2, 4})[0];
auto const lists2 = cudf::slice(lists2_original, {1, 3})[0];
auto const lists3 = cudf::slice(lists3_original, {0, 3})[0];
auto const [out_keys, out_lists] =
merge_sets(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
{1, 2, 3, 10, 11, 12}, // key = 1
{4, 5, 6, 20, 21, 22}, // key = 2
{11, 12, 13, 14, 15, 23, 24, 25}, // key = 3
{24, 25, 26} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
struct GroupbyMergeSetsTest : public cudf::test::BaseFixture {};
TEST_F(GroupbyMergeSetsTest, StringsColumnInput)
{
using strings_col = cudf::test::strings_column_wrapper;
using lists_col = cudf::test::lists_column_wrapper<cudf::string_view>;
auto const keys1 = strings_col{"apple", "dog", "unknown"};
auto const keys2 = strings_col{"banana", "unknown", "dog"};
auto const keys3 = strings_col{"apple", "dog", "water melon"};
auto const lists1 = lists_col{
lists_col{"Fuji", "Honey Bee"}, // key = "apple"
lists_col{"Poodle", "Golden Retriever", "Corgi"}, // key = "dog"
lists_col{{"Whale", "" /*NULL*/, "Polar Bear"}, null_at(1)} // key = "unknown"
};
auto const lists2 = lists_col{
lists_col{"Green", "Yellow"}, // key = "banana"
lists_col{}, // key = "unknown"
lists_col{{"" /*NULL*/, "" /*NULL*/, "" /*NULL*/}, all_nulls()} // key = "dog"
};
auto const lists3 = lists_col{
lists_col{"Fuji", "Red Delicious"}, // key = "apple"
lists_col{{"" /*NULL*/, "Corgi", "German Shepherd", "" /*NULL*/, "Golden Retriever"},
nulls_at({0, 3})}, // key = "dog"
lists_col{{"Seeedless", "Mini"}, no_nulls()} // key = "water melon"
};
auto const [out_keys, out_lists] =
merge_sets(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = strings_col{"apple", "banana", "dog", "unknown", "water melon"};
auto const expected_lists = lists_col{
lists_col{"Fuji", "Honey Bee", "Red Delicious"}, // key = "apple"
lists_col{"Green", "Yellow"}, // key = "banana"
lists_col{{
"Corgi", "German Shepherd", "Golden Retriever", "Poodle", "" /*NULL*/
},
null_at(4)}, // key = "dog"
lists_col{{"Polar Bear", "Whale", "" /*NULL*/}, null_at(2)}, // key = "unknown"
lists_col{{"Mini", "Seeedless"}, no_nulls()} // key = "water melon"
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/replace_nulls_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/groupby.hpp>
#include <cudf/replace.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
using namespace cudf::test::iterators;
using K = int32_t;
template <typename T>
struct GroupbyReplaceNullsFixedWidthTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupbyReplaceNullsFixedWidthTest, cudf::test::FixedWidthTypes);
template <typename K, typename V>
void TestReplaceNullsGroupbySingle(K const& key,
V const& input,
K const& expected_key,
V const& expected_val,
cudf::replace_policy policy)
{
cudf::groupby::groupby gb_obj(cudf::table_view({key}));
std::vector<cudf::replace_policy> policies{policy};
auto p = gb_obj.replace_nulls(cudf::table_view({input}), policies);
CUDF_TEST_EXPECT_TABLES_EQUAL(*p.first, cudf::table_view({expected_key}));
CUDF_TEST_EXPECT_TABLES_EQUAL(*p.second, cudf::table_view({expected_val}));
}
TYPED_TEST(GroupbyReplaceNullsFixedWidthTest, PrecedingFill)
{
// Group 0 value: {42, 24, null} --> {42, 24, 24}
// Group 1 value: {7, null, null} --> {7, 7, 7}
cudf::test::fixed_width_column_wrapper<K> key{0, 1, 0, 1, 0, 1};
cudf::test::fixed_width_column_wrapper<TypeParam> val({42, 7, 24, 10, 1, 1000},
{1, 1, 1, 0, 0, 0});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 1, 1, 1};
cudf::test::fixed_width_column_wrapper<TypeParam> expect_val({42, 24, 24, 7, 7, 7}, no_nulls());
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::PRECEDING);
}
TYPED_TEST(GroupbyReplaceNullsFixedWidthTest, FollowingFill)
{
// Group 0 value: {2, null, 32} --> {2, 32, 32}
// Group 1 value: {8, null, null, 128, 256} --> {8, 128, 128, 128, 256}
cudf::test::fixed_width_column_wrapper<K> key{0, 0, 1, 1, 0, 1, 1, 1};
cudf::test::fixed_width_column_wrapper<TypeParam> val({2, 4, 8, 16, 32, 64, 128, 256},
{1, 0, 1, 0, 1, 0, 1, 1});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 1, 1, 1, 1, 1};
cudf::test::fixed_width_column_wrapper<TypeParam> expect_val({2, 32, 32, 8, 128, 128, 128, 256},
no_nulls());
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::FOLLOWING);
}
TYPED_TEST(GroupbyReplaceNullsFixedWidthTest, PrecedingFillLeadingNulls)
{
// Group 0 value: {null, 24, null} --> {null, 24, 24}
// Group 1 value: {null, null, null} --> {null, null, null}
cudf::test::fixed_width_column_wrapper<K> key{0, 1, 0, 1, 0, 1};
cudf::test::fixed_width_column_wrapper<TypeParam> val({42, 7, 24, 10, 1, 1000},
{0, 0, 1, 0, 0, 0});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 1, 1, 1};
cudf::test::fixed_width_column_wrapper<TypeParam> expect_val({-1, 24, 24, -1, -1, -1},
{0, 1, 1, 0, 0, 0});
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::PRECEDING);
}
TYPED_TEST(GroupbyReplaceNullsFixedWidthTest, FollowingFillTrailingNulls)
{
// Group 0 value: {2, null, null} --> {2, null, null}
// Group 1 value: {null, null, 64, null, null} --> {64, 64, 64, null, null}
cudf::test::fixed_width_column_wrapper<K> key{0, 0, 1, 1, 0, 1, 1, 1};
cudf::test::fixed_width_column_wrapper<TypeParam> val({2, 4, 8, 16, 32, 64, 128, 256},
{1, 0, 0, 0, 0, 1, 0, 0});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 1, 1, 1, 1, 1};
cudf::test::fixed_width_column_wrapper<TypeParam> expect_val({2, -1, -1, 64, 64, 64, -1, -1},
{1, 0, 0, 1, 1, 1, 0, 0});
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::FOLLOWING);
}
struct GroupbyReplaceNullsStringsTest : public cudf::test::BaseFixture {};
TEST_F(GroupbyReplaceNullsStringsTest, PrecedingFill)
{
// Group 0 value: {"y" "42"} --> {"y", "42"}
// Group 1 value: {"xx" @ "zzz" @ "one"} --> {"xx" "xx" "zzz" "zzz" "one"}
cudf::test::fixed_width_column_wrapper<K> key{1, 1, 0, 1, 0, 1, 1};
cudf::test::strings_column_wrapper val({"xx", "", "y", "zzz", "42", "", "one"},
{true, false, true, true, true, false, true});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 1, 1, 1, 1, 1};
cudf::test::strings_column_wrapper expect_val({"y", "42", "xx", "xx", "zzz", "zzz", "one"},
no_nulls());
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::PRECEDING);
}
TEST_F(GroupbyReplaceNullsStringsTest, FollowingFill)
{
// Group 0 value: {@ "42"} --> {"42", "42"}
// Group 1 value: {"xx" @ "zzz" @ "one"} --> {"xx" "zzz" "zzz" "one" "one"}
cudf::test::fixed_width_column_wrapper<K> key{1, 1, 0, 1, 0, 1, 1};
cudf::test::strings_column_wrapper val({"xx", "", "", "zzz", "42", "", "one"},
{true, false, false, true, true, false, true});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 1, 1, 1, 1, 1};
cudf::test::strings_column_wrapper expect_val({"42", "42", "xx", "zzz", "zzz", "one", "one"},
no_nulls());
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::FOLLOWING);
}
TEST_F(GroupbyReplaceNullsStringsTest, PrecedingFillPrecedingNull)
{
// Group 0 value: {"y" "42"} --> {"y", "42"}
// Group 1 value: {@ @ "zzz" "zzz" "zzz"} --> {@ @ "zzz" "zzz" "zzz"}
cudf::test::fixed_width_column_wrapper<K> key{1, 1, 0, 1, 0, 1, 1};
cudf::test::strings_column_wrapper val({"", "", "y", "zzz", "42", "", ""},
{false, false, true, true, true, false, false});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 1, 1, 1, 1, 1};
cudf::test::strings_column_wrapper expect_val({"y", "42", "", "", "zzz", "zzz", "zzz"},
{true, true, false, false, true, true, true});
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::PRECEDING);
}
TEST_F(GroupbyReplaceNullsStringsTest, FollowingFillTrailingNull)
{
// Group 0 value: {@ "y"} --> {"y", "y"}
// Group 1 value: {"xx" @ "zzz" @ @} --> {"xx" "zzz" "zzz" @ @}
cudf::test::fixed_width_column_wrapper<K> key{1, 1, 0, 1, 0, 1, 1};
cudf::test::strings_column_wrapper val({"xx", "", "", "zzz", "y", "", ""},
{true, false, false, true, true, false, false});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 1, 1, 1, 1, 1};
cudf::test::strings_column_wrapper expect_val({"y", "y", "xx", "zzz", "zzz", "", ""},
{true, true, true, true, true, false, false});
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::FOLLOWING);
}
template <typename T>
struct GroupbyReplaceNullsListsTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupbyReplaceNullsListsTest, cudf::test::FixedWidthTypes);
TYPED_TEST(GroupbyReplaceNullsListsTest, PrecedingFillNonNested)
{
using LCW = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
// Group 0 value: {{1 2 3} @ {4 5} @} --> {{1 2 3} {1 2 3} {4 5} {4 5}}, w/o leading nulls
// Group 1 value: {@ {} @} --> {@ {} {}}, w/ leading nulls
cudf::test::fixed_width_column_wrapper<K> key{0, 1, 0, 0, 1, 1, 0};
std::vector<cudf::valid_type> mask{1, 0, 0, 1, 1, 0, 0};
LCW val({{1, 2, 3}, {}, {}, {4, 5}, {}, {}, {}}, mask.begin());
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 0, 1, 1, 1};
std::vector<cudf::valid_type> expected_mask{1, 1, 1, 1, 0, 1, 1};
LCW expect_val({{1, 2, 3}, {1, 2, 3}, {4, 5}, {4, 5}, {-1}, {}, {}}, expected_mask.begin());
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::PRECEDING);
}
TYPED_TEST(GroupbyReplaceNullsListsTest, FollowingFillNonNested)
{
using LCW = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
// Group 0 value: {@ {5 6} @ {-1}} --> {{5 6} {5 6} {-1} {-1}}, w/o trailing nulls
// Group 1 value: {@ {} @} --> {{} {} @}}, w/ trailing nulls
cudf::test::fixed_width_column_wrapper<K> key{0, 1, 0, 0, 1, 1, 0};
std::vector<cudf::valid_type> mask{0, 0, 1, 0, 1, 0, 1};
LCW val({{}, {}, {5, 6}, {}, {}, {}, {-1}}, mask.begin());
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 0, 1, 1, 1};
std::vector<cudf::valid_type> expected_mask{1, 1, 1, 1, 1, 1, 0};
LCW expect_val({{5, 6}, {5, 6}, {-1}, {-1}, {}, {}, {}}, expected_mask.begin());
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::FOLLOWING);
}
TYPED_TEST(GroupbyReplaceNullsListsTest, PrecedingFillNested)
{
using LCW = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
using Mask_t = std::vector<cudf::valid_type>;
// Group 0 value: {{{1 @ 3} @}
// @
// {{@} {}}}} -->
// {{{1 @ 3} @}
// {{1 @ 3} @}
// {{@} {}}}}, w/o leading nulls
// Group 1 value: {@
// {@ {102 @}}
// @
// {{@ 202} {}}}} -->
// {@
// {@ {102 @}}
// {@ {102 @}}
// {{@ 202} {}}}}, w/ leading nulls
// Only top level nulls are replaced.
cudf::test::fixed_width_column_wrapper<K> key{1, 0, 1, 1, 0, 0, 1};
// clang-format off
LCW val({{},
LCW({LCW({1, -1, 3}, Mask_t{1, 0, 1}.begin()), {}}, Mask_t{1, 0}.begin()),
LCW({LCW{}, LCW({102, -1}, Mask_t{1, 0}.begin())}, Mask_t{0, 1}.begin()),
{},
{},
{LCW({{}}, Mask_t{0}.begin()), LCW{}},
{LCW({-1, 202}, Mask_t{0, 1}.begin()), LCW{}}},
Mask_t{0, 1, 1, 0, 0, 1, 1}.begin());
// clang-format on
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 1, 1, 1, 1};
// clang-format off
LCW expect_val({LCW({LCW({1, -1, 3}, Mask_t{1, 0, 1}.begin()), {}}, Mask_t{1, 0}.begin()),
LCW({LCW({1, -1, 3}, Mask_t{1, 0, 1}.begin()), {}}, Mask_t{1, 0}.begin()),
{LCW({{}}, Mask_t{0}.begin()), LCW{}},
{},
LCW({LCW{}, LCW({102, -1}, Mask_t{1, 0}.begin())}, Mask_t{0, 1}.begin()),
LCW({LCW{}, LCW({102, -1}, Mask_t{1, 0}.begin())}, Mask_t{0, 1}.begin()),
{LCW({-1, 202}, Mask_t{0, 1}.begin()), LCW{}}},
Mask_t{1, 1, 1, 0, 1 ,1 ,1}.begin());
// clang-format on
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::PRECEDING);
}
TYPED_TEST(GroupbyReplaceNullsListsTest, FollowingFillNested)
{
using LCW = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
using Mask_t = std::vector<cudf::valid_type>;
// Group 0 value: {{{1 @ 3} @}
// @
// {{@} {}}}} -->
// {{{1 @ 3} @}
// {{@} {}}}}
// {{@} {}}}}, w/o trailing nulls
// Group 1 value: {{@ {102 @}}
// @
// {{@ 202} {}}}}
// @ -->
// {{@ {102 @}}
// {{@ 202} {}}}
// {{@ 202} {}}}
// @}, w/ trailing nulls
// Only top level nulls are replaced.
cudf::test::fixed_width_column_wrapper<K> key{1, 0, 1, 1, 0, 0, 1};
// clang-format off
LCW val({LCW({LCW{}, LCW({102, -1}, Mask_t{1, 0}.begin())}, Mask_t{0, 1}.begin()),
LCW({LCW({1, -1, 3}, Mask_t{1, 0, 1}.begin()), {}}, Mask_t{1, 0}.begin()),
{},
{LCW({-1, 202}, Mask_t{0, 1}.begin()), LCW{}},
{},
{LCW({{}}, Mask_t{0}.begin()), LCW{}},
{}},
Mask_t{1, 1, 0, 1, 0, 1, 0}.begin());
// clang-format on
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 1, 1, 1, 1};
// clang-format off
LCW expect_val({LCW({LCW({1, -1, 3}, Mask_t{1, 0, 1}.begin()), {}}, Mask_t{1, 0}.begin()),
{LCW({{}}, Mask_t{0}.begin()), LCW{}},
{LCW({{}}, Mask_t{0}.begin()), LCW{}},
LCW({LCW{}, LCW({102, -1}, Mask_t{1, 0}.begin())}, Mask_t{0, 1}.begin()),
{LCW({-1, 202}, Mask_t{0, 1}.begin()), LCW{}},
{LCW({-1, 202}, Mask_t{0, 1}.begin()), LCW{}},
{}},
Mask_t{1, 1, 1, 1, 1, 1, 0}.begin());
// clang-format on
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::FOLLOWING);
}
struct GroupbyReplaceNullsStructsTest : public cudf::test::BaseFixture {
using SCW = cudf::test::structs_column_wrapper;
SCW data(cudf::test::fixed_width_column_wrapper<int32_t> field0,
cudf::test::strings_column_wrapper field1,
cudf::test::lists_column_wrapper<int32_t> field2,
std::initializer_list<cudf::valid_type> mask)
{
return SCW({field0, field1, field2}, mask.begin());
}
};
TEST_F(GroupbyReplaceNullsStructsTest, PrecedingFill)
{
using LCW = cudf::test::lists_column_wrapper<int32_t>;
using Mask_t = std::vector<cudf::valid_type>;
cudf::test::fixed_width_column_wrapper<K> key{1, 0, 0, 1, 0, 1, 1};
// Only null rows are replaced.
SCW val =
this->data({{1, -1, 3, -1, -1, -1, 7}, {1, 0, 1, 0, 0, 0, 1}},
{{"x", "yy", "", "", "", "zz", ""}, {true, true, false, false, false, true, false}},
LCW({{1, 2, 3}, {-1}, {}, {}, {42}, {}, {}}, Mask_t{1, 1, 0, 0, 1, 0, 0}.begin()),
{1, 1, 0, 0, 1, 1, 0});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 1, 1, 1, 1};
SCW expect_val = this->data(
{{-1, -1, -1, 1, 1, -1, -1}, {0, 0, 0, 1, 1, 0, 0}},
{{"yy", "yy", "", "x", "x", "zz", "zz"}, {true, true, false, true, true, true, true}},
LCW({LCW{-1}, {-1}, {42}, {1, 2, 3}, {1, 2, 3}, {}, {}}, Mask_t{1, 1, 1, 1, 1, 0, 0}.begin()),
{1, 1, 1, 1, 1, 1, 1});
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::PRECEDING);
}
TEST_F(GroupbyReplaceNullsStructsTest, FollowingFill)
{
using LCW = cudf::test::lists_column_wrapper<int32_t>;
using Mask_t = std::vector<cudf::valid_type>;
cudf::test::fixed_width_column_wrapper<K> key{1, 0, 0, 1, 0, 1, 1};
// Only null rows are replaced.
SCW val =
this->data({{1, -1, 3, -1, -1, -1, 7}, {1, 0, 1, 0, 0, 0, 1}},
{{"x", "yy", "", "", "", "zz", ""}, {true, true, false, false, false, true, false}},
LCW({{1, 2, 3}, {-1}, {}, {}, {42}, {}, {}}, Mask_t{1, 1, 0, 0, 1, 0, 0}.begin()),
{1, 1, 0, 0, 1, 1, 0});
cudf::test::fixed_width_column_wrapper<K> expect_key{0, 0, 0, 1, 1, 1, 1};
SCW expect_val = this->data(
{{-1, -1, -1, 1, -1, -1, -1}, {0, 0, 0, 1, 0, 0, 0}},
{{"yy", "", "", "x", "zz", "zz", ""}, {true, false, false, true, true, true, false}},
LCW({LCW{-1}, {42}, {42}, {1, 2, 3}, {}, {}, {}}, Mask_t{1, 1, 1, 1, 0, 0, 0}.begin()),
{1, 1, 1, 1, 1, 1, 0});
TestReplaceNullsGroupbySingle(key, val, expect_key, expect_val, cudf::replace_policy::FOLLOWING);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/min_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/dictionary/update_keys.hpp>
#include <limits>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_min_test : public cudf::test::BaseFixture {};
using K = int32_t;
TYPED_TEST_SUITE(groupby_min_test, cudf::test::FixedWidthTypesWithoutFixedPoint);
TYPED_TEST(groupby_min_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MIN>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0, 1, 2});
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_min_test, empty_cols)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MIN>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_min_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MIN>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5});
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_min_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MIN>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_min_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MIN>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 3, 6, 1, 4, 9, 2, 8, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3, 1, 2, 0}, {1, 1, 1, 0});
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
struct groupby_min_string_test : public cudf::test::BaseFixture {};
TEST_F(groupby_min_string_test, basic)
{
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::strings_column_wrapper vals{
"año", "bit", "₹1", "aaa", "zit", "bat", "aaa", "$1", "₹1", "wut"};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::strings_column_wrapper expect_vals({"aaa", "bat", "$1"});
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TEST_F(groupby_min_string_test, zero_valid_values)
{
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::strings_column_wrapper vals({"año", "bit", "₹1"}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::strings_column_wrapper expect_vals({""}, all_nulls());
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TEST_F(groupby_min_string_test, min_sorted_strings)
{
// testcase replicated in issue #8717
cudf::test::strings_column_wrapper keys(
{"", "", "", "", "", "", "06", "06", "06", "06", "10", "10", "10", "10", "14", "14",
"14", "14", "18", "18", "18", "18", "22", "22", "22", "22", "26", "26", "26", "26", "30", "30",
"30", "30", "34", "34", "34", "34", "38", "38", "38", "38", "42", "42", "42", "42"},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
cudf::test::strings_column_wrapper vals(
{"", "", "", "", "", "", "06", "", "", "", "10", "", "", "", "14", "",
"", "", "18", "", "", "", "22", "", "", "", "26", "", "", "", "30", "",
"", "", "34", "", "", "", "38", "", "", "", "42", "", "", ""},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0});
cudf::test::strings_column_wrapper expect_keys(
{"06", "10", "14", "18", "22", "26", "30", "34", "38", "42", ""},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
cudf::test::strings_column_wrapper expect_vals(
{"06", "10", "14", "18", "22", "26", "30", "34", "38", "42", ""},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
force_use_sort_impl::NO,
cudf::null_policy::INCLUDE,
cudf::sorted::YES);
}
struct groupby_dictionary_min_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_min_test, basic)
{
using V = std::string;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2 };
cudf::test::dictionary_column_wrapper<V> vals{ "año", "bit", "₹1", "aaa", "zit", "bat", "aaa", "$1", "₹1", "wut"};
cudf::test::fixed_width_column_wrapper<K> expect_keys { 1, 2, 3 };
cudf::test::dictionary_column_wrapper<V> expect_vals_w({ "aaa", "bat", "$1" });
// clang-format on
auto expect_vals = cudf::dictionary::set_keys(expect_vals_w, vals.keys());
test_single_agg(keys,
vals,
expect_keys,
expect_vals->view(),
cudf::make_min_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals->view(),
cudf::make_min_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
TEST_F(groupby_dictionary_min_test, fixed_width)
{
using V = int64_t;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2 };
cudf::test::dictionary_column_wrapper<V> vals{ 0xABC, 0xBBB, 0xF1, 0xAAA, 0xFFF, 0xBAA, 0xAAA, 0x01, 0xF1, 0xEEE};
cudf::test::fixed_width_column_wrapper<K> expect_keys { 1, 2, 3 };
cudf::test::fixed_width_column_wrapper<V> expect_vals_w({ 0xAAA, 0xBAA, 0x01 });
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals_w,
cudf::make_min_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals_w,
cudf::make_min_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
template <typename T>
struct GroupByMinFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupByMinFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(GroupByMinFixedPointTest, GroupBySortMinDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
// clang-format on
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals_min = fp_wrapper{{0, 1, 2}, scale};
auto agg2 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(
keys, vals, expect_keys, expect_vals_min, std::move(agg2), force_use_sort_impl::YES);
}
}
TYPED_TEST(GroupByMinFixedPointTest, GroupByHashMinDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using K = int32_t;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
// clang-format on
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals_min = fp_wrapper{{0, 1, 2}, scale};
auto agg6 = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals_min, std::move(agg6));
}
}
struct groupby_min_struct_test : public cudf::test::BaseFixture {};
TEST_F(groupby_min_struct_test, basic)
{
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{"aaa", "bat", "$1"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{4, 6, 8};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_min_struct_test, slice_input)
{
constexpr int32_t dont_care{1};
auto const keys_original = cudf::test::fixed_width_column_wrapper<int32_t>{
dont_care, dont_care, 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, dont_care};
auto const vals_original = [] {
auto child1 = cudf::test::strings_column_wrapper{"dont_care",
"dont_care",
"año",
"bit",
"₹1",
"aaa",
"zit",
"bat",
"aab",
"$1",
"€1",
"wut",
"dont_care"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{
dont_care, dont_care, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, dont_care};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const keys = cudf::slice(keys_original, {2, 12})[0];
auto const vals = cudf::slice(vals_original, {2, 12})[0];
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{"aaa", "bat", "$1"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{4, 6, 8};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_min_struct_test, null_keys_and_values)
{
constexpr int32_t null{0};
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{
{1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4}, null_at(7)};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "" /*NULL*/, "" /*NULL*/, "$1", "€1", "wut", "" /*NULL*/};
auto child2 =
cudf::test::fixed_width_column_wrapper<int32_t>{9, 8, 7, 6, 5, null, null, 2, 1, 0, null};
return cudf::test::structs_column_wrapper{{child1, child2}, nulls_at({5, 6, 10})};
}();
auto const expect_keys =
cudf::test::fixed_width_column_wrapper<int32_t>{{1, 2, 3, 4}, no_nulls()};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{"aaa", "bit", "€1", "" /*NULL*/};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{6, 8, 1, null};
return cudf::test::structs_column_wrapper{{child1, child2}, null_at(3)};
}();
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_min_struct_test, values_with_null_child)
{
constexpr int32_t null{0};
{
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 1};
auto const vals = [] {
auto child1 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 1};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{{-1, null}, null_at(1)};
return cudf::test::structs_column_wrapper{child1, child2};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1};
auto const expect_vals = [] {
auto child1 = cudf::test::fixed_width_column_wrapper<int32_t>{1};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{{null}, null_at(0)};
return cudf::test::structs_column_wrapper{child1, child2};
}();
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
{
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 1};
auto const vals = [] {
auto child1 = cudf::test::fixed_width_column_wrapper<int32_t>{{-1, null}, null_at(1)};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{{null, null}, nulls_at({0, 1})};
return cudf::test::structs_column_wrapper{child1, child2};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1};
auto const expect_vals = [] {
auto child1 = cudf::test::fixed_width_column_wrapper<int32_t>{{null}, null_at(0)};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{{null}, null_at(0)};
return cudf::test::structs_column_wrapper{child1, child2};
}();
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
}
struct groupby_min_list_test : public cudf::test::BaseFixture {};
TEST_F(groupby_min_list_test, basic)
{
using lists = cudf::test::lists_column_wrapper<int32_t>;
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 1, 2};
auto const vals = lists{{1, 2}, {3, 4}, {5, 6, 7}, {0, 8}, {9, 10}};
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int>{1, 2, 3};
auto const expect_vals = lists{{0, 8}, {3, 4}, {5, 6, 7}};
test_single_agg(
keys, vals, expect_keys, expect_vals, cudf::make_min_aggregation<cudf::groupby_aggregation>());
}
TEST_F(groupby_min_list_test, slice_input)
{
using lists = cudf::test::lists_column_wrapper<int32_t>;
constexpr int32_t dont_care{1};
auto const keys_original =
cudf::test::fixed_width_column_wrapper<int32_t>{dont_care, 1, 2, 3, 1, 2, dont_care};
auto const vals_original =
lists{{1, 2, 3, 4, 5} /*dont care*/, {1, 2}, {3, 4}, {5, 6, 7}, {0, 8}, {9, 10}};
auto const keys = cudf::slice(keys_original, {1, 6})[0];
auto const vals = cudf::slice(vals_original, {1, 6})[0];
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int>{1, 2, 3};
auto const expect_vals = lists{{0, 8}, {3, 4}, {5, 6, 7}};
test_single_agg(
keys, vals, expect_keys, expect_vals, cudf::make_min_aggregation<cudf::groupby_aggregation>());
}
TEST_F(groupby_min_list_test, null_keys_and_values)
{
using lists = cudf::test::lists_column_wrapper<int32_t>;
constexpr int32_t null{0};
auto const keys =
cudf::test::fixed_width_column_wrapper<int32_t>{{1, 2, 3, null, 1, 2}, null_at(3)};
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int>{{1, 2, 3}, no_nulls()};
// Null list element.
{
auto const vals = lists{{{} /*null*/, {1, 2}, {3, 4}, {5, 6, 7}, {0, 8}, {9, 10}}, null_at(0)};
auto const expect_vals = lists{{0, 8}, {1, 2}, {3, 4}};
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_min_aggregation<cudf::groupby_aggregation>());
}
// Null child element.
{
auto const vals = lists{lists{{0, null}, null_at(1)},
lists{1, 2},
lists{3, 4},
lists{5, 6, 7},
lists{0, 8},
lists{9, 10}};
auto const expect_vals = lists{lists{{0, null}, null_at(1)}, {1, 2}, {3, 4}};
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_min_aggregation<cudf::groupby_aggregation>());
}
}
template <typename V>
struct groupby_min_floating_point_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_min_floating_point_test, cudf::test::FloatingPointTypes);
TYPED_TEST(groupby_min_floating_point_test, values_with_infinity)
{
using T = TypeParam;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_col = cudf::test::fixed_width_column_wrapper<T, int32_t>;
auto constexpr inf = std::numeric_limits<T>::infinity();
auto const keys = int32s_col{1, 2, 1, 2};
auto const vals = floats_col{static_cast<T>(1), static_cast<T>(1), -inf, static_cast<T>(2)};
auto const expected_keys = int32s_col{1, 2};
auto const expected_vals = floats_col{-inf, static_cast<T>(1)};
// Related issue: https://github.com/rapidsai/cudf/issues/11352
// The issue only occurs in sort-based aggregation.
auto agg = cudf::make_min_aggregation<cudf::groupby_aggregation>();
test_single_agg(
keys, vals, expected_keys, expected_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_min_floating_point_test, values_with_nan)
{
using T = TypeParam;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_col = cudf::test::fixed_width_column_wrapper<T, int32_t>;
auto constexpr nan = std::numeric_limits<T>::quiet_NaN();
auto const keys = int32s_col{1, 1};
auto const vals = floats_col{nan, nan};
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = vals;
requests[0].aggregations.emplace_back(cudf::make_min_aggregation<cudf::groupby_aggregation>());
// Without properly handling NaN, this will hang forever in hash-based aggregate (which is the
// default back-end for min/max in groupby context).
// This test is just to verify that the aggregate operation does not hang.
auto gb_obj = cudf::groupby::groupby(cudf::table_view({keys}));
auto const result = gb_obj.aggregate(requests);
EXPECT_EQ(result.first->num_rows(), 1);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/max_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/dictionary/update_keys.hpp>
#include <limits>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_max_test : public cudf::test::BaseFixture {};
using K = int32_t;
TYPED_TEST_SUITE(groupby_max_test, cudf::test::FixedWidthTypesWithoutFixedPoint);
TYPED_TEST(groupby_max_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MAX>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals({6, 9, 8});
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_max_test, empty_cols)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MAX>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_max_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MAX>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5});
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_max_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MAX>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_max_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MAX>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 0, 3, 1, 4, 5, 2, 8, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3, 5, 8, 0}, {1, 1, 1, 0});
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
struct groupby_max_string_test : public cudf::test::BaseFixture {};
TEST_F(groupby_max_string_test, basic)
{
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::strings_column_wrapper vals{
"año", "bit", "₹1", "aaa", "zit", "bat", "aaa", "$1", "₹1", "wut"};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::strings_column_wrapper expect_vals({"año", "zit", "₹1"});
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TEST_F(groupby_max_string_test, zero_valid_values)
{
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::strings_column_wrapper vals({"año", "bit", "₹1"}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::strings_column_wrapper expect_vals({""}, all_nulls());
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TEST_F(groupby_max_string_test, max_sorted_strings)
{
// testcase replicated in issue #8717
cudf::test::strings_column_wrapper keys(
{"", "", "", "", "", "", "06", "06", "06", "06", "10", "10", "10", "10", "14", "14",
"14", "14", "18", "18", "18", "18", "22", "22", "22", "22", "26", "26", "26", "26", "30", "30",
"30", "30", "34", "34", "34", "34", "38", "38", "38", "38", "42", "42", "42", "42"},
{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
cudf::test::strings_column_wrapper vals(
{"", "", "", "", "", "", "06", "", "", "", "10", "", "", "", "14", "",
"", "", "18", "", "", "", "22", "", "", "", "26", "", "", "", "30", "",
"", "", "34", "", "", "", "38", "", "", "", "42", "", "", ""},
{0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1,
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0});
cudf::test::strings_column_wrapper expect_keys(
{"06", "10", "14", "18", "22", "26", "30", "34", "38", "42", ""},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
cudf::test::strings_column_wrapper expect_vals(
{"06", "10", "14", "18", "22", "26", "30", "34", "38", "42", ""},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
// cudf::test::fixed_width_column_wrapper<size_type> expect_argmax(
// {6, 10, 14, 18, 22, 26, 30, 34, 38, 42, -1},
// {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
force_use_sort_impl::NO,
cudf::null_policy::INCLUDE,
cudf::sorted::YES);
}
struct groupby_dictionary_max_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_max_test, basic)
{
using V = std::string;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2 };
cudf::test::dictionary_column_wrapper<V> vals{ "año", "bit", "₹1", "aaa", "zit", "bat", "aaa", "$1", "₹1", "wut"};
cudf::test::fixed_width_column_wrapper<K> expect_keys { 1, 2, 3 };
cudf::test::dictionary_column_wrapper<V> expect_vals_w({ "año", "zit", "₹1" });
// clang-format on
auto expect_vals = cudf::dictionary::set_keys(expect_vals_w, vals.keys());
test_single_agg(keys,
vals,
expect_keys,
expect_vals->view(),
cudf::make_max_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals->view(),
cudf::make_max_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
TEST_F(groupby_dictionary_max_test, fixed_width)
{
using V = int64_t;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2 };
cudf::test::dictionary_column_wrapper<V> vals{ 0xABC, 0xBBB, 0xF1, 0xAAA, 0xFFF, 0xBAA, 0xAAA, 0x01, 0xF1, 0xEEE};
cudf::test::fixed_width_column_wrapper<K> expect_keys { 1, 2, 3 };
cudf::test::fixed_width_column_wrapper<V> expect_vals_w({ 0xABC, 0xFFF, 0xF1 });
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals_w,
cudf::make_max_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals_w,
cudf::make_max_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
template <typename T>
struct GroupByMaxFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupByMaxFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(GroupByMaxFixedPointTest, GroupBySortMaxDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using K = int32_t;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
// clang-format on
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals_max = fp_wrapper{{6, 9, 8}, scale};
auto agg3 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(
keys, vals, expect_keys, expect_vals_max, std::move(agg3), force_use_sort_impl::YES);
}
}
TYPED_TEST(GroupByMaxFixedPointTest, GroupByHashMaxDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using K = int32_t;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{ {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
// clang-format on
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals_max = fp_wrapper{{6, 9, 8}, scale};
auto agg7 = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals_max, std::move(agg7));
}
}
struct groupby_max_struct_test : public cudf::test::BaseFixture {};
TEST_F(groupby_max_struct_test, basic)
{
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{"año", "zit", "₹1"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 5, 3};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_max_struct_test, slice_input)
{
constexpr int32_t dont_care{1};
auto const keys_original = cudf::test::fixed_width_column_wrapper<int32_t>{
dont_care, dont_care, 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, dont_care};
auto const vals_original = [] {
auto child1 = cudf::test::strings_column_wrapper{"dont_care",
"dont_care",
"año",
"bit",
"₹1",
"aaa",
"zit",
"bat",
"aab",
"$1",
"€1",
"wut",
"dont_care"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{
dont_care, dont_care, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, dont_care};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const keys = cudf::slice(keys_original, {2, 12})[0];
auto const vals = cudf::slice(vals_original, {2, 12})[0];
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{"año", "zit", "₹1"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 5, 3};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_max_struct_test, null_keys_and_values)
{
constexpr int32_t null{0};
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{
{1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4}, null_at(7)};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "" /*NULL*/, "" /*NULL*/, "$1", "€1", "wut", "" /*NULL*/};
auto child2 =
cudf::test::fixed_width_column_wrapper<int32_t>{9, 8, 7, 6, 5, null, null, 2, 1, 0, null};
return cudf::test::structs_column_wrapper{{child1, child2}, nulls_at({5, 6, 10})};
}();
auto const expect_keys =
cudf::test::fixed_width_column_wrapper<int32_t>{{1, 2, 3, 4}, no_nulls()};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{"año", "zit", "₹1", "" /*NULL*/};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{9, 5, 7, null};
return cudf::test::structs_column_wrapper{{child1, child2}, null_at(3)};
}();
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_max_struct_test, values_with_null_child)
{
constexpr int32_t null{0};
{
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 1};
auto const vals = [] {
auto child1 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 1};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{{-1, null}, null_at(1)};
return cudf::test::structs_column_wrapper{child1, child2};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1};
auto const expect_vals = [] {
auto child1 = cudf::test::fixed_width_column_wrapper<int32_t>{1};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{-1};
return cudf::test::structs_column_wrapper{child1, child2};
}();
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
{
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 1};
auto const vals = [] {
auto child1 = cudf::test::fixed_width_column_wrapper<int32_t>{{-1, null}, null_at(1)};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{{null, null}, nulls_at({0, 1})};
return cudf::test::structs_column_wrapper{child1, child2};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1};
auto const expect_vals = [] {
auto child1 = cudf::test::fixed_width_column_wrapper<int32_t>{-1};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{{null}, null_at(0)};
return cudf::test::structs_column_wrapper{child1, child2};
}();
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
}
struct groupby_max_list_test : public cudf::test::BaseFixture {};
TEST_F(groupby_max_list_test, basic)
{
using lists = cudf::test::lists_column_wrapper<int32_t>;
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 1, 2};
auto const vals = lists{{1, 2}, {3, 4}, {5, 6, 7}, {0, 8}, {9, 10}};
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int>{1, 2, 3};
auto const expect_vals = lists{{1, 2}, {9, 10}, {5, 6, 7}};
test_single_agg(
keys, vals, expect_keys, expect_vals, cudf::make_max_aggregation<cudf::groupby_aggregation>());
}
TEST_F(groupby_max_list_test, slice_input)
{
using lists = cudf::test::lists_column_wrapper<int32_t>;
constexpr int32_t dont_care{1};
auto const keys_original =
cudf::test::fixed_width_column_wrapper<int32_t>{dont_care, 1, 2, 3, 1, 2, dont_care};
auto const vals_original =
lists{{1, 2, 3, 4, 5} /*dont care*/, {1, 2}, {3, 4}, {5, 6, 7}, {0, 8}, {9, 10}};
auto const keys = cudf::slice(keys_original, {1, 6})[0];
auto const vals = cudf::slice(vals_original, {1, 6})[0];
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int>{1, 2, 3};
auto const expect_vals = lists{{1, 2}, {9, 10}, {5, 6, 7}};
test_single_agg(
keys, vals, expect_keys, expect_vals, cudf::make_max_aggregation<cudf::groupby_aggregation>());
}
TEST_F(groupby_max_list_test, null_keys_and_values)
{
using lists = cudf::test::lists_column_wrapper<int32_t>;
constexpr int32_t null{0};
auto const keys =
cudf::test::fixed_width_column_wrapper<int32_t>{{1, 2, 3, null, 1, 2}, null_at(3)};
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int>{{1, 2, 3}, no_nulls()};
// Null list element.
{
auto const vals = lists{{{} /*null*/, {1, 2}, {3, 4}, {5, 6, 7}, {0, 8}, {9, 10}}, null_at(0)};
auto const expect_vals = lists{{0, 8}, {9, 10}, {3, 4}};
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_max_aggregation<cudf::groupby_aggregation>());
}
// Null child element.
{
auto const vals = lists{lists{{0, null}, null_at(1)},
lists{1, 2},
lists{3, 4},
lists{5, 6, 7},
lists{0, 8},
lists{9, 10}};
auto const expect_vals = lists{{0, 8}, {9, 10}, {3, 4}};
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_max_aggregation<cudf::groupby_aggregation>());
}
}
template <typename V>
struct groupby_max_floating_point_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_max_floating_point_test, cudf::test::FloatingPointTypes);
TYPED_TEST(groupby_max_floating_point_test, values_with_infinity)
{
using T = TypeParam;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_col = cudf::test::fixed_width_column_wrapper<T, int32_t>;
auto constexpr inf = std::numeric_limits<T>::infinity();
auto const keys = int32s_col{1, 2, 1, 2};
auto const vals = floats_col{static_cast<T>(1), static_cast<T>(1), inf, static_cast<T>(2)};
auto const expected_keys = int32s_col{1, 2};
auto const expected_vals = floats_col{inf, static_cast<T>(2)};
// Related issue: https://github.com/rapidsai/cudf/issues/11352
// The issue only occurs in sort-based cudf::aggregation.
auto agg = cudf::make_max_aggregation<cudf::groupby_aggregation>();
test_single_agg(
keys, vals, expected_keys, expected_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_max_floating_point_test, values_with_nan)
{
using T = TypeParam;
using int32s_col = cudf::test::fixed_width_column_wrapper<int32_t>;
using floats_col = cudf::test::fixed_width_column_wrapper<T, int32_t>;
auto constexpr nan = std::numeric_limits<T>::quiet_NaN();
auto const keys = int32s_col{1, 1};
auto const vals = floats_col{nan, nan};
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = vals;
requests[0].aggregations.emplace_back(cudf::make_max_aggregation<cudf::groupby_aggregation>());
// Without properly handling NaN, this will hang forever in hash-based aggregate (which is the
// default back-end for min/max in groupby context).
// This test is just to verify that the aggregate operation does not hang.
auto gb_obj = cudf::groupby::groupby(cudf::table_view({keys}));
auto const result = gb_obj.aggregate(requests);
EXPECT_EQ(result.first->num_rows(), 1);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/product_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_product_test : public cudf::test::BaseFixture {};
using supported_types = cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>;
TYPED_TEST_SUITE(groupby_product_test, supported_types);
TYPED_TEST(groupby_product_test, basic)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::PRODUCT>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys { 1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// { 1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys { 1, 2, 3 };
// { 0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({ 0., 180., 112. }, no_nulls());
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_product_aggregation<cudf::groupby_aggregation>());
}
TYPED_TEST(groupby_product_test, empty_cols)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::PRODUCT>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_product_aggregation<cudf::groupby_aggregation>());
}
TYPED_TEST(groupby_product_test, zero_valid_keys)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::PRODUCT>;
cudf::test::fixed_width_column_wrapper<K> keys({0, 0, 0}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_product_aggregation<cudf::groupby_aggregation>());
}
TYPED_TEST(groupby_product_test, zero_valid_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::PRODUCT>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({0, 0, 0}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_product_aggregation<cudf::groupby_aggregation>());
}
TYPED_TEST(groupby_product_test, null_keys_and_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::PRODUCT>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys( { 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{ 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals( { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3},
{ 0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 3, 4}, no_nulls());
// { _, 3, 6, 1, 4, 9, 2, 8, _}
cudf::test::fixed_width_column_wrapper<R> expect_vals({ 18., 36., 16., 3.},
{ 1, 1, 1, 0});
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_product_aggregation<cudf::groupby_aggregation>());
}
TYPED_TEST(groupby_product_test, dictionary)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::PRODUCT>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// { 1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 3 });
// { 0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({ 0., 180., 112. }, no_nulls());
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_product_aggregation<cudf::groupby_aggregation>());
}
TYPED_TEST(groupby_product_test, dictionary_with_nulls)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::PRODUCT>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
{1, 0, 0, 1, 1, 1, 1, 1, 1, 1}};
// { 1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 3 });
// { 0, 3, 6, @, 4, 5, 9, @, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({ 0., 180., 56. }, no_nulls());
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_product_aggregation<cudf::groupby_aggregation>());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/nunique_tests.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
template <typename V>
struct groupby_nunique_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_nunique_test, cudf::test::AllTypes);
TYPED_TEST(groupby_nunique_test, basic)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// clang-format off
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals{3, 4, 3};
cudf::test::fixed_width_column_wrapper<R> expect_bool_vals{2, 1, 1};
// clang-format on
auto agg = cudf::make_nunique_aggregation<cudf::groupby_aggregation>();
if (std::is_same<V, bool>())
test_single_agg(keys, vals, expect_keys, expect_bool_vals, std::move(agg));
else
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nunique_test, empty_cols)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_nunique_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nunique_test, basic_duplicates)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 3, 2, 2, 9};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals{2, 4, 1};
cudf::test::fixed_width_column_wrapper<R> expect_bool_vals{2, 1, 1};
auto agg = cudf::make_nunique_aggregation<cudf::groupby_aggregation>();
if (std::is_same<V, bool>())
test_single_agg(keys, vals, expect_keys, expect_bool_vals, std::move(agg));
else
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nunique_test, zero_valid_keys)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
cudf::test::fixed_width_column_wrapper<K> keys({0, 0, 0}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5});
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_nunique_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nunique_test, zero_valid_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({0, 0, 0}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals{0};
auto agg = cudf::make_nunique_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nunique_test, null_keys_and_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// {1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4},
cudf::test::iterators::no_nulls());
// all unique values only {3, 6, 1, 4, 9, 2, 8, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals{2, 3, 2, 0};
cudf::test::fixed_width_column_wrapper<R> expect_bool_vals{1, 1, 1, 0};
auto agg = cudf::make_nunique_aggregation<cudf::groupby_aggregation>();
if (std::is_same<V, bool>())
test_single_agg(keys, vals, expect_keys, expect_bool_vals, std::move(agg));
else
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nunique_test, null_keys_and_values_with_duplicates)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 3, 1, 2, 2, 1, 3, 3, 2, 4, 4, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 4, 4, 2},
{0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4},
cudf::test::iterators::no_nulls());
// { 3, 6,- 1, 4, 9,- 2*, 8, -*}
// unique, with null, dup, dup null
cudf::test::fixed_width_column_wrapper<R> expect_vals{2, 3, 2, 0};
cudf::test::fixed_width_column_wrapper<R> expect_bool_vals{1, 1, 1, 0};
auto agg = cudf::make_nunique_aggregation<cudf::groupby_aggregation>();
if (std::is_same<V, bool>())
test_single_agg(keys, vals, expect_keys, expect_bool_vals, std::move(agg));
else
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nunique_test, include_nulls)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 3, 1, 2, 2, 1, 3, 3, 2, 4, 4, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 4, 4, 2},
{0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4},
cudf::test::iterators::no_nulls());
// { 3, 6,- 1, 4, 9,- 2*, 8, -*}
// unique, with null, dup, dup null
cudf::test::fixed_width_column_wrapper<R> expect_vals{3, 4, 2, 1};
cudf::test::fixed_width_column_wrapper<R> expect_bool_vals{2, 2, 1, 1};
auto agg = cudf::make_nunique_aggregation<cudf::groupby_aggregation>(cudf::null_policy::INCLUDE);
if (std::is_same<V, bool>())
test_single_agg(keys, vals, expect_keys, expect_bool_vals, std::move(agg));
else
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nunique_test, dictionary)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NUNIQUE>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 3, 1, 2, 2, 1, 0, 3, 2, 4, 4, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
cudf::test::dictionary_column_wrapper<V> vals({0, 1, 2, 2, 3, 4, 0, 6, 7, 8, 9, 0, 0, 0},
{0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, cudf::test::iterators::no_nulls());
// { 3, 6,- 1, 4, 9,- 2*, 8, -*}
// unique, with null, dup, dup null
cudf::test::fixed_width_column_wrapper<R> expect_fixed_vals({3, 4, 2, 1});
cudf::test::fixed_width_column_wrapper<R> expect_bool_vals{2, 2, 1, 1};
// clang-format on
cudf::column_view expect_vals = (std::is_same<V, bool>()) ? cudf::column_view{expect_bool_vals}
: cudf::column_view{expect_fixed_vals};
test_single_agg(
keys,
vals,
expect_keys,
expect_vals,
cudf::make_nunique_aggregation<cudf::groupby_aggregation>(cudf::null_policy::INCLUDE));
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/std_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_std_test : public cudf::test::BaseFixture {};
using supported_types = cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>;
TYPED_TEST_SUITE(groupby_std_test, supported_types);
TYPED_TEST(groupby_std_test, basic)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::STD>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// clang-format off
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3., sqrt(131./12), sqrt(31./3)}, no_nulls());
// clang-format on
auto agg = cudf::make_std_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_std_test, empty_cols)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::STD>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_std_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_std_test, zero_valid_keys)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::STD>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_std_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_std_test, zero_valid_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::STD>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_std_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_std_test, null_keys_and_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::STD>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 3, 6, 1, 4, 9, 2, 8, 3}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3 / sqrt(2), 7 / sqrt(3), 3 * sqrt(2), 0.},
{1, 1, 1, 0});
auto agg = cudf::make_std_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_std_test, ddof_non_default)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::STD>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 3, 6, 1, 4, 9, 2, 8, 3}
cudf::test::fixed_width_column_wrapper<R> expect_vals({0., 7 * sqrt(2. / 3), 0., 0.},
{0, 1, 0, 0});
auto agg = cudf::make_std_aggregation<cudf::groupby_aggregation>(2);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_std_test, dictionary)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::STD>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3});
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3., sqrt(131./12), sqrt(31./3)}, no_nulls());
// clang-format on
test_single_agg(
keys, vals, expect_keys, expect_vals, cudf::make_std_aggregation<cudf::groupby_aggregation>());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/sum_scan_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using key_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
template <typename T>
struct groupby_sum_scan_test : public cudf::test::BaseFixture {
using V = T;
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
using value_wrapper = cudf::test::fixed_width_column_wrapper<V, int32_t>;
using result_wrapper = cudf::test::fixed_width_column_wrapper<R, int32_t>;
};
using supported_types =
cudf::test::Concat<cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>,
cudf::test::DurationTypes>;
TYPED_TEST_SUITE(groupby_sum_scan_test, supported_types);
TYPED_TEST(groupby_sum_scan_test, basic)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys {1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
value_wrapper vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
key_wrapper expect_keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
result_wrapper expect_vals{0, 3, 9, 1, 5, 10, 19, 2, 9, 17};
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_scan_test, pre_sorted)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
value_wrapper vals{0, 3, 6, 1, 4, 5, 9, 2, 7, 8};
key_wrapper expect_keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
result_wrapper expect_vals{0, 3, 9, 1, 5, 10, 19, 2, 9, 17};
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
cudf::null_policy::EXCLUDE,
cudf::sorted::YES);
}
TYPED_TEST(groupby_sum_scan_test, empty_cols)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys{};
value_wrapper vals{};
key_wrapper expect_keys{};
result_wrapper expect_vals{};
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_scan_test, zero_valid_keys)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys({1, 2, 3}, cudf::test::iterators::all_nulls());
value_wrapper vals{3, 4, 5};
key_wrapper expect_keys{};
result_wrapper expect_vals{};
auto agg = cudf::make_sum_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_scan_test, zero_valid_values)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys{1, 1, 1};
value_wrapper vals({3, 4, 5}, cudf::test::iterators::all_nulls());
key_wrapper expect_keys{1, 1, 1};
result_wrapper expect_vals({3, 4, 5}, cudf::test::iterators::all_nulls());
auto agg = cudf::make_sum_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_sum_scan_test, null_keys_and_values)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys( {1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4}, {1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
value_wrapper vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4}, {0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// { 1, 1, 1, 2, 2, 2, 2, 3, *, 3, 4};
key_wrapper expect_keys( { 1, 1, 1, 2, 2, 2, 2, 3, 3, 4}, cudf::test::iterators::no_nulls());
// { -, 3, 6, 1, 4, -, 9, 2, _, 8, -}
result_wrapper expect_vals({-1, 3, 9, 1, 5, -1, 14, 2, 10, -1},
{ 0, 1, 1, 1, 1, 0, 1, 1, 1, 0});
// clang-format on
auto agg = cudf::make_sum_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
template <typename T>
struct GroupBySumScanFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupBySumScanFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(GroupBySumScanFixedPointTest, GroupBySortSumScanDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = key_wrapper{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
auto const expect_keys = key_wrapper{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
auto const expect_vals_sum = fp_wrapper{{0, 3, 9, 1, 5, 10, 19, 2, 9, 17}, scale};
// clang-format on
auto agg2 = cudf::make_sum_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals_sum, std::move(agg2));
}
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/tdigest_tests.cu
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/tdigest_utilities.cuh>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/detail/tdigest/tdigest.hpp>
#include <cudf/tdigest/tdigest_column_view.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <thrust/copy.h>
#include <thrust/fill.h>
#include <thrust/iterator/counting_iterator.h>
/**
* @brief Functor to generate a tdigest by key.
*
*/
struct tdigest_gen_grouped {
template <
typename T,
typename std::enable_if_t<cudf::is_numeric<T>() || cudf::is_fixed_point<T>()>* = nullptr>
std::unique_ptr<cudf::column> operator()(cudf::column_view const& keys,
cudf::column_view const& values,
int delta)
{
cudf::table_view t({keys});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({values, std::move(aggregations)});
auto result = gb.aggregate(requests);
return std::move(result.second[0].results[0]);
}
template <
typename T,
typename std::enable_if_t<!cudf::is_numeric<T>() && !cudf::is_fixed_point<T>()>* = nullptr>
std::unique_ptr<cudf::column> operator()(cudf::column_view const& keys,
cudf::column_view const& values,
int delta)
{
CUDF_FAIL("Invalid tdigest test type");
}
};
/**
* @brief Functor for generating a tdigest using groupby with a constant key.
*
*/
struct tdigest_groupby_simple_op {
std::unique_ptr<cudf::column> operator()(cudf::column_view const& values, int delta) const
{
// make a simple set of matching keys.
auto keys = cudf::make_fixed_width_column(
cudf::data_type{cudf::type_id::INT32}, values.size(), cudf::mask_state::UNALLOCATED);
thrust::fill(rmm::exec_policy(cudf::get_default_stream()),
keys->mutable_view().template begin<int>(),
keys->mutable_view().template end<int>(),
0);
cudf::table_view t({*keys});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({values, std::move(aggregations)});
auto result = gb.aggregate(requests);
return std::move(result.second[0].results[0]);
}
};
/**
* @brief Functor for merging tdigests using groupby with a constant key.
*
*/
struct tdigest_groupby_simple_merge_op {
std::unique_ptr<cudf::column> operator()(cudf::column_view const& merge_values,
int merge_delta) const
{
// make a simple set of matching keys.
auto merge_keys = cudf::make_fixed_width_column(
cudf::data_type{cudf::type_id::INT32}, merge_values.size(), cudf::mask_state::UNALLOCATED);
thrust::fill(rmm::exec_policy(cudf::get_default_stream()),
merge_keys->mutable_view().template begin<int>(),
merge_keys->mutable_view().template end<int>(),
0);
cudf::table_view key_table({*merge_keys});
cudf::groupby::groupby gb(key_table);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(
cudf::make_merge_tdigest_aggregation<cudf::groupby_aggregation>(merge_delta));
requests.push_back({merge_values, std::move(aggregations)});
auto result = gb.aggregate(requests);
return std::move(result.second[0].results[0]);
}
};
template <typename T>
struct TDigestAllTypes : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(TDigestAllTypes, cudf::test::NumericTypes);
TYPED_TEST(TDigestAllTypes, Simple)
{
using T = TypeParam;
cudf::test::tdigest_simple_aggregation<T>(tdigest_groupby_simple_op{});
}
TYPED_TEST(TDigestAllTypes, SimpleWithNulls)
{
using T = TypeParam;
cudf::test::tdigest_simple_with_nulls_aggregation<T>(tdigest_groupby_simple_op{});
}
TYPED_TEST(TDigestAllTypes, AllNull)
{
using T = TypeParam;
cudf::test::tdigest_simple_all_nulls_aggregation<T>(tdigest_groupby_simple_op{});
}
TYPED_TEST(TDigestAllTypes, LargeGroups)
{
auto _values = cudf::test::generate_standardized_percentile_distribution(
cudf::data_type{cudf::type_id::FLOAT64});
int const delta = 1000;
// generate a random set of keys
std::vector<int> h_keys;
h_keys.reserve(_values->size());
auto iter = thrust::make_counting_iterator(0);
std::transform(iter, iter + _values->size(), std::back_inserter(h_keys), [](int i) {
return static_cast<int>(round(cudf::test::rand_range(0, 8)));
});
cudf::test::fixed_width_column_wrapper<int> _keys(h_keys.begin(), h_keys.end());
// group the input values together
cudf::table_view k({_keys});
cudf::groupby::groupby setup_gb(k);
cudf::table_view v({*_values});
auto groups = setup_gb.get_groups(v);
// slice it all up so we have keys/columns for everything.
std::vector<cudf::column_view> keys;
std::vector<cudf::column_view> values;
for (size_t idx = 0; idx < groups.offsets.size() - 1; idx++) {
auto k =
cudf::slice(groups.keys->get_column(0), {groups.offsets[idx], groups.offsets[idx + 1]});
keys.push_back(k[0]);
auto v =
cudf::slice(groups.values->get_column(0), {groups.offsets[idx], groups.offsets[idx + 1]});
values.push_back(v[0]);
}
// generate a separate tdigest for each group
std::vector<std::unique_ptr<cudf::column>> parts;
std::transform(
iter, iter + values.size(), std::back_inserter(parts), [&keys, &values, delta](int i) {
cudf::table_view t({keys[i]});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({values[i], std::move(aggregations)});
auto result = gb.aggregate(requests);
return std::move(result.second[0].results[0]);
});
std::vector<cudf::column_view> part_views;
std::transform(parts.begin(),
parts.end(),
std::back_inserter(part_views),
[](std::unique_ptr<cudf::column> const& col) { return col->view(); });
auto merged_parts = cudf::concatenate(part_views);
// generate a tdigest on the whole input set
cudf::table_view t({_keys});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({*_values, std::move(aggregations)});
auto result = gb.aggregate(requests);
// verify that they end up the same.
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*result.second[0].results[0], *merged_parts);
}
struct TDigestTest : public cudf::test::BaseFixture {};
TEST_F(TDigestTest, EmptyMixed)
{
cudf::test::fixed_width_column_wrapper<double> values{
{123456.78, 10.0, 20.0, 25.0, 30.0, 40.0, 50.0, 60.0, 70.0}, {1, 0, 0, 1, 0, 0, 1, 1, 0}};
cudf::test::strings_column_wrapper keys{"b", "a", "c", "c", "d", "d", "e", "e", "f"};
auto const delta = 1000;
cudf::table_view t({keys});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({values, std::move(aggregations)});
auto result = gb.aggregate(requests);
using FCW = cudf::test::fixed_width_column_wrapper<double>;
auto expected =
cudf::test::make_expected_tdigest_column({{FCW{}, FCW{}, 0, 0},
{FCW{123456.78}, FCW{1.0}, 123456.78, 123456.78},
{FCW{25.0}, FCW{1.0}, 25.0, 25.0},
{FCW{}, FCW{}, 0, 0},
{FCW{50.0, 60.0}, FCW{1.0, 1.0}, 50.0, 60.0},
{FCW{}, FCW{}, 0, 0}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result.second[0].results[0], *expected);
}
TEST_F(TDigestTest, LargeInputDouble)
{
cudf::test::tdigest_simple_large_input_double_aggregation(tdigest_groupby_simple_op{});
}
TEST_F(TDigestTest, LargeInputInt)
{
cudf::test::tdigest_simple_large_input_int_aggregation(tdigest_groupby_simple_op{});
}
TEST_F(TDigestTest, LargeInputDecimal)
{
cudf::test::tdigest_simple_large_input_decimal_aggregation(tdigest_groupby_simple_op{});
}
struct TDigestMergeTest : public cudf::test::BaseFixture {};
// Note: there is no need to test different types here as the internals of a tdigest are always
// the same regardless of input.
TEST_F(TDigestMergeTest, Simple)
{
cudf::test::tdigest_merge_simple(tdigest_groupby_simple_op{}, tdigest_groupby_simple_merge_op{});
}
struct key_groups {
__device__ cudf::size_type operator()(cudf::size_type i) { return i < 250000 ? 0 : 1; }
};
TEST_F(TDigestMergeTest, Grouped)
{
auto values = cudf::test::generate_standardized_percentile_distribution(
cudf::data_type{cudf::type_id::FLOAT64});
ASSERT_EQ(values->size(), 750000);
// all in the same group
auto keys = cudf::make_fixed_width_column(
cudf::data_type{cudf::type_id::INT32}, values->size(), cudf::mask_state::UNALLOCATED);
// 3 groups. 0-250000 in group 0. 250000-500000 in group 1 and 500000-750000 in group 1
auto key_iter = cudf::detail::make_counting_transform_iterator(0, key_groups{});
thrust::copy(rmm::exec_policy(cudf::get_default_stream()),
key_iter,
key_iter + keys->size(),
keys->mutable_view().template begin<int>());
auto split_values = cudf::split(*values, {250000, 500000});
auto grouped_split_values = cudf::split(*values, {250000});
auto split_keys = cudf::split(*keys, {250000, 500000});
int const delta = 1000;
// generate separate digests
std::vector<std::unique_ptr<cudf::column>> parts;
auto iter = thrust::make_counting_iterator(0);
std::transform(
iter,
iter + split_values.size(),
std::back_inserter(parts),
[&split_keys, &split_values, delta](int i) {
cudf::table_view t({split_keys[i]});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({split_values[i], std::move(aggregations)});
auto result = gb.aggregate(requests);
return std::move(result.second[0].results[0]);
});
std::vector<cudf::column_view> part_views;
std::transform(parts.begin(),
parts.end(),
std::back_inserter(part_views),
[](std::unique_ptr<cudf::column> const& col) { return col->view(); });
// merge delta = 1000
{
int const merge_delta = 1000;
// merge them
auto merge_input = cudf::concatenate(part_views);
cudf::test::fixed_width_column_wrapper<int> merge_keys{0, 1, 1};
cudf::table_view key_table({merge_keys});
cudf::groupby::groupby gb(key_table);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(
cudf::make_merge_tdigest_aggregation<cudf::groupby_aggregation>(merge_delta));
requests.push_back({*merge_input, std::move(aggregations)});
auto result = gb.aggregate(requests);
ASSERT_EQ(result.second[0].results[0]->size(), 2);
cudf::tdigest::tdigest_column_view tdv(*result.second[0].results[0]);
// verify centroids
std::vector<cudf::test::expected_value> expected{// group 0
{0, 0.00013945158577498588, 2},
{10, 0.04804393446447509375, 50},
{66, 2.10089484962640948851, 316},
{139, 8.92977366346101852912, 601},
{243, 23.89152910016953867967, 784},
{366, 41.62636569363655780762, 586},
{432, 47.73085102980330418632, 326},
{460, 49.20637897385523018556, 196},
{501, 49.99998311512171511595, 1},
// group 1
{502 + 0, 50.00022508669655252334, 2},
{502 + 15, 50.05415694538910287292, 74},
{502 + 70, 51.21421484112906341579, 334},
{502 + 150, 55.19367617848146778670, 635},
{502 + 260, 63.24605285552920008740, 783},
{502 + 380, 76.99522005804017510400, 1289},
{502 + 440, 84.22673817294192133431, 758},
{502 + 490, 88.11787981529532487457, 784},
{502 + 555, 93.02766411136053648079, 704},
{502 + 618, 96.91486035315536184953, 516},
{502 + 710, 99.87755861436669135855, 110},
{502 + 733, 99.99970905482754801596, 1}};
cudf::test::tdigest_sample_compare(tdv, expected);
// verify min/max
auto split_results = cudf::split(*result.second[0].results[0], {1});
auto iter = thrust::make_counting_iterator(0);
std::for_each(iter, iter + split_results.size(), [&](cudf::size_type i) {
auto copied = std::make_unique<cudf::column>(split_results[i]);
cudf::test::tdigest_minmax_compare<double>(cudf::tdigest::tdigest_column_view(*copied),
grouped_split_values[i]);
});
}
// merge delta = 100
{
int const merge_delta = 100;
// merge them
auto merge_input = cudf::concatenate(part_views);
cudf::test::fixed_width_column_wrapper<int> merge_keys{0, 1, 1};
cudf::table_view key_table({merge_keys});
cudf::groupby::groupby gb(key_table);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(
cudf::make_merge_tdigest_aggregation<cudf::groupby_aggregation>(merge_delta));
requests.push_back({*merge_input, std::move(aggregations)});
auto result = gb.aggregate(requests);
ASSERT_EQ(result.second[0].results[0]->size(), 2);
cudf::tdigest::tdigest_column_view tdv(*result.second[0].results[0]);
// verify centroids
std::vector<cudf::test::expected_value> expected{// group 0
{0, 0.02182479870203561656, 231},
{3, 0.60625795002234528219, 1688},
{13, 8.40462931740497687372, 5867},
{27, 28.79997783486397722186, 7757},
{35, 40.22391421196020644402, 6224},
{45, 48.96506331299028857984, 2225},
{50, 49.99979491345574444949, 4},
// group 1
{51 + 0, 50.02171921312970681583, 460},
{51 + 5, 51.45308398121498072442, 5074},
{51 + 11, 55.96880716301625113829, 10011},
{51 + 22, 70.18029861315150697010, 15351},
{51 + 38, 92.65943436519887654867, 10718},
{51 + 47, 99.27745505225347244505, 3639}};
cudf::test::tdigest_sample_compare(tdv, expected);
// verify min/max
auto split_results = cudf::split(*result.second[0].results[0], {1});
auto iter = thrust::make_counting_iterator(0);
std::for_each(iter, iter + split_results.size(), [&](cudf::size_type i) {
auto copied = std::make_unique<cudf::column>(split_results[i]);
cudf::test::tdigest_minmax_compare<double>(cudf::tdigest::tdigest_column_view(*copied),
grouped_split_values[i]);
});
}
// merge delta = 10
{
int const merge_delta = 10;
// merge them
auto merge_input = cudf::concatenate(part_views);
cudf::test::fixed_width_column_wrapper<int> merge_keys{0, 1, 1};
cudf::table_view key_table({merge_keys});
cudf::groupby::groupby gb(key_table);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(
cudf::make_merge_tdigest_aggregation<cudf::groupby_aggregation>(merge_delta));
requests.push_back({*merge_input, std::move(aggregations)});
auto result = gb.aggregate(requests);
ASSERT_EQ(result.second[0].results[0]->size(), 2);
cudf::tdigest::tdigest_column_view tdv(*result.second[0].results[0]);
// verify centroids
std::vector<cudf::test::expected_value> expected{// group 0
{0, 2.34644806683495144028, 23623},
{1, 10.95523693698660672169, 62290},
{2, 24.90731657803452847588, 77208},
{3, 38.88062495289155862110, 62658},
{4, 47.56288303840698006297, 24217},
{5, 49.99979491345574444949, 4},
// group 1
{6 + 0, 52.40174463129091719793, 47410},
{6 + 1, 60.97025126481504031517, 124564},
{6 + 2, 74.91722742839780835311, 154387},
{6 + 3, 88.87559489177009197647, 124810},
{6 + 4, 97.55823307073454486726, 48817},
{6 + 5, 99.99901807905750672489, 12}};
cudf::test::tdigest_sample_compare(tdv, expected);
// verify min/max
auto split_results = cudf::split(*result.second[0].results[0], {1});
auto iter = thrust::make_counting_iterator(0);
std::for_each(iter, iter + split_results.size(), [&](cudf::size_type i) {
auto copied = std::make_unique<cudf::column>(split_results[i]);
cudf::test::tdigest_minmax_compare<double>(cudf::tdigest::tdigest_column_view(*copied),
grouped_split_values[i]);
});
}
}
TEST_F(TDigestMergeTest, Empty)
{
cudf::test::tdigest_merge_empty(tdigest_groupby_simple_merge_op{});
}
TEST_F(TDigestMergeTest, EmptyGroups)
{
cudf::test::fixed_width_column_wrapper<double> values_b{{126, 15, 1, 99, 67, 55, 2},
{1, 0, 0, 1, 1, 1, 1}};
cudf::test::fixed_width_column_wrapper<double> values_d{{100, 200, 300, 400, 500, 600, 700},
{1, 1, 1, 1, 1, 1, 0}};
cudf::test::fixed_width_column_wrapper<int> keys{0, 0, 0, 0, 0, 0, 0};
int const delta = 1000;
auto a = cudf::tdigest::detail::make_empty_tdigest_column(cudf::get_default_stream(),
rmm::mr::get_current_device_resource());
auto b = cudf::type_dispatcher(
static_cast<cudf::column_view>(values_b).type(), tdigest_gen_grouped{}, keys, values_b, delta);
auto c = cudf::tdigest::detail::make_empty_tdigest_column(cudf::get_default_stream(),
rmm::mr::get_current_device_resource());
auto d = cudf::type_dispatcher(
static_cast<cudf::column_view>(values_d).type(), tdigest_gen_grouped{}, keys, values_d, delta);
auto e = cudf::tdigest::detail::make_empty_tdigest_column(cudf::get_default_stream(),
rmm::mr::get_current_device_resource());
std::vector<cudf::column_view> cols;
cols.push_back(*a);
cols.push_back(*b);
cols.push_back(*c);
cols.push_back(*d);
cols.push_back(*e);
auto values = cudf::concatenate(cols);
cudf::test::fixed_width_column_wrapper<int> merge_keys{0, 0, 1, 0, 2};
cudf::table_view t({merge_keys});
cudf::groupby::groupby gb(t);
std::vector<cudf::groupby::aggregation_request> requests;
std::vector<std::unique_ptr<cudf::groupby_aggregation>> aggregations;
aggregations.push_back(cudf::make_merge_tdigest_aggregation<cudf::groupby_aggregation>(delta));
requests.push_back({*values, std::move(aggregations)});
auto result = gb.aggregate(requests);
using FCW = cudf::test::fixed_width_column_wrapper<double>;
cudf::test::fixed_width_column_wrapper<double> expected_means{
2, 55, 67, 99, 100, 126, 200, 300, 400, 500, 600};
cudf::test::fixed_width_column_wrapper<double> expected_weights{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
auto expected = cudf::test::make_expected_tdigest_column(
{{expected_means, expected_weights, 2, 600}, {FCW{}, FCW{}, 0, 0}, {FCW{}, FCW{}, 0, 0}});
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected, *result.second[0].results[0]);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/quantile_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_quantile_test : public cudf::test::BaseFixture {};
using supported_types = cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>;
using K = int32_t;
TYPED_TEST_SUITE(groupby_quantile_test, supported_types);
TYPED_TEST(groupby_quantile_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::QUANTILE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// clang-format on
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3., 4.5, 7.}, no_nulls());
// clang-format on
auto agg =
cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.5}, cudf::interpolation::LINEAR);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_quantile_test, empty_cols)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::QUANTILE>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg =
cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.5}, cudf::interpolation::LINEAR);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_quantile_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::QUANTILE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg =
cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.5}, cudf::interpolation::LINEAR);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_quantile_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::QUANTILE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg =
cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.5}, cudf::interpolation::LINEAR);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_quantile_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::QUANTILE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 3, 6, 1, 4, 9, 2, 8, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals({4.5, 4., 5., 0.}, {1, 1, 1, 0});
auto agg =
cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.5}, cudf::interpolation::LINEAR);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_quantile_test, multiple_quantile)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::QUANTILE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// clang-format off
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({1.5, 4.5, 3.25, 6., 4.5, 7.5}, no_nulls());
// clang-format on
auto agg = cudf::make_quantile_aggregation<cudf::groupby_aggregation>(
{0.25, 0.75}, cudf::interpolation::LINEAR);
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
force_use_sort_impl::YES,
cudf::null_policy::EXCLUDE,
cudf::sorted::NO,
{},
{},
cudf::sorted::YES);
}
TYPED_TEST(groupby_quantile_test, interpolation_types)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::QUANTILE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 9};
// clang-format off
// {1, 1, 1, 2, 2, 2, 2, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
// {0, 3, 6, 1, 4, 5, 9, 2, 7}
cudf::test::fixed_width_column_wrapper<R> expect_vals1({2.4, 4.2, 4.}, no_nulls());
auto agg1 = cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.4}, cudf::interpolation::LINEAR);
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg1));
// {0, 3, 6, 1, 4, 5, 9, 2, 7}
cudf::test::fixed_width_column_wrapper<R> expect_vals2({3, 4, 2}, no_nulls());
auto agg2 = cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.4}, cudf::interpolation::NEAREST);
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg2));
// {0, 3, 6, 1, 4, 5, 9, 2, 7}
cudf::test::fixed_width_column_wrapper<R> expect_vals3({0, 4, 2}, no_nulls());
auto agg3 = cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.4}, cudf::interpolation::LOWER);
test_single_agg(keys, vals, expect_keys, expect_vals3, std::move(agg3));
// {0, 3, 6, 1, 4, 5, 9, 2, 7}
cudf::test::fixed_width_column_wrapper<R> expect_vals4({3, 5, 7}, no_nulls());
auto agg4 = cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.4}, cudf::interpolation::HIGHER);
test_single_agg(keys, vals, expect_keys, expect_vals4, std::move(agg4));
// {0, 3, 6, 1, 4, 5, 9, 2, 7}
cudf::test::fixed_width_column_wrapper<R> expect_vals5({1.5, 4.5, 4.5}, no_nulls());
auto agg5 = cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.4}, cudf::interpolation::MIDPOINT);
test_single_agg(keys, vals, expect_keys, expect_vals5, std::move(agg5));
// clang-format on
}
TYPED_TEST(groupby_quantile_test, dictionary)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::QUANTILE>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3});
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3., 4.5, 7.}, no_nulls());
// clang-format on
test_single_agg(
keys,
vals,
expect_keys,
expect_vals,
cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.5}, cudf::interpolation::LINEAR));
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/m2_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/aggregation.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/groupby.hpp>
using namespace cudf::test::iterators;
namespace {
constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::FIRST_ERROR};
constexpr int32_t null{0}; // Mark for null elements
constexpr double NaN{std::numeric_limits<double>::quiet_NaN()}; // Mark for NaN double elements
template <class T>
using keys_col = cudf::test::fixed_width_column_wrapper<T, int32_t>;
template <class T>
using vals_col = cudf::test::fixed_width_column_wrapper<T>;
template <class T>
using M2s_col = cudf::test::fixed_width_column_wrapper<T>;
auto compute_M2(cudf::column_view const& keys, cudf::column_view const& values)
{
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = values;
requests[0].aggregations.emplace_back(cudf::make_m2_aggregation<cudf::groupby_aggregation>());
auto gb_obj = cudf::groupby::groupby(cudf::table_view({keys}));
auto result = gb_obj.aggregate(requests);
return std::pair(std::move(result.first->release()[0]), std::move(result.second[0].results[0]));
}
} // namespace
template <class T>
struct GroupbyM2TypedTest : public cudf::test::BaseFixture {};
using TestTypes = cudf::test::Concat<cudf::test::Types<int8_t, int16_t, int32_t, int64_t>,
cudf::test::FloatingPointTypes>;
TYPED_TEST_SUITE(GroupbyM2TypedTest, TestTypes);
TYPED_TEST(GroupbyM2TypedTest, EmptyInput)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
auto const keys = keys_col<T>{};
auto const vals = vals_col<T>{};
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_M2s = M2s_col<R>{};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
TYPED_TEST(GroupbyM2TypedTest, AllNullKeysInput)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
auto const keys = keys_col<T>{{1, 2, 3}, all_nulls()};
auto const vals = vals_col<T>{3, 4, 5};
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_keys = keys_col<T>{};
auto const expected_M2s = M2s_col<R>{};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
TYPED_TEST(GroupbyM2TypedTest, AllNullValuesInput)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
auto const keys = keys_col<T>{1, 2, 3};
auto const vals = vals_col<T>{{3, 4, 5}, all_nulls()};
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_M2s = M2s_col<R>{{null, null, null}, all_nulls()};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
TYPED_TEST(GroupbyM2TypedTest, SimpleInput)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// key = 1: vals = [0, 3, 6]
// key = 2: vals = [1, 4, 5, 9]
// key = 3: vals = [2, 7, 8]
auto const keys = keys_col<T>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = vals_col<T>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_keys = keys_col<T>{1, 2, 3};
auto const expected_M2s = M2s_col<R>{18.0, 32.75, 20.0 + 2.0 / 3.0};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
TYPED_TEST(GroupbyM2TypedTest, SimpleInputHavingNegativeValues)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// key = 1: vals = [0, 3, -6]
// key = 2: vals = [1, -4, -5, 9]
// key = 3: vals = [-2, 7, -8]
auto const keys = keys_col<T>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = vals_col<T>{0, 1, -2, 3, -4, -5, -6, 7, -8, 9};
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_keys = keys_col<T>{1, 2, 3};
auto const expected_M2s = M2s_col<R>{42.0, 122.75, 114.0};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
TYPED_TEST(GroupbyM2TypedTest, ValuesHaveNulls)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
auto const keys = keys_col<T>{1, 2, 3, 4, 5, 2, 3, 2};
auto const vals = vals_col<T>{{0, null, 2, 3, null, 5, 6, 7}, nulls_at({1, 4})};
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_keys = keys_col<T>{1, 2, 3, 4, 5};
auto const expected_M2s = M2s_col<R>{{0.0, 2.0, 8.0, 0.0, 0.0 /*NULL*/}, null_at(4)};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
TYPED_TEST(GroupbyM2TypedTest, KeysAndValuesHaveNulls)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// key = 1: vals = [null, 3, 6]
// key = 2: vals = [1, 4, null, 9]
// key = 3: vals = [2, 8]
// key = 4: vals = [null]
auto const keys = keys_col<T>{{1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4}, null_at(7)};
auto const vals = vals_col<T>{{null, 1, 2, 3, 4, null, 6, 7, 8, 9, null}, nulls_at({0, 5, 10})};
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_keys = keys_col<T>{1, 2, 3, 4};
auto const expected_M2s = M2s_col<R>{{4.5, 32.0 + 2.0 / 3.0, 18.0, 0.0 /*NULL*/}, null_at(3)};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
TYPED_TEST(GroupbyM2TypedTest, InputHaveNullsAndNaNs)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// key = 1: vals = [0, 3, 6]
// key = 2: vals = [1, 4, NaN, 9]
// key = 3: vals = [null, 2, 8]
// key = 4: vals = [null, 10, NaN]
auto const keys = keys_col<T>{{4, 3, 1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4, 4}, null_at(9)};
auto const vals = vals_col<double>{
{0.0 /*NULL*/, 0.0 /*NULL*/, 0.0, 1.0, 2.0, 3.0, 4.0, NaN, 6.0, 7.0, 8.0, 9.0, 10.0, NaN},
nulls_at({0, 1})};
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_keys = keys_col<T>{1, 2, 3, 4};
auto const expected_M2s = M2s_col<R>{18.0, NaN, 18.0, NaN};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
TYPED_TEST(GroupbyM2TypedTest, SlicedColumnsInput)
{
using T = TypeParam;
using R = cudf::detail::target_type_t<T, cudf::aggregation::M2>;
// This test should compute M2 aggregation on the same dataset as the InputHaveNullsAndNaNs test.
// i.e.:
//
// key = 1: vals = [0, 3, 6]
// key = 2: vals = [1, 4, NaN, 9]
// key = 3: vals = [null, 2, 8]
// key = 4: vals = [null, 10, NaN]
auto const keys_original =
keys_col<T>{{
1, 2, 3, 4, 5, 1, 2, 3, 4, 5, // will not use, don't care
4, 3, 1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4, 4, // use this
1, 2, 3, 4, 5, 1, 2, 3, 4, 5 // will not use, don't care
},
null_at(19)};
auto const vals_original = vals_col<double>{
{
3.0, 2.0, 5.0, 4.0, 6.0, 9.0, 1.0, 0.0, 1.0, 7.0, // will not use, don't care
0.0, 0.0, 0.0, 1.0, 2.0, 3.0, 4.0, NaN, 6.0, 7.0, 8.0, 9.0, 10.0, NaN, // use this
9.0, 10.0, 11.0, 12.0, 0.0, 5.0, 1.0, 20.0, 19.0, 15.0 // will not use, don't care
},
nulls_at({10, 11})};
auto const keys = cudf::slice(keys_original, {10, 24})[0];
auto const vals = cudf::slice(vals_original, {10, 24})[0];
auto const [out_keys, out_M2s] = compute_M2(keys, vals);
auto const expected_keys = keys_col<T>{1, 2, 3, 4};
auto const expected_M2s = M2s_col<R>{18.0, NaN, 18.0, NaN};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(expected_M2s, *out_M2s, verbosity);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/count_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
template <typename V>
struct groupby_count_test : public cudf::test::BaseFixture {};
using K = int32_t;
TYPED_TEST_SUITE(groupby_count_test, cudf::test::AllTypes);
TYPED_TEST(groupby_count_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals{3, 4, 3};
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg1 = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg1), force_use_sort_impl::YES);
auto agg2 = cudf::make_count_aggregation<cudf::groupby_aggregation>(cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
TYPED_TEST(groupby_count_test, empty_cols)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals;
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals;
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg1 = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg1), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_count_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg1 = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg1), force_use_sort_impl::YES);
auto agg2 = cudf::make_count_aggregation<cudf::groupby_aggregation>(cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
TYPED_TEST(groupby_count_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals{0};
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg1 = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg1), force_use_sort_impl::YES);
cudf::test::fixed_width_column_wrapper<R> expect_vals2{3};
auto agg2 = cudf::make_count_aggregation<cudf::groupby_aggregation>(cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg2));
}
TYPED_TEST(groupby_count_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// clang-format off
// {1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, cudf::test::iterators::no_nulls());
// {3, 6, 1, 4, 9, 2, 8, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals({2, 3, 2, 0});
// clang-format on
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg1 = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg1), force_use_sort_impl::YES);
cudf::test::fixed_width_column_wrapper<R> expect_vals2{3, 4, 2, 1};
auto agg2 = cudf::make_count_aggregation<cudf::groupby_aggregation>(cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg2));
}
struct groupby_count_string_test : public cudf::test::BaseFixture {};
TEST_F(groupby_count_string_test, basic)
{
using V = cudf::string_view;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{1, 3, 3, 5, 5, 0};
cudf::test::strings_column_wrapper vals{"1", "1", "1", "1", "1", "1"};
// clang-format on
cudf::test::fixed_width_column_wrapper<K> expect_keys{0, 1, 3, 5};
cudf::test::fixed_width_column_wrapper<R> expect_vals{1, 1, 2, 2};
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg1 = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg1), force_use_sort_impl::YES);
}
// clang-format on
template <typename T>
struct GroupByCountFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupByCountFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(GroupByCountFixedPointTest, GroupByCount)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using V = decimalXX;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
auto const scale = scale_type{-1};
auto const keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
auto const expect_keys = cudf::test::fixed_width_column_wrapper<K>{1, 2, 3};
auto const expect_vals = cudf::test::fixed_width_column_wrapper<R>{3, 4, 3};
auto agg = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg1 = cudf::make_count_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg1), force_use_sort_impl::YES);
auto agg2 = cudf::make_count_aggregation<cudf::groupby_aggregation>(cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
struct groupby_dictionary_count_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_count_test, basic)
{
using V = std::string;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_VALID>;
// clang-format off
cudf::test::strings_column_wrapper keys{"1", "3", "3", "5", "5", "0"};
cudf::test::dictionary_column_wrapper<K> vals{1, 1, 1, 1, 1, 1};
cudf::test::strings_column_wrapper expect_keys{"0", "1", "3", "5"};
cudf::test::fixed_width_column_wrapper<R> expect_vals{1, 1, 2, 2};
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_count_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_count_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/structs_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/debug_utilities.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_structs_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_structs_test, cudf::test::FixedWidthTypes);
template <typename T>
using fwcw = cudf::test::fixed_width_column_wrapper<T>;
template <typename T>
using lcw = cudf::test::lists_column_wrapper<T>;
namespace {
static constexpr auto null = -1; // Signifies null value.
// Checking with a single aggregation, and aggregation column.
// This test is orthogonal to the aggregation type; it focuses on testing the grouping
// with STRUCT keys.
// Set this to true to enable printing, for debugging.
auto constexpr print_enabled = false;
void print_agg_results(cudf::column_view const& keys, cudf::column_view const& vals)
{
if constexpr (print_enabled) {
auto requests = std::vector<cudf::groupby::aggregation_request>{};
requests.push_back(cudf::groupby::aggregation_request{});
requests.back().values = vals;
requests.back().aggregations.push_back(cudf::make_sum_aggregation<cudf::groupby_aggregation>());
requests.back().aggregations.push_back(
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0));
auto gby = cudf::groupby::groupby{
cudf::table_view({keys}), cudf::null_policy::INCLUDE, cudf::sorted::NO, {}, {}};
auto result = gby.aggregate(requests);
std::cout << "Results: Keys: " << std::endl;
cudf::test::print(result.first->get_column(0).view());
std::cout << "Results: Values: " << std::endl;
cudf::test::print(result.second.front().results[0]->view());
}
}
} // namespace
TYPED_TEST(groupby_structs_test, basic)
{
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto member_0 = fwcw<M0>{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto member_1 = fwcw<M1>{ 11, 22, 33, 11, 22, 22, 11, 33, 33, 22};
auto member_2 = cudf::test::strings_column_wrapper {"11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = cudf::test::structs_column_wrapper{member_0, member_1, member_2};
auto expected_values = fwcw<R> { 9, 19, 17 };
auto expected_member_0 = fwcw<M0>{ 1, 2, 3 };
auto expected_member_1 = fwcw<M1>{ 11, 22, 33 };
auto expected_member_2 = cudf::test::strings_column_wrapper {"11", "22", "33"};
auto expected_keys = cudf::test::structs_column_wrapper{expected_member_0, expected_member_1, expected_member_2};
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_structs_test, structs_with_nulls_in_members)
{
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
auto member_0 = fwcw<M0>{{ 1, null, 3, 1, 2, 2, 1, 3, 3, 2 }, null_at(1)};
auto member_1 = fwcw<M1>{{ 11, 22, 33, 11, 22, 22, 11, null, 33, 22 }, null_at(7)};
auto member_2 = cudf::test::strings_column_wrapper { "11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = cudf::test::structs_column_wrapper{{member_0, member_1, member_2}};
// clang-format on
print_agg_results(keys, values);
// clang-format off
auto expected_values = fwcw<R> { 9, 18, 10, 7, 1 };
auto expected_member_0 = fwcw<M0>{ { 1, 2, 3, 3, null }, null_at(4)};
auto expected_member_1 = fwcw<M1>{ { 11, 22, 33, null, 22 }, null_at(3)};
auto expected_member_2 = cudf::test::strings_column_wrapper { "11", "22", "33", "33", "22" };
auto expected_keys = cudf::test::structs_column_wrapper{expected_member_0, expected_member_1, expected_member_2};
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_structs_test, structs_with_null_rows)
{
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto member_0 = fwcw<M0>{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto member_1 = fwcw<M1>{ 11, 22, 33, 11, 22, 22, 11, 33, 33, 22};
auto member_2 = cudf::test::strings_column_wrapper {"11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = cudf::test::structs_column_wrapper{{member_0, member_1, member_2}, nulls_at({0, 3})};
auto expected_values = fwcw<R> { 6, 19, 17, 3 };
auto expected_member_0 = fwcw<M0>{ { 1, 2, 3, null }, null_at(3)};
auto expected_member_1 = fwcw<M1>{ { 11, 22, 33, null }, null_at(3)};
auto expected_member_2 = cudf::test::strings_column_wrapper { {"11", "22", "33", "null" }, null_at(3)};
auto expected_keys = cudf::test::structs_column_wrapper{{expected_member_0, expected_member_1, expected_member_2}, null_at(3)};
// clang-format on
print_agg_results(keys, values);
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_structs_test, structs_with_nulls_in_rows_and_members)
{
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
auto member_0 = fwcw<M0>{{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2 }, null_at(1)};
auto member_1 = fwcw<M1>{{ 11, 22, 33, 11, 22, 22, 11, 33, 33, 22 }, null_at(7)};
auto member_2 = cudf::test::strings_column_wrapper { "11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = cudf::test::structs_column_wrapper{{member_0, member_1, member_2}, null_at(4)};
// clang-format on
print_agg_results(keys, values);
// clang-format off
auto expected_values = fwcw<R> { 9, 14, 10, 7, 1, 4 };
auto expected_member_0 = fwcw<M0>{{ 1, 2, 3, 3, null, null }, nulls_at({4,5})};
auto expected_member_1 = fwcw<M1>{{ 11, 22, 33, null, 22, null }, nulls_at({3,5})};
auto expected_member_2 = cudf::test::strings_column_wrapper {{ "11", "22", "33", "33", "22", "null" }, null_at(5)};
auto expected_keys = cudf::test::structs_column_wrapper{{expected_member_0, expected_member_1, expected_member_2}, null_at(5)};
// clang-format on
print_agg_results(keys, values);
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_structs_test, null_members_differ_from_null_structs)
{
// This test specifically confirms that a non-null STRUCT row `{null, null, null}` is grouped
// differently from a null STRUCT row (whose members are incidentally null).
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
auto member_0 = fwcw<M0>{{ 1, null, 3, 1, 2, 2, 1, 3, 3, 2 }, null_at(1)};
auto member_1 = fwcw<M1>{{ 11, null, 33, 11, 22, 22, 11, 33, 33, 22 }, null_at(1)};
auto member_2 = cudf::test::strings_column_wrapper {{ "11", "null", "33", "11", "22", "22", "11", "33", "33", "22"}, null_at(1)};
auto keys = cudf::test::structs_column_wrapper{{member_0, member_1, member_2}, null_at(4)};
// clang-format on
print_agg_results(keys, values);
// Index-3 => Non-null Struct row, with nulls for all members.
// Index-4 => Null Struct row.
// clang-format off
auto expected_values = fwcw<R> { 9, 14, 17, 1, 4 };
auto expected_member_0 = fwcw<M0>{ { 1, 2, 3, null, null }, nulls_at({3,4})};
auto expected_member_1 = fwcw<M1>{ { 11, 22, 33, null, null }, nulls_at({3,4})};
auto expected_member_2 = cudf::test::strings_column_wrapper { {"11", "22", "33", "null", "null" }, nulls_at({3,4})};
auto expected_keys = cudf::test::structs_column_wrapper{{expected_member_0, expected_member_1, expected_member_2}, null_at(4)};
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_structs_test, structs_of_structs)
{
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
auto struct_0_member_0 = fwcw<M0>{{ 1, null, 3, 1, 2, 2, 1, 3, 3, 2 }, null_at(1)};
auto struct_0_member_1 = fwcw<M1>{{ 11, null, 33, 11, 22, 22, 11, 33, 33, 22 }, null_at(1)};
auto struct_0_member_2 = cudf::test::strings_column_wrapper {{ "11", "null", "33", "11", "22", "22", "11", "33", "33", "22"}, null_at(1)};
// clang-format on
auto struct_0 = cudf::test::structs_column_wrapper{
{struct_0_member_0, struct_0_member_1, struct_0_member_2}, null_at(4)};
auto struct_1_member_1 = fwcw<M1>{8, 9, 6, 8, 0, 7, 8, 6, 6, 7};
auto keys = cudf::test::structs_column_wrapper{
{struct_0, struct_1_member_1}}; // Struct of cudf::test::structs_column_wrapper.
print_agg_results(keys, values);
// clang-format off
auto expected_values = fwcw<R> { 9, 14, 17, 1, 4 };
auto expected_member_0 = fwcw<M0>{ { 1, 2, 3, null, null }, nulls_at({3,4})};
auto expected_member_1 = fwcw<M1>{ { 11, 22, 33, null, null }, nulls_at({3,4})};
auto expected_member_2 = cudf::test::strings_column_wrapper { {"11", "22", "33", "null", "null" }, nulls_at({3,4})};
auto expected_structs = cudf::test::structs_column_wrapper{{expected_member_0, expected_member_1, expected_member_2}, null_at(4)};
auto expected_struct_1_member_1 = fwcw<M1>{ 8, 7, 6, 9, 0 };
auto expected_keys = cudf::test::structs_column_wrapper{{expected_structs, expected_struct_1_member_1}};
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_structs_test, empty_input)
{
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> {};
auto member_0 = fwcw<M0>{};
auto member_1 = fwcw<M1>{};
auto member_2 = cudf::test::strings_column_wrapper {};
auto keys = cudf::test::structs_column_wrapper{member_0, member_1, member_2};
auto expected_values = fwcw<R> {};
auto expected_member_0 = fwcw<M0>{};
auto expected_member_1 = fwcw<M1>{};
auto expected_member_2 = cudf::test::strings_column_wrapper {};
auto expected_keys = cudf::test::structs_column_wrapper{expected_member_0, expected_member_1, expected_member_2};
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_structs_test, all_null_input)
{
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto member_0 = fwcw<M0>{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto member_1 = fwcw<M1>{ 11, 22, 33, 11, 22, 22, 11, 33, 33, 22};
auto member_2 = cudf::test::strings_column_wrapper {"11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = cudf::test::structs_column_wrapper{{member_0, member_1, member_2}, all_nulls()};
auto expected_values = fwcw<R> { 45 };
auto expected_member_0 = fwcw<M0>{ null };
auto expected_member_1 = fwcw<M1>{ null };
auto expected_member_2 = cudf::test::strings_column_wrapper {"null"};
auto expected_keys = cudf::test::structs_column_wrapper{{expected_member_0, expected_member_1, expected_member_2}, all_nulls()};
// clang-format on
test_sum_agg(keys, values, expected_keys, expected_values);
}
TYPED_TEST(groupby_structs_test, lists_as_keys)
{
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
using R = cudf::detail::target_type_t<V, cudf::aggregation::SUM>;
// clang-format off
auto values = fwcw<V> { 0, 1, 2, 3, 4 };
auto member_0 = lcw<M0> { {1,1}, {2,2}, {3,3}, {1,1}, {2,2} };
auto member_1 = fwcw<M1>{ 1, 2, 3, 1, 2 };
// clang-format on
auto keys = cudf::test::structs_column_wrapper{{member_0, member_1}};
// clang-format off
auto expected_values = fwcw<R> { 3, 5, 2 };
auto expected_member_0 = lcw<M0> { {1,1}, {2,2}, {3,3} };
auto expected_member_1 = fwcw<M1>{ 1, 2, 3 };
// clang-format on
auto expected_keys = cudf::test::structs_column_wrapper{{expected_member_0, expected_member_1}};
test_sum_agg(keys, values, expected_keys, expected_values);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/median_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_median_test : public cudf::test::BaseFixture {};
using supported_types = cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>;
TYPED_TEST_SUITE(groupby_median_test, supported_types);
TYPED_TEST(groupby_median_test, basic)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEDIAN>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// clang-format off
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3., 4.5, 7.}, no_nulls());
// clang-format on
auto agg = cudf::make_median_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_median_test, empty_cols)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEDIAN>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_median_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_median_test, zero_valid_keys)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEDIAN>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_median_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_median_test, zero_valid_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEDIAN>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_median_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_median_test, null_keys_and_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEDIAN>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 3, 6, 1, 4, 9, 2, 8, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals({4.5, 4., 5., 0.}, {1, 1, 1, 0});
auto agg = cudf::make_median_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_median_test, dictionary)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MEDIAN>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3 });
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3., 4.5, 7. }, no_nulls());
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_median_aggregation<cudf::groupby_aggregation>());
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/argmax_tests.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
template <typename V>
struct groupby_argmax_test : public cudf::test::BaseFixture {};
using K = int32_t;
TYPED_TEST_SUITE(groupby_argmax_test, cudf::test::FixedWidthTypes);
TYPED_TEST(groupby_argmax_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMAX>;
if (std::is_same_v<V, bool>) return;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals{0, 1, 2};
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_argmax_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMAX>;
if (std::is_same_v<V, bool>) return;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5});
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_argmax_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMAX>;
if (std::is_same_v<V, bool>) return;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, cudf::test::iterators::all_nulls());
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_argmax_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMAX>;
if (std::is_same_v<V, bool>) return;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 4},
{0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0});
// {1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4},
cudf::test::iterators::no_nulls());
// {6, 3, 5, 4, 0, 2, 1, -}
cudf::test::fixed_width_column_wrapper<R> expect_vals({3, 4, 7, 0}, {1, 1, 1, 0});
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
struct groupby_argmax_string_test : public cudf::test::BaseFixture {};
TEST_F(groupby_argmax_string_test, basic)
{
using R = cudf::detail::target_type_t<cudf::string_view, cudf::aggregation::ARGMAX>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::strings_column_wrapper vals{
"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0, 4, 2});
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
TEST_F(groupby_argmax_string_test, zero_valid_values)
{
using R = cudf::detail::target_type_t<cudf::string_view, cudf::aggregation::ARGMAX>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::strings_column_wrapper vals({"año", "bit", "₹1"}, cudf::test::iterators::all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, cudf::test::iterators::all_nulls());
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
auto agg2 = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg2), force_use_sort_impl::YES);
}
struct groupby_dictionary_argmax_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_argmax_test, basic)
{
using V = std::string;
using R = cudf::detail::target_type_t<V, cudf::aggregation::ARGMAX>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{ 1, 2, 3, 1, 2, 2, 1, 3, 3, 2 };
cudf::test::dictionary_column_wrapper<V> vals{ "año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
cudf::test::fixed_width_column_wrapper<K> expect_keys({ 1, 2, 3 });
cudf::test::fixed_width_column_wrapper<R> expect_vals({ 0, 4, 2 });
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_argmax_aggregation<cudf::groupby_aggregation>());
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_argmax_aggregation<cudf::groupby_aggregation>(),
force_use_sort_impl::YES);
}
struct groupby_argmax_struct_test : public cudf::test::BaseFixture {};
TEST_F(groupby_argmax_struct_test, basic)
{
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3};
auto const expect_indices = cudf::test::fixed_width_column_wrapper<int32_t>{0, 4, 2};
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_indices, std::move(agg));
}
TEST_F(groupby_argmax_struct_test, slice_input)
{
constexpr int32_t dont_care{1};
auto const keys_original = cudf::test::fixed_width_column_wrapper<int32_t>{
dont_care, dont_care, 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, dont_care};
auto const vals_original = [] {
auto child1 = cudf::test::strings_column_wrapper{"dont_care",
"dont_care",
"año",
"bit",
"₹1",
"aaa",
"zit",
"bat",
"aab",
"$1",
"€1",
"wut",
"dont_care"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{
dont_care, dont_care, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, dont_care};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const keys = cudf::slice(keys_original, {2, 12})[0];
auto const vals = cudf::slice(vals_original, {2, 12})[0];
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3};
auto const expect_indices = cudf::test::fixed_width_column_wrapper<int32_t>{0, 4, 2};
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_indices, std::move(agg));
}
TEST_F(groupby_argmax_struct_test, null_keys_and_values)
{
constexpr int32_t null{0};
auto const keys = cudf::test::fixed_width_column_wrapper<int32_t>{
{1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4}, cudf::test::iterators::null_at(7)};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "" /*NULL*/, "" /*NULL*/, "$1", "€1", "wut", "" /*NULL*/};
auto child2 =
cudf::test::fixed_width_column_wrapper<int32_t>{9, 8, 7, 6, 5, null, null, 2, 1, 0, null};
return cudf::test::structs_column_wrapper{{child1, child2},
cudf::test::iterators::nulls_at({5, 6, 10})};
}();
auto const expect_keys = cudf::test::fixed_width_column_wrapper<int32_t>{
{1, 2, 3, 4}, cudf::test::iterators::no_nulls()};
auto const expect_indices = cudf::test::fixed_width_column_wrapper<int32_t>{
{0, 4, 2, null}, cudf::test::iterators::null_at(3)};
auto agg = cudf::make_argmax_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_indices, std::move(agg));
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/nth_element_tests.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/dictionary/update_keys.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_nth_element_test : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_nth_element_test, cudf::test::AllTypes);
// clang-format off
TYPED_TEST(groupby_nth_element_test, basic)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
//keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
//vals {0, 3, 6, 1, 4, 5, 9, 2, 7, 8};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
//groupby.first()
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0);
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals0({0, 1, 2});
test_single_agg(keys, vals, expect_keys, expect_vals0, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(1);
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals1({3, 4, 7});
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(2);
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals2({6, 5, 8});
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, empty_cols)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, basic_out_of_bounds)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({0, 1, 2, 3, 4, 5, 3, 2, 2, 9});
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(3);
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals({0, 9, 0}, {0, 1, 0});
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, negative)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
//keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
//vals {0, 3, 6, 1, 4, 5, 9, 2, 7, 8};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
//groupby.last()
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-1);
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals0({6, 9, 8});
test_single_agg(keys, vals, expect_keys, expect_vals0, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-2);
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals1({3, 5, 7});
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-3);
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals2({0, 4, 2});
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, negative_out_of_bounds)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({0, 1, 2, 3, 4, 5, 3, 2, 2, 9});
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-4);
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals({0, 1, 0}, {0, 1, 0});
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, zero_valid_keys)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({3, 4, 5});
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, zero_valid_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals({3}, all_nulls());
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, null_keys_and_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
//keys {1, 1, 1 2,2,2,2 3, 3, 4}
//vals {-,3,6, 1,4,-,9, 2,8, -}
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals({-1, 1, 2, -1}, {0, 1, 1, 0});
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, null_keys_and_values_out_of_bounds)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// {1, 1, 1 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// {-,3,6, 1,4,-,9, 2,8, -}
// value, null, out, out
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals({6, -1, -1, -1}, {1, 0, 0, 0});
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(2);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, exclude_nulls)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 3, 1, 2, 2, 1, 3, 3, 2, 4, 4, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 4, 4, 2},
{0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0});
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
//keys {1, 1, 1 2, 2, 2, 2 3, 3, 3 4}
//vals {-, 3, 6 1, 4, -, 9, - 2, 2, 8, 4,-}
// 0 null, value, value, null
// 1 value, value, value, null
// 2 value, null, value, out
//null_policy::INCLUDE
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_nuls0({-1, 1, 2, 4}, {0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_nuls1({3, 4, 2, -1}, {1, 1, 1, 0});
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_nuls2({6, -1, 8, -1}, {1, 0, 1, 0});
//null_policy::EXCLUDE
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals0({3, 1, 2, 4});
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals1({6, 4, 2, -1}, {1, 1, 1, 0});
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals2({-1, 9, 8, -1}, {0, 1, 1, 0});
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0, cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_nuls0, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(1, cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_nuls1, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(2, cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_nuls2, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0, cudf::null_policy::EXCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals0, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(1, cudf::null_policy::EXCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(2, cudf::null_policy::EXCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg));
}
TYPED_TEST(groupby_nth_element_test, exclude_nulls_negative_index)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::NTH_ELEMENT>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 3, 1, 2, 2, 1, 3, 3, 2, 4, 4, 2},
{1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V, int32_t> vals({0, 1, 2, 2, 3, 4, 5, 6, 7, 8, 9, 4, 4, 2},
{0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0});
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
//keys {1, 1, 1 2, 2, 2, 3, 3, 4}
//vals {-, 3, 6 1, 4, -, 9, - 2, 2, 8, 4,-}
// 0 null, value, value, value
// 1 value, value, value, null
// 2 value, null, value, out
// 3 out, value, out, out
// 4 out, null, out, out
//null_policy::INCLUDE
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_nuls0({6, -1, 8, -1}, {1, 0, 1, 0});
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_nuls1({3, 9, 2, 4});
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_nuls2({-1, -1, 2, -1}, {0, 0, 1, 0});
//null_policy::EXCLUDE
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals0({6, 9, 8, 4});
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals1({3, 4, 2, -1}, {1, 1, 1, 0});
cudf::test::fixed_width_column_wrapper<R, int32_t> expect_vals2({-1, 1, 2, -1}, {0, 1, 1, 0});
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-1, cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_nuls0, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-2, cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_nuls1, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-3, cudf::null_policy::INCLUDE);
test_single_agg(keys, vals, expect_keys, expect_nuls2, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-1, cudf::null_policy::EXCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals0, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-2, cudf::null_policy::EXCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-3, cudf::null_policy::EXCLUDE);
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg));
}
struct groupby_nth_element_string_test : public cudf::test::BaseFixture {
};
TEST_F(groupby_nth_element_string_test, basic_string)
{
using K = int32_t;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::strings_column_wrapper vals{"ABCD", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
//groupby.first()
auto agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0);
cudf::test::strings_column_wrapper expect_vals0{"ABCD", "1", "2"};
test_single_agg(keys, vals, expect_keys, expect_vals0, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(1);
cudf::test::strings_column_wrapper expect_vals1{"3", "4", "7"};
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(2);
cudf::test::strings_column_wrapper expect_vals2{"6", "5", "8"};
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg));
//+ve out of bounds
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(3);
cudf::test::strings_column_wrapper expect_vals3{{"", "9", ""}, {0, 1, 0}};
test_single_agg(keys, vals, expect_keys, expect_vals3, std::move(agg));
//groupby.last()
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-1);
cudf::test::strings_column_wrapper expect_vals4{"6", "9", "8"};
test_single_agg(keys, vals, expect_keys, expect_vals4, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-2);
cudf::test::strings_column_wrapper expect_vals5{"3", "5", "7"};
test_single_agg(keys, vals, expect_keys, expect_vals5, std::move(agg));
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-3);
cudf::test::strings_column_wrapper expect_vals6{"ABCD", "4", "2"};
test_single_agg(keys, vals, expect_keys, expect_vals6, std::move(agg));
//-ve out of bounds
agg = cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(-4);
cudf::test::strings_column_wrapper expect_vals7{{"", "1", ""}, {0, 1, 0}};
test_single_agg(keys, vals, expect_keys, expect_vals7, std::move(agg));
}
// clang-format on
TEST_F(groupby_nth_element_string_test, dictionary)
{
using K = int32_t;
using V = std::string;
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{"AB", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::dictionary_column_wrapper<V> expect_vals_w{"6", "5", "8"};
auto expect_vals = cudf::dictionary::set_keys(expect_vals_w, vals.keys());
test_single_agg(keys,
vals,
expect_keys,
expect_vals->view(),
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(2));
}
template <typename T>
struct groupby_nth_element_lists_test : cudf::test::BaseFixture {};
TYPED_TEST_SUITE(groupby_nth_element_lists_test, cudf::test::FixedWidthTypesWithoutFixedPoint);
TYPED_TEST(groupby_nth_element_lists_test, Basics)
{
using K = int32_t;
using V = TypeParam;
using lists = cudf::test::lists_column_wrapper<V, int32_t>;
auto keys = cudf::test::fixed_width_column_wrapper<K, int32_t>{1, 1, 2, 2, 3, 3};
auto values = lists{{1, 2}, {3, 4}, {5, 6, 7}, lists{}, {9, 10}, {11}};
auto expected_keys = cudf::test::fixed_width_column_wrapper<K, int32_t>{1, 2, 3};
auto expected_values = lists{{1, 2}, {5, 6, 7}, {9, 10}};
test_single_agg(keys,
values,
expected_keys,
expected_values,
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0));
}
TYPED_TEST(groupby_nth_element_lists_test, EmptyInput)
{
using K = int32_t;
using V = TypeParam;
using lists = cudf::test::lists_column_wrapper<V, int32_t>;
auto keys = cudf::test::fixed_width_column_wrapper<K, int32_t>{};
auto values = lists{};
auto expected_keys = cudf::test::fixed_width_column_wrapper<K, int32_t>{};
auto expected_values = lists{};
test_single_agg(keys,
values,
expected_keys,
expected_values,
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(2));
}
struct groupby_nth_element_structs_test : cudf::test::BaseFixture {};
TEST_F(groupby_nth_element_structs_test, Basics)
{
using structs = cudf::test::structs_column_wrapper;
using ints = cudf::test::fixed_width_column_wrapper<int>;
using doubles = cudf::test::fixed_width_column_wrapper<double>;
using strings = cudf::test::strings_column_wrapper;
auto keys = ints{0, 0, 0, 1, 1, 1, 2, 2, 2, 3};
auto child0 = ints{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto child1 = doubles{0.1, 1.2, 2.3, 3.4, 4.51, 5.3e4, 6.3231, -0.07, 832.1, 9.999};
auto child2 = strings{"", "a", "b", "c", "d", "e", "f", "g", "HH", "JJJ"};
auto values = structs{{child0, child1, child2}, {1, 0, 1, 0, 1, 1, 1, 1, 0, 1}};
auto expected_keys = ints{0, 1, 2, 3};
auto expected_ch0 = ints{1, 4, 7, 0};
auto expected_ch1 = doubles{1.2, 4.51, -0.07, 0.0};
auto expected_ch2 = strings{"a", "d", "g", ""};
auto expected_values = structs{{expected_ch0, expected_ch1, expected_ch2}, {0, 1, 1, 0}};
test_single_agg(keys,
values,
expected_keys,
expected_values,
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(1));
expected_keys = ints{0, 1, 2, 3};
expected_ch0 = ints{0, 4, 6, 9};
expected_ch1 = doubles{0.1, 4.51, 6.3231, 9.999};
expected_ch2 = strings{"", "d", "f", "JJJ"};
expected_values = structs{{expected_ch0, expected_ch1, expected_ch2}, {1, 1, 1, 1}};
test_single_agg(
keys,
values,
expected_keys,
expected_values,
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0, cudf::null_policy::EXCLUDE));
}
TEST_F(groupby_nth_element_structs_test, NestedStructs)
{
using structs = cudf::test::structs_column_wrapper;
using ints = cudf::test::fixed_width_column_wrapper<int>;
using doubles = cudf::test::fixed_width_column_wrapper<double>;
using lists = cudf::test::lists_column_wrapper<int>;
auto keys = ints{0, 0, 0, 1, 1, 1, 2, 2, 2, 3};
auto child0 = ints{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
auto child0_of_child1 = ints{0, -1, -2, -3, -4, -5, -6, -7, -8, -9};
auto child1_of_child1 = doubles{0.1, 1.2, 2.3, 3.4, 4.51, 5.3e4, 6.3231, -0.07, 832.1, 9.999};
auto child1 = structs{child0_of_child1, child1_of_child1};
auto child2 = lists{{0}, {1, 2, 3}, {}, {4}, {5, 6}, {}, {}, {7}, {8, 9}, {}};
auto values = structs{{child0, child1, child2}, {1, 0, 1, 0, 1, 1, 1, 1, 0, 1}};
auto expected_keys = ints{0, 1, 2, 3};
auto expected_ch0 = ints{1, 4, 7, 0};
auto expected_ch0_of_ch1 = ints{-1, -4, -7, 0};
auto expected_ch1_of_ch1 = doubles{1.2, 4.51, -0.07, 0.0};
auto expected_ch1 = structs{expected_ch0_of_ch1, expected_ch1_of_ch1};
auto expected_ch2 = lists{{1, 2, 3}, {5, 6}, {7}, {}};
auto expected_values = structs{{expected_ch0, expected_ch1, expected_ch2}, {0, 1, 1, 0}};
test_single_agg(keys,
values,
expected_keys,
expected_values,
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(1));
expected_keys = ints{0, 1, 2, 3};
expected_ch0 = ints{0, 4, 6, 9};
expected_ch0_of_ch1 = ints{0, -4, -6, -9};
expected_ch1_of_ch1 = doubles{0.1, 4.51, 6.3231, 9.999};
expected_ch1 = structs{expected_ch0_of_ch1, expected_ch1_of_ch1};
expected_ch2 = lists{{0}, {5, 6}, {}, {}};
expected_values = structs{{expected_ch0, expected_ch1, expected_ch2}, {1, 1, 1, 1}};
test_single_agg(
keys,
values,
expected_keys,
expected_values,
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0, cudf::null_policy::EXCLUDE));
}
TEST_F(groupby_nth_element_structs_test, EmptyInput)
{
using structs = cudf::test::structs_column_wrapper;
using ints = cudf::test::fixed_width_column_wrapper<int>;
using doubles = cudf::test::fixed_width_column_wrapper<double>;
using strings = cudf::test::strings_column_wrapper;
auto keys = ints{};
auto child0 = ints{};
auto child1 = doubles{};
auto child2 = strings{};
auto values = structs{{child0, child1, child2}};
auto expected_keys = ints{};
auto expected_ch0 = ints{};
auto expected_ch1 = doubles{};
auto expected_ch2 = strings{};
auto expected_values = structs{{expected_ch0, expected_ch1, expected_ch2}};
test_single_agg(keys,
values,
expected_keys,
expected_values,
cudf::make_nth_element_aggregation<cudf::groupby_aggregation>(0));
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/merge_lists_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/concatenate.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/groupby.hpp>
#include <cudf/table/table_view.hpp>
using namespace cudf::test::iterators;
namespace {
constexpr cudf::test::debug_output_level verbosity{cudf::test::debug_output_level::FIRST_ERROR};
constexpr int32_t null{0}; // Mark for null elements
using vcol_views = std::vector<cudf::column_view>;
auto merge_lists(vcol_views const& keys_cols, vcol_views const& values_cols)
{
// Append all the keys and lists together.
auto const keys = cudf::concatenate(keys_cols);
auto const values = cudf::concatenate(values_cols);
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back(cudf::groupby::aggregation_request());
requests[0].values = *values;
requests[0].aggregations.emplace_back(
cudf::make_merge_lists_aggregation<cudf::groupby_aggregation>());
auto gb_obj = cudf::groupby::groupby(cudf::table_view({*keys}));
auto result = gb_obj.aggregate(requests);
return std::pair(std::move(result.first->release()[0]), std::move(result.second[0].results[0]));
}
} // namespace
template <typename V>
struct GroupbyMergeListsTypedTest : public cudf::test::BaseFixture {};
using FixedWidthTypesNotBool = cudf::test::Concat<cudf::test::IntegralTypesNotBool,
cudf::test::FloatingPointTypes,
cudf::test::TimestampTypes>;
TYPED_TEST_SUITE(GroupbyMergeListsTypedTest, FixedWidthTypesNotBool);
TYPED_TEST(GroupbyMergeListsTypedTest, InvalidInput)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys = keys_col{1, 2, 3};
// The input lists column must NOT be nullable.
auto const lists = lists_col{{lists_col{1}, lists_col{} /*NULL*/, lists_col{2}}, null_at(1)};
EXPECT_THROW(merge_lists({keys}, {lists}), cudf::logic_error);
// The input column must be a lists column.
auto const non_lists = keys_col{1, 2, 3, 4, 5};
EXPECT_THROW(merge_lists({keys}, {non_lists}), cudf::logic_error);
}
TYPED_TEST(GroupbyMergeListsTypedTest, EmptyInput)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
// Keys and lists columns are all empty.
auto const keys = keys_col{};
auto const lists0 = lists_col{{1, 2, 3}, {4, 5, 6}};
auto const lists = cudf::empty_like(lists0);
auto const [out_keys, out_lists] = merge_lists(vcol_views{keys}, vcol_views{*lists});
auto const expected_keys = keys_col{};
auto const expected_lists = cudf::empty_like(lists0);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeListsTypedTest, InputWithoutNull)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2};
auto const keys2 = keys_col{1, 3};
auto const keys3 = keys_col{2, 3, 4};
auto const lists1 = lists_col{
{1, 2, 3}, // key = 1
{4, 5, 6} // key = 2
};
auto const lists2 = lists_col{
{10, 11}, // key = 1
{11, 12} // key = 3
};
auto const lists3 = lists_col{
{20, 21, 22}, // key = 2
{23, 24, 25}, // key = 3
{24, 25, 26} // key = 4
};
auto const [out_keys, out_lists] =
merge_lists(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
{1, 2, 3, 10, 11}, // key = 1
{4, 5, 6, 20, 21, 22}, // key = 2
{11, 12, 23, 24, 25}, // key = 3
{24, 25, 26} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeListsTypedTest, InputHasNulls)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2};
auto const keys2 = keys_col{1, 3};
auto const keys3 = keys_col{2, 3, 4};
// Note that the null elements here are not sorted, while the results from current collect_list
// are sorted.
auto const lists1 = lists_col{
lists_col{{1, null, 3}, null_at(1)}, // key = 1
lists_col{4, 5, 6} // key = 2
};
auto const lists2 = lists_col{
lists_col{10, 11}, // key = 1
lists_col{{null, null, null}, all_nulls()} // key = 3
};
auto const lists3 = lists_col{
lists_col{20, 21, 22}, // key = 2
lists_col{{null, 24, null}, nulls_at({0, 2})}, // key = 3
lists_col{{24, 25, 26}, no_nulls()} // key = 4
};
auto const [out_keys, out_lists] =
merge_lists(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
lists_col{{1, null, 3, 10, 11}, null_at(1)}, // key = 1
lists_col{4, 5, 6, 20, 21, 22}, // key = 2
lists_col{{null, null, null, null, 24, null}, nulls_at({0, 1, 2, 3, 5})}, // key = 3
lists_col{24, 25, 26} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeListsTypedTest, InputHasEmptyLists)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2};
auto const keys2 = keys_col{1, 3};
auto const keys3 = keys_col{2, 3, 4};
auto const lists1 = lists_col{
{1, 2, 3}, // key = 1
{} // key = 2
};
auto const lists2 = lists_col{
{}, // key = 1
{11, 12} // key = 3
};
auto const lists3 = lists_col{
{}, // key = 2
{}, // key = 3
{24, 25, 26} // key = 4
};
auto const [out_keys, out_lists] =
merge_lists(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
{1, 2, 3}, // key = 1
{}, // key = 2
{11, 12}, // key = 3
{24, 25, 26} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeListsTypedTest, InputHasNullsAndEmptyLists)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2, 3};
auto const keys2 = keys_col{1, 3, 4};
auto const keys3 = keys_col{2, 3, 4};
// Note that the null elements here are not sorted, while the results from current collect_list
// are sorted.
auto const lists1 = lists_col{
lists_col{{1, null, 3}, null_at(1)}, // key = 1
lists_col{}, // key = 2
lists_col{4, 5} // key = 3
};
auto const lists2 = lists_col{
lists_col{10, 11}, // key = 1
lists_col{{null, null, null}, all_nulls()}, // key = 3
lists_col{} // key = 4
};
auto const lists3 = lists_col{
lists_col{20, 21, 22}, // key = 2
lists_col{{null, 24, null}, nulls_at({0, 2})}, // key = 3
lists_col{{24, 25, 26}, no_nulls()} // key = 4
};
auto const [out_keys, out_lists] =
merge_lists(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
lists_col{{1, null, 3, 10, 11}, null_at(1)}, // key = 1
lists_col{20, 21, 22}, // key = 2
lists_col{{4, 5, null, null, null, null, 24, null}, nulls_at({2, 3, 4, 5, 7})}, // key = 3
lists_col{24, 25, 26} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeListsTypedTest, InputHasListsOfLists)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1 = keys_col{1, 2};
auto const keys2 = keys_col{1, 3};
auto const keys3 = keys_col{2, 3, 4};
auto const lists1 = lists_col{
lists_col{lists_col{1, 2, 3}, lists_col{4}, lists_col{5, 6}}, // key = 1
lists_col{lists_col{}, lists_col{7}} // key = 2
};
auto const lists2 = lists_col{
lists_col{lists_col{}, lists_col{8, 9}}, // key = 1
lists_col{lists_col{11}, lists_col{12, 13}} // key = 3
};
auto const lists3 = lists_col{
lists_col{lists_col{14}, lists_col{15, 16, 17, 18}}, // key = 2
lists_col{lists_col{}}, // key = 3
lists_col{lists_col{17, 18, 19, 20, 21}, lists_col{18, 19, 20}} // key = 4
};
auto const [out_keys, out_lists] =
merge_lists(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
lists_col{
lists_col{1, 2, 3}, lists_col{4}, lists_col{5, 6}, lists_col{}, lists_col{8, 9}}, // key = 1
lists_col{lists_col{}, lists_col{7}, lists_col{14}, lists_col{15, 16, 17, 18}}, // key = 2
lists_col{lists_col{11}, lists_col{12, 13}, lists_col{}}, // key = 3
lists_col{lists_col{17, 18, 19, 20, 21}, lists_col{18, 19, 20}} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
TYPED_TEST(GroupbyMergeListsTypedTest, SlicedColumnsInput)
{
using keys_col = cudf::test::fixed_width_column_wrapper<TypeParam, int32_t>;
using lists_col = cudf::test::lists_column_wrapper<TypeParam, int32_t>;
auto const keys1_original = keys_col{1, 2, 4, 5, 6, 7, 8, 9, 10};
auto const keys2_original = keys_col{0, 0, 1, 1, 1, 3, 4, 5, 6};
auto const keys3_original = keys_col{0, 1, 2, 3, 4, 5, 6, 7, 8};
auto const keys1 = cudf::slice(keys1_original, {0, 2})[0]; // { 1, 2 }
auto const keys2 = cudf::slice(keys2_original, {4, 6})[0]; // { 1, 3 }
auto const keys3 = cudf::slice(keys3_original, {2, 5})[0]; // { 2, 3, 4 }
auto const lists1_original = lists_col{
{10, 11, 12},
{12, 13, 14},
{1, 2, 3}, // key = 1
{4, 5, 6} // key = 2
};
auto const lists2_original = lists_col{{1, 2},
{10, 11}, // key = 1
{11, 12}, // key = 3
{13},
{14},
{15, 16}};
auto const lists3_original = lists_col{{20, 21, 22}, // key = 2
{23, 24, 25}, // key = 3
{24, 25, 26}, // key = 4
{1, 2, 3, 4, 5}};
auto const lists1 = cudf::slice(lists1_original, {2, 4})[0];
auto const lists2 = cudf::slice(lists2_original, {1, 3})[0];
auto const lists3 = cudf::slice(lists3_original, {0, 3})[0];
auto const [out_keys, out_lists] =
merge_lists(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = keys_col{1, 2, 3, 4};
auto const expected_lists = lists_col{
{1, 2, 3, 10, 11}, // key = 1
{4, 5, 6, 20, 21, 22}, // key = 2
{11, 12, 23, 24, 25}, // key = 3
{24, 25, 26} // key = 4
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
struct GroupbyMergeListsTest : public cudf::test::BaseFixture {};
TEST_F(GroupbyMergeListsTest, StringsColumnInput)
{
using strings_col = cudf::test::strings_column_wrapper;
using lists_col = cudf::test::lists_column_wrapper<cudf::string_view>;
auto const keys1 = strings_col{"dog", "unknown"};
auto const keys2 = strings_col{"banana", "unknown", "dog"};
auto const keys3 = strings_col{"apple", "dog", "water melon"};
auto const lists1 = lists_col{
lists_col{"Poodle", "Golden Retriever", "Corgi"}, // key = "dog"
lists_col{{"Whale", "" /*NULL*/, "Polar Bear"}, null_at(1)} // key = "unknown"
};
auto const lists2 = lists_col{
lists_col{"Green", "Yellow"}, // key = "banana"
lists_col{}, // key = "unknown"
lists_col{{"" /*NULL*/, "" /*NULL*/}, all_nulls()} // key = "dog"
};
auto const lists3 = lists_col{
lists_col{"Fuji", "Red Delicious"}, // key = "apple"
lists_col{{"" /*NULL*/, "German Shepherd", "" /*NULL*/}, nulls_at({0, 2})}, // key = "dog"
lists_col{{"Seeedless", "Mini"}, no_nulls()} // key = "water melon"
};
auto const [out_keys, out_lists] =
merge_lists(vcol_views{keys1, keys2, keys3}, vcol_views{lists1, lists2, lists3});
auto const expected_keys = strings_col{"apple", "banana", "dog", "unknown", "water melon"};
auto const expected_lists = lists_col{
lists_col{"Fuji", "Red Delicious"}, // key = "apple"
lists_col{"Green", "Yellow"}, // key = "banana"
lists_col{{
"Poodle",
"Golden Retriever",
"Corgi",
"" /*NULL*/,
"" /*NULL*/,
"" /*NULL*/,
"German Shepherd",
"" /*NULL*/
},
nulls_at({3, 4, 5, 7})}, // key = "dog"
lists_col{{"Whale", "" /*NULL*/, "Polar Bear"}, null_at(1)}, // key = "unknown"
lists_col{{"Seeedless", "Mini"}, no_nulls()} // key = "water melon"
};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_keys, *out_keys, verbosity);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_lists, *out_lists, verbosity);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/rank_scan_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename T>
using input = cudf::test::fixed_width_column_wrapper<T>;
using rank_result_col = cudf::test::fixed_width_column_wrapper<cudf::size_type>;
using percent_result_col = cudf::test::fixed_width_column_wrapper<double>;
using null_iter_t = decltype(nulls_at({}));
auto constexpr X = int32_t{0}; // Placeholder for NULL rows.
auto const all_valid = nulls_at({});
void test_rank_scans(cudf::column_view const& keys,
cudf::column_view const& order,
cudf::column_view const& expected_dense,
cudf::column_view const& expected_rank,
cudf::column_view const& expected_percent_rank)
{
test_single_scan(keys,
order,
keys,
expected_dense,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::DENSE, {}, cudf::null_policy::INCLUDE),
cudf::null_policy::INCLUDE,
cudf::sorted::YES);
test_single_scan(keys,
order,
keys,
expected_rank,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN, {}, cudf::null_policy::INCLUDE),
cudf::null_policy::INCLUDE,
cudf::sorted::YES);
test_single_scan(keys,
order,
keys,
expected_percent_rank,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN,
{},
cudf::null_policy::INCLUDE,
{},
cudf::rank_percentage::ONE_NORMALIZED),
cudf::null_policy::INCLUDE,
cudf::sorted::YES);
}
struct groupby_rank_scan_test : public cudf::test::BaseFixture {};
struct groupby_rank_scan_test_failures : public cudf::test::BaseFixture {};
template <typename T>
struct typed_groupby_rank_scan_test : public cudf::test::BaseFixture {};
using testing_type_set = cudf::test::Concat<cudf::test::IntegralTypesNotBool,
cudf::test::FloatingPointTypes,
cudf::test::FixedPointTypes,
cudf::test::ChronoTypes>;
TYPED_TEST_SUITE(typed_groupby_rank_scan_test, testing_type_set);
TYPED_TEST(typed_groupby_rank_scan_test, empty_cols)
{
using T = TypeParam;
auto const keys = input<T>{};
auto const order_by = input<T>{};
auto const order_by_struct = cudf::test::structs_column_wrapper{};
auto const expected_dense = rank_result_col{};
auto const expected_rank = rank_result_col{};
auto const expected_percent = percent_result_col{};
test_rank_scans(keys, order_by, expected_dense, expected_rank, expected_percent);
test_rank_scans(keys, order_by_struct, expected_dense, expected_rank, expected_percent);
}
TYPED_TEST(typed_groupby_rank_scan_test, zero_valid_keys)
{
using T = TypeParam;
auto const keys = input<T>{{X, X, X}, all_nulls()};
auto const order_by = input<T>{{3, 3, 1}};
auto const order_by_struct = [] {
auto member_1 = input<T>{{3, 3, 1}};
auto member_2 = input<T>{{3, 3, 1}};
return cudf::test::structs_column_wrapper{member_1, member_2};
}();
auto const dense_rank_results = rank_result_col{1, 1, 2};
auto const rank_results = rank_result_col{1, 1, 3};
auto const percent_rank_result = percent_result_col{0, 0, 1};
test_rank_scans(keys, order_by, dense_rank_results, rank_results, percent_rank_result);
test_rank_scans(keys, order_by_struct, dense_rank_results, rank_results, percent_rank_result);
}
TYPED_TEST(typed_groupby_rank_scan_test, zero_valid_orders)
{
using T = TypeParam;
using null_iter_t = decltype(all_nulls());
auto const keys = input<T>{{1, 1, 3, 3}};
auto const make_order_by = [&] { return input<T>{{X, X, X, X}, all_nulls()}; };
auto const make_struct_order_by = [&](null_iter_t const& null_iter = no_nulls()) {
auto member1 = make_order_by();
auto member2 = make_order_by();
return cudf::test::structs_column_wrapper{{member1, member2}, null_iter};
};
auto const order_by = make_order_by();
auto const order_by_struct = make_struct_order_by();
auto const order_by_struct_all_nulls = make_struct_order_by(all_nulls());
auto const expected_dense = rank_result_col{1, 1, 1, 1};
auto const expected_rank = rank_result_col{1, 1, 1, 1};
auto const expected_percent = percent_result_col{0, 0, 0, 0};
test_rank_scans(keys, order_by, expected_dense, expected_rank, expected_percent);
test_rank_scans(keys, order_by_struct, expected_dense, expected_rank, expected_percent);
test_rank_scans(keys, order_by_struct_all_nulls, expected_dense, expected_rank, expected_percent);
}
TYPED_TEST(typed_groupby_rank_scan_test, basic)
{
using T = TypeParam;
auto const keys = /* */ input<T>{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1};
auto const make_order_by = [&] { return input<T>{5, 5, 5, 4, 4, 4, 3, 3, 2, 2, 1, 1}; };
auto const order_by = make_order_by();
auto const order_by_struct = [&] {
auto order2 = make_order_by();
auto order3 = make_order_by();
return cudf::test::structs_column_wrapper{order2, order3};
}();
auto const expected_dense = rank_result_col{1, 1, 1, 2, 2, 2, 3, 1, 2, 2, 3, 3};
auto const expected_rank = rank_result_col{1, 1, 1, 4, 4, 4, 7, 1, 2, 2, 4, 4};
auto const expected_percent = percent_result_col{
0.0, 0.0, 0.0, 3.0 / 6, 3.0 / 6, 3.0 / 6, 6.0 / 6, 0.0, 1.0 / 4, 1.0 / 4, 3.0 / 4, 3.0 / 4};
test_rank_scans(keys, order_by, expected_dense, expected_rank, expected_percent);
test_rank_scans(keys, order_by_struct, expected_dense, expected_rank, expected_percent);
}
TYPED_TEST(typed_groupby_rank_scan_test, null_orders)
{
using T = TypeParam;
auto const null_mask = nulls_at({2, 8});
auto const keys = input<T>{{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}};
auto const make_order_by = [&] {
return input<T>{{-1, -2, X, -2, -3, -3, -4, -4, X, -5, -5, -5}, null_mask};
};
auto const make_struct_order_by = [&](null_iter_t const& null_iter = all_valid) {
auto member1 = make_order_by();
auto member2 = make_order_by();
return cudf::test::structs_column_wrapper{{member1, member2}, null_iter};
};
auto const order_by = make_order_by();
auto const order_by_struct = make_struct_order_by();
auto const order_by_struct_with_nulls = make_struct_order_by(null_mask);
auto const expected_dense = rank_result_col{1, 2, 3, 4, 5, 5, 1, 1, 2, 3, 3, 3};
auto const expected_rank = rank_result_col{1, 2, 3, 4, 5, 5, 1, 1, 3, 4, 4, 4};
auto const expected_percent = percent_result_col{
0.0, 1.0 / 5, 2.0 / 5, 3.0 / 5, 4.0 / 5, 4.0 / 5, 0.0, 0.0, 2.0 / 5, 3.0 / 5, 3.0 / 5, 3.0 / 5};
test_rank_scans(keys, order_by, expected_dense, expected_rank, expected_percent);
test_rank_scans(keys, order_by_struct, expected_dense, expected_rank, expected_percent);
test_rank_scans(
keys, order_by_struct_with_nulls, expected_dense, expected_rank, expected_percent);
}
TYPED_TEST(typed_groupby_rank_scan_test, null_orders_and_keys)
{
using T = TypeParam;
auto const null_mask = nulls_at({2, 8});
auto const keys = input<T>{{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1}, nulls_at({9, 10, 11})};
auto const make_order_by = [&] {
return input<T>{{-1, -2, -2, -2, -3, -3, -4, -4, -4, -5, -5, -6}, null_mask};
};
auto const make_struct_order_by = [&](null_iter_t const& null_iter = all_valid) {
auto member1 = make_order_by();
auto member2 = make_order_by();
return cudf::test::structs_column_wrapper{{member1, member2}, null_iter};
};
auto const order_by = make_order_by();
auto const order_by_struct = make_struct_order_by();
auto const order_by_struct_with_nulls = make_struct_order_by(null_mask);
auto const expected_dense = rank_result_col{{1, 2, 3, 4, 5, 5, 1, 1, 2, 1, 1, 2}};
auto const expected_rank = rank_result_col{{1, 2, 3, 4, 5, 5, 1, 1, 3, 1, 1, 3}};
auto const expected_percent = percent_result_col{
{0.0, 1.0 / 5, 2.0 / 5, 3.0 / 5, 4.0 / 5, 4.0 / 5, 0.0, 0.0, 2.0 / 2, 0.0, 0.0, 2.0 / 2}};
test_rank_scans(keys, order_by, expected_dense, expected_rank, expected_percent);
test_rank_scans(keys, order_by_struct, expected_dense, expected_rank, expected_percent);
test_rank_scans(
keys, order_by_struct_with_nulls, expected_dense, expected_rank, expected_percent);
}
TYPED_TEST(typed_groupby_rank_scan_test, mixedStructs)
{
auto const struct_col = [] {
auto nums = input<TypeParam>{{0, 0, 7, 7, 7, X, 4, 4, 4, 9, 9, 9}, null_at(5)};
auto strings = cudf::test::strings_column_wrapper{
{"0a", "0a", "2a", "2a", "3b", "5", "6c", "6c", "XX", "9", "9", "10d"}, null_at(8)};
return cudf::test::structs_column_wrapper{{nums, strings}, null_at(11)}.release();
}();
auto const keys = cudf::test::strings_column_wrapper{
{"0", "0", "0", "0", "0", "0", "1", "1", "1", "X", "X", "X"}, nulls_at({9, 10, 11})};
auto const expected_dense = rank_result_col{1, 1, 2, 2, 3, 4, 1, 1, 2, 1, 1, 2};
auto const expected_rank = rank_result_col{1, 1, 3, 3, 5, 6, 1, 1, 3, 1, 1, 3};
auto const expected_percent = percent_result_col{
0.0, 0.0, 2.0 / 5, 2.0 / 5, 4.0 / 5, 5.0 / 5, 0.0, 0.0, 2.0 / 2, 0.0, 0.0, 2.0 / 2};
std::vector<cudf::groupby::scan_request> requests;
requests.emplace_back(cudf::groupby::scan_request());
requests[0].values = *struct_col;
requests[0].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::DENSE, {}, cudf::null_policy::INCLUDE));
requests[0].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN, {}, cudf::null_policy::INCLUDE));
requests[0].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN,
{},
cudf::null_policy::INCLUDE,
{},
cudf::rank_percentage::ONE_NORMALIZED));
cudf::groupby::groupby gb_obj(
cudf::table_view({keys}), cudf::null_policy::INCLUDE, cudf::sorted::YES);
auto [result_keys, agg_results] = gb_obj.scan(requests);
CUDF_TEST_EXPECT_TABLES_EQUAL(cudf::table_view({keys}), result_keys->view());
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[0], expected_dense);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[1], expected_rank);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[2], expected_percent);
}
TYPED_TEST(typed_groupby_rank_scan_test, nestedStructs)
{
using T = TypeParam;
auto nested_structs = [] {
auto structs_member = [] {
auto nums_member = input<T>{{0, 0, 7, 7, 7, 5, 4, 4, 4, 9, 9, 9}, null_at(5)};
auto strings_member = cudf::test::strings_column_wrapper{
{"0a", "0a", "2a", "2a", "3b", "5", "6c", "6c", "6c", "9", "9", "10d"}, null_at(8)};
return cudf::test::structs_column_wrapper{nums_member, strings_member};
}();
auto nums_member = input<T>{{0, 0, 7, 7, 7, 5, 4, 4, 4, 9, 9, 9}, null_at(5)};
return cudf::test::structs_column_wrapper{structs_member, nums_member}.release();
}();
auto flat_struct = [] {
auto nums_member = input<T>{{0, 0, 7, 7, 7, 5, 4, 4, 4, 9, 9, 9}, null_at(5)};
auto strings_member = cudf::test::strings_column_wrapper{
{"0a", "0a", "2a", "2a", "3b", "5", "6c", "6c", "6c", "9", "9", "10d"}, null_at(8)};
auto nuther_nums =
cudf::test::fixed_width_column_wrapper<T>{{0, 0, 7, 7, 7, 5, 4, 4, 4, 9, 9, 9}, null_at(5)};
return cudf::test::structs_column_wrapper{nums_member, strings_member, nuther_nums}.release();
}();
auto const keys = cudf::test::strings_column_wrapper{
{"0", "0", "0", "0", "0", "0", "1", "1", "1", "1", "0", "1"}, nulls_at({9, 10, 11})};
std::vector<cudf::groupby::scan_request> requests;
requests.emplace_back(cudf::groupby::scan_request());
requests.emplace_back(cudf::groupby::scan_request());
requests[0].values = *nested_structs;
requests[0].aggregations.push_back(
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(cudf::rank_method::DENSE));
requests[0].aggregations.push_back(
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(cudf::rank_method::MIN));
requests[0].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN,
{},
cudf::null_policy::INCLUDE,
{},
cudf::rank_percentage::ONE_NORMALIZED));
requests[1].values = *flat_struct;
requests[1].aggregations.push_back(
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(cudf::rank_method::DENSE));
requests[1].aggregations.push_back(
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(cudf::rank_method::MIN));
requests[1].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN,
{},
cudf::null_policy::INCLUDE,
{},
cudf::rank_percentage::ONE_NORMALIZED));
cudf::groupby::groupby gb_obj(
cudf::table_view({keys}), cudf::null_policy::INCLUDE, cudf::sorted::YES);
auto [result_keys, agg_results] = gb_obj.scan(requests);
CUDF_TEST_EXPECT_TABLES_EQUAL(cudf::table_view({keys}), result_keys->view());
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[0], *agg_results[1].results[0]);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[1], *agg_results[1].results[1]);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[2], *agg_results[1].results[2]);
}
TYPED_TEST(typed_groupby_rank_scan_test, structsWithNullPushdown)
{
using T = TypeParam;
auto constexpr num_rows = 12;
auto get_struct_column = [] {
auto nums_member =
cudf::test::fixed_width_column_wrapper<T>{{0, 0, 7, 7, 7, 5, 4, 4, 4, 9, 9, 9}, null_at(5)};
auto strings_member = cudf::test::strings_column_wrapper{
{"0a", "0a", "2a", "2a", "3b", "5", "6c", "6c", "6c", "9", "9", "10d"}, null_at(8)};
auto struct_column = cudf::test::structs_column_wrapper{nums_member, strings_member}.release();
// Reset null-mask, a posteriori. Nulls will not be pushed down to children.
auto const null_iter = nulls_at({1, 2, 11});
auto [null_mask, null_count] =
cudf::test::detail::make_null_mask(null_iter, null_iter + num_rows);
struct_column->set_null_mask(std::move(null_mask), null_count);
return struct_column;
};
auto const possibly_null_structs = get_struct_column();
auto const definitely_null_structs = [&] {
auto struct_column = get_struct_column();
struct_column->set_null_mask(cudf::create_null_mask(num_rows, cudf::mask_state::ALL_NULL),
num_rows);
return struct_column;
}();
cudf::test::strings_column_wrapper keys = {
{"0", "0", "0", "0", "0", "0", "1", "1", "1", "X", "X", "X"}, nulls_at({9, 10, 11})};
std::vector<cudf::groupby::scan_request> requests;
requests.emplace_back(cudf::groupby::scan_request());
requests.emplace_back(cudf::groupby::scan_request());
requests[0].values = *possibly_null_structs;
requests[0].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::DENSE, {}, cudf::null_policy::INCLUDE));
requests[0].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN, {}, cudf::null_policy::INCLUDE));
requests[0].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN,
{},
cudf::null_policy::INCLUDE,
{},
cudf::rank_percentage::ONE_NORMALIZED));
requests[1].values = *definitely_null_structs;
requests[1].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::DENSE, {}, cudf::null_policy::INCLUDE));
requests[1].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN, {}, cudf::null_policy::INCLUDE));
requests[1].aggregations.push_back(cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN,
{},
cudf::null_policy::INCLUDE,
{},
cudf::rank_percentage::ONE_NORMALIZED));
cudf::groupby::groupby gb_obj(
cudf::table_view({keys}), cudf::null_policy::INCLUDE, cudf::sorted::YES);
auto [result_keys, agg_results] = gb_obj.scan(requests);
auto expected_dense = rank_result_col{1, 2, 2, 3, 4, 5, 1, 1, 2, 1, 1, 2};
auto expected_rank = rank_result_col{1, 2, 2, 4, 5, 6, 1, 1, 3, 1, 1, 3};
auto expected_percent = percent_result_col{
0.0, 1.0 / 5, 1.0 / 5, 3.0 / 5, 4.0 / 5, 5.0 / 5, 0.0, 0.0, 2.0 / 2, 0.0, 0.0, 2.0 / 2};
auto expected_rank_for_null = rank_result_col{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
auto expected_percent_for_null =
percent_result_col{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[0], expected_dense);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[1], expected_rank);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[0].results[2], expected_percent);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[1].results[0], expected_rank_for_null);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[1].results[1], expected_rank_for_null);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(*agg_results[1].results[2], expected_percent_for_null);
}
/* List support dependent on https://github.com/rapidsai/cudf/issues/8683
template <typename T>
struct list_groupby_rank_scan_test : public cudf::test::BaseFixture {
};
using list_test_type_set = Concat<IntegralTypesNotBool,
FloatingPointTypes,
FixedPointTypes>;
TYPED_TEST_SUITE(list_groupby_rank_scan_test, list_test_type_set);
TYPED_TEST(list_groupby_rank_scan_test, lists)
{
using T = TypeParam;
auto list_col = lists_column_wrapper<T>{
{0, 0},
{0, 0},
{7, 2},
{7, 2},
{7, 3},
{5, 5},
{4, 6},
{4, 6},
{4, 6},
{9, 9},
{9, 9},
{9, 10}}.release();
cudf::test::fixed_width_column_wrapper<T> element1{0, 0, 7, 7, 7, 5, 4, 4, 4, 9, 9, 9};
cudf::test::fixed_width_column_wrapper<T> element2{0, 0, 2, 2, 3, 5, 6, 6, 6, 9, 9, 10};
auto struct_col = cudf::test::structs_column_wrapper{element1, element2}.release();
cudf::test::fixed_width_column_wrapper<T> keys = {{0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1},
{1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0}};
std::vector<cudf::groupby::scan_request> requests;
requests.emplace_back(groupby::aggregation_request());
requests.emplace_back(groupby::aggregation_request());
requests[0].values = list_col;
requests[0].aggregations.push_back(make_rank_aggregation<cudf::groupby_scan_aggregation>(rank_method::DENSE));
requests[0].aggregations.push_back(make_rank_aggregation<cudf::groupby_scan_aggregation>(rank_method::MIN));
requests[1].values = struct_col;
requests[1].aggregations.push_back(make_rank_aggregation<cudf::groupby_scan_aggregation>(rank_method::DENSE));
requests[1].aggregations.push_back(make_rank_aggregation<cudf::groupby_scan_aggregation>(rank_method::MIN));
cudf::groupby::groupby gb_obj(table_view({keys}), null_policy::INCLUDE, cudf::sorted::YES);
auto result = gb_obj.scan(requests);
CUDF_TEST_EXPECT_TABLES_EQUAL(table_view({keys}), result.first->view());
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(
*result.second[0].results[0], *result.second[1].results[0]);
CUDF_TEST_EXPECT_COLUMNS_EQUIVALENT(
*result.second[0].results[2], *result.second[1].results[2]);
}
*/
TEST(groupby_rank_scan_test, bools)
{
using bools = cudf::test::fixed_width_column_wrapper<bool>;
using null_iter_t = decltype(nulls_at({}));
auto const keys = bools{{0, 0, 0, 0, 0, 0, 1, 1, 1, X, X, X}, nulls_at({9, 10, 11})};
auto const nulls_6_8 = nulls_at({6, 8});
auto const make_order_by = [&] { return bools{{0, 0, 1, 1, 1, 1, X, 1, X, 0, 0, 1}, nulls_6_8}; };
auto const make_structs = [&](null_iter_t const& null_iter = all_valid) {
auto member_1 = make_order_by();
auto member_2 = make_order_by();
return cudf::test::structs_column_wrapper{{member_1, member_2}, null_iter};
};
auto const order_by = make_order_by();
auto const order_by_structs = make_structs();
auto const order_by_structs_with_nulls = make_structs(nulls_6_8);
auto const expected_dense = rank_result_col{{1, 1, 2, 2, 2, 2, 1, 2, 3, 1, 1, 2}};
auto const expected_rank = rank_result_col{{1, 1, 3, 3, 3, 3, 1, 2, 3, 1, 1, 3}};
auto const expected_percent = percent_result_col{
{0.0, 0.0, 2.0 / 5, 2.0 / 5, 2.0 / 5, 2.0 / 5, 0.0, 1.0 / 2, 2.0 / 2, 0.0, 0.0, 2.0 / 2}};
test_rank_scans(keys, order_by, expected_dense, expected_rank, expected_percent);
test_rank_scans(keys, order_by_structs, expected_dense, expected_rank, expected_percent);
test_rank_scans(
keys, order_by_structs_with_nulls, expected_dense, expected_rank, expected_percent);
}
TEST(groupby_rank_scan_test, strings)
{
using strings = cudf::test::strings_column_wrapper;
using null_iter_t = decltype(nulls_at({}));
auto const keys =
strings{{"0", "0", "0", "0", "0", "0", "1", "1", "1", "X", "X", "X"}, nulls_at({9, 10, 11})};
auto const nulls_2_8 = nulls_at({2, 8});
auto const make_order_by = [&] {
return strings{{"-1", "-2", "X", "-2", "-3", "-3", "-4", "-4", "X", "-5", "-5", "-6"},
nulls_2_8};
};
auto const make_structs = [&](null_iter_t const& null_iter = all_valid) {
auto member_1 = make_order_by();
auto member_2 = make_order_by();
return cudf::test::structs_column_wrapper{{member_1, member_2}, null_iter};
};
auto const order_by = make_order_by();
auto const order_by_structs = make_structs();
auto const order_by_structs_with_nulls = make_structs(nulls_at({4, 5, 11}));
auto const expected_dense = rank_result_col{{1, 2, 3, 4, 5, 5, 1, 1, 2, 1, 1, 2}};
auto const expected_rank = rank_result_col{{1, 2, 3, 4, 5, 5, 1, 1, 3, 1, 1, 3}};
auto const expected_percent = percent_result_col{
{0.0, 1.0 / 5, 2.0 / 5, 3.0 / 5, 4.0 / 5, 4.0 / 5, 0.0, 0.0, 2.0 / 2, 0.0, 0.0, 2.0 / 2}};
test_rank_scans(keys, order_by, expected_dense, expected_rank, expected_percent);
test_rank_scans(keys, order_by_structs, expected_dense, expected_rank, expected_percent);
test_rank_scans(
keys, order_by_structs_with_nulls, expected_dense, expected_rank, expected_percent);
}
TEST_F(groupby_rank_scan_test_failures, DISABLED_test_exception_triggers)
{
using T = uint32_t;
auto const keys = input<T>{{1, 2, 3}, null_at(2)};
auto const col = input<T>{3, 3, 1};
// All of these aggregations raise exceptions unless provided presorted keys
EXPECT_THROW(test_single_scan(keys,
col,
keys,
col,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::DENSE),
cudf::null_policy::INCLUDE,
cudf::sorted::NO),
cudf::logic_error);
EXPECT_THROW(test_single_scan(keys,
col,
keys,
col,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN),
cudf::null_policy::INCLUDE,
cudf::sorted::NO),
cudf::logic_error);
EXPECT_THROW(test_single_scan(keys,
col,
keys,
col,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::DENSE),
cudf::null_policy::EXCLUDE,
cudf::sorted::YES),
cudf::logic_error);
EXPECT_THROW(test_single_scan(keys,
col,
keys,
col,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN),
cudf::null_policy::EXCLUDE,
cudf::sorted::YES),
cudf::logic_error);
EXPECT_THROW(test_single_scan(keys,
col,
keys,
col,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::DENSE),
cudf::null_policy::EXCLUDE,
cudf::sorted::NO),
cudf::logic_error);
EXPECT_THROW(test_single_scan(keys,
col,
keys,
col,
cudf::make_rank_aggregation<cudf::groupby_scan_aggregation>(
cudf::rank_method::MIN),
cudf::null_policy::EXCLUDE,
cudf::sorted::NO),
cudf::logic_error);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/min_scan_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using key_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
template <typename T>
struct groupby_min_scan_test : public cudf::test::BaseFixture {
using V = T;
using R = cudf::detail::target_type_t<V, cudf::aggregation::MIN>;
using value_wrapper = cudf::test::fixed_width_column_wrapper<V, int32_t>;
using result_wrapper = cudf::test::fixed_width_column_wrapper<R, int32_t>;
};
TYPED_TEST_SUITE(groupby_min_scan_test, cudf::test::FixedWidthTypesWithoutFixedPoint);
TYPED_TEST(groupby_min_scan_test, basic)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys {1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
value_wrapper vals({5, 6, 7, 8, 9, 0, 1, 2, 3, 4});
key_wrapper expect_keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
result_wrapper expect_vals({5, 5, 1, 6, 6, 0, 0, 7, 2, 2});
// clang-format on
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_min_scan_test, pre_sorted)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
value_wrapper vals({5, 8, 1, 6, 9, 0, 4, 7, 2, 3});
key_wrapper expect_keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
result_wrapper expect_vals({5, 5, 1, 6, 6, 0, 0, 7, 2, 2});
// clang-format on
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys,
vals,
expect_keys,
expect_vals,
std::move(agg),
cudf::null_policy::EXCLUDE,
cudf::sorted::YES);
}
TYPED_TEST(groupby_min_scan_test, empty_cols)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys{};
value_wrapper vals{};
key_wrapper expect_keys{};
result_wrapper expect_vals{};
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_min_scan_test, zero_valid_keys)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys({1, 2, 3}, cudf::test::iterators::all_nulls());
value_wrapper vals({3, 4, 5});
key_wrapper expect_keys{};
result_wrapper expect_vals{};
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_min_scan_test, zero_valid_values)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys{1, 1, 1};
value_wrapper vals({3, 4, 5}, cudf::test::iterators::all_nulls());
key_wrapper expect_keys{1, 1, 1};
result_wrapper expect_vals({-1, -1, -1}, cudf::test::iterators::all_nulls());
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_min_scan_test, null_keys_and_values)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys( {1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4}, {1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
value_wrapper vals({5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 4}, {0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// { 1, 1, 1, 2, 2, 2, 2, 3, _, 3, 4}
key_wrapper expect_keys( { 1, 1, 1, 2, 2, 2, 2, 3, 3, 4}, cudf::test::iterators::no_nulls());
// { _, 8, 1, 6, 9, _, 4, 7, 2, 3, _}
result_wrapper expect_vals({-1, 8, 1, 6, 6, -1, 4, 7, 3, -1},
{ 0, 1, 1, 1, 1, 0, 1, 1, 1, 0});
// clang-format on
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
struct groupby_min_scan_string_test : public cudf::test::BaseFixture {};
TEST_F(groupby_min_scan_string_test, basic)
{
key_wrapper keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::strings_column_wrapper vals{
"año", "bit", "₹1", "aaa", "zit", "bat", "aaa", "$1", "₹1", "wut"};
key_wrapper expect_keys{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
cudf::test::strings_column_wrapper expect_vals(
{"año", "aaa", "aaa", "bit", "bit", "bat", "bat", "₹1", "$1", "$1"});
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
template <typename T>
struct GroupByMinScanFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupByMinScanFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(GroupByMinScanFixedPointTest, GroupBySortMinScanDecimalAsValue)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
for (auto const i : {2, 1, 0, -1, -2}) {
auto const scale = scale_type{i};
// clang-format off
auto const keys = key_wrapper{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{{5, 6, 7, 8, 9, 0, 1, 2, 3, 4}, scale};
// {5, 8, 1, 6, 9, 0, 4, 7, 2, 3}
auto const expect_keys = key_wrapper{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
auto const expect_vals_min = fp_wrapper{{5, 5, 1, 6, 6, 0, 0, 7, 2, 2}, scale};
// clang-format on
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals_min, std::move(agg));
}
}
struct groupby_min_scan_struct_test : public cudf::test::BaseFixture {};
TEST_F(groupby_min_scan_struct_test, basic)
{
auto const keys = key_wrapper{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "bat", "aab", "$1", "€1", "wut"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const expect_keys = key_wrapper{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "aaa", "aaa", "bit", "bit", "bat", "bat", "₹1", "$1", "$1"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 4, 4, 2, 2, 6, 6, 3, 8, 8};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_min_scan_struct_test, slice_input)
{
constexpr int32_t dont_care{1};
auto const keys_original =
key_wrapper{dont_care, dont_care, 1, 2, 3, 1, 2, 2, 1, 3, 3, 2, dont_care};
auto const vals_original = [] {
auto child1 = cudf::test::strings_column_wrapper{"dont_care",
"dont_care",
"año",
"bit",
"₹1",
"aaa",
"zit",
"bat",
"aab",
"$1",
"€1",
"wut",
"dont_care"};
auto child2 = key_wrapper{dont_care, dont_care, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, dont_care};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto const keys = cudf::slice(keys_original, {2, 12})[0];
auto const vals = cudf::slice(vals_original, {2, 12})[0];
auto const expect_keys = key_wrapper{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "aaa", "aaa", "bit", "bit", "bat", "bat", "₹1", "$1", "$1"};
auto child2 = cudf::test::fixed_width_column_wrapper<int32_t>{1, 4, 4, 2, 2, 6, 6, 3, 8, 8};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TEST_F(groupby_min_scan_struct_test, null_keys_and_values)
{
constexpr int32_t null{0};
auto const keys =
key_wrapper{{1, 2, 3, 1, 2, 2, 1, null, 3, 2, 4}, cudf::test::iterators::null_at(7)};
auto const vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "bit", "₹1", "aaa", "zit", "" /*NULL*/, "" /*NULL*/, "$1", "€1", "wut", "" /*NULL*/};
auto child2 =
cudf::test::fixed_width_column_wrapper<int32_t>{9, 8, 7, 6, 5, null, null, 2, 1, 0, null};
return cudf::test::structs_column_wrapper{{child1, child2},
cudf::test::iterators::nulls_at({5, 6, 10})};
}();
auto const expect_keys =
key_wrapper{{1, 1, 1, 2, 2, 2, 2, 3, 3, 4}, cudf::test::iterators::no_nulls()};
auto const expect_vals = [] {
auto child1 = cudf::test::strings_column_wrapper{
"año", "aaa", "" /*NULL*/, "bit", "bit", "" /*NULL*/, "bit", "₹1", "€1", "" /*NULL*/};
auto child2 =
cudf::test::fixed_width_column_wrapper<int32_t>{9, 6, null, 8, 8, null, 8, 7, 1, null};
return cudf::test::structs_column_wrapper{{child1, child2},
cudf::test::iterators::nulls_at({2, 5, 9})};
}();
auto agg = cudf::make_min_aggregation<cudf::groupby_scan_aggregation>();
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg));
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/count_scan_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using key_wrapper = cudf::test::fixed_width_column_wrapper<int32_t>;
template <typename T>
struct groupby_count_scan_test : public cudf::test::BaseFixture {
using V = T;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_ALL>;
using value_wrapper = cudf::test::fixed_width_column_wrapper<V, int32_t>;
using result_wrapper = cudf::test::fixed_width_column_wrapper<R, int32_t>;
};
TYPED_TEST_SUITE(groupby_count_scan_test, cudf::test::AllTypes);
TYPED_TEST(groupby_count_scan_test, basic)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys {1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
value_wrapper vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
key_wrapper expect_keys {1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
result_wrapper expect_vals{0, 1, 2, 0, 1, 2, 3, 0, 1, 2};
// clang-format on
// Count groupby aggregation is only supported with cudf::null_policy::EXCLUDE
auto agg1 = cudf::make_count_aggregation<cudf::groupby_scan_aggregation>();
EXPECT_THROW(test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg1)),
cudf::logic_error);
auto agg2 =
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>(cudf::null_policy::INCLUDE);
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
TYPED_TEST(groupby_count_scan_test, empty_cols)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys;
value_wrapper vals;
key_wrapper expect_keys;
result_wrapper expect_vals;
auto agg1 = cudf::make_count_aggregation<cudf::groupby_scan_aggregation>();
EXPECT_NO_THROW(test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg1)));
auto agg2 =
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>(cudf::null_policy::INCLUDE);
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
TYPED_TEST(groupby_count_scan_test, zero_valid_keys)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys({1, 2, 3}, cudf::test::iterators::all_nulls());
value_wrapper vals{3, 4, 5};
key_wrapper expect_keys{};
result_wrapper expect_vals{};
auto agg2 =
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>(cudf::null_policy::INCLUDE);
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
TYPED_TEST(groupby_count_scan_test, zero_valid_values)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
key_wrapper keys{1, 1, 1};
value_wrapper vals({3, 4, 5}, cudf::test::iterators::all_nulls());
key_wrapper expect_keys{1, 1, 1};
result_wrapper expect_vals{0, 1, 2};
auto agg2 =
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>(cudf::null_policy::INCLUDE);
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
TYPED_TEST(groupby_count_scan_test, null_keys_and_values)
{
using value_wrapper = typename TestFixture::value_wrapper;
using result_wrapper = typename TestFixture::result_wrapper;
// clang-format off
key_wrapper keys( {1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4}, {1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
value_wrapper vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 4}, {0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0});
// {1, 1, 1, 2, 2, 2, 2, 3, _, 3, 4}
key_wrapper expect_keys( {1, 1, 1, 2, 2, 2, 2, 3, 3, 4}, cudf::test::iterators::no_nulls());
// {0, 3, 6, 1, 4, _, 9, 2, 7, 8, -}
result_wrapper expect_vals{0, 1, 2, 0, 1, 2, 3, 0, 1, 0};
// clang-format on
auto agg2 =
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>(cudf::null_policy::INCLUDE);
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
struct groupby_count_scan_string_test : public cudf::test::BaseFixture {};
TEST_F(groupby_count_scan_string_test, basic)
{
using V = cudf::string_view;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_ALL>;
using result_wrapper = cudf::test::fixed_width_column_wrapper<R, int32_t>;
// clang-format off
key_wrapper keys { 1, 3, 3, 5, 5, 0};
cudf::test::strings_column_wrapper vals{"1", "1", "1", "1", "1", "1"};
key_wrapper expect_keys {0, 1, 3, 3, 5, 5};
result_wrapper expect_vals{0, 0, 0, 1, 0, 1};
// clang-format on
auto agg2 =
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>(cudf::null_policy::INCLUDE);
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
template <typename T>
struct GroupByCountScanFixedPointTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(GroupByCountScanFixedPointTest, cudf::test::FixedPointTypes);
TYPED_TEST(GroupByCountScanFixedPointTest, GroupByCountScan)
{
using namespace numeric;
using decimalXX = TypeParam;
using RepType = cudf::device_storage_type_t<decimalXX>;
using fp_wrapper = cudf::test::fixed_point_column_wrapper<RepType>;
using V = decimalXX;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_ALL>;
using result_wrapper = cudf::test::fixed_width_column_wrapper<R, int32_t>;
auto const scale = scale_type{-1};
auto const keys = key_wrapper{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
auto const vals = fp_wrapper{{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, scale};
auto const expect_keys = key_wrapper{1, 1, 1, 2, 2, 2, 2, 3, 3, 3};
auto const expect_vals = result_wrapper{0, 1, 2, 0, 1, 2, 3, 0, 1, 2};
// Count groupby aggregation is only supported with cudf::null_policy::EXCLUDE
EXPECT_THROW(test_single_scan(keys,
vals,
expect_keys,
expect_vals,
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>()),
cudf::logic_error);
auto agg2 =
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>(cudf::null_policy::INCLUDE);
test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg2));
}
struct groupby_dictionary_count_scan_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_count_scan_test, basic)
{
using K = int32_t;
using V = std::string;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COUNT_ALL>;
using result_wrapper = cudf::test::fixed_width_column_wrapper<R, int32_t>;
cudf::test::strings_column_wrapper keys{"1", "3", "3", "5", "5", "0"};
cudf::test::dictionary_column_wrapper<K> vals{1, 1, 1, 1, 1, 1};
cudf::test::strings_column_wrapper expect_keys{"0", "1", "3", "3", "5", "5"};
result_wrapper expect_vals{0, 0, 0, 1, 0, 1};
// Count groupby aggregation is only supported with cudf::null_policy::EXCLUDE
auto agg1 = cudf::make_count_aggregation<cudf::groupby_scan_aggregation>();
EXPECT_THROW(test_single_scan(keys, vals, expect_keys, expect_vals, std::move(agg1)),
cudf::logic_error);
test_single_scan(
keys,
vals,
expect_keys,
expect_vals,
cudf::make_count_aggregation<cudf::groupby_scan_aggregation>(cudf::null_policy::INCLUDE));
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/var_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_var_test : public cudf::test::BaseFixture {};
using supported_types = cudf::test::Types<int8_t, int16_t, int32_t, int64_t, float, double>;
TYPED_TEST_SUITE(groupby_var_test, supported_types);
TYPED_TEST(groupby_var_test, basic)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::VARIANCE>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::fixed_width_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({9., 131. / 12, 31. / 3}, no_nulls());
// clang-format on
auto agg = cudf::make_variance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_var_test, empty_cols)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::VARIANCE>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> vals{};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_variance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_var_test, zero_valid_keys)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::VARIANCE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> vals{3, 4, 5};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_variance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_var_test, zero_valid_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::VARIANCE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> vals({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_variance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_var_test, null_keys_and_values)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::VARIANCE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
// clang-format off
// {1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// {3, 6, 1, 4, 9, 2, 8, 3}
cudf::test::fixed_width_column_wrapper<R> expect_vals({4.5, 49. / 3, 18., 0.}, {1, 1, 1, 0});
// clang-format on
auto agg = cudf::make_variance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_var_test, ddof_non_default)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::VARIANCE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> vals({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 3},
{0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1});
// clang-format off
// { 1, 1, 2, 2, 2, 3, 3, 4}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
// { 3, 6, 1, 4, 9, 2, 8, 3}
cudf::test::fixed_width_column_wrapper<R> expect_vals({0., 98. / 3, 0., 0.},
{0, 1, 0, 0});
// clang-format on
auto agg = cudf::make_variance_aggregation<cudf::groupby_aggregation>(2);
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg));
}
TYPED_TEST(groupby_var_test, dictionary)
{
using K = int32_t;
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::VARIANCE>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys{1, 2, 3, 1, 2, 2, 1, 3, 3, 2};
cudf::test::dictionary_column_wrapper<V> vals{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
// {1, 1, 1, 2, 2, 2, 2, 3, 3, 3}
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3 });
// {0, 3, 6, 1, 4, 5, 9, 2, 7, 8}
cudf::test::fixed_width_column_wrapper<R> expect_vals({9., 131./12, 31./3 }, no_nulls());
// clang-format on
test_single_agg(keys,
vals,
expect_keys,
expect_vals,
cudf::make_variance_aggregation<cudf::groupby_aggregation>());
}
// This test ensures that the same results are produced by the sort-based and
// hash-based implementations of groupby-variance.
TYPED_TEST(groupby_var_test, sort_vs_hash)
{
using K = int32_t;
using V = double;
cudf::test::fixed_width_column_wrapper<K> keys{50, 30, 90, 80};
cudf::test::fixed_width_column_wrapper<V> vals{380.0, 370.0, 24.0, 26.0};
cudf::groupby::groupby gb_obj(cudf::table_view({keys}));
auto agg1 = cudf::make_variance_aggregation<cudf::groupby_aggregation>();
std::vector<cudf::groupby::aggregation_request> requests;
requests.emplace_back();
requests[0].values = vals;
requests[0].aggregations.push_back(std::move(agg1));
auto result1 = gb_obj.aggregate(requests);
// This agg forces a sort groupby.
auto agg2 = cudf::make_quantile_aggregation<cudf::groupby_aggregation>({0.25});
requests[0].aggregations.push_back(std::move(agg2));
auto result2 = gb_obj.aggregate(requests);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(*result1.second[0].results[0], *result2.second[0].results[0]);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/groupby/covariance_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <tests/groupby/groupby_test_util.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <cudf/detail/aggregation/aggregation.hpp>
#include <cudf/utilities/traits.hpp>
#include <limits>
#include <vector>
using namespace cudf::test::iterators;
template <typename V>
struct groupby_covariance_test : public cudf::test::BaseFixture {};
using supported_types =
cudf::test::RemoveIf<cudf::test::ContainedIn<cudf::test::Types<bool>>, cudf::test::NumericTypes>;
TYPED_TEST_SUITE(groupby_covariance_test, supported_types);
using K = int32_t;
TYPED_TEST(groupby_covariance_test, invalid_types)
{
using V = TypeParam;
auto keys = cudf::test::fixed_width_column_wrapper<K>{{1, 2, 2, 1}};
auto member_0 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2}};
// Covariance cudf::aggregations require all types are convertible to double, but
// duration_D cannot be converted to double.
auto member_1 =
cudf::test::fixed_width_column_wrapper<cudf::duration_D, cudf::duration_D::rep>{{0, 0, 1, 1}};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
EXPECT_THROW(test_single_agg(keys, vals, keys, vals, std::move(agg), force_use_sort_impl::YES),
cudf::logic_error);
}
TYPED_TEST(groupby_covariance_test, basic)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
auto keys = cudf::test::fixed_width_column_wrapper<K>{{1, 2, 3, 1, 2, 2, 1, 3, 3, 2}};
auto member_0 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 2, 3, 3, 1, 1, 4}};
auto member_1 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 0, 3, 3, 1, 1, 2}};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R, double> expect_vals{{1.0, 1.0, 0.0}};
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_covariance_test, empty_cols)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
cudf::test::fixed_width_column_wrapper<K> keys{};
cudf::test::fixed_width_column_wrapper<V> member_0{}, member_1{};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_covariance_test, zero_valid_keys)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> member_0{3, 4, 5}, member_1{6, 7, 8};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{};
cudf::test::fixed_width_column_wrapper<R> expect_vals{};
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_covariance_test, zero_valid_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
cudf::test::fixed_width_column_wrapper<K> keys{1, 1, 1};
cudf::test::fixed_width_column_wrapper<V> member_0({3, 4, 5}, all_nulls());
cudf::test::fixed_width_column_wrapper<V> member_1({3, 4, 5}, all_nulls());
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1};
cudf::test::fixed_width_column_wrapper<R> expect_vals({0}, all_nulls());
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_covariance_test, null_keys_and_values)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val0({9, 1, 1, 2, 2, 3, 3,-1, 1, 4, 4},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val1({1, 1, 1, 2, 0, 3, 3,-1, 0, 2, 2});
// clang-format on
auto vals = cudf::test::structs_column_wrapper{{val0, val1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
cudf::test::fixed_width_column_wrapper<R> expect_vals({0.5, 1.0, 0.0, -0.}, {1, 1, 1, 0});
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_covariance_test, null_values_same)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val0({9, 1, 1, 2, 2, 3, 3,-1, 1, 4, 4},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
cudf::test::fixed_width_column_wrapper<V> val1({1, 1, 1, 2, 0, 3, 3,-1, 0, 2, 2},
{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0});
// clang-format on
auto vals = cudf::test::structs_column_wrapper{{val0, val1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
cudf::test::fixed_width_column_wrapper<R> expect_vals({0.5, 1.0, 0.0, -0.}, {1, 1, 1, 0});
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_covariance_test, null_values_different)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
// clang-format off
cudf::test::fixed_width_column_wrapper<K> keys({1, 2, 3, 1, 2, 2, 1, 3, 3, 2, 4},
{1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val0({9, 1, 1, 2, 2, 3, 3,-1, 1, 4, 4},
{0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1});
cudf::test::fixed_width_column_wrapper<V> val1({1, 2, 1, 2,-1, 3, 3,-1, 0, 4, 2},
{0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0});
// clang-format on
auto vals = cudf::test::structs_column_wrapper{{val0, val1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys({1, 2, 3, 4}, no_nulls());
cudf::test::fixed_width_column_wrapper<R> expect_vals(
{std::numeric_limits<double>::quiet_NaN(), 1.5, 0.0, -0.}, {0, 1, 1, 0});
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_covariance_test, min_periods)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
auto keys = cudf::test::fixed_width_column_wrapper<K>{{1, 2, 3, 1, 2, 2, 1, 3, 3, 2}};
auto member_0 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 2, 3, 3, 1, 1, 4}};
auto member_1 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 0, 3, 3, 1, 1, 2}};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R, double> expect_vals1{{1.0, 1.0, 0.0}};
auto agg1 = cudf::make_covariance_aggregation<cudf::groupby_aggregation>(3);
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg1), force_use_sort_impl::YES);
cudf::test::fixed_width_column_wrapper<R, double> expect_vals2{{1.0, 1.0, 0.0}, {0, 1, 0}};
auto agg2 = cudf::make_covariance_aggregation<cudf::groupby_aggregation>(4);
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg2), force_use_sort_impl::YES);
cudf::test::fixed_width_column_wrapper<R, double> expect_vals3{{1.0, 1.0, 0.0}, {0, 0, 0}};
auto agg3 = cudf::make_covariance_aggregation<cudf::groupby_aggregation>(5);
test_single_agg(keys, vals, expect_keys, expect_vals3, std::move(agg3), force_use_sort_impl::YES);
}
TYPED_TEST(groupby_covariance_test, ddof)
{
using V = TypeParam;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
auto keys = cudf::test::fixed_width_column_wrapper<K>{{1, 2, 3, 1, 2, 2, 1, 3, 3, 2}};
auto member_0 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 2, 3, 3, 1, 1, 4}};
auto member_1 = cudf::test::fixed_width_column_wrapper<V>{{1, 1, 1, 2, 0, 3, 3, 1, 1, 2}};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R, double> expect_vals1{{2.0, 1.5, 0.0}};
auto agg1 = cudf::make_covariance_aggregation<cudf::groupby_aggregation>(1, 2);
test_single_agg(keys, vals, expect_keys, expect_vals1, std::move(agg1), force_use_sort_impl::YES);
auto const inf = std::numeric_limits<double>::infinity();
cudf::test::fixed_width_column_wrapper<R, double> expect_vals2{{inf, 3.0, 0.0}, {0, 1, 0}};
auto agg2 = cudf::make_covariance_aggregation<cudf::groupby_aggregation>(1, 3);
test_single_agg(keys, vals, expect_keys, expect_vals2, std::move(agg2), force_use_sort_impl::YES);
}
struct groupby_dictionary_covariance_test : public cudf::test::BaseFixture {};
TEST_F(groupby_dictionary_covariance_test, basic)
{
using V = int16_t;
using R = cudf::detail::target_type_t<V, cudf::aggregation::COVARIANCE>;
auto keys = cudf::test::fixed_width_column_wrapper<K>{{1, 2, 3, 1, 2, 2, 1, 3, 3, 2}};
auto member_0 = cudf::test::dictionary_column_wrapper<V>{{1, 1, 1, 2, 2, 3, 3, 1, 1, 4}};
auto member_1 = cudf::test::dictionary_column_wrapper<V>{{1, 1, 1, 2, 3, -3, 3, 1, 1, 2}};
auto vals = cudf::test::structs_column_wrapper{{member_0, member_1}};
cudf::test::fixed_width_column_wrapper<K> expect_keys{1, 2, 3};
cudf::test::fixed_width_column_wrapper<R, double> expect_vals{{1.0, -0.5, 0.0}};
auto agg = cudf::make_covariance_aggregation<cudf::groupby_aggregation>();
test_single_agg(keys, vals, expect_keys, expect_vals, std::move(agg), force_use_sort_impl::YES);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/join/cross_join_tests.cpp
|
/*
* Copyright (c) 2020-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/join.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
template <typename T, typename SourceT = T>
using column_wrapper = cudf::test::fixed_width_column_wrapper<T, SourceT>;
template <typename T>
class CrossJoinTypeTests : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(CrossJoinTypeTests, cudf::test::FixedWidthTypes);
TYPED_TEST(CrossJoinTypeTests, CrossJoin)
{
auto a_0 = column_wrapper<int32_t>{10, 20, 20, 50};
auto a_1 = column_wrapper<float>{5.0, .5, .5, .7};
auto a_2 = column_wrapper<TypeParam, int32_t>{0, 0, 0, 0};
auto a_3 = cudf::test::strings_column_wrapper({"quick", "accénted", "turtlé", "composéd"});
auto b_0 = column_wrapper<int32_t>{10, 20, 20};
auto b_1 = column_wrapper<float>{5.0, .7, .7};
auto b_2 = column_wrapper<TypeParam, int32_t>{0, 0, 0};
auto b_3 = cudf::test::strings_column_wrapper({"result", "", "words"});
auto expect_0 = column_wrapper<int32_t>{10, 10, 10, 20, 20, 20, 20, 20, 20, 50, 50, 50};
auto expect_1 = column_wrapper<float>{5.0, 5.0, 5.0, .5, .5, .5, .5, .5, .5, .7, .7, .7};
auto expect_2 = column_wrapper<TypeParam, int32_t>({0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0});
auto expect_3 = cudf::test::strings_column_wrapper({"quick",
"quick",
"quick",
"accénted",
"accénted",
"accénted",
"turtlé",
"turtlé",
"turtlé",
"composéd",
"composéd",
"composéd"});
auto expect_4 = column_wrapper<int32_t>{10, 20, 20, 10, 20, 20, 10, 20, 20, 10, 20, 20};
auto expect_5 = column_wrapper<float>{5.0, .7, .7, 5.0, .7, .7, 5.0, .7, .7, 5.0, .7, .7};
auto expect_6 = column_wrapper<TypeParam, int32_t>{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
auto expect_7 = cudf::test::strings_column_wrapper(
{"result", "", "words", "result", "", "words", "result", "", "words", "result", "", "words"});
auto table_a = cudf::table_view{{a_0, a_1, a_2, a_3}};
auto table_b = cudf::table_view{{b_0, b_1, b_2, b_3}};
auto table_expect = cudf::table_view{
{expect_0, expect_1, expect_2, expect_3, expect_4, expect_5, expect_6, expect_7}};
auto join_table = cudf::cross_join(table_a, table_b);
EXPECT_EQ(join_table->num_columns(), table_a.num_columns() + table_b.num_columns());
EXPECT_EQ(join_table->num_rows(), table_a.num_rows() * table_b.num_rows());
CUDF_TEST_EXPECT_TABLES_EQUAL(join_table->view(), table_expect);
}
class CrossJoinInvalidInputs : public cudf::test::BaseFixture {};
TEST_F(CrossJoinInvalidInputs, EmptyTable)
{
auto b_0 = column_wrapper<int32_t>{10, 20, 20, 50};
auto b_1 = column_wrapper<float>{5.0, .7, .7, .7};
auto b_2 = column_wrapper<int8_t>{90, 75, 62, 41};
auto b_3 = cudf::test::strings_column_wrapper({"quick", "words", "result", ""});
auto column_a = std::vector<std::unique_ptr<cudf::column>>{};
auto table_a = cudf::table(std::move(column_a));
auto table_b = cudf::table_view{{b_0, b_1, b_2, b_3}};
//
// table_a has no columns, table_b has columns
// Let's check different permutations of passing table
// with no columns to verify that exceptions are thrown
//
EXPECT_THROW(cudf::cross_join(table_a, table_b), cudf::logic_error);
EXPECT_THROW(cudf::cross_join(table_b, table_a), cudf::logic_error);
}
class CrossJoinEmptyResult : public cudf::test::BaseFixture {};
TEST_F(CrossJoinEmptyResult, NoRows)
{
auto a_0 = column_wrapper<int32_t>{};
auto a_1 = column_wrapper<float>{};
auto a_2 = column_wrapper<int8_t>{};
auto empty_strings = std::vector<std::string>();
auto a_3 = cudf::test::strings_column_wrapper(empty_strings.begin(), empty_strings.end());
auto b_0 = column_wrapper<int32_t>{10, 20, 20, 50};
auto b_1 = column_wrapper<float>{5.0, .7, .7, .7};
auto b_2 = column_wrapper<int8_t>{90, 75, 62, 41};
auto b_3 = cudf::test::strings_column_wrapper({"quick", "words", "result", ""});
auto expect_0 = column_wrapper<int32_t>{};
auto expect_1 = column_wrapper<float>{};
auto expect_2 = column_wrapper<int8_t>{};
auto expect_3 = cudf::test::strings_column_wrapper(empty_strings.begin(), empty_strings.end());
auto expect_4 = column_wrapper<int32_t>{};
auto expect_5 = column_wrapper<float>{};
auto expect_6 = column_wrapper<int8_t>{};
auto expect_7 = cudf::test::strings_column_wrapper(empty_strings.begin(), empty_strings.end());
auto table_a = cudf::table_view{{a_0, a_1, a_2, a_3}};
auto table_b = cudf::table_view{{b_0, b_1, b_2, b_3}};
auto table_expect = cudf::table_view{
{expect_0, expect_1, expect_2, expect_3, expect_4, expect_5, expect_6, expect_7}};
auto join_table = cudf::cross_join(table_a, table_b);
auto join_table_reverse = cudf::cross_join(table_b, table_a);
EXPECT_EQ(join_table->num_columns(), table_a.num_columns() + table_b.num_columns());
EXPECT_EQ(join_table->num_rows(), 0);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(join_table->view(), table_expect);
EXPECT_EQ(join_table_reverse->num_columns(), table_a.num_columns() + table_b.num_columns());
EXPECT_EQ(join_table_reverse->num_rows(), 0);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/join/conditional_join_tests.cu
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/ast/expressions.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/join.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <rmm/exec_policy.hpp>
#include <thrust/equal.h>
#include <thrust/iterator/counting_iterator.h>
#include <thrust/sort.h>
#include <thrust/transform.h>
#include <algorithm>
#include <iostream>
#include <random>
#include <stdexcept>
#include <tuple>
#include <utility>
#include <vector>
namespace {
using PairJoinReturn = std::pair<std::unique_ptr<rmm::device_uvector<cudf::size_type>>,
std::unique_ptr<rmm::device_uvector<cudf::size_type>>>;
using SingleJoinReturn = std::unique_ptr<rmm::device_uvector<cudf::size_type>>;
using NullMaskVector = std::vector<bool>;
template <typename T>
using ColumnVector = std::vector<std::vector<T>>;
template <typename T>
using NullableColumnVector = std::vector<std::pair<std::vector<T>, NullMaskVector>>;
constexpr cudf::size_type JoinNoneValue =
std::numeric_limits<cudf::size_type>::min(); // TODO: how to test if this isn't public?
// Common column references.
auto const col_ref_left_0 = cudf::ast::column_reference(0, cudf::ast::table_reference::LEFT);
auto const col_ref_right_0 = cudf::ast::column_reference(0, cudf::ast::table_reference::RIGHT);
// Common expressions.
auto left_zero_eq_right_zero =
cudf::ast::operation(cudf::ast::ast_operator::EQUAL, col_ref_left_0, col_ref_right_0);
// Generate a single pair of left/right non-nullable columns of random data
// suitable for testing a join against a reference join implementation.
template <typename T>
std::pair<std::vector<T>, std::vector<T>> gen_random_repeated_columns(
unsigned int N_left = 10000,
unsigned int num_repeats_left = 10,
unsigned int N_right = 10000,
unsigned int num_repeats_right = 10)
{
// Generate columns of num_repeats repeats of the integer range [0, num_unique),
// then merge a shuffled version and compare to hash join.
unsigned int num_unique_left = N_left / num_repeats_left;
unsigned int num_unique_right = N_right / num_repeats_right;
std::vector<T> left(N_left);
std::vector<T> right(N_right);
for (unsigned int i = 0; i < num_repeats_left; ++i) {
std::iota(std::next(left.begin(), num_unique_left * i),
std::next(left.begin(), num_unique_left * (i + 1)),
0);
}
for (unsigned int i = 0; i < num_repeats_right; ++i) {
std::iota(std::next(right.begin(), num_unique_right * i),
std::next(right.begin(), num_unique_right * (i + 1)),
0);
}
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(left.begin(), left.end(), gen);
std::shuffle(right.begin(), right.end(), gen);
return std::pair(std::move(left), std::move(right));
}
// Generate a single pair of left/right nullable columns of random data
// suitable for testing a join against a reference join implementation.
template <typename T>
std::pair<std::pair<std::vector<T>, std::vector<bool>>,
std::pair<std::vector<T>, std::vector<bool>>>
gen_random_nullable_repeated_columns(unsigned int N = 10000, unsigned int num_repeats = 10)
{
auto [left, right] = gen_random_repeated_columns<T>(N, num_repeats);
std::vector<bool> left_nulls(N);
std::vector<bool> right_nulls(N);
// Seed with a real random value, if available
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> uniform_dist(0, 1);
std::generate(left_nulls.begin(), left_nulls.end(), [&uniform_dist, &gen]() {
return uniform_dist(gen) > 0.5;
});
std::generate(right_nulls.begin(), right_nulls.end(), [&uniform_dist, &gen]() {
return uniform_dist(gen) > 0.5;
});
return std::pair(std::pair(std::move(left), std::move(left_nulls)),
std::pair(std::move(right), std::move(right_nulls)));
}
// `rmm::device_uvector<T>` requires that T be trivially copyable. `thrust::pair` does
// not satisfy this requirement because it defines nontrivial copy/move
// constructors. Therefore, we need a simple, trivially copyable pair-like
// object. `index_pair` is a minimal implementation suitable for use in the
// tests in this file.
struct index_pair {
cudf::size_type first{};
cudf::size_type second{};
__device__ index_pair(){};
__device__ index_pair(cudf::size_type const& first, cudf::size_type const& second)
: first(first), second(second){};
};
__device__ inline bool operator<(index_pair const& lhs, index_pair const& rhs)
{
if (lhs.first > rhs.first) return false;
return (lhs.first < rhs.first) || (lhs.second < rhs.second);
}
__device__ inline bool operator==(index_pair const& lhs, index_pair const& rhs)
{
return lhs.first == rhs.first && lhs.second == rhs.second;
}
} // namespace
/**
* Fixture for all nested loop conditional joins.
*/
template <typename T>
struct ConditionalJoinTest : public cudf::test::BaseFixture {
/**
* Convenience utility for parsing initializer lists of input data into
* suitable inputs for tables.
*/
template <typename U>
std::tuple<std::vector<cudf::test::fixed_width_column_wrapper<T>>,
std::vector<cudf::test::fixed_width_column_wrapper<T>>,
std::vector<cudf::column_view>,
std::vector<cudf::column_view>,
cudf::table_view,
cudf::table_view>
parse_input(std::vector<U> left_data, std::vector<U> right_data)
{
auto wrapper_generator = [](U& v) {
if constexpr (std::is_same_v<U, std::vector<T>>) {
return cudf::test::fixed_width_column_wrapper<T>(v.begin(), v.end());
} else if constexpr (std::is_same_v<U, std::pair<std::vector<T>, std::vector<bool>>>) {
return cudf::test::fixed_width_column_wrapper<T>(
v.first.begin(), v.first.end(), v.second.begin());
}
throw std::runtime_error("Invalid input to parse_input.");
return cudf::test::fixed_width_column_wrapper<T>();
};
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
std::vector<cudf::test::fixed_width_column_wrapper<T>> left_wrappers;
std::vector<cudf::column_view> left_columns;
for (auto v : left_data) {
left_wrappers.push_back(wrapper_generator(v));
left_columns.push_back(left_wrappers.back());
}
std::vector<cudf::test::fixed_width_column_wrapper<T>> right_wrappers;
std::vector<cudf::column_view> right_columns;
for (auto v : right_data) {
right_wrappers.push_back(wrapper_generator(v));
right_columns.push_back(right_wrappers.back());
}
return std::make_tuple(std::move(left_wrappers),
std::move(right_wrappers),
std::move(left_columns),
std::move(right_columns),
cudf::table_view(left_columns),
cudf::table_view(right_columns));
}
};
/**
* Fixture for join types that return both left and right indices (inner, left,
* and full joins).
*/
template <typename T>
struct ConditionalJoinPairReturnTest : public ConditionalJoinTest<T> {
/*
* Perform a join of tables constructed from two input data sets according to
* the provided predicate and verify that the outputs match the expected
* outputs (up to order).
*/
void _test(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate,
std::vector<std::pair<cudf::size_type, cudf::size_type>> expected_outputs)
{
auto result_size = this->join_size(left, right, predicate);
EXPECT_TRUE(result_size == expected_outputs.size());
auto result = this->join(left, right, predicate);
std::vector<std::pair<cudf::size_type, cudf::size_type>> result_pairs;
for (size_t i = 0; i < result.first->size(); ++i) {
// Note: Not trying to be terribly efficient here since these tests are
// small, otherwise a batch copy to host before constructing the tuples
// would be important.
result_pairs.push_back({result.first->element(i, cudf::get_default_stream()),
result.second->element(i, cudf::get_default_stream())});
}
std::sort(result_pairs.begin(), result_pairs.end());
std::sort(expected_outputs.begin(), expected_outputs.end());
EXPECT_TRUE(std::equal(expected_outputs.begin(), expected_outputs.end(), result_pairs.begin()));
}
/*
* Perform a join of tables constructed from two input data sets according to
* the provided predicate and verify that the outputs match the expected
* outputs (up to order).
*/
void test(ColumnVector<T> left_data,
ColumnVector<T> right_data,
cudf::ast::operation predicate,
std::vector<std::pair<cudf::size_type, cudf::size_type>> expected_outputs)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers, right_wrappers, left_columns, right_columns, left, right] =
this->parse_input(left_data, right_data);
this->_test(left, right, predicate, expected_outputs);
}
void test_nulls(NullableColumnVector<T> left_data,
NullableColumnVector<T> right_data,
cudf::ast::operation predicate,
std::vector<std::pair<cudf::size_type, cudf::size_type>> expected_outputs)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers, right_wrappers, left_columns, right_columns, left, right] =
this->parse_input(left_data, right_data);
this->_test(left, right, predicate, expected_outputs);
}
/*
* Perform a join of tables constructed from two input data sets according to
* an equality predicate on all corresponding columns and verify that the outputs match the
* expected outputs (up to order).
*/
void _compare_to_hash_join(PairJoinReturn const& result, PairJoinReturn const& reference)
{
auto result_pairs =
rmm::device_uvector<index_pair>(result.first->size(), cudf::get_default_stream());
auto reference_pairs =
rmm::device_uvector<index_pair>(reference.first->size(), cudf::get_default_stream());
thrust::transform(rmm::exec_policy(cudf::get_default_stream()),
result.first->begin(),
result.first->end(),
result.second->begin(),
result_pairs.begin(),
[] __device__(cudf::size_type first, cudf::size_type second) {
return index_pair{first, second};
});
thrust::transform(rmm::exec_policy(cudf::get_default_stream()),
reference.first->begin(),
reference.first->end(),
reference.second->begin(),
reference_pairs.begin(),
[] __device__(cudf::size_type first, cudf::size_type second) {
return index_pair{first, second};
});
thrust::sort(
rmm::exec_policy(cudf::get_default_stream()), result_pairs.begin(), result_pairs.end());
thrust::sort(
rmm::exec_policy(cudf::get_default_stream()), reference_pairs.begin(), reference_pairs.end());
EXPECT_TRUE(thrust::equal(rmm::exec_policy(cudf::get_default_stream()),
reference_pairs.begin(),
reference_pairs.end(),
result_pairs.begin()));
}
void compare_to_hash_join(ColumnVector<T> left_data, ColumnVector<T> right_data)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers, right_wrappers, left_columns, right_columns, left, right] =
this->parse_input(left_data, right_data);
auto result = this->join(left, right, left_zero_eq_right_zero);
auto reference = this->reference_join(left, right);
this->_compare_to_hash_join(result, reference);
}
void compare_to_hash_join_nulls(NullableColumnVector<T> left_data,
NullableColumnVector<T> right_data)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers, right_wrappers, left_columns, right_columns, left, right] =
this->parse_input(left_data, right_data);
// Test comparing nulls as equal (the default for ref joins, uses NULL_EQUAL for AST
// expression).
auto predicate =
cudf::ast::operation(cudf::ast::ast_operator::NULL_EQUAL, col_ref_left_0, col_ref_right_0);
auto result = this->join(left, right, predicate);
auto reference = this->reference_join(left, right);
this->_compare_to_hash_join(result, reference);
// Test comparing nulls as equal (null_equality::UNEQUAL for ref joins, uses EQUAL for AST
// expression).
result = this->join(left, right, left_zero_eq_right_zero);
reference = this->reference_join(left, right, cudf::null_equality::UNEQUAL);
this->_compare_to_hash_join(result, reference);
}
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* conditional join API.
*/
virtual PairJoinReturn join(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) = 0;
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* conditional join size computation API.
*/
virtual std::size_t join_size(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) = 0;
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* hash join API for comparison with conditional joins.
*/
virtual PairJoinReturn reference_join(
cudf::table_view left,
cudf::table_view right,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) = 0;
};
/**
* Tests of conditional inner joins.
*/
template <typename T>
struct ConditionalInnerJoinTest : public ConditionalJoinPairReturnTest<T> {
PairJoinReturn join(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_inner_join(left, right, predicate);
}
std::size_t join_size(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_inner_join_size(left, right, predicate);
}
PairJoinReturn reference_join(
cudf::table_view left,
cudf::table_view right,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::inner_join(left, right, compare_nulls);
}
};
TYPED_TEST_SUITE(ConditionalInnerJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(ConditionalInnerJoinTest, TestOneColumnOneRowAllEqual)
{
this->test({{0}}, {{0}}, left_zero_eq_right_zero, {{0, 0}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestOneColumnLeftEmpty)
{
this->test({{}}, {{3, 4, 5}}, left_zero_eq_right_zero, {});
};
TYPED_TEST(ConditionalInnerJoinTest, TestOneColumnTwoRowAllEqual)
{
this->test({{0, 1}}, {{0, 0}}, left_zero_eq_right_zero, {{0, 0}, {0, 1}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestTwoColumnOneRowAllEqual)
{
this->test({{0}, {0}}, {{0}, {0}}, left_zero_eq_right_zero, {{0, 0}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestTwoColumnThreeRowAllEqual)
{
this->test({{0, 1, 2}, {10, 20, 30}},
{{0, 1, 2}, {30, 40, 50}},
left_zero_eq_right_zero,
{{0, 0}, {1, 1}, {2, 2}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestTwoColumnThreeRowSomeEqual)
{
this->test({{0, 1, 2}, {10, 20, 30}},
{{0, 1, 3}, {30, 40, 50}},
left_zero_eq_right_zero,
{{0, 0}, {1, 1}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestNotComparison)
{
auto col_ref_0 = cudf::ast::column_reference(0);
auto expression = cudf::ast::operation(cudf::ast::ast_operator::NOT, col_ref_0);
this->test({{0, 1, 2}}, {{3, 4, 5}}, expression, {{0, 0}, {0, 1}, {0, 2}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestGreaterComparison)
{
auto col_ref_0 = cudf::ast::column_reference(0);
auto col_ref_1 = cudf::ast::column_reference(0, cudf::ast::table_reference::RIGHT);
auto expression = cudf::ast::operation(cudf::ast::ast_operator::GREATER, col_ref_0, col_ref_1);
this->test({{0, 1, 2}}, {{1, 0, 0}}, expression, {{1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestGreaterTwoColumnComparison)
{
auto col_ref_0 = cudf::ast::column_reference(0);
auto col_ref_1 = cudf::ast::column_reference(1, cudf::ast::table_reference::RIGHT);
auto expression = cudf::ast::operation(cudf::ast::ast_operator::GREATER, col_ref_0, col_ref_1);
this->test({{0, 1, 2}, {0, 0, 0}},
{{0, 0, 0}, {1, 0, 0}},
expression,
{{1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestGreaterDifferentNumberColumnComparison)
{
auto col_ref_0 = cudf::ast::column_reference(0);
auto col_ref_1 = cudf::ast::column_reference(1, cudf::ast::table_reference::RIGHT);
auto expression = cudf::ast::operation(cudf::ast::ast_operator::GREATER, col_ref_0, col_ref_1);
this->test(
{{0, 1, 2}}, {{0, 0, 0}, {1, 0, 0}}, expression, {{1, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestGreaterDifferentNumberColumnDifferentSizeComparison)
{
auto col_ref_0 = cudf::ast::column_reference(0);
auto col_ref_1 = cudf::ast::column_reference(1, cudf::ast::table_reference::RIGHT);
auto expression = cudf::ast::operation(cudf::ast::ast_operator::GREATER, col_ref_0, col_ref_1);
this->test({{0, 1}}, {{0, 0, 0}, {1, 0, 0}}, expression, {{1, 1}, {1, 2}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestComplexConditionMultipleColumns)
{
// LEFT is implicit, but specifying explicitly to validate that it works.
auto col_ref_0 = cudf::ast::column_reference(0, cudf::ast::table_reference::LEFT);
auto scalar_1 = cudf::numeric_scalar<TypeParam>(1);
auto literal_1 = cudf::ast::literal(scalar_1);
auto left_0_equal_1 = cudf::ast::operation(cudf::ast::ast_operator::EQUAL, col_ref_0, literal_1);
auto col_ref_1 = cudf::ast::column_reference(1, cudf::ast::table_reference::RIGHT);
auto comparison_filter =
cudf::ast::operation(cudf::ast::ast_operator::LESS, col_ref_1, col_ref_0);
auto expression =
cudf::ast::operation(cudf::ast::ast_operator::LOGICAL_AND, left_0_equal_1, comparison_filter);
this->test({{0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2}, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}},
{{0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2},
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
expression,
{{4, 0}, {5, 0}, {6, 0}, {7, 0}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestSymmetry)
{
auto col_ref_0 = cudf::ast::column_reference(0);
auto col_ref_1 = cudf::ast::column_reference(0, cudf::ast::table_reference::RIGHT);
auto expression = cudf::ast::operation(cudf::ast::ast_operator::GREATER, col_ref_1, col_ref_0);
auto expression_reverse =
cudf::ast::operation(cudf::ast::ast_operator::LESS, col_ref_0, col_ref_1);
this->test(
{{0, 1, 2}}, {{1, 2, 3}}, expression, {{0, 0}, {0, 1}, {0, 2}, {1, 1}, {1, 2}, {2, 2}});
this->test(
{{0, 1, 2}}, {{1, 2, 3}}, expression_reverse, {{0, 0}, {0, 1}, {0, 2}, {1, 1}, {1, 2}, {2, 2}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestCompareRandomToHash)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>();
this->compare_to_hash_join({left}, {right});
};
TYPED_TEST(ConditionalInnerJoinTest, TestCompareRandomToHashNulls)
{
auto [left, right] = gen_random_nullable_repeated_columns<TypeParam>();
this->compare_to_hash_join_nulls({left}, {right});
};
TYPED_TEST(ConditionalInnerJoinTest, TestCompareRandomToHashNullsLargerLeft)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>(2000, 10, 1000, 10);
this->compare_to_hash_join({left}, {right});
};
TYPED_TEST(ConditionalInnerJoinTest, TestCompareRandomToHashNullsLargerRight)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>(1000, 10, 2000, 10);
this->compare_to_hash_join({left}, {right});
};
TYPED_TEST(ConditionalInnerJoinTest, TestOneColumnTwoNullsRowAllEqual)
{
this->test_nulls(
{{{0, 1}, {1, 0}}}, {{{0, 0}, {1, 1}}}, left_zero_eq_right_zero, {{0, 0}, {0, 1}});
};
TYPED_TEST(ConditionalInnerJoinTest, TestOneColumnTwoNullsNoOutputRowAllEqual)
{
this->test_nulls({{{0, 1}, {0, 1}}}, {{{0, 0}, {1, 1}}}, left_zero_eq_right_zero, {});
};
/**
* Tests of conditional left joins.
*/
template <typename T>
struct ConditionalLeftJoinTest : public ConditionalJoinPairReturnTest<T> {
PairJoinReturn join(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_left_join(left, right, predicate);
}
std::size_t join_size(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_left_join_size(left, right, predicate);
}
PairJoinReturn reference_join(
cudf::table_view left,
cudf::table_view right,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::left_join(left, right, compare_nulls);
}
};
TYPED_TEST_SUITE(ConditionalLeftJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(ConditionalLeftJoinTest, TestTwoColumnThreeRowSomeEqual)
{
this->test({{0, 1, 2}, {10, 20, 30}},
{{0, 1, 3}, {30, 40, 50}},
left_zero_eq_right_zero,
{{0, 0}, {1, 1}, {2, JoinNoneValue}});
};
TYPED_TEST(ConditionalLeftJoinTest, TestOneColumnLeftEmpty)
{
this->test({{}}, {{3, 4, 5}}, left_zero_eq_right_zero, {});
};
TYPED_TEST(ConditionalLeftJoinTest, TestCompareRandomToHash)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>();
this->compare_to_hash_join({left}, {right});
};
TYPED_TEST(ConditionalLeftJoinTest, TestCompareRandomToHashNulls)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>();
this->compare_to_hash_join({left}, {right});
};
/**
* Tests of conditional full joins.
*/
template <typename T>
struct ConditionalFullJoinTest : public ConditionalJoinPairReturnTest<T> {
PairJoinReturn join(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_full_join(left, right, predicate);
}
std::size_t join_size(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
// Full joins don't actually support size calculations, but to support a
// uniform testing framework we just calculate it from the result of doing
// the join.
return cudf::conditional_full_join(left, right, predicate).first->size();
}
PairJoinReturn reference_join(
cudf::table_view left,
cudf::table_view right,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::full_join(left, right, compare_nulls);
}
};
TYPED_TEST_SUITE(ConditionalFullJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(ConditionalFullJoinTest, TestOneColumnNoneEqual)
{
this->test({{0, 1, 2}},
{{3, 4, 5}},
left_zero_eq_right_zero,
{{0, JoinNoneValue},
{1, JoinNoneValue},
{2, JoinNoneValue},
{JoinNoneValue, 0},
{JoinNoneValue, 1},
{JoinNoneValue, 2}});
};
TYPED_TEST(ConditionalFullJoinTest, TestOneColumnLeftEmpty)
{
this->test({{}},
{{3, 4, 5}},
left_zero_eq_right_zero,
{{JoinNoneValue, 0}, {JoinNoneValue, 1}, {JoinNoneValue, 2}});
};
TYPED_TEST(ConditionalFullJoinTest, TestTwoColumnThreeRowSomeEqual)
{
this->test({{0, 1, 2}, {10, 20, 30}},
{{0, 1, 3}, {30, 40, 50}},
left_zero_eq_right_zero,
{{0, 0}, {1, 1}, {2, JoinNoneValue}, {JoinNoneValue, 2}});
};
TYPED_TEST(ConditionalFullJoinTest, TestCompareRandomToHash)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>();
this->compare_to_hash_join({left}, {right});
};
TYPED_TEST(ConditionalFullJoinTest, TestCompareRandomToHashNulls)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>();
this->compare_to_hash_join({left}, {right});
};
/**
* Fixture for join types that return both only left indices (left semi and
* left anti).
*/
template <typename T>
struct ConditionalJoinSingleReturnTest : public ConditionalJoinTest<T> {
/*
* Perform a join of tables constructed from two input data sets according to
* the provided predicate and verify that the outputs match the expected
* outputs (up to order).
*/
void test(ColumnVector<T> left_data,
ColumnVector<T> right_data,
cudf::ast::operation predicate,
std::vector<cudf::size_type> expected_outputs)
{
auto [left_wrappers, right_wrappers, left_columns, right_columns, left, right] =
this->parse_input(left_data, right_data);
auto result_size = this->join_size(left, right, predicate);
EXPECT_TRUE(result_size == expected_outputs.size());
auto result = this->join(left, right, predicate);
std::vector<cudf::size_type> resulting_indices;
for (size_t i = 0; i < result->size(); ++i) {
// Note: Not trying to be terribly efficient here since these tests are
// small, otherwise a batch copy to host before constructing the tuples
// would be important.
resulting_indices.push_back(result->element(i, cudf::get_default_stream()));
}
std::sort(resulting_indices.begin(), resulting_indices.end());
std::sort(expected_outputs.begin(), expected_outputs.end());
EXPECT_TRUE(
std::equal(resulting_indices.begin(), resulting_indices.end(), expected_outputs.begin()));
}
void _compare_to_hash_join(std::unique_ptr<rmm::device_uvector<cudf::size_type>> const& result,
std::unique_ptr<rmm::device_uvector<cudf::size_type>> const& reference)
{
thrust::sort(rmm::exec_policy(cudf::get_default_stream()), result->begin(), result->end());
thrust::sort(
rmm::exec_policy(cudf::get_default_stream()), reference->begin(), reference->end());
EXPECT_TRUE(thrust::equal(rmm::exec_policy(cudf::get_default_stream()),
result->begin(),
result->end(),
reference->begin()));
}
/*
* Perform a join of tables constructed from two input data sets according to
* an equality predicate on all corresponding columns and verify that the outputs match the
* expected outputs (up to order).
*/
void compare_to_hash_join(ColumnVector<T> left_data, ColumnVector<T> right_data)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers, right_wrappers, left_columns, right_columns, left, right] =
this->parse_input(left_data, right_data);
auto result = this->join(left, right, left_zero_eq_right_zero);
auto reference = this->reference_join(left, right);
this->_compare_to_hash_join(result, reference);
}
void compare_to_hash_join_nulls(NullableColumnVector<T> left_data,
NullableColumnVector<T> right_data)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers, right_wrappers, left_columns, right_columns, left, right] =
this->parse_input(left_data, right_data);
auto predicate =
cudf::ast::operation(cudf::ast::ast_operator::NULL_EQUAL, col_ref_left_0, col_ref_right_0);
auto result = this->join(left, right, predicate);
auto reference = this->reference_join(left, right);
this->_compare_to_hash_join(result, reference);
result = this->join(left, right, left_zero_eq_right_zero);
reference = this->reference_join(left, right, cudf::null_equality::UNEQUAL);
this->_compare_to_hash_join(result, reference);
}
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* conditional join API.
*/
virtual SingleJoinReturn join(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) = 0;
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* conditional join size computation API.
*/
virtual std::size_t join_size(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) = 0;
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* hash join API for comparison with conditional joins.
*/
virtual SingleJoinReturn reference_join(
cudf::table_view left,
cudf::table_view right,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) = 0;
};
/**
* Tests of conditional left semi joins.
*/
template <typename T>
struct ConditionalLeftSemiJoinTest : public ConditionalJoinSingleReturnTest<T> {
SingleJoinReturn join(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_left_semi_join(left, right, predicate);
}
std::size_t join_size(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_left_semi_join_size(left, right, predicate);
}
SingleJoinReturn reference_join(
cudf::table_view left,
cudf::table_view right,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::left_semi_join(left, right, compare_nulls);
}
};
TYPED_TEST_SUITE(ConditionalLeftSemiJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(ConditionalLeftSemiJoinTest, TestTwoColumnThreeRowSomeEqual)
{
this->test({{0, 1, 2}, {10, 20, 30}}, {{0, 1, 3}, {30, 40, 50}}, left_zero_eq_right_zero, {0, 1});
};
TYPED_TEST(ConditionalLeftSemiJoinTest, TestCompareRandomToHash)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>();
this->compare_to_hash_join({left}, {right});
};
TYPED_TEST(ConditionalLeftSemiJoinTest, TestCompareRandomToHashNulls)
{
auto [left, right] = gen_random_nullable_repeated_columns<TypeParam>();
this->compare_to_hash_join_nulls({left}, {right});
};
/**
* Tests of conditional left anti joins.
*/
template <typename T>
struct ConditionalLeftAntiJoinTest : public ConditionalJoinSingleReturnTest<T> {
SingleJoinReturn join(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_left_anti_join(left, right, predicate);
}
std::size_t join_size(cudf::table_view left,
cudf::table_view right,
cudf::ast::operation predicate) override
{
return cudf::conditional_left_anti_join_size(left, right, predicate);
}
SingleJoinReturn reference_join(
cudf::table_view left,
cudf::table_view right,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::left_anti_join(left, right, compare_nulls);
}
};
TYPED_TEST_SUITE(ConditionalLeftAntiJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(ConditionalLeftAntiJoinTest, TestTwoColumnThreeRowSomeEqual)
{
this->test({{0, 1, 2}, {10, 20, 30}}, {{0, 1, 3}, {30, 40, 50}}, left_zero_eq_right_zero, {2});
};
TYPED_TEST(ConditionalLeftAntiJoinTest, TestCompareRandomToHash)
{
auto [left, right] = gen_random_repeated_columns<TypeParam>();
this->compare_to_hash_join({left}, {right});
};
TYPED_TEST(ConditionalLeftAntiJoinTest, TestCompareRandomToHashNulls)
{
auto [left, right] = gen_random_nullable_repeated_columns<TypeParam>();
this->compare_to_hash_join_nulls({left}, {right});
};
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/join/join_tests.cpp
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/column/column.hpp>
#include <cudf/column/column_factories.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/copying.hpp>
#include <cudf/detail/null_mask.hpp>
#include <cudf/detail/structs/utilities.hpp>
#include <cudf/dictionary/encode.hpp>
#include <cudf/join.hpp>
#include <cudf/scalar/scalar_factories.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/error.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <limits>
template <typename T>
using column_wrapper = cudf::test::fixed_width_column_wrapper<T>;
using strcol_wrapper = cudf::test::strings_column_wrapper;
using CVector = std::vector<std::unique_ptr<cudf::column>>;
using Table = cudf::table;
constexpr cudf::size_type NoneValue =
std::numeric_limits<cudf::size_type>::min(); // TODO: how to test if this isn't public?
// This function is a wrapper around cudf's join APIs that takes the gather map
// from join APIs and materializes the table that would be created by gathering
// from the joined tables. Join APIs originally returned tables like this, but
// they were modified in https://github.com/rapidsai/cudf/pull/7454. This
// helper function allows us to avoid rewriting all our tests in terms of
// gather maps.
template <std::pair<std::unique_ptr<rmm::device_uvector<cudf::size_type>>,
std::unique_ptr<rmm::device_uvector<cudf::size_type>>> (*join_impl)(
cudf::table_view const& left_keys,
cudf::table_view const& right_keys,
cudf::null_equality compare_nulls,
rmm::mr::device_memory_resource* mr),
cudf::out_of_bounds_policy oob_policy = cudf::out_of_bounds_policy::DONT_CHECK>
std::unique_ptr<cudf::table> join_and_gather(
cudf::table_view const& left_input,
cudf::table_view const& right_input,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
cudf::null_equality compare_nulls,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
{
auto left_selected = left_input.select(left_on);
auto right_selected = right_input.select(right_on);
auto const [left_join_indices, right_join_indices] =
join_impl(left_selected, right_selected, compare_nulls, mr);
auto left_indices_span = cudf::device_span<cudf::size_type const>{*left_join_indices};
auto right_indices_span = cudf::device_span<cudf::size_type const>{*right_join_indices};
auto left_indices_col = cudf::column_view{left_indices_span};
auto right_indices_col = cudf::column_view{right_indices_span};
auto left_result = cudf::gather(left_input, left_indices_col, oob_policy);
auto right_result = cudf::gather(right_input, right_indices_col, oob_policy);
auto joined_cols = left_result->release();
auto right_cols = right_result->release();
joined_cols.insert(joined_cols.end(),
std::make_move_iterator(right_cols.begin()),
std::make_move_iterator(right_cols.end()));
return std::make_unique<cudf::table>(std::move(joined_cols));
}
std::unique_ptr<cudf::table> inner_join(
cudf::table_view const& left_input,
cudf::table_view const& right_input,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
return join_and_gather<cudf::inner_join>(
left_input, right_input, left_on, right_on, compare_nulls);
}
std::unique_ptr<cudf::table> left_join(
cudf::table_view const& left_input,
cudf::table_view const& right_input,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
return join_and_gather<cudf::left_join, cudf::out_of_bounds_policy::NULLIFY>(
left_input, right_input, left_on, right_on, compare_nulls);
}
std::unique_ptr<cudf::table> full_join(
cudf::table_view const& full_input,
cudf::table_view const& right_input,
std::vector<cudf::size_type> const& full_on,
std::vector<cudf::size_type> const& right_on,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
return join_and_gather<cudf::full_join, cudf::out_of_bounds_policy::NULLIFY>(
full_input, right_input, full_on, right_on, compare_nulls);
}
struct JoinTest : public cudf::test::BaseFixture {
std::pair<std::unique_ptr<cudf::table>, std::unique_ptr<cudf::table>> gather_maps_as_tables(
cudf::column_view const& expected_left_map,
cudf::column_view const& expected_right_map,
std::pair<std::unique_ptr<rmm::device_uvector<cudf::size_type>>,
std::unique_ptr<rmm::device_uvector<cudf::size_type>>> const& result)
{
auto result_table =
cudf::table_view({cudf::column_view{cudf::data_type{cudf::type_id::INT32},
static_cast<cudf::size_type>(result.first->size()),
result.first->data(),
nullptr,
0},
cudf::column_view{cudf::data_type{cudf::type_id::INT32},
static_cast<cudf::size_type>(result.second->size()),
result.second->data(),
nullptr,
0}});
auto result_sort_order = cudf::sorted_order(result_table);
auto sorted_result = cudf::gather(result_table, *result_sort_order);
cudf::table_view gold({expected_left_map, expected_right_map});
auto gold_sort_order = cudf::sorted_order(gold);
auto sorted_gold = cudf::gather(gold, *gold_sort_order);
return std::pair(std::move(sorted_gold), std::move(sorted_result));
}
};
TEST_F(JoinTest, EmptySentinelRepro)
{
// This test reproduced an implementation specific behavior where the combination of these
// particular values ended up hashing to the empty key sentinel value used by the hash join
// This test may no longer be relevant if the implementation ever changes.
auto const left_first_col = cudf::test::fixed_width_column_wrapper<int32_t>{1197};
auto const left_second_col = cudf::test::strings_column_wrapper{"201812"};
auto const left_third_col = cudf::test::fixed_width_column_wrapper<int64_t>{2550000371};
auto const right_first_col = cudf::test::fixed_width_column_wrapper<int32_t>{1197};
auto const right_second_col = cudf::test::strings_column_wrapper{"201812"};
auto const right_third_col = cudf::test::fixed_width_column_wrapper<int64_t>{2550000371};
cudf::table_view left({left_first_col, left_second_col, left_third_col});
cudf::table_view right({right_first_col, right_second_col, right_third_col});
auto result = inner_join(left, right, {0, 1, 2}, {0, 1, 2});
EXPECT_EQ(result->num_rows(), 1);
}
TEST_F(JoinTest, LeftJoinNoNullsWithNoCommon)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 3}};
strcol_wrapper col0_1({"s0", "s1", "s2", "s4", "s1"});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1{{"s1", "s0", "s1", "s2", "s1"}};
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = left_join(t0, t1, {0}, {0});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 1, 2, 2, 0, 3}, {1, 1, 1, 1, 1, 1}};
strcol_wrapper col_gold_1({"s0", "s1", "s2", "s2", "s4", "s1"}, {1, 1, 1, 1, 1, 1});
column_wrapper<int32_t> col_gold_2{{0, 1, 2, 2, 4, 1}, {1, 1, 1, 1, 1, 1}};
column_wrapper<int32_t> col_gold_3{{3, -1, 2, 2, 0, 3}, {1, 0, 1, 1, 1, 1}};
strcol_wrapper col_gold_4({"s1", "", "s1", "s0", "s1", "s1"}, {1, 0, 1, 1, 1, 1});
column_wrapper<int32_t> col_gold_5{{1, -1, 1, 0, 1, 1}, {1, 0, 1, 1, 1, 1}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, FullJoinNoNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 3}};
strcol_wrapper col0_1({"s0", "s1", "s2", "s4", "s1"});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1{{"s1", "s0", "s1", "s2", "s1"}};
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = full_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 1, 2, 0, 3, -1, -1, -1, -1}, {1, 1, 1, 1, 1, 0, 0, 0, 0}};
strcol_wrapper col_gold_1({"s0", "s1", "s2", "s4", "s1", "", "", "", ""},
{1, 1, 1, 1, 1, 0, 0, 0, 0});
column_wrapper<int32_t> col_gold_2{{0, 1, 2, 4, 1, -1, -1, -1, -1}, {1, 1, 1, 1, 1, 0, 0, 0, 0}};
column_wrapper<int32_t> col_gold_3{{-1, -1, -1, -1, 3, 2, 2, 0, 4}, {0, 0, 0, 0, 1, 1, 1, 1, 1}};
strcol_wrapper col_gold_4({"", "", "", "", "s1", "s1", "s0", "s1", "s2"},
{0, 0, 0, 0, 1, 1, 1, 1, 1});
column_wrapper<int32_t> col_gold_5{{-1, -1, -1, -1, 1, 1, 0, 1, 2}, {0, 0, 0, 0, 1, 1, 1, 1, 1}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, FullJoinWithNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 3}};
strcol_wrapper col0_1({"s0", "s1", "s2", "s4", "s1"});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}, {1, 1, 1, 0, 1}};
strcol_wrapper col1_1{{"s1", "s0", "s1", "s2", "s1"}};
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = full_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 1, 2, 0, 3, -1, -1, -1, -1}, {1, 1, 1, 1, 1, 0, 0, 0, 0}};
strcol_wrapper col_gold_1({"s0", "s1", "s2", "s4", "s1", "", "", "", ""},
{1, 1, 1, 1, 1, 0, 0, 0, 0});
column_wrapper<int32_t> col_gold_2{{0, 1, 2, 4, 1, -1, -1, -1, -1}, {1, 1, 1, 1, 1, 0, 0, 0, 0}};
column_wrapper<int32_t> col_gold_3{{-1, -1, -1, -1, 3, 2, 2, 0, 4}, {0, 0, 0, 0, 1, 1, 1, 1, 0}};
strcol_wrapper col_gold_4({"", "", "", "", "s1", "s1", "s0", "s1", "s2"},
{0, 0, 0, 0, 1, 1, 1, 1, 1});
column_wrapper<int32_t> col_gold_5{{-1, -1, -1, -1, 1, 1, 0, 1, 2}, {0, 0, 0, 0, 1, 1, 1, 1, 1}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, FullJoinOnNulls)
{
// clang-format off
column_wrapper<int32_t> col0_0{{ 3, 1 },
{ 1, 0 }};
strcol_wrapper col0_1({"s0", "s1" });
column_wrapper<int32_t> col0_2{{ 0, 1 }};
column_wrapper<int32_t> col1_0{{ 2, 5, 3, 7 },
{ 1, 1, 1, 0 }};
strcol_wrapper col1_1({"s1", "s0", "s0", "s1" });
column_wrapper<int32_t> col1_2{{ 1, 4, 2, 8 }};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = full_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{ 3, -1, -1, -1},
{ 1, 0, 0, 0}};
strcol_wrapper col_gold_1{{ "s0", "s1", "", ""},
{ 1, 1, 0, 0}};
column_wrapper<int32_t> col_gold_2{{ 0, 1, -1, -1},
{ 1, 1, 0, 0}};
column_wrapper<int32_t> col_gold_3{{ 3, -1, 2, 5},
{ 1, 0, 1, 1}};
strcol_wrapper col_gold_4{{ "s0", "s1", "s1", "s0"}};
column_wrapper<int32_t> col_gold_5{{ 2, 8, 1, 4}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
// Repeat test with compare_nulls_equal=false,
// as per SQL standard.
result = full_join(t0, t1, {0, 1}, {0, 1}, cudf::null_equality::UNEQUAL);
result_sort_order = cudf::sorted_order(result->view());
sorted_result = cudf::gather(result->view(), *result_sort_order);
col_gold_0 = {{ 3, -1, -1, -1, -1},
{ 1, 0, 0, 0, 0}};
col_gold_1 = strcol_wrapper{{ "s0", "s1", "", "", ""},
{ 1, 1, 0, 0, 0}};
col_gold_2 = {{ 0, 1, -1, -1, -1},
{ 1, 1, 0, 0, 0}};
col_gold_3 = {{ 3, -1, 2, 5, -1},
{ 1, 0, 1, 1, 0}};
col_gold_4 = strcol_wrapper{{ "s0", "", "s1", "s0", "s1"},
{ 1, 0, 1, 1, 1}};
col_gold_5 = {{ 2, -1, 1, 4, 8},
{ 1, 0, 1, 1, 1}};
// clang-format on
CVector cols_gold_nulls_unequal;
cols_gold_nulls_unequal.push_back(col_gold_0.release());
cols_gold_nulls_unequal.push_back(col_gold_1.release());
cols_gold_nulls_unequal.push_back(col_gold_2.release());
cols_gold_nulls_unequal.push_back(col_gold_3.release());
cols_gold_nulls_unequal.push_back(col_gold_4.release());
cols_gold_nulls_unequal.push_back(col_gold_5.release());
Table gold_nulls_unequal{std::move(cols_gold_nulls_unequal)};
gold_sort_order = cudf::sorted_order(gold_nulls_unequal.view());
sorted_gold = cudf::gather(gold_nulls_unequal.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, LeftJoinNoNulls)
{
column_wrapper<int32_t> col0_0({3, 1, 2, 0, 3});
strcol_wrapper col0_1({"s0", "s1", "s2", "s4", "s1"});
column_wrapper<int32_t> col0_2({0, 1, 2, 4, 1});
column_wrapper<int32_t> col1_0({2, 2, 0, 4, 3});
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2({1, 0, 1, 2, 1});
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = left_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0({3, 1, 2, 0, 3});
strcol_wrapper col_gold_1({"s0", "s1", "s2", "s4", "s1"});
column_wrapper<int32_t> col_gold_2({0, 1, 2, 4, 1});
column_wrapper<int32_t> col_gold_3{{-1, -1, -1, -1, 3}, {0, 0, 0, 0, 1}};
strcol_wrapper col_gold_4{{"", "", "", "", "s1"}, {0, 0, 0, 0, 1}};
column_wrapper<int32_t> col_gold_5{{-1, -1, -1, -1, 1}, {0, 0, 0, 0, 1}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, LeftJoinWithNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 2}};
strcol_wrapper col0_1({"s1", "s1", "", "s4", "s0"}, {1, 1, 0, 1, 1});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = left_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 1, 2, 0, 2}, {1, 1, 1, 1, 1}};
strcol_wrapper col_gold_1({"s1", "s1", "", "s4", "s0"}, {1, 1, 0, 1, 1});
column_wrapper<int32_t> col_gold_2{{0, 1, 2, 4, 1}, {1, 1, 1, 1, 1}};
column_wrapper<int32_t> col_gold_3{{3, -1, -1, -1, 2}, {1, 0, 0, 0, 1}};
strcol_wrapper col_gold_4{{"s1", "", "", "", "s0"}, {1, 0, 0, 0, 1}};
column_wrapper<int32_t> col_gold_5{{1, -1, -1, -1, -1}, {1, 0, 0, 0, 0}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, LeftJoinWithStructsAndNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 2}};
strcol_wrapper col0_1({"s1", "s1", "", "s4", "s0"}, {1, 1, 0, 1, 1});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
auto col0_names_col = strcol_wrapper{
"Samuel Vimes", "Carrot Ironfoundersson", "Detritus", "Samuel Vimes", "Angua von Überwald"};
auto col0_ages_col = column_wrapper<int32_t>{{48, 27, 351, 31, 25}};
auto col0_is_human_col = column_wrapper<bool>{{true, true, false, false, false}, {1, 1, 0, 1, 0}};
auto col0_3 =
cudf::test::structs_column_wrapper{{col0_names_col, col0_ages_col, col0_is_human_col}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
auto col1_names_col = strcol_wrapper{
"Samuel Vimes", "Detritus", "Detritus", "Carrot Ironfoundersson", "Angua von Überwald"};
auto col1_ages_col = column_wrapper<int32_t>{{48, 35, 351, 22, 25}};
auto col1_is_human_col = column_wrapper<bool>{{true, true, false, false, true}, {1, 1, 0, 1, 1}};
auto col1_3 =
cudf::test::structs_column_wrapper{{col1_names_col, col1_ages_col, col1_is_human_col}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols0.push_back(col0_3.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
cols1.push_back(col1_3.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = left_join(t0, t1, {3}, {3});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 2, 1, 0, 2}, {1, 1, 1, 1, 1}};
strcol_wrapper col_gold_1({"s1", "", "s1", "s4", "s0"}, {1, 0, 1, 1, 1});
column_wrapper<int32_t> col_gold_2{{0, 2, 1, 4, 1}, {1, 1, 1, 1, 1}};
auto col0_gold_names_col = strcol_wrapper{
"Samuel Vimes", "Detritus", "Carrot Ironfoundersson", "Samuel Vimes", "Angua von Überwald"};
auto col0_gold_ages_col = column_wrapper<int32_t>{{48, 351, 27, 31, 25}};
auto col0_gold_is_human_col =
column_wrapper<bool>{{true, false, true, false, false}, {1, 0, 1, 1, 0}};
auto col_gold_3 = cudf::test::structs_column_wrapper{
{col0_gold_names_col, col0_gold_ages_col, col0_gold_is_human_col}};
column_wrapper<int32_t> col_gold_4{{2, 0, -1, -1, -1}, {1, 1, 0, 0, 0}};
strcol_wrapper col_gold_5{{"s1", "s1", "", "", ""}, {1, 1, 0, 0, 0}};
column_wrapper<int32_t> col_gold_6{{1, 1, -1, -1, -1}, {1, 1, 0, 0, 0}};
auto col1_gold_names_col = strcol_wrapper{{
"Samuel Vimes",
"Detritus",
"",
"",
"",
},
{1, 1, 0, 0, 0}};
auto col1_gold_ages_col = column_wrapper<int32_t>{{48, 351, -1, -1, -1}, {1, 1, 0, 0, 0}};
auto col1_gold_is_human_col =
column_wrapper<bool>{{true, false, false, false, false}, {1, 0, 0, 0, 0}};
auto col_gold_7 = cudf::test::structs_column_wrapper{
{col1_gold_names_col, col1_gold_ages_col, col1_gold_is_human_col}, {1, 1, 0, 0, 0}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
cols_gold.push_back(col_gold_6.release());
cols_gold.push_back(col_gold_7.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, LeftJoinOnNulls)
{
// clang-format off
column_wrapper<int32_t> col0_0{{ 3, 1, 2},
{ 1, 0, 1}};
strcol_wrapper col0_1({"s0", "s1", "s2" });
column_wrapper<int32_t> col0_2{{ 0, 1, 2 }};
column_wrapper<int32_t> col1_0{{ 2, 5, 3, 7 },
{ 1, 1, 1, 0 }};
strcol_wrapper col1_1({"s1", "s0", "s0", "s1" });
column_wrapper<int32_t> col1_2{{ 1, 4, 2, 8 }};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = left_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{ 3, -1, 2},
{ 1, 0, 1}};
strcol_wrapper col_gold_1({ "s0", "s1", "s2"},
{ 1, 1, 1});
column_wrapper<int32_t> col_gold_2{{ 0, 1, 2},
{ 1, 1, 1}};
column_wrapper<int32_t> col_gold_3{{ 3, -1, -1},
{ 1, 0, 0}};
strcol_wrapper col_gold_4({ "s0", "s1", ""},
{ 1, 1, 0});
column_wrapper<int32_t> col_gold_5{{ 2, 8, -1},
{ 1, 1, 0}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
// Repeat test with compare_nulls_equal=false,
// as per SQL standard.
result = left_join(t0, t1, {0, 1}, {0, 1}, cudf::null_equality::UNEQUAL);
result_sort_order = cudf::sorted_order(result->view());
sorted_result = cudf::gather(result->view(), *result_sort_order);
col_gold_0 = {{ 3, -1, 2},
{ 1, 0, 1}};
col_gold_1 = {{ "s0", "s1", "s2"},
{ 1, 1, 1}};
col_gold_2 = {{ 0, 1, 2},
{ 1, 1, 1}};
col_gold_3 = {{ 3, -1, -1},
{ 1, 0, 0}};
col_gold_4 = {{ "s0", "", ""},
{ 1, 0, 0}};
col_gold_5 = {{ 2, -1, -1},
{ 1, 0, 0}};
// clang-format on
CVector cols_gold_nulls_unequal;
cols_gold_nulls_unequal.push_back(col_gold_0.release());
cols_gold_nulls_unequal.push_back(col_gold_1.release());
cols_gold_nulls_unequal.push_back(col_gold_2.release());
cols_gold_nulls_unequal.push_back(col_gold_3.release());
Table gold_nulls_unequal{std::move(cols_gold_nulls_unequal)};
gold_sort_order = cudf::sorted_order(gold_nulls_unequal.view());
sorted_gold = cudf::gather(gold_nulls_unequal.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, InnerJoinNoNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 2}};
strcol_wrapper col0_1({"s1", "s1", "s0", "s4", "s0"});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = inner_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 2, 2}};
strcol_wrapper col_gold_1({"s1", "s0", "s0"});
column_wrapper<int32_t> col_gold_2{{0, 2, 1}};
column_wrapper<int32_t> col_gold_3{{3, 2, 2}};
strcol_wrapper col_gold_4({"s1", "s0", "s0"});
column_wrapper<int32_t> col_gold_5{{1, 0, 0}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, InnerJoinWithNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 2}};
strcol_wrapper col0_1({"s1", "s1", "s0", "s4", "s0"}, {1, 1, 0, 1, 1});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = inner_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 2}};
strcol_wrapper col_gold_1({"s1", "s0"}, {1, 1});
column_wrapper<int32_t> col_gold_2{{0, 1}};
column_wrapper<int32_t> col_gold_3{{3, 2}};
strcol_wrapper col_gold_4({"s1", "s0"}, {1, 1});
column_wrapper<int32_t> col_gold_5{{1, -1}, {1, 0}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, InnerJoinWithStructsAndNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 2}};
strcol_wrapper col0_1({"s1", "s1", "s0", "s4", "s0"}, {1, 1, 0, 1, 1});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
std::initializer_list<std::string> col0_names = {
"Samuel Vimes", "Carrot Ironfoundersson", "Detritus", "Samuel Vimes", "Angua von Überwald"};
auto col0_names_col = strcol_wrapper{col0_names.begin(), col0_names.end()};
auto col0_ages_col = column_wrapper<int32_t>{{48, 27, 351, 31, 25}};
auto col0_is_human_col = column_wrapper<bool>{{true, true, false, false, false}, {1, 1, 0, 1, 0}};
auto col0_3 =
cudf::test::structs_column_wrapper{{col0_names_col, col0_ages_col, col0_is_human_col}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
std::initializer_list<std::string> col1_names = {"Carrot Ironfoundersson",
"Angua von Überwald",
"Detritus",
"Carrot Ironfoundersson",
"Samuel Vimes"};
auto col1_names_col = strcol_wrapper{col1_names.begin(), col1_names.end()};
auto col1_ages_col = column_wrapper<int32_t>{{351, 25, 27, 31, 48}};
auto col1_is_human_col = column_wrapper<bool>{{true, false, false, false, true}, {1, 0, 0, 1, 1}};
auto col1_3 =
cudf::test::structs_column_wrapper{{col1_names_col, col1_ages_col, col1_is_human_col}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols0.push_back(col0_3.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
cols1.push_back(col1_3.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = inner_join(t0, t1, {0, 1, 3}, {0, 1, 3});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 2}};
strcol_wrapper col_gold_1({"s1", "s0"}, {1, 1});
column_wrapper<int32_t> col_gold_2{{0, 1}};
auto col_gold_3_names_col = strcol_wrapper{"Samuel Vimes", "Angua von Überwald"};
auto col_gold_3_ages_col = column_wrapper<int32_t>{{48, 25}};
auto col_gold_3_is_human_col = column_wrapper<bool>{{true, false}, {1, 0}};
auto col_gold_3 = cudf::test::structs_column_wrapper{
{col_gold_3_names_col, col_gold_3_ages_col, col_gold_3_is_human_col}};
column_wrapper<int32_t> col_gold_4{{3, 2}};
strcol_wrapper col_gold_5({"s1", "s0"}, {1, 1});
column_wrapper<int32_t> col_gold_6{{1, -1}, {1, 0}};
auto col_gold_7_names_col = strcol_wrapper{"Samuel Vimes", "Angua von Überwald"};
auto col_gold_7_ages_col = column_wrapper<int32_t>{{48, 25}};
auto col_gold_7_is_human_col = column_wrapper<bool>{{true, false}, {1, 0}};
auto col_gold_7 = cudf::test::structs_column_wrapper{
{col_gold_7_names_col, col_gold_7_ages_col, col_gold_7_is_human_col}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
cols_gold.push_back(col_gold_6.release());
cols_gold.push_back(col_gold_7.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
// // Test to check join behavior when join keys are null.
TEST_F(JoinTest, InnerJoinOnNulls)
{
// clang-format off
column_wrapper<int32_t> col0_0{{ 3, 1, 2, 0, 2}};
strcol_wrapper col0_1({"s1", "s1", "s8", "s4", "s0"},
{ 1, 1, 0, 1, 1});
column_wrapper<int32_t> col0_2{{ 0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{ 2, 2, 0, 4, 3}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"},
{ 1, 0, 1, 1, 1});
column_wrapper<int32_t> col1_2{{ 1, 0, 1, 2, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = inner_join(t0, t1, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0 {{ 3, 2}};
strcol_wrapper col_gold_1 ({"s1", "s0"},
{ 1, 0});
column_wrapper<int32_t> col_gold_2{{ 0, 2}};
column_wrapper<int32_t> col_gold_3 {{ 3, 2}};
strcol_wrapper col_gold_4 ({"s1", "s0"},
{ 1, 0});
column_wrapper<int32_t> col_gold_5{{ 1, 0}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
// Repeat test with compare_nulls_equal=false,
// as per SQL standard.
result = inner_join(t0, t1, {0, 1}, {0, 1}, cudf::null_equality::UNEQUAL);
result_sort_order = cudf::sorted_order(result->view());
sorted_result = cudf::gather(result->view(), *result_sort_order);
col_gold_0 = {{ 3}};
col_gold_1 = strcol_wrapper({"s1"},
{ 1});
col_gold_2 = {{ 0}};
col_gold_3 = {{ 3}};
col_gold_4 = strcol_wrapper({"s1"},
{ 1});
col_gold_5 = {{ 1}};
// clang-format on
CVector cols_gold_sql;
cols_gold_sql.push_back(col_gold_0.release());
cols_gold_sql.push_back(col_gold_1.release());
cols_gold_sql.push_back(col_gold_2.release());
cols_gold_sql.push_back(col_gold_3.release());
cols_gold_sql.push_back(col_gold_4.release());
cols_gold_sql.push_back(col_gold_5.release());
Table gold_sql(std::move(cols_gold_sql));
gold_sort_order = cudf::sorted_order(gold_sql.view());
sorted_gold = cudf::gather(gold_sql.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
// Empty Left Table
TEST_F(JoinTest, EmptyLeftTableInnerJoin)
{
column_wrapper<int32_t> col0_0;
column_wrapper<int32_t> col0_1;
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
column_wrapper<int32_t> col1_1{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table empty0(std::move(cols0));
Table t1(std::move(cols1));
auto result = inner_join(empty0, t1, {0, 1}, {0, 1});
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(empty0, *result);
}
TEST_F(JoinTest, EmptyLeftTableLeftJoin)
{
column_wrapper<int32_t> col0_0;
column_wrapper<int32_t> col0_1;
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
column_wrapper<int32_t> col1_1{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table empty0(std::move(cols0));
Table t1(std::move(cols1));
auto result = left_join(empty0, t1, {0, 1}, {0, 1});
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(empty0, *result);
}
TEST_F(JoinTest, EmptyLeftTableFullJoin)
{
column_wrapper<int32_t> col0_0;
column_wrapper<int32_t> col0_1;
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
column_wrapper<int32_t> col1_1{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table lhs(std::move(cols0));
Table rhs(std::move(cols1));
auto result = full_join(lhs, rhs, {0, 1}, {0, 1});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}};
column_wrapper<int32_t> col_gold_1{{-1, -1, -1, -1, -1}, {0, 0, 0, 0, 0}};
column_wrapper<int32_t> col_gold_2{{2, 2, 0, 4, 3}};
column_wrapper<int32_t> col_gold_3{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
// Empty Right Table
TEST_F(JoinTest, EmptyRightTableInnerJoin)
{
column_wrapper<int32_t> col0_0{{2, 2, 0, 4, 3}};
column_wrapper<int32_t> col0_1{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
column_wrapper<int32_t> col1_0;
column_wrapper<int32_t> col1_1;
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table empty1(std::move(cols1));
{
auto result = inner_join(t0, empty1, {0, 1}, {0, 1});
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(empty1, *result);
}
{
cudf::hash_join hash_join(empty1, cudf::null_equality::EQUAL);
auto output_size = hash_join.inner_join_size(t0);
std::optional<std::size_t> optional_size = output_size;
std::size_t const size_gold = 0;
EXPECT_EQ(output_size, size_gold);
auto result = hash_join.inner_join(t0, optional_size);
column_wrapper<int32_t> col_gold_0{};
column_wrapper<int32_t> col_gold_1{};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
}
TEST_F(JoinTest, EmptyRightTableLeftJoin)
{
column_wrapper<int32_t> col0_0{{2, 2, 0, 4, 3}, {1, 1, 1, 1, 1}};
column_wrapper<int32_t> col0_1{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
column_wrapper<int32_t> col1_0;
column_wrapper<int32_t> col1_1;
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table empty1(std::move(cols1));
{
auto result = left_join(t0, empty1, {0, 1}, {0, 1});
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(t0, *result);
}
{
cudf::hash_join hash_join(empty1, cudf::null_equality::EQUAL);
auto output_size = hash_join.left_join_size(t0);
std::optional<std::size_t> optional_size = output_size;
std::size_t const size_gold = 5;
EXPECT_EQ(output_size, size_gold);
auto result = hash_join.left_join(t0, optional_size);
column_wrapper<int32_t> col_gold_0{{0, 1, 2, 3, 4}};
column_wrapper<int32_t> col_gold_1{{NoneValue, NoneValue, NoneValue, NoneValue, NoneValue}};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
}
TEST_F(JoinTest, EmptyRightTableFullJoin)
{
column_wrapper<int32_t> col0_0{{2, 2, 0, 4, 3}};
column_wrapper<int32_t> col0_1{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
column_wrapper<int32_t> col1_0;
column_wrapper<int32_t> col1_1;
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table empty1(std::move(cols1));
{
auto result = full_join(t0, empty1, {0, 1}, {0, 1});
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(t0, *result);
}
{
cudf::hash_join hash_join(empty1, cudf::null_equality::EQUAL);
auto output_size = hash_join.full_join_size(t0);
std::optional<std::size_t> optional_size = output_size;
std::size_t const size_gold = 5;
EXPECT_EQ(output_size, size_gold);
auto result = hash_join.full_join(t0, optional_size);
column_wrapper<int32_t> col_gold_0{{0, 1, 2, 3, 4}};
column_wrapper<int32_t> col_gold_1{{NoneValue, NoneValue, NoneValue, NoneValue, NoneValue}};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
}
// Both tables empty
TEST_F(JoinTest, BothEmptyInnerJoin)
{
column_wrapper<int32_t> col0_0;
column_wrapper<int32_t> col0_1;
column_wrapper<int32_t> col1_0;
column_wrapper<int32_t> col1_1;
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table empty1(std::move(cols1));
auto result = inner_join(t0, empty1, {0, 1}, {0, 1});
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(empty1, *result);
}
TEST_F(JoinTest, BothEmptyLeftJoin)
{
column_wrapper<int32_t> col0_0;
column_wrapper<int32_t> col0_1;
column_wrapper<int32_t> col1_0;
column_wrapper<int32_t> col1_1;
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table empty1(std::move(cols1));
auto result = left_join(t0, empty1, {0, 1}, {0, 1});
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(empty1, *result);
}
TEST_F(JoinTest, BothEmptyFullJoin)
{
column_wrapper<int32_t> col0_0;
column_wrapper<int32_t> col0_1;
column_wrapper<int32_t> col1_0;
column_wrapper<int32_t> col1_1;
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table empty1(std::move(cols1));
auto result = full_join(t0, empty1, {0, 1}, {0, 1});
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(empty1, *result);
}
// // EqualValues X Inner,Left,Full
TEST_F(JoinTest, EqualValuesInnerJoin)
{
column_wrapper<int32_t> col0_0{{0, 0}};
strcol_wrapper col0_1({"s0", "s0"});
column_wrapper<int32_t> col1_0{{0, 0}};
strcol_wrapper col1_1({"s0", "s0"});
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = inner_join(t0, t1, {0, 1}, {0, 1});
column_wrapper<int32_t> col_gold_0{{0, 0, 0, 0}};
strcol_wrapper col_gold_1({"s0", "s0", "s0", "s0"});
column_wrapper<int32_t> col_gold_2{{0, 0, 0, 0}};
strcol_wrapper col_gold_3({"s0", "s0", "s0", "s0"});
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
Table gold(std::move(cols_gold));
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(gold, *result);
}
TEST_F(JoinTest, EqualValuesLeftJoin)
{
column_wrapper<int32_t> col0_0{{0, 0}};
strcol_wrapper col0_1({"s0", "s0"});
column_wrapper<int32_t> col1_0{{0, 0}};
strcol_wrapper col1_1({"s0", "s0"});
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = left_join(t0, t1, {0, 1}, {0, 1});
column_wrapper<int32_t> col_gold_0{{0, 0, 0, 0}, {1, 1, 1, 1}};
strcol_wrapper col_gold_1({"s0", "s0", "s0", "s0"}, {1, 1, 1, 1});
column_wrapper<int32_t> col_gold_2{{0, 0, 0, 0}, {1, 1, 1, 1}};
strcol_wrapper col_gold_3({"s0", "s0", "s0", "s0"}, {1, 1, 1, 1});
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
Table gold(std::move(cols_gold));
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(gold, *result);
}
TEST_F(JoinTest, EqualValuesFullJoin)
{
column_wrapper<int32_t> col0_0{{0, 0}};
strcol_wrapper col0_1({"s0", "s0"});
column_wrapper<int32_t> col1_0{{0, 0}};
strcol_wrapper col1_1({"s0", "s0"});
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = full_join(t0, t1, {0, 1}, {0, 1});
column_wrapper<int32_t> col_gold_0{{0, 0, 0, 0}};
strcol_wrapper col_gold_1({"s0", "s0", "s0", "s0"});
column_wrapper<int32_t> col_gold_2{{0, 0, 0, 0}};
strcol_wrapper col_gold_3({"s0", "s0", "s0", "s0"});
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
Table gold(std::move(cols_gold));
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(gold, *result);
}
TEST_F(JoinTest, InnerJoinCornerCase)
{
column_wrapper<int64_t> col0_0{{4, 1, 3, 2, 2, 2, 2}};
column_wrapper<int64_t> col1_0{{2}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols1.push_back(col1_0.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = inner_join(t0, t1, {0}, {0});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int64_t> col_gold_0{{2, 2, 2, 2}};
column_wrapper<int64_t> col_gold_1{{2, 2, 2, 2}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, HashJoinSequentialProbes)
{
CVector cols1;
cols1.emplace_back(column_wrapper<int32_t>{{2, 2, 0, 4, 3}}.release());
cols1.emplace_back(strcol_wrapper{{"s1", "s0", "s1", "s2", "s1"}}.release());
Table t1(std::move(cols1));
cudf::hash_join hash_join(t1, cudf::nullable_join::NO, cudf::null_equality::EQUAL);
{
CVector cols0;
cols0.emplace_back(column_wrapper<int32_t>{{3, 1, 2, 0, 3}}.release());
cols0.emplace_back(strcol_wrapper({"s0", "s1", "s2", "s4", "s1"}).release());
Table t0(std::move(cols0));
auto output_size = hash_join.full_join_size(t0);
std::optional<std::size_t> optional_size = output_size;
std::size_t const size_gold = 9;
EXPECT_EQ(output_size, size_gold);
auto result = hash_join.full_join(t0, optional_size);
column_wrapper<int32_t> col_gold_0{{NoneValue, NoneValue, NoneValue, NoneValue, 4, 0, 1, 2, 3}};
column_wrapper<int32_t> col_gold_1{{0, 1, 2, 3, 4, NoneValue, NoneValue, NoneValue, NoneValue}};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
{
CVector cols0;
cols0.emplace_back(column_wrapper<int32_t>{{3, 1, 2, 0, 3}}.release());
cols0.emplace_back(strcol_wrapper({"s0", "s1", "s2", "s4", "s1"}).release());
Table t0(std::move(cols0));
auto output_size = hash_join.left_join_size(t0);
std::optional<std::size_t> optional_size = output_size;
std::size_t const size_gold = 5;
EXPECT_EQ(output_size, size_gold);
auto result = hash_join.left_join(t0, optional_size);
column_wrapper<int32_t> col_gold_0{{0, 1, 2, 3, 4}};
column_wrapper<int32_t> col_gold_1{{NoneValue, NoneValue, NoneValue, NoneValue, 4}};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
{
CVector cols0;
cols0.emplace_back(column_wrapper<int32_t>{{3, 1, 2, 0, 2}}.release());
cols0.emplace_back(strcol_wrapper({"s1", "s1", "s0", "s4", "s0"}).release());
Table t0(std::move(cols0));
auto output_size = hash_join.inner_join_size(t0);
std::optional<std::size_t> optional_size = output_size;
std::size_t const size_gold = 3;
EXPECT_EQ(output_size, size_gold);
auto result = hash_join.inner_join(t0, optional_size);
column_wrapper<int32_t> col_gold_0{{2, 4, 0}};
column_wrapper<int32_t> col_gold_1{{1, 1, 4}};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
}
TEST_F(JoinTest, HashJoinWithStructsAndNulls)
{
auto col0_names_col = strcol_wrapper{
"Samuel Vimes", "Carrot Ironfoundersson", "Detritus", "Samuel Vimes", "Angua von Überwald"};
auto col0_ages_col = column_wrapper<int32_t>{{48, 27, 351, 31, 25}};
auto col0_is_human_col = column_wrapper<bool>{{true, true, false, false, false}, {1, 1, 0, 1, 0}};
auto col0 =
cudf::test::structs_column_wrapper{{col0_names_col, col0_ages_col, col0_is_human_col}};
auto col1_names_col = strcol_wrapper{
"Samuel Vimes", "Detritus", "Detritus", "Carrot Ironfoundersson", "Angua von Überwald"};
auto col1_ages_col = column_wrapper<int32_t>{{48, 35, 351, 22, 25}};
auto col1_is_human_col = column_wrapper<bool>{{true, true, false, false, true}, {1, 1, 0, 1, 1}};
auto col1 =
cudf::test::structs_column_wrapper{{col1_names_col, col1_ages_col, col1_is_human_col}};
CVector cols0, cols1;
cols0.push_back(col0.release());
cols1.push_back(col1.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto const has_nulls = cudf::has_nested_nulls(t0) || cudf::has_nested_nulls(t1)
? cudf::nullable_join::YES
: cudf::nullable_join::NO;
auto hash_join = cudf::hash_join(t1, has_nulls, cudf::null_equality::EQUAL);
{
auto output_size = hash_join.left_join_size(t0);
EXPECT_EQ(5, output_size);
auto result = hash_join.left_join(t0, output_size);
column_wrapper<int32_t> col_gold_0{{0, 1, 2, 3, 4}};
column_wrapper<int32_t> col_gold_1{{0, NoneValue, 2, NoneValue, NoneValue}};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
{
auto output_size = hash_join.inner_join_size(t0);
EXPECT_EQ(2, output_size);
auto result = hash_join.inner_join(t0, output_size);
column_wrapper<int32_t> col_gold_0{{0, 2}};
column_wrapper<int32_t> col_gold_1{{0, 2}};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
{
auto output_size = hash_join.full_join_size(t0);
EXPECT_EQ(8, output_size);
auto result = hash_join.full_join(t0, output_size);
column_wrapper<int32_t> col_gold_0{{NoneValue, NoneValue, NoneValue, 0, 1, 2, 3, 4}};
column_wrapper<int32_t> col_gold_1{{1, 3, 4, 0, NoneValue, 2, NoneValue, NoneValue}};
auto const [sorted_gold, sorted_result] = gather_maps_as_tables(col_gold_0, col_gold_1, result);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
}
TEST_F(JoinTest, HashJoinWithNullsOneSide)
{
auto const t0 = [] {
column_wrapper<int32_t> col0{2, 2, 0, 4, 3};
column_wrapper<int32_t> col1{1, 10, 1, 2, 1};
CVector cols;
cols.emplace_back(col0.release());
cols.emplace_back(col1.release());
return Table{std::move(cols)};
}();
auto const t1 = [] {
column_wrapper<int32_t> col0{1, 2, 3, 4, 5, 2, 2, 0, 4, 3, 1, 2, 3, 4, 5};
column_wrapper<int32_t> col1{{1, 2, 3, 4, 5, 1, 0, 1, 2, 1, 1, 2, 3, 4, 5},
cudf::test::iterators::null_at(6)};
CVector cols;
cols.emplace_back(col0.release());
cols.emplace_back(col1.release());
return Table{std::move(cols)};
}();
auto const hash_join = cudf::hash_join(t0, cudf::null_equality::EQUAL);
auto constexpr invalid = std::numeric_limits<int32_t>::min(); // invalid index sentinel
auto const sort_result = [](auto const& result) {
auto const left_cv = cudf::column_view{cudf::data_type{cudf::type_id::INT32},
static_cast<cudf::size_type>(result.first->size()),
result.first->data(),
nullptr,
0};
auto const right_cv = cudf::column_view{cudf::data_type{cudf::type_id::INT32},
static_cast<cudf::size_type>(result.second->size()),
result.second->data(),
nullptr,
0};
auto sorted_left = cudf::sort(cudf::table_view{{left_cv}});
auto sorted_right = cudf::sort(cudf::table_view{{right_cv}});
return std::pair{std::move(sorted_left), std::move(sorted_right)};
};
{
auto const output_size = hash_join.left_join_size(t1);
auto const result = hash_join.left_join(t1, std::optional<std::size_t>{output_size});
auto const [sorted_left_indices, sorted_right_indices] = sort_result(result);
auto const expected_left_indices =
column_wrapper<int32_t>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
auto const expected_right_indices = column_wrapper<int32_t>{invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
0,
2,
3,
4};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_left_indices, sorted_left_indices->get_column(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_right_indices, sorted_right_indices->get_column(0));
}
{
auto const output_size = hash_join.inner_join_size(t1);
auto const result = hash_join.inner_join(t1, std::optional<std::size_t>{output_size});
auto const [sorted_left_indices, sorted_right_indices] = sort_result(result);
auto const expected_left_indices = column_wrapper<int32_t>{5, 7, 8, 9};
auto const expected_right_indices = column_wrapper<int32_t>{0, 2, 3, 4};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_left_indices, sorted_left_indices->get_column(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_right_indices, sorted_right_indices->get_column(0));
}
{
auto const output_size = hash_join.full_join_size(t1);
auto const result = hash_join.full_join(t1, std::optional<std::size_t>{output_size});
auto const [sorted_left_indices, sorted_right_indices] = sort_result(result);
auto const expected_left_indices =
column_wrapper<int32_t>{invalid, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
auto const expected_right_indices = column_wrapper<int32_t>{invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
invalid,
0,
1,
2,
3,
4};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_left_indices, sorted_left_indices->get_column(0));
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_right_indices, sorted_right_indices->get_column(0));
}
}
TEST_F(JoinTest, HashJoinLargeOutputSize)
{
// self-join a table of zeroes to generate an output row count that would overflow int32_t
std::size_t col_size = 65567;
rmm::device_buffer zeroes(col_size * sizeof(int32_t), cudf::get_default_stream());
CUDF_CUDA_TRY(
cudaMemsetAsync(zeroes.data(), 0, zeroes.size(), cudf::get_default_stream().value()));
cudf::column_view col_zeros(
cudf::data_type{cudf::type_id::INT32}, col_size, zeroes.data(), nullptr, 0);
cudf::table_view tview{{col_zeros}};
cudf::hash_join hash_join(tview, cudf::nullable_join::NO, cudf::null_equality::UNEQUAL);
std::size_t output_size = hash_join.inner_join_size(tview);
EXPECT_EQ(col_size * col_size, output_size);
}
struct JoinDictionaryTest : public cudf::test::BaseFixture {};
TEST_F(JoinDictionaryTest, LeftJoinNoNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 3}};
strcol_wrapper col0_1_w({"s0", "s1", "s2", "s4", "s1"});
auto col0_1 = cudf::dictionary::encode(col0_1_w);
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1_w{{"s1", "s0", "s1", "s2", "s1"}};
auto col1_1 = cudf::dictionary::encode(col1_1_w);
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
auto t0 = cudf::table_view({col0_0, col0_1->view(), col0_2});
auto t1 = cudf::table_view({col1_0, col1_1->view(), col1_2});
auto g0 = cudf::table_view({col0_0, col0_1_w, col0_2});
auto g1 = cudf::table_view({col1_0, col1_1_w, col1_2});
auto result = left_join(t0, t1, {0}, {0});
auto result_view = result->view();
auto decoded1 = cudf::dictionary::decode(result_view.column(1));
auto decoded4 = cudf::dictionary::decode(result_view.column(4));
std::vector<cudf::column_view> result_decoded({result_view.column(0),
decoded1->view(),
result_view.column(2),
result_view.column(3),
decoded4->view(),
result_view.column(5)});
auto result_sort_order = cudf::sorted_order(cudf::table_view(result_decoded));
auto sorted_result = cudf::gather(cudf::table_view(result_decoded), *result_sort_order);
auto gold = left_join(g0, g1, {0}, {0});
auto gold_sort_order = cudf::sorted_order(gold->view());
auto sorted_gold = cudf::gather(gold->view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinDictionaryTest, LeftJoinWithNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 2}};
strcol_wrapper col0_1({"s1", "s1", "s0", "s4", "s0"}, {1, 1, 0, 1, 1});
column_wrapper<int32_t> col0_2_w{{0, 1, 2, 4, 1}};
auto col0_2 = cudf::dictionary::encode(col0_2_w);
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2_w{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
auto col1_2 = cudf::dictionary::encode(col1_2_w);
auto t0 = cudf::table_view({col0_0, col0_1, col0_2->view()});
auto t1 = cudf::table_view({col1_0, col1_1, col1_2->view()});
auto result = left_join(t0, t1, {0, 1}, {0, 1});
auto result_view = result->view();
auto decoded2 = cudf::dictionary::decode(result_view.column(2));
auto decoded5 = cudf::dictionary::decode(result_view.column(5));
std::vector<cudf::column_view> result_decoded({result_view.column(0),
result_view.column(1),
decoded2->view(),
result_view.column(3),
result_view.column(4),
decoded5->view()});
auto result_sort_order = cudf::sorted_order(cudf::table_view(result_decoded));
auto sorted_result = cudf::gather(cudf::table_view(result_decoded), *result_sort_order);
auto g0 = cudf::table_view({col0_0, col0_1, col0_2_w});
auto g1 = cudf::table_view({col1_0, col1_1, col1_2_w});
auto gold = left_join(g0, g1, {0, 1}, {0, 1});
auto gold_sort_order = cudf::sorted_order(gold->view());
auto sorted_gold = cudf::gather(gold->view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinDictionaryTest, InnerJoinNoNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 2}};
strcol_wrapper col0_1_w({"s1", "s1", "s0", "s4", "s0"});
auto col0_1 = cudf::dictionary::encode(col0_1_w);
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1_w({"s1", "s0", "s1", "s2", "s1"});
auto col1_1 = cudf::dictionary::encode(col1_1_w);
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
auto t0 = cudf::table_view({col0_0, col0_1->view(), col0_2});
auto t1 = cudf::table_view({col1_0, col1_1->view(), col1_2});
auto result = inner_join(t0, t1, {0, 1}, {0, 1});
auto result_view = result->view();
auto decoded1 = cudf::dictionary::decode(result_view.column(1));
auto decoded4 = cudf::dictionary::decode(result_view.column(4));
std::vector<cudf::column_view> result_decoded({result_view.column(0),
decoded1->view(),
result_view.column(2),
result_view.column(3),
decoded4->view(),
result_view.column(5)});
auto result_sort_order = cudf::sorted_order(cudf::table_view(result_decoded));
auto sorted_result = cudf::gather(cudf::table_view(result_decoded), *result_sort_order);
auto g0 = cudf::table_view({col0_0, col0_1_w, col0_2});
auto g1 = cudf::table_view({col1_0, col1_1_w, col1_2});
auto gold = inner_join(g0, g1, {0, 1}, {0, 1});
auto gold_sort_order = cudf::sorted_order(gold->view());
auto sorted_gold = cudf::gather(gold->view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinDictionaryTest, InnerJoinWithNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 2}};
strcol_wrapper col0_1({"s1", "s1", "s0", "s4", "s0"}, {1, 1, 0, 1, 1});
column_wrapper<int32_t> col0_2_w{{0, 1, 2, 4, 1}};
auto col0_2 = cudf::dictionary::encode(col0_2_w);
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2_w{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
auto col1_2 = cudf::dictionary::encode(col1_2_w);
auto t0 = cudf::table_view({col0_0, col0_1, col0_2->view()});
auto t1 = cudf::table_view({col1_0, col1_1, col1_2->view()});
auto result = inner_join(t0, t1, {0, 1}, {0, 1});
auto result_view = result->view();
auto decoded2 = cudf::dictionary::decode(result_view.column(2));
auto decoded5 = cudf::dictionary::decode(result_view.column(5));
std::vector<cudf::column_view> result_decoded({result_view.column(0),
result_view.column(1),
decoded2->view(),
result_view.column(3),
result_view.column(4),
decoded5->view()});
auto result_sort_order = cudf::sorted_order(cudf::table_view(result_decoded));
auto sorted_result = cudf::gather(cudf::table_view(result_decoded), *result_sort_order);
auto g0 = cudf::table_view({col0_0, col0_1, col0_2_w});
auto g1 = cudf::table_view({col1_0, col1_1, col1_2_w});
auto gold = inner_join(g0, g1, {0, 1}, {0, 1});
auto gold_sort_order = cudf::sorted_order(gold->view());
auto sorted_gold = cudf::gather(gold->view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinDictionaryTest, FullJoinNoNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 3}};
strcol_wrapper col0_1_w({"s0", "s1", "s2", "s4", "s1"});
auto col0_1 = cudf::dictionary::encode(col0_1_w);
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}};
strcol_wrapper col1_1_w{{"s1", "s0", "s1", "s2", "s1"}};
auto col1_1 = cudf::dictionary::encode(col1_1_w);
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
auto t0 = cudf::table_view({col0_0, col0_1->view(), col0_2});
auto t1 = cudf::table_view({col1_0, col1_1->view(), col1_2});
auto result = full_join(t0, t1, {0, 1}, {0, 1});
auto result_view = result->view();
auto decoded1 = cudf::dictionary::decode(result_view.column(1));
auto decoded4 = cudf::dictionary::decode(result_view.column(4));
std::vector<cudf::column_view> result_decoded({result_view.column(0),
decoded1->view(),
result_view.column(2),
result_view.column(3),
decoded4->view(),
result_view.column(5)});
auto result_sort_order = cudf::sorted_order(cudf::table_view(result_decoded));
auto sorted_result = cudf::gather(cudf::table_view(result_decoded), *result_sort_order);
auto g0 = cudf::table_view({col0_0, col0_1_w, col0_2});
auto g1 = cudf::table_view({col1_0, col1_1_w, col1_2});
auto gold = full_join(g0, g1, {0, 1}, {0, 1});
auto gold_sort_order = cudf::sorted_order(gold->view());
auto sorted_gold = cudf::gather(gold->view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinDictionaryTest, FullJoinWithNulls)
{
column_wrapper<int32_t> col0_0_w{{3, 1, 2, 0, 3}};
auto col0_0 = cudf::dictionary::encode(col0_0_w);
strcol_wrapper col0_1({"s0", "s1", "s2", "s4", "s1"});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
column_wrapper<int32_t> col1_0_w{{2, 2, 0, 4, 3}, {1, 1, 1, 0, 1}};
auto col1_0 = cudf::dictionary::encode(col1_0_w);
strcol_wrapper col1_1{{"s1", "s0", "s1", "s2", "s1"}};
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
auto t0 = cudf::table_view({col0_0->view(), col0_1, col0_2});
auto t1 = cudf::table_view({col1_0->view(), col1_1, col1_2});
auto result = full_join(t0, t1, {0, 1}, {0, 1});
auto result_view = result->view();
auto decoded0 = cudf::dictionary::decode(result_view.column(0));
auto decoded3 = cudf::dictionary::decode(result_view.column(3));
std::vector<cudf::column_view> result_decoded({decoded0->view(),
result_view.column(1),
result_view.column(2),
decoded3->view(),
result_view.column(4),
result_view.column(5)});
auto result_sort_order = cudf::sorted_order(cudf::table_view(result_decoded));
auto sorted_result = cudf::gather(cudf::table_view(result_decoded), *result_sort_order);
auto g0 = cudf::table_view({col0_0_w, col0_1, col0_2});
auto g1 = cudf::table_view({col1_0_w, col1_1, col1_2});
auto gold = full_join(g0, g1, {0, 1}, {0, 1});
auto gold_sort_order = cudf::sorted_order(gold->view());
auto sorted_gold = cudf::gather(gold->view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, FullJoinWithStructsAndNulls)
{
column_wrapper<int32_t> col0_0{{3, 1, 2, 0, 3}};
strcol_wrapper col0_1({"s0", "s1", "s2", "s4", "s1"});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
std::initializer_list<std::string> col0_names = {"Samuel Vimes",
"Carrot Ironfoundersson",
"Angua von Überwald",
"Detritus",
"Carrot Ironfoundersson"};
auto col0_names_col = strcol_wrapper{col0_names.begin(), col0_names.end()};
auto col0_ages_col = column_wrapper<int32_t>{{48, 27, 25, 31, 351}};
auto col0_is_human_col = column_wrapper<bool>{{true, true, false, false, false}, {1, 1, 0, 1, 1}};
auto col0_3 = cudf::test::structs_column_wrapper{
{col0_names_col, col0_ages_col, col0_is_human_col}, {1, 1, 1, 1, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, 3}, {1, 1, 1, 0, 1}};
strcol_wrapper col1_1{{"s1", "s0", "s1", "s2", "s1"}};
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}};
std::initializer_list<std::string> col1_names = {"Carrot Ironfoundersson",
"Samuel Vimes",
"Carrot Ironfoundersson",
"Angua von Überwald",
"Carrot Ironfoundersson"};
auto col1_names_col = strcol_wrapper{col1_names.begin(), col1_names.end()};
auto col1_ages_col = column_wrapper<int32_t>{{27, 48, 27, 25, 27}};
auto col1_is_human_col = column_wrapper<bool>{{true, true, true, false, true}, {1, 1, 1, 0, 1}};
auto col1_3 =
cudf::test::structs_column_wrapper{{col1_names_col, col1_ages_col, col1_is_human_col}};
CVector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols0.push_back(col0_3.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
cols1.push_back(col1_3.release());
Table t0(std::move(cols0));
Table t1(std::move(cols1));
auto result = full_join(t0, t1, {0, 1, 3}, {0, 1, 3});
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{3, 1, 2, 0, 3, -1, -1, -1, -1, -1},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0}};
strcol_wrapper col_gold_1({"s0", "s1", "s2", "s4", "s1", "", "", "", "", ""},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0});
column_wrapper<int32_t> col_gold_2{{0, 1, 2, 4, 1, -1, -1, -1, -1, -1},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0}};
auto gold_names0_col = strcol_wrapper{{"Samuel Vimes",
"Carrot Ironfoundersson",
"Angua von Überwald",
"Detritus",
"Carrot Ironfoundersson",
"",
"",
"",
"",
""},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0}};
auto gold_ages0_col = column_wrapper<int32_t>{{48, 27, 25, 31, 351, -1, -1, -1, -1, -1},
{1, 1, 1, 1, 1, 0, 0, 0, 0, 0}};
auto gold_is_human0_col =
column_wrapper<bool>{{true, true, false, false, false, false, false, false, false, false},
{1, 1, 0, 1, 1, 0, 0, 0, 0, 0}};
auto col_gold_3 = cudf::test::structs_column_wrapper{
{gold_names0_col, gold_ages0_col, gold_is_human0_col}, {1, 1, 1, 1, 1, 0, 0, 0, 0, 0}};
column_wrapper<int32_t> col_gold_4{{-1, -1, -1, -1, -1, 3, 2, 2, 0, 4},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 0}};
strcol_wrapper col_gold_5({"", "", "", "", "", "s1", "s1", "s0", "s1", "s2"},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1});
column_wrapper<int32_t> col_gold_6{{-1, -1, -1, -1, -1, 1, 1, 0, 1, 2},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1}};
auto gold_names1_col = strcol_wrapper{{"",
"",
"",
"",
"",
"Carrot Ironfoundersson",
"Carrot Ironfoundersson",
"Samuel Vimes",
"Carrot Ironfoundersson",
"Angua von Überwald"},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1}};
auto gold_ages1_col = column_wrapper<int32_t>{{-1, -1, -1, -1, -1, 27, 27, 48, 27, 25},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 1}};
auto gold_is_human1_col =
column_wrapper<bool>{{false, false, false, false, false, true, true, true, true, false},
{0, 0, 0, 0, 0, 1, 1, 1, 1, 0}};
auto col_gold_7 = cudf::test::structs_column_wrapper{
{gold_names1_col, gold_ages1_col, gold_is_human1_col}, {0, 0, 0, 0, 0, 1, 1, 1, 1, 1}};
CVector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
cols_gold.push_back(col_gold_4.release());
cols_gold.push_back(col_gold_5.release());
cols_gold.push_back(col_gold_6.release());
cols_gold.push_back(col_gold_7.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
using lcw = cudf::test::lists_column_wrapper<int32_t>;
using cudf::test::iterators::null_at;
struct JoinTestLists : public cudf::test::BaseFixture {
/*
[
NULL, 0
[1], 1
[2, NULL], 2
[], 3
[5, 6] 4
*/
lcw build{{{0}, {1}, {{2, 0}, null_at(1)}, {}, {5, 6}}, null_at(0)};
/*
[
[1], 0
[3], 1
NULL, 2
[], 3
[2, NULL], 4
[5], 5
[6] 6
]
*/
lcw probe{{{1}, {3}, {0}, {}, {{2, 0}, null_at(1)}, {5}, {6}}, null_at(2)};
auto column_view_from_device_uvector(rmm::device_uvector<cudf::size_type> const& vector)
{
auto const indices_span = cudf::device_span<cudf::size_type const>{vector};
return cudf::column_view{indices_span};
}
auto sort_and_gather(
cudf::table_view table,
cudf::column_view gather_map,
cudf::out_of_bounds_policy oob_policy = cudf::out_of_bounds_policy::DONT_CHECK)
{
auto const gather_table = cudf::gather(table, gather_map, oob_policy);
auto const sort_order = cudf::sorted_order(*gather_table);
return cudf::gather(*gather_table, *sort_order);
}
template <typename JoinFunc>
void join(cudf::column_view left_gold_map,
cudf::column_view right_gold_map,
cudf::null_equality nulls_equal,
JoinFunc join_func,
cudf::out_of_bounds_policy oob_policy)
{
auto const build_tv = cudf::table_view{{build}};
auto const probe_tv = cudf::table_view{{probe}};
auto const [left_result_map, right_result_map] =
join_func(build_tv, probe_tv, nulls_equal, rmm::mr::get_current_device_resource());
auto const left_result_table =
sort_and_gather(build_tv, column_view_from_device_uvector(*left_result_map), oob_policy);
auto const right_result_table =
sort_and_gather(probe_tv, column_view_from_device_uvector(*right_result_map), oob_policy);
auto const left_gold_table = sort_and_gather(build_tv, left_gold_map, oob_policy);
auto const right_gold_table = sort_and_gather(probe_tv, right_gold_map, oob_policy);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*left_result_table, *left_gold_table);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*right_result_table, *right_gold_table);
}
void inner_join(cudf::column_view left_gold_map,
cudf::column_view right_gold_map,
cudf::null_equality nulls_equal)
{
join(left_gold_map,
right_gold_map,
nulls_equal,
cudf::inner_join,
cudf::out_of_bounds_policy::DONT_CHECK);
}
void full_join(cudf::column_view left_gold_map,
cudf::column_view right_gold_map,
cudf::null_equality nulls_equal)
{
join(left_gold_map,
right_gold_map,
nulls_equal,
cudf::full_join,
cudf::out_of_bounds_policy::NULLIFY);
}
void left_join(cudf::column_view left_gold_map,
cudf::column_view right_gold_map,
cudf::null_equality nulls_equal)
{
join(left_gold_map,
right_gold_map,
nulls_equal,
cudf::left_join,
cudf::out_of_bounds_policy::NULLIFY);
}
};
TEST_F(JoinTestLists, ListWithNullsEqualInnerJoin)
{
auto const left_gold_map = column_wrapper<int32_t>({0, 1, 2, 3});
auto const right_gold_map = column_wrapper<int32_t>({0, 2, 3, 4});
this->inner_join(left_gold_map, right_gold_map, cudf::null_equality::EQUAL);
}
TEST_F(JoinTestLists, ListWithNullsUnequalInnerJoin)
{
auto const left_gold_map = column_wrapper<int32_t>({1, 3});
auto const right_gold_map = column_wrapper<int32_t>({0, 3});
this->inner_join(left_gold_map, right_gold_map, cudf::null_equality::UNEQUAL);
}
TEST_F(JoinTestLists, ListWithNullsEqualFullJoin)
{
auto const left_gold_map =
column_wrapper<int32_t>({0, 1, 2, 3, 4, NoneValue, NoneValue, NoneValue});
auto const right_gold_map = column_wrapper<int32_t>({2, 0, 4, 3, NoneValue, 1, 5, 6});
this->full_join(left_gold_map, right_gold_map, cudf::null_equality::EQUAL);
}
TEST_F(JoinTestLists, ListWithNullsUnequalFullJoin)
{
auto const left_gold_map =
column_wrapper<int32_t>({0, 1, 2, 3, 4, NoneValue, NoneValue, NoneValue, NoneValue, NoneValue});
auto const right_gold_map =
column_wrapper<int32_t>({NoneValue, 0, NoneValue, 3, NoneValue, 1, 5, 6, 2, 4});
this->full_join(left_gold_map, right_gold_map, cudf::null_equality::UNEQUAL);
}
TEST_F(JoinTestLists, ListWithNullsEqualLeftJoin)
{
auto const left_gold_map = column_wrapper<int32_t>({0, 1, 2, 3, 4});
auto const right_gold_map = column_wrapper<int32_t>({2, 0, 4, 3, NoneValue});
this->left_join(left_gold_map, right_gold_map, cudf::null_equality::EQUAL);
}
TEST_F(JoinTestLists, ListWithNullsUnequalLeftJoin)
{
auto const left_gold_map = column_wrapper<int32_t>({0, 1, 2, 3, 4});
auto const right_gold_map = column_wrapper<int32_t>({NoneValue, 0, NoneValue, 3, NoneValue});
this->left_join(left_gold_map, right_gold_map, cudf::null_equality::UNEQUAL);
}
CUDF_TEST_PROGRAM_MAIN()
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/join/mixed_join_tests.cu
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/ast/expressions.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/join.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/type_lists.hpp>
#include <thrust/device_vector.h>
#include <thrust/execution_policy.h>
#include <thrust/host_vector.h>
#include <thrust/pair.h>
#include <thrust/sort.h>
#include <algorithm>
#include <iostream>
#include <random>
#include <stdexcept>
#include <tuple>
#include <utility>
#include <vector>
namespace {
using PairJoinReturn = std::pair<std::unique_ptr<rmm::device_uvector<cudf::size_type>>,
std::unique_ptr<rmm::device_uvector<cudf::size_type>>>;
using SingleJoinReturn = std::unique_ptr<rmm::device_uvector<cudf::size_type>>;
using NullMaskVector = std::vector<bool>;
template <typename T>
using ColumnVector = std::vector<std::vector<T>>;
template <typename T>
using NullableColumnVector = std::vector<std::pair<std::vector<T>, NullMaskVector>>;
constexpr cudf::size_type JoinNoneValue =
std::numeric_limits<cudf::size_type>::min(); // TODO: how to test if this isn't public?
// Common column references.
auto const col_ref_left_0 = cudf::ast::column_reference(0, cudf::ast::table_reference::LEFT);
auto const col_ref_right_0 = cudf::ast::column_reference(0, cudf::ast::table_reference::RIGHT);
// Common expressions.
auto left_zero_eq_right_zero =
cudf::ast::operation(cudf::ast::ast_operator::EQUAL, col_ref_left_0, col_ref_right_0);
// Generate a single pair of left/right non-nullable columns of random data
// suitable for testing a join against a reference join implementation.
template <typename T>
std::pair<std::vector<T>, std::vector<T>> gen_random_repeated_columns(
unsigned int N_left = 10000,
unsigned int num_repeats_left = 10,
unsigned int N_right = 10000,
unsigned int num_repeats_right = 10)
{
// Generate columns of num_repeats repeats of the integer range [0, num_unique),
// then merge a shuffled version and compare to hash join.
unsigned int num_unique_left = N_left / num_repeats_left;
unsigned int num_unique_right = N_right / num_repeats_right;
std::vector<T> left(N_left);
std::vector<T> right(N_right);
for (unsigned int i = 0; i < num_repeats_left; ++i) {
std::iota(std::next(left.begin(), num_unique_left * i),
std::next(left.begin(), num_unique_left * (i + 1)),
0);
}
for (unsigned int i = 0; i < num_repeats_right; ++i) {
std::iota(std::next(right.begin(), num_unique_right * i),
std::next(right.begin(), num_unique_right * (i + 1)),
0);
}
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(left.begin(), left.end(), gen);
std::shuffle(right.begin(), right.end(), gen);
return std::pair(std::move(left), std::move(right));
}
// Generate a single pair of left/right nullable columns of random data
// suitable for testing a join against a reference join implementation.
template <typename T>
std::pair<std::pair<std::vector<T>, std::vector<bool>>,
std::pair<std::vector<T>, std::vector<bool>>>
gen_random_nullable_repeated_columns(unsigned int N = 10000, unsigned int num_repeats = 10)
{
auto [left, right] = gen_random_repeated_columns<T>(N, num_repeats);
std::vector<bool> left_nulls(N);
std::vector<bool> right_nulls(N);
// Seed with a real random value, if available
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<> uniform_dist(0, 1);
std::generate(left_nulls.begin(), left_nulls.end(), [&uniform_dist, &gen]() {
return uniform_dist(gen) > 0.5;
});
std::generate(right_nulls.begin(), right_nulls.end(), [&uniform_dist, &gen]() {
return uniform_dist(gen) > 0.5;
});
return std::pair(std::pair(std::move(left), std::move(left_nulls)),
std::pair(std::move(right), std::move(right_nulls)));
}
} // namespace
/**
* Fixture for all mixed hash + conditional joins.
*/
template <typename T>
struct MixedJoinTest : public cudf::test::BaseFixture {
/**
* Convenience utility for parsing initializer lists of input data into
* suitable inputs for tables.
*/
template <typename U>
std::tuple<std::vector<cudf::test::fixed_width_column_wrapper<T>>,
std::vector<cudf::test::fixed_width_column_wrapper<T>>,
std::vector<cudf::column_view>,
std::vector<cudf::column_view>,
cudf::table_view,
cudf::table_view,
cudf::table_view,
cudf::table_view>
parse_input(std::vector<U> left_data,
std::vector<U> right_data,
std::vector<cudf::size_type> equality_columns,
std::vector<cudf::size_type> conditional_columns)
{
auto wrapper_generator = [](U& v) {
if constexpr (std::is_same_v<U, std::vector<T>>) {
return cudf::test::fixed_width_column_wrapper<T>(v.begin(), v.end());
} else if constexpr (std::is_same_v<U, std::pair<std::vector<T>, std::vector<bool>>>) {
return cudf::test::fixed_width_column_wrapper<T>(
v.first.begin(), v.first.end(), v.second.begin());
}
throw std::runtime_error("Invalid input to parse_input.");
return cudf::test::fixed_width_column_wrapper<T>();
};
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
std::vector<cudf::test::fixed_width_column_wrapper<T>> left_wrappers;
std::vector<cudf::column_view> left_columns;
for (auto v : left_data) {
left_wrappers.push_back(wrapper_generator(v));
left_columns.push_back(left_wrappers.back());
}
std::vector<cudf::test::fixed_width_column_wrapper<T>> right_wrappers;
std::vector<cudf::column_view> right_columns;
for (auto v : right_data) {
right_wrappers.push_back(wrapper_generator(v));
right_columns.push_back(right_wrappers.back());
}
auto left = cudf::table_view(left_columns);
auto right = cudf::table_view(right_columns);
return std::make_tuple(std::move(left_wrappers),
std::move(right_wrappers),
std::move(left_columns),
std::move(right_columns),
left.select(equality_columns),
right.select(equality_columns),
left.select(conditional_columns),
right.select(conditional_columns));
}
};
/**
* Fixture for join types that return both left and right indices (inner, left,
* and full joins).
*/
template <typename T>
struct MixedJoinPairReturnTest : public MixedJoinTest<T> {
/*
* Perform a join of tables constructed from two input data sets according to
* verify that the outputs match the expected outputs (up to order).
*/
virtual void _test(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
std::vector<cudf::size_type> expected_counts,
std::vector<std::pair<cudf::size_type, cudf::size_type>> expected_outputs,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
auto [result_size, actual_counts] = this->join_size(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
EXPECT_TRUE(result_size == expected_outputs.size());
cudf::test::fixed_width_column_wrapper<cudf::size_type> expected_counts_cw(
expected_counts.begin(), expected_counts.end());
auto const actual_counts_view =
cudf::column_view(cudf::data_type{cudf::type_to_id<cudf::size_type>()},
actual_counts->size(),
actual_counts->data(),
nullptr,
0);
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected_counts_cw, actual_counts_view);
auto result = this->join(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
std::vector<std::pair<cudf::size_type, cudf::size_type>> result_pairs;
for (size_t i = 0; i < result.first->size(); ++i) {
// Note: Not trying to be terribly efficient here since these tests are
// small, otherwise a batch copy to host before constructing the tuples
// would be important.
result_pairs.push_back({result.first->element(i, cudf::get_default_stream()),
result.second->element(i, cudf::get_default_stream())});
}
std::sort(result_pairs.begin(), result_pairs.end());
std::sort(expected_outputs.begin(), expected_outputs.end());
EXPECT_TRUE(std::equal(expected_outputs.begin(), expected_outputs.end(), result_pairs.begin()));
}
/*
* Perform a join of tables constructed from two input data sets according to
* the provided predicate and verify that the outputs match the expected
* outputs (up to order).
*/
void test(ColumnVector<T> left_data,
ColumnVector<T> right_data,
std::vector<cudf::size_type> equality_columns,
std::vector<cudf::size_type> conditional_columns,
cudf::ast::operation predicate,
std::vector<cudf::size_type> expected_counts,
std::vector<std::pair<cudf::size_type, cudf::size_type>> expected_outputs)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers,
right_wrappers,
left_columns,
right_columns,
left_equality,
right_equality,
left_conditional,
right_conditional] =
this->parse_input(left_data, right_data, equality_columns, conditional_columns);
this->_test(left_equality,
right_equality,
left_conditional,
right_conditional,
predicate,
expected_counts,
expected_outputs);
}
/*
* Perform a join of tables constructed from two input data sets according to
* the provided predicate and verify that the outputs match the expected
* outputs (up to order).
*/
void test_nulls(NullableColumnVector<T> left_data,
NullableColumnVector<T> right_data,
std::vector<cudf::size_type> equality_columns,
std::vector<cudf::size_type> conditional_columns,
cudf::ast::operation predicate,
std::vector<cudf::size_type> expected_counts,
std::vector<std::pair<cudf::size_type, cudf::size_type>> expected_outputs,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers,
right_wrappers,
left_columns,
right_columns,
left_equality,
right_equality,
left_conditional,
right_conditional] =
this->parse_input(left_data, right_data, equality_columns, conditional_columns);
this->_test(left_equality,
right_equality,
left_conditional,
right_conditional,
predicate,
expected_counts,
expected_outputs,
compare_nulls);
}
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* mixed join API.
*/
virtual PairJoinReturn join(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) = 0;
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* mixed join size computation API.
*/
virtual std::pair<std::size_t, std::unique_ptr<rmm::device_uvector<cudf::size_type>>> join_size(
cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) = 0;
};
/**
* Tests of mixed inner joins.
*/
template <typename T>
struct MixedInnerJoinTest : public MixedJoinPairReturnTest<T> {
PairJoinReturn join(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_inner_join(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
std::pair<std::size_t, std::unique_ptr<rmm::device_uvector<cudf::size_type>>> join_size(
cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_inner_join_size(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
};
TYPED_TEST_SUITE(MixedInnerJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(MixedInnerJoinTest, Empty)
{
this->test({}, {}, {}, {}, left_zero_eq_right_zero, {}, {});
}
TYPED_TEST(MixedInnerJoinTest, BasicEquality)
{
this->test({{0, 1, 2}, {3, 4, 5}, {10, 20, 30}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 1, 0},
{{1, 1}});
}
TYPED_TEST(MixedInnerJoinTest, BasicNullEqualityEqual)
{
this->test_nulls({{{0, 1, 2}, {1, 1, 0}}, {{3, 4, 5}, {1, 1, 1}}, {{10, 20, 30}, {1, 1, 1}}},
{{{0, 1, 3}, {1, 1, 0}}, {{5, 4, 5}, {1, 1, 1}}, {{30, 40, 30}, {1, 1, 1}}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 1, 1},
{{1, 1}, {2, 2}},
cudf::null_equality::EQUAL);
};
TYPED_TEST(MixedInnerJoinTest, BasicNullEqualityUnequal)
{
this->test_nulls({{{0, 1, 2}, {1, 1, 0}}, {{3, 4, 5}, {1, 1, 1}}, {{10, 20, 30}, {1, 1, 1}}},
{{{0, 1, 3}, {1, 1, 0}}, {{5, 4, 5}, {1, 1, 1}}, {{30, 40, 30}, {1, 1, 1}}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 1, 0},
{{1, 1}},
cudf::null_equality::UNEQUAL);
};
TYPED_TEST(MixedInnerJoinTest, AsymmetricEquality)
{
this->test({{0, 2, 1}, {3, 5, 4}, {10, 30, 20}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 0, 1},
{{2, 1}});
}
TYPED_TEST(MixedInnerJoinTest, AsymmetricLeftLargerEquality)
{
this->test({{0, 2, 1, 4}, {3, 5, 4, 10}, {10, 30, 20, 100}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 0, 1, 0},
{{2, 1}});
}
TYPED_TEST(MixedInnerJoinTest, AsymmetricLeftLargerGreater)
{
auto col_ref_left_1 = cudf::ast::column_reference(1, cudf::ast::table_reference::LEFT);
auto col_ref_right_1 = cudf::ast::column_reference(1, cudf::ast::table_reference::RIGHT);
auto condition =
cudf::ast::operation(cudf::ast::ast_operator::GREATER, col_ref_left_1, col_ref_right_1);
this->test({{2, 3, 9, 0, 1, 7, 4, 6, 5, 8}, {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}},
{{6, 5, 9, 8, 10, 32}, {0, 1, 2, 3, 4, 5}, {7, 8, 9, 0, 1, 2}},
{0},
{0, 1},
condition,
{0, 0, 1, 0, 0, 0, 0, 1, 1, 0},
{{2, 2}, {7, 0}, {8, 1}});
}
TYPED_TEST(MixedInnerJoinTest, AsymmetricRightLargerEquality)
{
this->test({{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{{0, 2, 1, 4}, {3, 5, 4, 10}, {10, 30, 20, 100}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 0, 1, 0},
{{1, 2}});
}
TYPED_TEST(MixedInnerJoinTest, BasicInequality)
{
auto const col_ref_left_1 = cudf::ast::column_reference(0, cudf::ast::table_reference::LEFT);
auto const col_ref_right_1 = cudf::ast::column_reference(0, cudf::ast::table_reference::RIGHT);
auto const col_ref_left_2 = cudf::ast::column_reference(1, cudf::ast::table_reference::LEFT);
auto const col_ref_right_2 = cudf::ast::column_reference(1, cudf::ast::table_reference::RIGHT);
auto scalar_1 = cudf::numeric_scalar<TypeParam>(35);
auto const literal_1 = cudf::ast::literal(scalar_1);
auto const op1 =
cudf::ast::operation(cudf::ast::ast_operator::LESS, col_ref_left_1, col_ref_right_1);
auto const op2 = cudf::ast::operation(cudf::ast::ast_operator::LESS, literal_1, col_ref_right_2);
auto const predicate = cudf::ast::operation(cudf::ast::ast_operator::LOGICAL_AND, op1, op2);
this->test({{0, 1, 2, 4}, {3, 4, 5, 6}, {10, 20, 30, 40}},
{{0, 1, 3, 4}, {5, 4, 5, 7}, {30, 40, 50, 60}},
{0},
{1, 2},
predicate,
{0, 0, 0, 1},
{{3, 3}});
}
/**
* Tests of mixed left joins.
*/
template <typename T>
struct MixedLeftJoinTest : public MixedJoinPairReturnTest<T> {
PairJoinReturn join(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_left_join(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
std::pair<std::size_t, std::unique_ptr<rmm::device_uvector<cudf::size_type>>> join_size(
cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_left_join_size(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
};
TYPED_TEST_SUITE(MixedLeftJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(MixedLeftJoinTest, Basic)
{
this->test({{0, 1, 2}, {3, 4, 5}, {10, 20, 30}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{1, 1, 1},
{{0, JoinNoneValue}, {1, 1}, {2, JoinNoneValue}});
}
TYPED_TEST(MixedLeftJoinTest, Basic2)
{
auto const col_ref_left_1 = cudf::ast::column_reference(0, cudf::ast::table_reference::LEFT);
auto const col_ref_right_1 = cudf::ast::column_reference(0, cudf::ast::table_reference::RIGHT);
auto const col_ref_left_2 = cudf::ast::column_reference(1, cudf::ast::table_reference::LEFT);
auto const col_ref_right_2 = cudf::ast::column_reference(1, cudf::ast::table_reference::RIGHT);
auto scalar_1 = cudf::numeric_scalar<TypeParam>(35);
auto const literal_1 = cudf::ast::literal(scalar_1);
auto const op1 =
cudf::ast::operation(cudf::ast::ast_operator::LESS, col_ref_left_1, col_ref_right_1);
auto const op2 = cudf::ast::operation(cudf::ast::ast_operator::LESS, literal_1, col_ref_right_2);
auto const predicate = cudf::ast::operation(cudf::ast::ast_operator::LOGICAL_AND, op1, op2);
this->test({{0, 1, 2, 4}, {3, 4, 5, 6}, {10, 20, 30, 40}},
{{0, 1, 3, 4}, {5, 4, 5, 7}, {30, 40, 50, 60}},
{0},
{1, 2},
predicate,
{1, 1, 1, 1},
{{0, JoinNoneValue}, {1, JoinNoneValue}, {2, JoinNoneValue}, {3, 3}});
}
/**
* Tests of mixed full joins.
*/
template <typename T>
struct MixedFullJoinTest : public MixedJoinPairReturnTest<T> {
PairJoinReturn join(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_full_join(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
std::pair<std::size_t, std::unique_ptr<rmm::device_uvector<cudf::size_type>>> join_size(
cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
// Full joins don't actually support size calculations, and there's no easy way to spoof it.
CUDF_FAIL("Size calculation not supported for full joins.");
}
/*
* Override method to remove size calculation testing since it's not possible for full joins.
*/
void _test(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
std::vector<cudf::size_type> expected_counts,
std::vector<std::pair<cudf::size_type, cudf::size_type>> expected_outputs,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
auto result = this->join(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
std::vector<std::pair<cudf::size_type, cudf::size_type>> result_pairs;
for (size_t i = 0; i < result.first->size(); ++i) {
result_pairs.push_back({result.first->element(i, cudf::get_default_stream()),
result.second->element(i, cudf::get_default_stream())});
}
std::sort(result_pairs.begin(), result_pairs.end());
std::sort(expected_outputs.begin(), expected_outputs.end());
EXPECT_TRUE(std::equal(expected_outputs.begin(), expected_outputs.end(), result_pairs.begin()));
}
};
TYPED_TEST_SUITE(MixedFullJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(MixedFullJoinTest, Basic)
{
this->test(
{{0, 1, 2}, {3, 4, 5}, {10, 20, 30}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{1, 1, 1},
{{0, JoinNoneValue}, {1, 1}, {2, JoinNoneValue}, {JoinNoneValue, 0}, {JoinNoneValue, 2}});
}
TYPED_TEST(MixedFullJoinTest, Basic2)
{
auto const col_ref_left_1 = cudf::ast::column_reference(0, cudf::ast::table_reference::LEFT);
auto const col_ref_right_1 = cudf::ast::column_reference(0, cudf::ast::table_reference::RIGHT);
auto const col_ref_left_2 = cudf::ast::column_reference(1, cudf::ast::table_reference::LEFT);
auto const col_ref_right_2 = cudf::ast::column_reference(1, cudf::ast::table_reference::RIGHT);
auto scalar_1 = cudf::numeric_scalar<TypeParam>(35);
auto const literal_1 = cudf::ast::literal(scalar_1);
auto const op1 =
cudf::ast::operation(cudf::ast::ast_operator::LESS, col_ref_left_1, col_ref_right_1);
auto const op2 = cudf::ast::operation(cudf::ast::ast_operator::LESS, literal_1, col_ref_right_2);
auto const predicate = cudf::ast::operation(cudf::ast::ast_operator::LOGICAL_AND, op1, op2);
this->test({{0, 1, 2, 4}, {3, 4, 5, 6}, {10, 20, 30, 40}},
{{0, 1, 3, 4}, {5, 4, 5, 7}, {30, 40, 50, 60}},
{0},
{1, 2},
predicate,
{1, 1, 1, 1},
{{0, JoinNoneValue},
{1, JoinNoneValue},
{2, JoinNoneValue},
{3, 3},
{JoinNoneValue, 0},
{JoinNoneValue, 1},
{JoinNoneValue, 2}});
}
template <typename T>
struct MixedJoinSingleReturnTest : public MixedJoinTest<T> {
/*
* Perform a join of tables constructed from two input data sets according to
* verify that the outputs match the expected outputs (up to order).
*/
virtual void _test(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
std::vector<cudf::size_type> expected_outputs,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
auto [result_size, actual_counts] = this->join_size(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
EXPECT_TRUE(result_size == expected_outputs.size());
auto result = this->join(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
std::vector<cudf::size_type> resulting_indices;
for (size_t i = 0; i < result->size(); ++i) {
// Note: Not trying to be terribly efficient here since these tests are
// small, otherwise a batch copy to host before constructing the tuples
// would be important.
resulting_indices.push_back(result->element(i, cudf::get_default_stream()));
}
std::sort(resulting_indices.begin(), resulting_indices.end());
std::sort(expected_outputs.begin(), expected_outputs.end());
EXPECT_TRUE(
std::equal(resulting_indices.begin(), resulting_indices.end(), expected_outputs.begin()));
}
/*
* Perform a join of tables constructed from two input data sets according to
* the provided predicate and verify that the outputs match the expected
* outputs (up to order).
*/
void test(ColumnVector<T> left_data,
ColumnVector<T> right_data,
std::vector<cudf::size_type> equality_columns,
std::vector<cudf::size_type> conditional_columns,
cudf::ast::operation predicate,
std::vector<cudf::size_type> expected_outputs)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers,
right_wrappers,
left_columns,
right_columns,
left_equality,
right_equality,
left_conditional,
right_conditional] =
this->parse_input(left_data, right_data, equality_columns, conditional_columns);
this->_test(left_equality,
right_equality,
left_conditional,
right_conditional,
predicate,
expected_outputs);
}
/*
* Perform a join of tables constructed from two input data sets according to
* the provided predicate and verify that the outputs match the expected
* outputs (up to order).
*/
void test_nulls(NullableColumnVector<T> left_data,
NullableColumnVector<T> right_data,
std::vector<cudf::size_type> equality_columns,
std::vector<cudf::size_type> conditional_columns,
cudf::ast::operation predicate,
std::vector<cudf::size_type> expected_outputs,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
// Note that we need to maintain the column wrappers otherwise the
// resulting column views will be referencing potentially invalid memory.
auto [left_wrappers,
right_wrappers,
left_columns,
right_columns,
left_equality,
right_equality,
left_conditional,
right_conditional] =
this->parse_input(left_data, right_data, equality_columns, conditional_columns);
this->_test(left_equality,
right_equality,
left_conditional,
right_conditional,
predicate,
expected_outputs,
compare_nulls);
}
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* mixed join API.
*/
virtual SingleJoinReturn join(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) = 0;
/**
* This method must be implemented by subclasses for specific types of joins.
* It should be a simply forwarding of arguments to the appropriate cudf
* mixed join size computation API.
*/
virtual std::pair<std::size_t, std::unique_ptr<rmm::device_uvector<cudf::size_type>>> join_size(
cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) = 0;
};
/**
* Tests of mixed left semi joins.
*/
template <typename T>
struct MixedLeftSemiJoinTest : public MixedJoinSingleReturnTest<T> {
SingleJoinReturn join(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_left_semi_join(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
std::pair<std::size_t, std::unique_ptr<rmm::device_uvector<cudf::size_type>>> join_size(
cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_left_semi_join_size(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
};
TYPED_TEST_SUITE(MixedLeftSemiJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(MixedLeftSemiJoinTest, BasicEquality)
{
this->test({{0, 1, 2}, {3, 4, 5}, {10, 20, 30}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{1});
}
TYPED_TEST(MixedLeftSemiJoinTest, BasicEqualityDuplicates)
{
this->test({{0, 1, 2, 1}, {3, 4, 5, 6}, {10, 20, 30, 40}},
{{0, 1, 3, 1}, {5, 4, 5, 6}, {30, 40, 50, 40}},
{0},
{1, 2},
left_zero_eq_right_zero,
{1, 3});
}
TYPED_TEST(MixedLeftSemiJoinTest, BasicNullEqualityEqual)
{
this->test_nulls({{{0, 1, 2}, {1, 1, 0}}, {{3, 4, 5}, {1, 1, 1}}, {{10, 20, 30}, {1, 1, 1}}},
{{{0, 1, 3}, {1, 1, 0}}, {{5, 4, 5}, {1, 1, 1}}, {{30, 40, 30}, {1, 1, 1}}},
{0},
{1, 2},
left_zero_eq_right_zero,
{1, 2},
cudf::null_equality::EQUAL);
};
TYPED_TEST(MixedLeftSemiJoinTest, BasicNullEqualityUnequal)
{
this->test_nulls({{{0, 1, 2}, {1, 1, 0}}, {{3, 4, 5}, {1, 1, 1}}, {{10, 20, 30}, {1, 1, 1}}},
{{{0, 1, 3}, {1, 1, 0}}, {{5, 4, 5}, {1, 1, 1}}, {{30, 40, 30}, {1, 1, 1}}},
{0},
{1, 2},
left_zero_eq_right_zero,
{1},
cudf::null_equality::UNEQUAL);
};
TYPED_TEST(MixedLeftSemiJoinTest, AsymmetricEquality)
{
this->test({{0, 2, 1}, {3, 5, 4}, {10, 30, 20}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{2});
}
TYPED_TEST(MixedLeftSemiJoinTest, AsymmetricLeftLargerEquality)
{
this->test({{0, 2, 1, 4}, {3, 5, 4, 10}, {10, 30, 20, 100}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{2});
}
/**
* Tests of mixed left semi joins.
*/
template <typename T>
struct MixedLeftAntiJoinTest : public MixedJoinSingleReturnTest<T> {
SingleJoinReturn join(cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_left_anti_join(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
std::pair<std::size_t, std::unique_ptr<rmm::device_uvector<cudf::size_type>>> join_size(
cudf::table_view left_equality,
cudf::table_view right_equality,
cudf::table_view left_conditional,
cudf::table_view right_conditional,
cudf::ast::operation predicate,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL) override
{
return cudf::mixed_left_anti_join_size(
left_equality, right_equality, left_conditional, right_conditional, predicate, compare_nulls);
}
};
TYPED_TEST_SUITE(MixedLeftAntiJoinTest, cudf::test::IntegralTypesNotBool);
TYPED_TEST(MixedLeftAntiJoinTest, BasicEquality)
{
this->test({{0, 1, 2}, {3, 4, 5}, {10, 20, 30}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 2});
}
TYPED_TEST(MixedLeftAntiJoinTest, BasicNullEqualityEqual)
{
this->test_nulls({{{0, 1, 2}, {1, 1, 0}}, {{3, 4, 5}, {1, 1, 1}}, {{10, 20, 30}, {1, 1, 1}}},
{{{0, 1, 3}, {1, 1, 0}}, {{5, 4, 5}, {1, 1, 1}}, {{30, 40, 30}, {1, 1, 1}}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0},
cudf::null_equality::EQUAL);
};
TYPED_TEST(MixedLeftAntiJoinTest, BasicNullEqualityUnequal)
{
this->test_nulls({{{0, 1, 2}, {1, 1, 0}}, {{3, 4, 5}, {1, 1, 1}}, {{10, 20, 30}, {1, 1, 1}}},
{{{0, 1, 3}, {1, 1, 0}}, {{5, 4, 5}, {1, 1, 1}}, {{30, 40, 30}, {1, 1, 1}}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 2},
cudf::null_equality::UNEQUAL);
};
TYPED_TEST(MixedLeftAntiJoinTest, AsymmetricEquality)
{
this->test({{0, 2, 1}, {3, 5, 4}, {10, 30, 20}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 1});
}
TYPED_TEST(MixedLeftAntiJoinTest, AsymmetricLeftLargerEquality)
{
this->test({{0, 2, 1, 4}, {3, 5, 4, 10}, {10, 30, 20, 100}},
{{0, 1, 3}, {5, 4, 5}, {30, 40, 50}},
{0},
{1, 2},
left_zero_eq_right_zero,
{0, 1, 3});
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/join/semi_anti_join_tests.cpp
|
/*
* Copyright (c) 2021-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/column/column.hpp>
#include <cudf/column/column_view.hpp>
#include <cudf/dictionary/encode.hpp>
#include <cudf/join.hpp>
#include <cudf/sorting.hpp>
#include <cudf/table/table.hpp>
#include <cudf/table/table_view.hpp>
#include <cudf/types.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/column_utilities.hpp>
#include <cudf_test/column_wrapper.hpp>
#include <cudf_test/iterator_utilities.hpp>
#include <cudf_test/table_utilities.hpp>
#include <thrust/iterator/transform_iterator.h>
template <typename T>
using column_wrapper = cudf::test::fixed_width_column_wrapper<T>;
using strcol_wrapper = cudf::test::strings_column_wrapper;
using column_vector = std::vector<std::unique_ptr<cudf::column>>;
using Table = cudf::table;
struct JoinTest : public cudf::test::BaseFixture {};
namespace {
// This function is a wrapper around cudf's join APIs that takes the gather map
// from join APIs and materializes the table that would be created by gathering
// from the joined tables. Join APIs originally returned tables like this, but
// they were modified in https://github.com/rapidsai/cudf/pull/7454. This
// helper function allows us to avoid rewriting all our tests in terms of
// gather maps.
template <std::unique_ptr<rmm::device_uvector<cudf::size_type>> (*join_impl)(
cudf::table_view const& left_keys,
cudf::table_view const& right_keys,
cudf::null_equality compare_nulls,
rmm::mr::device_memory_resource* mr)>
std::unique_ptr<cudf::table> join_and_gather(
cudf::table_view const& left_input,
cudf::table_view const& right_input,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
cudf::null_equality compare_nulls,
rmm::mr::device_memory_resource* mr = rmm::mr::get_current_device_resource())
{
auto left_selected = left_input.select(left_on);
auto right_selected = right_input.select(right_on);
auto const join_indices = join_impl(left_selected, right_selected, compare_nulls, mr);
auto left_indices_span = cudf::device_span<cudf::size_type const>{*join_indices};
auto left_indices_col = cudf::column_view{left_indices_span};
return cudf::gather(left_input, left_indices_col);
}
} // namespace
std::unique_ptr<cudf::table> left_semi_join(
cudf::table_view const& left_input,
cudf::table_view const& right_input,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
return join_and_gather<cudf::left_semi_join>(
left_input, right_input, left_on, right_on, compare_nulls);
}
std::unique_ptr<cudf::table> left_anti_join(
cudf::table_view const& left_input,
cudf::table_view const& right_input,
std::vector<cudf::size_type> const& left_on,
std::vector<cudf::size_type> const& right_on,
cudf::null_equality compare_nulls = cudf::null_equality::EQUAL)
{
return join_and_gather<cudf::left_anti_join>(
left_input, right_input, left_on, right_on, compare_nulls);
}
TEST_F(JoinTest, TestSimple)
{
column_wrapper<int32_t> left_col0{0, 1, 2};
column_wrapper<int32_t> right_col0{0, 1, 3};
auto left = cudf::table_view{{left_col0}};
auto right = cudf::table_view{{right_col0}};
auto result = left_semi_join(left, right);
auto result_cv = cudf::column_view(cudf::data_type{cudf::type_to_id<cudf::size_type>()},
result->size(),
result->data(),
nullptr,
0);
column_wrapper<cudf::size_type> expected{0, 1};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result_cv);
}
std::pair<std::unique_ptr<cudf::table>, std::unique_ptr<cudf::table>> get_saj_tables(
std::vector<bool> const& left_is_human_nulls, std::vector<bool> const& right_is_human_nulls)
{
column_wrapper<int32_t> col0_0{{99, 1, 2, 0, 2}, {0, 1, 1, 1, 1}};
strcol_wrapper col0_1({"s1", "s1", "s0", "s4", "s0"}, {1, 1, 0, 1, 1});
column_wrapper<int32_t> col0_2{{0, 1, 2, 4, 1}};
auto col0_names_col = strcol_wrapper{
"Samuel Vimes", "Carrot Ironfoundersson", "Detritus", "Samuel Vimes", "Angua von Überwald"};
auto col0_ages_col = column_wrapper<int32_t>{{48, 27, 351, 31, 25}};
auto col0_is_human_col =
column_wrapper<bool>{{true, true, false, false, false}, left_is_human_nulls.begin()};
auto col0_3 = cudf::test::structs_column_wrapper{
{col0_names_col, col0_ages_col, col0_is_human_col}, {1, 1, 1, 1, 1}};
column_wrapper<int32_t> col1_0{{2, 2, 0, 4, -99}, {1, 1, 1, 1, 0}};
strcol_wrapper col1_1({"s1", "s0", "s1", "s2", "s1"});
column_wrapper<int32_t> col1_2{{1, 0, 1, 2, 1}, {1, 0, 1, 1, 1}};
auto col1_names_col = strcol_wrapper{"Carrot Ironfoundersson",
"Angua von Überwald",
"Detritus",
"Carrot Ironfoundersson",
"Samuel Vimes"};
auto col1_ages_col = column_wrapper<int32_t>{{351, 25, 27, 31, 48}};
auto col1_is_human_col =
column_wrapper<bool>{{true, false, false, false, true}, right_is_human_nulls.begin()};
auto col1_3 =
cudf::test::structs_column_wrapper{{col1_names_col, col1_ages_col, col1_is_human_col}};
column_vector cols0, cols1;
cols0.push_back(col0_0.release());
cols0.push_back(col0_1.release());
cols0.push_back(col0_2.release());
cols0.push_back(col0_3.release());
cols1.push_back(col1_0.release());
cols1.push_back(col1_1.release());
cols1.push_back(col1_2.release());
cols1.push_back(col1_3.release());
return {std::make_unique<Table>(std::move(cols0)), std::make_unique<Table>(std::move(cols1))};
}
TEST_F(JoinTest, SemiJoinWithStructsAndNulls)
{
auto tables = get_saj_tables({1, 1, 0, 1, 0}, {1, 0, 0, 1, 1});
auto result =
left_semi_join(*tables.first, *tables.second, {0, 1, 3}, {0, 1, 3}, cudf::null_equality::EQUAL);
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{99, 2}, {0, 1}};
strcol_wrapper col_gold_1({"s1", "s0"}, {1, 1});
column_wrapper<int32_t> col_gold_2{{0, 1}};
auto col_gold_3_names_col = strcol_wrapper{"Samuel Vimes", "Angua von Überwald"};
auto col_gold_3_ages_col = column_wrapper<int32_t>{{48, 25}};
auto col_gold_3_is_human_col = column_wrapper<bool>{{true, false}, {1, 0}};
auto col_gold_3 = cudf::test::structs_column_wrapper{
{col_gold_3_names_col, col_gold_3_ages_col, col_gold_3_is_human_col}};
column_vector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, SemiJoinWithStructsAndNullsNotEqual)
{
auto tables = get_saj_tables({1, 1, 0, 1, 1}, {1, 1, 0, 1, 1});
auto result = left_semi_join(
*tables.first, *tables.second, {0, 1, 3}, {0, 1, 3}, cudf::null_equality::UNEQUAL);
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{2}, {1}};
strcol_wrapper col_gold_1({"s0"}, {1});
column_wrapper<int32_t> col_gold_2{{1}};
auto col_gold_3_names_col = strcol_wrapper{"Angua von Überwald"};
auto col_gold_3_ages_col = column_wrapper<int32_t>{{25}};
auto col_gold_3_is_human_col = column_wrapper<bool>{{false}, {1}};
auto col_gold_3 = cudf::test::structs_column_wrapper{
{col_gold_3_names_col, col_gold_3_ages_col, col_gold_3_is_human_col}};
column_vector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, AntiJoinWithStructsAndNulls)
{
auto tables = get_saj_tables({1, 1, 0, 1, 0}, {1, 0, 0, 1, 1});
auto result =
left_anti_join(*tables.first, *tables.second, {0, 1, 3}, {0, 1, 3}, cudf::null_equality::EQUAL);
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{1, 2, 0}, {1, 1, 1}};
strcol_wrapper col_gold_1({"s1", "s0", "s4"}, {1, 0, 1});
column_wrapper<int32_t> col_gold_2{{1, 2, 4}};
auto col_gold_3_names_col = strcol_wrapper{"Carrot Ironfoundersson", "Detritus", "Samuel Vimes"};
auto col_gold_3_ages_col = column_wrapper<int32_t>{{27, 351, 31}};
auto col_gold_3_is_human_col = column_wrapper<bool>{{true, false, false}, {1, 0, 1}};
auto col_gold_3 = cudf::test::structs_column_wrapper{
{col_gold_3_names_col, col_gold_3_ages_col, col_gold_3_is_human_col}};
column_vector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, AntiJoinWithStructsAndNullsNotEqual)
{
auto tables = get_saj_tables({1, 1, 0, 1, 1}, {1, 1, 0, 1, 1});
auto result = left_anti_join(
*tables.first, *tables.second, {0, 1, 3}, {0, 1, 3}, cudf::null_equality::UNEQUAL);
auto result_sort_order = cudf::sorted_order(result->view());
auto sorted_result = cudf::gather(result->view(), *result_sort_order);
column_wrapper<int32_t> col_gold_0{{99, 1, 2, 0}, {0, 1, 1, 1}};
strcol_wrapper col_gold_1({"s1", "s1", "s0", "s4"}, {1, 1, 0, 1});
column_wrapper<int32_t> col_gold_2{{0, 1, 2, 4}};
auto col_gold_3_names_col =
strcol_wrapper{"Samuel Vimes", "Carrot Ironfoundersson", "Detritus", "Samuel Vimes"};
auto col_gold_3_ages_col = column_wrapper<int32_t>{{48, 27, 351, 31}};
auto col_gold_3_is_human_col = column_wrapper<bool>{{true, true, false, false}, {1, 1, 0, 1}};
auto col_gold_3 = cudf::test::structs_column_wrapper{
{col_gold_3_names_col, col_gold_3_ages_col, col_gold_3_is_human_col}};
column_vector cols_gold;
cols_gold.push_back(col_gold_0.release());
cols_gold.push_back(col_gold_1.release());
cols_gold.push_back(col_gold_2.release());
cols_gold.push_back(col_gold_3.release());
Table gold(std::move(cols_gold));
auto gold_sort_order = cudf::sorted_order(gold.view());
auto sorted_gold = cudf::gather(gold.view(), *gold_sort_order);
CUDF_TEST_EXPECT_TABLES_EQUIVALENT(*sorted_gold, *sorted_result);
}
TEST_F(JoinTest, AntiJoinWithStructsAndNullsOnOneSide)
{
auto constexpr null{0};
auto left_col0 = [] {
column_wrapper<int32_t> child1{{1, null}, cudf::test::iterators::null_at(1)};
column_wrapper<int32_t> child2{11, 12};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto right_col0 = [] {
column_wrapper<int32_t> child1{1, 2, 3, 4};
column_wrapper<int32_t> child2{11, 12, 13, 14};
return cudf::test::structs_column_wrapper{{child1, child2}};
}();
auto left = cudf::table_view{{left_col0}};
auto right = cudf::table_view{{right_col0}};
auto result = cudf::left_anti_join(left, right);
auto result_span = cudf::device_span<cudf::size_type const>{*result};
auto result_col = cudf::column_view{result_span};
auto expected = column_wrapper<cudf::size_type>{1};
CUDF_TEST_EXPECT_COLUMNS_EQUAL(expected, result_col);
}
| 0 |
rapidsai_public_repos/cudf/cpp/tests
|
rapidsai_public_repos/cudf/cpp/tests/scalar/scalar_device_view_test.cu
|
/*
* Copyright (c) 2019-2023, NVIDIA CORPORATION.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cudf/detail/utilities/vector_factories.hpp>
#include <cudf/scalar/scalar.hpp>
#include <cudf/scalar/scalar_device_view.cuh>
#include <cudf/strings/string_view.cuh>
#include <cudf/types.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <cudf/utilities/type_dispatcher.hpp>
#include <cudf_test/base_fixture.hpp>
#include <cudf_test/cudf_gtest.hpp>
#include <cudf_test/type_list_utilities.hpp>
#include <cudf_test/type_lists.hpp>
#include <random>
#include <thrust/sequence.h>
template <typename T>
struct TypedScalarDeviceViewTest : public cudf::test::BaseFixture {};
TYPED_TEST_SUITE(TypedScalarDeviceViewTest, cudf::test::FixedWidthTypesWithoutFixedPoint);
template <typename ScalarDeviceViewType>
__global__ void test_set_value(ScalarDeviceViewType s, ScalarDeviceViewType s1)
{
s1.set_value(s.value());
s1.set_valid(true);
}
template <typename ScalarDeviceViewType>
__global__ void test_value(ScalarDeviceViewType s, ScalarDeviceViewType s1, bool* result)
{
*result = (s.value() == s1.value());
}
TYPED_TEST(TypedScalarDeviceViewTest, Value)
{
TypeParam value = cudf::test::make_type_param_scalar<TypeParam>(7);
TypeParam value1 = cudf::test::make_type_param_scalar<TypeParam>(11);
cudf::scalar_type_t<TypeParam> s(value);
cudf::scalar_type_t<TypeParam> s1{value1};
auto scalar_device_view = cudf::get_scalar_device_view(s);
auto scalar_device_view1 = cudf::get_scalar_device_view(s1);
rmm::device_scalar<bool> result{cudf::get_default_stream()};
test_set_value<<<1, 1, 0, cudf::get_default_stream().value()>>>(scalar_device_view,
scalar_device_view1);
CUDF_CHECK_CUDA(0);
EXPECT_EQ(s1.value(), value);
EXPECT_TRUE(s1.is_valid());
test_value<<<1, 1, 0, cudf::get_default_stream().value()>>>(
scalar_device_view, scalar_device_view1, result.data());
CUDF_CHECK_CUDA(0);
EXPECT_TRUE(result.value(cudf::get_default_stream()));
}
template <typename ScalarDeviceViewType>
__global__ void test_null(ScalarDeviceViewType s, bool* result)
{
*result = s.is_valid();
}
TYPED_TEST(TypedScalarDeviceViewTest, ConstructNull)
{
TypeParam value = cudf::test::make_type_param_scalar<TypeParam>(5);
cudf::scalar_type_t<TypeParam> s(value, false);
auto scalar_device_view = cudf::get_scalar_device_view(s);
rmm::device_scalar<bool> result{cudf::get_default_stream()};
test_null<<<1, 1, 0, cudf::get_default_stream().value()>>>(scalar_device_view, result.data());
CUDF_CHECK_CUDA(0);
EXPECT_FALSE(result.value(cudf::get_default_stream()));
}
template <typename ScalarDeviceViewType>
__global__ void test_setnull(ScalarDeviceViewType s)
{
s.set_valid(false);
}
TYPED_TEST(TypedScalarDeviceViewTest, SetNull)
{
TypeParam value = cudf::test::make_type_param_scalar<TypeParam>(5);
cudf::scalar_type_t<TypeParam> s{value};
auto scalar_device_view = cudf::get_scalar_device_view(s);
s.set_valid_async(true);
EXPECT_TRUE(s.is_valid());
test_setnull<<<1, 1, 0, cudf::get_default_stream().value()>>>(scalar_device_view);
CUDF_CHECK_CUDA(0);
EXPECT_FALSE(s.is_valid());
}
struct StringScalarDeviceViewTest : public cudf::test::BaseFixture {};
__global__ void test_string_value(cudf::string_scalar_device_view s,
char const* value,
cudf::size_type size,
bool* result)
{
*result = (s.value() == cudf::string_view(value, size));
}
TEST_F(StringScalarDeviceViewTest, Value)
{
std::string value("test string");
cudf::string_scalar s(value);
auto scalar_device_view = cudf::get_scalar_device_view(s);
rmm::device_scalar<bool> result{cudf::get_default_stream()};
auto value_v = cudf::detail::make_device_uvector_sync(
value, cudf::get_default_stream(), rmm::mr::get_current_device_resource());
test_string_value<<<1, 1, 0, cudf::get_default_stream().value()>>>(
scalar_device_view, value_v.data(), value.size(), result.data());
CUDF_CHECK_CUDA(0);
EXPECT_TRUE(result.value(cudf::get_default_stream()));
}
| 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.