patch 4.0

This commit is contained in:
wangzelin.wzl
2022-10-24 10:34:53 +08:00
parent 4ad6e00ec3
commit 93a1074b0c
10533 changed files with 2588271 additions and 2299373 deletions

View File

@ -10,7 +10,7 @@
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_ENG
#define USING_LOG_PREFIX SQL_ENG
#include "ob_expr_char.h"
#include "lib/charset/ob_charset.h"
@ -18,63 +18,63 @@
#include "common/sql_mode/ob_sql_mode_utils.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/expr/ob_expr_cast.h"
#include "sql/engine/expr/ob_expr_util.h"
#include "sql/engine/ob_exec_context.h"
#include "sql/ob_sql_utils.h"
using namespace oceanbase::common;
using namespace oceanbase::sql;
namespace oceanbase {
namespace sql {
namespace oceanbase
{
namespace sql
{
#define INT32_TO_STR1(buf, int_val) *(reinterpret_cast(uchar*>(buf)) = reinterpret_cast<uchar>(int_val);
#define INT32_TO_STR2(buf, int_val) \
{ \
uint32_t temp = (uint32_t)(int_val); \
(reinterpret_cast<uchar*>(buf))[1] = static_cast<uchar>(temp); \
(reinterpret_cast<uchar*>(buf))[0] = static_cast<uchar>(temp >> 8); \
}
#define INT32_TO_STR3(buf, int_val) \
{ /* lint -save -e734 */ \
uint64_t temp = (uint64_t)(int_val); \
(reinterpret_cast<uchar*>(buf))[2] = static_cast<uchar>(temp); \
(reinterpret_cast<uchar*>(buf))[1] = static_cast<uchar>(temp >> 8); \
(reinterpret_cast<uchar*>(buf))[0] = static_cast<uchar>(temp >> 16); \
#define INT32_TO_STR2(buf, int_val) { uint32_t temp = (uint32_t) (int_val) ;\
(reinterpret_cast<uchar *>(buf))[1] = static_cast<uchar>(temp);\
(reinterpret_cast<uchar *>(buf))[0] = static_cast<uchar>(temp >> 8); }
#define INT32_TO_STR3(buf, int_val) { /* lint -save -e734 */\
uint64_t temp= (uint64_t) (int_val);\
(reinterpret_cast<uchar *>(buf))[2] = static_cast<uchar>(temp);\
(reinterpret_cast<uchar *>(buf))[1] = static_cast<uchar>(temp >> 8);\
(reinterpret_cast<uchar *>(buf))[0] = static_cast<uchar>(temp >> 16);\
/* lint -restore */}
#define INT32_TO_STR4(buf, int_val) \
{ \
uint64_t temp = (uint64_t)(int_val); \
(reinterpret_cast<uchar*>(buf))[3] = static_cast<uchar>(temp); \
(reinterpret_cast<uchar*>(buf))[2] = static_cast<uchar>(temp >> 8); \
(reinterpret_cast<uchar*>(buf))[1] = static_cast<uchar>(temp >> 16); \
(reinterpret_cast<uchar*>(buf))[0] = static_cast<uchar>(temp >> 24); \
}
#define INT32_TO_STR4(buf, int_val) { uint64_t temp = (uint64_t) (int_val);\
(reinterpret_cast<uchar *>(buf))[3] = static_cast<uchar>(temp);\
(reinterpret_cast<uchar *>(buf))[2] = static_cast<uchar>(temp >> 8);\
(reinterpret_cast<uchar *>(buf))[1] = static_cast<uchar>(temp >> 16);\
(reinterpret_cast<uchar *>(buf))[0] = static_cast<uchar>(temp >> 24); }\
ObExprChar::ObExprChar(ObIAllocator& alloc)
ObExprChar::ObExprChar(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_CHAR, N_CHAR, MORE_THAN_ONE, NOT_ROW_DIMENSION)
{}
{
}
ObExprChar::~ObExprChar()
{}
{
}
int ObExprChar::calc_result_typeN(
ObExprResType& type, ObExprResType* types, int64_t param_num, ObExprTypeCtx& type_ctx) const
int ObExprChar::calc_result_typeN(ObExprResType &type, ObExprResType *types, int64_t param_num,
ObExprTypeCtx &type_ctx) const
{
UNUSED(type_ctx);
int ret = OB_SUCCESS;
if (param_num <= 1) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument number, param should not less than 2", K(param_num), K(ret));
} else if (OB_FAIL(calc_result_type(type, types[0]))) {
} else if (OB_FAIL(calc_result_type(type, types[param_num-1]))) {
LOG_WARN("failed to calc result type", K(ret));
} else {
// set calc type
// i starts from 1 rather than 0 since the first param is obvarchar always.
for (int64_t i = 1; i < param_num; ++i) {
//set calc type
//i starts from 1 rather than 0 since the first param is obvarchar always.
//see http://review.alibaba-inc.com/r/21109/ for more details when necessary.
for (int64_t i = 0; i < param_num-1; ++i) {
types[i].set_calc_type(ObIntType);
}
ObExprOperator::calc_result_flagN(type, types, param_num);
// set length
CK(OB_INVALID_COUNT != type_ctx.get_max_allowed_packet());
//set length
CK( OB_INVALID_COUNT != type_ctx.get_max_allowed_packet());
if (OB_SUCC(ret)) {
type.set_length(static_cast<ObLength>(type_ctx.get_max_allowed_packet()));
}
@ -82,83 +82,7 @@ int ObExprChar::calc_result_typeN(
return ret;
}
int ObExprChar::calc_resultN(ObObj& result, const ObObj* obj_array, int64_t param_num, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.my_session_)) {
ret = OB_NOT_INIT;
LOG_WARN("Session is NULL", K(ret));
} else if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("calc buf not is not inited", K(ret));
} else if (param_num < 2) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("Incorrect parameter number", K(param_num), K(ret));
} else if (OB_ISNULL(obj_array)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("obj_array is NULL", K(ret));
} else {
ObSqlString str_buf;
if (OB_SUCC(ret)) {
for (int64_t i = 1; OB_SUCC(ret) && i < param_num; ++i) {
const ObObj& param_obj = obj_array[i];
if (param_obj.is_null()) {
// ignore null obj which is syntax of char()
continue;
} else {
TYPE_CHECK(param_obj, ObIntType);
int64_t int64_val = param_obj.get_int();
int32_t int32_val = static_cast<int32_t>(int64_val);
char buf[4] = {0};
int64_t append_len = 0;
if (int32_val & 0xFF000000L) {
INT32_TO_STR4(buf, int32_val);
append_len = 4;
} else if (int32_val & 0xFF0000L) {
INT32_TO_STR3(buf, int32_val);
append_len = 3;
} else if (int32_val & 0xFF00L) {
INT32_TO_STR2(buf, int32_val);
append_len = 2;
} else {
buf[0] = static_cast<char>(int32_val);
append_len = 1;
}
if (OB_SUCCESS == ret && OB_FAIL(str_buf.append(buf, append_len))) {
LOG_WARN("fail to append convert result", K(ret));
}
}
}
}
if (OB_SUCC(ret)) {
const int64_t result_len = str_buf.length();
char* result_str = reinterpret_cast<char*>(expr_ctx.calc_buf_->alloc(result_len + 1));
if (OB_UNLIKELY(NULL == result_str)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("fail to allocate memory", K(result_len), K(ret));
} else {
if (NULL != str_buf.ptr()) {
MEMCPY(result_str, str_buf.ptr(), result_len);
}
result_str[result_len] = '\0';
result.set_varchar(result_str, static_cast<int32_t>(result_len));
result.set_collation(result_type_);
ObSQLMode sql_mode = expr_ctx.my_session_->get_sql_mode();
bool is_strict = is_strict_mode(sql_mode);
ObObj real_result;
if (OB_FAIL(ObSQLUtils::check_well_formed_str(result, real_result, is_strict))) {
LOG_WARN("fail to checkout_well_formed_str", K(result), K(is_strict), K(ret));
} else {
result = real_result;
}
}
}
}
return ret;
}
int ObExprChar::calc_result_type(ObExprResType& type, ObExprResType& type1) const
int ObExprChar::calc_result_type(ObExprResType &type, ObExprResType &type1) const
{
int ret = OB_SUCCESS;
ObString cs_name;
@ -186,17 +110,17 @@ int ObExprChar::calc_result_type(ObExprResType& type, ObExprResType& type1) cons
return ret;
}
int calc_char_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
int calc_char_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum)
{
int ret = OB_SUCCESS;
ObSqlString str_buf;
ObDatum* child_res = NULL;
ObSQLSessionInfo* session = ctx.exec_ctx_.get_my_session();
ObDatum *child_res = NULL;
ObSQLSessionInfo *session = ctx.exec_ctx_.get_my_session();
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
}
for (int64_t i = 1; OB_SUCC(ret) && i < expr.arg_cnt_; ++i) {
for (int64_t i = 0; OB_SUCC(ret) && i < expr.arg_cnt_-1; ++i) {
if (OB_FAIL(expr.args_[i]->eval(ctx, child_res))) {
LOG_WARN("eval arg failed", K(ret), K(i));
} else if (child_res->is_null()) {
@ -204,7 +128,7 @@ int calc_char_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
} else {
int64_t int64_val = child_res->get_int();
int32_t int32_val = static_cast<int32_t>(int64_val);
char buf[4] = {0};
char buf[4]={0};
int64_t append_len = 0;
if (int32_val & 0xFF000000L) {
INT32_TO_STR4(buf, int32_val);
@ -225,22 +149,39 @@ int calc_char_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
}
}
if (OB_SUCC(ret)) {
const int64_t res_len = str_buf.length();
char* res_str = expr.get_str_res_mem(ctx, res_len + 1);
//
// convert utf8 to dest collation
//
ObString out;
const ObString in_str(str_buf.length(), str_buf.ptr());
ObEvalCtx::TempAllocGuard alloc_guard(ctx);
ObIAllocator &tmp_alloc = alloc_guard.get_allocator();
if (OB_FAIL(ObExprUtil::convert_string_collation(in_str, CS_TYPE_UTF8MB4_BIN, out,
expr.datum_meta_.cs_type_, tmp_alloc))) {
LOG_WARN("failed to convert string from utf8 to other collation", K(ret),
K(expr.datum_meta_.cs_type_));
}
const int64_t res_len = out.length();
char *res_str = expr.get_str_res_mem(ctx, res_len + 1);
if (OB_UNLIKELY(NULL == res_str)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("fail to allocate memory", K(res_len), K(ret));
} else {
if (NULL != str_buf.ptr()) {
MEMCPY(res_str, str_buf.ptr(), res_len);
MEMCPY(res_str, out.ptr(), res_len);
}
res_str[res_len] = '\0';
bool is_null = false;
ObString checked_res_str;
bool is_strict = is_strict_mode(session->get_sql_mode());
if (OB_FAIL(ObSQLUtils::check_well_formed_str(
ObString(res_len, res_str), expr.datum_meta_.cs_type_, checked_res_str, is_null, is_strict))) {
LOG_WARN("check_well_formed_str failed", K(ret), K(res_str), K(expr.datum_meta_));
if (OB_FAIL(ObSQLUtils::check_well_formed_str(ObString(res_len, res_str),
expr.datum_meta_.cs_type_,
checked_res_str, is_null,
is_strict))) {
LOG_WARN("check_well_formed_str failed", K(ret), K(res_str),
K(expr.datum_meta_));
} else if (is_null) {
res_datum.set_null();
} else {
@ -251,7 +192,8 @@ int calc_char_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
return ret;
}
int ObExprChar::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprChar::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(expr_cg_ctx);
@ -260,5 +202,5 @@ int ObExprChar::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObE
return ret;
}
} // end of namespace sql
} // end of namespace oceanbase
}//end of namespace sql
}//end of namespace oceanbase