forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_exec_context.cpp
More file actions
384 lines (348 loc) · 12.4 KB
/
Copy pathtest_exec_context.cpp
File metadata and controls
384 lines (348 loc) · 12.4 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
/**
* 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 "../optimizer/ob_mock_part_mgr.h"
#define protected public
#define private public
#include "sql/engine/test_engine_util.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/engine/ob_phy_operator.h"
#include "sql/engine/ob_phy_operator_type.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/expr/ob_expr_in.h"
#include "observer/ob_server.h"
#include "observer/ob_server_struct.h"
#include "sql/executor/ob_interm_result_pool.h"
#include "share/ob_tenant_mgr.h"
using namespace oceanbase::sql;
using namespace oceanbase::common;
using namespace test;
using namespace oceanbase::observer;
using namespace oceanbase::share;
class ObExecContextTest: public ::testing::Test
{
public:
ObExecContextTest();
virtual ~ObExecContextTest();
virtual void SetUp();
virtual void TearDown();
private:
// disallow copy
ObExecContextTest(const ObExecContextTest &other);
ObExecContextTest& operator=(const ObExecContextTest &other);
private:
// data members
};
ObExecContextTest::ObExecContextTest()
{
}
ObExecContextTest::~ObExecContextTest()
{
}
void ObExecContextTest::SetUp()
{
}
void ObExecContextTest::TearDown()
{
}
class ObPhyOperatorCtxFake : public ObPhyOperator::ObPhyOperatorCtx
{
public:
ObPhyOperatorCtxFake(ObExecContext &ctx);
~ObPhyOperatorCtxFake();
virtual void destroy() { return ObPhyOperatorCtx::destroy_base(); }
int phy_op_calc();
void set_phy_op_ctx_a(int a);
void set_phy_op_ctx_b(int b);
private:
int phy_op_ctx_a_;
int phy_op_ctx_b_;
};
ObPhyOperatorCtxFake::ObPhyOperatorCtxFake(ObExecContext &ctx)
: ObPhyOperatorCtx(ctx), phy_op_ctx_a_(0), phy_op_ctx_b_(0)
{
}
ObPhyOperatorCtxFake::~ObPhyOperatorCtxFake()
{
}
void ObPhyOperatorCtxFake::set_phy_op_ctx_a(int a)
{
phy_op_ctx_a_ = a;
}
void ObPhyOperatorCtxFake::set_phy_op_ctx_b(int b)
{
phy_op_ctx_b_ = b;
}
int ObPhyOperatorCtxFake::phy_op_calc()
{
return phy_op_ctx_a_ + phy_op_ctx_b_;
}
class ObRootTransmitInput : public ObIPhyOperatorInput
{
public:
ObRootTransmitInput(ObExecContext &ctx)
: input_param_a_(0)
{
UNUSED(ctx);
}
virtual void reset() override
{
input_param_a_ = 0;
}
void set_input_param_a(int64_t a)
{
input_param_a_ = a;
}
int64_t get_input_param_a() const
{
return input_param_a_;
}
int serialize(char *buf, int64_t buf_len, int64_t &pos) const
{
UNUSED(buf);
UNUSED(buf_len);
UNUSED(pos);
return OB_NOT_IMPLEMENT;
}
int deserialize(const char *buf, int64_t data_len, int64_t &pos)
{
UNUSED(buf);
UNUSED(data_len);
UNUSED(pos);
return OB_NOT_IMPLEMENT;
}
int64_t get_serialize_size() const
{
return 0;
}
int init(ObExecContext &ctx, ObTaskInfo &task_info, ObPhyOperator &op)
{
UNUSED(ctx);
UNUSED(task_info);
UNUSED(op);
return OB_NOT_IMPLEMENT;
}
ObPhyOperatorType get_phy_op_type() const
{
return PHY_ROOT_TRANSMIT;
}
private:
int64_t input_param_a_;
};
const uint64_t phy_op_id = 0;
//test create physical operator normal
TEST_F(ObExecContextTest, create_phy_op_test)
{
int ret = OB_SUCCESS;
void *ptr = NULL;
ObExecContext exec_context;
ObIAllocator *allocator = NULL;
ObPhyOperatorCtxFake *phy_op_ctx = NULL;
ObRootTransmitInput *input_param = NULL;
int64_t phy_op_size = 64;
ASSERT_EQ(OB_SUCCESS, create_test_session(exec_context));
allocator = &exec_context.get_allocator();
ASSERT_EQ(OB_SUCCESS, exec_context.init_phy_op(phy_op_size));
for (int i = 0; i < phy_op_size; i++)
{
ret = CREATE_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake,
exec_context,
phy_op_id + i,
static_cast<ObPhyOperatorType>(0),
phy_op_ctx);
ASSERT_FALSE(NULL == phy_op_ctx);
phy_op_ctx->set_phy_op_ctx_a(i);
phy_op_ctx->set_phy_op_ctx_b(i + 1);
ASSERT_EQ(i * 2 + 1, phy_op_ctx->phy_op_calc());
//create input param
ret = CREATE_PHY_OP_INPUT(ObRootTransmitInput,
exec_context,
phy_op_id + i,
PHY_ROOT_TRANSMIT,
input_param);
ASSERT_FALSE(NULL == input_param);
input_param->set_input_param_a(i);
ASSERT_EQ(i, input_param->get_input_param_a());
}
for (int i = 0; i < phy_op_size; i++)
{
phy_op_ctx = GET_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake, exec_context, phy_op_id + i);
ASSERT_FALSE(NULL == phy_op_ctx);
ASSERT_EQ(i * 2 + 1, phy_op_ctx->phy_op_calc());
input_param = GET_PHY_OP_INPUT(ObRootTransmitInput, exec_context, phy_op_id + i);
ASSERT_FALSE(NULL == input_param);
ASSERT_EQ(i, input_param->get_input_param_a());
}
ptr = allocator->alloc(10);
ASSERT_FALSE(NULL == ptr);
allocator->free(ptr);
UNUSED(ret);
}
//excepted create physical operator fail, create the same physical operator twice
TEST_F(ObExecContextTest, create_phy_op_fail)
{
ObExecContext exec_context;
ASSERT_EQ(OB_SUCCESS, create_test_session(exec_context));
int64_t phy_op_size = 64;
ASSERT_EQ(OB_SUCCESS, exec_context.init_phy_op(phy_op_size));
ObPhyOperatorCtxFake *phy_op_ctx = NULL;
ASSERT_EQ(OB_SUCCESS, CREATE_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake,
exec_context,
phy_op_id,
static_cast<ObPhyOperatorType>(0),
phy_op_ctx));
ASSERT_FALSE(NULL == phy_op_ctx);
phy_op_ctx = GET_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake, exec_context, phy_op_id);
ASSERT_FALSE(NULL == phy_op_ctx);
//create phy_op_ctx twice
ASSERT_EQ(OB_INIT_TWICE, CREATE_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake,
exec_context,
phy_op_id,
static_cast<ObPhyOperatorType>(0),
phy_op_ctx));
ASSERT_TRUE(NULL == phy_op_ctx);
//test create input param failed
ObRootTransmitInput *input_param = NULL;
ASSERT_EQ(OB_SUCCESS, CREATE_PHY_OP_INPUT(ObRootTransmitInput,
exec_context,
phy_op_id,
PHY_ROOT_TRANSMIT,
input_param));
ASSERT_FALSE(NULL == input_param);
input_param = GET_PHY_OP_INPUT(ObRootTransmitInput, exec_context, phy_op_id);
ASSERT_FALSE(NULL == input_param);
//create input_param twice
ASSERT_EQ(OB_INIT_TWICE, CREATE_PHY_OP_INPUT(ObRootTransmitInput,
exec_context,
phy_op_id,
PHY_ROOT_TRANSMIT,
input_param));
ASSERT_TRUE(NULL == input_param);
}
TEST_F(ObExecContextTest, create_physical_plan_ctx)
{
ObExecContext exec_context;
ASSERT_EQ(OB_SUCCESS, create_test_session(exec_context));
ObPhysicalPlanCtx *plan_ctx = NULL;
ASSERT_EQ(OB_SUCCESS, exec_context.init_phy_op(64));
//create normal
ASSERT_EQ(OB_SUCCESS, exec_context.create_physical_plan_ctx());
ASSERT_FALSE(NULL == exec_context.get_physical_plan_ctx());
//get plan ctx normal
plan_ctx = exec_context.get_physical_plan_ctx();
ASSERT_FALSE(NULL == plan_ctx);
}
TEST_F(ObExecContextTest, create_physical_plan_ctx_fail)
{
ObExecContext exec_context;
ASSERT_EQ(OB_SUCCESS, create_test_session(exec_context));
ObPhysicalPlanCtx *plan_ctx = NULL;
ASSERT_EQ(OB_SUCCESS, exec_context.init_phy_op(64));
//get physical plan but not be created
plan_ctx = exec_context.get_physical_plan_ctx();
ASSERT_TRUE(NULL == plan_ctx);
//create physical plan twice
ASSERT_EQ(OB_SUCCESS, exec_context.create_physical_plan_ctx());
ASSERT_FALSE(NULL == exec_context.get_physical_plan_ctx());
ASSERT_EQ(OB_INIT_TWICE, exec_context.create_physical_plan_ctx());
}
TEST_F(ObExecContextTest, invalid_argument)
{
ObExecContext exec_context;
ASSERT_EQ(OB_SUCCESS, create_test_session(exec_context));
int64_t phy_op_size = 64;
uint64_t phy_op_id = 100;
ASSERT_EQ(OB_SUCCESS, exec_context.init_phy_op(phy_op_size));
//phy_op_id larger than phy_op_size
ObPhyOperatorCtxFake *phy_op_ctx = NULL;
ASSERT_EQ(OB_INVALID_ARGUMENT, CREATE_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake,
exec_context,
phy_op_id,
static_cast<ObPhyOperatorType>(0),
phy_op_ctx));
ASSERT_TRUE(NULL == phy_op_ctx);
//OB_INVALID_ID phy_op_id
ASSERT_EQ(OB_INVALID_ARGUMENT, CREATE_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake,
exec_context,
OB_INVALID_ID,
static_cast<ObPhyOperatorType>(0),
phy_op_ctx));
ASSERT_EQ(NULL, phy_op_ctx);
phy_op_ctx = GET_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake, exec_context, phy_op_id);
ASSERT_TRUE(NULL == phy_op_ctx);
}
TEST_F(ObExecContextTest, serialization)
{
int ret = OB_SUCCESS;
ObExecContext exec_context;
ASSERT_EQ(OB_SUCCESS, create_test_session(exec_context));
ObPhyOperatorCtxFake *phy_op_ctx = NULL;
ObRootTransmitInput *input_param = NULL;
int64_t phy_op_size = 64;
ObSql sql_engine;
ASSERT_EQ(OB_SUCCESS, ObPreProcessSysVars::init_sys_var());
ASSERT_EQ(OB_SUCCESS, exec_context.init_phy_op(phy_op_size));
ASSERT_EQ(OB_SUCCESS, create_test_session(exec_context));
auto my_session = exec_context.get_my_session();
ASSERT_FALSE(NULL == my_session);
ObPhysicalPlan cur_phy_plan;
ASSERT_EQ(OB_SUCCESS, my_session->set_cur_phy_plan(&cur_phy_plan));
ASSERT_EQ(OB_SUCCESS, exec_context.create_physical_plan_ctx());
ASSERT_EQ(OB_SUCCESS, my_session->load_default_sys_variable(false, true));
GCTX.sql_engine_ = &sql_engine;
for (int i = 0; i < phy_op_size; i++)
{
ret = CREATE_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake,
exec_context,
phy_op_id + i,
static_cast<ObPhyOperatorType>(0),
phy_op_ctx);
ASSERT_FALSE(NULL == phy_op_ctx);
phy_op_ctx->set_phy_op_ctx_a(i);
phy_op_ctx->set_phy_op_ctx_b(i + 1);
ASSERT_EQ(i * 2 + 1, phy_op_ctx->phy_op_calc());
//create input param
ret = CREATE_PHY_OP_INPUT(ObRootTransmitInput,
exec_context,
phy_op_id + i,
PHY_ROOT_TRANSMIT,
input_param);
ASSERT_FALSE(NULL == input_param);
input_param->set_input_param_a(i);
ASSERT_EQ(i, input_param->get_input_param_a());
}
for (int i = 0; i < phy_op_size; i++)
{
phy_op_ctx = GET_PHY_OPERATOR_CTX(ObPhyOperatorCtxFake, exec_context, phy_op_id + i);
ASSERT_FALSE(NULL == phy_op_ctx);
ASSERT_EQ(i * 2 + 1, phy_op_ctx->phy_op_calc());
input_param = GET_PHY_OP_INPUT(ObRootTransmitInput, exec_context, phy_op_id + i);
ASSERT_FALSE(NULL == input_param);
ASSERT_EQ(i, input_param->get_input_param_a());
}
int64_t pos = 0;
const int64_t MAX_SERIALIZE_BUF_LEN = 10240;
char buf[MAX_SERIALIZE_BUF_LEN] = {'\0'};
ASSERT_EQ(OB_SUCCESS, exec_context.serialize(buf, MAX_SERIALIZE_BUF_LEN, pos));
ASSERT_EQ(pos, exec_context.get_serialize_size());
UNUSED(ret);
}
int main(int argc, char **argv)
{
system("rm -rf test_exec_context.log");
::testing::InitGoogleTest(&argc,argv);
OB_LOGGER.set_log_level("INFO");
OB_LOGGER.set_file_name("test_exec_context.log", true);
int ret = RUN_ALL_TESTS();
OB_LOGGER.disable();
return ret;
}