-
Notifications
You must be signed in to change notification settings - Fork 915
/
sum_tests.cpp
231 lines (180 loc) · 9.04 KB
/
sum_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* 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);
}
}