From 6e3f35fc942b332c1506668edc2fc5be1242a88b Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Fri, 20 Oct 2023 16:18:39 +0800 Subject: [PATCH 1/5] fix issue https://github.com/ClickHouse/ClickHouse/issues/55858 --- src/IO/readFloatText.h | 6 +++--- tests/queries/0_stateless/02900_issue_55858.reference | 4 ++++ tests/queries/0_stateless/02900_issue_55858.sql | 10 ++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) create mode 100644 tests/queries/0_stateless/02900_issue_55858.reference create mode 100644 tests/queries/0_stateless/02900_issue_55858.sql diff --git a/src/IO/readFloatText.h b/src/IO/readFloatText.h index feab9589c2e..e3d549c8e17 100644 --- a/src/IO/readFloatText.h +++ b/src/IO/readFloatText.h @@ -148,11 +148,11 @@ ReturnType readFloatTextPreciseImpl(T & x, ReadBuffer & buf) static_assert('a' > '.' && 'A' > '.' && '\n' < '.' && '\t' < '.' && '\'' < '.' && '"' < '.', "Layout of char is not like ASCII"); static constexpr bool throw_exception = std::is_same_v; - - /// Fast path (avoid copying) if the buffer have at least MAX_LENGTH bytes. static constexpr int MAX_LENGTH = 316; + ReadBufferFromMemory * buf_from_memory = dynamic_cast(&buf); - if (likely(!buf.eof() && buf.position() + MAX_LENGTH <= buf.buffer().end())) + /// Fast path (avoid copying) if the buffer have at least MAX_LENGTH bytes or buf is ReadBufferFromMemory + if (likely(!buf.eof() && (buf_from_memory || buf.position() + MAX_LENGTH <= buf.buffer().end()))) { auto * initial_position = buf.position(); auto res = fast_float::from_chars(initial_position, buf.buffer().end(), x); diff --git a/tests/queries/0_stateless/02900_issue_55858.reference b/tests/queries/0_stateless/02900_issue_55858.reference new file mode 100644 index 00000000000..1d64c9ea17c --- /dev/null +++ b/tests/queries/0_stateless/02900_issue_55858.reference @@ -0,0 +1,4 @@ +0 +0 +\N +\N diff --git a/tests/queries/0_stateless/02900_issue_55858.sql b/tests/queries/0_stateless/02900_issue_55858.sql new file mode 100644 index 00000000000..b7b6704cdb5 --- /dev/null +++ b/tests/queries/0_stateless/02900_issue_55858.sql @@ -0,0 +1,10 @@ +set precise_float_parsing = 1; + +select cast('2023-01-01' as Float64); -- { serverError 6 } +select cast('2023-01-01' as Float32); -- { serverError 6 } +select toFloat32('2023-01-01'); -- { serverError 6 } +select toFloat64('2023-01-01'); -- { serverError 6 } +select toFloat32OrZero('2023-01-01'); +select toFloat64OrZero('2023-01-01'); +select toFloat32OrNull('2023-01-01'); +select toFloat64OrNull('2023-01-01'); From 2a894335b71df44564e1267f33237307b0dccbde Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Tue, 24 Oct 2023 16:16:05 +0800 Subject: [PATCH 2/5] commit again --- src/IO/readFloatText.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/IO/readFloatText.h b/src/IO/readFloatText.h index e3d549c8e17..ad14563c859 100644 --- a/src/IO/readFloatText.h +++ b/src/IO/readFloatText.h @@ -149,6 +149,7 @@ ReturnType readFloatTextPreciseImpl(T & x, ReadBuffer & buf) static constexpr bool throw_exception = std::is_same_v; static constexpr int MAX_LENGTH = 316; + ReadBufferFromMemory * buf_from_memory = dynamic_cast(&buf); /// Fast path (avoid copying) if the buffer have at least MAX_LENGTH bytes or buf is ReadBufferFromMemory From d5feaa72c1917682fb8955663804c9514c3e496f Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Wed, 25 Oct 2023 20:16:18 +0800 Subject: [PATCH 3/5] improve thrown exception message --- src/IO/readFloatText.h | 10 +++++++--- tests/queries/0_stateless/02900_issue_55858.sql | 8 ++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/IO/readFloatText.h b/src/IO/readFloatText.h index ad14563c859..c0da9129a05 100644 --- a/src/IO/readFloatText.h +++ b/src/IO/readFloatText.h @@ -161,7 +161,10 @@ ReturnType readFloatTextPreciseImpl(T & x, ReadBuffer & buf) if (unlikely(res.ec != std::errc())) { if constexpr (throw_exception) - throw ParsingException(ErrorCodes::CANNOT_PARSE_NUMBER, "Cannot read floating point value"); + throw ParsingException( + ErrorCodes::CANNOT_PARSE_NUMBER, + "Cannot read floating point value here: {}", + String(initial_position, buf.buffer().end() - initial_position)); else return ReturnType(false); } @@ -248,10 +251,11 @@ ReturnType readFloatTextPreciseImpl(T & x, ReadBuffer & buf) res = fast_float::from_chars(tmp_buf, tmp_buf + num_copied_chars, x64); x = static_cast(x64); } - if (unlikely(res.ec != std::errc())) + if (unlikely(res.ec != std::errc() || res.ptr - tmp_buf != num_copied_chars)) { if constexpr (throw_exception) - throw ParsingException(ErrorCodes::CANNOT_PARSE_NUMBER, "Cannot read floating point value"); + throw ParsingException( + ErrorCodes::CANNOT_PARSE_NUMBER, "Cannot read floating point value here: {}", String(tmp_buf, num_copied_chars)); else return ReturnType(false); } diff --git a/tests/queries/0_stateless/02900_issue_55858.sql b/tests/queries/0_stateless/02900_issue_55858.sql index b7b6704cdb5..65fc06d9797 100644 --- a/tests/queries/0_stateless/02900_issue_55858.sql +++ b/tests/queries/0_stateless/02900_issue_55858.sql @@ -1,9 +1,9 @@ set precise_float_parsing = 1; -select cast('2023-01-01' as Float64); -- { serverError 6 } -select cast('2023-01-01' as Float32); -- { serverError 6 } -select toFloat32('2023-01-01'); -- { serverError 6 } -select toFloat64('2023-01-01'); -- { serverError 6 } +select cast('2023-01-01' as Float64); -- { serverError 72 } +select cast('2023-01-01' as Float32); -- { serverError 72 } +select toFloat32('2023-01-01'); -- { serverError 72 } +select toFloat64('2023-01-01'); -- { serverError 72 } select toFloat32OrZero('2023-01-01'); select toFloat64OrZero('2023-01-01'); select toFloat32OrNull('2023-01-01'); From 3009e535063a66d854ed57aa4d8490d8bbdc8f29 Mon Sep 17 00:00:00 2001 From: taiyang-li <654010905@qq.com> Date: Thu, 26 Oct 2023 15:05:21 +0800 Subject: [PATCH 4/5] fix failed ut --- tests/queries/0_stateless/02900_issue_55858.sql | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/queries/0_stateless/02900_issue_55858.sql b/tests/queries/0_stateless/02900_issue_55858.sql index 65fc06d9797..b7b6704cdb5 100644 --- a/tests/queries/0_stateless/02900_issue_55858.sql +++ b/tests/queries/0_stateless/02900_issue_55858.sql @@ -1,9 +1,9 @@ set precise_float_parsing = 1; -select cast('2023-01-01' as Float64); -- { serverError 72 } -select cast('2023-01-01' as Float32); -- { serverError 72 } -select toFloat32('2023-01-01'); -- { serverError 72 } -select toFloat64('2023-01-01'); -- { serverError 72 } +select cast('2023-01-01' as Float64); -- { serverError 6 } +select cast('2023-01-01' as Float32); -- { serverError 6 } +select toFloat32('2023-01-01'); -- { serverError 6 } +select toFloat64('2023-01-01'); -- { serverError 6 } select toFloat32OrZero('2023-01-01'); select toFloat64OrZero('2023-01-01'); select toFloat32OrNull('2023-01-01'); From 308b2942d7a3168139a38f18e2283fa4596addc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=89=AC?= <654010905@qq.com> Date: Fri, 27 Oct 2023 11:42:33 +0800 Subject: [PATCH 5/5] Update readFloatText.h --- src/IO/readFloatText.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/IO/readFloatText.h b/src/IO/readFloatText.h index c0da9129a05..b0682576183 100644 --- a/src/IO/readFloatText.h +++ b/src/IO/readFloatText.h @@ -151,7 +151,6 @@ ReturnType readFloatTextPreciseImpl(T & x, ReadBuffer & buf) static constexpr int MAX_LENGTH = 316; ReadBufferFromMemory * buf_from_memory = dynamic_cast(&buf); - /// Fast path (avoid copying) if the buffer have at least MAX_LENGTH bytes or buf is ReadBufferFromMemory if (likely(!buf.eof() && (buf_from_memory || buf.position() + MAX_LENGTH <= buf.buffer().end()))) {