patch 4.0
This commit is contained in:
@ -14,28 +14,32 @@
|
||||
#include "sql/engine/expr/ob_expr_instrb.h"
|
||||
#include "sql/engine/expr/ob_expr_instr.h"
|
||||
#include "sql/engine/expr/ob_expr_util.h"
|
||||
#include "sql/engine/ob_exec_context.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/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
|
||||
{
|
||||
|
||||
ObExprInstrb::ObExprInstrb(ObIAllocator& alloc)
|
||||
: ObLocationExprOperator(alloc, T_FUN_SYS_INSTRB, N_INSTRB, MORE_THAN_ONE, NOT_ROW_DIMENSION)
|
||||
{}
|
||||
ObExprInstrb::ObExprInstrb(ObIAllocator &alloc)
|
||||
: ObLocationExprOperator(alloc, T_FUN_SYS_INSTRB, N_INSTRB, MORE_THAN_ONE, NOT_ROW_DIMENSION) {}
|
||||
|
||||
ObExprInstrb::~ObExprInstrb()
|
||||
{}
|
||||
ObExprInstrb::~ObExprInstrb() {}
|
||||
|
||||
int ObExprInstrb::calc_result_type2(
|
||||
ObExprResType& type, ObExprResType& type1, ObExprResType& type2, common::ObExprTypeCtx& type_ctx) const
|
||||
int ObExprInstrb::calc_result_type2(ObExprResType &type,
|
||||
ObExprResType &type1,
|
||||
ObExprResType &type2,
|
||||
common::ObExprTypeCtx &type_ctx) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
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));
|
||||
@ -55,8 +59,10 @@ int ObExprInstrb::calc_result_type2(
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprInstrb::calc_result_typeN(
|
||||
ObExprResType& type, ObExprResType* type_array, int64_t param_num, ObExprTypeCtx& type_ctx) const
|
||||
int ObExprInstrb::calc_result_typeN(ObExprResType &type,
|
||||
ObExprResType *type_array,
|
||||
int64_t param_num,
|
||||
ObExprTypeCtx &type_ctx) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
if (OB_UNLIKELY(param_num < 2 || param_num > 4)) {
|
||||
@ -69,150 +75,32 @@ int ObExprInstrb::calc_result_typeN(
|
||||
LOG_WARN("fail calc result type", K(param_num), K(ret));
|
||||
} else {
|
||||
if (3 == param_num) {
|
||||
type_array[2].set_calc_type(ObNumberType); // position
|
||||
type_array[2].set_calc_type(ObNumberType); // position
|
||||
} else if (4 == param_num) {
|
||||
type_array[2].set_calc_type(ObNumberType); // position
|
||||
type_array[3].set_calc_type(ObNumberType); // occurrence
|
||||
type_array[2].set_calc_type(ObNumberType); // position
|
||||
type_array[3].set_calc_type(ObNumberType); // occurrence
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprInstrb::calc_resultN(
|
||||
ObObj& result, const common::ObObj* param_array, int64_t param_num, common::ObExprCtx& expr_ctx) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
if (OB_ISNULL(param_array)) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("param_array is null", K(ret));
|
||||
} else if (OB_UNLIKELY(param_num < 2 || param_num > 4)) {
|
||||
ret = OB_ERR_PARAM_SIZE;
|
||||
LOG_WARN("invalid param count", K(param_num), K(ret));
|
||||
} else if (param_array[0].is_null() || param_array[1].is_null()) {
|
||||
result.set_null();
|
||||
} else if (param_array[0].is_clob() && (0 == param_array[0].get_string().length())) {
|
||||
result.set_lob_value(ObLongTextType, param_array[0].get_string().ptr(), param_array[0].get_string().length());
|
||||
result.set_meta_type(result_type_);
|
||||
} else if (2 == param_num) {
|
||||
ObObj position(static_cast<int64_t>(1));
|
||||
ObObj occurrence(static_cast<int64_t>(1));
|
||||
ret = calc(result, param_array[0], param_array[1], position, occurrence, expr_ctx);
|
||||
} else if (3 == param_num) {
|
||||
ObObj occurrence(static_cast<int64_t>(1));
|
||||
ret = calc(result, param_array[0], param_array[1], param_array[2], occurrence, expr_ctx);
|
||||
} else {
|
||||
ret = calc(result, param_array[0], param_array[1], param_array[2], param_array[3], expr_ctx);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprInstrb::calc(ObObj& result, const ObObj& haystack, const ObObj& needle, const ObObj& position,
|
||||
const ObObj& occurrence, ObExprCtx& expr_ctx) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
int64_t pos_val = -1;
|
||||
int64_t occ_val = -1;
|
||||
int64_t ret_idx = -1;
|
||||
number::ObNumber ret_num;
|
||||
|
||||
if (OB_ISNULL(expr_ctx.calc_buf_)) {
|
||||
ret = OB_NOT_INIT;
|
||||
LOG_WARN("expr_ctx.calc_buf_ is null", K(ret));
|
||||
} else if (OB_UNLIKELY(haystack.is_null_oracle() || needle.is_null_oracle() || position.is_null_oracle() ||
|
||||
occurrence.is_null_oracle())) {
|
||||
result.set_null();
|
||||
} else if (OB_UNLIKELY(!is_type_valid(haystack.get_type()) || !is_type_valid(needle.get_type()) ||
|
||||
!is_type_valid(position.get_type()) || !is_type_valid(occurrence.get_type()))) {
|
||||
ret = OB_INVALID_ARGUMENT;
|
||||
LOG_WARN("param is not castable", K(haystack), K(needle), K(position), K(occurrence), K(ret));
|
||||
} else if (OB_FAIL(ObExprUtil::get_trunc_int64(position, expr_ctx, pos_val))) {
|
||||
LOG_WARN("get position failed", K(position), K(ret));
|
||||
} else if (OB_FAIL(ObExprUtil::get_trunc_int64(occurrence, expr_ctx, occ_val))) {
|
||||
LOG_WARN("get occurrence failed", K(occurrence), K(ret));
|
||||
} else if (OB_UNLIKELY(0 >= occ_val)) {
|
||||
ret = OB_ERR_ARGUMENT_OUT_OF_RANGE;
|
||||
LOG_USER_ERROR(OB_ERR_ARGUMENT_OUT_OF_RANGE, occ_val);
|
||||
} else if (OB_UNLIKELY(0 == pos_val)) {
|
||||
ret_num.set_zero();
|
||||
result.set_number(ret_num);
|
||||
} else {
|
||||
const ObString& haystack_str = haystack.get_string();
|
||||
const ObString& needle_str = needle.get_string();
|
||||
|
||||
if (OB_SUCC(ret)) {
|
||||
if (OB_FAIL(search(haystack_str, needle_str, pos_val, occ_val, ret_idx))) {
|
||||
LOG_WARN("search needle in haystack failed", K(ret), K(haystack_str), K(needle_str));
|
||||
} else if (OB_FAIL(ret_num.from(ret_idx, *(expr_ctx.calc_buf_)))) {
|
||||
LOG_WARN("int64_t to ObNumber failed", K(ret), K(ret_idx));
|
||||
} else {
|
||||
result.set_number(ret_num);
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprInstrb::search(const ObString& haystack, const ObString& needle, int64_t start, int64_t occ, int64_t& ret_idx)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
if (OB_ISNULL(haystack.ptr())) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("ptr is null", K(haystack));
|
||||
} else if (OB_ISNULL(needle.ptr())) {
|
||||
ret = OB_CLOB_ONLY_SUPPORT_WITH_MULTIBYTE_FUN;
|
||||
LOG_WARN("ptr is null", K(needle));
|
||||
} else if (haystack.length() < needle.length()) {
|
||||
ret_idx = 0;
|
||||
} else {
|
||||
if (0 < start) {
|
||||
start = start - 1;
|
||||
if (start > haystack.length() || start + needle.length() > haystack.length()) {
|
||||
ret_idx = 0;
|
||||
} else if (OB_FAIL(ObExprUtil::kmp(
|
||||
needle.ptr(), needle.length(), haystack.ptr() + start, haystack.length() - start, occ, ret_idx))) {
|
||||
LOG_WARN("ObExprInstrb kmp failed", K(occ), K(start), K(needle), K(haystack));
|
||||
} else if (-1 < ret_idx) {
|
||||
ret_idx = ret_idx + 1 + start;
|
||||
} else {
|
||||
ret_idx = 0;
|
||||
}
|
||||
} else { // start < 0
|
||||
occ = -occ;
|
||||
int64_t compare_len = min(haystack.length(), haystack.length() + start + needle.length());
|
||||
if ((-start) > haystack.length()) {
|
||||
ret_idx = 0;
|
||||
} else {
|
||||
if (OB_FAIL(
|
||||
ObExprUtil::kmp_reverse(needle.ptr(), needle.length(), haystack.ptr(), compare_len, occ, ret_idx))) {
|
||||
LOG_WARN("ObExprInstrb kmp_reverse failed", K(occ), K(compare_len), K(needle), K(haystack));
|
||||
} else if (-1 < ret_idx) {
|
||||
ret_idx = ret_idx + 1;
|
||||
} else {
|
||||
ret_idx = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int calc_instrb_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
|
||||
int calc_instrb_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
bool is_null = false;
|
||||
ObDatum* haystack = NULL;
|
||||
ObDatum* needle = NULL;
|
||||
ObDatum *haystack = NULL;
|
||||
ObDatum *needle = NULL;
|
||||
int64_t pos_int = 0;
|
||||
int64_t occ_int = 0;
|
||||
number::ObNumber res_nmb;
|
||||
ObCollationType calc_cs_type = CS_TYPE_INVALID;
|
||||
if (OB_FAIL(ObExprOracleInstr::calc_oracle_instr_arg(
|
||||
expr, ctx, is_null, haystack, needle, pos_int, occ_int, calc_cs_type))) {
|
||||
if (OB_FAIL(ObExprOracleInstr::calc_oracle_instr_arg(expr, ctx, is_null,
|
||||
haystack, needle,
|
||||
pos_int, occ_int, calc_cs_type))) {
|
||||
LOG_WARN("calc_instrb_arg failed", K(ret));
|
||||
} else if (is_null) {
|
||||
res_datum.set_null();
|
||||
} else if (0 == haystack->get_string().length()) {
|
||||
number::ObNumber res_nmb;
|
||||
} else if (OB_UNLIKELY(0 == haystack->get_string().length())) {
|
||||
ObNumStackOnceAlloc tmp_alloc;
|
||||
if (OB_FAIL(res_nmb.from((uint64_t)(0), tmp_alloc))) {
|
||||
LOG_WARN("get number from int failed", K(ret));
|
||||
@ -222,7 +110,7 @@ int calc_instrb_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
|
||||
} else if (OB_ISNULL(haystack) || OB_ISNULL(needle)) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("haystack or needle is NULL", K(ret), KP(haystack), KP(needle));
|
||||
} else if (0 >= occ_int) {
|
||||
} else if (OB_UNLIKELY(0 >= occ_int)) {
|
||||
ret = OB_ERR_ARGUMENT_OUT_OF_RANGE;
|
||||
LOG_USER_ERROR(OB_ERR_ARGUMENT_OUT_OF_RANGE, occ_int);
|
||||
} else if (OB_UNLIKELY(0 == pos_int)) {
|
||||
@ -232,28 +120,155 @@ int calc_instrb_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
|
||||
const ObString& haystack_str = haystack->get_string();
|
||||
const ObString& needle_str = needle->get_string();
|
||||
int64_t ret_idx = -1;
|
||||
if (OB_SUCC(ret)) {
|
||||
ObNumStackOnceAlloc tmp_alloc;
|
||||
if (OB_FAIL(ObExprInstrb::search(haystack_str, needle_str, pos_int, occ_int, ret_idx))) {
|
||||
LOG_WARN("search needle in haystack failed", K(ret), K(haystack_str), K(needle_str));
|
||||
} else if (OB_FAIL(res_nmb.from(ret_idx, tmp_alloc))) {
|
||||
LOG_WARN("int64_t to ObNumber failed", K(ret), K(ret_idx));
|
||||
ObNumStackOnceAlloc tmp_alloc;
|
||||
ObExprKMPSearchCtx *kmp_ctx = NULL;
|
||||
const uint64_t op_id = static_cast<uint64_t>(expr.expr_ctx_id_);
|
||||
if (OB_ISNULL(needle_str.ptr())) {
|
||||
ret = OB_CLOB_ONLY_SUPPORT_WITH_MULTIBYTE_FUN;
|
||||
LOG_WARN("ptr is null", K(needle));
|
||||
} else if (OB_UNLIKELY(0 == needle_str.length())) {
|
||||
if (OB_FAIL(res_nmb.from((uint64_t)(0), tmp_alloc))) {
|
||||
LOG_WARN("get number from int failed", K(ret));
|
||||
} else {
|
||||
res_datum.set_number(res_nmb);
|
||||
}
|
||||
} else if (OB_FAIL(ObExprKMPSearchCtx::get_kmp_ctx_from_exec_ctx(ctx.exec_ctx_, op_id, kmp_ctx))) {
|
||||
LOG_WARN("get kmp ctx failed", K(ret));
|
||||
} else if (OB_FAIL(kmp_ctx->init(needle_str, pos_int < 0, ctx.exec_ctx_.get_allocator()))) {
|
||||
LOG_WARN("init kmp ctx failed", K(ret), K(needle_str));
|
||||
} else if (OB_FAIL(kmp_ctx->instrb_search(haystack_str, pos_int, occ_int, ret_idx))) {
|
||||
LOG_WARN("search needle in haystack failed", K(ret), K(haystack_str), K(needle_str));
|
||||
} else if (OB_FAIL(res_nmb.from(ret_idx, tmp_alloc))) {
|
||||
LOG_WARN("int64_t to ObNumber failed", K(ret), K(ret_idx));
|
||||
} else {
|
||||
res_datum.set_number(res_nmb);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprInstrb::cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
|
||||
int ObExprInstrb::calc_instrb_expr_batch(const ObExpr &expr,
|
||||
ObEvalCtx &ctx,
|
||||
const ObBitVector &skip,
|
||||
const int64_t batch_size)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
ObExprKMPSearchCtx *kmp_ctx = NULL;
|
||||
const uint64_t op_id = static_cast<uint64_t>(expr.expr_ctx_id_);
|
||||
ObCollationType calc_cs_type = CS_TYPE_INVALID;
|
||||
if (OB_FAIL(expr.args_[0]->eval_batch(ctx, skip, batch_size))) {
|
||||
LOG_WARN("eval args_0 failed", K(ret));
|
||||
} else if (OB_FAIL(expr.args_[1]->eval_batch(ctx, skip, batch_size))) {
|
||||
LOG_WARN("eval args_1 failed", K(ret));
|
||||
} else if (expr.arg_cnt_ > 2 && OB_FAIL(expr.args_[2]->eval_batch(ctx, skip, batch_size))) {
|
||||
LOG_WARN("eval args_2 failed", K(ret));
|
||||
} else if (expr.arg_cnt_ == 4 && OB_FAIL(expr.args_[3]->eval_batch(ctx, skip, batch_size))) {
|
||||
LOG_WARN("eval args_3 failed", K(ret));
|
||||
} else if (OB_FAIL(ObLocationExprOperator::get_calc_cs_type(expr, calc_cs_type))) {
|
||||
LOG_WARN("get_calc_cs_type failed", K(ret));
|
||||
} else if (OB_FAIL(ObExprKMPSearchCtx::get_kmp_ctx_from_exec_ctx(ctx.exec_ctx_,
|
||||
op_id, kmp_ctx))) {
|
||||
LOG_WARN("get kmp ctx failed", K(ret));
|
||||
} else {
|
||||
ObDatum *res_datum = expr.locate_batch_datums(ctx);
|
||||
ObBitVector &eval_flags = expr.get_evaluated_flags(ctx);
|
||||
for (int64_t i = 0; OB_SUCC(ret) && i < batch_size; ++i) {
|
||||
if (skip.at(i) || eval_flags.at(i)) {
|
||||
continue;;
|
||||
}
|
||||
ObDatum &haystack = expr.args_[0]->locate_expr_datum(ctx, i);
|
||||
ObDatum &needle = expr.args_[1]->locate_expr_datum(ctx, i);
|
||||
bool is_null = false;
|
||||
int64_t pos_int = 1;
|
||||
int64_t occ_int = 1;
|
||||
number::ObNumber res_nmb;
|
||||
if (haystack.is_null() || needle.is_null()) {
|
||||
is_null = true;
|
||||
}
|
||||
if (!is_null && (3 == expr.arg_cnt_ || 4 == expr.arg_cnt_)) {
|
||||
ObDatum &pos = expr.args_[2]->locate_expr_datum(ctx, i);
|
||||
if (pos.is_null()) {
|
||||
is_null = true;
|
||||
} else {
|
||||
number::ObNumber pos_nmb(pos.get_number());
|
||||
if (OB_FAIL(ObExprUtil::trunc_num2int64(pos_nmb, pos_int))) {
|
||||
LOG_WARN("trunc_num2int64 failed", K(ret));
|
||||
} else if (INT64_MIN == pos_int) {
|
||||
pos_int = INT64_MAX;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (OB_SUCC(ret) && !is_null && 4 == expr.arg_cnt_) {
|
||||
ObDatum &occ = expr.args_[3]->locate_expr_datum(ctx, i);
|
||||
if (occ.is_null()) {
|
||||
is_null = true;
|
||||
} else {
|
||||
number::ObNumber occ_nmb(occ.get_number());
|
||||
if (OB_FAIL(ObExprUtil::trunc_num2int64(occ_nmb, occ_int))) {
|
||||
LOG_WARN("trunc_num2int64 failed", K(ret));
|
||||
}
|
||||
}
|
||||
}
|
||||
if (OB_FAIL(ret)) {
|
||||
} else if (OB_UNLIKELY(is_null)) {
|
||||
res_datum[i].set_null();
|
||||
} else if (OB_UNLIKELY(0 == haystack.get_string().length())) {
|
||||
ObNumStackOnceAlloc tmp_alloc;
|
||||
if (OB_FAIL(res_nmb.from((uint64_t)(0), tmp_alloc))) {
|
||||
LOG_WARN("get number from int failed", K(ret));
|
||||
} else {
|
||||
res_datum[i].set_number(res_nmb);
|
||||
}
|
||||
} else if (OB_UNLIKELY(0 >= occ_int)) {
|
||||
ret = OB_ERR_ARGUMENT_OUT_OF_RANGE;
|
||||
LOG_USER_ERROR(OB_ERR_ARGUMENT_OUT_OF_RANGE, occ_int);
|
||||
} else if (OB_UNLIKELY(0 == pos_int)) {
|
||||
res_nmb.set_zero();
|
||||
res_datum[i].set_number(res_nmb);
|
||||
} else {
|
||||
const ObString& haystack_str = haystack.get_string();
|
||||
const ObString& needle_str = needle.get_string();
|
||||
int64_t ret_idx = -1;
|
||||
ObNumStackOnceAlloc tmp_alloc;
|
||||
if (OB_ISNULL(needle_str.ptr())) {
|
||||
ret = OB_CLOB_ONLY_SUPPORT_WITH_MULTIBYTE_FUN;
|
||||
LOG_WARN("ptr is null", K(needle_str));
|
||||
} else if (OB_UNLIKELY(0 == needle_str.length())) {
|
||||
if (OB_FAIL(res_nmb.from((uint64_t)(0), tmp_alloc))) {
|
||||
LOG_WARN("get number from int failed", K(ret));
|
||||
} else {
|
||||
res_datum[i].set_number(res_nmb);
|
||||
}
|
||||
} else if (OB_FAIL(kmp_ctx->init(needle_str, pos_int < 0, ctx.exec_ctx_.get_allocator()))) {
|
||||
LOG_WARN("init kmp ctx failed", K(ret), K(needle_str));
|
||||
} else if (OB_FAIL(kmp_ctx->instrb_search(haystack_str, pos_int, occ_int, ret_idx))) {
|
||||
LOG_WARN("search needle in haystack failed", K(ret), K(haystack_str), K(needle_str));
|
||||
} else if (OB_FAIL(res_nmb.from(ret_idx, tmp_alloc))) {
|
||||
LOG_WARN("int64_t to ObNumber failed", K(ret), K(ret_idx));
|
||||
} else {
|
||||
res_datum[i].set_number(res_nmb);
|
||||
}
|
||||
}
|
||||
eval_flags.set(i);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprInstrb::cg_expr(ObExprCGCtx &expr_cg_ctx, const ObRawExpr &raw_expr,
|
||||
ObExpr &rt_expr) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
UNUSED(expr_cg_ctx);
|
||||
UNUSED(raw_expr);
|
||||
if (OB_UNLIKELY(rt_expr.arg_cnt_ < 2 || rt_expr.arg_cnt_ > 4)) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("unexpected arg cnt", K(ret), K(rt_expr.arg_cnt_));
|
||||
}
|
||||
rt_expr.eval_func_ = calc_instrb_expr;
|
||||
rt_expr.eval_batch_func_ = ObExprInstrb::calc_instrb_expr_batch;
|
||||
return ret;
|
||||
}
|
||||
|
||||
} // namespace sql
|
||||
} // namespace oceanbase
|
||||
} //namespace sql
|
||||
} //namespace oceanbase
|
||||
|
||||
Reference in New Issue
Block a user