[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:
@ -42,6 +42,7 @@ int ObExprRegexpCount::calc_result_typeN(ObExprResType &type,
|
||||
common::ObExprTypeCtx &type_ctx) const
|
||||
{
|
||||
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 > 4)) {
|
||||
@ -55,189 +56,51 @@ int ObExprRegexpCount::calc_result_typeN(ObExprResType &type,
|
||||
}
|
||||
}
|
||||
if (OB_SUCC(ret)) {
|
||||
switch (param_num) {//because of regexp engine need utf8 code, here need reset calc collation type and can implicit cast.
|
||||
case 4:
|
||||
bool is_case_sensitive = ObCharset::is_bin_sort(types[0].get_calc_collation_type());
|
||||
bool need_utf8 = false;
|
||||
switch (param_num) {
|
||||
case 4/*match type*/:
|
||||
types[3].set_calc_type(ObVarcharType);
|
||||
case 3:
|
||||
types[3].set_calc_collation_type(CS_TYPE_UTF8MB4_BIN);
|
||||
types[3].set_calc_collation_level(CS_LEVEL_IMPLICIT);
|
||||
case 3/*postition*/:
|
||||
types[2].set_calc_type(ObNumberType);
|
||||
case 2:
|
||||
types[2].set_scale(NUMBER_SCALE_UNKNOWN_YET);
|
||||
types[2].set_precision(PRECISION_UNKNOWN_YET);
|
||||
case 2/*pattern and text*/:
|
||||
//why we set the calc collation type is utf16, because the ICU regexp engine is used uft16,
|
||||
//we need convert it the need collation in advance, and no need to think about in regexp.
|
||||
//lob TODO,jiangxiu.wt
|
||||
types[1].set_calc_type(ObVarcharType);
|
||||
types[1].set_calc_collation_type(ObCharset::get_default_collation_oracle(CHARSET_UTF8MB4));
|
||||
types[1].set_calc_collation_level(CS_LEVEL_IMPLICIT);
|
||||
if (!types[0].is_clob()) {
|
||||
types[0].set_calc_type(ObVarcharType);
|
||||
types[0].set_calc_collation_type(ObCharset::get_default_collation_oracle(CHARSET_UTF8MB4));
|
||||
}
|
||||
types[0].set_calc_collation_level(CS_LEVEL_IMPLICIT);
|
||||
need_utf8 = false;
|
||||
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;
|
||||
}
|
||||
type.set_calc_type(ObVarcharType);
|
||||
type.set_calc_collation_type(ObCharset::get_default_collation_oracle(CHARSET_UTF8MB4));
|
||||
type.set_calc_collation_level(CS_LEVEL_IMPLICIT);
|
||||
type.set_number();
|
||||
type.set_precision(PRECISION_UNKNOWN_YET);
|
||||
type.set_scale(NUMBER_SCALE_UNKNOWN_YET);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprRegexpCount::calc(int64_t &ret_count,
|
||||
const ObString &text,
|
||||
const ObString &pattern,
|
||||
int64_t position,
|
||||
ObCollationType calc_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;
|
||||
int64_t sub = 0;
|
||||
ret_count = 0;
|
||||
ObExprRegexContext *regexp_count_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;
|
||||
if (OB_ISNULL(regexp_count_ctx)) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("regexp_count_ctx is null", K(ret));
|
||||
} else if (OB_FAIL(get_regexp_flags(calc_cs_type, match_param, flags, multi_flag))) {
|
||||
LOG_WARN("fail to get regexp flags", K(ret), K(match_param));
|
||||
} else if (OB_FAIL(regexp_count_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)) {
|
||||
//为了更好的和oracle兼容
|
||||
if (has_null_argument) {
|
||||
/*do nothing*/
|
||||
} else if (lib::is_mysql_mode() && pattern.empty()) {
|
||||
ret = OB_ERR_REGEXP_ERROR;
|
||||
LOG_WARN("empty regex expression", K(ret));
|
||||
} else {
|
||||
if (OB_NOT_NULL(pattern)) {
|
||||
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) {
|
||||
ret_count = sub;
|
||||
} 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)) {
|
||||
if (OB_FAIL(regexp_count_ctx->count_match_str(text, subexpr, sub, string_buf, from_begin,
|
||||
from_end, begin_locations))) {
|
||||
LOG_WARN("fail to get count_match_str", K(ret), K(text), K(pattern),
|
||||
K(match_param), K(flags), K(subexpr));
|
||||
} else {
|
||||
ret_count = sub;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprRegexpCount::get_regexp_flags(const common::ObCollationType calc_cs_type,
|
||||
const ObString &match_param, int& flags,
|
||||
int &multi_flag)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
int n_flag = 1;
|
||||
int m_flag = 0;
|
||||
const char *ptr = match_param.ptr();
|
||||
int length = match_param.length();
|
||||
flags = OB_REG_EXTENDED | OB_REG_NLSTOP | OB_REG_NEWLINE;
|
||||
if (lib::is_oracle_mode()) {
|
||||
flags |= OB_REG_ORACLE_MODE;
|
||||
} else {
|
||||
flags &= ~OB_REG_ORACLE_MODE;
|
||||
}
|
||||
if (!ObCharset::is_bin_sort(calc_cs_type)) {
|
||||
flags |= OB_REG_ICASE;
|
||||
}
|
||||
for (int i = 0; OB_SUCC(ret) && i < length; i++) {
|
||||
char c = ptr[i];
|
||||
switch (c) {
|
||||
case 'i':
|
||||
flags |= OB_REG_ICASE;
|
||||
break;
|
||||
case 'c':
|
||||
flags &= ~OB_REG_ICASE;
|
||||
break;
|
||||
case 'n':
|
||||
flags &= ~OB_REG_NLSTOP;
|
||||
n_flag = 0;
|
||||
break;
|
||||
case 'm':
|
||||
flags &= ~OB_REG_NLSTOP;
|
||||
flags |= OB_REG_NEWLINE;
|
||||
flags |= OB_REG_NLANCH;
|
||||
m_flag = 1;
|
||||
break;
|
||||
case 'x':
|
||||
if (lib::is_oracle_mode()) {//compatible oracle
|
||||
flags |= OB_REG_EXPANDED;
|
||||
} else {
|
||||
ret = OB_INVALID_ARGUMENT;
|
||||
LOG_WARN("invalid match_param", K(match_param));
|
||||
}
|
||||
break;
|
||||
case 'u':
|
||||
if (lib::is_mysql_mode()) {//compatible oracle
|
||||
flags |= OB_REG_NEWLINE;
|
||||
} else {
|
||||
ret = OB_INVALID_ARGUMENT;
|
||||
LOG_WARN("invalid match_param", K(match_param));
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ret = OB_INVALID_ARGUMENT;
|
||||
LOG_WARN("invalid match_param", K(match_param));
|
||||
}
|
||||
}
|
||||
if (OB_SUCC(ret)) {
|
||||
multi_flag = m_flag & n_flag;
|
||||
if (m_flag == 1 && n_flag == 0) {
|
||||
flags &= ~OB_REG_NEWLINE;
|
||||
flags &= ~OB_REG_NLANCH;
|
||||
type.set_precision(PRECISION_UNKNOWN_YET);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
@ -275,57 +138,88 @@ int ObExprRegexpCount::eval_regexp_count(
|
||||
ObDatum *text = NULL;
|
||||
ObDatum *pattern = NULL;
|
||||
ObDatum *position = NULL;
|
||||
ObDatum *flags = NULL;
|
||||
if (OB_FAIL(expr.eval_param_value(ctx, text, pattern, position, flags))) {
|
||||
ObDatum *match_type = NULL;
|
||||
if (OB_FAIL(expr.eval_param_value(ctx, text, pattern, position, match_type))) {
|
||||
LOG_WARN("evaluate parameters failed", K(ret));
|
||||
} 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 {
|
||||
// use default flags if NULL == flags and flags->is_null()
|
||||
const bool null_result = (text->is_null() || pattern->is_null()
|
||||
|| (NULL != position && position->is_null()) ||
|
||||
(lib::is_mysql_mode() && NULL != flags && flags->is_null()));
|
||||
int64_t pos = 1;
|
||||
ObString match_param = (NULL != flags && !flags->is_null())
|
||||
? flags->get_string()
|
||||
: ObString();
|
||||
if (OB_FAIL(ObExprUtil::get_int_param_val(position, pos))) {
|
||||
if (position != NULL && position->is_null()) {
|
||||
expr_datum.set_null();
|
||||
} else if (OB_FAIL(ObExprUtil::get_int_param_val(position, pos))) {
|
||||
LOG_WARN("truncate number to int64 failed", K(ret));
|
||||
} else if (pos <= 0) {
|
||||
ret = OB_ERR_ARGUMENT_OUT_OF_RANGE;
|
||||
LOG_WARN("position out of range", K(ret), K(position), K(pos));
|
||||
}
|
||||
|
||||
ObEvalCtx::TempAllocGuard alloc_guard(ctx);
|
||||
ObIAllocator &alloc = alloc_guard.get_allocator();
|
||||
ObNumStackOnceAlloc num_alloc;
|
||||
number::ObNumber num;
|
||||
int64_t subexpr = 0;
|
||||
int64_t cnt = 0;
|
||||
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));
|
||||
} 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();
|
||||
int64_t res_count = 0;
|
||||
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_);
|
||||
uint32_t flags = 0;
|
||||
ObString text_utf16;
|
||||
bool is_null = true;
|
||||
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() &&
|
||||
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() ||
|
||||
(position != NULL && position->is_null())) {
|
||||
expr_datum.set_null();
|
||||
} 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) {
|
||||
if (OB_FAIL(ObExprUtil::convert_string_collation(text->get_string(), expr.args_[0]->datum_meta_.cs_type_, text_utf16,
|
||||
ObCharset::is_bin_sort(expr.args_[0]->datum_meta_.cs_type_) ? CS_TYPE_UTF16_BIN : CS_TYPE_UTF16_GENERAL_CI,
|
||||
tmp_alloc))) {
|
||||
LOG_WARN("convert charset failed", K(ret));
|
||||
}
|
||||
} else {
|
||||
text_utf16 = text->get_string();
|
||||
}
|
||||
}
|
||||
if (OB_FAIL(ret) || is_null) {
|
||||
} else if (OB_FAIL(regexp_ctx->count(tmp_alloc, text_utf16, pos - 1, res_count))) {
|
||||
LOG_WARN("failed to regexp count", K(ret));
|
||||
} else {
|
||||
number::ObNumber nmb;
|
||||
ObNumStackOnceAlloc nmb_alloc;
|
||||
if (OB_FAIL(nmb.from(res_count, nmb_alloc))) {
|
||||
LOG_WARN("set integer to number failed", K(ret));
|
||||
} else {
|
||||
expr_datum.set_number(nmb);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (OB_FAIL(ret)) {
|
||||
} else if (OB_FAIL(calc(cnt, text->get_string(), pattern->get_string(), pos,
|
||||
expr.args_[0]->datum_meta_.cs_type_,
|
||||
match_param, subexpr, null_result, reusable, regexp_ctx, alloc,
|
||||
ctx.exec_ctx_.get_allocator()))) {
|
||||
LOG_WARN("calc regex count failed", K(ret));
|
||||
} else if (null_result) {
|
||||
expr_datum.set_null();
|
||||
} else if (OB_FAIL(num.from(cnt, num_alloc))) {
|
||||
LOG_WARN("convert int to number failed", K(ret));
|
||||
} else {
|
||||
expr_datum.set_number(num);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
|
||||
Reference in New Issue
Block a user