forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_parser.cpp
More file actions
388 lines (375 loc) · 16 KB
/
Copy pathtest_parser.cpp
File metadata and controls
388 lines (375 loc) · 16 KB
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
/*
* Copyright (c) 2025 OceanBase.
*
* 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 "../test_sql_utils.h"
using namespace oceanbase::common;
using namespace oceanbase::sql;
namespace test
{
class TestParser: public TestSqlUtils, public ::testing::Test
{
public:
TestParser();
virtual ~TestParser();
virtual void SetUp();
virtual void TearDown();
private:
// disallow copy
DISALLOW_COPY_AND_ASSIGN(TestParser);
protected:
// function members
void do_parse(const char *query_str, std::ofstream &of_result, int64_t expect_error = OB_SUCCESS);
void print_parse_tree(const char *query_str, std::ofstream &of_result, int64_t expect_error = OB_SUCCESS);
void print_parse_outline(const char *query_str, std::ofstream &of_result, int64_t expect_error = OB_SUCCESS);
void do_filter_hint(const char *query_str, std::ofstream &of_result, int64_t expect_error = OB_SUCCESS);
bool pretreat_cmd(std::string line, int64_t &expect_error);
void parse_keyword(std::ifstream &if_tests, std::ofstream &of_result, bool is_sql_keyword);
void do_parse_keyword(const char *keyword, std::ofstream &of_result, bool is_reserved, bool is_sql_keyword);
bool non_reserved_keyword_can_not_be_name(const char *keyword, bool is_sql_keyword);
protected:
// data members
ObArenaAllocator allocator_;
};
TestParser::TestParser() : allocator_(ObModIds::TEST)
{
}
TestParser::~TestParser()
{
}
void TestParser::SetUp()
{
init();
}
void TestParser::TearDown()
{
}
void TestParser::print_parse_tree(const char *query_str, std::ofstream &of_result, int64_t expect_error) {
ObSQLMode mode = test::clp.sql_mode;
ObParser parser(allocator_, mode);
ParseResult parse_result;
ObString query = ObString::make_string(query_str);
int ret = OB_SUCCESS;
ret = parser.parse(query, parse_result);
if(expect_error != -ret) {
fprintf(stderr, "QUERY:%s failed\n", query_str);
}
ASSERT_EQ(expect_error, -ret);
if (NULL != parse_result.result_tree_) {
_OB_LOG(INFO, "%s", CSJ(ObParserResultTreePrintWrapper(*parse_result.result_tree_)));
of_result << "question_mask_size: " << parse_result.result_tree_->children_[0]->value_ << std::endl;
of_result << CSJ(ObParserResultTreePrintWrapper(*parse_result.result_tree_)) << std::endl;
}
parser.free_result(parse_result);
}
void TestParser::print_parse_outline(const char *query_str, std::ofstream &of_result, int64_t expect_error) {
ObSQLMode mode = test::clp.sql_mode;
ObParser parser(allocator_, mode);
ParseResult parse_result;
ObString query = ObString::make_string(query_str);
int ret = OB_SUCCESS;
ret = parser.parse(query, parse_result, FP_PARAMERIZE_AND_FILTER_HINT_MODE);
if(expect_error != -ret) {
fprintf(stderr, "QUERY:%s failed\n", query_str);
}
ASSERT_EQ(expect_error, -ret);
of_result << parse_result.no_param_sql_<<std::endl<<std::endl;
parser.free_result(parse_result);
}
void TestParser::parse_keyword(std::ifstream &if_tests, std::ofstream &of_result, bool is_sql_keyword)
{
std::string line;
const char* reserved_keyword_begin = "//-----------------------------reserved keyword begin-----------------------------------------------";
const char* reserved_keyword_end = "//-----------------------------reserved keyword end-------------------------------------------------";
const char* non_reserved_keyword_begin = "//-----------------------------non_reserved keyword begin-------------------------------------------";
const char* non_keyword_end = "//-----------------------------non_reserved keyword end---------------------------------------------";
bool test_reserved_keyword = false;
bool test_non_reserved_keyword = false;
int64_t num_reserved_keyword = 0;
while (std::getline(if_tests, line)) {
if (strncmp(line.c_str(), reserved_keyword_begin, strlen(reserved_keyword_begin)) == 0) {
of_result << "************** Begin Test Reserved Keyword ***************" << std::endl;
test_reserved_keyword = true;
continue;
} else if (strncmp(line.c_str(), reserved_keyword_end, strlen(reserved_keyword_end)) == 0) {
of_result << "************** Total Count of Reserved Keyword:" << num_reserved_keyword <<" ***************" << std::endl;
of_result << "************** End Test Reserved Keyword ***************" << std::endl;
test_reserved_keyword = false;
continue;
} else if (strncmp(line.c_str(), non_reserved_keyword_begin, strlen(non_reserved_keyword_begin)) == 0) {
of_result << "************** Begin Test Non-Reserved Keyword ***************" << std::endl;
test_non_reserved_keyword = true;
continue;
} else if (strncmp(line.c_str(), non_keyword_end, strlen(non_keyword_end)) == 0) {
of_result << "************** End Test Non-Reserved Keyword ***************" << std::endl;
break;
} else if (!test_reserved_keyword && !test_non_reserved_keyword) {
continue;
}
int64_t line_size = strlen(line.c_str());
char *tmp_buf = NULL;
if (NULL == (tmp_buf = static_cast<char*>(allocator_.alloc(line_size + 1)))) {
fprintf(stderr, "failed to alloc memory for: %s\n", line.c_str());
break;
} else {
MEMSET(tmp_buf, '\0', line_size + 1);
int64_t valid_buf_len = 0;
for (int64_t i = 0; i < line_size; ++i) {
if (isspace(line.c_str()[i])) {
if (valid_buf_len > 0 && valid_buf_len <= line_size) {
tmp_buf[valid_buf_len] = '\0';
if ((is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "FILE_KEY") == 0) ||
(!is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "BEGIN_KEY") == 0) ||
(!is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "END_KEY") == 0)) {
valid_buf_len -= 4;//FILE_KEY ==> FILE、BEGIN_KEY==> DEGIN、END_KEY==> END
tmp_buf[valid_buf_len] = '\0';
} else if ((is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "INITIAL_") == 0) ||
(!is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "NULLX") == 0)) {
valid_buf_len -= 1;//INITIAL_ ==> INITIAL、NULLX==>NULL
tmp_buf[valid_buf_len] = '\0';
}
ASSERT_NO_FATAL_FAILURE(do_parse_keyword(tmp_buf, of_result, test_reserved_keyword, is_sql_keyword));
valid_buf_len = 0;
if (test_reserved_keyword) {
++ num_reserved_keyword;
}
}
} else if (line.c_str()[i] == '/' && i < line_size - 1 && line.c_str()[i + 1] == '*') {
i = i + 2;
while (i < line_size) {
if (line.c_str()[i] == '*' && i < line_size - 1 && line.c_str()[i + 1] == '/') {
i = i + 2;
break;
}
++ i;
}
} else if (valid_buf_len < line_size) {
tmp_buf[valid_buf_len++] = line.c_str()[i];
}
}
if (valid_buf_len > 0 && valid_buf_len <= line_size) {
tmp_buf[valid_buf_len] = '\0';
if ((is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "FILE_KEY") == 0) ||
(!is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "BEGIN_KEY") == 0) ||
(!is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "END_KEY") == 0)) {
valid_buf_len -= 4;//FILE_KEY ==> FILE、BEGIN_KEY==> DEGIN、END_KEY==> END
tmp_buf[valid_buf_len] = '\0';
} else if ((is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "INITIAL_") == 0) ||
(!is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(tmp_buf, "NULLX") == 0)) {
valid_buf_len -= 1;//INITIAL_ ==> INITIAL、NULLX==>NULL
tmp_buf[valid_buf_len] = '\0';
}
ASSERT_NO_FATAL_FAILURE(do_parse_keyword(tmp_buf, of_result, test_reserved_keyword, is_sql_keyword));
valid_buf_len = 0;
if (test_reserved_keyword) {
++ num_reserved_keyword;
}
}
}
}
}
//reserved keyword test: create table table_name(column_name int);
//non_reserved keyword test: create table table_name(column_name int)
// select 1 alias_name from t1 alias_name;
void TestParser::do_parse_keyword(const char *keyword, std::ofstream &of_result, bool is_reserved, bool is_sql_keyword)
{
char *buf = NULL;
int64_t buf_len = strlen(keyword) * 2 + 1024;
if (NULL == (buf = static_cast<char*>(allocator_.alloc(buf_len)))) {
fprintf(stderr, "failed to alloc memory for: %s\n", keyword);
} else if (is_sql_keyword) {
sprintf(buf, "create table %s(%s int);", keyword, keyword);
if (is_reserved) {
of_result << "Reserved Keyword: "<< keyword << std::endl;
of_result << "Test Table Name and Column Name Sql:" << buf << std::endl;
}
} else if (test::clp.sql_mode & SMO_ORACLE) {
sprintf(buf, "create type %s as table of varchar2(1);", keyword);
if (is_reserved) {
of_result << "Reserved Keyword: "<< keyword << std::endl;
of_result << "Test PL type name Sql:" << buf << std::endl;
}
} else {
sprintf(buf, "create procedure %s () select 1;", keyword);
if (is_reserved) {
of_result << "Reserved Keyword: "<< keyword << std::endl;
of_result << "Test PL procedure name Sql:" << buf << std::endl;
}
}
ObString query = ObString::make_string(buf);
int ret = OB_SUCCESS;
_OB_LOG(INFO, "test keyword to be name: %s", buf);
ObSQLMode mode = test::clp.sql_mode;
ObParser parser(allocator_, mode);
ParseResult parse_result;
ret = parser.parse(query, parse_result);
if ((is_reserved && ret != 0) ||
(!is_reserved && ret == 0)) {
ret = OB_SUCCESS;
} else if (!is_reserved && is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(keyword, "CONSTRAINT") == 0) {
ret = OB_SUCCESS;
} else if ((is_reserved && !is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(keyword, "EXISTS") == 0) ||
(is_reserved && !is_sql_keyword && (test::clp.sql_mode & SMO_ORACLE) && strcmp(keyword, "SET") == 0)) {
ret = OB_SUCCESS;
} else if (!is_reserved && !is_sql_keyword && non_reserved_keyword_can_not_be_name(keyword, is_sql_keyword)) {
ret = OB_SUCCESS;
} else {
fprintf(stderr, "test keyword to be name failed:%s\n", buf);
}
ASSERT_EQ(OB_SUCCESS, -ret);
if (!is_reserved && is_sql_keyword) {
sprintf(buf, "select 1 %s from t1 %s;", keyword, keyword);
query = ObString::make_string(buf);
_OB_LOG(INFO, "test keyword to alias name: %s", buf);
ret = parser.parse(query, parse_result);
if (ret != 0) {
if (non_reserved_keyword_can_not_be_name(keyword, is_sql_keyword)) {
ret = 0;
} else {
fprintf(stderr, "test keyword to alias name failed:%s\n", buf);
}
}
}
ASSERT_EQ(OB_SUCCESS, -ret);
parser.free_result(parse_result);
}
bool TestParser::non_reserved_keyword_can_not_be_name(const char *keyword, bool is_sql_keyword)
{
bool ret = false;
if (is_sql_keyword) {
if (test::clp.sql_mode & SMO_ORACLE) {
if (strcmp(keyword, "USING") == 0 ||
strcmp(keyword, "BULK") == 0 ||
strcmp(keyword, "COLLATE") == 0 ||
strcmp(keyword, "CROSS") == 0 ||
strcmp(keyword, "FULL") == 0 ||
strcmp(keyword, "INNER") == 0 ||
strcmp(keyword, "JOIN") == 0 ||
strcmp(keyword, "LOG") == 0 ||
strcmp(keyword, "LEFT") == 0 ||
strcmp(keyword, "NATURAL") == 0 ||
strcmp(keyword, "RETURN") == 0 ||
strcmp(keyword, "RIGHT") == 0 ||
strcmp(keyword, "RETURNING") == 0) {
ret = true;
}
} else if (strcmp(keyword, "EXCEPT") == 0 ||
strcmp(keyword, "INTERSECT") == 0 ||
strcmp(keyword, "MINUS") == 0 ||
strcmp(keyword, "MEMBER") == 0 ||
strcmp(keyword, "SOUNDS") == 0 ||
strcmp(keyword, "WINDOW") == 0) {
ret = true;
}
} else {
if (test::clp.sql_mode & SMO_ORACLE) {
if (strcmp(keyword, "CHARACTER") == 0 ||
strcmp(keyword, "CONSTANT") == 0 ||
strcmp(keyword, "DATE") == 0 ||
strcmp(keyword, "FLOAT") == 0 ||
strcmp(keyword, "LOOP") == 0 ||
strcmp(keyword, "NUMBER") == 0 ||
strcmp(keyword, "RAW") == 0 ||
strcmp(keyword, "REAL") == 0 ||
strcmp(keyword, "VARCHAR") == 0 ||
strcmp(keyword, "VARCHAR2") == 0) {
ret = true;
}
}
}
return ret;
}
void TestParser::do_parse(const char *query_str, std::ofstream &of_result, int64_t expect_error) {
ObSQLMode mode = test::clp.sql_mode;
ObParser parser(allocator_, mode);
ParseResult parse_result;
ObString query = ObString::make_string(query_str);
int ret = OB_SUCCESS;
_OB_LOG(INFO, "QUERY: %s", query_str);
ret = parser.parse(query, parse_result);
if(expect_error != -ret) {
fprintf(stderr, "QUERY:%s failed\n", query_str);
}
ASSERT_EQ(expect_error, -ret);
if (NULL != parse_result.result_tree_){
_OB_LOG(INFO, "%s", CSJ(ObParserResultPrintWrapper(*parse_result.result_tree_)));
of_result << "question_mask_size: " << parse_result.result_tree_->children_[0]->value_ << std::endl;
of_result << CSJ(ObParserResultPrintWrapper(*parse_result.result_tree_)) << std::endl;
}
parser.free_result(parse_result);
}
void TestParser::do_filter_hint(const char *query_str, std::ofstream &of_result, int64_t expect_error)
{
ObString real_query = ObString::make_string(query_str).trim();
if (real_query.length() > 0 && *real_query.ptr() != '#') {
//ignore empty query and comment
_OB_LOG(INFO, "query_str: %s", query_str);
int ret = OB_SUCCESS;
ObString result_query;
if (OB_FAIL(ObSQLUtils::filter_hint_in_query_sql(allocator_,
session_info_,
real_query,
result_query))) {
_OB_LOG(INFO, "query_str: %s", query_str);
if(expect_error != -ret) {
fprintf(stderr, "QUERY:%s failed\n", query_str);
}
ASSERT_EQ(expect_error, -ret);
}
if (expect_error == 0) {
char buffer[OB_MAX_SQL_LENGTH];
memset(buffer, '\0', OB_MAX_SQL_LENGTH);
memcpy(buffer, result_query.ptr(), result_query.length());
of_result << buffer << std::endl;
}
}
}
bool TestParser::pretreat_cmd(std::string line, int64_t &expect_error)
{
bool skip_cmd = false;
char *w = NULL;
char *p = NULL;
if (line.size() <= 0) skip_cmd = true;
else if (line.at(0) == '#') skip_cmd = true;
else if (strncmp(line.c_str(), "--error", strlen("--error")) == 0) {
p = const_cast<char*>(line.c_str());
w = strsep(&p, " ");
expect_error = atol(p);
skip_cmd = true;
} else if (strncmp(line.c_str(), "--sql_mode", strlen("--sql_mode")) == 0) {
p = const_cast<char*>(line.c_str());
w = strsep(&p, " ");
if (strncmp(p, "oracle", strlen("oracle")) == 0) {
OB_LOG(INFO, "switch parser sql_mode to oracle");
test::clp.sql_mode = DEFAULT_ORACLE_MODE | SMO_ORACLE;
set_compat_mode(oceanbase::lib::Worker::CompatMode::ORACLE);
} else if (strncmp(p, "mysql", strlen("mysql")) == 0) {
OB_LOG(INFO, "switch parser sql_mode to mysql");
test::clp.sql_mode = DEFAULT_MYSQL_MODE;
set_compat_mode(oceanbase::lib::Worker::CompatMode::MYSQL);
}
skip_cmd = true;
UNUSED(w);
}
return skip_cmd;
}
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc,argv);
OB_LOGGER.set_log_level("INFO");
OB_LOGGER.set_file_name("test_parser.log", true);
test::parse_cmd_line_param(argc, argv, test::clp);
return RUN_ALL_TESTS();
}