patch 4.0
This commit is contained in:
@ -10,7 +10,6 @@
|
||||
* See the Mulan PubL v2 for more details.
|
||||
*/
|
||||
|
||||
// This file contains implementation for json_replace.
|
||||
#define USING_LOG_PREFIX SQL_ENG
|
||||
#include "ob_expr_json_replace.h"
|
||||
#include "sql/engine/expr/ob_expr_json_func_helper.h"
|
||||
@ -60,99 +59,13 @@ int ObExprJsonReplace::calc_result_typeN(ObExprResType& type,
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprJsonReplace::calc_resultN(common::ObObj &result,
|
||||
const common::ObObj *params,
|
||||
int64_t param_num,
|
||||
common::ObExprCtx &expr_ctx) const
|
||||
{
|
||||
INIT_SUCC(ret);
|
||||
ObIAllocator *allocator = expr_ctx.calc_buf_;
|
||||
|
||||
if (OB_ISNULL(allocator)) { // check allocator
|
||||
ret = OB_NOT_INIT;
|
||||
LOG_WARN("varchar buffer not init", K(ret));
|
||||
} else if (result_type_.get_collation_type() != CS_TYPE_UTF8MB4_BIN) {
|
||||
ret = OB_ERR_INVALID_JSON_CHARSET;
|
||||
LOG_WARN("invalid out put charset", K(ret), K(result_type_));
|
||||
} else {
|
||||
// get json doc
|
||||
ObIJsonBase *json_doc = NULL;
|
||||
bool is_null_result = false;
|
||||
if (OB_FAIL(ObJsonExprHelper::get_json_doc(params, allocator, 0,
|
||||
json_doc, is_null_result))) {
|
||||
LOG_WARN("get_json_doc failed", K(ret));
|
||||
}
|
||||
|
||||
ObJsonPathCache ctx_cache(allocator);
|
||||
ObJsonPathCache* path_cache = &ctx_cache;
|
||||
|
||||
for (int64_t i = 1; OB_SUCC(ret) && !is_null_result && i < param_num; i+=2) {
|
||||
ObJsonBaseVector hit;
|
||||
ObString path_val = params[i].get_string();
|
||||
ObObjType val_type = params[i].get_type();
|
||||
if (val_type == ObNullType || params[i].is_null()) {
|
||||
is_null_result = true;
|
||||
break;
|
||||
} else {
|
||||
ObJsonPath *json_path;
|
||||
if (OB_FAIL(ObJsonExprHelper::find_and_add_cache(path_cache, json_path, path_val, i, false))) {
|
||||
ret = OB_ERR_INVALID_JSON_PATH;
|
||||
LOG_USER_ERROR(OB_ERR_INVALID_JSON_PATH);
|
||||
} else if (OB_FAIL(json_doc->seek(*json_path, json_path->path_node_cnt(),
|
||||
true, false, hit))) {
|
||||
LOG_WARN("json seek failed", K(path_val), K(ret));
|
||||
} else {}
|
||||
}
|
||||
|
||||
if (OB_SUCC(ret) && !is_null_result) {
|
||||
ObIJsonBase *json_val = NULL;
|
||||
bool is_bool = false;
|
||||
if (OB_FAIL(get_param_is_boolean(expr_ctx, params[i+1], is_bool))) {
|
||||
LOG_WARN("get_param_is_boolean failed", K(ret));
|
||||
} else if (OB_FAIL(ObJsonExprHelper::get_json_val(params[i+1], expr_ctx,
|
||||
is_bool, allocator, json_val))) {
|
||||
LOG_WARN("get_json_val failed", K(ret));
|
||||
}
|
||||
|
||||
// replace
|
||||
int32_t hits = hit.size();
|
||||
if (hits == 0) {
|
||||
// do nothing
|
||||
} else if (hits != 1) {
|
||||
ret = OB_ERR_UNEXPECTED;
|
||||
LOG_WARN("Input path seek failed", K(ret));
|
||||
} else {
|
||||
if (OB_FAIL(ObJsonExprHelper::json_base_replace(hit[0], json_val, json_doc))) {
|
||||
LOG_WARN("json_base_replace failed", K(ret));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set result
|
||||
if (OB_UNLIKELY(OB_FAIL(ret))) {
|
||||
LOG_WARN("Json parse and seek failed", K(ret));
|
||||
} else if (is_null_result) {
|
||||
result.set_null();
|
||||
} else {
|
||||
ObString raw_bin;
|
||||
if (OB_FAIL(json_doc->get_raw_binary(raw_bin, allocator))) {
|
||||
LOG_WARN("json_replace result to binary failed", K(ret));
|
||||
} else {
|
||||
result.set_collation_type(CS_TYPE_UTF8MB4_BIN);
|
||||
result.set_string(ObJsonType, raw_bin.ptr(), raw_bin.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprJsonReplace::eval_json_replace(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
ObIJsonBase *json_doc = NULL;
|
||||
bool is_null_result = false;
|
||||
common::ObArenaAllocator &temp_allocator = ctx.get_reset_tmp_alloc();
|
||||
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
|
||||
common::ObArenaAllocator &temp_allocator = tmp_alloc_g.get_allocator();
|
||||
if (expr.datum_meta_.cs_type_ != CS_TYPE_UTF8MB4_BIN) {
|
||||
ret = OB_ERR_INVALID_JSON_CHARSET;
|
||||
LOG_WARN("invalid out put charset", K(ret), K(expr.datum_meta_.cs_type_));
|
||||
|
||||
Reference in New Issue
Block a user