-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathGroupTest.cpp
253 lines (232 loc) · 13.1 KB
/
GroupTest.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
/*
* Copyright 2021 The DAPHNE Consortium
*
* 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 <ir/daphneir/Daphne.h>
#include <runtime/local/datagen/GenGivenVals.h>
#include <runtime/local/datastructures/DataObjectFactory.h>
#include <runtime/local/datastructures/DenseMatrix.h>
#include <runtime/local/datastructures/Frame.h>
#include <runtime/local/kernels/CheckEq.h>
#include <runtime/local/kernels/Group.h>
#include <runtime/local/kernels/Order.h>
#include <catch.hpp>
#include <tags.h>
#include <vector>
TEMPLATE_TEST_CASE("Group", TAG_KERNELS, (Frame)) {
using VT0 = double;
using VT1 = float;
using VT2 = int64_t;
using VT3 = uint32_t;
size_t numRows = 20;
auto c0 = genGivenVals<DenseMatrix<VT0>>(
numRows, {1.5, 2.7, 3.2, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5});
auto c1 = genGivenVals<DenseMatrix<VT1>>(
numRows, {1.6, 2.7, 3.2, 1.5, 2.7, 1.5, 2.7, 1.6, 2.8, 1.5, 2.7, 1.6, 2.7, 1.5, 2.8, 1.5, 2.7, 1.6, 2.8, 1.5});
auto c2 =
genGivenVals<DenseMatrix<VT2>>(numRows, {-1, 0, 1, -1, 0, 1, -1, 0, 1, 3, 2, -1, 0, 1, 1, 1, 1, -1, -1, -1});
auto c3 = genGivenVals<DenseMatrix<VT3>>(numRows, {1, 0, 1, 1, 0, 1, 1, 0, 1, 3, 2, 1, 0, 1, 1, 1, 1, 1, 1, 1});
;
auto c4 = genGivenVals<DenseMatrix<VT1>>(
numRows, {1.5, 2.7, 3.2, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5, 2.7, 1.5});
auto c5 = genGivenVals<DenseMatrix<VT1>>(
numRows, {1.6, 2.7, 3.2, 1.5, 2.7, 1.5, 2.7, 1.6, 2.8, 1.5, 2.7, 1.6, 2.7, 1.5, 2.8, 1.5, 2.7, 1.6, 2.8, 1.5});
auto c6 =
genGivenVals<DenseMatrix<VT2>>(numRows, {-1, 0, 1, -37, 17, 1, -1, 0, 1, 3, 2, -1, 0, 1, 1, 1, 1, -1, -1, -1});
auto c7 = genGivenVals<DenseMatrix<VT2>>(numRows, {1, 0, 1, 1, 0, 1, 1, 0, 1, 3, 2, 1, 0, 1, 1, 1, 1, 1, 1, 1});
auto c8 = genGivenVals<DenseMatrix<VT3>>(numRows, {1, 2, 3, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1});
auto c9 = genGivenVals<DenseMatrix<VT1>>(
numRows, {1.6, 2.7, 3.2, 1.5, 2.7, 1.5, 2.7, 1.6, 2.8, 1.5, 2.7, 1.6, 2.7, 1.5, 2.8, 1.5, 2.7, 1.6, 2.8, 1.5});
auto c10 = genGivenVals<DenseMatrix<VT0>>(numRows, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
auto c11 =
genGivenVals<DenseMatrix<VT0>>(numRows, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19});
std::vector<Structure *> colsArg{c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11};
std::string labels[] = {"aaa", "bbb", "ccc", "ddd", "eee", "fff", "ggg", "hhh", "iii", "jjj", "kkk", "lll"};
auto arg = DataObjectFactory::create<Frame>(colsArg, labels);
DataObjectFactory::destroy(c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11);
Frame *exp{};
Frame *res{};
size_t numKeyCols;
size_t numAggCols;
const char **keyCols = nullptr;
const char **aggCols = nullptr;
;
mlir::daphne::GroupEnum *aggFuncs = nullptr;
std::vector<Structure *> colsExp;
auto context = new mlir::MLIRContext();
SECTION("1 grouping column, 1 aggregation column") {
numKeyCols = 1;
numAggCols = 1;
keyCols = new const char *[10]{labels[0].c_str()};
aggCols = new const char *[10]{labels[2].c_str()};
aggFuncs = new mlir::daphne::GroupEnum[numAggCols];
aggFuncs[0] = mlir::daphne::GroupEnum::COUNT;
numRows = 3;
DenseMatrix<VT0> *c0Exp = genGivenVals<DenseMatrix<VT0>>(numRows, {1.5, 2.7, 3.2});
DenseMatrix<uint64_t> *c1Exp = genGivenVals<DenseMatrix<uint64_t>>(numRows, {10, 9, 1});
std::vector<Structure *> colsExp{c0Exp, c1Exp};
std::string labelsExp[] = {"aaa", "COUNT(ccc)"};
exp = DataObjectFactory::create<Frame>(colsExp, labelsExp);
DataObjectFactory::destroy(c0Exp, c1Exp);
}
SECTION("1 grouping column with one distinct value, 1 aggregation columns") {
numKeyCols = 1;
numAggCols = 1;
keyCols = new const char *[10]{labels[10].c_str()};
aggCols = new const char *[10]{labels[0].c_str()};
aggFuncs = new mlir::daphne::GroupEnum[numAggCols];
aggFuncs[0] = mlir::daphne::GroupEnum::COUNT;
numRows = 1;
DenseMatrix<VT0> *c0Exp = genGivenVals<DenseMatrix<VT0>>(numRows, {1});
DenseMatrix<uint64_t> *c1Exp = genGivenVals<DenseMatrix<uint64_t>>(numRows, {20});
std::vector<Structure *> colsExp{c0Exp, c1Exp};
std::string labelsExp[] = {"kkk", "COUNT(aaa)"};
exp = DataObjectFactory::create<Frame>(colsExp, labelsExp);
DataObjectFactory::destroy(c0Exp, c1Exp);
}
SECTION("1 grouping column with all distinct values, 1 aggregation columns") {
numKeyCols = 1;
numAggCols = 1;
keyCols = new const char *[10]{labels[11].c_str()};
aggCols = new const char *[10]{labels[0].c_str()};
aggFuncs = new mlir::daphne::GroupEnum[numAggCols];
aggFuncs[0] = mlir::daphne::GroupEnum::COUNT;
numRows = 20;
DenseMatrix<VT0> *c0Exp = genGivenVals<DenseMatrix<VT0>>(
numRows, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19});
DenseMatrix<uint64_t> *c1Exp =
genGivenVals<DenseMatrix<uint64_t>>(numRows, {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
std::vector<Structure *> colsExp{c0Exp, c1Exp};
std::string labelsExp[] = {"lll", "COUNT(aaa)"};
exp = DataObjectFactory::create<Frame>(colsExp, labelsExp);
DataObjectFactory::destroy(c0Exp, c1Exp);
}
SECTION("1 grouping column, 3 aggregation columns") {
numKeyCols = 1;
numAggCols = 3;
keyCols = new const char *[10]{labels[7].c_str()};
aggCols = new const char *[10]{labels[0].c_str(), labels[3].c_str(), labels[2].c_str()};
aggFuncs = new mlir::daphne::GroupEnum[numAggCols];
aggFuncs[0] = mlir::daphne::GroupEnum::SUM;
aggFuncs[1] = mlir::daphne::GroupEnum::MIN;
aggFuncs[2] = mlir::daphne::GroupEnum::MAX;
numRows = 4;
DenseMatrix<VT2> *c0Exp = genGivenVals<DenseMatrix<VT2>>(numRows, {0, 1, 2, 3});
DenseMatrix<VT0> *c1Exp =
genGivenVals<DenseMatrix<VT0>>(numRows, {3 * 2.7 + 1.5, 8 * 1.5 + 5 * 2.7 + 3.2, 2.7, 1.5});
DenseMatrix<VT3> *c2Exp = genGivenVals<DenseMatrix<VT3>>(numRows, {0, 1, 2, 3});
DenseMatrix<VT2> *c3Exp = genGivenVals<DenseMatrix<VT2>>(numRows, {0, 1, 2, 3});
std::vector<Structure *> colsExp{c0Exp, c1Exp, c2Exp, c3Exp};
std::string labelsExp[] = {"hhh", "SUM(aaa)", "MIN(ddd)", "MAX(ccc)"};
exp = DataObjectFactory::create<Frame>(colsExp, labelsExp);
DataObjectFactory::destroy(c0Exp, c1Exp, c2Exp, c3Exp);
}
SECTION("3 grouping columns, 1 aggregation column") {
numKeyCols = 3;
numAggCols = 1;
keyCols = new const char *[10]{labels[2].c_str(), labels[1].c_str(), labels[0].c_str()};
aggCols = new const char *[10]{labels[6].c_str()};
aggFuncs = new mlir::daphne::GroupEnum[numAggCols];
aggFuncs[0] = mlir::daphne::GroupEnum::AVG;
numRows = 12;
DenseMatrix<VT2> *c0Exp = genGivenVals<DenseMatrix<VT2>>(numRows, {-1, -1, -1, -1, 0, 0, 1, 1, 1, 1, 2, 3});
DenseMatrix<VT1> *c1Exp =
genGivenVals<DenseMatrix<VT1>>(numRows, {1.5, 1.6, 2.7, 2.8, 1.6, 2.7, 1.5, 2.7, 2.8, 3.2, 2.7, 1.5});
DenseMatrix<VT0> *c2Exp =
genGivenVals<DenseMatrix<VT0>>(numRows, {1.5, 1.5, 2.7, 2.7, 1.5, 2.7, 1.5, 2.7, 2.7, 3.2, 2.7, 1.5});
DenseMatrix<double> *c3Exp =
genGivenVals<DenseMatrix<double>>(numRows, {-19, -1, -1, -1, 0, 17.0 / 3.0, 1, 1, 1, 1, 2, 3});
std::vector<Structure *> colsExp{c0Exp, c1Exp, c2Exp, c3Exp};
std::string labelsExp[] = {"ccc", "bbb", "aaa", "AVG(ggg)"};
exp = DataObjectFactory::create<Frame>(colsExp, labelsExp);
DataObjectFactory::destroy(c0Exp, c1Exp, c2Exp, c3Exp);
}
SECTION("5 grouping columns, 5 aggregation columns") {
numKeyCols = 5;
numAggCols = 5;
keyCols = new const char *[10]{labels[0].c_str(), labels[6].c_str(), labels[2].c_str(), labels[8].c_str(),
labels[4].c_str()};
aggCols = new const char *[10]{labels[5].c_str(), labels[1].c_str(), labels[7].c_str(), labels[3].c_str(),
labels[9].c_str()};
aggFuncs = new mlir::daphne::GroupEnum[numAggCols];
aggFuncs[0] = mlir::daphne::GroupEnum::COUNT;
aggFuncs[1] = mlir::daphne::GroupEnum::SUM;
aggFuncs[2] = mlir::daphne::GroupEnum::MIN;
aggFuncs[3] = mlir::daphne::GroupEnum::MAX;
aggFuncs[4] = mlir::daphne::GroupEnum::AVG;
numRows = 11;
DenseMatrix<VT0> *c0Exp =
genGivenVals<DenseMatrix<VT0>>(numRows, {1.5, 1.5, 1.5, 1.5, 1.5, 2.7, 2.7, 2.7, 2.7, 2.7, 3.2});
DenseMatrix<VT2> *c1Exp = genGivenVals<DenseMatrix<VT2>>(numRows, {-37, -1, 0, 1, 3, -1, 0, 1, 2, 17, 1});
DenseMatrix<VT2> *c2Exp = genGivenVals<DenseMatrix<VT2>>(numRows, {-1, -1, 0, 1, 3, -1, 0, 1, 2, 0, 1});
DenseMatrix<VT3> *c3Exp = genGivenVals<DenseMatrix<VT3>>(numRows, {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3});
DenseMatrix<VT1> *c4Exp =
genGivenVals<DenseMatrix<VT1>>(numRows, {1.5, 1.5, 1.5, 1.5, 1.5, 2.7, 2.7, 2.7, 2.7, 2.7, 3.2});
DenseMatrix<uint64_t> *c5Exp = genGivenVals<DenseMatrix<uint64_t>>(numRows, {1, 4, 1, 3, 1, 2, 2, 3, 1, 1, 1});
DenseMatrix<VT1> *c6Exp =
genGivenVals<DenseMatrix<VT1>>(numRows, {1.5f, 3 * 1.6f + 1.5f, 1.6f, 3 * 1.5f, 1.5f, 2.7f + 2.8f, 2 * 2.7f,
2 * 2.8f + 2.7f, 2.7f, 2.7f, 3.2f});
DenseMatrix<VT2> *c7Exp = genGivenVals<DenseMatrix<VT2>>(numRows, {1, 1, 0, 1, 3, 1, 0, 1, 2, 0, 1});
DenseMatrix<VT3> *c8Exp = genGivenVals<DenseMatrix<VT3>>(numRows, {1, 1, 0, 1, 3, 1, 0, 1, 2, 0, 1});
DenseMatrix<VT0> *c9Exp = genGivenVals<DenseMatrix<VT0>>(
numRows,
{(double)1.5f, (((double)3 * 1.6f) + ((double)1.5f)) / 4.0, (double)1.6f, ((double)(3 * 1.5f)) / 3.0,
(double)1.5f, ((double)(2.7f + 2.8f)) / 2.0, ((double)(2 * 2.7f)) / 2.0,
(((double)2 * 2.8f) + ((double)2.7f)) / 3.0, (double)2.7f, (double)2.7f, ((double)3.2f)});
std::vector<Structure *> colsExp{c0Exp, c1Exp, c2Exp, c3Exp, c4Exp, c5Exp, c6Exp, c7Exp, c8Exp, c9Exp};
std::string labelsExp[] = {"aaa", "ggg", "ccc", "iii", "eee",
"COUNT(fff)", "SUM(bbb)", "MIN(hhh)", "MAX(ddd)", "AVG(jjj)"};
exp = DataObjectFactory::create<Frame>(colsExp, labelsExp);
DataObjectFactory::destroy(c0Exp, c1Exp, c2Exp, c3Exp, c4Exp, c5Exp, c6Exp, c7Exp, c8Exp, c9Exp);
}
SECTION("0 grouping columns, 2 identical aggregation columns") {
numKeyCols = 0;
numAggCols = 2;
keyCols = new const char *[10]{};
aggCols = new const char *[10]{labels[2].c_str(), labels[2].c_str()};
aggFuncs = new mlir::daphne::GroupEnum[numAggCols];
aggFuncs[0] = mlir::daphne::GroupEnum::COUNT;
aggFuncs[1] = mlir::daphne::GroupEnum::SUM;
numRows = 1;
DenseMatrix<uint64_t> *c0Exp = genGivenVals<DenseMatrix<uint64_t>>(numRows, {20});
DenseMatrix<VT2> *c1Exp = genGivenVals<DenseMatrix<VT2>>(numRows, {5});
std::vector<Structure *> colsExp{c0Exp, c1Exp};
std::string labelsExp[] = {"COUNT(ccc)", "SUM(ccc)"};
exp = DataObjectFactory::create<Frame>(colsExp, labelsExp);
DataObjectFactory::destroy(c0Exp, c1Exp);
}
SECTION("3 grouping column, 0 aggregation columns") {
numKeyCols = 3;
numAggCols = 0;
keyCols = new const char *[10]{labels[0].c_str(), labels[2].c_str(), labels[3].c_str()};
aggCols = new const char *[10]{};
aggFuncs = nullptr;
numRows = 9;
DenseMatrix<VT0> *c0Exp =
genGivenVals<DenseMatrix<VT0>>(numRows, {1.5, 1.5, 1.5, 1.5, 2.7, 2.7, 2.7, 2.7, 3.2});
DenseMatrix<VT2> *c1Exp = genGivenVals<DenseMatrix<VT2>>(numRows, {-1, 0, 1, 3, -1, 0, 1, 2, 1});
DenseMatrix<VT3> *c2Exp = genGivenVals<DenseMatrix<VT3>>(numRows, {1, 0, 1, 3, 1, 0, 1, 2, 1});
std::vector<Structure *> colsExp{c0Exp, c1Exp, c2Exp};
std::string labelsExp[] = {"aaa", "ccc", "ddd"};
exp = DataObjectFactory::create<Frame>(colsExp, labelsExp);
DataObjectFactory::destroy(c0Exp, c1Exp, c2Exp);
}
group(res, arg, keyCols, numKeyCols, aggCols, numAggCols, aggFuncs, numAggCols, nullptr);
CHECK(*res == *exp);
delete[] keyCols;
delete[] aggCols;
delete aggFuncs;
delete context;
DataObjectFactory::destroy(arg, exp, res);
}