diff --git a/src/query/frontend/ast/cypher_main_visitor.cpp b/src/query/frontend/ast/cypher_main_visitor.cpp index fdf695149..d0245a2bd 100644 --- a/src/query/frontend/ast/cypher_main_visitor.cpp +++ b/src/query/frontend/ast/cypher_main_visitor.cpp @@ -33,6 +33,40 @@ antlrcpp::Any CypherMainVisitor::visitExplainQuery( return query_; } +antlrcpp::Any CypherMainVisitor::visitCypherQuery( + MemgraphCypher::CypherQueryContext *ctx) { + query_ = storage_.query(); + DCHECK(ctx->singleQuery()) << "Expected single query."; + query_->single_query_ = ctx->singleQuery()->accept(this).as(); + + // Check that union and union all dont mix + bool has_union = false; + bool has_union_all = false; + for (auto *child : ctx->cypherUnion()) { + if (child->ALL()) { + has_union_all = true; + } else { + has_union = true; + } + if (has_union && has_union_all) { + throw SemanticException("Invalid combination of UNION and UNION ALL."); + } + query_->cypher_unions_.push_back(child->accept(this).as()); + } + + return query_; +} + +antlrcpp::Any CypherMainVisitor::visitIndexQuery( + MemgraphCypher::IndexQueryContext *ctx) { + query_ = storage_.query(); + DCHECK(ctx->createIndex()) << "Expected CREATE INDEX"; + query_->single_query_ = storage_.Create(); + query_->single_query_->clauses_.emplace_back( + ctx->createIndex()->accept(this).as()); + return query_; +} + antlrcpp::Any CypherMainVisitor::visitAuthQuery( MemgraphCypher::AuthQueryContext *ctx) { query_ = storage_.query(); @@ -67,30 +101,6 @@ antlrcpp::Any CypherMainVisitor::visitStreamQuery( return query_; } -antlrcpp::Any CypherMainVisitor::visitRegularQuery( - MemgraphCypher::RegularQueryContext *ctx) { - query_ = storage_.query(); - DCHECK(ctx->singleQuery()) << "Expected single query."; - query_->single_query_ = ctx->singleQuery()->accept(this).as(); - - // Check that union and union all dont mix - bool has_union = false; - bool has_union_all = false; - for (auto *child : ctx->cypherUnion()) { - if (child->ALL()) { - has_union_all = true; - } else { - has_union = true; - } - if (has_union && has_union_all) { - throw SemanticException("Invalid combination of UNION and UNION ALL."); - } - query_->cypher_unions_.push_back(child->accept(this).as()); - } - - return query_; -} - antlrcpp::Any CypherMainVisitor::visitCypherUnion( MemgraphCypher::CypherUnionContext *ctx) { bool distinct = !ctx->ALL(); @@ -227,10 +237,6 @@ antlrcpp::Any CypherMainVisitor::visitClause( if (ctx->unwind()) { return static_cast(ctx->unwind()->accept(this).as()); } - if (ctx->createIndex()) { - return static_cast( - ctx->createIndex()->accept(this).as()); - } // TODO: implement other clauses. throw utils::NotYetImplemented("clause '{}'", ctx->getText()); return 0; diff --git a/src/query/frontend/ast/cypher_main_visitor.hpp b/src/query/frontend/ast/cypher_main_visitor.hpp index 53b960d4c..388ba6875 100644 --- a/src/query/frontend/ast/cypher_main_visitor.hpp +++ b/src/query/frontend/ast/cypher_main_visitor.hpp @@ -134,6 +134,18 @@ class CypherMainVisitor : public antlropencypher::MemgraphCypherBaseVisitor { return expression; } + /** + * @return Query* + */ + antlrcpp::Any visitCypherQuery( + MemgraphCypher::CypherQueryContext *ctx) override; + + /** + * @return Query* + */ + antlrcpp::Any visitIndexQuery( + MemgraphCypher::IndexQueryContext *ctx) override; + /** * @return Query* */ @@ -151,12 +163,6 @@ class CypherMainVisitor : public antlropencypher::MemgraphCypherBaseVisitor { antlrcpp::Any visitStreamQuery( MemgraphCypher::StreamQueryContext *ctx) override; - /** - * @return Query* - */ - antlrcpp::Any visitRegularQuery( - MemgraphCypher::RegularQueryContext *ctx) override; - /** * @return CypherUnion* */ diff --git a/src/query/frontend/opencypher/grammar/Cypher.g4 b/src/query/frontend/opencypher/grammar/Cypher.g4 index bb1d3eef9..7044e5925 100644 --- a/src/query/frontend/opencypher/grammar/Cypher.g4 +++ b/src/query/frontend/opencypher/grammar/Cypher.g4 @@ -23,13 +23,16 @@ cypher : statement ';'? EOF ; statement : query ; -query : regularQuery +query : cypherQuery + | indexQuery | explainQuery ; -explainQuery : EXPLAIN regularQuery ; +explainQuery : EXPLAIN cypherQuery ; -regularQuery : singleQuery ( cypherUnion )* ; +cypherQuery : singleQuery ( cypherUnion )* ; + +indexQuery : createIndex ; singleQuery : clause ( clause )* ; @@ -46,7 +49,6 @@ clause : cypherMatch | remove | with | cypherReturn - | createIndex ; cypherMatch : OPTIONAL? MATCH pattern where? ; diff --git a/src/query/frontend/opencypher/grammar/MemgraphCypher.g4 b/src/query/frontend/opencypher/grammar/MemgraphCypher.g4 index a2935e1e8..aa3f24551 100644 --- a/src/query/frontend/opencypher/grammar/MemgraphCypher.g4 +++ b/src/query/frontend/opencypher/grammar/MemgraphCypher.g4 @@ -45,10 +45,11 @@ symbolicName : UnescapedSymbolicName | memgraphCypherKeyword ; -query : regularQuery +query : cypherQuery + | indexQuery + | explainQuery | authQuery | streamQuery - | explainQuery ; authQuery : createRole diff --git a/tests/unit/cypher_main_visitor.cpp b/tests/unit/cypher_main_visitor.cpp index e949e879e..25727a2cf 100644 --- a/tests/unit/cypher_main_visitor.cpp +++ b/tests/unit/cypher_main_visitor.cpp @@ -1510,14 +1510,6 @@ TYPED_TEST(CypherMainVisitorTest, ClausesOrdering) { SemanticException); TypeParam("UNWIND [1,2,3] AS x CREATE (n) RETURN x"); TypeParam("CREATE (n) WITH n UNWIND [1,2,3] AS x RETURN x"); - - TypeParam("CREATE INDEX ON :a(b)"); - ASSERT_THROW(TypeParam("CREATE INDEX ON :a(n) CREATE INDEX ON :b(c)"), - SemanticException); - ASSERT_THROW(TypeParam("CREATE (n) CREATE INDEX ON :a(n)"), - SemanticException); - ASSERT_THROW(TypeParam("CREATE INDEX ON :a(n) RETURN 2 + 2"), - SemanticException); } TYPED_TEST(CypherMainVisitorTest, Merge) {