From 464d79eb06937966b922007093d0b2436388a329 Mon Sep 17 00:00:00 2001 From: artpaul Date: Tue, 3 Oct 2017 18:40:21 +0500 Subject: [PATCH] #33 cut off milliseconds from timestamps --- driver/escaping/escape_sequences.cpp | 34 +++++++++++++++++++++++++++- driver/ut/escape_sequences_ut.cpp | 12 ++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/driver/escaping/escape_sequences.cpp b/driver/escaping/escape_sequences.cpp index d7700daa7..bf9b4f0ca 100644 --- a/driver/escaping/escape_sequences.cpp +++ b/driver/escaping/escape_sequences.cpp @@ -104,12 +104,44 @@ string processDate(const StringView seq, Lexer& lex) { } } +string removeMilliseconds(const StringView token) { + if (token.empty()) { + return string(); + } + + const char* begin = token.data(); + const char* p = begin + token.size() - 1; + const char* dot = nullptr; + const bool quoted = (*p == '\''); + if (quoted) { + --p; + } + for (; p > begin; --p) { + if (isdigit(*p)) { + continue; + } + if (*p == '.') { + if (dot) { + return token.to_string(); + } + dot = p; + } else { + if (dot) { + return string(begin, dot) + (quoted ? "'" : ""); + } + return token.to_string(); + } + } + + return token.to_string(); +} + string processDateTime(const StringView seq, Lexer& lex) { Token data = lex.Consume(Token::STRING); if (data.isInvalid()) { return seq.to_string(); } else { - return string("toDateTime(") + data.literal.to_string() + ")"; + return string("toDateTime(") + removeMilliseconds(data.literal) + ")"; } } diff --git a/driver/ut/escape_sequences_ut.cpp b/driver/ut/escape_sequences_ut.cpp index 4719c6655..0afd8be9a 100644 --- a/driver/ut/escape_sequences_ut.cpp +++ b/driver/ut/escape_sequences_ut.cpp @@ -42,4 +42,16 @@ TEST(EscapeSequencesCase, DateTime) { replaceEscapeSequences("SELECT {ts '2017-01-01 10:01:01'}"), "SELECT toDateTime('2017-01-01 10:01:01')" ); + + // We cutting off milliseconds from timestamp because CH server + // doesn't support them. + ASSERT_EQ( + replaceEscapeSequences("SELECT {ts '2017-01-01 10:01:01.555'}"), + "SELECT toDateTime('2017-01-01 10:01:01')" + ); + // Strange date format. Symbols after last dot shouldn't be cutted off. + ASSERT_EQ( + replaceEscapeSequences("SELECT {ts '2017.01.01 10:01:01'}"), + "SELECT toDateTime('2017.01.01 10:01:01')" + ); }