#include #include #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" using namespace query; namespace { class PlanChecker : public LogicalOperatorVisitor { public: using LogicalOperatorVisitor::Visit; using LogicalOperatorVisitor::PostVisit; PlanChecker(std::list types) : types_(types) {} void Visit(CreateOp &op) override { AssertType(op); } void Visit(CreateExpand &op) override { AssertType(op); } 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 types_; }; // Returns a `(name1) -[name2]- (name3) ...` pattern. auto GetPattern(AstTreeStorage &storage, std::vector names) { bool is_node{true}; auto pattern = storage.Create(); for (auto &name : names) { PatternAtom *atom; auto identifier = storage.Create(name); if (is_node) { atom = storage.Create(identifier); } else { atom = storage.Create(identifier); } pattern->atoms_.emplace_back(atom); is_node = !is_node; } return pattern; } TEST(TestLogicalPlanner, MatchNodeReturn) { // Test MATCH (n) RETURN n AS n AstTreeStorage storage; auto match = storage.Create(); match->patterns_.emplace_back(GetPattern(storage, {"n"})); auto query = storage.query(); query->clauses_.emplace_back(match); auto named_expr = storage.Create(); named_expr->name_ = "n"; named_expr->expression_ = storage.Create("n"); auto ret = storage.Create(); ret->named_expressions_.emplace_back(named_expr); query->clauses_.emplace_back(ret); SymbolTable symbol_table; SymbolGenerator symbol_generator(symbol_table); query->Accept(symbol_generator); auto plan = MakeLogicalPlan(*query, symbol_table); std::list 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; auto create = storage.Create(); create->patterns_.emplace_back(GetPattern(storage, {"n"})); auto query = storage.query(); query->clauses_.emplace_back(create); auto named_expr = storage.Create(); named_expr->name_ = "n"; named_expr->expression_ = storage.Create("n"); auto ret = storage.Create(); ret->named_expressions_.emplace_back(named_expr); query->clauses_.emplace_back(ret); SymbolTable symbol_table; SymbolGenerator symbol_generator(symbol_table); query->Accept(symbol_generator); auto plan = MakeLogicalPlan(*query, symbol_table); std::list expected_types; expected_types.emplace_back(typeid(CreateOp).hash_code()); expected_types.emplace_back(typeid(Produce).hash_code()); PlanChecker plan_checker(expected_types); plan->Accept(plan_checker); } TEST(TestLogicalPlanner, CreateExpand) { // Test CREATE (n) -[r :rel1]-> (m) AstTreeStorage storage; auto create = storage.Create(); auto pattern = GetPattern(storage, {"n", "r", "m"}); create->patterns_.emplace_back(pattern); auto edge_atom = dynamic_cast(pattern->atoms_[1]); edge_atom->direction_ = EdgeAtom::Direction::RIGHT; std::string relationship("relationship"); edge_atom->edge_types_.emplace_back(&relationship); auto query = storage.query(); query->clauses_.emplace_back(create); SymbolTable symbol_table; SymbolGenerator symbol_generator(symbol_table); query->Accept(symbol_generator); auto plan = MakeLogicalPlan(*query, symbol_table); std::list expected_types; expected_types.emplace_back(typeid(CreateOp).hash_code()); expected_types.emplace_back(typeid(CreateExpand).hash_code()); PlanChecker plan_checker(expected_types); plan->Accept(plan_checker); } TEST(TestLogicalPlanner, MatchCreateExpand) { // Test MATCH (n) CREATE (n) -[r :rel1]-> (m) AstTreeStorage storage; auto match = storage.Create(); match->patterns_.emplace_back(GetPattern(storage, {"n"})); auto query = storage.query(); query->clauses_.emplace_back(match); auto create = storage.Create(); auto pattern = GetPattern(storage, {"n", "r", "m"}); create->patterns_.emplace_back(pattern); auto edge_atom = dynamic_cast(pattern->atoms_[1]); edge_atom->direction_ = EdgeAtom::Direction::RIGHT; std::string relationship("relationship"); edge_atom->edge_types_.emplace_back(&relationship); query->clauses_.emplace_back(create); SymbolTable symbol_table; SymbolGenerator symbol_generator(symbol_table); query->Accept(symbol_generator); auto plan = MakeLogicalPlan(*query, symbol_table); std::list 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); } TEST(TestLogicalPlanner, MatchLabeledNodes) { // Test MATCH (n :label) RETURN n AS n AstTreeStorage storage; auto pattern = storage.Create(); auto node_atom = storage.Create(storage.Create("n")); std::string label("label"); node_atom->labels_.emplace_back(&label); pattern->atoms_.emplace_back(node_atom); auto match = storage.Create(); match->patterns_.emplace_back(pattern); auto query = storage.query(); query->clauses_.emplace_back(match); auto named_expr = storage.Create(); named_expr->name_ = "n"; named_expr->expression_ = storage.Create("n"); auto ret = storage.Create(); ret->named_expressions_.emplace_back(named_expr); query->clauses_.emplace_back(ret); SymbolTable symbol_table; SymbolGenerator symbol_generator(symbol_table); query->Accept(symbol_generator); auto plan = MakeLogicalPlan(*query, symbol_table); std::list 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; auto match = storage.Create(); auto pattern = GetPattern(storage, {"n", "r", "m"}); match->patterns_.emplace_back(pattern); auto edge_atom = dynamic_cast(pattern->atoms_[1]); std::string relationship("relationship"); edge_atom->edge_types_.emplace_back(&relationship); auto query = storage.query(); query->clauses_.emplace_back(match); auto named_expr = storage.Create(); named_expr->name_ = "n"; named_expr->expression_ = storage.Create("n"); auto ret = storage.Create(); ret->named_expressions_.emplace_back(named_expr); query->clauses_.emplace_back(ret); SymbolTable symbol_table; SymbolGenerator symbol_generator(symbol_table); query->Accept(symbol_generator); auto plan = MakeLogicalPlan(*query, symbol_table); std::list 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); } }