diff --git a/deps/oblib/src/lib/charset/ob_charset.cpp b/deps/oblib/src/lib/charset/ob_charset.cpp index 9ea14b08e8a814381930f0e9931c948aca865fa2..6bd8b6f7b018db64656eeb7741607664816575d2 100644 --- a/deps/oblib/src/lib/charset/ob_charset.cpp +++ b/deps/oblib/src/lib/charset/ob_charset.cpp @@ -2235,7 +2235,9 @@ int ObCharset::charset_convert(ObIAllocator& alloc, const ObString& in, const Ob ret = OB_INVALID_ARGUMENT; LOG_WARN("invalid collation type", K(ret), K(src_cs_type), K(dst_cs_type)); } else { - if (0 == in.length() || charset_type_by_coll(src_cs_type) == charset_type_by_coll(dst_cs_type)) { + if (0 == in.length() + || charset_type_by_coll(src_cs_type) == charset_type_by_coll(dst_cs_type) + || charset_type_by_coll(dst_cs_type) == CHARSET_BINARY) { if (!(convert_flag & COPY_STRING_ON_SAME_CHARSET)) { out = in; } else { @@ -2243,6 +2245,24 @@ int ObCharset::charset_convert(ObIAllocator& alloc, const ObString& in, const Ob LOG_WARN("fail to write string", K(ret), K(in)); } } + } else if (charset_type_by_coll(src_cs_type) == CHARSET_BINARY) { + char *buf = nullptr; + int32_t align_offset = 0; + int32_t res_buf_len = 0; + int mbminlen = ObCharset::get_charset(dst_cs_type)->mbminlen; + if (mbminlen > 0 && in.length() % mbminlen != 0) { + align_offset = mbminlen - in.length() % mbminlen; + } + res_buf_len = in.length() + align_offset; + if (OB_ISNULL(buf = static_cast(alloc.alloc(res_buf_len)))) { + ret = OB_ALLOCATE_MEMORY_FAILED; + out.reset(); + LOG_WARN("allocate memory failed", K(ret), K(in), K(align_offset)); + } else { + MEMCPY(buf + align_offset, in.ptr(), in.length()); + MEMSET(buf, 0, align_offset); + out.assign_ptr(buf, res_buf_len); + } } else { const uint32_t res_buf_len = in.length() * 4; uint32_t res_len = 0; diff --git a/src/sql/ob_sql_utils.cpp b/src/sql/ob_sql_utils.cpp index 40586bb2df5760fa4039070f9b8910f9332d7eb7..a27c1e37aed8ad8dc20b4ddb77d99b46f71530d1 100644 --- a/src/sql/ob_sql_utils.cpp +++ b/src/sql/ob_sql_utils.cpp @@ -2886,32 +2886,8 @@ int ObSQLUtils::clear_evaluated_flag(const ObExprPtrIArray& calc_exprs, ObEvalCt int ObSQLUtils::copy_and_convert_string_charset( ObIAllocator& allocator, const ObString& src, ObString& dst, ObCollationType src_coll, ObCollationType dst_coll) { - int ret = OB_SUCCESS; - - if (OB_UNLIKELY(!ObCharset::is_valid_collation(src_coll) || !ObCharset::is_valid_collation(dst_coll))) { - ret = OB_INVALID_ARGUMENT; - LOG_WARN("invalid input collation", K(ret), K(src_coll), K(dst_coll)); - } else if (OB_ISNULL(src.ptr()) || OB_UNLIKELY(0 >= src.length())) { - dst.reset(); - } else if (ObCharset::charset_type_by_coll(src_coll) == ObCharset::charset_type_by_coll(dst_coll)) { - OZ(ob_write_string(allocator, src, dst)); - } else { - const int32_t CharConvertFactorNum = 4; - char* buf = nullptr; - int32_t buf_len = src.length() * CharConvertFactorNum; - uint32_t result_len = 0; - if (OB_ISNULL(buf = static_cast(allocator.alloc(buf_len)))) { - ret = OB_ALLOCATE_MEMORY_FAILED; - LOG_WARN("allocate memory failed", K(ret), "len", buf_len); - } else if (OB_FAIL( - ObCharset::charset_convert(src_coll, src.ptr(), src.length(), dst_coll, buf, buf_len, result_len))) { - LOG_WARN("fail to do charset convert", K(ret)); - } else { - dst.assign_ptr(buf, result_len); - } - } - - return ret; + return ObCharset::charset_convert(allocator, src, src_coll, dst_coll, dst, + ObCharset::COPY_STRING_ON_SAME_CHARSET); } int ObSQLUtils::update_session_last_schema_version(