-
Notifications
You must be signed in to change notification settings - Fork 916
/
structs_tests.cpp
334 lines (270 loc) · 13.8 KB
/
structs_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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* Copyright (c) 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 <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;
namespace cudf {
namespace test {
template <typename V>
struct groupby_structs_test : public cudf::test::BaseFixture {
};
TYPED_TEST_SUITE(groupby_structs_test, cudf::test::FixedWidthTypes);
using V = int32_t; // Type of Aggregation Column.
using M0 = int32_t; // Type of STRUCT's first (i.e. 0th) member.
using R = cudf::detail::target_type_t<V, aggregation::SUM>; // Type of aggregation result.
using offsets = std::vector<cudf::offset_type>;
using strings = strings_column_wrapper;
using structs = structs_column_wrapper;
template <typename T>
using fwcw = fixed_width_column_wrapper<T>;
template <typename T>
using lcw = 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.
auto sum_agg() { return cudf::make_sum_aggregation<groupby_aggregation>(); }
// Set this to true to enable printing, for debugging.
auto constexpr print_enabled = false;
void print_agg_results(column_view const& keys, column_view const& vals)
{
if constexpr (print_enabled) {
auto requests = std::vector<groupby::aggregation_request>{};
requests.push_back(groupby::aggregation_request{});
requests.back().values = vals;
requests.back().aggregations.push_back(sum_agg());
requests.back().aggregations.push_back(
cudf::make_nth_element_aggregation<groupby_aggregation>(0));
auto gby = groupby::groupby{table_view({keys}), null_policy::INCLUDE, sorted::NO, {}, {}};
auto result = gby.aggregate(requests);
std::cout << "Results: Keys: " << std::endl;
print(result.first->get_column(0).view());
std::cout << "Results: Values: " << std::endl;
print(result.second.front().results[0]->view());
}
}
void test_sort_based_sum_agg(column_view const& keys,
column_view const& values,
column_view const& expected_keys,
column_view const& expected_values)
{
test_single_agg(keys,
values,
expected_keys,
expected_values,
sum_agg(),
force_use_sort_impl::YES,
null_policy::INCLUDE);
}
void test_hash_based_sum_agg(column_view const& keys,
column_view const& values,
column_view const& expected_keys,
column_view const& expected_values)
{
test_single_agg(keys,
values,
expected_keys,
expected_values,
sum_agg(),
force_use_sort_impl::NO,
null_policy::INCLUDE);
}
void test_sum_agg(column_view const& keys,
column_view const& values,
column_view const& expected_keys,
column_view const& expected_values)
{
test_sort_based_sum_agg(keys, values, expected_keys, expected_values);
test_hash_based_sum_agg(keys, values, expected_keys, expected_values);
}
} // namespace
TYPED_TEST(groupby_structs_test, basic)
{
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// 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 = strings {"11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = structs{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 = strings {"11", "22", "33"};
auto expected_keys = structs{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 M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// 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 = strings { "11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = structs{{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 = strings { "11", "22", "33", "33", "22" };
auto expected_keys = structs{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 M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// 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 = strings {"11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = structs{{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 = strings { {"11", "22", "33", "null" }, null_at(3)};
auto expected_keys = structs{{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 M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// 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 = strings { "11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = structs{{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 = strings {{ "11", "22", "33", "33", "22", "null" }, null_at(5)};
auto expected_keys = structs{{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 M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// 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 = strings {{ "11", "null", "33", "11", "22", "22", "11", "33", "33", "22"}, null_at(1)};
auto keys = structs{{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 = strings { {"11", "22", "33", "null", "null" }, nulls_at({3,4})};
auto expected_keys = structs{{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 M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// 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 = strings {{ "11", "null", "33", "11", "22", "22", "11", "33", "33", "22"}, null_at(1)};
// clang-format on
auto struct_0 = structs{{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 = structs{{struct_0, struct_1_member_1}}; // Struct of structs.
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 = strings { {"11", "22", "33", "null", "null" }, nulls_at({3,4})};
auto expected_structs = structs{{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 = structs{{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 M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// clang-format off
auto values = fwcw<V> {};
auto member_0 = fwcw<M0>{};
auto member_1 = fwcw<M1>{};
auto member_2 = strings {};
auto keys = structs{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 = strings {};
auto expected_keys = structs{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 M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// 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 = strings {"11", "22", "33", "11", "22", "22", "11", "33", "33", "22"};
auto keys = structs{{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 = strings {"null"};
auto expected_keys = structs{{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_are_unsupported)
{
using M1 = TypeParam; // Type of STRUCT's second (i.e. 1th) member.
// 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 = structs{{member_0, member_1}};
EXPECT_THROW(test_sort_based_sum_agg(keys, values, keys, values), cudf::logic_error);
EXPECT_THROW(test_hash_based_sum_agg(keys, values, keys, values), cudf::logic_error);
}
} // namespace test
} // namespace cudf