mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-11-21 15:12:02 +00:00
Add reinterpretAsUUID function
This commit is contained in:
parent
c48d212320
commit
e45edd57f2
@ -39,8 +39,11 @@ public:
|
|||||||
const char * name() const throw() override { return "DB::Exception"; }
|
const char * name() const throw() override { return "DB::Exception"; }
|
||||||
const char * what() const throw() override { return message().data(); }
|
const char * what() const throw() override { return message().data(); }
|
||||||
|
|
||||||
/// Add something to the existing message.
|
template <typename ...Fmt>
|
||||||
void addMessage(const std::string & arg) { extendedMessage(arg); }
|
void addMessage(Fmt&&... fmt)
|
||||||
|
{
|
||||||
|
extendedMessage(fmt::format(std::forward<Fmt>(fmt)...));
|
||||||
|
}
|
||||||
|
|
||||||
std::string getStackTraceString() const;
|
std::string getStackTraceString() const;
|
||||||
|
|
||||||
|
@ -390,13 +390,21 @@ String BaseSettings<Traits_>::valueToStringUtil(const std::string_view & name, c
|
|||||||
template <typename Traits_>
|
template <typename Traits_>
|
||||||
Field BaseSettings<Traits_>::stringToValueUtil(const std::string_view & name, const String & str)
|
Field BaseSettings<Traits_>::stringToValueUtil(const std::string_view & name, const String & str)
|
||||||
{
|
{
|
||||||
const auto & accessor = Traits::Accessor::instance();
|
try
|
||||||
if (size_t index = accessor.find(name); index != static_cast<size_t>(-1))
|
{
|
||||||
return accessor.stringToValueUtil(index, str);
|
const auto & accessor = Traits::Accessor::instance();
|
||||||
if constexpr (Traits::allow_custom_settings)
|
if (size_t index = accessor.find(name); index != static_cast<size_t>(-1))
|
||||||
return Field::restoreFromDump(str);
|
return accessor.stringToValueUtil(index, str);
|
||||||
else
|
if constexpr (Traits::allow_custom_settings)
|
||||||
BaseSettingsHelpers::throwSettingNotFound(name);
|
return Field::restoreFromDump(str);
|
||||||
|
else
|
||||||
|
BaseSettingsHelpers::throwSettingNotFound(name);
|
||||||
|
}
|
||||||
|
catch (Exception & e)
|
||||||
|
{
|
||||||
|
e.addMessage("while parsing value '{}' for setting '{}'", str, name);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Traits_>
|
template <typename Traits_>
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
#include <DataTypes/DataTypeFixedString.h>
|
#include <DataTypes/DataTypeFixedString.h>
|
||||||
#include <DataTypes/DataTypeDate.h>
|
#include <DataTypes/DataTypeDate.h>
|
||||||
#include <DataTypes/DataTypeDateTime.h>
|
#include <DataTypes/DataTypeDateTime.h>
|
||||||
|
#include <DataTypes/DataTypeUUID.h>
|
||||||
#include <Columns/ColumnString.h>
|
#include <Columns/ColumnString.h>
|
||||||
#include <Columns/ColumnFixedString.h>
|
#include <Columns/ColumnFixedString.h>
|
||||||
#include <Columns/ColumnConst.h>
|
#include <Columns/ColumnConst.h>
|
||||||
@ -68,7 +69,7 @@ public:
|
|||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
for (size_t i = 0; i < size; ++i)
|
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));
|
memcpy(&value, &data_from[offset], std::min(static_cast<UInt64>(sizeof(ToFieldType)), offsets_from[i] - offset - 1));
|
||||||
vec_res[i] = value;
|
vec_res[i] = value;
|
||||||
offset = offsets_from[i];
|
offset = offsets_from[i];
|
||||||
@ -90,7 +91,7 @@ public:
|
|||||||
size_t copy_size = std::min(step, sizeof(ToFieldType));
|
size_t copy_size = std::min(step, sizeof(ToFieldType));
|
||||||
for (size_t i = 0; i < size; ++i)
|
for (size_t i = 0; i < size; ++i)
|
||||||
{
|
{
|
||||||
ToFieldType value = 0;
|
ToFieldType value{};
|
||||||
memcpy(&value, &data_from[offset], copy_size);
|
memcpy(&value, &data_from[offset], copy_size);
|
||||||
vec_res[i] = value;
|
vec_res[i] = value;
|
||||||
offset += step;
|
offset += step;
|
||||||
@ -120,6 +121,7 @@ struct NameReinterpretAsFloat32 { static constexpr auto name = "reinterpretA
|
|||||||
struct NameReinterpretAsFloat64 { static constexpr auto name = "reinterpretAsFloat64"; };
|
struct NameReinterpretAsFloat64 { static constexpr auto name = "reinterpretAsFloat64"; };
|
||||||
struct NameReinterpretAsDate { static constexpr auto name = "reinterpretAsDate"; };
|
struct NameReinterpretAsDate { static constexpr auto name = "reinterpretAsDate"; };
|
||||||
struct NameReinterpretAsDateTime { static constexpr auto name = "reinterpretAsDateTime"; };
|
struct NameReinterpretAsDateTime { static constexpr auto name = "reinterpretAsDateTime"; };
|
||||||
|
struct NameReinterpretAsUUID { static constexpr auto name = "reinterpretAsUUID"; };
|
||||||
|
|
||||||
using FunctionReinterpretAsUInt8 = FunctionReinterpretStringAs<DataTypeUInt8, NameReinterpretAsUInt8>;
|
using FunctionReinterpretAsUInt8 = FunctionReinterpretStringAs<DataTypeUInt8, NameReinterpretAsUInt8>;
|
||||||
using FunctionReinterpretAsUInt16 = FunctionReinterpretStringAs<DataTypeUInt16, NameReinterpretAsUInt16>;
|
using FunctionReinterpretAsUInt16 = FunctionReinterpretStringAs<DataTypeUInt16, NameReinterpretAsUInt16>;
|
||||||
@ -133,6 +135,7 @@ using FunctionReinterpretAsFloat32 = FunctionReinterpretStringAs<DataTypeFloat32
|
|||||||
using FunctionReinterpretAsFloat64 = FunctionReinterpretStringAs<DataTypeFloat64, NameReinterpretAsFloat64>;
|
using FunctionReinterpretAsFloat64 = FunctionReinterpretStringAs<DataTypeFloat64, NameReinterpretAsFloat64>;
|
||||||
using FunctionReinterpretAsDate = FunctionReinterpretStringAs<DataTypeDate, NameReinterpretAsDate>;
|
using FunctionReinterpretAsDate = FunctionReinterpretStringAs<DataTypeDate, NameReinterpretAsDate>;
|
||||||
using FunctionReinterpretAsDateTime = FunctionReinterpretStringAs<DataTypeDateTime, NameReinterpretAsDateTime>;
|
using FunctionReinterpretAsDateTime = FunctionReinterpretStringAs<DataTypeDateTime, NameReinterpretAsDateTime>;
|
||||||
|
using FunctionReinterpretAsUUID = FunctionReinterpretStringAs<DataTypeUUID, NameReinterpretAsUUID>;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -150,6 +153,7 @@ void registerFunctionsReinterpretStringAs(FunctionFactory & factory)
|
|||||||
factory.registerFunction<FunctionReinterpretAsFloat64>();
|
factory.registerFunction<FunctionReinterpretAsFloat64>();
|
||||||
factory.registerFunction<FunctionReinterpretAsDate>();
|
factory.registerFunction<FunctionReinterpretAsDate>();
|
||||||
factory.registerFunction<FunctionReinterpretAsDateTime>();
|
factory.registerFunction<FunctionReinterpretAsDateTime>();
|
||||||
|
factory.registerFunction<FunctionReinterpretAsUUID>();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
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
|
3f1ed72e-f7fe-4459-9cbe-95fe9298f845
|
||||||
|
1
|
||||||
|
@ -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 '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 materialize('01234567-89ab-cdef-0123-456789abcdef') AS str, UUIDNumToString(UUIDStringToNum(str)), UUIDNumToString(UUIDStringToNum(toFixedString(str, 36)));
|
||||||
SELECT toString(toUUID('3f1ed72e-f7fe-4459-9cbe-95fe9298f845'));
|
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;
|
||||||
|
Loading…
Reference in New Issue
Block a user