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

@ -14,30 +14,37 @@
#include "ob_expr_quote.h"
#include "lib/oblog/ob_log.h"
#include "share/object/ob_obj_cast.h"
#include "sql/parser/ob_item_type.h"
#include "objit/common/ob_item_type.h"
//#include "sql/engine/expr/ob_expr_promotion_util.h"
#include "sql/session/ob_sql_session_info.h"
namespace oceanbase {
namespace oceanbase
{
using namespace common;
namespace sql {
namespace sql
{
ObExprQuote::ObExprQuote(ObIAllocator& alloc) : ObStringExprOperator(alloc, T_OP_QUOTE, N_QUOTE, 1)
{}
ObExprQuote::ObExprQuote(ObIAllocator &alloc)
: ObStringExprOperator(alloc, T_OP_QUOTE, N_QUOTE, 1)
{
}
ObExprQuote::~ObExprQuote()
{}
{
}
int ObExprQuote::calc_result_type1(ObExprResType& type, ObExprResType& type1, ObExprTypeCtx& type_ctx) const
int ObExprQuote::calc_result_type1(ObExprResType &type, ObExprResType &type1,
ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
if (is_oracle_mode()) {
const ObSQLSessionInfo* session = type_ctx.get_session();
const ObSQLSessionInfo *session = type_ctx.get_session();
if (OB_ISNULL(session)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
ObSEArray<ObExprResType*, 1, ObNullAllocator> params;
type.set_length(2 * type1.get_length() + 2);
OZ(params.push_back(&type1));
OZ(aggregate_string_type_and_charset_oracle(*session, params, type));
OZ(deduce_string_param_calc_type_and_charset(*session, type, params));
@ -45,7 +52,7 @@ int ObExprQuote::calc_result_type1(ObExprResType& type, ObExprResType& type1, Ob
} else {
type.set_varchar();
type.set_length(2 * type1.get_length() + 2);
if OB_FAIL (aggregate_charsets_for_string_result(type, &type1, 1, type_ctx.get_coll_type())) {
if OB_FAIL(aggregate_charsets_for_string_result(type, &type1, 1, type_ctx.get_coll_type())) {
LOG_WARN("aggregate charset for res failed", K(ret));
} else {
type1.set_calc_type(ObVarcharType);
@ -55,45 +62,7 @@ int ObExprQuote::calc_result_type1(ObExprResType& type, ObExprResType& type1, Ob
return ret;
}
int ObExprQuote::calc_result1(ObObj& result, const ObObj& obj, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("calc buf is NULL", K(ret));
} else if (obj.is_null()) {
// in mysql, quote(null) does not return nulltype but 'null' (without enclosing single quotation marks)
char* buf = reinterpret_cast<char*>(expr_ctx.calc_buf_->alloc(LEN_OF_NULL));
if (NULL == buf) { // alloc memory failed
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("alloc memory failed.", K(ret));
} else {
MEMCPY(buf, "NULL", LEN_OF_NULL);
ObString obstr;
obstr.assign_ptr(buf, LEN_OF_NULL);
result.set_varchar(obstr);
result.set_collation(result_type_);
}
} else {
ObString str = obj.get_string();
ObString res_str;
if (OB_FAIL(calc(res_str, str, obj.get_collation_type(), expr_ctx.calc_buf_))) {
result.set_null();
// ret = OB_ERR_INCORRECT_STRING_VALUE means input string is not invalid, then output NULL.
if (OB_ERR_INCORRECT_STRING_VALUE == ret) {
ret = OB_SUCCESS;
} else {
LOG_WARN("calc quote expr failed", K(ret), K(str));
}
} else {
result.set_varchar(res_str);
result.set_collation(result_type_);
}
}
return ret;
}
int ObExprQuote::string_write_buf(const ObString& str, char* buf, const int64_t buf_len, int64_t& pos)
int ObExprQuote::string_write_buf(const ObString &str, char *buf, const int64_t buf_len, int64_t &pos)
{
int ret = OB_SUCCESS;
if (OB_LIKELY(!str.empty())) {
@ -107,8 +76,8 @@ int ObExprQuote::string_write_buf(const ObString& str, char* buf, const int64_t
return ret;
}
int ObExprQuote::calc(ObString& res_str, ObString str, ObCollationType coll_type,
ObIAllocator* allocator) // make sure alloc() is called once
int ObExprQuote::calc(ObString &res_str, ObString str, ObCollationType coll_type,
ObIAllocator *allocator) // make sure alloc() is called once
{
int ret = OB_SUCCESS;
if (OB_ISNULL(allocator)) {
@ -118,62 +87,63 @@ int ObExprQuote::calc(ObString& res_str, ObString str, ObCollationType coll_type
ObString escape_char = ObCharsetUtils::get_const_str(coll_type, '\\');
int64_t buf_len = str.length() * 2 + 2 * escape_char.length();
int64_t pos = 0;
char* buf = reinterpret_cast<char*>(allocator->alloc(buf_len));
if (NULL == buf) { // alloc memory failed
char *buf = reinterpret_cast<char *>(allocator->alloc(buf_len));
if (NULL == buf) { //alloc memory failed
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("alloc memory failed.", K(ret));
} else {
auto handle_char_func = [&](ObString code_point, int wchar) -> int {
int ret = OB_SUCCESS;
switch (wchar) {
case '\0': {
OZ(string_write_buf(escape_char, buf, buf_len, pos));
OZ(string_write_buf(ObCharsetUtils::get_const_str(coll_type, '0'), buf, buf_len, pos));
break;
}
case '\'': {
OZ(string_write_buf(escape_char, buf, buf_len, pos));
OZ(string_write_buf(ObCharsetUtils::get_const_str(coll_type, '\''), buf, buf_len, pos));
break;
}
case '\\': {
OZ(string_write_buf(escape_char, buf, buf_len, pos));
OZ(string_write_buf(escape_char, buf, buf_len, pos));
break;
}
case '\032': {
OZ(string_write_buf(escape_char, buf, buf_len, pos));
OZ(string_write_buf(ObCharsetUtils::get_const_str(coll_type, 'Z'), buf, buf_len, pos));
break;
}
default: {
OZ(string_write_buf(code_point, buf, buf_len, pos));
break;
}
case '\0': {
OZ (string_write_buf(escape_char, buf, buf_len, pos));
OZ (string_write_buf(ObCharsetUtils::get_const_str(coll_type, '0'), buf, buf_len, pos));
break;
}
case '\'': {
OZ (string_write_buf(escape_char, buf, buf_len, pos));
OZ (string_write_buf(ObCharsetUtils::get_const_str(coll_type, '\''), buf, buf_len, pos));
break;
}
case '\\': {
OZ (string_write_buf(escape_char, buf, buf_len, pos));
OZ (string_write_buf(escape_char, buf, buf_len, pos));
break;
}
case '\032': {
OZ (string_write_buf(escape_char, buf, buf_len, pos));
OZ (string_write_buf(ObCharsetUtils::get_const_str(coll_type, 'Z'), buf, buf_len, pos));
break;
}
default: {
OZ (string_write_buf(code_point, buf, buf_len, pos));
break;
}
}
LOG_DEBUG("debug result", K(wchar), KPHEX(buf, pos));
return ret;
};
OZ(string_write_buf(ObCharsetUtils::get_const_str(coll_type, '\''), buf, buf_len, pos));
OZ(ObCharsetUtils::foreach_char(str, coll_type, handle_char_func));
OZ(string_write_buf(ObCharsetUtils::get_const_str(coll_type, '\''), buf, buf_len, pos));
OZ (string_write_buf(ObCharsetUtils::get_const_str(coll_type, '\''), buf, buf_len, pos));
OZ (ObCharsetUtils::foreach_char(str, coll_type, handle_char_func));
OZ (string_write_buf(ObCharsetUtils::get_const_str(coll_type, '\''), buf, buf_len, pos));
res_str.assign_ptr(buf, pos);
}
}
return ret;
}
int ObExprQuote::calc_quote_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
int ObExprQuote::calc_quote_expr(const ObExpr &expr, ObEvalCtx &ctx,
ObDatum &res_datum)
{
int ret = OB_SUCCESS;
ObDatum* arg = NULL;
ObDatum *arg = NULL;
if (OB_FAIL(expr.eval_param_value(ctx, arg))) {
LOG_WARN("eval param failed", K(ret));
} else if (arg->is_null()) {
// in mysql, quote(null) does not return nulltype but 'null'
// (without enclosing single quotation marks)
ObExprStrResAlloc res_alloc(expr, ctx);
char* buf = reinterpret_cast<char*>(res_alloc.alloc(LEN_OF_NULL));
char *buf = reinterpret_cast<char *>(res_alloc.alloc(LEN_OF_NULL));
if (NULL == buf) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("alloc memory failed.", K(ret));
@ -182,7 +152,7 @@ int ObExprQuote::calc_quote_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& re
res_datum.set_string(buf, LEN_OF_NULL);
}
} else {
const ObString& str = arg->get_string();
const ObString &str = arg->get_string();
ObString res_str;
ObExprStrResAlloc res_alloc(expr, ctx);
if (OB_FAIL(calc(res_str, str, expr.datum_meta_.cs_type_, &res_alloc))) {
@ -200,7 +170,8 @@ int ObExprQuote::calc_quote_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& re
return ret;
}
int ObExprQuote::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprQuote::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(expr_cg_ctx);
@ -208,5 +179,5 @@ int ObExprQuote::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, Ob
rt_expr.eval_func_ = calc_quote_expr;
return ret;
}
} // namespace sql
} // namespace oceanbase
} //namespace sql
} //namespace oceanbase