patch 4.0
This commit is contained in:
@ -21,36 +21,37 @@
|
||||
#include <iterator>
|
||||
using namespace oceanbase::common;
|
||||
using namespace oceanbase::sql;
|
||||
namespace test {
|
||||
class TestParser : public TestSqlUtils, public ::testing::Test {
|
||||
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 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);
|
||||
protected:
|
||||
// data members
|
||||
ObArenaAllocator allocator_;
|
||||
};
|
||||
|
||||
TestParser::TestParser() : allocator_(ObModIds::TEST)
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
TestParser::~TestParser()
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
void TestParser::SetUp()
|
||||
{
|
||||
@ -58,7 +59,8 @@ void TestParser::SetUp()
|
||||
}
|
||||
|
||||
void TestParser::TearDown()
|
||||
{}
|
||||
{
|
||||
}
|
||||
|
||||
TEST_F(TestParser, basic_test)
|
||||
{
|
||||
@ -77,7 +79,7 @@ TEST_F(TestParser, basic_test)
|
||||
if (pretreat_cmd(line, expect_error)) {
|
||||
continue;
|
||||
}
|
||||
of_result << "************** Case " << ++case_id << " ***************" << std::endl;
|
||||
of_result << "************** Case "<< ++case_id << " ***************" << std::endl;
|
||||
of_result << line << std::endl;
|
||||
ASSERT_NO_FATAL_FAILURE(do_parse(line.c_str(), of_result, expect_error));
|
||||
if (expect_error != 0) {
|
||||
@ -86,7 +88,7 @@ TEST_F(TestParser, basic_test)
|
||||
}
|
||||
of_result.close();
|
||||
// verify results
|
||||
is_equal_content(tmp_file, result_file);
|
||||
is_equal_content(tmp_file,result_file);
|
||||
}
|
||||
|
||||
TEST_F(TestParser, filter_hint)
|
||||
@ -106,7 +108,7 @@ TEST_F(TestParser, filter_hint)
|
||||
if (pretreat_cmd(line, expect_error)) {
|
||||
continue;
|
||||
}
|
||||
of_result << "************** Case " << ++case_id << " ***************" << std::endl;
|
||||
of_result << "************** Case "<< ++case_id << " ***************" << std::endl;
|
||||
of_result << line << std::endl;
|
||||
do_filter_hint(line.c_str(), of_result, expect_error);
|
||||
if (expect_error != 0) {
|
||||
@ -115,7 +117,7 @@ TEST_F(TestParser, filter_hint)
|
||||
}
|
||||
of_result.close();
|
||||
// verify results
|
||||
is_equal_content(tmp_file, result_file);
|
||||
is_equal_content(tmp_file,result_file);
|
||||
}
|
||||
|
||||
TEST_F(TestParser, print_parser_tree)
|
||||
@ -131,12 +133,15 @@ TEST_F(TestParser, print_parser_tree)
|
||||
std::string line;
|
||||
int64_t case_id = 0;
|
||||
int64_t expect_error = 0;
|
||||
int64_t line_number = 0;
|
||||
while (std::getline(if_tests, line)) {
|
||||
++line_number;
|
||||
if (pretreat_cmd(line, expect_error)) {
|
||||
continue;
|
||||
}
|
||||
of_result << "************** Case " << ++case_id << " ***************" << std::endl;
|
||||
of_result << "************** Case "<< ++case_id << " ***************" << std::endl;
|
||||
of_result << line << std::endl;
|
||||
_OB_LOG(INFO, "line content:%ld, %s", line_number, line.c_str());
|
||||
ASSERT_NO_FATAL_FAILURE(print_parse_tree(line.c_str(), of_result, expect_error));
|
||||
if (expect_error != 0) {
|
||||
expect_error = 0;
|
||||
@ -144,15 +149,17 @@ TEST_F(TestParser, print_parser_tree)
|
||||
}
|
||||
of_result.close();
|
||||
// verify results
|
||||
is_equal_content(tmp_file, result_file);
|
||||
is_equal_content(tmp_file,result_file);
|
||||
}
|
||||
TEST_F(TestParser, pre_parse)
|
||||
{
|
||||
constexpr int STR_NUM = 4;
|
||||
std::string str[] = {"\t/*\ttrace_id=ABC\t*/rpc_id=xxx",
|
||||
"\n/*\ntrace_id=ABC\nrpc_id=xxx*/",
|
||||
"\f/*\ftrace_id=ABC\frpc_id=xxx*/",
|
||||
"\r/*\rtrace_id=ABC\rrpc_id=xxx*/"};
|
||||
std::string str[] = {
|
||||
"\t/*\ttrace_id=ABC\t*/rpc_id=xxx",
|
||||
"\n/*\ntrace_id=ABC\nrpc_id=xxx*/",
|
||||
"\f/*\ftrace_id=ABC\frpc_id=xxx*/",
|
||||
"\r/*\rtrace_id=ABC\rrpc_id=xxx*/"
|
||||
};
|
||||
const char* test_file = "./test_pre_parse.test";
|
||||
const char* result_file = "./test_pre_parse.result";
|
||||
const char* tmp_file = "./test_pre_parse.tmp";
|
||||
@ -163,26 +170,24 @@ TEST_F(TestParser, pre_parse)
|
||||
ASSERT_TRUE(of_result.is_open());
|
||||
std::string line;
|
||||
int64_t case_id = 0;
|
||||
char* w;
|
||||
char* p;
|
||||
char *w;
|
||||
char *p;
|
||||
for (int i = 0; i < STR_NUM; i++) {
|
||||
of_result << "************** Case " << ++case_id << " ***************" << std::endl;
|
||||
of_result << "************** Case "<< ++case_id << " ***************" << std::endl;
|
||||
of_result << str[i] << std::endl;
|
||||
PreParseResult res;
|
||||
ASSERT_EQ(OB_SUCCESS, ObParser::pre_parse(ObString(str[i].length(), str[i].c_str()), res));
|
||||
of_result << "trace_id: " << std::string(res.trace_id_.ptr(), res.trace_id_.length()) << std::endl;
|
||||
}
|
||||
while (std::getline(if_tests, line)) {
|
||||
if (line.size() <= 0)
|
||||
continue;
|
||||
if (line.at(0) == '#')
|
||||
continue;
|
||||
if (line.size() <= 0) continue;
|
||||
if (line.at(0) == '#') continue;
|
||||
if (strncmp(line.c_str(), "--error", strlen("--error")) == 0) {
|
||||
p = const_cast<char*>(line.c_str());
|
||||
w = strsep(&p, " ");
|
||||
continue;
|
||||
}
|
||||
of_result << "************** Case " << ++case_id << " ***************" << std::endl;
|
||||
of_result << "************** Case "<< ++case_id << " ***************" << std::endl;
|
||||
of_result << line << std::endl;
|
||||
PreParseResult res;
|
||||
ASSERT_EQ(OB_SUCCESS, ObParser::pre_parse(ObString(line.length(), line.c_str()), res));
|
||||
@ -190,19 +195,18 @@ TEST_F(TestParser, pre_parse)
|
||||
}
|
||||
UNUSED(w);
|
||||
of_result.close();
|
||||
// verify results
|
||||
is_equal_content(tmp_file, result_file);
|
||||
//verify results
|
||||
is_equal_content(tmp_file,result_file);
|
||||
}
|
||||
|
||||
void TestParser::print_parse_tree(const char* query_str, std::ofstream& of_result, int64_t expect_error)
|
||||
{
|
||||
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) {
|
||||
if(expect_error != -ret) {
|
||||
fprintf(stderr, "QUERY:%s failed\n", query_str);
|
||||
}
|
||||
ASSERT_EQ(expect_error, -ret);
|
||||
@ -214,19 +218,18 @@ void TestParser::print_parse_tree(const char* query_str, std::ofstream& of_resul
|
||||
parser.free_result(parse_result);
|
||||
}
|
||||
|
||||
void TestParser::print_parse_outline(const char* query_str, std::ofstream& of_result, int64_t expect_error)
|
||||
{
|
||||
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) {
|
||||
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;
|
||||
of_result << parse_result.no_param_sql_<<std::endl<<std::endl;
|
||||
parser.free_result(parse_result);
|
||||
}
|
||||
|
||||
@ -247,7 +250,7 @@ TEST_F(TestParser, test_parser_outline)
|
||||
if (pretreat_cmd(line, expect_error)) {
|
||||
continue;
|
||||
}
|
||||
of_result << "************** Case " << ++case_id << " ***************" << std::endl;
|
||||
of_result << "************** Case "<< ++case_id << " ***************" << std::endl;
|
||||
of_result << line << std::endl;
|
||||
ASSERT_NO_FATAL_FAILURE(print_parse_outline(line.c_str(), of_result, expect_error));
|
||||
if (expect_error != 0) {
|
||||
@ -256,11 +259,10 @@ TEST_F(TestParser, test_parser_outline)
|
||||
}
|
||||
of_result.close();
|
||||
// verify results
|
||||
is_equal_content(tmp_file, result_file);
|
||||
is_equal_content(tmp_file,result_file);
|
||||
}
|
||||
|
||||
void TestParser::do_parse(const char* query_str, std::ofstream& of_result, int64_t expect_error)
|
||||
{
|
||||
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;
|
||||
@ -268,11 +270,11 @@ void TestParser::do_parse(const char* query_str, std::ofstream& of_result, int64
|
||||
int ret = OB_SUCCESS;
|
||||
_OB_LOG(INFO, "QUERY: %s", query_str);
|
||||
ret = parser.parse(query, parse_result);
|
||||
if (expect_error != -ret) {
|
||||
if(expect_error != -ret) {
|
||||
fprintf(stderr, "QUERY:%s failed\n", query_str);
|
||||
}
|
||||
ASSERT_EQ(expect_error, -ret);
|
||||
if (NULL != parse_result.result_tree_) {
|
||||
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;
|
||||
@ -280,17 +282,20 @@ void TestParser::do_parse(const char* query_str, std::ofstream& of_result, int64
|
||||
parser.free_result(parse_result);
|
||||
}
|
||||
|
||||
void TestParser::do_filter_hint(const char* query_str, std::ofstream& of_result, int64_t expect_error)
|
||||
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
|
||||
//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))) {
|
||||
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) {
|
||||
if(expect_error != -ret) {
|
||||
fprintf(stderr, "QUERY:%s failed\n", query_str);
|
||||
}
|
||||
ASSERT_EQ(expect_error, -ret);
|
||||
@ -304,15 +309,13 @@ void TestParser::do_filter_hint(const char* query_str, std::ofstream& of_result,
|
||||
}
|
||||
}
|
||||
|
||||
bool TestParser::pretreat_cmd(std::string line, int64_t& expect_error)
|
||||
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;
|
||||
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, " ");
|
||||
@ -333,11 +336,11 @@ bool TestParser::pretreat_cmd(std::string line, int64_t& expect_error)
|
||||
}
|
||||
return skip_cmd;
|
||||
}
|
||||
} // namespace test
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, 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);
|
||||
|
Reference in New Issue
Block a user