diff --git a/src/Functions/FunctionsHashing.h b/src/Functions/FunctionsHashing.h index 69c3a299eea..7525153bd48 100644 --- a/src/Functions/FunctionsHashing.h +++ b/src/Functions/FunctionsHashing.h @@ -55,7 +55,7 @@ #include #include #include - +#include namespace DB { @@ -1026,16 +1026,37 @@ private: if constexpr (Impl::use_int_hash_for_pods) { if constexpr (std::is_same_v) - h = IntHash64Impl::apply(bit_cast(vec_from[i])); + { + UInt64 v = bit_cast(vec_from[i]); + + /// Consider using std::byteswap(c++23) in the future + if constexpr (std::endian::native == std::endian::big) + v = __builtin_bswap64(v); + h = IntHash64Impl::apply(v); + } else - h = IntHash32Impl::apply(bit_cast(vec_from[i])); + { + UInt32 v = bit_cast(vec_from[i]); + if constexpr (std::endian::native == std::endian::big) + v = __builtin_bswap32(v); + h = IntHash32Impl::apply(v); + } } else { - if (std::is_same_v) + if constexpr (std::is_same_v) h = JavaHashImpl::apply(vec_from[i]); else - h = apply(key, reinterpret_cast(&vec_from[i]), sizeof(vec_from[i])); + { + FromType v = vec_from[i]; + if constexpr (std::endian::native == std::endian::big) + { + FromType tmp_v; + reverseMemcpy(&tmp_v, &v, sizeof(v)); + v = tmp_v; + } + h = apply(key, reinterpret_cast(&v), sizeof(v)); + } } if constexpr (first) @@ -1049,9 +1070,19 @@ private: auto value = col_from_const->template getValue(); ToType hash; if constexpr (std::is_same_v) - hash = IntHash64Impl::apply(bit_cast(value)); + { + UInt64 v = bit_cast(value); + if constexpr (std::endian::native == std::endian::big) + v = __builtin_bswap64(v); + hash = IntHash64Impl::apply(v); + } else - hash = IntHash32Impl::apply(bit_cast(value)); + { + UInt32 v = bit_cast(value); + if constexpr (std::endian::native == std::endian::big) + v = __builtin_bswap32(v); + hash = IntHash32Impl::apply(v); + } size_t size = vec_to.size(); if constexpr (first) @@ -1080,8 +1111,17 @@ private: size_t size = vec_from.size(); for (size_t i = 0; i < size; ++i) { - ToType h = apply(key, reinterpret_cast(&vec_from[i]), sizeof(vec_from[i])); - + ToType h; + if constexpr (std::endian::native == std::endian::little) + { + h = apply(key, reinterpret_cast(&vec_from[i]), sizeof(vec_from[i])); + } + else + { + char tmp_buffer[sizeof(vec_from[i])]; + reverseMemcpy(tmp_buffer, &vec_from[i], sizeof(vec_from[i])); + h = apply(key, reinterpret_cast(tmp_buffer), sizeof(vec_from[i])); + } if constexpr (first) vec_to[i] = h; else @@ -1092,8 +1132,17 @@ private: { auto value = col_from_const->template getValue(); - ToType h = apply(key, reinterpret_cast(&value), sizeof(value)); - + ToType h; + if constexpr (std::endian::native == std::endian::little) + { + h = apply(key, reinterpret_cast(&value), sizeof(value)); + } + else + { + char tmp_buffer[sizeof(value)]; + reverseMemcpy(tmp_buffer, &value, sizeof(value)); + h = apply(key, reinterpret_cast(tmp_buffer), sizeof(value)); + } size_t size = vec_to.size(); if constexpr (first) {