From b820cfb3b67c673c9af002c9caa0315ff62bf6ca Mon Sep 17 00:00:00 2001 From: zhang2014 Date: Mon, 22 Jun 2020 21:30:55 +0800 Subject: [PATCH] fix read extra bytes when with separator --- src/IO/ReadHelpers.h | 14 ++++++++++++-- .../01338_uuid_without_separator.reference | 2 ++ .../0_stateless/01338_uuid_without_separator.sql | 9 +++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/IO/ReadHelpers.h b/src/IO/ReadHelpers.h index a64fbacd467..08d5d6f9de1 100644 --- a/src/IO/ReadHelpers.h +++ b/src/IO/ReadHelpers.h @@ -634,12 +634,22 @@ 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 >= 32) + 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(s), std::reverse_iterator(reinterpret_cast(&uuid) + 16)); + } else parseUUID(reinterpret_cast(s), std::reverse_iterator(reinterpret_cast(&uuid) + 16)); } diff --git a/tests/queries/0_stateless/01338_uuid_without_separator.reference b/tests/queries/0_stateless/01338_uuid_without_separator.reference index f7575a7f9f8..c009ee7a13f 100644 --- a/tests/queries/0_stateless/01338_uuid_without_separator.reference +++ b/tests/queries/0_stateless/01338_uuid_without_separator.reference @@ -1,2 +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 diff --git a/tests/queries/0_stateless/01338_uuid_without_separator.sql b/tests/queries/0_stateless/01338_uuid_without_separator.sql index 4dd26a7629e..efbf4bc2812 100644 --- a/tests/queries/0_stateless/01338_uuid_without_separator.sql +++ b/tests/queries/0_stateless/01338_uuid_without_separator.sql @@ -1,2 +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;