forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pcv_set.cpp
More file actions
165 lines (151 loc) · 6.18 KB
/
Copy pathtest_pcv_set.cpp
File metadata and controls
165 lines (151 loc) · 6.18 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
/**
* 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
#include "sql/test_sql_utils.h"
#include <unistd.h>
#include "lib/json/ob_json.h"
#include "test_sql.h"
#include "sql/resolver/ob_resolver_utils.h"
#include "sql/plan_cache/ob_pcv_set.h"
#include "sql/ob_sql_utils.h"
#include "../optimizer/ob_mock_part_mgr.h"
#include <stdio.h>
using namespace oceanbase::common;
using namespace oceanbase::json;
using namespace oceanbase::share::schema;
namespace test
{
static TestSQL *test_sql = NULL;
static ObPlanCache *plan_cache = NULL;
static const int64_t BUKET_NUM = 102400;
void init_pcv_set()
{
if (NULL == test_sql) {
test_sql = new TestSQL(ObString("test_schema.sql"));
ASSERT_TRUE(test_sql);
test_sql->init();
}
if (NULL == plan_cache) {
plan_cache = new ObPlanCache;
plan_cache->init(BUKET_NUM, test_sql->get_addr(), test_sql->get_part_cache(), OB_SYS_TENANT_ID, NULL);
plan_cache->set_mem_limit_pct(20);
plan_cache->set_mem_high_pct(90);
plan_cache->set_mem_low_pct(50);
}
}
void generate_plan(TestSqlCtx &test_sql_ctx, const char *query,
ParamStore ¶ms, ObLogPlan *&logical_plan,
ObPhysicalPlan *&phy_plan, const ObSQLSessionInfo &session)
{
LOG_INFO("generate_plan!");
ParseResult parse_result;
ObStmt *stmt = NULL;
//parse
test_sql->do_parse(*test_sql_ctx.allocator_, query, parse_result);
LOG_INFO("tree", "tree", CSJ(ObParserResultPrintWrapper(*(parse_result.result_tree_))));
//replace const expt with ? in syntax tree; get params;
SqlInfo not_param_info;
ObMaxConcurrentParam::FixParamStore fixed_param_store(OB_MALLOC_NORMAL_BLOCK_SIZE,
ObWrapperAllocator(test_sql_ctx.allocator_));
bool is_transform_outline = false;
ObSqlParameterization::transform_syntax_tree(*test_sql_ctx.allocator_,
session,
NULL,
parse_result.result_tree_,
not_param_info,
params,
NULL,
fixed_param_store,
is_transform_outline);
LOG_INFO("tree", "tree trans", CSJ(ObParserResultPrintWrapper(*(parse_result.result_tree_))));
//resolve
test_sql->do_resolve(test_sql_ctx, parse_result, stmt, ¶ms);
//logical_plan
test_sql->generate_logical_plan(test_sql_ctx, ¶ms, stmt, logical_plan);
//physical plan
test_sql->generate_physical_plan(logical_plan, phy_plan);
}
class TestPCVSet : public ::testing::Test
{
public:
TestPCVSet() {}
virtual ~TestPCVSet() {}
void SetUp() {}
void TearDown() {}
private:
//disallow copy
TestPCVSet(const TestPCVSet &other);
TestPCVSet& operator=(const TestPCVSet &other);
};
TEST_F(TestPCVSet, basic)
{
ObArenaAllocator allocator(ObModIds::TEST);
ObRawExprFactory expr_factory(allocator);
ObStmtFactory stmt_factory(allocator);
ObLogPlanFactory log_plan_factory(allocator);
TestSqlCtx test_sql_ctx;
test_sql_ctx.allocator_ = &allocator;
test_sql_ctx.expr_factory_ = &expr_factory;
test_sql_ctx.stmt_factory_ = &stmt_factory;
test_sql_ctx.log_plan_factory_ = &log_plan_factory;
ObSQLSessionInfo session;
ObExecContext exec_ctx;
ObSqlCtx sql_ctx;
// just empty array to avoid failure while adding plan
ObSEArray<ObPCConstParamInfo, 4> const_params_constraints;
ObSEArray<ObPCParamEqualInfo, 4> equal_param_constraints;
ObSEArray<ObPCParamPreciseInfo, 4> precise_param_constraints;
ObDList<ObPreCalcExprConstraint> all_pre_calc_constraints_;
ObSEArray<uint64_t, 4> trans_happened_route;
int64_t merged_version = test_sql->get_merged_version();
sql_ctx.schema_guard_ = &(test_sql->get_schema_guard());
sql_ctx.session_info_ = &session;
sql_ctx.all_plan_const_param_constraints_ = &const_params_constraints;
sql_ctx.all_possible_const_param_constraints_ = &const_params_constraints;
sql_ctx.all_equal_param_constraints_ = &equal_param_constraints;
sql_ctx.all_precise_param_constraints_ = &precise_param_constraints;
sql_ctx.all_pre_calc_constraints_ = &all_pre_calc_constraints_;
sql_ctx.trans_happened_route_ = &trans_happened_route;
ASSERT_EQ(OB_SUCCESS, session.init_tenant(ObString(OB_SYS_TENANT_NAME), OB_SYS_TENANT_ID));
exec_ctx.create_physical_plan_ctx();
exec_ctx.set_my_session(&session);
exec_ctx.set_sql_ctx(&sql_ctx);
exec_ctx.get_sql_ctx()->session_info_ = &session;
exec_ctx.get_task_executor_ctx()->set_min_cluster_version(CLUSTER_VERSION_1500);
ObLogPlan *logical_plan = nullptr;
ObPhysicalPlan *phy_plan = nullptr;
ASSERT_EQ(OB_SUCCESS, ObPreProcessSysVars::init_sys_var());
EXPECT_TRUE(OB_SUCCESS == session.load_default_sys_variable(false, true));
// when add generate_plan() , change TEST_PLAN_NUM
ObString sql = ObString::make_string("select * from t1 where c1 = 0");
ObPlanCacheCtx pc_ctx(sql, PC_TEXT_MODE, allocator, sql_ctx, exec_ctx, common::OB_SYS_TENANT_ID);
ObPCVSet pcv_set(plan_cache);
int ret = OB_SUCCESS;
generate_plan(test_sql_ctx,
sql.ptr(),
exec_ctx.get_physical_plan_ctx()->get_param_store_for_update(),
logical_plan, phy_plan, session);
ret = pcv_set.init(pc_ctx, phy_plan);
ret = pcv_set.add_cache_obj(phy_plan, pc_ctx);
EXPECT_EQ(OB_SUCCESS, ret);
}
}
int main(int argc, char **argv)
{
system("rm -rf test_pcv_set.log");
::oceanbase::sql::init_sql_factories();
::testing::InitGoogleTest(&argc,argv);
OB_LOGGER.set_log_level("INFO");
OB_LOGGER.set_file_name("test_pcv_set.log", true);
::test::init_pcv_set();
return RUN_ALL_TESTS();
}