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

@ -12,103 +12,148 @@
#define USING_LOG_PREFIX SQL_ENG
#include "sql/engine/expr/ob_expr_left.h"
#include "sql/engine/expr/ob_expr_util.h"
#include "lib/charset/ob_charset.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/ob_exec_context.h"
using namespace oceanbase::common;
using namespace oceanbase::sql;
namespace oceanbase {
namespace sql {
namespace oceanbase
{
namespace sql
{
ObExprLeft::ObExprLeft(ObIAllocator& alloc) : ObStringExprOperator(alloc, T_FUN_SYS_LEFT, "left", 2)
{}
ObExprLeft::ObExprLeft(ObIAllocator &alloc)
: ObStringExprOperator(alloc, T_FUN_SYS_LEFT, "left", 2)
{
}
ObExprLeft::~ObExprLeft()
{}
int ObExprLeft::calc_result_type2(
ObExprResType& type, ObExprResType& type1, ObExprResType& type2, ObExprTypeCtx& type_ctx) const
{
int ret = OB_SUCCESS;
type2.set_calc_type(ObIntType);
type_ctx.set_cast_mode(type_ctx.get_cast_mode() | CM_STRING_INTEGER_TRUNC);
type.set_varchar();
type.set_length(type1.get_length());
OZ(aggregate_charsets_for_string_result(type, &type1, 1, type_ctx.get_coll_type()));
OX(type1.set_calc_type(ObVarcharType));
OX(type1.set_calc_collation_type(type.get_collation_type()));
return ret;
}
int calc_left(ObString& res_str, const ObString& text, const ObCollationType type, const int64_t required_num_char)
// to make "select left('abcd', '1.9')" compatible with mysql
int ObExprLeft::cast_param_type(const ObObj& in,
ObExprCtx& expr_ctx,
ObObj& out) const
{
int ret = OB_SUCCESS;
const char* str_ptr = text.ptr();
int64_t str_length = text.length();
if (OB_ISNULL(str_ptr) && 0 != str_length) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret));
} else {
int64_t input_num_char = ObCharset::strlen_char(type, text.ptr(), text.length());
int64_t expected_num_char = min(required_num_char, input_num_char);
int64_t start_pos = 0;
int64_t char_length = 0;
while ((start_pos < str_length) && OB_SUCC(ret) && (0 < expected_num_char)) {
if (OB_FAIL(ObCharset::first_valid_char(type, str_ptr + start_pos, str_length - start_pos, char_length))) {
LOG_WARN("get char failed", K(ret));
} else {
start_pos += char_length;
--expected_num_char;
}
}
if (OB_SUCC(ret)) {
res_str.assign_ptr(str_ptr, static_cast<int32_t>(start_pos));
}
}
return ret;
}
int ObExprLeft::calc_result2(ObObj& result, const ObObj& obj1, const ObObj& obj2, ObExprCtx& expr_ctx) const
{
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() || obj2.is_null()) {
result.set_null();
} else {
TYPE_CHECK(obj1, ObVarcharType);
TYPE_CHECK(obj2, ObIntType);
ObString res_str;
if (OB_FAIL(calc_left(res_str, obj1.get_string(), obj1.get_collation_type(), obj2.get_int()))) {
LOG_WARN("failed to calculate left expression", K(ret));
ObCastMode cast_mode = CM_NONE;
EXPR_DEFINE_CAST_CTX(expr_ctx, cast_mode);
// select left('abcd', '1.9'); -- MySQL will trunc '1.9' to 2
// select left('abcd', 1.9); -- MySQL will round 1.9 to 1
if (ObVarcharType == in.get_type()) {
int64_t tmp = 0;
if (OB_FAIL(ObExprUtil::get_trunc_int64(in, expr_ctx, tmp))) {
LOG_WARN("ObExprLeft get_trunc_int64 failed", K(in.get_type()));
} else if (INT_MAX < tmp) {
out.set_int(INT_MAX);
} else if (INT_MIN > tmp) {
out.set_int(INT_MIN);
} else {
result.set_varchar(res_str);
result.set_collation(result_type_);
out.set_int(static_cast<int>(tmp));
}
} else if (OB_FAIL(ObObjCaster::to_type(ObIntType, cast_ctx, in, out))) {
LOG_WARN("ObExprLeft to_type failed", K(in.get_type()));
}
return ret;
}
int calc_left_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
int ObExprLeft::calc_result_type2(ObExprResType &type,
ObExprResType &type1,
ObExprResType &type2,
ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
ObSQLSessionInfo *session = const_cast<ObSQLSessionInfo *>(type_ctx.get_session());
ObExecContext *exec_ctx = nullptr;
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else if (OB_ISNULL(exec_ctx = session->get_cur_exec_ctx())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("exec context is NULL", K(ret));
} else if (exec_ctx->is_ps_prepare_stage()) {
// the ps prepare stage does not do type deduction, and directly gives a default type.
type.set_char();
type.set_length(0);
} else {
type2.set_calc_type(ObIntType);
type_ctx.set_cast_mode(type_ctx.get_cast_mode() | CM_STRING_INTEGER_TRUNC);
type.set_varchar();
int64_t result_len = type1.get_length();
const ObObj &result_len_obj = type2.get_param();
ObObj int_obj;
ObExprCtx tmp_expr_ctx;
ObArenaAllocator allocator(common::ObModIds::OB_SQL_EXPR_CALC);
tmp_expr_ctx.calc_buf_ = &allocator;
tmp_expr_ctx.cast_mode_ = type_ctx.get_cast_mode();
if (OB_FAIL(cast_param_type(result_len_obj, tmp_expr_ctx, int_obj))) {
LOG_WARN("cast_param_type failed", K(result_len_obj.get_type()));
} else if (int_obj.get_int() <= 0) {
type.set_char();
type.set_length(0);
} else {
type.set_length(int_obj.get_int());
}
OZ(aggregate_charsets_for_string_result(type, &type1, 1, type_ctx.get_coll_type()));
OX(type1.set_calc_type(ObVarcharType));
OX(type1.set_calc_collation_type(type.get_collation_type()));
}
return ret;
}
int calc_left(ObString &res_str, const ObString &text, const ObCollationType type,
const int64_t required_num_char)
{
int ret = OB_SUCCESS;
const char* str_ptr = text.ptr();
int64_t str_length = text.length();
if(OB_ISNULL(str_ptr) && 0 != str_length) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("invalid argument", K(ret));
} else {
int64_t input_num_char = ObCharset::strlen_char(type, text.ptr(), text.length());
int64_t expected_num_char = min(required_num_char, input_num_char);
int64_t start_pos = 0;
int64_t char_length = 0;
while((start_pos < str_length) && OB_SUCC(ret) && (0 < expected_num_char)) {
if(OB_FAIL(ObCharset::first_valid_char(type, str_ptr + start_pos,
str_length - start_pos, char_length))) {
LOG_WARN("get char failed", K(ret));
} else {
start_pos += char_length;
--expected_num_char;
}
}
if(OB_SUCC(ret)) {
res_str.assign_ptr(str_ptr, static_cast<int32_t> (start_pos));
}
}
return ret;
}
int calc_left_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum)
{
int ret = OB_SUCCESS;
// left(s, n)
ObDatum* s_datum = NULL;
ObDatum* n_datum = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, s_datum)) || OB_FAIL(expr.args_[1]->eval(ctx, n_datum))) {
ObDatum *s_datum = NULL;
ObDatum *n_datum = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, s_datum)) ||
OB_FAIL(expr.args_[1]->eval(ctx, n_datum))) {
LOG_WARN("eval arg failed", K(ret), KP(s_datum), KP(n_datum));
} else if (s_datum->is_null() || n_datum->is_null()) {
res_datum.set_null();
} else {
// res_str will point to the memory space of s_datum, so the string pointed to
// by res_str cannot be changed below
// res_str会指向s_datum的内存空间,所以下面不能改变res_str指向的字符串
ObString res_str;
const ObCollationType arg_cs_type = expr.args_[0]->datum_meta_.cs_type_;
if (OB_FAIL(calc_left(res_str, s_datum->get_string(), arg_cs_type, n_datum->get_int()))) {
LOG_WARN("failed to calculate left expression", K(ret));
} else {
if(OB_FAIL(calc_left(res_str, s_datum->get_string(), arg_cs_type, n_datum->get_int()))) {
LOG_WARN("failed to calculate left expression", K(ret));
} else {
if (res_str.empty() && is_oracle_mode()) {
res_datum.set_null();
} else {
@ -119,7 +164,9 @@ int calc_left_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
return ret;
}
int ObExprLeft::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprLeft::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(expr_cg_ctx);