From 76504f7b732bb0714f036fa5ea1a97725c271c4f Mon Sep 17 00:00:00 2001 From: Lovro Lugovic Date: Thu, 18 Oct 2018 11:19:10 +0200 Subject: [PATCH] Fix anonymous variable capture Reviewers: teon.banek, mtomic Reviewed By: teon.banek Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D1673 --- src/query/frontend/ast/cypher_main_visitor.cpp | 1 + tests/unit/cypher_main_visitor.cpp | 17 +++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/query/frontend/ast/cypher_main_visitor.cpp b/src/query/frontend/ast/cypher_main_visitor.cpp index 674dcc73e..91ddf96a5 100644 --- a/src/query/frontend/ast/cypher_main_visitor.cpp +++ b/src/query/frontend/ast/cypher_main_visitor.cpp @@ -700,6 +700,7 @@ antlrcpp::Any CypherMainVisitor::visitReturnItem( if (ctx->variable()) { named_expr->name_ = std::string(ctx->variable()->accept(this).as()); + users_identifiers.insert(named_expr->name_); } else { if (in_with_ && !dynamic_cast(named_expr->expression_)) { throw SemanticException("Only variables can be non-aliased in WITH."); diff --git a/tests/unit/cypher_main_visitor.cpp b/tests/unit/cypher_main_visitor.cpp index f3e09f39d..b17c653b0 100644 --- a/tests/unit/cypher_main_visitor.cpp +++ b/tests/unit/cypher_main_visitor.cpp @@ -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(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(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