Skip to content

Commit

Permalink
#33 cut off milliseconds from timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
artpaul committed Oct 3, 2017
1 parent 3a58f19 commit 464d79e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
34 changes: 33 additions & 1 deletion driver/escaping/escape_sequences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ")";
}
}

Expand Down
12 changes: 12 additions & 0 deletions driver/ut/escape_sequences_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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')"
);
}

0 comments on commit 464d79e

Please sign in to comment.