forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_expr_ascii.cpp
More file actions
334 lines (309 loc) · 10.1 KB
/
ob_expr_ascii.cpp
File metadata and controls
334 lines (309 loc) · 10.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
/**
* 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 "lib/charset/ob_charset.h"
#include "lib/ob_name_def.h"
#include "lib/utility/ob_macro_utils.h"
#include "share/object/ob_obj_cast.h"
#include "sql/engine/expr/ob_expr_ascii.h"
#include "sql/engine/expr/ob_expr_util.h"
#include "sql/session/ob_sql_session_info.h"
namespace oceanbase {
using namespace common;
namespace sql {
ObExprAscii::ObExprAscii(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_ASCII, N_ASCII, 1, NOT_ROW_DIMENSION)
{}
int ObExprAscii::calc_result_type1(ObExprResType& type, ObExprResType& type1, ObExprTypeCtx& type_ctx) const
{
int ret = OB_SUCCESS;
const ObSQLSessionInfo* session = type_ctx.get_session();
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
if (session->use_static_typing_engine()) {
if (is_oracle_mode()) {
type.set_number();
ObSEArray<ObExprResType*, 1, ObNullAllocator> params;
OZ(params.push_back(&type1));
ObExprResType tmp_type;
OZ(aggregate_string_type_and_charset_oracle(*session, params, tmp_type));
OZ(deduce_string_param_calc_type_and_charset(*session, tmp_type, params));
} else {
type.set_int32();
if (ob_is_string_type(type1.get_type())) {
type1.set_calc_type(type1.get_type());
type1.set_calc_collation_type(type1.get_collation_type());
} else {
type1.set_calc_type(ObVarcharType);
type1.set_calc_collation_type(ObCharset::get_system_collation());
}
}
} else {
if (type1.is_null()) {
type.set_null();
} else if (share::is_oracle_mode()) {
type.set_number();
} else {
type.set_utinyint();
}
if (ob_is_string_type(type1.get_type())) {
type1.set_calc_type(type1.get_type());
type1.set_calc_collation_type(type1.get_collation_type());
} else {
type1.set_calc_type(ObVarcharType);
type1.set_calc_collation_type(ObCharset::get_system_collation());
}
}
}
return ret;
}
int ObExprAscii::calc(common::ObObj& obj, const common::ObObj& obj1, ObExprCtx& expr_ctx)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else if (obj1.is_null()) {
obj.set_null();
} else {
uint8_t tiny_val;
ObString str_val = obj1.get_string();
EXPR_DEFINE_CAST_CTX(expr_ctx, CM_NONE);
if (str_val.length() <= 0) {
tiny_val = 0;
} else {
tiny_val = static_cast<uint8_t>(str_val[0]);
}
obj.set_utinyint(tiny_val);
}
return ret;
}
int ObExprAscii::calc_result1(common::ObObj& obj, const common::ObObj& obj1, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (share::is_oracle_mode()) {
common::ObObj tmp;
number::ObNumber result;
if (OB_FAIL(ObExprOrd::calc(tmp, obj1, expr_ctx))) {
LOG_WARN("failed to calc expr ord", K(ret));
} else if (tmp.is_null()) {
obj.set_null();
} else if (OB_FAIL(result.from(tmp.get_uint64(), *expr_ctx.calc_buf_))) {
LOG_WARN("failed to convert uint64 to ob number", K(ret));
} else {
obj.set_number(result);
}
} else {
ret = ObExprAscii::calc(obj, obj1, expr_ctx);
}
return ret;
}
int ObExprAscii::calc_ascii_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
if (is_oracle_mode()) {
if (OB_FAIL(ObExprOrd::calc_ord_expr(expr, ctx, res_datum))) {
LOG_WARN("calc_ord_expr failed", K(ret));
}
} else {
ObDatum* s_datum = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, s_datum))) {
LOG_WARN("eval arg failed", K(ret));
} else if (s_datum->is_null()) {
res_datum.set_null();
} else {
const ObString& str_val = s_datum->get_string();
if (str_val.empty()) {
res_datum.set_int32(0);
} else {
res_datum.set_int32(static_cast<uint8_t>(str_val[0]));
}
}
}
return ret;
}
int ObExprAscii::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(expr_cg_ctx);
UNUSED(raw_expr);
rt_expr.eval_func_ = calc_ascii_expr;
return ret;
}
// ObExprOrd start
ObExprOrd::ObExprOrd(ObIAllocator& alloc) : ObFuncExprOperator(alloc, T_FUN_SYS_ORD, N_ORD, 1, NOT_ROW_DIMENSION)
{}
int ObExprOrd::calc_result_type1(ObExprResType& type, ObExprResType& type1, ObExprTypeCtx& type_ctx) const
{
int ret = OB_SUCCESS;
const ObSQLSessionInfo* session = type_ctx.get_session();
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
if (session->use_static_typing_engine()) {
type.set_int();
if (ob_is_string_type(type1.get_type())) {
type1.set_calc_type(type1.get_type());
type1.set_calc_collation_type(type1.get_collation_type());
} else {
type1.set_calc_type(ObVarcharType);
type1.set_calc_collation_type(ObCharset::get_system_collation());
}
} else {
if (type1.is_null()) {
type.set_null();
} else {
type.set_uint64();
}
if (ob_is_string_type(type1.get_type())) {
type1.set_calc_type(type1.get_type());
type1.set_calc_collation_type(type1.get_collation_type());
} else {
type1.set_calc_type(ObVarcharType);
type1.set_calc_collation_type(ObCharset::get_system_collation());
}
}
}
return ret;
}
int ObExprOrd::calc(common::ObObj& obj, const common::ObObj& obj1, ObExprCtx& expr_ctx)
{
ObCollationType cs_type = obj1.get_collation_type();
int ret = OB_SUCCESS;
uint8_t type = 0; // 0 for Ord(MB set), 1 for ascii(byte set)
if (OB_UNLIKELY(!ObCharset::is_valid_collation(cs_type))) {
LOG_WARN("invalid collation type", K(ret));
ret = OB_ERR_UNEXPECTED;
} else if (obj1.is_null()) {
type = 1;
} else if (ObCharset::usemb(cs_type)) {
// test mb then calc
// String trans
ObString str_val = obj1.get_string();
const char* str_ptr = str_val.ptr();
if (NULL != str_ptr) {
const ObCharsetInfo* cs = ObCharset::get_charset(cs_type);
uint64_t n = 0, char_len = cs->cset->ismbchar(str_val.ptr(), str_val.length());
if (char_len > str_val.length()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("my_ismbchar return wrong value", K(ret), K(char_len), K(str_val.length()));
} else if (0 == char_len) {
type = 1;
} else {
while (char_len--) {
n = (n << 8) | (uint64_t)((uint8_t)*str_ptr++);
}
obj.set_uint64(n);
}
} else {
type = 1;
}
} else {
type = 1;
}
if (OB_SUCCESS == ret && 1 == type) {
ret = ObExprAscii::calc(obj, obj1, expr_ctx);
if (OB_SUCC(ret) && (false == obj.is_null())) {
obj.set_uint64(static_cast<uint64_t>(obj.get_utinyint()));
}
}
return ret;
}
int ObExprOrd::calc_result1(common::ObObj& obj, const common::ObObj& obj1, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
// test expr_ctx
if (OB_ISNULL(expr_ctx.calc_buf_) || OB_ISNULL(expr_ctx.my_session_)) {
ret = OB_NOT_INIT;
LOG_WARN("expr_ctx not init", K(ret));
} else {
ret = calc(obj, obj1, expr_ctx);
}
return ret;
}
// ascii expr in oracle mode use this function too.
int ObExprOrd::calc_ord_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
ObDatum* s_datum = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, s_datum))) {
LOG_WARN("eval arg failed", K(ret));
} else if (s_datum->is_null()) {
res_datum.set_null();
} else {
int64_t res_int = 0;
bool res_null = false;
const ObCollationType cs_type = expr.args_[0]->datum_meta_.cs_type_;
const ObCharsetInfo* cs = ObCharset::get_charset(cs_type);
const ObString& str_val = s_datum->get_string();
if (OB_ISNULL(cs)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid cs_type", K(ret), K(cs_type));
} else if (str_val.empty()) {
if (is_oracle_mode()) {
res_null = true;
} else {
res_int = 0;
}
} else if (ObCharset::usemb(cs_type)) {
const char* str_ptr = str_val.ptr();
uint32_t n = 0;
uint32_t char_len = cs->cset->ismbchar(str_val.ptr(), str_val.length());
if (char_len > str_val.length()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("my_ismbchar return wrong value", K(ret), K(char_len), K(str_val.length()));
} else if (0 == char_len) {
res_int = static_cast<uint8_t>(str_val[0]);
} else {
while (char_len--) {
n = (n << 8) | static_cast<uint32_t>(static_cast<uint8_t>(*str_ptr++));
}
res_int = n;
}
} else {
res_int = static_cast<uint8_t>(str_val[0]);
}
if (OB_SUCC(ret)) {
const ObObjType& res_type = expr.datum_meta_.type_;
if (res_null) {
res_datum.set_null();
} else if (ObNumberType == res_type) {
number::ObNumber res_nmb;
ObNumStackOnceAlloc tmp_alloc;
if (OB_FAIL(res_nmb.from(res_int, tmp_alloc))) {
LOG_WARN("get nmb from int failed", K(ret), K(res_int));
} else {
res_datum.set_number(res_nmb);
}
} else if (ObIntType == res_type) {
res_datum.set_int(res_int);
} else {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpected res_type", K(ret), K(res_type));
}
}
}
return ret;
}
int ObExprOrd::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(expr_cg_ctx);
UNUSED(raw_expr);
rt_expr.eval_func_ = calc_ord_expr;
return ret;
}
} // namespace sql
} // namespace oceanbase