From a69c936b44449ec82380dbd9f912ea4ec204d96c Mon Sep 17 00:00:00 2001 From: chertus Date: Wed, 29 Aug 2018 14:58:40 +0300 Subject: [PATCH] improve field visitors for UUID CLICKHOUSE-3249 --- dbms/src/Common/FieldVisitors.cpp | 21 +++----------------- dbms/src/IO/ReadHelpers.h | 5 +++++ dbms/src/IO/WriteHelpers.h | 14 ++++++------- dbms/src/Interpreters/convertFieldToType.cpp | 13 ------------ 4 files changed, 15 insertions(+), 38 deletions(-) diff --git a/dbms/src/Common/FieldVisitors.cpp b/dbms/src/Common/FieldVisitors.cpp index 0bf90d03b70..e2b855c8f83 100644 --- a/dbms/src/Common/FieldVisitors.cpp +++ b/dbms/src/Common/FieldVisitors.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -12,11 +13,6 @@ namespace DB { -UInt128 stringToUUID(const String & str) -{ - return parseFromString(str); -} - template static inline String formatQuoted(T x) { @@ -50,14 +46,8 @@ String FieldVisitorDump::operator() (const Float64 & x) const { return formatQuo String FieldVisitorDump::operator() (const DecimalField & x) const { return formatQuotedWithPrefix(x, "Decimal32_"); } String FieldVisitorDump::operator() (const DecimalField & x) const { return formatQuotedWithPrefix(x, "Decimal64_"); } String FieldVisitorDump::operator() (const DecimalField & x) const { return formatQuotedWithPrefix(x, "Decimal128_"); } +String FieldVisitorDump::operator() (const UInt128 & x) const { return formatQuotedWithPrefix(UUID(x), "UUID_"); } -String FieldVisitorDump::operator() (const UInt128 & x) const -{ - WriteBufferFromOwnString wb; - wb << "UInt128_" << x.low << "_" << x.high; - return wb.str(); - -} String FieldVisitorDump::operator() (const String & x) const { @@ -130,12 +120,7 @@ String FieldVisitorToString::operator() (const String & x) const { return format String FieldVisitorToString::operator() (const DecimalField & x) const { return formatQuoted(x); } String FieldVisitorToString::operator() (const DecimalField & x) const { return formatQuoted(x); } String FieldVisitorToString::operator() (const DecimalField & x) const { return formatQuoted(x); } - -String FieldVisitorToString::operator() (const UInt128 & x) const -{ - /// Dummy implementation. There is no UInt128 literals in SQL. - return FieldVisitorDump()(x); -} +String FieldVisitorToString::operator() (const UInt128 & x) const { return formatQuoted(UUID(x)); } String FieldVisitorToString::operator() (const Array & x) const { diff --git a/dbms/src/IO/ReadHelpers.h b/dbms/src/IO/ReadHelpers.h index 4bf3771c72f..0fbc4a99e91 100644 --- a/dbms/src/IO/ReadHelpers.h +++ b/dbms/src/IO/ReadHelpers.h @@ -561,6 +561,11 @@ inline T parseFromString(const String & str) return parse(str.data(), str.size()); } +inline UInt128 stringToUUID(const String & str) +{ + return parseFromString(str); +} + template ReturnType readDateTimeTextFallback(time_t & datetime, ReadBuffer & buf, const DateLUTImpl & date_lut); diff --git a/dbms/src/IO/WriteHelpers.h b/dbms/src/IO/WriteHelpers.h index d840139777d..d58fd784c6b 100644 --- a/dbms/src/IO/WriteHelpers.h +++ b/dbms/src/IO/WriteHelpers.h @@ -704,13 +704,7 @@ inline void writeText(const char * x, size_t size, WriteBuffer & buf) { writeEsc inline void writeText(const LocalDate & x, WriteBuffer & buf) { writeDateText(x, buf); } inline void writeText(const LocalDateTime & x, WriteBuffer & buf) { writeDateTimeText(x, buf); } inline void writeText(const UUID & x, WriteBuffer & buf) { writeUUIDText(x, buf); } -inline void writeText(const UInt128 &, WriteBuffer &) -{ - /** Because UInt128 isn't a natural type, without arithmetic operator and only use as an intermediary type -for UUID- - * it should never arrive here. But because we used the DataTypeNumber class we should have at least a definition of it. - */ - throw Exception("UInt128 cannot be write as a text", ErrorCodes::ILLEGAL_TYPE_OF_ARGUMENT); -} +inline void writeText(const UInt128 & x, WriteBuffer & buf) { writeText(UUID(x), buf); } template inline T decimalScaleMultiplier(UInt32 scale); template <> inline Int32 decimalScaleMultiplier(UInt32 scale) { return common::exp10_i32(scale); } @@ -763,6 +757,12 @@ inline void writeQuoted(const LocalDateTime & x, WriteBuffer & buf) writeChar('\'', buf); } +inline void writeQuoted(const UUID & x, WriteBuffer & buf) +{ + writeChar('\'', buf); + writeText(x, buf); + writeChar('\'', buf); +} /// String, date, datetime are in double quotes with C-style escaping. Numbers - without. template diff --git a/dbms/src/Interpreters/convertFieldToType.cpp b/dbms/src/Interpreters/convertFieldToType.cpp index 36f5063ac00..22e688f150b 100644 --- a/dbms/src/Interpreters/convertFieldToType.cpp +++ b/dbms/src/Interpreters/convertFieldToType.cpp @@ -138,19 +138,6 @@ UInt64 stringToDateTime(const String & s) return UInt64(date_time); } -UInt128 stringToUUID(const String & s) -{ - ReadBufferFromString in(s); - UUID uuid; - - readText(uuid, in); - if (!in.eof()) - throw Exception("String is too long for UUID: " + s); - - return UInt128(uuid); -} - - Field convertFieldToTypeImpl(const Field & src, const IDataType & type) { if (type.isValueRepresentedByNumber())