forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_expr_values_with_child.cpp
More file actions
343 lines (321 loc) · 11.1 KB
/
ob_expr_values_with_child.cpp
File metadata and controls
343 lines (321 loc) · 11.1 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
/**
* 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_ENG
#include "sql/engine/basic/ob_expr_values_with_child.h"
#include "share/object/ob_obj_cast.h"
#include "common/sql_mode/ob_sql_mode_utils.h"
#include "sql/ob_sql_utils.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/ob_physical_plan.h"
#include "sql/engine/ob_exec_context.h"
namespace oceanbase {
using namespace common;
using namespace share;
namespace sql {
class ObExprValuesWithChild::ObExprValuesWithChildCtx : public ObPhyOperatorCtx {
public:
explicit ObExprValuesWithChildCtx(ObExecContext& ctx) : ObPhyOperatorCtx(ctx), node_idx_(0)
{}
~ObExprValuesWithChildCtx()
{}
virtual void destroy()
{
ObPhyOperatorCtx::destroy_base();
}
private:
int64_t node_idx_;
friend class ObExprValuesWithChild;
};
ObExprValuesWithChild::ObExprValuesWithChild(ObIAllocator& alloc) : ObSingleChildPhyOperator(alloc), values_(alloc)
{}
ObExprValuesWithChild::~ObExprValuesWithChild()
{}
int ObExprValuesWithChild::init_value_count(int64_t value_cnt)
{
return values_.init(value_cnt);
}
int ObExprValuesWithChild::add_value(ObSqlExpression* value)
{
return values_.push_back(value);
}
int ObExprValuesWithChild::init_op_ctx(ObExecContext& ctx) const
{
int ret = OB_SUCCESS;
ObPhyOperatorCtx* op_ctx = NULL;
if (OB_FAIL(CREATE_PHY_OPERATOR_CTX(ObExprValuesWithChildCtx, ctx, get_id(), get_type(), op_ctx))) {
LOG_WARN("create physical operator context failed", K(ret));
} else if (OB_ISNULL(op_ctx)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("op_ctx is null");
} else if (OB_FAIL(op_ctx->create_cur_row(get_column_count(), projector_, projector_size_))) {
LOG_WARN("create current row failed", K(ret));
}
return ret;
}
int ObExprValuesWithChild::inner_open(ObExecContext& ctx) const
{
int ret = OB_SUCCESS;
ObExprValuesWithChildCtx* values_ctx = NULL;
if (OB_ISNULL(child_op_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("child op is not set", K(ret));
} else if (OB_FAIL(init_op_ctx(ctx))) {
LOG_WARN("init operator context failed", K(ret));
} else if (OB_ISNULL(values_ctx = GET_PHY_OPERATOR_CTX(ObExprValuesWithChildCtx, ctx, get_id()))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get physical operator context failed", K(ret), K_(id));
} else {
values_ctx->node_idx_ = 0;
}
return ret;
}
int ObExprValuesWithChild::rescan(ObExecContext& ctx) const
{
int ret = OB_SUCCESS;
ObExprValuesWithChildCtx* values_ctx = NULL;
if (OB_FAIL(ObSingleChildPhyOperator::rescan(ctx))) {
LOG_WARN("rescan ObSingleChildPhyOperator failed", K(ret));
} else if (OB_ISNULL(values_ctx = GET_PHY_OPERATOR_CTX(ObExprValuesWithChildCtx, ctx, get_id()))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("values_ctx is null");
} else {
values_ctx->node_idx_ = 0;
}
return ret;
}
int ObExprValuesWithChild::inner_close(ObExecContext& ctx) const
{
int ret = OB_SUCCESS;
ObExprValuesWithChildCtx* values_ctx = NULL;
if (OB_ISNULL(values_ctx = GET_PHY_OPERATOR_CTX(ObExprValuesWithChildCtx, ctx, get_id()))) {
LOG_DEBUG("The operator has not been opened.", K(ret), K_(id), "op_type", ob_phy_operator_type_str(get_type()));
} else {
values_ctx->node_idx_ = 0;
}
return ret;
}
int ObExprValuesWithChild::inner_get_next_row(ObExecContext& ctx, const ObNewRow*& row) const
{
int ret = OB_SUCCESS;
NG_TRACE_TIMES(2, value_start_next_row);
ObPhysicalPlanCtx* plan_ctx = NULL;
ObExprValuesWithChildCtx* values_ctx = NULL;
// reset temp auto-increment value before calculate a row
if (OB_ISNULL(plan_ctx = GET_PHY_PLAN_CTX(ctx))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("plan_ctx is null");
} else {
plan_ctx->set_autoinc_id_tmp(0);
}
if (!OB_SUCC(ret)) {
// do nothing
} else if (OB_ISNULL(values_ctx = GET_PHY_OPERATOR_CTX(ObExprValuesWithChildCtx, ctx, id_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get physical operator context failed", K(ret), K_(id));
} else if (OB_FAIL(THIS_WORKER.check_status())) {
LOG_WARN("check physical plan status faild", K(ret));
} else if (OB_FAIL(calc_next_row(ctx))) {
if (OB_ITER_END != ret) {
LOG_WARN("get next row from row store failed", K(ret));
}
} else {
row = &(values_ctx->get_cur_row());
LOG_DEBUG("get next row from expr values", K(*row));
NG_TRACE_TIMES(2, value_end_next_row);
}
return ret;
}
int ObExprValuesWithChild::calc_next_row(ObExecContext& ctx) const
{
int ret = OB_SUCCESS;
ObExprValuesWithChildCtx* value_ctx = NULL;
ObSQLSessionInfo* session = ctx.get_my_session();
int64_t col_num = get_column_count();
int64_t col_index = 0;
NG_TRACE_TIMES(2, value_start_calc_row);
if (OB_UNLIKELY(get_size() <= 0 || get_column_count() <= 0 || get_size() % get_column_count() != 0 ||
OB_ISNULL(my_phy_plan_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr values status is unexpected", K(get_size()), K(get_column_count()), K_(my_phy_plan));
} else if (OB_ISNULL(value_ctx = GET_PHY_OPERATOR_CTX(ObExprValuesWithChildCtx, ctx, id_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("value_ctx is null");
} else if (OB_FAIL(ObSQLUtils::get_default_cast_mode(my_phy_plan_, session, value_ctx->expr_ctx_.cast_mode_))) {
LOG_WARN("get cast mode failed", K(ret));
} else {
ObExprCtx& expr_ctx = value_ctx->expr_ctx_;
ObNewRow& cur_row = value_ctx->get_cur_row();
if (OB_UNLIKELY(value_ctx->node_idx_ == get_size())) {
// there is no values any more
ret = OB_ITER_END;
} else if (OB_UNLIKELY(cur_row.is_invalid())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("cur row is not init", K(cur_row));
} else if (OB_FAIL(try_get_next_row(ctx))) {
LOG_WARN("fail get next row from child", K(ret));
} else {
bool is_break = false;
while (OB_SUCC(ret) && value_ctx->node_idx_ < get_size() && !is_break) {
if (OB_ISNULL(values_.at(value_ctx->node_idx_))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("value exprs is null", K(values_.at(value_ctx->node_idx_)));
} else if (OB_FAIL(values_.at(value_ctx->node_idx_)->calc(expr_ctx, cur_row, cur_row.cells_[col_index]))) {
LOG_WARN("failed to calc expr", K(ret));
} else {
++value_ctx->node_idx_;
if (col_index == col_num - 1) {
// last cell values resolved, output row now
is_break = true;
} else {
col_index = (col_index + 1) % col_num;
}
}
}
}
}
NG_TRACE_TIMES(2, value_after_calc_row);
return ret;
}
int ObExprValuesWithChild::try_get_next_row(ObExecContext& ctx) const
{
int ret = OB_SUCCESS;
const ObNewRow* sequence_row = nullptr;
if (OB_FAIL(child_op_->get_next_row(ctx, sequence_row))) {
LOG_WARN("fail get row from child", KR(ret));
}
return ret;
}
int64_t ObExprValuesWithChild::to_string_kv(char* buf, const int64_t buf_len) const
{
int64_t pos = 0;
J_KV(K_(values));
return pos;
}
int ObExprValuesWithChild::serialize(char* buf, int64_t buf_len, int64_t& pos, ObPhyOpSeriCtx& seri_ctx) const
{
int ret = OB_SUCCESS;
int64_t len = get_serialize_size_(seri_ctx);
int64_t value_count = get_size();
OB_UNIS_ENCODE(UNIS_VERSION);
OB_UNIS_ENCODE(len);
int64_t col_num = get_column_count();
const ObIArray<int64_t>* row_id_list = static_cast<const ObIArray<int64_t>*>(seri_ctx.row_id_list_);
if (OB_SUCC(ret) && row_id_list != NULL) {
value_count = col_num * row_id_list->count();
}
OB_UNIS_ENCODE(value_count);
if (OB_SUCC(ret) && row_id_list != NULL) {
ARRAY_FOREACH(*row_id_list, idx)
{
int64_t start_idx = row_id_list->at(idx) * col_num;
int64_t end_idx = start_idx + col_num;
for (int64_t i = start_idx; OB_SUCC(ret) && i < end_idx; ++i) {
if (OB_ISNULL(values_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("node is null");
} else if (OB_FAIL(values_.at(i)->serialize(buf, buf_len, pos))) {
LOG_WARN("serialize expr node failed", K(ret));
}
}
}
}
if (OB_SUCC(ret) && row_id_list == NULL) {
ARRAY_FOREACH(values_, i)
{
if (OB_ISNULL(values_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr node is null");
} else if (OB_FAIL(values_.at(i)->serialize(buf, buf_len, pos))) {
LOG_WARN("serialize expr node failed", K(ret));
}
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(ObSingleChildPhyOperator::serialize(buf, buf_len, pos))) {
LOG_WARN("serialize physical operator failed", K(ret));
}
}
return ret;
}
int64_t ObExprValuesWithChild::get_serialize_size(const ObPhyOpSeriCtx& seri_ctx) const
{
int64_t len = get_serialize_size_(seri_ctx);
OB_UNIS_ADD_LEN(len);
OB_UNIS_ADD_LEN(UNIS_VERSION);
return len;
}
OB_DEF_DESERIALIZE(ObExprValuesWithChild)
{
int ret = OB_SUCCESS;
int32_t value_size = 0;
ObSqlExpression* expr = NULL;
if (OB_ISNULL(my_phy_plan_)) {
ret = OB_NOT_INIT;
LOG_WARN("phy_plan is null");
}
OB_UNIS_DECODE(value_size);
if (OB_FAIL(init_value_count(value_size))) {
LOG_WARN("init value count failed", K(ret));
}
for (int32_t i = 0; OB_SUCC(ret) && i < value_size; ++i) {
if (OB_FAIL(ObSqlExpressionUtil::make_sql_expr(my_phy_plan_, expr))) {
LOG_WARN("make sql expression failed", K(ret));
} else if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr is null");
} else if (OB_FAIL(expr->deserialize(buf, data_len, pos))) {
LOG_WARN("deserialize expression failed", K(ret), K(i));
} else if (OB_FAIL(values_.push_back(expr))) {
LOG_WARN("add expr node value failed", K(ret));
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(ObSingleChildPhyOperator::deserialize(buf, data_len, pos))) {
LOG_WARN("deserialize physical operator failed", K(ret));
}
}
return ret;
}
int64_t ObExprValuesWithChild::get_serialize_size_(const ObPhyOpSeriCtx& seri_ctx) const
{
int64_t len = 0;
int64_t col_num = get_column_count();
int64_t value_size = get_size();
const ObIArray<int64_t>* row_id_list = static_cast<const ObIArray<int64_t>*>(seri_ctx.row_id_list_);
if (row_id_list != NULL) {
value_size = col_num * row_id_list->count();
}
OB_UNIS_ADD_LEN(value_size);
if (row_id_list != NULL) {
ARRAY_FOREACH_NORET(*row_id_list, idx)
{
int64_t start_idx = row_id_list->at(idx) * col_num;
int64_t end_idx = start_idx + col_num;
for (int64_t i = start_idx; i < end_idx; ++i) {
if (values_.at(i) != NULL) {
OB_UNIS_ADD_LEN(*values_.at(i));
}
}
}
} else {
ARRAY_FOREACH_NORET(values_, i)
{
if (values_.at(i) != NULL) {
OB_UNIS_ADD_LEN(*values_.at(i));
}
}
}
len += ObSingleChildPhyOperator::get_serialize_size();
return len;
}
} // namespace sql
} // namespace oceanbase