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-21 17:30:14 +08:00
|
|
|
using namespace query;
|
2017-03-24 23:50:42 +08:00
|
|
|
using Direction = 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-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); }
|
|
|
|
void Visit(Produce &op) override { AssertType(op); }
|
|
|
|
|
|
|
|
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_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(TestLogicalPlanner, MatchNodeReturn) {
|
|
|
|
// Test MATCH (n) RETURN n AS n
|
|
|
|
AstTreeStorage storage;
|
2017-03-24 23:50:42 +08:00
|
|
|
auto query = QUERY(MATCH(PATTERN(NODE("n"))), RETURN(NEXPR("n", IDENT("n"))));
|
2017-03-21 17:30:14 +08:00
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
query->Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(*query, symbol_table);
|
|
|
|
std::list<size_t> expected_types;
|
|
|
|
expected_types.emplace_back(typeid(ScanAll).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(Produce).hash_code());
|
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(TestLogicalPlanner, CreateNodeReturn) {
|
|
|
|
// Test CREATE (n) RETURN n AS n
|
|
|
|
AstTreeStorage storage;
|
2017-03-24 23:50:42 +08:00
|
|
|
auto query =
|
|
|
|
QUERY(CREATE(PATTERN(NODE("n"))), RETURN(NEXPR("n", IDENT("n"))));
|
2017-03-21 17:30:14 +08:00
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
query->Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(*query, symbol_table);
|
|
|
|
std::list<size_t> expected_types;
|
2017-03-24 19:59:23 +08:00
|
|
|
expected_types.emplace_back(typeid(CreateNode).hash_code());
|
2017-03-21 17:30:14 +08:00
|
|
|
expected_types.emplace_back(typeid(Produce).hash_code());
|
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
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-22 20:09:38 +08:00
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
query->Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(*query, symbol_table);
|
|
|
|
std::list<size_t> expected_types;
|
2017-03-24 19:59:23 +08:00
|
|
|
expected_types.emplace_back(typeid(CreateNode).hash_code());
|
2017-03-22 20:09:38 +08:00
|
|
|
expected_types.emplace_back(typeid(CreateExpand).hash_code());
|
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
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"))));
|
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
query->Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(*query, symbol_table);
|
|
|
|
std::list<size_t> expected_types;
|
|
|
|
expected_types.emplace_back(typeid(CreateNode).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(CreateNode).hash_code());
|
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
|
|
|
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"))));
|
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
query->Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(*query, symbol_table);
|
|
|
|
std::list<size_t> expected_types;
|
|
|
|
expected_types.emplace_back(typeid(CreateNode).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(CreateExpand).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(CreateNode).hash_code());
|
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
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-22 20:09:38 +08:00
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
query->Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(*query, symbol_table);
|
|
|
|
std::list<size_t> expected_types;
|
|
|
|
expected_types.emplace_back(typeid(ScanAll).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(CreateExpand).hash_code());
|
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
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 =
|
|
|
|
QUERY(MATCH(PATTERN(NODE("n", &label))), RETURN(NEXPR("n", IDENT("n"))));
|
2017-03-21 17:30:14 +08:00
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
query->Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(*query, symbol_table);
|
|
|
|
std::list<size_t> expected_types;
|
|
|
|
expected_types.emplace_back(typeid(ScanAll).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(NodeFilter).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(Produce).hash_code());
|
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
|
|
|
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"))),
|
|
|
|
RETURN(NEXPR("n", IDENT("n"))));
|
2017-03-21 17:30:14 +08:00
|
|
|
SymbolTable symbol_table;
|
|
|
|
SymbolGenerator symbol_generator(symbol_table);
|
|
|
|
query->Accept(symbol_generator);
|
|
|
|
auto plan = MakeLogicalPlan(*query, symbol_table);
|
|
|
|
std::list<size_t> expected_types;
|
|
|
|
expected_types.emplace_back(typeid(ScanAll).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(Expand).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(EdgeFilter).hash_code());
|
|
|
|
expected_types.emplace_back(typeid(Produce).hash_code());
|
|
|
|
PlanChecker plan_checker(expected_types);
|
|
|
|
plan->Accept(plan_checker);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|