Add reinterpretAsUUID function

This commit is contained in:
Alexander Kuzmenkov 2020-09-30 20:19:32 +03:00
parent c48d212320
commit e45edd57f2
5 changed files with 33 additions and 11 deletions

View File

@ -39,8 +39,11 @@ public:
const char * name() const throw() override { return "DB::Exception"; }
const char * what() const throw() override { return message().data(); }
/// Add something to the existing message.
void addMessage(const std::string & arg) { extendedMessage(arg); }
template <typename ...Fmt>
void addMessage(Fmt&&... fmt)
{
extendedMessage(fmt::format(std::forward<Fmt>(fmt)...));
}
std::string getStackTraceString() const;

View File

@ -390,13 +390,21 @@ String BaseSettings<Traits_>::valueToStringUtil(const std::string_view & name, c
template <typename Traits_>
Field BaseSettings<Traits_>::stringToValueUtil(const std::string_view & name, const String & str)
{
const auto & accessor = Traits::Accessor::instance();
if (size_t index = accessor.find(name); index != static_cast<size_t>(-1))
return accessor.stringToValueUtil(index, str);
if constexpr (Traits::allow_custom_settings)
return Field::restoreFromDump(str);
else
BaseSettingsHelpers::throwSettingNotFound(name);
try
{
const auto & accessor = Traits::Accessor::instance();
if (size_t index = accessor.find(name); index != static_cast<size_t>(-1))
return accessor.stringToValueUtil(index, str);
if constexpr (Traits::allow_custom_settings)
return Field::restoreFromDump(str);
else
BaseSettingsHelpers::throwSettingNotFound(name);
}
catch (Exception & e)
{
e.addMessage("while parsing value '{}' for setting '{}'", str, name);
throw;
}
}
template <typename Traits_>

View File

@ -5,6 +5,7 @@
#include <DataTypes/DataTypeFixedString.h>
#include <DataTypes/DataTypeDate.h>
#include <DataTypes/DataTypeDateTime.h>
#include <DataTypes/DataTypeUUID.h>
#include <Columns/ColumnString.h>
#include <Columns/ColumnFixedString.h>
#include <Columns/ColumnConst.h>
@ -68,7 +69,7 @@ public:
size_t offset = 0;
for (size_t i = 0; i < size; ++i)
{
ToFieldType value = 0;
ToFieldType value{};
memcpy(&value, &data_from[offset], std::min(static_cast<UInt64>(sizeof(ToFieldType)), offsets_from[i] - offset - 1));
vec_res[i] = value;
offset = offsets_from[i];
@ -90,7 +91,7 @@ public:
size_t copy_size = std::min(step, sizeof(ToFieldType));
for (size_t i = 0; i < size; ++i)
{
ToFieldType value = 0;
ToFieldType value{};
memcpy(&value, &data_from[offset], copy_size);
vec_res[i] = value;
offset += step;
@ -120,6 +121,7 @@ struct NameReinterpretAsFloat32 { static constexpr auto name = "reinterpretA
struct NameReinterpretAsFloat64 { static constexpr auto name = "reinterpretAsFloat64"; };
struct NameReinterpretAsDate { static constexpr auto name = "reinterpretAsDate"; };
struct NameReinterpretAsDateTime { static constexpr auto name = "reinterpretAsDateTime"; };
struct NameReinterpretAsUUID { static constexpr auto name = "reinterpretAsUUID"; };
using FunctionReinterpretAsUInt8 = FunctionReinterpretStringAs<DataTypeUInt8, NameReinterpretAsUInt8>;
using FunctionReinterpretAsUInt16 = FunctionReinterpretStringAs<DataTypeUInt16, NameReinterpretAsUInt16>;
@ -133,6 +135,7 @@ using FunctionReinterpretAsFloat32 = FunctionReinterpretStringAs<DataTypeFloat32
using FunctionReinterpretAsFloat64 = FunctionReinterpretStringAs<DataTypeFloat64, NameReinterpretAsFloat64>;
using FunctionReinterpretAsDate = FunctionReinterpretStringAs<DataTypeDate, NameReinterpretAsDate>;
using FunctionReinterpretAsDateTime = FunctionReinterpretStringAs<DataTypeDateTime, NameReinterpretAsDateTime>;
using FunctionReinterpretAsUUID = FunctionReinterpretStringAs<DataTypeUUID, NameReinterpretAsUUID>;
}
@ -150,6 +153,7 @@ void registerFunctionsReinterpretStringAs(FunctionFactory & factory)
factory.registerFunction<FunctionReinterpretAsFloat64>();
factory.registerFunction<FunctionReinterpretAsDate>();
factory.registerFunction<FunctionReinterpretAsDateTime>();
factory.registerFunction<FunctionReinterpretAsUUID>();
}
}

View File

@ -5,3 +5,4 @@
01234567-89ab-cdef-0123-456789abcdef 01234567-89ab-cdef-0123-456789abcdef 01234567-89ab-cdef-0123-456789abcdef
01234567-89ab-cdef-0123-456789abcdef 01234567-89ab-cdef-0123-456789abcdef 01234567-89ab-cdef-0123-456789abcdef
3f1ed72e-f7fe-4459-9cbe-95fe9298f845
1

View File

@ -5,3 +5,9 @@ SELECT hex(UUIDStringToNum(materialize('01234567-89ab-cdef-0123-456789abcdef')))
SELECT '01234567-89ab-cdef-0123-456789abcdef' AS str, UUIDNumToString(UUIDStringToNum(str)), UUIDNumToString(UUIDStringToNum(toFixedString(str, 36)));
SELECT materialize('01234567-89ab-cdef-0123-456789abcdef') AS str, UUIDNumToString(UUIDStringToNum(str)), UUIDNumToString(UUIDStringToNum(toFixedString(str, 36)));
SELECT toString(toUUID('3f1ed72e-f7fe-4459-9cbe-95fe9298f845'));
-- conversion back and forth to big-endian hex string
with generateUUIDv4() as uuid,
identity(lower(hex(reverse(reinterpretAsString(uuid))))) as str,
reinterpretAsUUID(reverse(unhex(str))) as uuid2
select uuid = uuid2;