Skip to content

Commit

Permalink
[Fix](functions) fix strcmp return value #34565
Browse files Browse the repository at this point in the history
  • Loading branch information
zclllyybb authored May 12, 2024
1 parent 2053380 commit 5cc7dd5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 11 deletions.
24 changes: 13 additions & 11 deletions be/src/vec/common/string_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@

// IWYU pragma: no_include <crc32intrin.h>
#include <glog/logging.h>
#include <stdint.h>

#include <algorithm>
#include <climits>
#include <cstdint>
#include <cstring>
#include <ostream>
#include <string>
Expand All @@ -38,7 +38,6 @@
#include "util/hash_util.hpp"
#include "util/slice.h"
#include "util/sse_util.hpp"
#include "vec/common/string_ref.h"
#include "vec/common/unaligned.h"
#include "vec/core/types.h"

Expand Down Expand Up @@ -150,7 +149,8 @@ inline bool memequalSSE2Wide(const char* p1, const char* p2, size_t size) {
// - s1/n1: ptr/len for the first string
// - s2/n2: ptr/len for the second string
// - len: min(n1, n2) - this can be more cheaply passed in by the caller
inline int string_compare(const char* s1, int64_t n1, const char* s2, int64_t n2, int64_t len) {
PURE inline int string_compare(const char* s1, int64_t n1, const char* s2, int64_t n2,
int64_t len) {
DCHECK_EQ(len, std::min(n1, n2));
#if defined(__SSE4_2__) || defined(__aarch64__)
while (len >= sse_util::CHARS_PER_128_BIT_REGISTER) {
Expand Down Expand Up @@ -199,8 +199,8 @@ struct StringRef {

std::string to_string() const { return std::string(data, size); }
std::string debug_string() const { return to_string(); }
std::string_view to_string_view() const { return std::string_view(data, size); }
Slice to_slice() const { return doris::Slice(data, size); }
std::string_view to_string_view() const { return {data, size}; }
Slice to_slice() const { return {data, size}; }

// this is just for show, e.g. print data to error log, to avoid print large string.
std::string to_prefix(size_t length) const { return std::string(data, std::min(length, size)); }
Expand All @@ -209,7 +209,7 @@ struct StringRef {
operator std::string_view() const { return std::string_view {data, size}; }

StringRef substring(int start_pos, int new_len) const {
return StringRef(data + start_pos, (new_len < 0) ? (size - start_pos) : new_len);
return {data + start_pos, (new_len < 0) ? (size - start_pos) : new_len};
}

StringRef substring(int start_pos) const { return substring(start_pos, size - start_pos); }
Expand Down Expand Up @@ -255,7 +255,9 @@ struct StringRef {
}
}

return string_compare(this->data, this->size, other.data, other.size, l);
// string_compare doesn't have sign result
int cmp_result = string_compare(this->data, this->size, other.data, other.size, l);
return (cmp_result > 0) - (cmp_result < 0);
}

void replace(const char* ptr, int len) {
Expand Down Expand Up @@ -363,8 +365,8 @@ inline size_t hash_less_than8(const char* data, size_t size) {

inline size_t hash_less_than16(const char* data, size_t size) {
if (size > 8) {
doris::vectorized::UInt64 a = unaligned_load<doris::vectorized::UInt64>(data);
doris::vectorized::UInt64 b = unaligned_load<doris::vectorized::UInt64>(data + size - 8);
auto a = unaligned_load<doris::vectorized::UInt64>(data);
auto b = unaligned_load<doris::vectorized::UInt64>(data + size - 8);
return hash_len16(a, rotate_by_at_least1(b + size, size)) ^ b;
}

Expand All @@ -388,13 +390,13 @@ struct CRC32Hash {
size_t res = -1ULL;

do {
doris::vectorized::UInt64 word = unaligned_load<doris::vectorized::UInt64>(pos);
auto word = unaligned_load<doris::vectorized::UInt64>(pos);
res = _mm_crc32_u64(res, word);

pos += 8;
} while (pos + 8 < end);

doris::vectorized::UInt64 word = unaligned_load<doris::vectorized::UInt64>(
auto word = unaligned_load<doris::vectorized::UInt64>(
end - 8); /// I'm not sure if this is normal.
res = _mm_crc32_u64(res, word);

Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -358,4 +358,7 @@ suite("test_string_function", "arrow_flight_sql") {
qt_sql_func_char9 """ select char(0) = ' '; """
qt_sql_func_char10 """ select char(0) = '\0'; """

qt_strcmp1 """ select strcmp('a', 'abc'); """
qt_strcmp2 """ select strcmp('abc', 'abc'); """
qt_strcmp3 """ select strcmp('abcd', 'abc'); """
}

0 comments on commit 5cc7dd5

Please sign in to comment.