support json type
This commit is contained in:
202
src/sql/engine/expr/ob_expr_json_member_of.cpp
Normal file
202
src/sql/engine/expr/ob_expr_json_member_of.cpp
Normal file
@ -0,0 +1,202 @@
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
// This file contains implementation for json_member_of.
|
||||
#define USING_LOG_PREFIX SQL_ENG
|
||||
#include "ob_expr_json_func_helper.h"
|
||||
#include "ob_expr_json_member_of.h"
|
||||
|
||||
using namespace oceanbase::common;
|
||||
using namespace oceanbase::sql;
|
||||
|
||||
namespace oceanbase
|
||||
{
|
||||
namespace sql
|
||||
{
|
||||
|
||||
ObExprJsonMemberOf::ObExprJsonMemberOf(ObIAllocator &alloc)
|
||||
: ObFuncExprOperator(alloc, T_FUN_SYS_JSON_MEMBER_OF, N_JSON_MEMBER_OF, 2, NOT_ROW_DIMENSION)
|
||||
{
|
||||
}
|
||||
|
||||
ObExprJsonMemberOf::~ObExprJsonMemberOf()
|
||||
{
|
||||
}
|
||||
|
||||
int ObExprJsonMemberOf::calc_result_type2(ObExprResType &type,
|
||||
ObExprResType &type1,
|
||||
ObExprResType &type2,
|
||||
ObExprTypeCtx &type_ctx) const
|
||||
{
|
||||
UNUSED(type_ctx);
|
||||
UNUSED(type2);
|
||||
int ret = OB_SUCCESS;
|
||||
|
||||
// set result to bool
|
||||
type.set_int32();
|
||||
type.set_precision(DEFAULT_PRECISION_FOR_BOOL);
|
||||
type.set_scale(ObAccuracy::DDL_DEFAULT_ACCURACY[ObIntType].scale_);
|
||||
|
||||
// set json_val
|
||||
if (type1.get_type() == ObNullType) {
|
||||
} else if(ob_is_string_type(type1.get_type())) {
|
||||
if (type1.get_charset_type() != CHARSET_UTF8MB4) {
|
||||
type1.set_calc_collation_type(CS_TYPE_UTF8MB4_BIN);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int json_member_of_array(const ObIJsonBase *json_a, const ObIJsonBase *json_b, bool *result)
|
||||
{
|
||||
int ret = OB_SUCCESS;
|
||||
int ret_tmp;
|
||||
|
||||
uint64_t b_len = json_b->element_count();
|
||||
for (uint64_t i = 0; i < b_len && OB_SUCC(ret); i++) {
|
||||
ObIJsonBase *tmp = NULL;
|
||||
if (OB_FAIL(json_b->get_array_element(i, tmp))) {
|
||||
break;
|
||||
} else {
|
||||
if (OB_SUCC(json_a->compare(*tmp, ret_tmp)) && ret_tmp == 0) {
|
||||
*result = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprJsonMemberOf::eval_json_member_of(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res)
|
||||
{
|
||||
INIT_SUCC(ret);
|
||||
ObIJsonBase *json_a = NULL;
|
||||
ObIJsonBase *json_b = NULL;
|
||||
common::ObArenaAllocator &temp_allocator = ctx.get_reset_tmp_alloc();
|
||||
bool is_null_result = (expr.args_[0]->datum_meta_.type_ == ObNullType);
|
||||
if (!is_null_result) {
|
||||
ObDatum *json_datum = NULL;
|
||||
ObExpr *json_arg = expr.args_[0];
|
||||
ObObjType type2 = expr.args_[1]->datum_meta_.type_;
|
||||
if (OB_FAIL(json_arg->eval(ctx, json_datum))) {
|
||||
LOG_WARN("eval json arg failed", K(ret));
|
||||
} else if (json_datum->is_null()) {
|
||||
is_null_result = true;
|
||||
} else if (OB_FAIL(ObJsonExprHelper::get_json_val(expr, ctx, &temp_allocator, 0, json_a))) {
|
||||
LOG_WARN("get_json_value failed", K(ret));
|
||||
} else if (!ObJsonExprHelper::is_convertible_to_json(type2)) {
|
||||
ret = OB_ERR_INVALID_TYPE_FOR_JSON;
|
||||
LOG_USER_ERROR(OB_ERR_INVALID_TYPE_FOR_JSON, 2, N_JSON_MEMBER_OF);
|
||||
} else if (OB_FAIL(ObJsonExprHelper::get_json_doc(expr, ctx,
|
||||
temp_allocator, 1,
|
||||
json_b, is_null_result))) {
|
||||
LOG_WARN("get_json_doc failed", K(ret));
|
||||
}
|
||||
}
|
||||
|
||||
bool is_member_of = false;
|
||||
if (!is_null_result && OB_SUCC(ret)) {
|
||||
// make sura w_b is J_ARRAY type
|
||||
if (json_b->json_type() != ObJsonNodeType::J_ARRAY) {
|
||||
int result = -1;
|
||||
if (OB_FAIL(json_b->compare(*json_a, result))) {
|
||||
LOG_WARN("json compare failed", K(ret));
|
||||
} else {
|
||||
is_member_of = (result == 0);
|
||||
}
|
||||
} else if (OB_FAIL(json_member_of_array(json_a, json_b, &is_member_of))) {
|
||||
LOG_WARN("json_member_of_array failed", K(ret));
|
||||
}
|
||||
}
|
||||
|
||||
// set result
|
||||
if (OB_FAIL(ret)) {
|
||||
LOG_WARN("json_member_of failed", K(ret));
|
||||
} else if (is_null_result) {
|
||||
res.set_null();
|
||||
} else {
|
||||
res.set_int(static_cast<int64_t>(is_member_of));
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprJsonMemberOf::calc_result2(common::ObObj &result,
|
||||
const common::ObObj &obj1,
|
||||
const common::ObObj &obj2,
|
||||
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 {
|
||||
ObIJsonBase *json_a = NULL;
|
||||
ObIJsonBase *json_b = NULL;
|
||||
bool is_null_result = (obj1.get_type() == ObNullType);
|
||||
ObObjType type2 = obj2.get_type();
|
||||
bool is_bool = false;
|
||||
if (OB_FAIL(get_param_is_boolean(expr_ctx, obj1, is_bool))) {
|
||||
LOG_WARN("get_param_is_boolean failed", K(ret));
|
||||
} else if (!is_null_result && OB_FAIL(ObJsonExprHelper::get_json_val(obj1, expr_ctx, is_bool,
|
||||
allocator, json_a))) {
|
||||
LOG_WARN("get_json_val failed", K(ret));
|
||||
} else if (!ObJsonExprHelper::is_convertible_to_json(type2)) {
|
||||
ret = OB_ERR_INVALID_TYPE_FOR_JSON;
|
||||
LOG_USER_ERROR(OB_ERR_INVALID_TYPE_FOR_JSON, 2, N_JSON_MEMBER_OF);
|
||||
} else if (!is_null_result && OB_FAIL(ObJsonExprHelper::get_json_doc(&obj2, allocator, 0,
|
||||
json_b, is_null_result))) {
|
||||
LOG_WARN("get_json_doc failed", K(ret));
|
||||
}
|
||||
|
||||
bool is_member_of = false;
|
||||
if (!is_null_result && OB_SUCC(ret)) {
|
||||
// make sura json_b is J_ARRAY type
|
||||
if (json_b->json_type() != ObJsonNodeType::J_ARRAY) {
|
||||
int result = -1;
|
||||
if (OB_FAIL(json_b->compare(*json_a, result))) {
|
||||
LOG_WARN("json compare failed", K(ret));
|
||||
} else {
|
||||
is_member_of = (result == 0);
|
||||
}
|
||||
} else if (OB_FAIL(json_member_of_array(json_a, json_b, &is_member_of))) {
|
||||
LOG_WARN("json_member_of_array failed", K(ret));
|
||||
}
|
||||
}
|
||||
|
||||
// set result
|
||||
if (OB_FAIL(ret)) {
|
||||
LOG_WARN("json_member_of failed", K(ret));
|
||||
} else if (is_null_result) {
|
||||
result.set_null();
|
||||
} else {
|
||||
result.set_int(static_cast<int64_t>(is_member_of));
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int ObExprJsonMemberOf::cg_expr(ObExprCGCtx &expr_cg_ctx,
|
||||
const ObRawExpr &raw_expr,
|
||||
ObExpr &rt_expr) const
|
||||
{
|
||||
UNUSED(expr_cg_ctx);
|
||||
UNUSED(raw_expr);
|
||||
rt_expr.eval_func_ = eval_json_member_of;
|
||||
return OB_SUCCESS;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user