From 8d8490ac5805c01b51603eec4333d2326febbece Mon Sep 17 00:00:00 2001 From: ltrk2 <107155950+ltrk2@users.noreply.github.com> Date: Wed, 23 Aug 2023 11:14:34 -0700 Subject: [PATCH] Implement endianness-independent serialization for UUIDs --- .../Serializations/SerializationUUID.cpp | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/DataTypes/Serializations/SerializationUUID.cpp b/src/DataTypes/Serializations/SerializationUUID.cpp index 93658fd05a3..947394a2bd8 100644 --- a/src/DataTypes/Serializations/SerializationUUID.cpp +++ b/src/DataTypes/Serializations/SerializationUUID.cpp @@ -7,6 +7,7 @@ #include #include +#include namespace DB { @@ -136,23 +137,31 @@ void SerializationUUID::deserializeBinary(IColumn & column, ReadBuffer & istr, c void SerializationUUID::serializeBinaryBulk(const IColumn & column, WriteBuffer & ostr, size_t offset, size_t limit) const { const typename ColumnVector::Container & x = typeid_cast &>(column).getData(); - - size_t size = x.size(); - - if (limit == 0 || offset + limit > size) + if (const size_t size = x.size(); limit == 0 || offset + limit > size) limit = size - offset; - if (limit) + if (limit == 0) + return; + + if constexpr (std::endian::native == std::endian::big) + { + std::ranges::for_each( + x | std::views::drop(offset) | std::views::take(limit), [&ostr](const auto & uuid) { writeBinaryLittleEndian(uuid, ostr); }); + } + else ostr.write(reinterpret_cast(&x[offset]), sizeof(UUID) * limit); } void SerializationUUID::deserializeBinaryBulk(IColumn & column, ReadBuffer & istr, size_t limit, double /*avg_value_size_hint*/) const { typename ColumnVector::Container & x = typeid_cast &>(column).getData(); - size_t initial_size = x.size(); + const size_t initial_size = x.size(); x.resize(initial_size + limit); - size_t size = istr.readBig(reinterpret_cast(&x[initial_size]), sizeof(UUID) * limit); + const size_t size = istr.readBig(reinterpret_cast(&x[initial_size]), sizeof(UUID) * limit); x.resize(initial_size + size / sizeof(UUID)); -} + if constexpr (std::endian::native == std::endian::big) + std::ranges::for_each( + x | std::views::drop(initial_size), [](auto & uuid) { transformEndianness(uuid); }); +} }