forked from heavyai/heavydb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlterColumnTest.cpp
286 lines (259 loc) · 9.63 KB
/
AlterColumnTest.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
/*
* 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.
*/
#include <glog/logging.h>
#include <gtest/gtest.h>
#include <boost/algorithm/string.hpp>
#include <boost/iterator/counting_iterator.hpp>
#include "boost/filesystem.hpp"
#include "Catalog/Catalog.h"
#include "Fragmenter/InsertOrderFragmenter.h"
#include "Import/Importer.h"
#include "Parser/parser.h"
#include "QueryEngine/ResultSet.h"
#include "QueryRunner/QueryRunner.h"
#include "Shared/UpdelRoll.h"
#include "Shared/geo_types.h"
#include "Tests/TestHelpers.h"
#include <tuple>
#ifndef BASE_PATH
#define BASE_PATH "./tmp"
#endif
#define CALCITEPORT 9093
using namespace std;
using namespace Catalog_Namespace;
using namespace TestHelpers;
namespace {
std::unique_ptr<SessionInfo> gsession;
bool g_hoist_literals{true};
inline void run_ddl_statement(const string& input_str) {
QueryRunner::run_ddl_statement(input_str, gsession);
}
std::shared_ptr<ResultSet> run_query(const string& query_str) {
return QueryRunner::run_multiple_agg(
query_str, gsession, ExecutorDeviceType::CPU, g_hoist_literals, true);
}
template <typename E = std::runtime_error>
bool alter_common(const string& table,
const string& column,
const string& type,
const string& comp,
const string& val,
const string& val2,
const bool expect_throw = false) {
std::string alter_query = "alter table " + table + " add column " + column + " " + type;
if (val != "") {
alter_query += " default " + val;
}
if (comp != "") {
alter_query += " encoding " + comp;
}
if (expect_throw) {
EXPECT_THROW(run_ddl_statement(alter_query + ";"), E);
return true;
} else {
EXPECT_NO_THROW(run_ddl_statement(alter_query + ";"););
}
if (val2 != "") {
std::string query_str = "SELECT " + column + " FROM " + table;
auto rows = run_query(query_str + ";");
int r_cnt = 0;
while (true) {
auto crt_row = rows->getNextRow(true, true);
if (0 == crt_row.size()) {
break;
}
auto geo = boost::get<std::string>(v<NullableString>(crt_row[0]));
#if 1
if (geo == val2) {
++r_cnt;
}
#else
// somehow these do not work as advertised ...
using namespace Geo_namespace;
if (boost::iequals(type, "POINT") && GeoPoint(geo) == GeoPoint(val2))
++r_cnt;
else if (boost::iequals(type, "LINESTRING") &&
GeoLineString(geo) == GeoLineString(val2))
++r_cnt;
else if (boost::iequals(type, "POLYGON") && GeoPolygon(geo) == GeoPolygon(val2))
++r_cnt;
else if (boost::iequals(type, "MULTIPOLYGON") &&
GeoMultiPolygon(geo) == GeoMultiPolygon(val2))
++r_cnt;
#endif
}
return r_cnt == 100;
} else {
std::string query_str =
"SELECT count() FROM " + table + " WHERE " + column +
("" == val || boost::iequals("NULL", val) ? " IS NULL" : (" = " + val));
auto rows = run_query(query_str + ";");
auto crt_row = rows->getNextRow(true, true);
CHECK_EQ(size_t(1), crt_row.size());
auto r_cnt = v<int64_t>(crt_row[0]);
return r_cnt == 100;
}
}
void import_table_file(const string& table, const string& file) {
std::string query_str = string("COPY trips FROM '") + "../../Tests/Import/datafiles/" +
file + "' WITH (header='true');";
SQLParser parser;
std::list<std::unique_ptr<Parser::Stmt>> parse_trees;
std::string last_parsed;
if (parser.parse(query_str, parse_trees, last_parsed)) {
throw std::runtime_error("Failed to parse: " + query_str);
}
CHECK_EQ(parse_trees.size(), size_t(1));
const auto& stmt = parse_trees.front();
Parser::DDLStmt* ddl = dynamic_cast<Parser::DDLStmt*>(stmt.get());
if (!ddl) {
throw std::runtime_error("Not a DDLStmt: " + query_str);
}
ddl->execute(*gsession);
}
class SQLTestEnv : public ::testing::Environment {
public:
virtual void SetUp() {
gsession.reset(QueryRunner::get_session(BASE_PATH,
"gtest",
"test!test!",
"gtest_db",
std::vector<LeafHostInfo>{},
std::vector<LeafHostInfo>{},
false,
true,
true));
}
};
// don't use R"()" format; somehow it causes many blank lines
// to be output on console. how come?
const char* create_table_trips =
" CREATE TABLE trips ("
" medallion TEXT ENCODING DICT,"
" hack_license TEXT ENCODING DICT,"
" vendor_id TEXT ENCODING DICT,"
" rate_code_id SMALLINT,"
" store_and_fwd_flag TEXT ENCODING DICT,"
" pickup_datetime TIMESTAMP,"
" dropoff_datetime TIMESTAMP,"
" passenger_count SMALLINT,"
" trip_time_in_secs INTEGER,"
" trip_distance FLOAT,"
" pickup_longitude DECIMAL(14,7),"
" pickup_latitude DECIMAL(14,7),"
" dropoff_longitude DOUBLE,"
" dropoff_latitude DECIMAL(18,5),"
" deleted BOOLEAN"
" ) WITH (FRAGMENT_SIZE=50);"; // so 2 fragments here
void init_table_data(const string& table = "trips",
const string& create_table_cmd = create_table_trips,
const string& file = "trip_data_b.txt") {
run_ddl_statement("drop table if exists " + table + ";");
run_ddl_statement(create_table_cmd);
if (file.size()) {
import_table_file(table, file);
}
}
class AlterColumnTest : public ::testing::Test {
protected:
virtual void SetUp() { ASSERT_NO_THROW(init_table_data();); }
virtual void TearDown() { ASSERT_NO_THROW(run_ddl_statement("drop table trips;");); }
};
#define MT std::make_tuple
std::vector<std::tuple<std::string, std::string, std::string, std::string>> type_vals = {
MT("text", "none", "'abc'", ""),
MT("text", "dict(8)", "'ijk'", ""),
MT("text", "dict(32)", "'xyz'", ""),
MT("float", "", "1.25", ""),
MT("double", "", "1.25", ""),
MT("smallint", "", "123", ""),
MT("integer", "", "123", ""),
MT("bigint", "", "123", ""),
MT("bigint encoding fixed(8)", "", "", ""),
MT("bigint encoding fixed(16)", "", "", ""),
MT("bigint encoding fixed(32)", "", "", ""),
MT("decimal(8)", "", "123", ""),
MT("decimal(8,2)", "", "1.23", ""),
MT("date", "", "'2011-10-23'", ""),
MT("time", "", "'10:23:45'", ""),
MT("timestamp", "", "'2011-10-23 10:23:45'", ""),
MT("POINT", "", "'POINT (1 2)'", "POINT (1 2)"),
MT("LINESTRING", "", "'LINESTRING (1 1,2 2,3 3)'", "LINESTRING (1 1,2 2,3 3)"),
MT("POLYGON",
"",
"'POLYGON((0 0,0 9,9 9,9 0),(1 1,2 2,3 3))'",
"POLYGON ((9 0,9 9,0 9,0 0,9 0),(3 3,2 2,1 1,3 3))"),
MT("MULTIPOLYGON",
"",
"'MULTIPOLYGON(((0 0,0 9,9 9,9 0),(1 1,2 2,3 3)))'",
"MULTIPOLYGON (((9 0,9 9,0 9,0 0,9 0),(3 3,2 2,1 1,3 3)))"),
};
TEST_F(AlterColumnTest, Add_column_with_default) {
int cid = 0;
for (const auto& tv : type_vals) {
EXPECT_TRUE(alter_common("trips",
"x" + std::to_string(++cid),
std::get<0>(tv),
std::get<1>(tv),
std::get<2>(tv),
std::get<3>(tv),
false));
}
}
TEST_F(AlterColumnTest, Add_column_with_null) {
int cid = 0;
for (const auto& tv : type_vals) {
if (std::get<3>(tv) == "") {
EXPECT_TRUE(alter_common("trips",
"x" + std::to_string(++cid),
std::get<0>(tv),
std::get<1>(tv),
"",
"",
false));
} else {
EXPECT_TRUE(alter_common("trips",
"x" + std::to_string(++cid),
std::get<0>(tv),
std::get<1>(tv),
"",
std::get<3>(tv),
true));
}
}
}
TEST(AlterColumnTest2, Drop_after_fail_to_add) {
EXPECT_NO_THROW(run_ddl_statement("drop table if exists t;"););
EXPECT_NO_THROW(run_ddl_statement("create table t(c1 int);"););
EXPECT_NO_THROW(run_query("insert into t values (10);"););
EXPECT_THROW(
run_ddl_statement("alter table t add column c2 TEXT NOT NULL ENCODING DICT;"),
std::runtime_error);
EXPECT_NO_THROW(run_ddl_statement("drop table t;"););
}
} // namespace
int main(int argc, char** argv) {
google::InitGoogleLogging(argv[0]);
testing::InitGoogleTest(&argc, argv);
::testing::AddGlobalTestEnvironment(new SQLTestEnv);
int err{0};
try {
err = RUN_ALL_TESTS();
} catch (const std::exception& e) {
LOG(ERROR) << e.what();
}
return err;
}