2017-03-21 17:30:14 +08:00
|
|
|
#include <list>
|
|
|
|
#include <typeinfo>
|
|
|
|
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#include "query/frontend/ast/ast.hpp"
|
|
|
|
#include "query/frontend/logical/operator.hpp"
|
|
|
|
#include "query/frontend/logical/planner.hpp"
|
|
|
|
#include "query/frontend/semantic/symbol_table.hpp"
|
|
|
|
#include "query/frontend/semantic/symbol_generator.hpp"
|
|
|
|
|
2017-03-24 23:50:42 +08:00
|
|
|
#include "query_common.hpp"
|
|
|
|
|
2017-03-27 19:09:14 +08:00
|
|
|
using namespace query::plan;
|
|
|
|
using query::AstTreeStorage;
|
|
|
|
using query::SymbolTable;
|
|
|
|
using query::SymbolGenerator;
|
|
|
|
using Direction = query::EdgeAtom::Direction;
|
2017-03-21 17:30:14 +08:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PlanChecker : public LogicalOperatorVisitor {
|
|
|
|
public:
|
|
|
|
using LogicalOperatorVisitor::Visit;
|
|
|
|
using LogicalOperatorVisitor::PostVisit;
|
|
|
|
|
|
|
|
PlanChecker(std::list<size_t> types) : types_(types) {}
|
|
|
|
|
2017-03-24 19:59:23 +08:00
|
|
|
void Visit(CreateNode &op) override { AssertType(op); }
|
2017-03-22 20:09:38 +08:00
|
|
|
void Visit(CreateExpand &op) override { AssertType(op); }
|
2017-03-27 20:23:31 +08:00
|
|
|
void Visit(Delete &op) override { AssertType(op); }
|
2017-03-21 17:30:14 +08:00
|
|
|
void Visit(ScanAll &op) override { AssertType(op); }
|
|
|
|
void Visit(Expand &op) override { AssertType(op); }
|
|
|
|
void Visit(NodeFilter &op) override { AssertType(op); }
|
|
|
|
void Visit(EdgeFilter &op) override { AssertType(op); }
|
2017-03-27 18:10:50 +08:00
|
|
|
void Visit(Filter &op) override { AssertType(op); }
|
2017-03-21 17:30:14 +08:00
|
|
|
void Visit(Produce &op) override { AssertType(op); }
|
2017-03-28 17:04:28 +08:00
|
|
|
void Visit(SetProperty &op) override { AssertType(op); }
|
|
|
|
void Visit(SetProperties &op) override { AssertType(op); }
|
|
|
|
void Visit(SetLabels &op) override { AssertType(op); }
|
2017-03-30 14:44:56 +08:00
|
|
|
void Visit(RemoveProperty &op) override { AssertType(op); }
|
|
|
|
void Visit(RemoveLabels &op) override { AssertType(op); }
|
2017-03-21 17:30:14 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
void AssertType(const LogicalOperator &op) {
|
|
|
|
ASSERT_FALSE(types_.empty());
|
|
|
|
ASSERT_EQ(types_.back(), typeid(op).hash_code());
|
|
|
|
types_.pop_back();
|
|
|
|
}
|
|
|
|
std::list<size_t> types_;
|
|
|
|
};
|
|
|
|
|
2017-03-27 18:10:50 +08:00
|
|
|
auto CheckPlan(query::Query &query, std::list<size_t> expected_types) {
|
2017-03-21 17:30:14 +08:00
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
2017-03-27 18:10:50 +08:00
|
|
|
query.Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(query, symbol_table);
|
2017-03-21 17:30:14 +08:00
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:10:50 +08:00
|
|
|
TEST(TestLogicalPlanner, MatchNodeReturn) {
|
|
|
|
// Test MATCH (n) RETURN n AS n
|
|
|
|
AstTreeStorage storage;
|
Make Where and NamedExpression macros easier to use
Summary:
`Where` can now be constructed in a `QUERY`, instead of requiring manual
addition to `Match`. For example:
auto query = QUERY(MATCH(pattern), WHERE(expr), ...);
compared to:
auto match = MATCH(pattern);
match->where_ = WHERE(expr);
auto query = QUERY(match, ...);
Similarly, `AS` can be used instead of `NEXPR` to create
`NamedExpressions` only with a name. This is meant to be used with
`RETURN` which will look at the previous `Expression` and store it
inside `NamedExpression`. For example:
auto ret = RETURN(IDENT("n"), AS("n"),
PROPERTY_LOOKUP("n", prop), AS("prop_val"));
compared to:
auto ret = RETURN(NEXPR("n", IDENT("n")),
NEXPR("prop_val", PROPERTY_LOOKUP("n", prop)));
Reviewers: florijan, mislav.bradac
Reviewed By: florijan
Subscribers: pullbot
Differential Revision: https://phabricator.memgraph.io/D195
2017-03-28 21:39:18 +08:00
|
|
|
auto query = QUERY(MATCH(PATTERN(NODE("n"))), RETURN(IDENT("n"), AS("n")));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query, {typeid(ScanAll).hash_code(), typeid(Produce).hash_code()});
|
|
|
|
}
|
|
|
|
|
2017-03-21 17:30:14 +08:00
|
|
|
TEST(TestLogicalPlanner, CreateNodeReturn) {
|
|
|
|
// Test CREATE (n) RETURN n AS n
|
|
|
|
AstTreeStorage storage;
|
Make Where and NamedExpression macros easier to use
Summary:
`Where` can now be constructed in a `QUERY`, instead of requiring manual
addition to `Match`. For example:
auto query = QUERY(MATCH(pattern), WHERE(expr), ...);
compared to:
auto match = MATCH(pattern);
match->where_ = WHERE(expr);
auto query = QUERY(match, ...);
Similarly, `AS` can be used instead of `NEXPR` to create
`NamedExpressions` only with a name. This is meant to be used with
`RETURN` which will look at the previous `Expression` and store it
inside `NamedExpression`. For example:
auto ret = RETURN(IDENT("n"), AS("n"),
PROPERTY_LOOKUP("n", prop), AS("prop_val"));
compared to:
auto ret = RETURN(NEXPR("n", IDENT("n")),
NEXPR("prop_val", PROPERTY_LOOKUP("n", prop)));
Reviewers: florijan, mislav.bradac
Reviewed By: florijan
Subscribers: pullbot
Differential Revision: https://phabricator.memgraph.io/D195
2017-03-28 21:39:18 +08:00
|
|
|
auto query = QUERY(CREATE(PATTERN(NODE("n"))), RETURN(IDENT("n"), AS("n")));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(CreateNode).hash_code(), typeid(Produce).hash_code()});
|
2017-03-21 17:30:14 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 20:09:38 +08:00
|
|
|
TEST(TestLogicalPlanner, CreateExpand) {
|
|
|
|
// Test CREATE (n) -[r :rel1]-> (m)
|
|
|
|
AstTreeStorage storage;
|
|
|
|
std::string relationship("relationship");
|
2017-03-24 23:50:42 +08:00
|
|
|
auto query = QUERY(CREATE(PATTERN(
|
|
|
|
NODE("n"), EDGE("r", &relationship, Direction::RIGHT), NODE("m"))));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(CreateNode).hash_code(), typeid(CreateExpand).hash_code()});
|
2017-03-22 20:09:38 +08:00
|
|
|
}
|
|
|
|
|
2017-03-24 23:50:42 +08:00
|
|
|
TEST(TestLogicalPlanner, CreateMultipleNode) {
|
|
|
|
// Test CREATE (n), (m)
|
|
|
|
AstTreeStorage storage;
|
|
|
|
auto query = QUERY(CREATE(PATTERN(NODE("n")), PATTERN(NODE("m"))));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(CreateNode).hash_code(), typeid(CreateNode).hash_code()});
|
2017-03-24 23:50:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestLogicalPlanner, CreateNodeExpandNode) {
|
|
|
|
// Test CREATE (n) -[r :rel]-> (m), (l)
|
|
|
|
AstTreeStorage storage;
|
|
|
|
std::string relationship("rel");
|
|
|
|
auto query = QUERY(CREATE(
|
|
|
|
PATTERN(NODE("n"), EDGE("r", &relationship, Direction::RIGHT), NODE("m")),
|
|
|
|
PATTERN(NODE("l"))));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(CreateNode).hash_code(), typeid(CreateExpand).hash_code(),
|
|
|
|
typeid(CreateNode).hash_code()});
|
2017-03-24 23:50:42 +08:00
|
|
|
}
|
|
|
|
|
2017-03-22 20:09:38 +08:00
|
|
|
TEST(TestLogicalPlanner, MatchCreateExpand) {
|
|
|
|
// Test MATCH (n) CREATE (n) -[r :rel1]-> (m)
|
|
|
|
AstTreeStorage storage;
|
|
|
|
std::string relationship("relationship");
|
2017-03-24 23:50:42 +08:00
|
|
|
auto query = QUERY(
|
|
|
|
MATCH(PATTERN(NODE("n"))),
|
|
|
|
CREATE(PATTERN(NODE("n"), EDGE("r", &relationship, Direction::RIGHT),
|
|
|
|
NODE("m"))));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(ScanAll).hash_code(), typeid(CreateExpand).hash_code()});
|
2017-03-22 20:09:38 +08:00
|
|
|
}
|
|
|
|
|
2017-03-21 17:30:14 +08:00
|
|
|
TEST(TestLogicalPlanner, MatchLabeledNodes) {
|
|
|
|
// Test MATCH (n :label) RETURN n AS n
|
|
|
|
AstTreeStorage storage;
|
|
|
|
std::string label("label");
|
2017-03-24 23:50:42 +08:00
|
|
|
auto query =
|
Make Where and NamedExpression macros easier to use
Summary:
`Where` can now be constructed in a `QUERY`, instead of requiring manual
addition to `Match`. For example:
auto query = QUERY(MATCH(pattern), WHERE(expr), ...);
compared to:
auto match = MATCH(pattern);
match->where_ = WHERE(expr);
auto query = QUERY(match, ...);
Similarly, `AS` can be used instead of `NEXPR` to create
`NamedExpressions` only with a name. This is meant to be used with
`RETURN` which will look at the previous `Expression` and store it
inside `NamedExpression`. For example:
auto ret = RETURN(IDENT("n"), AS("n"),
PROPERTY_LOOKUP("n", prop), AS("prop_val"));
compared to:
auto ret = RETURN(NEXPR("n", IDENT("n")),
NEXPR("prop_val", PROPERTY_LOOKUP("n", prop)));
Reviewers: florijan, mislav.bradac
Reviewed By: florijan
Subscribers: pullbot
Differential Revision: https://phabricator.memgraph.io/D195
2017-03-28 21:39:18 +08:00
|
|
|
QUERY(MATCH(PATTERN(NODE("n", &label))), RETURN(IDENT("n"), AS("n")));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(ScanAll).hash_code(), typeid(NodeFilter).hash_code(),
|
|
|
|
typeid(Produce).hash_code()});
|
2017-03-21 17:30:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestLogicalPlanner, MatchPathReturn) {
|
|
|
|
// Test MATCH (n) -[r :relationship]- (m) RETURN n AS n
|
|
|
|
AstTreeStorage storage;
|
|
|
|
std::string relationship("relationship");
|
2017-03-24 23:50:42 +08:00
|
|
|
auto query =
|
|
|
|
QUERY(MATCH(PATTERN(NODE("n"), EDGE("r", &relationship), NODE("m"))),
|
Make Where and NamedExpression macros easier to use
Summary:
`Where` can now be constructed in a `QUERY`, instead of requiring manual
addition to `Match`. For example:
auto query = QUERY(MATCH(pattern), WHERE(expr), ...);
compared to:
auto match = MATCH(pattern);
match->where_ = WHERE(expr);
auto query = QUERY(match, ...);
Similarly, `AS` can be used instead of `NEXPR` to create
`NamedExpressions` only with a name. This is meant to be used with
`RETURN` which will look at the previous `Expression` and store it
inside `NamedExpression`. For example:
auto ret = RETURN(IDENT("n"), AS("n"),
PROPERTY_LOOKUP("n", prop), AS("prop_val"));
compared to:
auto ret = RETURN(NEXPR("n", IDENT("n")),
NEXPR("prop_val", PROPERTY_LOOKUP("n", prop)));
Reviewers: florijan, mislav.bradac
Reviewed By: florijan
Subscribers: pullbot
Differential Revision: https://phabricator.memgraph.io/D195
2017-03-28 21:39:18 +08:00
|
|
|
RETURN(IDENT("n"), AS("n")));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(ScanAll).hash_code(), typeid(Expand).hash_code(),
|
|
|
|
typeid(EdgeFilter).hash_code(), typeid(Produce).hash_code()});
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestLogicalPlanner, MatchWhereReturn) {
|
|
|
|
// Test MATCH (n) WHERE n.property < 42 RETURN n AS n
|
|
|
|
AstTreeStorage storage;
|
|
|
|
std::string property("property");
|
Make Where and NamedExpression macros easier to use
Summary:
`Where` can now be constructed in a `QUERY`, instead of requiring manual
addition to `Match`. For example:
auto query = QUERY(MATCH(pattern), WHERE(expr), ...);
compared to:
auto match = MATCH(pattern);
match->where_ = WHERE(expr);
auto query = QUERY(match, ...);
Similarly, `AS` can be used instead of `NEXPR` to create
`NamedExpressions` only with a name. This is meant to be used with
`RETURN` which will look at the previous `Expression` and store it
inside `NamedExpression`. For example:
auto ret = RETURN(IDENT("n"), AS("n"),
PROPERTY_LOOKUP("n", prop), AS("prop_val"));
compared to:
auto ret = RETURN(NEXPR("n", IDENT("n")),
NEXPR("prop_val", PROPERTY_LOOKUP("n", prop)));
Reviewers: florijan, mislav.bradac
Reviewed By: florijan
Subscribers: pullbot
Differential Revision: https://phabricator.memgraph.io/D195
2017-03-28 21:39:18 +08:00
|
|
|
auto query = QUERY(MATCH(PATTERN(NODE("n"))),
|
|
|
|
WHERE(LESS(PROPERTY_LOOKUP("n", &property), LITERAL(42))),
|
|
|
|
RETURN(IDENT("n"), AS("n")));
|
2017-03-27 18:10:50 +08:00
|
|
|
CheckPlan(*query, {typeid(ScanAll).hash_code(), typeid(Filter).hash_code(),
|
|
|
|
typeid(Produce).hash_code()});
|
2017-03-21 17:30:14 +08:00
|
|
|
}
|
|
|
|
|
2017-03-27 20:23:31 +08:00
|
|
|
TEST(TestLogicalPlanner, MatchDelete) {
|
|
|
|
// Test MATCH (n) DELETE n
|
|
|
|
AstTreeStorage storage;
|
|
|
|
auto query = QUERY(MATCH(PATTERN(NODE("n"))), DELETE(IDENT("n")));
|
|
|
|
CheckPlan(*query, {typeid(ScanAll).hash_code(), typeid(Delete).hash_code()});
|
|
|
|
}
|
|
|
|
|
2017-03-28 17:04:28 +08:00
|
|
|
TEST(TestLogicalPlanner, MatchNodeSet) {
|
|
|
|
// Test MATCH (n) SET n.prop = 42, n = n, n :label
|
|
|
|
AstTreeStorage storage;
|
|
|
|
std::string prop("prop");
|
|
|
|
std::string label("label");
|
|
|
|
auto query = QUERY(MATCH(PATTERN(NODE("n"))),
|
|
|
|
SET(PROPERTY_LOOKUP("n", &prop), LITERAL(42)),
|
|
|
|
SET("n", IDENT("n")), SET("n", {&label}));
|
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(ScanAll).hash_code(), typeid(SetProperty).hash_code(),
|
|
|
|
typeid(SetProperties).hash_code(), typeid(SetLabels).hash_code()});
|
|
|
|
}
|
|
|
|
|
2017-03-30 14:44:56 +08:00
|
|
|
TEST(TestLogicalPlanner, MatchRemove) {
|
|
|
|
// Test MATCH (n) REMOVE n.prop REMOVE n :label
|
|
|
|
AstTreeStorage storage;
|
|
|
|
std::string prop("prop");
|
|
|
|
std::string label("label");
|
|
|
|
auto query =
|
|
|
|
QUERY(MATCH(PATTERN(NODE("n"))), REMOVE(PROPERTY_LOOKUP("n", &prop)),
|
|
|
|
REMOVE("n", {&label}));
|
|
|
|
CheckPlan(*query,
|
|
|
|
{typeid(ScanAll).hash_code(), typeid(RemoveProperty).hash_code(),
|
|
|
|
typeid(RemoveLabels).hash_code()});
|
|
|
|
}
|
|
|
|
|
2017-03-21 17:30:14 +08:00
|
|
|
}
|