/** * Copyright (c) 2021 OceanBase * OceanBase CE is licensed under Mulan PubL v2. * You can use this software according to the terms and conditions of the Mulan PubL v2. * You may obtain a copy of Mulan PubL v2 at: * http://license.coscl.org.cn/MulanPubL-2.0 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. * See the Mulan PubL v2 for more details. */ #define USING_LOG_PREFIX SQL_ENG #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 "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 { ObExprInstrb::ObExprInstrb(ObIAllocator &alloc) : ObLocationExprOperator(alloc, T_FUN_SYS_INSTRB, N_INSTRB, MORE_THAN_ONE, NOT_ROW_DIMENSION) {} ObExprInstrb::~ObExprInstrb() {} 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(); if (OB_ISNULL(session)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("session is NULL", K(ret)); } else { ObExprOperator::calc_result_flag2(type, type1, type2); type.set_number(); type.set_precision(PRECISION_UNKNOWN_YET); type.set_scale(NUMBER_SCALE_UNKNOWN_YET); ObSEArray params; OZ(params.push_back(&type1)); ObExprResType tmp_type; OZ(aggregate_string_type_and_charset_oracle(*session, params, tmp_type)); OZ(params.push_back(&type2)); OZ(deduce_string_param_calc_type_and_charset(*session, tmp_type, params)); } return ret; } 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)) { ret = OB_ERR_PARAM_SIZE; LOG_WARN("param num is invalid", K(ret), K(param_num)); } else if (OB_ISNULL(type_array)) { ret = OB_ERR_UNEXPECTED; LOG_WARN("type array is null", K(ret)); } else if (OB_FAIL(calc_result_type2(type, type_array[0], type_array[1], type_ctx))) { LOG_WARN("fail calc result type", K(param_num), K(ret)); } else { if (3 == param_num) { 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 } } return ret; } 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; 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))) { LOG_WARN("calc_instrb_arg failed", K(ret)); } else if (is_null) { res_datum.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.set_number(res_nmb); } } 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 (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.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; ObExprKMPSearchCtx *kmp_ctx = NULL; const uint64_t op_id = static_cast(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::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(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