forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_expr_and.cpp
More file actions
266 lines (253 loc) · 8.54 KB
/
Copy pathob_expr_and.cpp
File metadata and controls
266 lines (253 loc) · 8.54 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
/**
* 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/expr/ob_expr_and.h"
#include "lib/oblog/ob_log.h"
#include "share/object/ob_obj_cast.h"
#include "common/object/ob_obj_compare.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/resolver/expr/ob_raw_expr_deduce_type.h"
namespace oceanbase
{
using namespace common;
namespace sql
{
ObExprAnd::ObExprAnd(ObIAllocator &alloc):
ObLogicalExprOperator(alloc, T_OP_AND, N_AND, PARAM_NUM_UNKNOWN, NOT_ROW_DIMENSION)
{
param_lazy_eval_ = true;
};
// scale以及precision也统一下
int ObExprAnd::calc_result_typeN(ObExprResType &type,
ObExprResType *types_stack,
int64_t param_num,
ObExprTypeCtx &type_ctx) const
{
UNUSED(type_ctx);
int ret = OB_SUCCESS;
if (OB_ISNULL(types_stack)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("null types", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < param_num; i++) {
if (types_stack[i].is_ext()) {
ret = OB_ERR_EXPRESSION_WRONG_TYPE;
LOG_WARN("PLS-00382: expression is of wrong type", K(ret), K(types_stack[i].get_type()));
}
}
}
if (OB_SUCC(ret)) {
type.set_type(ObInt32Type);
type.set_precision(DEFAULT_PRECISION_FOR_BOOL);
type.set_scale(DEFAULT_SCALE_FOR_INTEGER);
}
return ret;
}
static int calc_and_expr2(const ObDatum &left, const ObDatum &right,
ObDatum &res)
{
int ret = OB_SUCCESS;
if (left.is_false() || right.is_false()) {
res.set_false();
} else if (left.is_null() || right.is_null()) {
res.set_null();
} else {
res.set_true();
}
return ret;
}
int calc_and_exprN(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum)
{
LOG_DEBUG("calc and common mode");
int ret = OB_SUCCESS;
ObDatum *tmp_res = NULL;
ObDatum *child_res = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, tmp_res))) {
LOG_WARN("eval arg 0 failed", K(ret));
} else if (tmp_res->is_null()) {
res_datum.set_null();
} else {
res_datum.set_bool(tmp_res->get_bool());
}
if (OB_SUCC(ret) && !res_datum.is_false()) {
for (int64_t i = 1; OB_SUCC(ret) && i < expr.arg_cnt_; ++i) {
if (OB_FAIL(expr.args_[i]->eval(ctx, child_res))) {
LOG_WARN("eval arg failed", K(ret), K(i));
}
if (OB_SUCC(ret)) {
if (OB_FAIL(calc_and_expr2(res_datum, *child_res, res_datum))) {
LOG_WARN("calc_and_expr2 failed", K(ret), K(i));
} else if (res_datum.is_false()) {
break;
}
}
}
}
return ret;
}
int ObExprAnd::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(expr_cg_ctx);
if (OB_ISNULL(rt_expr.args_) || OB_UNLIKELY(2 > rt_expr.arg_cnt_) ||
OB_UNLIKELY(rt_expr.arg_cnt_ != raw_expr.get_param_count())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("args_ is NULL or arg_cnt_ is invalid or raw_expr is invalid",
K(ret), K(rt_expr), K(raw_expr));
} else {
rt_expr.eval_func_ = calc_and_exprN;
rt_expr.eval_batch_func_ = eval_and_batch_exprN;
}
return ret;
}
int ObExprAnd::eval_and_batch_exprN(const ObExpr &expr, ObEvalCtx &ctx,
const ObBitVector &skip, const int64_t batch_size)
{
LOG_DEBUG("eval and batch mode", K(batch_size));
int ret = OB_SUCCESS;
ObDatum* results = expr.locate_batch_datums(ctx);
if (OB_ISNULL(results)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr results frame is not init", K(ret));
} else {
ObBitVector &eval_flags = expr.get_evaluated_flags(ctx);
ObBitVector &my_skip = expr.get_pvt_skip(ctx);
my_skip.deep_copy(skip, batch_size);
const int64_t real_param = batch_size - my_skip.accumulate_bit_cnt(batch_size);
int64_t skip_cnt = 0; //记录一个batch中skip的数量, 所有参数被skip即可结束
/*为省略对results的初始化操作以及减少对my_skip的写入,分成3段求值
*args_[first]需要设置eval flags,设置初始值,为false设置skip
*args_[middle]需要设置非true的result,和false的skip
*args_[last]只需要设置非true的result
*/
//eval first
if (real_param == skip_cnt) {
} else if (OB_FAIL(expr.args_[0]->eval_batch(ctx, my_skip, batch_size))) {
LOG_WARN("failed to eval batch result args0", K(ret));
} else if (expr.args_[0]->is_batch_result()) {
ObDatum *curr_datum = nullptr;
ObDatum *datum_array = expr.args_[0]->locate_batch_datums(ctx);
for (int64_t j = 0; OB_SUCC(ret) && j < batch_size; ++j) {
if (my_skip.at(j)) {
continue;
}
curr_datum = &datum_array[j];
if (curr_datum->is_null()) {
results[j].set_null();
} else if (false == curr_datum->get_bool()) {
results[j].set_bool(false);
my_skip.set(j);
++skip_cnt;
} else {
results[j].set_bool(true);
}
eval_flags.set(j);
}
} else {
ObDatum *curr_datum = &expr.args_[0]->locate_expr_datum(ctx);
for (int64_t j = 0; OB_SUCC(ret) && j < batch_size; ++j) {
if (my_skip.at(j)) {
continue;
}
if (curr_datum->is_null()) {
results[j].set_null();
} else if (false == curr_datum->get_bool()) {
results[j].set_bool(false);
my_skip.set(j);
++skip_cnt;
} else {
results[j].set_bool(true);
}
eval_flags.set(j);
}
}
//eval middle
int64_t arg_idx = 1;
for (; OB_SUCC(ret) && arg_idx < expr.arg_cnt_ - 1 && skip_cnt < real_param; ++arg_idx) {
if (OB_FAIL(expr.args_[arg_idx]->eval_batch(ctx, my_skip, batch_size))) {
LOG_WARN("failed to eval batch result", K(ret), K(arg_idx));
} else if (expr.args_[arg_idx]->is_batch_result()) {
ObDatum *curr_datum = nullptr;
ObDatum *datum_array = expr.args_[arg_idx]->locate_batch_datums(ctx);
for (int64_t j = 0; OB_SUCC(ret) && j < batch_size; ++j) {
if (my_skip.at(j)) {
continue;
}
curr_datum = &datum_array[j];
if (curr_datum->is_null()) {
results[j].set_null();
} else if (false == curr_datum->get_bool()) {
results[j].set_bool(false);
my_skip.set(j);
} else {
//do nothing
}
}
} else {
ObDatum *curr_datum = &expr.args_[arg_idx]->locate_expr_datum(ctx);
for (int64_t j = 0; OB_SUCC(ret) && j < batch_size; ++j) {
if (my_skip.at(j)) {
continue;
}
if (curr_datum->is_null()) {
results[j].set_null();
} else if (false == curr_datum->get_bool()) {
results[j].set_bool(false);
my_skip.set(j);
} else {
//do nothing
}
}
}
}
//eval last
if (OB_FAIL(ret)) {
} else if (real_param == skip_cnt) {
} else if (OB_FAIL(expr.args_[arg_idx]->eval_batch(ctx, my_skip, batch_size))) {
LOG_WARN("failed to eval batch result args0", K(ret));
} else if (expr.args_[arg_idx]->is_batch_result()) {
ObDatum *curr_datum = nullptr;
ObDatum *datum_array = expr.args_[arg_idx]->locate_batch_datums(ctx);
for (int64_t j = 0; OB_SUCC(ret) && j < batch_size; ++j) {
if (my_skip.at(j)) {
continue;
}
curr_datum = &datum_array[j];
if (curr_datum->is_null()) {
results[j].set_null();
} else if (false == curr_datum->get_bool()) {
results[j].set_bool(false);
} else {
//do nothing
}
}
} else {
ObDatum *curr_datum = &expr.args_[arg_idx]->locate_expr_datum(ctx);
for (int64_t j = 0; OB_SUCC(ret) && j < batch_size; ++j) {
if (my_skip.at(j)) {
continue;
}
if (curr_datum->is_null()) {
results[j].set_null();
} else if (false == curr_datum->get_bool()) {
results[j].set_bool(false);
} else {
//do nothing
}
}
}
}
return ret;
}
}
}