From 02e3302d0829f197105873a2b30e97576e0414d4 Mon Sep 17 00:00:00 2001 From: st0 Date: Mon, 13 Sep 2021 18:01:41 +0800 Subject: [PATCH] support function crc32 --- deps/oblib/src/lib/ob_name_def.h | 1 + src/sql/CMakeLists.txt | 1 + src/sql/engine/expr/ob_expr_crc32.cpp | 108 ++++++++++++++++++ src/sql/engine/expr/ob_expr_crc32.h | 39 +++++++ .../engine/expr/ob_expr_eval_functions.cpp | 2 + .../engine/expr/ob_expr_operator_factory.cpp | 2 + src/sql/parser/ob_item_type.h | 1 + 7 files changed, 154 insertions(+) create mode 100644 src/sql/engine/expr/ob_expr_crc32.cpp create mode 100644 src/sql/engine/expr/ob_expr_crc32.h diff --git a/deps/oblib/src/lib/ob_name_def.h b/deps/oblib/src/lib/ob_name_def.h index 99e44effb2..1b99704436 100644 --- a/deps/oblib/src/lib/ob_name_def.h +++ b/deps/oblib/src/lib/ob_name_def.h @@ -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" diff --git a/src/sql/CMakeLists.txt b/src/sql/CMakeLists.txt index 8558b12af9..0749eee120 100644 --- a/src/sql/CMakeLists.txt +++ b/src/sql/CMakeLists.txt @@ -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 diff --git a/src/sql/engine/expr/ob_expr_crc32.cpp b/src/sql/engine/expr/ob_expr_crc32.cpp new file mode 100644 index 0000000000..4f47caf2f5 --- /dev/null +++ b/src/sql/engine/expr/ob_expr_crc32.cpp @@ -0,0 +1,108 @@ +/** + * 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(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(const_cast(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 diff --git a/src/sql/engine/expr/ob_expr_crc32.h b/src/sql/engine/expr/ob_expr_crc32.h new file mode 100644 index 0000000000..54d41e47a0 --- /dev/null +++ b/src/sql/engine/expr/ob_expr_crc32.h @@ -0,0 +1,39 @@ +/** + * 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 diff --git a/src/sql/engine/expr/ob_expr_eval_functions.cpp b/src/sql/engine/expr/ob_expr_eval_functions.cpp index 9a401cc8ba..a88f9b2c0e 100644 --- a/src/sql/engine/expr/ob_expr_eval_functions.cpp +++ b/src/sql/engine/expr/ob_expr_eval_functions.cpp @@ -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)); diff --git a/src/sql/engine/expr/ob_expr_operator_factory.cpp b/src/sql/engine/expr/ob_expr_operator_factory.cpp index 1473d62b6a..0835ea6315 100644 --- a/src/sql/engine/expr/ob_expr_operator_factory.cpp +++ b/src/sql/engine/expr/ob_expr_operator_factory.cpp @@ -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); diff --git a/src/sql/parser/ob_item_type.h b/src/sql/parser/ob_item_type.h index ff2bb241b1..19ae6c817f 100644 --- a/src/sql/parser/ob_item_type.h +++ b/src/sql/parser/ob_item_type.h @@ -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, -- GitLab