Fix anonymous variable capture

Reviewers: teon.banek, mtomic

Reviewed By: teon.banek

Subscribers: pullbot

Differential Revision: https://phabricator.memgraph.io/D1673
This commit is contained in:
Lovro Lugovic 2018-10-18 11:19:10 +02:00
parent 125502cbb5
commit 76504f7b73
2 changed files with 18 additions and 0 deletions

View File

@ -700,6 +700,7 @@ antlrcpp::Any CypherMainVisitor::visitReturnItem(
if (ctx->variable()) {
named_expr->name_ =
std::string(ctx->variable()->accept(this).as<std::string>());
users_identifiers.insert(named_expr->name_);
} else {
if (in_with_ && !dynamic_cast<Identifier *>(named_expr->expression_)) {
throw SemanticException("Only variables can be non-aliased in WITH.");

View File

@ -1461,6 +1461,23 @@ TYPED_TEST(CypherMainVisitorTest, WithWhere) {
ASSERT_EQ(identifier2->name_, "n");
}
TYPED_TEST(CypherMainVisitorTest, WithAnonymousVariableCapture) {
TypeParam ast_generator("WITH 5 as anon1 MATCH () return *");
auto *query = ast_generator.query_;
ASSERT_TRUE(query->single_query_);
auto *single_query = query->single_query_;
ASSERT_EQ(single_query->clauses_.size(), 3U);
auto *match = dynamic_cast<Match *>(single_query->clauses_[1]);
ASSERT_TRUE(match);
ASSERT_EQ(match->patterns_.size(), 1U);
auto *pattern = match->patterns_[0];
ASSERT_TRUE(pattern);
ASSERT_EQ(pattern->atoms_.size(), 1U);
auto *atom = dynamic_cast<NodeAtom *>(pattern->atoms_[0]);
ASSERT_TRUE(atom);
ASSERT_NE("anon1", atom->identifier_->name_);
}
TYPED_TEST(CypherMainVisitorTest, ClausesOrdering) {
// Obviously some of the ridiculous combinations don't fail here, but they
// will fail in semantic analysis or they make perfect sense as a part of