[FEAT MERGE]4_1_sql_feature

Co-authored-by: leslieyuchen <leslieyuchen@gmail.com>
Co-authored-by: Charles0429 <xiezhenjiang@gmail.com>
Co-authored-by: raywill <hustos@gmail.com>
This commit is contained in:
obdev
2023-01-28 16:01:26 +08:00
committed by ob-robot
parent 3080f2b66f
commit 2d19a9d8f5
846 changed files with 161957 additions and 116661 deletions

View File

@ -43,9 +43,10 @@ int ObExprRegexpSubstr::calc_result_typeN(ObExprResType &type,
{
UNUSED(type_ctx);
int ret = OB_SUCCESS;
ObRawExpr * raw_expr = type_ctx.get_raw_expr();
CK(NULL != type_ctx.get_raw_expr());
if (OB_FAIL(ret)) {
} else if (OB_UNLIKELY(param_num < 2 || param_num > 6)) {//由于引入的正则表达式库引擎有utf8限制,因此需要重设相关参数类型,隐式转换一下
} else if (OB_UNLIKELY(param_num < 2 || param_num > 6)) {
ret = OB_ERR_PARAM_SIZE;
LOG_WARN("param number of regexp_substr at least 2 and at most 6", K(ret), K(param_num));
} else {
@ -55,172 +56,79 @@ int ObExprRegexpSubstr::calc_result_typeN(ObExprResType &type,
LOG_WARN("the parameter is not castable", K(ret), K(i));
}
}
if (lib::is_oracle_mode()) {
// set max length.
type.set_length(static_cast<common::ObLength>(types[0].get_length()));
auto str_params = make_const_carray(&types[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(type.set_length_semantics(type_ctx.get_session()->get_actual_nls_length_semantics()));
} else {
const common::ObLengthSemantics default_length_semantics = (OB_NOT_NULL(type_ctx.get_session())
? type_ctx.get_session()->get_actual_nls_length_semantics()
: common::LS_BYTE);
type.set_varchar();
type.set_length(types[0].get_length());
type.set_length_semantics(types[0].is_varchar_or_char() ? types[0].get_length_semantics() : default_length_semantics);
ret = aggregate_charsets_for_string_result(type, types, 1, type_ctx.get_coll_type());
}
if (OB_SUCC(ret)) {//because of regexp engine need utf8 code, here need reset calc collation type and can implicit cast.
switch (param_num) {
case 6:
if (lib::is_oracle_mode()) {
types[5].set_calc_type(ObNumberType);
} else {
type_ctx.set_cast_mode(type_ctx.get_cast_mode() | CM_STRING_INTEGER_TRUNC);
types[5].set_calc_type(ObIntType);
}
case 5:
types[4].set_calc_type(ObVarcharType);
case 4:
if (lib::is_oracle_mode()) {
types[3].set_calc_type(ObNumberType);
} else {
type_ctx.set_cast_mode(type_ctx.get_cast_mode() | CM_STRING_INTEGER_TRUNC);
types[3].set_calc_type(ObIntType);
}
case 3:
if (lib::is_oracle_mode()) {
types[2].set_calc_type(ObNumberType);
} else {
type_ctx.set_cast_mode(type_ctx.get_cast_mode() | CM_STRING_INTEGER_TRUNC);
types[2].set_calc_type(ObIntType);
}
case 2:
types[1].set_calc_type(ObVarcharType);
if (ObCharset::is_bin_sort(types[1].get_calc_collation_type())) {
types[1].set_calc_collation_type(CS_TYPE_UTF8MB4_BIN);
} else {
types[1].set_calc_collation_type(CS_TYPE_UTF8MB4_GENERAL_CI);
}
types[1].set_calc_collation_level(type.get_collation_level());
if (!types[0].is_clob()) {
types[0].set_calc_type(ObVarcharType);
if (ObCharset::is_bin_sort(types[0].get_calc_collation_type())) {
types[0].set_calc_collation_type(CS_TYPE_UTF8MB4_BIN);
} else {
types[0].set_calc_collation_type(CS_TYPE_UTF8MB4_GENERAL_CI);
}
types[0].set_calc_collation_level(type.get_collation_level());
}
default:
// already check before
break;
}
}
}
return ret;
}
int ObExprRegexpSubstr::calc(ObString &result,
bool &is_null,
const ObString &text,
const ObString &pattern,
int64_t position,
int64_t occurrence,
const ObCollationType cs_type,
const ObString &match_param,
int64_t subexpr,
bool has_null_argument,
bool reusable,
ObExprRegexContext* regexp_ptr,
ObExprStringBuf &string_buf,
ObIAllocator &exec_ctx_alloc)
{
int ret = OB_SUCCESS;
//begin with '^'
bool from_begin = false;
//end with '$'
bool from_end = false;
ObExprRegexContext *regexp_substr_ctx = regexp_ptr;
ObSEArray<uint32_t, 4> begin_locations(common::ObModIds::OB_SQL_EXPR_REPLACE,
common::OB_MALLOC_NORMAL_BLOCK_SIZE);
int flags = 0, multi_flag = 0;
ObString sub;
if (OB_ISNULL(regexp_substr_ctx)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("regexp ptr is null", K(ret));
} else if (OB_FAIL(ObExprRegexpCount::get_regexp_flags(cs_type, match_param,
flags, multi_flag))) {
LOG_WARN("fail to get regexp flags", K(ret), K(match_param));
} else if (OB_FAIL(regexp_substr_ctx->init(pattern, flags,
reusable ? exec_ctx_alloc : string_buf,
reusable))) {
LOG_WARN("fail to init regexp", K(pattern), K(flags));
}
if (OB_SUCC(ret)) {
if (has_null_argument) {
is_null = true;
} else if (position <= 0 || occurrence <= 0) {
if (OB_SUCC(ret)) {
//need duduce result type, then reset calc type.
if (lib::is_oracle_mode()) {
ret = OB_ERR_ARGUMENT_OUT_OF_RANGE;
// set max length.
type.set_length(static_cast<common::ObLength>(types[0].get_length()));
auto str_params = make_const_carray(&types[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(type.set_length_semantics(type_ctx.get_session()->get_actual_nls_length_semantics()));
} else {
ret = OB_INVALID_ARGUMENT;
const common::ObLengthSemantics default_length_semantics = (OB_NOT_NULL(type_ctx.get_session())
? type_ctx.get_session()->get_actual_nls_length_semantics()
: common::LS_BYTE);
type.set_varchar();
type.set_length(types[0].get_length());
type.set_length_semantics(types[0].is_varchar_or_char() ? types[0].get_length_semantics() : default_length_semantics);
ret = aggregate_charsets_for_string_result(type, types, 1, type_ctx.get_coll_type());
}
LOG_WARN("position or occurrence is invalid", K(ret), K(position), K(occurrence));
} else {
if (!pattern.empty()) {
const char* pat = pattern.ptr();
if ('^' == pat[0]) {
from_begin = true;
} else if ('$' == pat[pattern.length() - 1]) {
from_end = true;
}
}
if (OB_FAIL(begin_locations.push_back(position - 1))) {
LOG_WARN("begin_locations push_back error", K(ret));
} else if (from_begin && 1 != position && multi_flag) {
is_null = true;
result = ObString();
} else {
ObSEArray<size_t, 4> byte_num;
ObSEArray<size_t, 4> byte_offsets;
if (OB_FAIL(ObExprUtil::get_mb_str_info(text,
ObCharset::get_default_collation_oracle(CHARSET_UTF8MB4),
byte_num,
byte_offsets))) {
LOG_WARN("failed to get mb str info", K(ret));
} else if (multi_flag) {
if (from_begin && 1 != position) {
begin_locations.pop_back();
}
const char *text_ptr = text.ptr();
for (int64_t idx = position; OB_SUCC(ret) && idx < byte_offsets.count() - 1; ++idx) {
if (OB_UNLIKELY(byte_offsets.at(idx) >= text.length())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(byte_offsets.at(idx)), K(text.length()), K(ret));
} else if (text_ptr[byte_offsets.at(idx)] == '\n') {
if (OB_FAIL(begin_locations.push_back(idx + 1))) {
LOG_WARN("begin_locations push_back error", K(ret));
}
if (OB_SUCC(ret)) {
bool is_case_sensitive = ObCharset::is_bin_sort(types[0].get_calc_collation_type());
bool need_utf8 = false;
switch (param_num) {
case 6/*subexpr*/:
types[5].set_calc_type(lib::is_oracle_mode() ? ObNumberType : ObIntType);
types[5].set_scale(lib::is_oracle_mode() ? NUMBER_SCALE_UNKNOWN_YET : DEFAULT_SCALE_FOR_INTEGER);
types[5].set_precision(lib::is_oracle_mode() ? PRECISION_UNKNOWN_YET :
ObAccuracy::DDL_DEFAULT_ACCURACY[ObIntType].precision_);
case 5/*match type*/:
types[4].set_calc_type(ObVarcharType);
types[4].set_calc_collation_type(CS_TYPE_UTF8MB4_BIN);
types[4].set_calc_collation_level(CS_LEVEL_IMPLICIT);
case 4/*occurrence*/:
types[3].set_calc_type(lib::is_oracle_mode() ? ObNumberType : ObIntType);
types[3].set_scale(lib::is_oracle_mode() ? NUMBER_SCALE_UNKNOWN_YET : DEFAULT_SCALE_FOR_INTEGER);
types[3].set_precision(lib::is_oracle_mode() ? PRECISION_UNKNOWN_YET :
ObAccuracy::DDL_DEFAULT_ACCURACY[ObIntType].precision_);
case 3/*position*/:
types[2].set_calc_type(lib::is_oracle_mode() ? ObNumberType : ObIntType);
types[2].set_scale(lib::is_oracle_mode() ? NUMBER_SCALE_UNKNOWN_YET : DEFAULT_SCALE_FOR_INTEGER);
types[2].set_precision(lib::is_oracle_mode() ? PRECISION_UNKNOWN_YET :
ObAccuracy::DDL_DEFAULT_ACCURACY[ObIntType].precision_);
type_ctx.set_cast_mode(type_ctx.get_cast_mode() | CM_STRING_INTEGER_TRUNC);
case 2/*text and pattern*/:
types[1].set_calc_type(ObVarcharType);
types[1].set_calc_collation_level(CS_LEVEL_IMPLICIT);
if (!types[0].is_clob()) {
types[0].set_calc_type(ObVarcharType);
}
}
}
if (OB_SUCC(ret)) {
if (OB_FAIL(regexp_substr_ctx->substr(text, occurrence, subexpr, sub, string_buf,
from_begin, from_end, begin_locations))) {
LOG_WARN("fail to get substr", K(ret), K(text), K(pattern),
K(occurrence), K(match_param), K(flags), K(subexpr));
} else if (lib::is_mysql_mode() && OB_ISNULL(sub.ptr())) {
is_null = true;
} else if (lib::is_oracle_mode() && OB_ISNULL(sub.ptr()) && OB_NOT_NULL(text.ptr())) {
is_null = true;
} else {
result = sub;
}
types[0].set_calc_collation_level(CS_LEVEL_IMPLICIT);
need_utf8 = false;
if (OB_FAIL(ret)) {
} else if (OB_FAIL(ObExprRegexContext::check_need_utf8(raw_expr->get_param_expr(1), need_utf8))) {
LOG_WARN("fail to check need utf8", K(ret));
} else if (need_utf8) {
types[1].set_calc_collation_type(is_case_sensitive ? CS_TYPE_UTF8MB4_BIN : CS_TYPE_UTF8MB4_GENERAL_CI);
} else {
types[1].set_calc_collation_type(is_case_sensitive ? CS_TYPE_UTF16_BIN : CS_TYPE_UTF16_GENERAL_CI);
}
need_utf8 = false;
if (OB_FAIL(ret)) {
} else if (OB_FAIL(ObExprRegexContext::check_need_utf8(raw_expr->get_param_expr(0), need_utf8))) {
LOG_WARN("fail to check need utf8", K(ret));
} else if (need_utf8) {
types[0].set_calc_collation_type(is_case_sensitive ? CS_TYPE_UTF8MB4_BIN : CS_TYPE_UTF8MB4_GENERAL_CI);
} else {
types[0].set_calc_collation_type(is_case_sensitive ? CS_TYPE_UTF16_BIN : CS_TYPE_UTF16_GENERAL_CI);
}
default:
// already check before
break;
}
}
}
@ -259,96 +167,124 @@ int ObExprRegexpSubstr::eval_regexp_substr(
ObDatum *pattern = NULL;
ObDatum *position = NULL;
ObDatum *occurrence = NULL;
ObDatum *flags = NULL;
ObDatum *match_type = NULL;
ObDatum *subexpr= NULL;
int64_t pos = 1;
int64_t occur = 1;
int64_t subexpr_val = 0;
if (OB_FAIL(expr.eval_param_value(ctx, text, pattern, position, occurrence, flags, subexpr))) {
if (OB_FAIL(expr.eval_param_value(ctx, text, pattern, position, occurrence, match_type, subexpr))) {
LOG_WARN("evaluate parameters failed", K(ret));
} else if (OB_FAIL(ObExprUtil::get_int_param_val(position, pos))
|| OB_FAIL(ObExprUtil::get_int_param_val(occurrence, occur))
|| OB_FAIL(ObExprUtil::get_int_param_val(subexpr, subexpr_val))) {
LOG_WARN("get integer parameter value failed", K(ret));
} else if (pos < 0 || occur <= 0 || subexpr_val < 0) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("regexp_substr position or occurrence or subexpr is invalid",
K(ret), K(pos), K(occur), K(subexpr_val));
} else {
// %flags may be null
bool is_flag_null = (NULL != flags && flags->is_null());
const bool null_result = (text->is_null()
|| pattern->is_null()
|| (NULL != position && position->is_null())
|| (NULL != occurrence && occurrence->is_null())
|| (NULL != subexpr && subexpr->is_null())
|| (lib::is_mysql_mode() && is_flag_null));
ObString match_param = (NULL != flags && !flags->is_null())
? flags->get_string()
: ObString();
ObString res;
ObEvalCtx::TempAllocGuard alloc_guard(ctx);
ObIAllocator &alloc = alloc_guard.get_allocator();
bool is_null = false;
ObExprRegexContext local_regex_ctx;
ObExprRegexContext *regexp_ctx = &local_regex_ctx;
const bool reusable = (0 != expr.extra_) && ObExpr::INVALID_EXP_CTX_ID != expr.expr_ctx_id_;
if (OB_SUCC(ret) && reusable) {
if (NULL == (regexp_ctx = static_cast<ObExprRegexContext *>(
ctx.exec_ctx_.get_expr_op_ctx(expr.expr_ctx_id_)))) {
if (OB_FAIL(ctx.exec_ctx_.create_expr_op_ctx(expr.expr_ctx_id_, regexp_ctx))) {
LOG_WARN("create expr regex context failed", K(ret), K(expr));
} else if (OB_ISNULL(regexp_ctx)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("NULL context returned", K(ret));
}
}
}
if (OB_FAIL(ret)) {
} else if (lib::is_mysql_mode() && !pattern->is_null() &&
pattern->get_string().empty() && !is_flag_null) {//compatible mysql
} else if (OB_UNLIKELY(expr.arg_cnt_ < 2 ||
(expr.args_[0]->datum_meta_.cs_type_ != CS_TYPE_UTF8MB4_GENERAL_CI &&
expr.args_[0]->datum_meta_.cs_type_ != CS_TYPE_UTF8MB4_BIN &&
expr.args_[0]->datum_meta_.cs_type_ != CS_TYPE_UTF16_GENERAL_CI &&
expr.args_[0]->datum_meta_.cs_type_ != CS_TYPE_UTF16_BIN) ||
(expr.args_[1]->datum_meta_.cs_type_ != CS_TYPE_UTF8MB4_GENERAL_CI &&
expr.args_[1]->datum_meta_.cs_type_ != CS_TYPE_UTF8MB4_BIN &&
expr.args_[1]->datum_meta_.cs_type_ != CS_TYPE_UTF16_GENERAL_CI &&
expr.args_[1]->datum_meta_.cs_type_ != CS_TYPE_UTF16_BIN))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected error", K(ret), K(expr));
} else if (lib::is_mysql_mode() && !pattern->is_null() && pattern->get_string().empty()) {
if (NULL == match_type || !match_type->is_null()) {
ret = OB_ERR_REGEXP_ERROR;
LOG_WARN("empty regex expression", K(ret));
} else if (OB_FAIL(calc(res, is_null, text->get_string(), pattern->get_string(), pos, occur,
expr.datum_meta_.cs_type_, match_param, subexpr_val,
null_result, reusable, regexp_ctx, alloc,
ctx.exec_ctx_.get_allocator()))) {
LOG_WARN("calc failed", K(ret));
} else if (null_result) {
expr_datum.set_null();
} else {
if (is_null || (res.empty()
&& lib::is_oracle_mode()
&& !(expr.args_[0]->datum_meta_.is_clob()))) {
expr_datum.set_null();
}
} else {
bool null_result = (position != NULL && position->is_null()) ||
(occurrence != NULL && occurrence->is_null()) ||
(subexpr != NULL && subexpr->is_null()) ||
(lib::is_mysql_mode() && match_type != NULL && match_type->is_null());
if (OB_FAIL(ObExprUtil::get_int_param_val(position, pos))
|| OB_FAIL(ObExprUtil::get_int_param_val(occurrence, occur))
|| OB_FAIL(ObExprUtil::get_int_param_val(subexpr, subexpr_val))) {
LOG_WARN("get integer parameter value failed", K(ret));
} else if (!null_result && (pos <= 0 || occur <= 0 || subexpr_val < 0)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("regexp_substr position or occurrence or subexpr is invalid",
K(ret), K(pos), K(occur), K(subexpr_val));
LOG_USER_ERROR(OB_INVALID_ARGUMENT, "use position or occurrence or subexpr in regexp_substr");
} else {
ObString match_param = (NULL != match_type && !match_type->is_null()) ? match_type->get_string() : ObString();
ObEvalCtx::TempAllocGuard alloc_guard(ctx);
ObIAllocator &tmp_alloc = alloc_guard.get_allocator();
ObExprRegexContext local_regex_ctx;
ObExprRegexContext *regexp_ctx = &local_regex_ctx;
const bool reusable = (0 != expr.extra_) && ObExpr::INVALID_EXP_CTX_ID != expr.expr_ctx_id_;
bool is_case_sensitive = ObCharset::is_bin_sort(expr.args_[0]->datum_meta_.cs_type_);
ObString res_substr;
ObString text_utf16;
ObCollationType res_coll_type = CS_TYPE_INVALID;
bool is_null = true;
uint32_t flags = 0;
if (reusable) {
if (NULL == (regexp_ctx = static_cast<ObExprRegexContext *>(
ctx.exec_ctx_.get_expr_op_ctx(expr.expr_ctx_id_)))) {
if (OB_FAIL(ctx.exec_ctx_.create_expr_op_ctx(expr.expr_ctx_id_, regexp_ctx))) {
LOG_WARN("create expr regex context failed", K(ret), K(expr));
} else if (OB_ISNULL(regexp_ctx)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("NULL context returned", K(ret));
}
}
}
if (OB_FAIL(ret)) {
} else if (OB_FAIL(ObExprRegexContext::get_regexp_flags(match_param, is_case_sensitive, flags))) {
LOG_WARN("fail to get regexp flags", K(ret), K(match_param));
} else if (!pattern->is_null() && !null_result &&
OB_FAIL(regexp_ctx->init(reusable ? ctx.exec_ctx_.get_allocator() : tmp_alloc,
ctx.exec_ctx_.get_my_session(),
pattern->get_string(), flags, reusable,
expr.args_[1]->datum_meta_.cs_type_))) {
LOG_WARN("fail to init regexp", K(pattern), K(flags), K(ret));
} else if (text->is_null() || pattern->is_null() || null_result ||
(NULL != position && position->is_null()) ||
(NULL != occurrence && occurrence->is_null()) ||
(NULL != subexpr && subexpr->is_null()) ||
(lib::is_mysql_mode() && NULL != match_type && match_type->is_null())) {
expr_datum.set_null();
} else if (res.empty()
&& lib::is_oracle_mode()
&& expr.args_[0]->datum_meta_.is_clob()) {
} else {
is_null = false;
if (expr.args_[0]->datum_meta_.cs_type_ == CS_TYPE_UTF8MB4_BIN ||
expr.args_[0]->datum_meta_.cs_type_ == CS_TYPE_UTF8MB4_GENERAL_CI) {
res_coll_type = ObCharset::is_bin_sort(expr.args_[0]->datum_meta_.cs_type_) ? CS_TYPE_UTF16_BIN : CS_TYPE_UTF16_GENERAL_CI;
if (OB_FAIL(ObExprUtil::convert_string_collation(text->get_string(), expr.args_[0]->datum_meta_.cs_type_, text_utf16,
res_coll_type, tmp_alloc))) {
LOG_WARN("convert charset failed", K(ret));
}
} else {
res_coll_type = expr.args_[0]->datum_meta_.cs_type_;
text_utf16 = text->get_string();
}
}
if (OB_FAIL(ret) || is_null) {
} else if (OB_FAIL(regexp_ctx->substr(tmp_alloc, text_utf16, pos - 1,
occur, subexpr_val, res_substr))) {
LOG_WARN("failed to regexp substr", K(ret));
} else if (res_substr.empty() && lib::is_oracle_mode() && expr.args_[0]->datum_meta_.is_clob()) {
expr_datum.set_string(ObString().ptr(), ObString().length());
} else if (res_substr.empty()) {
expr_datum.set_null();
} else {
ObExprStrResAlloc out_alloc(expr, ctx);
ObString out;
if (OB_FAIL(ObExprUtil::convert_string_collation(res,
expr.args_[0]->datum_meta_.cs_type_,
out,
expr.datum_meta_.cs_type_,
out_alloc))) {
if (OB_FAIL(ObExprUtil::convert_string_collation(res_substr, res_coll_type,
out, expr.datum_meta_.cs_type_, out_alloc))) {
LOG_WARN("convert charset failed", K(ret));
} else {
if (out.ptr() == res.ptr()) {
// res is allocated in temporary allocator, deep copy here.
char *mem = expr.get_str_res_mem(ctx, res.length());
if (OB_ISNULL(mem)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else {
MEMCPY(mem, res.ptr(), res.length());
expr_datum.set_string(mem, res.length());
}
} else if (out.ptr() == res_substr.ptr()) {
// res_substr is allocated in temporary allocator, deep copy here.
char *mem = expr.get_str_res_mem(ctx, res_substr.length());
if (OB_ISNULL(mem)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("allocate memory failed", K(ret));
} else {
expr_datum.set_string(out.ptr(), out.length());
MEMCPY(mem, res_substr.ptr(), res_substr.length());
expr_datum.set_string(mem, res_substr.length());
}
} else {
expr_datum.set_string(out.ptr(), out.length());
}
}
}
@ -357,4 +293,4 @@ int ObExprRegexpSubstr::eval_regexp_substr(
}
}
}
}