Fix line comment stripping in query stripper

Summary:
When there was an empty line comment starting right at the end of the
query, stripper wouldn't properly change state from `IN_LINE_COMMENT` to `OUT`
and it would return the wrong length (0) from `MatchWhitespaceAndComments`.
Because of that, the two slashes would be interpreted as division operators.

Query "RETURN 5;//" would be changed by stripper into "RETURN 5; / /" which
obviously can't be parsed;

Reviewers: teon.banek

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1819
This commit is contained in:
Marin Tomic 2019-01-18 11:19:15 +01:00
parent f8e76efa3e
commit 903263d896
2 changed files with 20 additions and 2 deletions
src/query/frontend
tests/unit

View File

@ -462,7 +462,7 @@ int StrippedQuery::MatchWhitespaceAndComments(int start) const {
State state = State::OUT;
int i = start;
int len = original_.size();
// We need to remember at which position comment started because if we faile
// We need to remember at which position comment started because if we fail
// to match comment finish we have a match until comment start position.
int comment_position = -1;
while (i < len) {
@ -478,7 +478,11 @@ int StrippedQuery::MatchWhitespaceAndComments(int start) const {
} else if (i + 1 < len && original_[i] == '/' &&
original_[i + 1] == '/') {
comment_position = i;
state = State::IN_LINE_COMMENT;
if (i + 2 < len) {
// Special case for an empty line comment starting right at the end of
// the query.
state = State::IN_LINE_COMMENT;
}
i += 2;
} else {
break;

View File

@ -249,6 +249,20 @@ TEST(QueryStripper, LineComment4) {
EXPECT_EQ(stripped.query(), "MaTch ( n : peropero ) return n / / komentar");
}
TEST(QueryStripper, LineComment5) {
{
StrippedQuery stripped("MaTch (n:peropero) return n//");
EXPECT_EQ(stripped.literals().size(), 0);
EXPECT_EQ(stripped.query(), "MaTch ( n : peropero ) return n");
}
{
StrippedQuery stripped("MATCH (n) MATCH (n)-[*bfs]->(m) RETURN n;\n//");
EXPECT_EQ(stripped.literals().size(), 0);
EXPECT_EQ(stripped.query(),
"MATCH ( n ) MATCH ( n ) - [ * bfs ] - > ( m ) RETURN n ;");
}
}
TEST(QueryStripper, Spaces) {
StrippedQuery stripped(u8"RETURN \r\n\u202f\t\u2007 NuLl");
EXPECT_EQ(stripped.literals().size(), 0);