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,39 +10,46 @@
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_ENG
#define USING_LOG_PREFIX SQL_ENG
#include "ob_expr_convert.h"
#include "lib/charset/ob_charset.h"
#include "sql/engine/expr/ob_expr_cast.h"
#include "sql/session/ob_sql_session_info.h"
#include "sql/engine/expr/ob_expr_result_type_util.h"
using namespace oceanbase::common;
using namespace oceanbase::sql;
namespace oceanbase {
namespace sql {
namespace oceanbase
{
namespace sql
{
ObExprConvert::ObExprConvert(ObIAllocator& alloc)
ObExprConvert::ObExprConvert(ObIAllocator &alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_CONVERT, N_CONVERT, 2, NOT_ROW_DIMENSION)
{}
{
}
ObExprConvert::~ObExprConvert()
{}
{
}
int ObExprConvert::calc_result_type2(
ObExprResType& type, ObExprResType& type1, ObExprResType& type2, ObExprTypeCtx& type_ctx) const
int ObExprConvert::calc_result_type2(ObExprResType &type,
ObExprResType &type1,
ObExprResType &type2,
ObExprTypeCtx &type_ctx) const
{
UNUSED(type_ctx);
int ret = OB_SUCCESS;
type.set_type(type1.get_type());
type.set_scale(type1.get_scale());
type.set_type(ObVarcharType); // Only convert (xx using collation) will reach here now. It must be a varchar result.
type.set_scale(type1.get_scale());
type.set_precision(type1.get_precision());
if (ob_is_string_type(type.get_type())) {
type.set_length(type1.get_length());
}
const ObObj& dest_collation = type2.get_param();
const ObObj &dest_collation = type2.get_param();
TYPE_CHECK(dest_collation, ObVarcharType);
if (OB_SUCC(ret)) {
ObString cs_name = dest_collation.get_string();
@ -53,12 +60,15 @@ int ObExprConvert::calc_result_type2(
} else {
type.set_collation_level(CS_LEVEL_EXPLICIT);
type.set_collation_type(ObCharset::get_default_collation(charset_type));
// set calc type
// only set type2 here.
//set calc type
//only set type2 here.
type2.set_calc_type(ObVarcharType);
// cast表达式会对convert表达式的第一个子节点cast为type1,计算时convert的结果就是第一个
// 子节点的结果
type1.set_calc_meta(type.get_obj_meta());
type1.set_calc_collation_type(type.get_collation_type());
type1.set_calc_collation_level(type.get_collation_level());
type_ctx.set_cast_mode(type_ctx.get_cast_mode() | CM_CHARSET_CONVERT_IGNORE_ERR);
LOG_DEBUG("in calc result type", K(ret), K(type1), K(type2), K(type));
}
}
@ -66,19 +76,10 @@ int ObExprConvert::calc_result_type2(
return ret;
}
int ObExprConvert::calc_result2(ObObj& result, const ObObj& obj1, const ObObj& obj2, ObExprCtx& expr_ctx) const
int calc_convert_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum)
{
int ret = OB_SUCCESS;
UNUSED(obj2);
UNUSED(expr_ctx);
result = obj1;
return ret;
}
int calc_convert_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
ObDatum* child_res = NULL;
ObDatum *child_res = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, child_res))) {
LOG_WARN("eval arg 0 failed", K(ret));
} else {
@ -87,7 +88,8 @@ int calc_convert_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
return ret;
}
int ObExprConvert::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
int ObExprConvert::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(expr_cg_ctx);
@ -96,5 +98,149 @@ int ObExprConvert::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr,
return ret;
}
} // namespace sql
} // namespace oceanbase
ObExprConvertOracle::ObExprConvertOracle(ObIAllocator &alloc)
: ObStringExprOperator(alloc, T_FUN_SYS_CONVERT, N_CONVERT, TWO_OR_THREE)
{
}
ObExprConvertOracle::~ObExprConvertOracle()
{
}
int ObExprConvertOracle::calc_result_typeN(ObExprResType &type,
ObExprResType *types_array,
int64_t param_num,
ObExprTypeCtx &type_ctx) const
{
int ret = OB_SUCCESS;
if (!(param_num >= 2 && param_num <= 3)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("param num should be 2 or 3", K(ret));
}
//result meta deduce
if (OB_SUCC(ret)) {
ObLength length;
auto str_params = make_const_carray(&types_array[0]);
OZ (aggregate_string_type_and_charset_oracle(*type_ctx.get_session(),
str_params,
type,
PREFER_VAR_LEN_CHAR));
OZ (deduce_string_param_calc_type_and_charset(*type_ctx.get_session(),
type,
str_params));
OX (length = types_array[0].get_calc_length());
OX (type.set_length(length * ObCharset::MAX_MB_LEN));
}
//param calc type deduce
if (OB_SUCC(ret)) {
types_array[1].set_calc_type_default_varchar();
if (3 == param_num) {
types_array[2].set_calc_type_default_varchar();
}
}
return ret;
}
int ObExprConvertOracle::calc_convert_oracle_expr(const ObExpr &expr,
ObEvalCtx &ctx,
ObDatum &res_datum)
{
int ret = OB_SUCCESS;
ObDatum *src_param = NULL;
ObCollationType src_cs_type = CS_TYPE_INVALID;
ObCollationType dst_cs_type = CS_TYPE_INVALID;
ObValueChecker<int> charset_checker(CHARSET_INVALID + 1, CHARSET_MAX - 1,
OB_ERR_UNSUPPORTED_CHARACTER_SET);
//param1
if (OB_FAIL(expr.args_[0]->eval(ctx, src_param))) {
LOG_WARN("eval arg failed", K(ret));
}
//param2
if (OB_SUCC(ret)) {
ObString dst_character_set;
ObDatum *dst_cs_type_param = NULL;
if (OB_FAIL(expr.args_[1]->eval(ctx, dst_cs_type_param))) {
LOG_WARN("eval arg failed", K(ret));
} else {
dst_character_set = dst_cs_type_param->get_string();
dst_cs_type = ObCharset::get_default_collation_by_mode(
ObCharset::charset_type_by_name_oracle(dst_character_set), lib::is_oracle_mode());
if (OB_FAIL(charset_checker.validate(ObCharset::charset_type_by_coll(dst_cs_type)))) {
LOG_WARN("invalid charset value", K(ret), K(dst_character_set));
}
}
}
//param3
if (OB_SUCC(ret)) {
if (3 == expr.arg_cnt_) {
ObString src_character_set;
ObDatum *src_cs_type_param = NULL;
if (OB_FAIL(expr.args_[2]->eval(ctx, src_cs_type_param))) {
LOG_WARN("eval arg failed", K(ret));
} else {
src_character_set = src_cs_type_param->get_string();
src_cs_type = ObCharset::get_default_collation_by_mode(
ObCharset::charset_type_by_name_oracle(src_character_set), lib::is_oracle_mode());
if (OB_FAIL(charset_checker.validate(ObCharset::charset_type_by_coll(src_cs_type)))) {
LOG_WARN("invalid charset value", K(ret), K(src_character_set));
}
}
} else {
src_cs_type = expr.args_[0]->datum_meta_.cs_type_;
}
}
//convert result
if (OB_SUCC(ret)) {
if (src_param->is_null()) {
res_datum.set_null();
} else {
ObString src = src_param->get_string();
ObString dst;
char *res_buf = NULL;
const int64_t res_buf_len = src.length() * ObCharset::MAX_MB_LEN;
if (OB_ISNULL(res_buf = expr.get_str_res_mem(ctx, res_buf_len))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret), K(res_buf_len));
} else {
ObDataBuffer data_buf(res_buf, res_buf_len);
if (OB_FAIL(ObCharset::charset_convert(data_buf, src, src_cs_type, dst_cs_type, dst,
ObCharset::REPLACE_UNKNOWN_CHARACTER))) {
LOG_WARN("fail to convert input string", K(src), K(src_cs_type), K(dst_cs_type),
KPHEX(src.ptr(), src.length()), K(res_buf_len));
} else {
if (dst.empty()) {
res_datum.set_null();
} else {
res_datum.set_string(dst);
}
}
}
}
}
return ret;
}
int ObExprConvertOracle::cg_expr(ObExprCGCtx &op_cg_ctx,
const ObRawExpr &raw_expr,
ObExpr &rt_expr) const
{
int ret = OB_SUCCESS;
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
rt_expr.eval_func_ = calc_convert_oracle_expr;
return ret;
}
} //namespace sql
} //namespace oceanbase