提交 02e3302d 编写于 作者: S st0 提交者: wangzelin.wzl

support function crc32

上级 c19f7ea9
......@@ -397,6 +397,7 @@
#define N_INSERT "insert"
#define N_SUBSTRING_INDEX "substring_index"
#define N_MD5 "md5"
#define N_CRC32 "crc32"
#define N_HEX "hex"
#define N_UNHEX "unhex"
#define N_HEXTORAW "hextoraw"
......
......@@ -222,6 +222,7 @@ ob_set_subtarget(ob_sql engine
engine/expr/ob_expr_conv.cpp
engine/expr/ob_expr_convert.cpp
engine/expr/ob_expr_cosh.cpp
engine/expr/ob_expr_crc32.cpp
engine/expr/ob_expr_timezone.cpp
engine/expr/ob_expr_tz_offset.cpp
engine/expr/ob_expr_from_tz.cpp
......
/**
* 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 "lib/compress/zlib/zlib.h"
#include "lib/charset/ob_charset.h"
#include "lib/ob_name_def.h"
#include "lib/utility/ob_macro_utils.h"
#include "share/object/ob_obj_cast.h"
#include "sql/engine/expr/ob_expr_crc32.h"
#include "sql/engine/expr/ob_expr_util.h"
#include "sql/session/ob_sql_session_info.h"
namespace oceanbase {
using namespace common;
namespace sql {
ObExprCrc32::ObExprCrc32(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_CRC32, N_CRC32, 1, NOT_ROW_DIMENSION)
{}
int ObExprCrc32::calc_result_type1(ObExprResType& type, ObExprResType& type1, ObExprTypeCtx& type_ctx) const
{
int ret = OB_SUCCESS;
UNUSED(type_ctx);
type.set_precision(10);
type.set_uint32();
if (OB_LIKELY(type1.is_not_null())) {
type.set_result_flag(OB_MYSQL_NOT_NULL_FLAG);
}
if (ob_is_string_type(type1.get_type())) {
type1.set_calc_type(type1.get_type());
type1.set_calc_collation_type(type1.get_collation_type());
} else {
type1.set_calc_type(ObVarcharType);
type1.set_calc_collation_type(ObCharset::get_system_collation());
}
return ret;
}
int ObExprCrc32::calc_result1(common::ObObj& obj, const common::ObObj& obj1, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else if (obj1.is_null()) {
obj.set_null();
} else {
uint64_t val;
ObString str_val = obj1.get_string();
if (str_val.length() <= 0) {
val = 0ULL;
} else {
val = crc32(0, reinterpret_cast<unsigned char*>(str_val.ptr()), str_val.length());
}
obj.set_uint32(val);
}
return ret;
}
int ObExprCrc32::calc_crc32_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum)
{
int ret = OB_SUCCESS;
unsigned char* buf = NULL;
ObDatum* s_datum = NULL;
if (OB_FAIL(expr.args_[0]->eval(ctx, s_datum))) {
LOG_WARN("eval arg failed", K(ret));
} else if (s_datum->is_null()) {
res_datum.set_null();
} else {
const ObString& str_val = s_datum->get_string();
if (str_val.empty()) {
res_datum.set_uint(0ULL);
} else {
buf = reinterpret_cast<unsigned char*>(const_cast<char*>(str_val.ptr()));
res_datum.set_uint(crc32(0, buf, str_val.length()));
}
}
return ret;
}
int ObExprCrc32::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);
CK(1 == rt_expr.arg_cnt_);
CK(!OB_ISNULL(rt_expr.args_) && !OB_ISNULL(rt_expr.args_[0]));
CK(ob_is_string_type(rt_expr.args_[0]->datum_meta_.type_));
rt_expr.eval_func_ = calc_crc32_expr;
return ret;
}
} // namespace sql
} // namespace oceanbase
\ No newline at end of file
/**
* 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.
*/
#ifndef OCEANBASE_SQL_ENGINE_EXPR_OB_EXPR_CRC32_
#define OCEANBASE_SQL_ENGINE_EXPR_OB_EXPR_CRC32_
#include "sql/engine/expr/ob_expr_operator.h"
namespace oceanbase {
namespace sql {
class ObExprCrc32 : public ObFuncExprOperator {
public:
explicit ObExprCrc32(common::ObIAllocator& alloc);
virtual ~ObExprCrc32(){};
static int calc_crc32(common::ObObj& obj, const common::ObObj& obj1, common::ObExprCtx& expr_ctx);
virtual int calc_result_type1(ObExprResType& type, ObExprResType& type1, common::ObExprTypeCtx& type_ctx) const;
virtual int calc_result1(common::ObObj& obj, const common::ObObj& obj1, common::ObExprCtx& expr_ctx) const;
virtual int cg_expr(ObExprCGCtx& expr_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const;
static int calc_crc32_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum);
private:
DISALLOW_COPY_AND_ASSIGN(ObExprCrc32);
};
} // namespace sql
} // namespace oceanbase
#endif // OCEANBASE_SQL_ENGINE_EXPR_OB_EXPR_CRC32_
\ No newline at end of file
......@@ -50,6 +50,7 @@
#include "ob_expr_like.h"
#include "ob_expr_lower.h"
#include "ob_expr_md5.h"
#include "ob_expr_crc32.h"
#include "ob_expr_mid.h"
#include "ob_expr_minus.h"
#include "ob_expr_mod.h"
......@@ -715,6 +716,7 @@ static ObExpr::EvalFunc g_expr_eval_functions[] = {
ObExprInetAton::calc_inet_aton, /* 456 */
ObExprInet6Ntoa::calc_inet6_ntoa, /* 457 */
ObExprConvertTZ::eval_convert_tz, /* 458 */
ObExprCrc32::calc_crc32_expr, /* 459 */
};
REG_SER_FUNC_ARRAY(OB_SFA_SQL_EXPR_EVAL, g_expr_eval_functions, ARRAYSIZEOF(g_expr_eval_functions));
......
......@@ -170,6 +170,7 @@
#include "sql/engine/expr/ob_expr_reverse.h"
#include "sql/engine/expr/ob_expr_right.h"
#include "sql/engine/expr/ob_expr_md5.h"
#include "sql/engine/expr/ob_expr_crc32.h"
#include "sql/engine/expr/ob_expr_lrpad.h"
#include "sql/engine/expr/ob_expr_conv.h"
#include "sql/engine/expr/ob_expr_sign.h"
......@@ -629,6 +630,7 @@ void ObExprOperatorFactory::register_expr_operators()
REG_OP(ObExprIfNull);
REG_OP(ObExprConcatWs);
REG_OP(ObExprCmpMeta);
REG_OP(ObExprCrc32);
REG_OP(ObExprQuote);
REG_OP(ObExprPad);
REG_OP(ObExprHostIP);
......
......@@ -435,6 +435,7 @@ typedef enum ObItemType {
T_FUN_SYS_IS_IPV4_MAPPED = 719,
T_FUN_SYS_IS_IPV4_COMPAT = 720,
T_FUN_SYS_INETATON = 721,
T_FUN_SYS_CRC32 = 722,
///< @note add new mysql only function type before this line
T_MYSQL_ONLY_SYS_MAX_OP = 800,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册