forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestHelpers.h
319 lines (281 loc) · 9.41 KB
/
TestHelpers.h
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
/*
* Copyright 2017 MapD Technologies, 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.
*/
#ifndef TEST_HELPERS_H_
#define TEST_HELPERS_H_
#include "../QueryEngine/TargetValue.h"
#include "Logger/Logger.h"
#include "LeafHostInfo.h"
#include <gtest/gtest.h>
#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <boost/variant.hpp>
namespace TestHelpers {
template <class T>
void compare_array(const TargetValue& r,
const std::vector<T>& arr,
const double tol = -1.) {
auto array_tv = boost::get<ArrayTargetValue>(&r);
CHECK(array_tv);
if (!array_tv->is_initialized()) {
ASSERT_EQ(size_t(0), arr.size());
return;
}
const auto& scalar_tv_vector = array_tv->get();
ASSERT_EQ(scalar_tv_vector.size(), arr.size());
size_t ctr = 0;
for (const ScalarTargetValue& scalar_tv : scalar_tv_vector) {
auto p = boost::get<T>(&scalar_tv);
CHECK(p);
if (tol < 0.) {
ASSERT_EQ(*p, arr[ctr++]);
} else {
ASSERT_NEAR(*p, arr[ctr++], tol);
}
}
}
template <>
void compare_array(const TargetValue& r,
const std::vector<std::string>& arr,
const double tol) {
auto array_tv = boost::get<ArrayTargetValue>(&r);
CHECK(array_tv);
if (!array_tv->is_initialized()) {
ASSERT_EQ(size_t(0), arr.size());
return;
}
const auto& scalar_tv_vector = array_tv->get();
ASSERT_EQ(scalar_tv_vector.size(), arr.size());
size_t ctr = 0;
for (const ScalarTargetValue& scalar_tv : scalar_tv_vector) {
auto ns = boost::get<NullableString>(&scalar_tv);
CHECK(ns);
auto str = boost::get<std::string>(ns);
CHECK(str);
ASSERT_TRUE(*str == arr[ctr++]);
}
}
template <class T>
void compare_array(const std::vector<T>& a,
const std::vector<T>& b,
const double tol = -1.) {
CHECK_EQ(a.size(), b.size());
for (size_t i = 0; i < a.size(); i++) {
if (tol < 0.) {
ASSERT_EQ(a[i], b[i]);
} else {
ASSERT_NEAR(a[i], b[i], tol);
}
}
}
struct GeoTargetComparator {
static void compare(const GeoPointTargetValue& a,
const GeoPointTargetValue& b,
const double tol = -1.) {
compare_array(*a.coords, *b.coords, tol);
}
static void compare(const GeoLineStringTargetValue& a,
const GeoLineStringTargetValue& b,
const double tol = -1.) {
compare_array(*a.coords, *b.coords, tol);
}
static void compare(const GeoPolyTargetValue& a,
const GeoPolyTargetValue& b,
const double tol = -1.) {
compare_array(*a.coords, *b.coords, tol);
compare_array(*a.ring_sizes, *b.ring_sizes);
}
static void compare(const GeoMultiPolyTargetValue& a,
const GeoMultiPolyTargetValue& b,
const double tol = -1.) {
compare_array(*a.coords, *b.coords, tol);
compare_array(*a.ring_sizes, *b.ring_sizes);
compare_array(*a.poly_rings, *b.poly_rings);
}
};
template <class T>
T g(const TargetValue& r) {
auto geo_r = boost::get<GeoTargetValue>(&r);
CHECK(geo_r);
CHECK(geo_r->is_initialized());
return boost::get<T>(geo_r->get());
}
template <class T>
void compare_geo_target(const TargetValue& r,
const T& geo_truth_target,
const double tol = -1.) {
const auto geo_value = g<T>(r);
GeoTargetComparator::compare(geo_value, geo_truth_target, tol);
}
template <class T>
T v(const TargetValue& r) {
auto scalar_r = boost::get<ScalarTargetValue>(&r);
CHECK(scalar_r);
auto p = boost::get<T>(scalar_r);
CHECK(p);
return *p;
}
template <typename T>
inline std::string convert(const T& t) {
return std::to_string(t);
}
template <std::size_t N>
inline std::string convert(const char (&t)[N]) {
return std::string(t);
}
template <>
inline std::string convert(const std::string& t) {
return t;
}
bool is_null_tv(const TargetValue& tv, const SQLTypeInfo& ti) {
if (ti.get_notnull()) {
return false;
}
const auto scalar_tv = boost::get<ScalarTargetValue>(&tv);
if (!scalar_tv) {
CHECK(ti.is_array());
const auto array_tv = boost::get<ArrayTargetValue>(&tv);
CHECK(array_tv);
return !array_tv->is_initialized();
}
if (boost::get<int64_t>(scalar_tv)) {
int64_t data = *(boost::get<int64_t>(scalar_tv));
switch (ti.get_type()) {
case kBOOLEAN:
return data == NULL_BOOLEAN;
case kTINYINT:
return data == NULL_TINYINT;
case kSMALLINT:
return data == NULL_SMALLINT;
case kINT:
return data == NULL_INT;
case kDECIMAL:
case kNUMERIC:
case kBIGINT:
return data == NULL_BIGINT;
case kTIME:
case kTIMESTAMP:
case kDATE:
case kINTERVAL_DAY_TIME:
case kINTERVAL_YEAR_MONTH:
return data == NULL_BIGINT;
default:
CHECK(false);
}
} else if (boost::get<double>(scalar_tv)) {
double data = *(boost::get<double>(scalar_tv));
if (ti.get_type() == kFLOAT) {
return data == NULL_FLOAT;
} else {
return data == NULL_DOUBLE;
}
} else if (boost::get<float>(scalar_tv)) {
CHECK_EQ(kFLOAT, ti.get_type());
float data = *(boost::get<float>(scalar_tv));
return data == NULL_FLOAT;
} else if (boost::get<NullableString>(scalar_tv)) {
auto s_n = boost::get<NullableString>(scalar_tv);
auto s = boost::get<std::string>(s_n);
return !s;
}
CHECK(false);
return false;
}
struct ValuesGenerator {
ValuesGenerator(const std::string& table_name) : table_name_(table_name) {}
template <typename... COL_ARGS>
std::string operator()(COL_ARGS&&... args) const {
std::vector<std::string> vals({convert(std::forward<COL_ARGS>(args))...});
return std::string("INSERT INTO " + table_name_ + " VALUES(" +
boost::algorithm::join(vals, ",") + ");");
}
const std::string table_name_;
};
LeafHostInfo to_leaf_host_info(std::string& server_info, NodeRole role) {
size_t pos = server_info.find(':');
if (pos == std::string::npos) {
throw std::runtime_error("Invalid host:port -> " + server_info);
}
auto host = server_info.substr(0, pos);
auto port = server_info.substr(pos + 1);
return LeafHostInfo(host, std::stoi(port), role);
}
std::vector<LeafHostInfo> to_leaf_host_info(std::vector<std::string>& server_infos,
NodeRole role) {
std::vector<LeafHostInfo> host_infos;
for (auto& server_info : server_infos) {
host_infos.push_back(to_leaf_host_info(server_info, role));
}
return host_infos;
}
void init_logger_stderr_only(int argc, char const* const* argv) {
logger::LogOptions log_options(argv[0]);
log_options.max_files_ = 0; // stderr only by default
log_options.parse_command_line(argc, argv);
logger::init(log_options);
}
void init_logger_stderr_only() {
logger::LogOptions log_options(nullptr);
log_options.max_files_ = 0; // stderr only by default
logger::init(log_options);
}
struct ShardInfo {
const std::string shard_col;
const size_t shard_count;
};
struct SharedDictionaryInfo {
const std::string col;
const std::string ref_table;
const std::string ref_col;
};
std::string build_create_table_statement(
const std::string& columns_definition,
const std::string& table_name,
const ShardInfo& shard_info,
const std::vector<SharedDictionaryInfo>& shared_dict_info,
const size_t fragment_size,
const bool use_temporary_tables,
const bool delete_support = true,
const bool replicated = false) {
const std::string shard_key_def{
shard_info.shard_col.empty() ? "" : ", SHARD KEY (" + shard_info.shard_col + ")"};
std::vector<std::string> shared_dict_def;
if (shared_dict_info.size() > 0) {
for (size_t idx = 0; idx < shared_dict_info.size(); ++idx) {
shared_dict_def.push_back(", SHARED DICTIONARY (" + shared_dict_info[idx].col +
") REFERENCES " + shared_dict_info[idx].ref_table + "(" +
shared_dict_info[idx].ref_col + ")");
}
}
std::ostringstream with_statement_assembly;
if (!shard_info.shard_col.empty()) {
with_statement_assembly << "shard_count=" << shard_info.shard_count << ", ";
}
with_statement_assembly << "fragment_size=" << fragment_size;
if (delete_support) {
with_statement_assembly << ", vacuum='delayed'";
} else {
with_statement_assembly << ", vacuum='immediate'";
}
const std::string replicated_def{
(!replicated || !shard_info.shard_col.empty()) ? "" : ", PARTITIONS='REPLICATED' "};
const std::string create_def{use_temporary_tables ? "CREATE TEMPORARY TABLE "
: "CREATE TABLE "};
return create_def + table_name + "(" + columns_definition + shard_key_def +
boost::algorithm::join(shared_dict_def, "") + ") WITH (" +
with_statement_assembly.str() + replicated_def + ");";
}
} // namespace TestHelpers
#endif // TEST_HELPERS_H_