Merge pull request #11856 from zhang2014/feature/uuid_without_separator

Support parse UUID without separator
This commit is contained in:
alexey-milovidov 2020-06-25 16:26:55 +03:00 committed by GitHub
commit 9de5db9025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 56 additions and 9 deletions

View File

@ -33,11 +33,8 @@ void parseHex(IteratorSrc src, IteratorDst dst, const size_t num_bytes)
{
size_t src_pos = 0;
size_t dst_pos = 0;
for (; dst_pos < num_bytes; ++dst_pos)
{
dst[dst_pos] = UInt8(unhex(src[src_pos])) * 16 + UInt8(unhex(src[src_pos + 1]));
src_pos += 2;
}
for (; dst_pos < num_bytes; ++dst_pos, src_pos += 2)
dst[dst_pos] = unhex2(reinterpret_cast<const char *>(&src[src_pos]));
}
void parseUUID(const UInt8 * src36, UInt8 * dst16)
@ -51,6 +48,13 @@ void parseUUID(const UInt8 * src36, UInt8 * dst16)
parseHex(&src36[24], &dst16[10], 6);
}
void parseUUIDWithoutSeparator(const UInt8 * src36, UInt8 * dst16)
{
/// If string is not like UUID - implementation specific behaviour.
parseHex(&src36[0], &dst16[0], 16);
}
/** Function used when byte ordering is important when parsing uuid
* ex: When we create an UUID type
*/
@ -66,6 +70,17 @@ void parseUUID(const UInt8 * src36, std::reverse_iterator<UInt8 *> dst16)
parseHex(&src36[24], dst16 + 2, 6);
}
/** Function used when byte ordering is important when parsing uuid
* ex: When we create an UUID type
*/
void parseUUIDWithoutSeparator(const UInt8 * src36, std::reverse_iterator<UInt8 *> dst16)
{
/// If string is not like UUID - implementation specific behaviour.
parseHex(&src36[0], dst16 + 8, 8);
parseHex(&src36[16], dst16, 8);
}
UInt128 stringToUUID(const String & str)
{
return parseFromString<UUID>(str);

View File

@ -515,7 +515,9 @@ struct NullSink
};
void parseUUID(const UInt8 * src36, UInt8 * dst16);
void parseUUIDWithoutSeparator(const UInt8 * src36, UInt8 * dst16);
void parseUUID(const UInt8 * src36, std::reverse_iterator<UInt8 *> dst16);
void parseUUIDWithoutSeparator(const UInt8 * src36, std::reverse_iterator<UInt8 *> dst16);
template <typename IteratorSrc, typename IteratorDst>
void formatHex(IteratorSrc src, IteratorDst dst, const size_t num_bytes);
@ -602,15 +604,30 @@ inline bool tryReadDateText(DayNum & date, ReadBuffer & buf)
inline void readUUIDText(UUID & uuid, ReadBuffer & buf)
{
char s[36];
size_t size = buf.read(s, 36);
size_t size = buf.read(s, 32);
if (size != 36)
if (size == 32)
{
if (s[8] == '-')
{
size += buf.read(&s[32], 4);
if (size != 36)
{
s[size] = 0;
throw Exception(std::string("Cannot parse uuid ") + s, ErrorCodes::CANNOT_PARSE_UUID);
}
parseUUID(reinterpret_cast<const UInt8 *>(s), std::reverse_iterator<UInt8 *>(reinterpret_cast<UInt8 *>(&uuid) + 16));
}
else
parseUUIDWithoutSeparator(reinterpret_cast<const UInt8 *>(s), std::reverse_iterator<UInt8 *>(reinterpret_cast<UInt8 *>(&uuid) + 16));
}
else
{
s[size] = 0;
throw Exception(std::string("Cannot parse uuid ") + s, ErrorCodes::CANNOT_PARSE_UUID);
}
parseUUID(reinterpret_cast<const UInt8 *>(s), std::reverse_iterator<UInt8 *>(reinterpret_cast<UInt8 *>(&uuid) + 16));
}

View File

@ -0,0 +1,4 @@
417ddc5d-e556-4d27-95dd-a34d84e46a50
417ddc5d-e556-4d27-95dd-a34d84e46a50
1 417ddc5d-e556-4d27-95dd-a34d84e46a50 Example 1
2 417ddc5d-e556-4d27-95dd-a34d84e46a51 Example 2

View File

@ -0,0 +1,11 @@
SELECT toUUID('417ddc5de5564d2795dda34d84e46a50');
SELECT toUUID('417ddc5d-e556-4d27-95dd-a34d84e46a50');
DROP TABLE IF EXISTS t_uuid;
CREATE TABLE t_uuid (x UInt8, y UUID, z String) ENGINE = TinyLog;
INSERT INTO t_uuid VALUES (1, '417ddc5de5564d2795dda34d84e46a50', 'Example 1');
INSERT INTO t_uuid VALUES (2, '417ddc5d-e556-4d27-95dd-a34d84e46a51', 'Example 2');
SELECT * FROM t_uuid ORDER BY x ASC;
DROP TABLE IF EXISTS t_uuid;