diff --git a/src/share/object/ob_obj_cast.cpp b/src/share/object/ob_obj_cast.cpp index 478458e42..5268d911a 100644 --- a/src/share/object/ob_obj_cast.cpp +++ b/src/share/object/ob_obj_cast.cpp @@ -7829,13 +7829,19 @@ int get_bit_len(const ObString& str, int32_t& bit_len) } else { const char* ptr = str.ptr(); uint32_t uneven_value = reinterpret_cast(ptr[0]); + int32_t len = str.length(); if (0 == uneven_value) { - bit_len = 1; + if (len > 8) { + // Compatible with MySQL, if the length of bit string greater than 8 Bytes, + // it would be considered too long. We set bit_len to OB_MAX_BIT_LENGTH + 1. + bit_len = OB_MAX_BIT_LENGTH + 1; + } else { + bit_len = 1; + } } else { // Built-in Function: int __builtin_clz (unsigned int x). // Returns the number of leading 0-bits in x, starting at the most significant bit position. // If x is 0, the result is undefined. - int32_t len = str.length(); int32_t uneven_len = static_cast(sizeof(unsigned int) * 8 - __builtin_clz(uneven_value)); bit_len = uneven_len + 8 * (len - 1); } diff --git a/src/sql/engine/expr/ob_datum_cast.cpp b/src/sql/engine/expr/ob_datum_cast.cpp index 8514c257d..ea1457a5b 100644 --- a/src/sql/engine/expr/ob_datum_cast.cpp +++ b/src/sql/engine/expr/ob_datum_cast.cpp @@ -916,13 +916,19 @@ static OB_INLINE int common_get_bit_len(const ObString& str, int32_t& bit_len) } else { const char* ptr = str.ptr(); uint32_t uneven_value = reinterpret_cast(ptr[0]); + int32_t len = str.length(); if (0 == uneven_value) { - bit_len = 1; + if (len > 8) { + // Compatible with MySQL, if the length of bit string greater than 8 Bytes, + // it would be considered too long. We set bit_len to OB_MAX_BIT_LENGTH + 1. + bit_len = OB_MAX_BIT_LENGTH + 1; + } else { + bit_len = 1; + } } else { // Built-in Function: int __builtin_clz (unsigned int x). // Returns the number of leading 0-bits in x, starting at the most significant bit position. // If x is 0, the result is undefined. - int32_t len = str.length(); int32_t uneven_len = static_cast(sizeof(unsigned int) * 8 - __builtin_clz(uneven_value)); bit_len = uneven_len + 8 * (len - 1); }