From 589b565d91cdf0b07f4dd211040de99e5bd05c58 Mon Sep 17 00:00:00 2001 From: yy0 Date: Tue, 22 Feb 2022 16:04:51 +0800 Subject: [PATCH] fix: old engine distinct boolean type --- deps/oblib/src/rpc/obmysql/ob_mysql_global.h | 1 + src/share/object/ob_obj_cast.cpp | 5 +++++ src/share/object/ob_obj_cast.h | 2 ++ src/sql/engine/expr/ob_expr_to_type.cpp | 9 +++++++++ src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp | 6 +++++- 5 files changed, 22 insertions(+), 1 deletion(-) diff --git a/deps/oblib/src/rpc/obmysql/ob_mysql_global.h b/deps/oblib/src/rpc/obmysql/ob_mysql_global.h index 94bd1bf8a7..02470c131c 100644 --- a/deps/oblib/src/rpc/obmysql/ob_mysql_global.h +++ b/deps/oblib/src/rpc/obmysql/ob_mysql_global.h @@ -88,6 +88,7 @@ namespace obmysql { #define OB_MYSQL_BINCMP_FLAG (1 << 17) #define OB_MYSQL_GET_FIXED_FIELDS_FLAG (1 << 18) #define OB_MYSQL_FIELD_IN_PART_FUNC_FLAG (1 << 19) +#define IS_BOOL_FLAG (1 << 20) enum EMySQLFieldType { MYSQL_TYPE_DECIMAL, diff --git a/src/share/object/ob_obj_cast.cpp b/src/share/object/ob_obj_cast.cpp index fe162ecb3d..56cbcc03d9 100644 --- a/src/share/object/ob_obj_cast.cpp +++ b/src/share/object/ob_obj_cast.cpp @@ -1213,7 +1213,12 @@ static int int_json(const ObObjType expect_type, ObObjCastParams ¶ms, LOG_ERROR("NULL allocator in json cast function", K(ret), K(in), K(expect_type)); } else { ObJsonInt j_int(in.get_int()); + bool bool_val = (in.get_int() == 1) ? true : false; + ObJsonBoolean j_bool(bool_val); ObIJsonBase *j_base = &j_int; + if (CM_HAS_BOOLEAN_FLAG(cast_mode)) { + j_base = &j_bool; + } ObString raw_bin; if (OB_FAIL(j_base->get_raw_binary(raw_bin, params.allocator_v2_))) { LOG_WARN("fail to get int json binary", K(ret), K(in), K(expect_type), K(*j_base)); diff --git a/src/share/object/ob_obj_cast.h b/src/share/object/ob_obj_cast.h index 67f1e063bc..3f25d3e36c 100644 --- a/src/share/object/ob_obj_cast.h +++ b/src/share/object/ob_obj_cast.h @@ -43,6 +43,7 @@ namespace common { #define CM_STRICT_MODE (1ULL << 8) #define CM_SET_MIN_IF_OVERFLOW (1ULL << 9) #define CM_ERROR_ON_SCALE_OVER (1ULL << 10) +#define CM_TO_BOOLEAN (1ULL << 11) // when casting string to integer, round if not set this flag, otherwise trunc. // but ceil will always round to +inf, and floor will always round to -inf. #define CM_STRING_INTEGER_TRUNC (1ULL << 57) @@ -84,6 +85,7 @@ typedef uint64_t ObCastMode; #define CM_IS_SET_MIN_IF_OVERFLOW(mode) ((CM_SET_MIN_IF_OVERFLOW & (mode)) != 0) #define CM_IS_ERROR_ON_SCALE_OVER(mode) ((CM_ERROR_ON_SCALE_OVER & (mode)) != 0) #define CM_IS_JSON_VALUE(mode) CM_IS_ERROR_ON_SCALE_OVER(mode) +#define CM_HAS_BOOLEAN_FLAG(mode) ((CM_TO_BOOLEAN & (mode)) != 0) struct ObObjCastParams { // add params when necessary diff --git a/src/sql/engine/expr/ob_expr_to_type.cpp b/src/sql/engine/expr/ob_expr_to_type.cpp index ed23e70275..7ca1a22bf5 100644 --- a/src/sql/engine/expr/ob_expr_to_type.cpp +++ b/src/sql/engine/expr/ob_expr_to_type.cpp @@ -66,6 +66,12 @@ int ObExprToType::calc_result1(ObObj& result, const ObObj& obj1, ObExprCtx& expr EXPR_DEFINE_CAST_CTX(expr_ctx, cast_mode_); if (ob_is_json(expect_type_)) { cast_ctx.dest_collation_ = result_type_.get_collation_type(); + bool is_bool = false; + if (OB_FAIL(get_param_is_boolean(expr_ctx, obj1, is_bool))) { + LOG_WARN("get is_boolean type failed, bool may be cast as json int", K(ret), K(obj1)); + } else { + cast_ctx.cast_mode_ |= CM_TO_BOOLEAN; + } } if (OB_FAIL(ObObjCaster::to_type(expect_type_, cast_ctx, obj1, result))) { LOG_WARN("failed to cast obj", K(ret)); @@ -119,6 +125,9 @@ int ObExprToType::calc_result_type_for_literal(ObExprResType& type, ObExprResTyp } } else if (lib::is_mysql_mode() && ob_is_json(expect_type_)) { cast_coll_type = CS_TYPE_UTF8MB4_BIN; + if (type1.has_result_flag(IS_BOOL_FLAG)) { + cast_mode |= CM_TO_BOOLEAN; + } } ObAccuracy res_accuracy; diff --git a/src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp b/src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp index 900d6d89ed..de302704e2 100644 --- a/src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp +++ b/src/sql/resolver/expr/ob_raw_expr_deduce_type.cpp @@ -1768,7 +1768,11 @@ int ObRawExprDeduceType::visit(ObSysFunRawExpr& expr) LOG_WARN("fail to visit for column_conv", K(ret), K(i)); } } else { - if (OB_FAIL(types.push_back(param_expr->get_result_type()))) { + ObExprResType res_type = param_expr->get_result_type(); + if (param_expr->is_bool_expr()) { + res_type.set_result_flag(IS_BOOL_FLAG); + } + if (OB_FAIL(types.push_back(res_type))) { LOG_WARN("push back param type failed", K(ret)); } }