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_extract.
|
||||
#define USING_LOG_PREFIX SQL_ENG
|
||||
#include "ob_expr_json_extract.h"
|
||||
#include "ob_expr_json_func_helper.h"
|
||||
@ -81,111 +80,6 @@ int ObExprJsonExtract::calc_result_typeN(ObExprResType& type,
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprJsonExtract::calc_resultN(ObObj &result, const ObObj *objs,
|
||||
int64_t param_num, ObExprCtx &expr_ctx) const
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
ObIAllocator *allocator = expr_ctx.calc_buf_;
|
||||
ObIJsonBase *j_base = NULL;
|
||||
bool is_null_result = (get_result_type().get_type() == ObNullType);
|
||||
bool may_match_many = (param_num> 2);
|
||||
|
||||
if (is_null_result) {
|
||||
// do nothing;
|
||||
} 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 if (objs[0].is_null()) {
|
||||
is_null_result = true; // mysql return NULL result
|
||||
} else if (objs[0].get_type() != ObJsonType && ob_is_string_type(objs[0].get_type()) == false) {
|
||||
ret = OB_ERR_INVALID_TYPE_FOR_JSON;
|
||||
LOG_WARN("input type error", K(objs[0].get_type()));
|
||||
} else if (OB_FAIL(ObJsonExprHelper::ensure_collation(objs[0].get_type(),
|
||||
objs[0].get_collation_type()))) {
|
||||
LOG_WARN("fail to ensure collation", K(ret), K(objs[0].get_type()), K(objs[0].get_collation_type()));
|
||||
} else {
|
||||
ObString j_str = objs[0].get_string();
|
||||
ObJsonInType j_in_type = ObJsonExprHelper::get_json_internal_type(objs[0].get_type());
|
||||
if (OB_FAIL(ObJsonBaseFactory::get_json_base(allocator, j_str, j_in_type, j_in_type, j_base))) {
|
||||
LOG_WARN("fail to get json base", K(ret), K(j_in_type));
|
||||
ret = OB_ERR_INVALID_JSON_TEXT;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_null_result) {
|
||||
// do nothing
|
||||
} else if (OB_UNLIKELY(OB_FAIL(ret))) {
|
||||
if (ret == OB_ERR_INVALID_TYPE_FOR_JSON) {
|
||||
LOG_USER_ERROR(OB_ERR_INVALID_TYPE_FOR_JSON, 1, "json_extract");
|
||||
} else if (ret == OB_ERR_INVALID_JSON_CHARSET) {
|
||||
} else {
|
||||
ret = OB_ERR_INVALID_JSON_TEXT_IN_PARAM;
|
||||
LOG_USER_ERROR(OB_ERR_INVALID_JSON_TEXT_IN_PARAM);
|
||||
}
|
||||
LOG_WARN("fail to handle json param 0 in json extract in old sql engine", K(ret));
|
||||
} else {
|
||||
ObJsonBaseVector hit;
|
||||
ObJsonPathCache ctx_cache(allocator);
|
||||
ObJsonPathCache *path_cache = &ctx_cache;
|
||||
for (int64_t i = 1; OB_SUCC(ret) && (!is_null_result) && i < param_num; i++) {
|
||||
if (objs[i].get_type() == ObNullType) {
|
||||
is_null_result = true;
|
||||
} else {
|
||||
ObString path_text = objs[i].get_string();
|
||||
ObJsonPath *j_path = NULL;
|
||||
if (OB_FAIL(ObJsonExprHelper::find_and_add_cache(path_cache, j_path, path_text, i, true))) {
|
||||
LOG_WARN("parse text to path failed", K(path_text), K(ret));
|
||||
} else if (OB_FAIL(j_base->seek(*j_path, j_path->path_node_cnt(), true, false, hit))) {
|
||||
LOG_WARN("json seek failed", K(path_text), K(ret));
|
||||
} else {
|
||||
if (j_path->can_match_many()) {
|
||||
may_match_many = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int32_t hit_size = hit.size();
|
||||
ObJsonArray j_arr_res(allocator);
|
||||
ObIJsonBase *jb_res = NULL;
|
||||
if (OB_UNLIKELY(OB_FAIL(ret))) {
|
||||
LOG_WARN("json seek failed", K(ret));
|
||||
} else if (hit_size == 0 || is_null_result) {
|
||||
result.set_null();
|
||||
} else {
|
||||
if (hit_size == 1 && (may_match_many == false)) {
|
||||
jb_res = hit[0];
|
||||
} else {
|
||||
jb_res = &j_arr_res;
|
||||
ObJsonNode *j_node = NULL;
|
||||
ObIJsonBase *jb_node = NULL;
|
||||
for (int32_t i = 0; OB_SUCC(ret) && i < hit_size; i++) {
|
||||
if (OB_FAIL(ObJsonBaseFactory::transform(allocator, hit[i],
|
||||
ObJsonInType::JSON_TREE, jb_node))) { // to tree
|
||||
LOG_WARN("fail to transform to tree", K(ret), K(i), K(*(hit[i])));
|
||||
} else { // is_tree, need deep copy, cause array append will change parent of value.
|
||||
j_node = static_cast<ObJsonNode *>(jb_node);
|
||||
if (OB_FAIL(jb_res->array_append(j_node->clone(allocator)))) {
|
||||
LOG_WARN("result array append failed", K(ret), K(i), K(*j_node));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ObString raw_bin;
|
||||
if (OB_FAIL(ret)) {
|
||||
LOG_WARN("json extarct get results failed", K(ret));
|
||||
} else if (OB_FAIL(jb_res->get_raw_binary(raw_bin, allocator))) {
|
||||
LOG_WARN("json extarct get result 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 ObExprJsonExtract::eval_json_extract_null(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res)
|
||||
{
|
||||
UNUSED(expr);
|
||||
@ -204,7 +98,8 @@ int ObExprJsonExtract::eval_json_extract(const ObExpr &expr, ObEvalCtx &ctx, ObD
|
||||
ObIJsonBase *j_base = NULL;
|
||||
bool is_null_result = false;
|
||||
bool may_match_many = (expr.arg_cnt_ > 2);
|
||||
common::ObArenaAllocator &allocator = ctx.get_reset_tmp_alloc();
|
||||
ObEvalCtx::TempAllocGuard tmp_alloc_g(ctx);
|
||||
common::ObArenaAllocator &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_));
|
||||
@ -276,10 +171,9 @@ int ObExprJsonExtract::eval_json_extract(const ObExpr &expr, ObEvalCtx &ctx, ObD
|
||||
ObJsonNode *j_node = NULL;
|
||||
ObIJsonBase *jb_node = NULL;
|
||||
for (int32_t i = 0; OB_SUCC(ret) && i < hit_size; i++) {
|
||||
if (OB_FAIL(ObJsonBaseFactory::transform(&allocator, hit[i],
|
||||
ObJsonInType::JSON_TREE, jb_node))) { // to tree
|
||||
if (ObJsonBaseFactory::transform(&allocator, hit[i], ObJsonInType::JSON_TREE, jb_node)) { // to tree
|
||||
LOG_WARN("fail to transform to tree", K(ret), K(i), K(*(hit[i])));
|
||||
} else { // is_tree, need deep copy, cause array append will change parent of value.
|
||||
} else {
|
||||
j_node = static_cast<ObJsonNode *>(jb_node);
|
||||
if (OB_FAIL(jb_res->array_append(j_node->clone(&allocator)))) {
|
||||
LOG_WARN("result array append failed", K(ret), K(i), K(*j_node));
|
||||
|
||||
Reference in New Issue
Block a user