init push
This commit is contained in:
120
unittest/sql/resolver/expr/test_raw_expr_canonicalizer.cpp
Normal file
120
unittest/sql/resolver/expr/test_raw_expr_canonicalizer.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OceanBase
|
||||
* OceanBase CE is licensed under Mulan PubL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PubL v2.
|
||||
* You may obtain a copy of Mulan PubL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPubL-2.0
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PubL v2 for more details.
|
||||
*/
|
||||
|
||||
#include "sql/test_sql_utils.h"
|
||||
#include "sql/resolver/expr/ob_raw_expr_canonicalizer_impl.h"
|
||||
#include <gtest/gtest.h>
|
||||
#include "lib/utility/ob_test_util.h"
|
||||
#include "sql/resolver/expr/ob_raw_expr_util.h"
|
||||
#include "sql/resolver/ob_stmt.h"
|
||||
#include "sql/ob_sql_context.h"
|
||||
#include "lib/json/ob_json_print_utils.h"
|
||||
#include <fstream>
|
||||
#include <iterator>
|
||||
using namespace oceanbase::common;
|
||||
using namespace oceanbase::sql;
|
||||
|
||||
class TestRawExprCanonicalizer : public ::testing::Test {
|
||||
public:
|
||||
TestRawExprCanonicalizer();
|
||||
virtual ~TestRawExprCanonicalizer();
|
||||
virtual void SetUp();
|
||||
virtual void TearDown();
|
||||
|
||||
private:
|
||||
// disallow copy
|
||||
DISALLOW_COPY_AND_ASSIGN(TestRawExprCanonicalizer);
|
||||
|
||||
protected:
|
||||
// function members
|
||||
void canon(const char* expr, const char*& canon_expr);
|
||||
|
||||
protected:
|
||||
// data members
|
||||
};
|
||||
|
||||
TestRawExprCanonicalizer::TestRawExprCanonicalizer()
|
||||
{}
|
||||
|
||||
TestRawExprCanonicalizer::~TestRawExprCanonicalizer()
|
||||
{}
|
||||
|
||||
void TestRawExprCanonicalizer::SetUp()
|
||||
{}
|
||||
|
||||
void TestRawExprCanonicalizer::TearDown()
|
||||
{}
|
||||
|
||||
void TestRawExprCanonicalizer::canon(const char* expr, const char*& canon_expr)
|
||||
{
|
||||
ObArray<ObQualifiedName> columns;
|
||||
ObArray<ObVarInfo> sys_vars;
|
||||
ObArray<ObSubQueryInfo> sub_query_info;
|
||||
ObArray<ObAggFunRawExpr*> aggr_exprs;
|
||||
ObArray<ObWinFunRawExpr*> win_exprs;
|
||||
const char* expr_str = expr;
|
||||
ObArenaAllocator allocator(ObModIds::TEST);
|
||||
ObRawExprFactory expr_factory(allocator);
|
||||
ObTimeZoneInfo tz_info;
|
||||
ObStmt stmt;
|
||||
ObQueryCtx query_ctx;
|
||||
ObNameCaseMode case_mode = OB_NAME_CASE_INVALID;
|
||||
ObExprResolveContext ctx(expr_factory, &tz_info, case_mode);
|
||||
stmt.query_ctx_ = &query_ctx;
|
||||
ctx.connection_charset_ = ObCharset::get_default_charset();
|
||||
ctx.dest_collation_ = ObCharset::get_default_collation(ctx.connection_charset_);
|
||||
ctx.stmt_ = &stmt;
|
||||
ObSQLSessionInfo session;
|
||||
session.set_use_static_typing_engine(false);
|
||||
ctx.session_info_ = &session;
|
||||
ObRawExpr* raw_expr = NULL;
|
||||
OK(ObRawExprUtils::make_raw_expr_from_str(
|
||||
expr_str, strlen(expr_str), ctx, raw_expr, columns, sys_vars, &sub_query_info, aggr_exprs, win_exprs));
|
||||
_OB_LOG(DEBUG, "================================================================");
|
||||
_OB_LOG(DEBUG, "%s", expr);
|
||||
_OB_LOG(DEBUG, "%s", CSJ(raw_expr));
|
||||
ObRawExprCanonicalizerImpl canon(ctx);
|
||||
OK(canon.canonicalize(raw_expr));
|
||||
canon_expr = CSJ(raw_expr);
|
||||
_OB_LOG(DEBUG, "canon_expr=%s", canon_expr);
|
||||
}
|
||||
|
||||
TEST_F(TestRawExprCanonicalizer, basic_test)
|
||||
{
|
||||
std::ifstream if_tests("./expr/test_raw_expr_canonicalizer.test");
|
||||
ASSERT_TRUE(if_tests.is_open());
|
||||
std::string line;
|
||||
const char* canon_expr = NULL;
|
||||
std::ofstream of_result("./expr/test_raw_expr_canonicalizer.tmp");
|
||||
ASSERT_TRUE(of_result.is_open());
|
||||
while (std::getline(if_tests, line)) {
|
||||
of_result << line << std::endl;
|
||||
canon(line.c_str(), canon_expr);
|
||||
of_result << canon_expr << std::endl;
|
||||
}
|
||||
of_result.close();
|
||||
|
||||
std::ifstream if_result("./expr/test_raw_expr_canonicalizer.tmp");
|
||||
ASSERT_TRUE(if_result.is_open());
|
||||
std::istream_iterator<std::string> it_result(if_result);
|
||||
std::ifstream if_expected("./expr/test_raw_expr_canonicalizer.result");
|
||||
ASSERT_TRUE(if_expected.is_open());
|
||||
std::istream_iterator<std::string> it_expected(if_expected);
|
||||
ASSERT_TRUE(std::equal(it_result, std::istream_iterator<std::string>(), it_expected));
|
||||
std::remove("./test_raw_expr_canonicalizer.tmp");
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
2030
unittest/sql/resolver/expr/test_raw_expr_canonicalizer.result
Normal file
2030
unittest/sql/resolver/expr/test_raw_expr_canonicalizer.result
Normal file
File diff suppressed because it is too large
Load Diff
15
unittest/sql/resolver/expr/test_raw_expr_canonicalizer.test
Normal file
15
unittest/sql/resolver/expr/test_raw_expr_canonicalizer.test
Normal file
@ -0,0 +1,15 @@
|
||||
not (10 > 5 and 100 < 9)
|
||||
not not a > b
|
||||
not a > b
|
||||
not a = b
|
||||
not a < b
|
||||
not a is true
|
||||
not a between 1 and 100
|
||||
(1 and 2) and (3 and 4) and 5
|
||||
(1 or 2) or (3 or 4 or 5)
|
||||
(1 and 2) or (3 and 1)
|
||||
not(not(a))
|
||||
A or (A And B) or (A And C)
|
||||
(A and B) or (A and C and D)
|
||||
(A and B) or (A and C)
|
||||
(A and B and C) or (A and B and D)
|
||||
105
unittest/sql/resolver/expr/test_raw_expr_hash.cpp
Normal file
105
unittest/sql/resolver/expr/test_raw_expr_hash.cpp
Normal file
@ -0,0 +1,105 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OceanBase
|
||||
* OceanBase CE is licensed under Mulan PubL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PubL v2.
|
||||
* You may obtain a copy of Mulan PubL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPubL-2.0
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PubL v2 for more details.
|
||||
*/
|
||||
|
||||
#define USING_LOG_PREFIX SQL_OPTIMIZER
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "lib/json/ob_json.h"
|
||||
#include "lib/allocator/ob_mod_define.h"
|
||||
#include "lib/allocator/page_arena.h"
|
||||
#include "lib/oblog/ob_log.h"
|
||||
#include "sql/resolver/expr/ob_raw_expr_util.h"
|
||||
#include "sql/engine/expr/ob_expr_operator.h"
|
||||
|
||||
using namespace oceanbase::sql;
|
||||
using namespace oceanbase::common;
|
||||
namespace test {
|
||||
|
||||
#define MAKE_RAW_EXPR_FROM_STR(str, expr) \
|
||||
({ \
|
||||
ret = ObRawExprUtils::make_raw_expr_from_str( \
|
||||
str, strlen(str), ctx, expr, columns, sys_vars, &sub_query_info, aggr_exprs, win_exprs); \
|
||||
ret; \
|
||||
})
|
||||
|
||||
class TestRawExprToStr : public ::testing::Test {
|
||||
public:
|
||||
TestRawExprToStr()
|
||||
{}
|
||||
virtual ~TestRawExprToStr()
|
||||
{}
|
||||
virtual void SetUp()
|
||||
{}
|
||||
virtual void TearDown()
|
||||
{}
|
||||
|
||||
private:
|
||||
// disallow copy and assign
|
||||
TestRawExprToStr(const TestRawExprToStr& other);
|
||||
TestRawExprToStr& operator=(const TestRawExprToStr& ohter);
|
||||
};
|
||||
#define T(expr1) \
|
||||
do { \
|
||||
MAKE_RAW_EXPR_FROM_STR(expr1, expr); \
|
||||
EXPECT_TRUE(OB_SUCC(ret)); \
|
||||
uint64_t hash = expr->hash(0); \
|
||||
_OB_LOG(INFO, "hash(%s) = %lu", expr1, hash); \
|
||||
} while (0)
|
||||
TEST_F(TestRawExprToStr, basic)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
// ObExprOperatorGFactory::get_instance()->init();
|
||||
// mock params
|
||||
// stmts
|
||||
ObArenaAllocator allocator(ObModIds::TEST);
|
||||
ObRawExprFactory expr_factory(allocator);
|
||||
ObArray<ObQualifiedName> columns;
|
||||
ObArray<ObVarInfo> sys_vars;
|
||||
ObArray<ObSubQueryInfo> sub_query_info;
|
||||
ObArray<ObAggFunRawExpr*> aggr_exprs;
|
||||
ObArray<ObWinFunRawExpr*> win_exprs;
|
||||
ObTimeZoneInfo tz_info;
|
||||
ObNameCaseMode case_mode = OB_NAME_CASE_INVALID;
|
||||
ObExprResolveContext ctx(expr_factory, &tz_info, case_mode);
|
||||
ctx.connection_charset_ = ObCharset::get_default_charset();
|
||||
ctx.dest_collation_ = ObCharset::get_default_collation(ctx.connection_charset_);
|
||||
ctx.is_extract_param_type_ = false;
|
||||
ObSQLSessionInfo session;
|
||||
session.set_use_static_typing_engine(false);
|
||||
ctx.session_info_ = &session;
|
||||
|
||||
// const int64_t buf_len = 1024;
|
||||
// int64_t pos = 0;
|
||||
// char buf[buf_len];
|
||||
|
||||
ObRawExpr* expr = NULL;
|
||||
// const char* inner_offset = "1+c1 > ? and 'abc' || c2 = 'def'";
|
||||
// const char* expr1 = "1+c1 > ? and SUM(1) OR 2 >= 1";
|
||||
// const char* expr2 = "CASE WHEN 10>=2 THEN 1+2 ELSE 0 END";
|
||||
// const char* expr3 = "CASE WHEN 10>=2 THEN 10-2 ELSE SUM(10-2) END";
|
||||
|
||||
T("1+c1 > ? and SUM(1) OR 2 >= 1");
|
||||
T("CASE WHEN 10>=2 THEN 1+2 ELSE 0 END");
|
||||
T("1");
|
||||
T("100");
|
||||
T("sum(1)");
|
||||
T("c1");
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
oceanbase::common::ObLogger::get_logger().set_log_level("INFO");
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
172
unittest/sql/resolver/expr/test_raw_expr_print_visitor.cpp
Normal file
172
unittest/sql/resolver/expr/test_raw_expr_print_visitor.cpp
Normal file
@ -0,0 +1,172 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OceanBase
|
||||
* OceanBase CE is licensed under Mulan PubL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PubL v2.
|
||||
* You may obtain a copy of Mulan PubL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPubL-2.0
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PubL v2 for more details.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "lib/utility/ob_test_util.h"
|
||||
#include "sql/resolver/expr/ob_raw_expr_print_visitor.h"
|
||||
|
||||
using namespace oceanbase::common;
|
||||
using namespace oceanbase::sql;
|
||||
|
||||
class TestRawExprPrintVisitor : public ::testing::Test {
|
||||
public:
|
||||
TestRawExprPrintVisitor();
|
||||
virtual ~TestRawExprPrintVisitor();
|
||||
virtual void SetUp();
|
||||
virtual void TearDown();
|
||||
|
||||
private:
|
||||
// disallow copy
|
||||
DISALLOW_COPY_AND_ASSIGN(TestRawExprPrintVisitor);
|
||||
|
||||
protected:
|
||||
// function members
|
||||
protected:
|
||||
// data members
|
||||
};
|
||||
|
||||
TestRawExprPrintVisitor::TestRawExprPrintVisitor()
|
||||
{}
|
||||
|
||||
TestRawExprPrintVisitor::~TestRawExprPrintVisitor()
|
||||
{}
|
||||
|
||||
void TestRawExprPrintVisitor::SetUp()
|
||||
{}
|
||||
|
||||
void TestRawExprPrintVisitor::TearDown()
|
||||
{}
|
||||
|
||||
TEST_F(TestRawExprPrintVisitor, const_test)
|
||||
{
|
||||
{
|
||||
ObObj obj;
|
||||
obj.set_int(123);
|
||||
ObConstRawExpr expr(obj, T_INT);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
{
|
||||
ObString str = ObString::make_string("abcd");
|
||||
ObObj obj;
|
||||
obj.set_varchar(str);
|
||||
ObConstRawExpr expr(obj, T_VARCHAR);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
{
|
||||
ObObj obj;
|
||||
obj.set_null();
|
||||
ObConstRawExpr expr(obj, T_NULL);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
{
|
||||
ObObj obj;
|
||||
obj.set_int(3);
|
||||
ObConstRawExpr expr(obj, T_QUESTIONMARK);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
{
|
||||
number::ObNumber nmb;
|
||||
ObArenaAllocator allocator(ObModIds::TEST);
|
||||
nmb.from(9000000000L, allocator);
|
||||
ObObj obj;
|
||||
obj.set_number(nmb);
|
||||
ObConstRawExpr expr(obj, T_NUMBER);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
{
|
||||
ObString var = ObString::make_string("sql_mode");
|
||||
ObObj obj;
|
||||
obj.set_varchar(var);
|
||||
ObConstRawExpr expr(obj, T_SYSTEM_VARIABLE);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(TestRawExprPrintVisitor, unary_ref_test)
|
||||
{
|
||||
ObQueryRefRawExpr expr(1, T_REF_QUERY);
|
||||
_OB_LOG(INFO, "unary=%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
|
||||
TEST_F(TestRawExprPrintVisitor, binary_ref_test)
|
||||
{
|
||||
ObColumnRefRawExpr expr(3, 7, T_REF_COLUMN);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
|
||||
TEST_F(TestRawExprPrintVisitor, multi_op_test)
|
||||
{
|
||||
ObObj obj;
|
||||
obj.set_int(123);
|
||||
ObConstRawExpr const_expr1(obj, T_INT);
|
||||
ObConstRawExpr const_expr2(obj, T_INT);
|
||||
ObConstRawExpr const_expr3(obj, T_INT);
|
||||
ObConstRawExpr const_expr4(obj, T_INT);
|
||||
ObOpRawExpr expr;
|
||||
expr.set_expr_type(T_OP_ROW);
|
||||
OK(expr.add_param_expr(&const_expr1));
|
||||
OK(expr.add_param_expr(&const_expr2));
|
||||
OK(expr.add_param_expr(&const_expr3));
|
||||
OK(expr.add_param_expr(&const_expr4));
|
||||
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
|
||||
TEST_F(TestRawExprPrintVisitor, case_op_test)
|
||||
{
|
||||
ObObj obj;
|
||||
obj.set_int(123);
|
||||
ObConstRawExpr const_expr1(obj, T_INT);
|
||||
ObConstRawExpr const_expr2(obj, T_INT);
|
||||
ObConstRawExpr const_expr3(obj, T_INT);
|
||||
ObConstRawExpr const_expr4(obj, T_INT);
|
||||
|
||||
ObCaseOpRawExpr expr;
|
||||
expr.set_arg_param_expr(&const_expr1);
|
||||
OK(expr.add_when_param_expr(&const_expr2));
|
||||
OK(expr.add_then_param_expr(&const_expr3));
|
||||
expr.set_default_param_expr(&const_expr4);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
|
||||
TEST_F(TestRawExprPrintVisitor, agg_op_test)
|
||||
{
|
||||
ObObj obj;
|
||||
obj.set_int(123);
|
||||
ObConstRawExpr const_expr1(obj, T_INT);
|
||||
ObSEArray<ObRawExpr*, 1, ModulePageAllocator, true> real_param_exprs1;
|
||||
OK(real_param_exprs1.push_back(&const_expr1));
|
||||
ObAggFunRawExpr expr(real_param_exprs1, true, T_FUN_MAX);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
ObSEArray<ObRawExpr*, 1, ModulePageAllocator, true> real_param_exprs2;
|
||||
ObAggFunRawExpr expr2(real_param_exprs2, false, T_FUN_COUNT);
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr2)));
|
||||
}
|
||||
|
||||
TEST_F(TestRawExprPrintVisitor, sys_fun_test)
|
||||
{
|
||||
ObObj obj;
|
||||
obj.set_int(123);
|
||||
ObConstRawExpr const_expr1(obj, T_INT);
|
||||
ObConstRawExpr const_expr2(obj, T_INT);
|
||||
ObSysFunRawExpr expr;
|
||||
expr.set_func_name(ObString::make_string("myfunc"));
|
||||
OK(expr.add_param_expr(&const_expr1));
|
||||
OK(expr.add_param_expr(&const_expr2));
|
||||
_OB_LOG(INFO, "%s", S(ObRawExprPrintVisitor(expr)));
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
134
unittest/sql/resolver/expr/test_raw_expr_resolver.cpp
Normal file
134
unittest/sql/resolver/expr/test_raw_expr_resolver.cpp
Normal file
@ -0,0 +1,134 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OceanBase
|
||||
* OceanBase CE is licensed under Mulan PubL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PubL v2.
|
||||
* You may obtain a copy of Mulan PubL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPubL-2.0
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PubL v2 for more details.
|
||||
*/
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "sql/test_sql_utils.h"
|
||||
#include "lib/utility/ob_test_util.h"
|
||||
#include "sql/resolver/expr/ob_raw_expr_resolver_impl.h"
|
||||
#include "sql/resolver/expr/ob_raw_expr_util.h"
|
||||
#include "sql/resolver/expr/ob_raw_expr_print_visitor.h"
|
||||
#include "sql/ob_sql_init.h"
|
||||
#include "lib/json/ob_json_print_utils.h"
|
||||
#include <fstream>
|
||||
using namespace oceanbase::common;
|
||||
using namespace oceanbase::sql;
|
||||
using namespace oceanbase::lib;
|
||||
using namespace oceanbase;
|
||||
|
||||
class TestRawExprResolver : public ::testing::Test {
|
||||
public:
|
||||
TestRawExprResolver();
|
||||
virtual ~TestRawExprResolver();
|
||||
virtual void SetUp();
|
||||
virtual void TearDown();
|
||||
|
||||
private:
|
||||
// disallow copy
|
||||
DISALLOW_COPY_AND_ASSIGN(TestRawExprResolver);
|
||||
|
||||
protected:
|
||||
// function members
|
||||
void resolve(const char* expr, const char*& json_expr);
|
||||
|
||||
protected:
|
||||
// data members
|
||||
};
|
||||
|
||||
TestRawExprResolver::TestRawExprResolver()
|
||||
{}
|
||||
|
||||
TestRawExprResolver::~TestRawExprResolver()
|
||||
{}
|
||||
|
||||
void TestRawExprResolver::SetUp()
|
||||
{}
|
||||
|
||||
void TestRawExprResolver::TearDown()
|
||||
{}
|
||||
|
||||
void TestRawExprResolver::resolve(const char* expr, const char*& json_expr)
|
||||
{
|
||||
ObArray<ObQualifiedName> columns;
|
||||
ObArray<ObVarInfo> sys_vars;
|
||||
ObArray<ObSubQueryInfo> sub_query_info;
|
||||
ObArray<ObAggFunRawExpr*> aggr_exprs;
|
||||
ObArray<ObWinFunRawExpr*> win_exprs;
|
||||
const char* expr_str = expr;
|
||||
ObIAllocator& allocator = CURRENT_CONTEXT.get_arena_allocator();
|
||||
ObRawExprFactory expr_factory(allocator);
|
||||
ObTimeZoneInfo tz_info;
|
||||
ObNameCaseMode case_mode = OB_NAME_CASE_INVALID;
|
||||
ObExprResolveContext ctx(expr_factory, &tz_info, case_mode);
|
||||
ctx.connection_charset_ = ObCharset::get_default_charset();
|
||||
ctx.dest_collation_ = ObCharset::get_default_collation(ctx.connection_charset_);
|
||||
ctx.is_extract_param_type_ = false;
|
||||
ObSQLSessionInfo session;
|
||||
session.set_use_static_typing_engine(false);
|
||||
ctx.session_info_ = &session;
|
||||
ObRawExpr* raw_expr = NULL;
|
||||
OK(ObRawExprUtils::make_raw_expr_from_str(
|
||||
expr_str, strlen(expr_str), ctx, raw_expr, columns, sys_vars, &sub_query_info, aggr_exprs, win_exprs));
|
||||
_OB_LOG(DEBUG, "================================================================");
|
||||
_OB_LOG(DEBUG, "%s", expr);
|
||||
_OB_LOG(DEBUG, "%s", CSJ(raw_expr));
|
||||
OK(raw_expr->extract_info());
|
||||
// OK(raw_expr->deduce_type());
|
||||
json_expr = CSJ(raw_expr);
|
||||
}
|
||||
|
||||
TEST_F(TestRawExprResolver, all)
|
||||
{
|
||||
static const char* test_file = "./expr/test_raw_expr_resolver.test";
|
||||
static const char* tmp_file = "./expr/test_raw_expr_resolver.tmp";
|
||||
static const char* result_file = "./expr/test_raw_expr_resolver.result";
|
||||
|
||||
std::ifstream if_tests(test_file);
|
||||
ASSERT_TRUE(if_tests.is_open());
|
||||
std::string line;
|
||||
const char* json_expr = NULL;
|
||||
std::ofstream of_result(tmp_file);
|
||||
ASSERT_TRUE(of_result.is_open());
|
||||
int64_t case_id = 0;
|
||||
while (std::getline(if_tests, line)) {
|
||||
of_result << '[' << case_id++ << "] " << line << std::endl;
|
||||
resolve(line.c_str(), json_expr);
|
||||
of_result << json_expr << std::endl;
|
||||
}
|
||||
of_result.close();
|
||||
// verify results
|
||||
fprintf(stderr, "If tests failed, use `diff %s %s' to see the differences. \n", result_file, tmp_file);
|
||||
std::ifstream if_result(tmp_file);
|
||||
ASSERT_TRUE(if_result.is_open());
|
||||
std::istream_iterator<std::string> it_result(if_result);
|
||||
std::ifstream if_expected(result_file);
|
||||
ASSERT_TRUE(if_expected.is_open());
|
||||
std::istream_iterator<std::string> it_expected(if_expected);
|
||||
ASSERT_TRUE(std::equal(it_result, std::istream_iterator<std::string>(), it_expected));
|
||||
std::remove(tmp_file);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int ret = 0;
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
system("rm -rf test_optimizer.log");
|
||||
OB_LOGGER.set_log_level("INFO");
|
||||
OB_LOGGER.set_file_name("test_raw_expr_resolver.log", true);
|
||||
ContextParam param;
|
||||
param.set_mem_attr(1001, 0, ObCtxIds::WORK_AREA).set_page_size(OB_MALLOC_BIG_BLOCK_SIZE);
|
||||
init_sql_factories();
|
||||
CREATE_WITH_TEMP_CONTEXT(param)
|
||||
{
|
||||
ret = RUN_ALL_TESTS();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
5025
unittest/sql/resolver/expr/test_raw_expr_resolver.result
Normal file
5025
unittest/sql/resolver/expr/test_raw_expr_resolver.result
Normal file
File diff suppressed because it is too large
Load Diff
36
unittest/sql/resolver/expr/test_raw_expr_resolver.test
Normal file
36
unittest/sql/resolver/expr/test_raw_expr_resolver.test
Normal file
@ -0,0 +1,36 @@
|
||||
1 like 1.0
|
||||
'1' like '1'
|
||||
2 in (1, 2, 3)
|
||||
2 not in (1, 2, 3)
|
||||
1 or 1
|
||||
100 and 0
|
||||
100 or 0 and 100
|
||||
case name when 'sam' then 'yong' when 'lee' then 'handsome' else 'good' end
|
||||
case when 1>0 then 'true' else 'false' end
|
||||
now()
|
||||
utc_timestamp()
|
||||
floor(3.14)
|
||||
count(*)
|
||||
max(c1)
|
||||
c1 in (select t1 from table1)
|
||||
1=(select c1 from table1 where c2>20)
|
||||
(t1,t2)=(select c1, c2 from table1)
|
||||
10
|
||||
@@auto_increment_increment
|
||||
@a
|
||||
c1
|
||||
10+10
|
||||
not 10
|
||||
c1+c2=c3+c4
|
||||
c1=3
|
||||
c1>1
|
||||
c1 > c2
|
||||
c1 in (1,2,3,4)
|
||||
c1 between 2 and 10
|
||||
10+10+c1
|
||||
c1 = 1 + 2
|
||||
c1 between 10+10 and 30
|
||||
1+c1 > ? and 'abc' || c2 = 'def'
|
||||
binary binary 'abc' collate utf8_general_ci collate utf8_bin
|
||||
X not like Y
|
||||
Z not regexp A
|
||||
120
unittest/sql/resolver/expr/test_raw_expr_to_str.cpp
Normal file
120
unittest/sql/resolver/expr/test_raw_expr_to_str.cpp
Normal file
@ -0,0 +1,120 @@
|
||||
/**
|
||||
* Copyright (c) 2021 OceanBase
|
||||
* OceanBase CE is licensed under Mulan PubL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PubL v2.
|
||||
* You may obtain a copy of Mulan PubL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPubL-2.0
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PubL v2 for more details.
|
||||
*/
|
||||
|
||||
#define USING_LOG_PREFIX SQL_OPTIMIZER
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include "lib/json/ob_json.h"
|
||||
#include "lib/allocator/ob_mod_define.h"
|
||||
#include "lib/allocator/page_arena.h"
|
||||
#include "lib/oblog/ob_log.h"
|
||||
#include "sql/resolver/expr/ob_raw_expr_util.h"
|
||||
#include "sql/engine/expr/ob_expr_operator.h"
|
||||
|
||||
using namespace oceanbase::sql;
|
||||
using namespace oceanbase::common;
|
||||
namespace test {
|
||||
|
||||
#define MAKE_RAW_EXPR_FROM_STR(str, expr) \
|
||||
({ \
|
||||
ret = ObRawExprUtils::make_raw_expr_from_str( \
|
||||
str, strlen(str), ctx, expr, columns, sys_vars, &sub_query_info, aggr_exprs, win_exprs); \
|
||||
ret; \
|
||||
})
|
||||
|
||||
class TestRawExprToStr : public ::testing::Test {
|
||||
public:
|
||||
TestRawExprToStr()
|
||||
{}
|
||||
virtual ~TestRawExprToStr()
|
||||
{}
|
||||
virtual void SetUp()
|
||||
{}
|
||||
virtual void TearDown()
|
||||
{}
|
||||
|
||||
private:
|
||||
// disallow copy and assign
|
||||
TestRawExprToStr(const TestRawExprToStr& other);
|
||||
TestRawExprToStr& operator=(const TestRawExprToStr& ohter);
|
||||
};
|
||||
|
||||
TEST_F(TestRawExprToStr, basic)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
// mock params
|
||||
// stmts
|
||||
ObArenaAllocator allocator(ObModIds::TEST);
|
||||
ObRawExprFactory expr_factory(allocator);
|
||||
ObArray<ObQualifiedName> columns;
|
||||
ObArray<ObVarInfo> sys_vars;
|
||||
ObArray<ObSubQueryInfo> sub_query_info;
|
||||
ObArray<ObAggFunRawExpr*> aggr_exprs;
|
||||
ObArray<ObWinFunRawExpr*> win_exprs;
|
||||
ObTimeZoneInfo tz_info;
|
||||
ObNameCaseMode case_mode = OB_NAME_CASE_INVALID;
|
||||
ObExprResolveContext ctx(expr_factory, &tz_info, case_mode);
|
||||
ctx.connection_charset_ = ObCharset::get_default_charset();
|
||||
ctx.dest_collation_ = ObCharset::get_default_collation(ctx.connection_charset_);
|
||||
ctx.is_extract_param_type_ = false;
|
||||
ObSQLSessionInfo session;
|
||||
session.set_use_static_typing_engine(false);
|
||||
ctx.session_info_ = &session;
|
||||
|
||||
const int64_t buf_len = 1024;
|
||||
int64_t pos = 0;
|
||||
char buf[buf_len];
|
||||
|
||||
ObRawExpr* expr = NULL;
|
||||
// const char* inner_offset = "1+c1 > ? and 'abc' || c2 = 'def'";
|
||||
const char* expr1 = "1+c1 > ? and SUM(1) OR 2 >= 1";
|
||||
const char* expr2 = "CASE WHEN 10>=2 THEN 1+2 ELSE 0 END";
|
||||
const char* expr3 = "CASE WHEN 10>=2 THEN 10-2 ELSE SUM(10-2) END";
|
||||
const char* expr4 = "1/2";
|
||||
|
||||
MAKE_RAW_EXPR_FROM_STR(expr1, expr);
|
||||
EXPECT_TRUE(OB_SUCC(ret));
|
||||
pos = 0;
|
||||
ret = expr->get_name(buf, buf_len, pos);
|
||||
EXPECT_TRUE(OB_SUCC(ret));
|
||||
_OB_LOG(INFO, "%.*s", static_cast<int32_t>(pos), buf);
|
||||
|
||||
pos = 0;
|
||||
MAKE_RAW_EXPR_FROM_STR(expr2, expr);
|
||||
EXPECT_TRUE(OB_SUCC(ret));
|
||||
ret = expr->get_name(buf, buf_len, pos);
|
||||
EXPECT_TRUE(OB_SUCC(ret));
|
||||
_OB_LOG(INFO, "%.*s", static_cast<int32_t>(pos), buf);
|
||||
|
||||
pos = 0;
|
||||
MAKE_RAW_EXPR_FROM_STR(expr3, expr);
|
||||
EXPECT_TRUE(OB_SUCC(ret));
|
||||
ret = expr->get_name(buf, buf_len, pos);
|
||||
EXPECT_TRUE(OB_SUCC(ret));
|
||||
_OB_LOG(INFO, "%.*s", static_cast<int32_t>(pos), buf);
|
||||
|
||||
pos = 0;
|
||||
MAKE_RAW_EXPR_FROM_STR(expr4, expr);
|
||||
EXPECT_TRUE(OB_SUCC(ret));
|
||||
ret = expr->get_name(buf, buf_len, pos);
|
||||
EXPECT_TRUE(OB_SUCC(ret));
|
||||
_OB_LOG(INFO, "%.*s", static_cast<int32_t>(pos), buf);
|
||||
}
|
||||
|
||||
} // namespace test
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
oceanbase::common::ObLogger::get_logger().set_log_level("INFO");
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
7663
unittest/sql/resolver/expr/test_raw_op_expr.result
Normal file
7663
unittest/sql/resolver/expr/test_raw_op_expr.result
Normal file
File diff suppressed because it is too large
Load Diff
59
unittest/sql/resolver/expr/test_raw_op_expr.test
Normal file
59
unittest/sql/resolver/expr/test_raw_op_expr.test
Normal file
@ -0,0 +1,59 @@
|
||||
c1 = 1 + 2
|
||||
c1 = 1 - 2
|
||||
c1 = 1 * 2
|
||||
c1 = 1 / 2
|
||||
c1 = 1 % 2
|
||||
c1 = 1 div 2
|
||||
c1 = (1 <= 2)
|
||||
c1 = (1 < 2)
|
||||
c1 = 2
|
||||
c1 = (1 >= 2)
|
||||
c1 = (1 > 2)
|
||||
c1 = (1 != 2)
|
||||
c1 = (1 and 0)
|
||||
c1 = (1 or 0)
|
||||
c1 in (select * from t1)
|
||||
c1 = last_insert_id(c1 + 1)
|
||||
c1 = locate("bar", "foobarbar")
|
||||
c1 = locate("bar", "foobarbar", 4)
|
||||
c1 = round(2.55)
|
||||
c1 = round(2.55, 2)
|
||||
-(1.5)
|
||||
-(1)
|
||||
c1 like '//' escape '/'
|
||||
c1 like '//' escape 2
|
||||
c1 like '//' escape '//'
|
||||
c1 like '_'
|
||||
c1 like 2
|
||||
c1 in (select t1 from table1)
|
||||
1=(select c1 from table1 where c2>20)
|
||||
(t1,t2)=(select c1, c2 from table1)
|
||||
exists (select c1 from t1)
|
||||
not (not (not exists (select c1 from t1)))
|
||||
(select c1 from t1 where (c1, c2) in ((1, 2), (2, 3))) in (select c1 from t1)
|
||||
case name when 'sam' then 'yong' when 'lee' then 'handsome' else 'good' end
|
||||
case when 1>0 then 'true' else 'false' end
|
||||
now()
|
||||
utc_timestamp()
|
||||
floor(3.14)
|
||||
count(*)
|
||||
max(c1)
|
||||
c1 in (select t1 from table1)
|
||||
1=(select c1 from table1 where c2>20)
|
||||
(t1,t2)=(select c1, c2 from table1)
|
||||
10
|
||||
@@auto_increment_increment
|
||||
@a
|
||||
c1
|
||||
10+10
|
||||
not 10
|
||||
c1+c2=c3+c4
|
||||
c1=3
|
||||
c1>1
|
||||
c1 > c2
|
||||
c1 in (1,2,3,4)
|
||||
c1 between 2 and 10
|
||||
10+10+c1
|
||||
c1 = 1 + 2
|
||||
c1 between 10+10 and 30
|
||||
1+c1 > ? and 'abc' || c2 = 'def'
|
||||
Reference in New Issue
Block a user