cypher parser update

This commit is contained in:
Marko Budiselic 2016-07-09 14:50:04 +01:00
parent b538fb041c
commit 84db4b6657
25 changed files with 327 additions and 62 deletions

View File

@ -202,8 +202,8 @@ set(memgraph_src_files
)
# hard coded implementation of queries
add_executable(queries src/query_engine/main_queries.cpp ${memgraph_src_files})
target_link_libraries(queries ${fmt_static_lib})
# add_executable(queries src/query_engine/main_queries.cpp ${memgraph_src_files})
# target_link_libraries(queries ${fmt_static_lib})
# tests
enable_testing()

View File

@ -18,3 +18,4 @@
#include "start.hpp"
#include "set.hpp"
#include "expr.hpp"
#include "with.hpp"

View File

@ -6,6 +6,7 @@ namespace ast
{
struct Identifier;
struct IdentifierList;
struct Alias;
// properties
@ -42,6 +43,7 @@ struct Node;
struct LabelList;
struct Pattern;
struct PatternExpr;
struct PatternList;
struct Return;
struct ReturnList;
@ -58,17 +60,29 @@ struct WriteQuery;
struct ReadQuery;
struct UpdateQuery;
struct DeleteQuery;
struct ReadWriteQuery;
struct SetKey;
struct SetValue;
struct SetElement;
struct SetList;
struct AstVisitor : public Visitor<Accessor, Boolean, Float, Identifier, Alias,
Integer, String, Property, And, Or, Lt, Gt, Ge, Le, Eq, Ne, Plus, Minus,
Star, Slash, Rem, PropertyList, RelationshipList, Relationship, Node,
RelationshipSpecs, LabelList, ReturnList, Pattern, PatternExpr, Match, ReadQuery,
Start, Where, WriteQuery, Create, Return, Distinct, Delete,
DeleteQuery, UpdateQuery, Set, SetKey, SetValue, SetElement, SetList> {};
struct WithList;
struct WithClause;
struct WithQuery;
struct AstVisitor
: public Visitor<Accessor, Boolean, Float, Identifier, Alias, Integer,
String, Property, And, Or, Lt, Gt, Ge, Le, Eq, Ne, Plus,
Minus, Star, Slash, Rem, PropertyList, RelationshipList,
Relationship, Node, RelationshipSpecs, LabelList,
ReturnList, Pattern, PatternExpr, PatternList, Match,
ReadQuery, Start, Where, WriteQuery, Create, Return,
Distinct, Delete, DeleteQuery, UpdateQuery, Set, SetKey,
ReadWriteQuery, IdentifierList,
WithList, WithClause, WithQuery,
SetValue, SetElement, SetList>
{
};
}

View File

@ -3,6 +3,7 @@
#include <string>
#include "ast_node.hpp"
#include "list.hpp"
namespace ast
{
@ -15,4 +16,9 @@ struct Identifier : public AstNode<Identifier>
std::string name;
};
struct IdentifierList : public List<Identifier, IdentifierList>
{
using List::List;
};
}

View File

@ -9,10 +9,10 @@ namespace ast
struct Match : public AstNode<Match>
{
Match(Pattern* pattern, Where* where)
: pattern(pattern), where(where) {}
Match(PatternList* pattern_list, Where* where)
: pattern_list(pattern_list), where(where) {}
Pattern* pattern;
PatternList* pattern_list;
Where* where;
};

View File

@ -10,62 +10,62 @@ struct And : public BinaryExpr<And>
using BinaryExpr::BinaryExpr;
};
struct Or : public BinaryExpr<And>
struct Or : public BinaryExpr<Or>
{
using BinaryExpr::BinaryExpr;
};
struct Lt : public BinaryExpr<And>
struct Lt : public BinaryExpr<Lt>
{
using BinaryExpr::BinaryExpr;
};
struct Gt : public BinaryExpr<And>
struct Gt : public BinaryExpr<Gt>
{
using BinaryExpr::BinaryExpr;
};
struct Ge : public BinaryExpr<And>
struct Ge : public BinaryExpr<Ge>
{
using BinaryExpr::BinaryExpr;
};
struct Le : public BinaryExpr<And>
struct Le : public BinaryExpr<Le>
{
using BinaryExpr::BinaryExpr;
};
struct Eq : public BinaryExpr<And>
struct Eq : public BinaryExpr<Eq>
{
using BinaryExpr::BinaryExpr;
};
struct Ne : public BinaryExpr<And>
struct Ne : public BinaryExpr<Ne>
{
using BinaryExpr::BinaryExpr;
};
struct Plus : public BinaryExpr<And>
struct Plus : public BinaryExpr<Plus>
{
using BinaryExpr::BinaryExpr;
};
struct Minus : public BinaryExpr<And>
struct Minus : public BinaryExpr<Minus>
{
using BinaryExpr::BinaryExpr;
};
struct Star : public BinaryExpr<And>
struct Star : public BinaryExpr<Star>
{
using BinaryExpr::BinaryExpr;
};
struct Slash : public BinaryExpr<And>
struct Slash : public BinaryExpr<Slash>
{
using BinaryExpr::BinaryExpr;
};
struct Rem : public BinaryExpr<And>
struct Rem : public BinaryExpr<Rem>
{
using BinaryExpr::BinaryExpr;
};

View File

@ -17,4 +17,9 @@ struct Pattern : public AstNode<Pattern>
Pattern* next;
};
struct PatternList : public List<Pattern, PatternList>
{
using List::List;
};
}

View File

@ -2,52 +2,72 @@
#include "ast_node.hpp"
#include "create.hpp"
#include "match.hpp"
#include "set.hpp"
#include "delete.hpp"
#include "match.hpp"
#include "return.hpp"
#include "set.hpp"
namespace ast
{
struct WriteQuery : public AstNode<WriteQuery>
{
WriteQuery(Create* create, Return* return_clause)
: create(create), return_clause(return_clause) {}
WriteQuery(Create *create, Return *return_clause)
: create(create), return_clause(return_clause)
{
}
Create* create;
Return* return_clause;
Create *create;
Return *return_clause;
};
struct ReadQuery : public AstNode<ReadQuery>
{
ReadQuery(Match* match, Return* return_clause)
: match(match), return_clause(return_clause) {}
ReadQuery(Match *match, Return *return_clause)
: match(match), return_clause(return_clause)
{
}
Match* match;
Return* return_clause;
Match *match;
Return *return_clause;
};
struct UpdateQuery : public AstNode<UpdateQuery>
{
UpdateQuery(Match* match_clause, Set* set_clause, Return* return_clause)
UpdateQuery(Match *match_clause, Set *set_clause, Return *return_clause)
: match_clause(match_clause), set_clause(set_clause),
return_clause(return_clause)
{
}
Match* match_clause;
Set* set_clause;
Return* return_clause;
Match *match_clause;
Set *set_clause;
Return *return_clause;
};
struct DeleteQuery : public AstNode<DeleteQuery>
{
DeleteQuery(Match* match, Delete* delete_clause)
: match(match), delete_clause(delete_clause) {}
DeleteQuery(Match *match, Delete *delete_clause)
: match(match), delete_clause(delete_clause)
{
}
Match* match;
Delete* delete_clause;
Match *match;
Delete *delete_clause;
};
struct ReadWriteQuery : public AstNode<ReadWriteQuery>
{
ReadWriteQuery(Match *match_clause,
Create *create_clause, Return *return_clause)
: match_clause(match_clause),
create_clause(create_clause), return_clause(return_clause)
{
}
Match *match_clause;
Create *create_clause;
Return *return_clause;
};
}

41
src/cypher/ast/with.hpp Normal file
View File

@ -0,0 +1,41 @@
#pragma once
#include <string>
#include "ast_node.hpp"
#include "identifier.hpp"
#include "match.hpp"
namespace ast
{
struct WithClause : public AstNode<WithClause>
{
WithClause(IdentifierList *identifier_list, Match *match_clause)
: identifier_list(identifier_list), match_clause(match_clause)
{
}
IdentifierList *identifier_list;
Match *match_clause;
};
struct WithList : public List<WithClause, WithList>
{
using List::List;
};
struct WithQuery : public AstNode<WithQuery>
{
WithQuery(Match *match_clause, WithList *with_list, Return *return_clause)
: match_clause(match_clause), with_list(with_list),
return_clause(return_clause)
{
}
Match *match_clause;
WithList *with_list;
Return *return_clause;
};
}

View File

@ -55,22 +55,71 @@
%left PLUS MINUS.
%left STAR SLASH REM.
// start structure
// -- start structure
start ::= write_query(WQ). {
ast->root = WQ;
start ::= with_query(Q). {
ast->root = Q;
}
start ::= read_query(RQ). {
ast->root = RQ;
start ::= write_query(Q). {
ast->root = Q;
}
start ::= update_query(UQ). {
ast->root = UQ;
start ::= read_query(Q). {
ast->root = Q;
}
start ::= delete_query(DQ). {
ast->root = DQ;
start ::= update_query(Q). {
ast->root = Q;
}
start ::= delete_query(Q). {
ast->root = Q;
}
start ::= read_write_query(Q). {
ast->root = Q;
}
// -- with query
%type with_query {ast::WithQuery*}
with_query(WQ) ::= with_start_clause(SQ) with_list(WL) return_clause(RC). {
WQ = ast->create<ast::WithQuery>(SQ, WL, RC);
}
%type with_list {ast::WithList*}
with_list(L) ::= WITH with_clause(WC) with_list(N). {
L = ast->create<ast::WithList>(WC, N);
}
with_list(L) ::= . {
L = nullptr;
}
// TODO: replace Match with something that has Match, Create, etc.
%type with_start_clause {ast::Match*}
with_start_clause(WSC) ::= match_clause(MC). {
WSC = MC;
}
%type with_clause {ast::WithClause*}
with_clause(WC) ::= identifier_list(IL) with_match_clause(MC). {
WC = ast->create<ast::WithClause>(IL, MC);
}
%type with_match_clause {ast::Match*}
with_match_clause(WMC) ::= match_clause(M). {
WMC = M;
}
with_match_clause(WMC) ::= where_clause(WC). {
WMC = ast->create<ast::Match>(nullptr, WC);
}
// write query structure
@ -93,6 +142,21 @@ read_query(RQ) ::= match_clause(M) return_clause(R). {
RQ = ast->create<ast::ReadQuery>(M, R);
}
// -- match create query
%type read_write_query {ast::ReadWriteQuery*}
read_write_query(Q) ::= match_clause(M) create_clause(C) return_clause(R). {
Q = ast->create<ast::ReadWriteQuery>(M, C, R);
}
read_write_query(Q) ::= match_clause(M) create_clause(C). {
Q = ast->create<ast::ReadWriteQuery>(M, C, nullptr);
}
// ---------------------
// update query structure
%type update_query {ast::UpdateQuery*}
@ -129,7 +193,7 @@ create_clause(C) ::= CREATE pattern(P). {
%type match_clause {ast::Match*}
match_clause(M) ::= MATCH pattern(P) where_clause(W). {
match_clause(M) ::= MATCH pattern_list(P) where_clause(W). {
M = ast->create<ast::Match>(P, W);
}
@ -139,6 +203,20 @@ delete_clause(D) ::= DELETE idn(I). {
D = ast->create<ast::Delete>(I);
}
// ---- pattern list
%type pattern_list {ast::PatternList*}
pattern_list(L) ::= pattern(P) COMMA pattern_list(N). {
L = ast->create<ast::PatternList>(P, N);
}
pattern_list(L) ::= pattern(P). {
L = ast->create<ast::PatternList>(P, nullptr);
}
// ----
%type pattern {ast::Pattern*}
pattern(P) ::= node(N) rel(R) pattern(NEXT). {
@ -372,6 +450,20 @@ idn(I) ::= IDN(X). {
I = ast->create<ast::Identifier>(X->value);
}
// ---- identifier list
%type identifier_list {ast::IdentifierList*}
identifier_list(L) ::= idn(I) COMMA identifier_list(N). {
L = ast->create<ast::IdentifierList>(I, N);
}
identifier_list(L) ::= idn(I). {
L = ast->create<ast::IdentifierList>(I, nullptr);
}
// ----
value_expr(E) ::= INT(V). {
auto value = std::stoi(V->value);
E = ast->create<ast::Integer>(value);

View File

@ -94,6 +94,12 @@ public:
Traverser::visit(read_query);
}
void visit(ast::ReadWriteQuery& query) override
{
auto entry = printer.advance("Read Write Query");
Traverser::visit(query);
}
void visit(ast::Match& match) override
{
auto entry = printer.advance("Match");
@ -112,6 +118,12 @@ public:
Traverser::visit(pattern_expr);
}
void visit(ast::PatternList& pattern_list) override
{
auto entry = printer.advance("Pattern List");
Traverser::visit(pattern_list);
}
void visit(ast::Node& node) override
{
auto entry = printer.advance("Node");
@ -130,6 +142,12 @@ public:
entry << "Identifier '" << idn.name << "'";
}
void visit(ast::IdentifierList& list) override
{
auto entry = printer.advance("Identifier List");
Traverser::visit(list);
}
void visit(ast::Return& return_clause) override
{
auto entry = printer.advance("Return");
@ -353,6 +371,24 @@ public:
Traverser::visit(set_list);
}
void visit(ast::WithClause& with_clause) override
{
auto entry = printer.advance("With Clause");
Traverser::visit(with_clause);
}
void visit(ast::WithList& with_list) override
{
auto entry = printer.advance("With List");
Traverser::visit(with_list);
}
void visit(ast::WithQuery& with_query) override
{
auto entry = printer.advance("With Query");
Traverser::visit(with_query);
}
private:
Printer printer;
};

View File

@ -49,6 +49,7 @@ public:
rule("(?i:RETURN)", TK_RETURN);
rule("(?i:DISTINCT)", TK_DISTINCT);
rule("(?i:DELETE)", TK_DELETE);
rule("(?i:WITH)", TK_WITH);
rule("(?i:AND)", TK_AND);
rule("(?i:OR)", TK_OR);

View File

@ -10,8 +10,9 @@ public:
using uptr = std::unique_ptr<Traverser>;
using sptr = std::shared_ptr<Traverser>;
void visit(ast::Start& start) override
void visit(ast::Start& start_query) override
{
// DELETE
}
void visit(ast::DeleteQuery& delete_query) override
@ -26,9 +27,19 @@ public:
accept(read_query.return_clause);
}
void visit(ast::ReadWriteQuery& query) override
{
accept(query.match_clause);
accept(query.create_clause);
if (query.return_clause != nullptr)
accept(query.return_clause);
}
void visit(ast::Match& match) override
{
accept(match.pattern);
accept(match.pattern_list);
accept(match.where);
}
@ -53,10 +64,8 @@ public:
void visit(ast::Return& return_clause) override
{
if (return_clause.return_list != nullptr)
accept(return_clause.return_list);
if (return_clause.distinct != nullptr)
accept(return_clause.distinct);
accept(return_clause.return_list);
accept(return_clause.distinct);
}
void visit(ast::Accessor& accessor) override
@ -154,12 +163,24 @@ public:
accept(prop_list.next);
}
void visit(ast::PatternList& pattern_list) override
{
accept(pattern_list.value);
accept(pattern_list.next);
}
void visit(ast::RelationshipList& rel_list) override
{
accept(rel_list.value);
accept(rel_list.next);
}
void visit(ast::IdentifierList& list) override
{
accept(list.value);
accept(list.next);
}
void visit(ast::Relationship& rel) override
{
accept(rel.specs);
@ -199,8 +220,7 @@ public:
{
accept(update_query.match_clause);
accept(update_query.set_clause);
if (update_query.return_clause != nullptr)
accept(update_query.return_clause);
accept(update_query.return_clause);
}
void visit(ast::Set& set_clause) override
@ -222,8 +242,7 @@ public:
void visit(ast::SetList& set_list) override
{
accept(set_list.value);
if (set_list.next != nullptr)
accept(set_list.next);
accept(set_list.next);
}
void visit(ast::Create& create) override
@ -241,6 +260,25 @@ public:
accept(delete_clause.identifier);
}
void visit(ast::WithClause& with_clause) override
{
accept(with_clause.identifier_list);
accept(with_clause.match_clause);
}
void visit(ast::WithList& with_list) override
{
accept(with_list.value);
accept(with_list.next);
}
void visit(ast::WithQuery& with_query) override
{
accept(with_query.match_clause);
accept(with_query.with_list);
accept(with_query.return_clause);
}
protected:
template<class T>
void accept(T* node)

View File

@ -50,7 +50,7 @@ foreach(test ${concurrency_test_names})
endforeach()
# build integration tests
message(STATUS ${memgraph_src_files})
# message(STATUS ${memgraph_src_files})
add_executable(integration_queries integration/queries.cpp ${memgraph_src_files})
target_link_libraries(integration_queries ${fmt_static_lib})
add_test(NAME integration_queries COMMAND integration_queries)

View File

@ -0,0 +1 @@
CREATE (n {id: 2322, name: "TEST", country: "Croatia", created_at: 2352352}) RETURN n

View File

@ -0,0 +1 @@
CREATE (n:ACCOUNT {id: 2322, name: "TEST", country: "Croatia", created_at: 2352352}) RETURN n

View File

@ -0,0 +1 @@
MATCH (n {id: 0}) RETURN n

View File

@ -0,0 +1 @@
MATCH (a {id:0}), (p {id: 1}) CREATE (a)-[r:IS]->(p) RETURN r

View File

@ -0,0 +1 @@
MATCH (n {id: 0}) SET n.name = "TEST100" RETURN n

View File

@ -0,0 +1 @@
MATCH (n:LABEL) RETURN n

View File

@ -0,0 +1 @@
MATCH (a {id:0}), (p {id: 1}) CREATE (a)-[r:IS]->(p)

View File

@ -0,0 +1 @@
MATCH (n:Data) WHERE n.queryID = "KMeasyKMID" OR n.queryID = "easyQueryID" RETURN n

View File

@ -0,0 +1 @@
MATCH (n:Data) WHERE n.queryID = "KMeasyKMID" OR n.queryID = "easyQueryID" WITH n MATCH (n)-[r:Fact]->(m) WHERE r.relationshipType = 'recommended exercise' WITH n,r,m WHERE n.value = 'G' AND m.value = 'HK.03 - Aqua Walking' return n, r, m

View File

@ -0,0 +1 @@
# MATCH (n:Data) WHERE n.queryID IN ["KMeasyKMID", 'easyQueryID'] WITH n MATCH n-[r:Fact]->(m) WHERE r.relationshipType = 'recommended exercise' WITH n,r,m WHERE n.value = 'G' AND m.value = 'HK.03 - Aqua Walking' return n, r, m

View File

@ -60,6 +60,7 @@ int main()
cout << "Query is commented out: " << query << endl;
continue;
}
cout << "QUERY IS: " << query << endl;
auto print_visitor = new PrintVisitor(cout);
cypher::Compiler compiler;
auto tree = compiler.syntax_tree(query);