-
Notifications
You must be signed in to change notification settings - Fork 449
/
Copy pathGroupByTest.cpp
458 lines (393 loc) · 18.2 KB
/
GroupByTest.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/*
* Copyright 2022 HEAVY.AI, Inc.
*
* 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 "TestHelpers.h"
#include <boost/filesystem.hpp>
#include <fstream>
#include "../QueryEngine/Execute.h"
#include "../QueryEngine/InputMetadata.h"
#include "../QueryRunner/QueryRunner.h"
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
extern bool g_is_test_env;
extern bool g_enable_watchdog;
extern size_t g_big_group_threshold;
extern size_t g_watchdog_baseline_max_groups;
using QR = QueryRunner::QueryRunner;
using namespace TestHelpers;
inline void run_ddl_statement(const std::string& input_str) {
QR::get()->runDDLStatement(input_str);
}
bool skip_tests(const ExecutorDeviceType device_type) {
#ifdef HAVE_CUDA
return device_type == ExecutorDeviceType::GPU && !(QR::get()->gpusPresent());
#else
return device_type == ExecutorDeviceType::GPU;
#endif
}
#define SKIP_NO_GPU() \
if (skip_tests(dt)) { \
CHECK(dt == ExecutorDeviceType::GPU); \
LOG(WARNING) << "GPU not available, skipping GPU tests"; \
continue; \
}
class HighCardinalityStringEnv : public ::testing::Test {
protected:
void SetUp() override {
run_ddl_statement("DROP TABLE IF EXISTS high_cardinality_str;");
run_ddl_statement(
"CREATE TABLE high_cardinality_str (x INT, str TEXT ENCODING DICT (32));");
QR::get()->runSQL("INSERT INTO high_cardinality_str VALUES (1, 'hi');",
ExecutorDeviceType::CPU);
QR::get()->runSQL("INSERT INTO high_cardinality_str VALUES (2, 'bye');",
ExecutorDeviceType::CPU);
}
void TearDown() override {
run_ddl_statement("DROP TABLE IF EXISTS high_cardinality_str;");
}
};
TEST_F(HighCardinalityStringEnv, PerfectHashNoFallback) {
// make our own executor with a custom col ranges cache
auto executor =
Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID, "", "", SystemParameters());
auto cat = QR::get()->getCatalog().get();
CHECK(cat);
auto td = cat->getMetadataForTable("high_cardinality_str");
CHECK(td);
auto cd = cat->getMetadataForColumn(td->tableId, "str");
CHECK(cd);
auto filter_cd = cat->getMetadataForColumn(td->tableId, "x");
CHECK(filter_cd);
auto db_id = cat->getDatabaseId();
PhysicalInput group_phys_input{cd->columnId, td->tableId, db_id};
PhysicalInput filter_phys_input{filter_cd->columnId, td->tableId, db_id};
std::unordered_set<PhysicalInput> phys_inputs{group_phys_input, filter_phys_input};
std::unordered_set<shared::TableKey> phys_table_ids;
phys_table_ids.insert({cat->getDatabaseId(), group_phys_input.table_id});
executor->setupCaching(phys_inputs, phys_table_ids);
auto input_descs = std::vector<InputDescriptor>{InputDescriptor(db_id, td->tableId, 0)};
std::list<std::shared_ptr<const InputColDescriptor>> input_col_descs;
input_col_descs.push_back(
std::make_shared<InputColDescriptor>(cd->columnId, td->tableId, db_id, 0));
input_col_descs.push_back(
std::make_shared<InputColDescriptor>(filter_cd->columnId, td->tableId, db_id, 0));
std::vector<InputTableInfo> table_infos = get_table_infos(input_descs, executor.get());
auto count_expr = makeExpr<Analyzer::AggExpr>(
SQLTypeInfo(kBIGINT, false), kCOUNT, nullptr, false, nullptr);
auto group_expr = makeExpr<Analyzer::ColumnVar>(
cd->columnType, shared::ColumnKey{db_id, td->tableId, cd->columnId}, 0);
auto filter_col_expr = makeExpr<Analyzer::ColumnVar>(
filter_cd->columnType,
shared::ColumnKey{db_id, td->tableId, filter_cd->columnId},
0);
Datum d{int64_t(1)};
auto filter_val_expr = makeExpr<Analyzer::Constant>(SQLTypeInfo(kINT, false), false, d);
auto simple_filter_expr = makeExpr<Analyzer::BinOper>(SQLTypeInfo(kBOOLEAN, false),
false,
SQLOps::kEQ,
SQLQualifier::kONE,
filter_col_expr,
filter_val_expr);
RelAlgExecutionUnit ra_exe_unit{input_descs,
input_col_descs,
{simple_filter_expr},
{},
{},
{group_expr},
{count_expr.get()},
{},
nullptr,
SortInfo(),
0};
ColumnCacheMap column_cache;
size_t max_groups_buffer_entry_guess = 1;
auto result =
executor->executeWorkUnit(max_groups_buffer_entry_guess,
/*is_agg=*/true,
table_infos,
ra_exe_unit,
CompilationOptions::defaults(ExecutorDeviceType::CPU),
ExecutionOptions::defaults(),
nullptr,
/*has_cardinality_estimation=*/false,
column_cache);
EXPECT_TRUE(result);
EXPECT_EQ(result->rowCount(), size_t(1));
auto row = result->getNextRow(false, false);
EXPECT_EQ(row.size(), size_t(1));
EXPECT_EQ(v<int64_t>(row[0]), 1);
}
std::unordered_set<PhysicalInput> setup_str_col_caching(PhysicalInput& group_phys_input,
const int64_t min,
const int64_t max,
PhysicalInput& filter_phys_input,
Executor* executor) {
std::unordered_set<PhysicalInput> phys_inputs{group_phys_input, filter_phys_input};
std::unordered_set<shared::TableKey> phys_table_ids;
auto db_id = QR::get()->getCatalog()->getDatabaseId();
phys_table_ids.insert({db_id, group_phys_input.table_id});
executor->setupCaching(phys_inputs, phys_table_ids);
auto filter_col_range = executor->getColRange(filter_phys_input);
// reset the col range to trigger the optimization
AggregatedColRange col_range_cache;
col_range_cache.setColRange(group_phys_input,
ExpressionRange::makeIntRange(min, max, 0, false));
col_range_cache.setColRange(filter_phys_input, filter_col_range);
executor->setColRangeCache(col_range_cache);
return phys_inputs;
}
TEST_F(HighCardinalityStringEnv, BaselineFallbackTest) {
// make our own executor with a custom col ranges cache
auto executor =
Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID, "", "", SystemParameters());
auto cat = QR::get()->getCatalog().get();
CHECK(cat);
auto td = cat->getMetadataForTable("high_cardinality_str");
CHECK(td);
auto cd = cat->getMetadataForColumn(td->tableId, "str");
CHECK(cd);
auto filter_cd = cat->getMetadataForColumn(td->tableId, "x");
CHECK(filter_cd);
auto db_id = cat->getDatabaseId();
PhysicalInput group_phys_input{cd->columnId, td->tableId, db_id};
PhysicalInput filter_phys_input{filter_cd->columnId, td->tableId, db_id};
// 134217728 is 1 additional value over the max buffer size
auto phys_inputs = setup_str_col_caching(
group_phys_input, /*min=*/0, /*max=*/134217728, filter_phys_input, executor.get());
auto input_descs = std::vector<InputDescriptor>{InputDescriptor(db_id, td->tableId, 0)};
std::list<std::shared_ptr<const InputColDescriptor>> input_col_descs;
input_col_descs.push_back(
std::make_shared<InputColDescriptor>(cd->columnId, td->tableId, db_id, 0));
input_col_descs.push_back(
std::make_shared<InputColDescriptor>(filter_cd->columnId, td->tableId, db_id, 0));
std::vector<InputTableInfo> table_infos = get_table_infos(input_descs, executor.get());
auto count_expr = makeExpr<Analyzer::AggExpr>(
SQLTypeInfo(kBIGINT, false), kCOUNT, nullptr, false, nullptr);
auto group_expr = makeExpr<Analyzer::ColumnVar>(
cd->columnType, shared::ColumnKey{db_id, td->tableId, cd->columnId}, 0);
auto filter_col_expr = makeExpr<Analyzer::ColumnVar>(
filter_cd->columnType,
shared::ColumnKey{db_id, td->tableId, filter_cd->columnId},
0);
Datum d{int64_t(1)};
auto filter_val_expr = makeExpr<Analyzer::Constant>(SQLTypeInfo(kINT, false), false, d);
auto simple_filter_expr = makeExpr<Analyzer::BinOper>(SQLTypeInfo(kBOOLEAN, false),
false,
SQLOps::kEQ,
SQLQualifier::kONE,
filter_col_expr,
filter_val_expr);
RelAlgExecutionUnit ra_exe_unit{input_descs,
input_col_descs,
{simple_filter_expr},
{},
{},
{group_expr},
{count_expr.get()},
{},
nullptr,
SortInfo(),
0};
ColumnCacheMap column_cache;
size_t max_groups_buffer_entry_guess = 1;
// expect throw w/out cardinality estimation
EXPECT_THROW(
executor->executeWorkUnit(max_groups_buffer_entry_guess,
/*is_agg=*/true,
table_infos,
ra_exe_unit,
CompilationOptions::defaults(ExecutorDeviceType::CPU),
ExecutionOptions::defaults(),
nullptr,
/*has_cardinality_estimation=*/false,
column_cache),
CardinalityEstimationRequired);
auto result =
executor->executeWorkUnit(max_groups_buffer_entry_guess,
/*is_agg=*/true,
table_infos,
ra_exe_unit,
CompilationOptions::defaults(ExecutorDeviceType::CPU),
ExecutionOptions::defaults(),
nullptr,
/*has_cardinality_estimation=*/true,
column_cache);
EXPECT_TRUE(result);
EXPECT_EQ(result->rowCount(), size_t(1));
auto row = result->getNextRow(false, false);
EXPECT_EQ(row.size(), size_t(1));
EXPECT_EQ(v<int64_t>(row[0]), 1);
}
TEST_F(HighCardinalityStringEnv, BaselineNoFilters) {
// make our own executor with a custom col ranges cache
auto executor =
Executor::getExecutor(Executor::UNITARY_EXECUTOR_ID, "", "", SystemParameters());
auto cat = QR::get()->getCatalog().get();
CHECK(cat);
auto td = cat->getMetadataForTable("high_cardinality_str");
CHECK(td);
auto cd = cat->getMetadataForColumn(td->tableId, "str");
CHECK(cd);
auto filter_cd = cat->getMetadataForColumn(td->tableId, "x");
CHECK(filter_cd);
auto db_id = cat->getDatabaseId();
PhysicalInput group_phys_input{cd->columnId, td->tableId, db_id};
PhysicalInput filter_phys_input{filter_cd->columnId, td->tableId, db_id};
// 134217728 is 1 additional value over the max buffer size
auto phys_inputs = setup_str_col_caching(
group_phys_input, /*min=*/0, /*max=*/134217728, filter_phys_input, executor.get());
auto input_descs = std::vector<InputDescriptor>{InputDescriptor(db_id, td->tableId, 0)};
std::list<std::shared_ptr<const InputColDescriptor>> input_col_descs;
input_col_descs.push_back(
std::make_shared<InputColDescriptor>(cd->columnId, td->tableId, db_id, 0));
input_col_descs.push_back(
std::make_shared<InputColDescriptor>(filter_cd->columnId, td->tableId, db_id, 0));
std::vector<InputTableInfo> table_infos = get_table_infos(input_descs, executor.get());
auto count_expr = makeExpr<Analyzer::AggExpr>(
SQLTypeInfo(kBIGINT, false), kCOUNT, nullptr, false, nullptr);
auto group_expr = makeExpr<Analyzer::ColumnVar>(
cd->columnType, shared::ColumnKey{db_id, td->tableId, cd->columnId}, 0);
RelAlgExecutionUnit ra_exe_unit{input_descs,
input_col_descs,
{},
{},
{},
{group_expr},
{count_expr.get()},
{},
nullptr,
SortInfo(),
0};
ColumnCacheMap column_cache;
size_t max_groups_buffer_entry_guess = 1;
// no filters, so expect no throw w/out cardinality estimation
auto result =
executor->executeWorkUnit(max_groups_buffer_entry_guess,
/*is_agg=*/true,
table_infos,
ra_exe_unit,
CompilationOptions::defaults(ExecutorDeviceType::CPU),
ExecutionOptions::defaults(),
nullptr,
/*has_cardinality_estimation=*/false,
column_cache);
EXPECT_TRUE(result);
EXPECT_EQ(result->rowCount(), size_t(2));
{
auto row = result->getNextRow(false, false);
EXPECT_EQ(row.size(), size_t(1));
EXPECT_EQ(v<int64_t>(row[0]), 1);
}
{
auto row = result->getNextRow(false, false);
EXPECT_EQ(row.size(), size_t(1));
EXPECT_EQ(v<int64_t>(row[0]), 1);
}
}
class LowCardinalityThresholdTest : public ::testing::Test {
protected:
void SetUp() override {
run_ddl_statement("DROP TABLE IF EXISTS low_cardinality;");
run_ddl_statement("CREATE TABLE low_cardinality (fl text,ar text, dep text);");
// note - some boost::filesystem::path methods returns wchar strings on windows
// and char strings on linux.
// write some data to a file
boost::filesystem::path filename =
boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
filename.replace_extension(boost::filesystem::path{".csv"});
// Note on win path::native() returns wchar, which is fine with fstream
std::fstream f(filename.native(), std::ios::binary | std::ios::out | std::ios::trunc);
CHECK(f.is_open());
for (size_t i = 0; i < g_big_group_threshold; i++) {
f << i << ", " << i + 1 << ", " << i + 2 << std::endl;
}
f.close();
// Note the path::string() method returns the appropriate type on win and linux
run_ddl_statement("COPY low_cardinality FROM '" + filename.string() +
"' WITH (header='false');");
}
void TearDown() override { run_ddl_statement("DROP TABLE IF EXISTS low_cardinality;"); }
};
TEST_F(LowCardinalityThresholdTest, GroupBy) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result = QR::get()->runSQL(
R"(select fl,ar,dep from low_cardinality group by fl,ar,dep;)", dt);
EXPECT_EQ(result->rowCount(), g_big_group_threshold);
}
}
class BigCardinalityThresholdTest : public ::testing::Test {
protected:
void SetUp() override {
g_enable_watchdog = true;
initial_g_watchdog_baseline_max_groups = g_watchdog_baseline_max_groups;
g_watchdog_baseline_max_groups = g_big_group_threshold + 1;
run_ddl_statement("DROP TABLE IF EXISTS big_cardinality;");
run_ddl_statement("CREATE TABLE big_cardinality (fl text,ar text, dep text);");
// note - some boost::filesystem::path methods returns wchar strings on windows
// and char strings on linux.
// write some data to a file
boost::filesystem::path filename =
boost::filesystem::temp_directory_path() / boost::filesystem::unique_path();
filename.replace_extension(boost::filesystem::path{".csv"});
std::fstream f(filename.native(), std::ios::binary | std::ios::out | std::ios::trunc);
CHECK(f.is_open()) << filename.string();
// add enough groups to trigger the watchdog exception if we use a poor estimate
for (size_t i = 0; i < g_watchdog_baseline_max_groups; i++) {
f << i << ", " << i + 1 << ", " << i + 2 << std::endl;
}
f.close();
run_ddl_statement("COPY big_cardinality FROM '" + filename.string() +
"' WITH (header='false');");
}
void TearDown() override {
g_enable_watchdog = false;
g_watchdog_baseline_max_groups = initial_g_watchdog_baseline_max_groups;
run_ddl_statement("DROP TABLE IF EXISTS big_cardinality;");
}
size_t initial_g_watchdog_baseline_max_groups{0};
};
TEST_F(BigCardinalityThresholdTest, EmptyFilters) {
for (auto dt : {ExecutorDeviceType::CPU, ExecutorDeviceType::GPU}) {
SKIP_NO_GPU();
auto result = QR::get()->runSQL(
R"(SELECT fl,ar,dep FROM big_cardinality WHERE fl = 'a' GROUP BY fl,ar,dep;)",
dt);
EXPECT_EQ(result->rowCount(), size_t(0));
}
}
int main(int argc, char** argv) {
g_is_test_env = true;
TestHelpers::init_logger_stderr_only(argc, argv);
testing::InitGoogleTest(&argc, argv);
namespace po = boost::program_options;
po::options_description desc("Options");
logger::LogOptions log_options(argv[0]);
log_options.max_files_ = 0; // stderr only by default
desc.add(log_options.get_options());
po::variables_map vm;
po::store(po::command_line_parser(argc, argv).options(desc).run(), vm);
po::notify(vm);
QR::init(BASE_PATH);
int err{0};
try {
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
QR::reset();
return err;
}