EdgeAccessor::is_cycle added. Query::Plan::Expand fixed

Reviewers: mislav.bradac

Reviewed By: mislav.bradac

Differential Revision: https://phabricator.memgraph.io/D262
This commit is contained in:
florijan 2017-04-11 15:44:44 +02:00
parent 593e4e72b9
commit 1273cea870
5 changed files with 44 additions and 1 deletions

View File

@ -242,6 +242,12 @@ bool Expand::ExpandCursor::Pull(Frame &frame, const SymbolTable &symbol_table) {
// attempt to get a value from the outgoing edges
if (out_edges_ && *out_edges_it_ != out_edges_->end()) {
EdgeAccessor edge = *(*out_edges_it_)++;
// when expanding in EdgeAtom::Direction::BOTH directions
// we should do only one expansion for cycles, and it was
// already done in the block above
if (self_.edge_atom_->direction_ == EdgeAtom::Direction::BOTH &&
edge.is_cycle())
continue;
if (HandleEdgeCycle(edge, frame, symbol_table) &&
PullNode(edge, EdgeAtom::Direction::RIGHT, frame, symbol_table))
return true;

View File

@ -15,6 +15,9 @@ VertexAccessor EdgeAccessor::from() const {
VertexAccessor EdgeAccessor::to() const {
return VertexAccessor(current().to_, db_accessor());
}
bool EdgeAccessor::is_cycle() const {
return &current().to_ == &current().from_;
}
std::ostream &operator<<(std::ostream &os, const EdgeAccessor &ea) {
os << "E[" << ea.db_accessor().edge_type_name(ea.edge_type());

View File

@ -38,7 +38,9 @@ class EdgeAccessor : public RecordAccessor<Edge> {
*/
VertexAccessor to() const;
// void remove();
/** Returns true if this edge is a cycle (start and end node are
* the same. */
bool is_cycle() const;
};
std::ostream &operator<<(std::ostream &, const EdgeAccessor &);

View File

@ -314,6 +314,25 @@ TEST(QueryPlan, ExpandEdgeCycle) {
test_cycle(false, 6);
}
TEST(QueryPlan, ExpandBothCycleEdgeCase) {
// we're testing that expanding on BOTH
// does only one expansion for a cycle
Dbms dbms;
auto dba = dbms.active();
auto v = dba->insert_vertex();
dba->insert_edge(v, v, dba->edge_type("et"));
dba->advance_command();
AstTreeStorage storage;
SymbolTable symbol_table;
auto n = MakeScanAll(storage, symbol_table, "n");
auto r_ = MakeExpand(storage, symbol_table, n.op_, n.sym_, "r",
EdgeAtom::Direction::BOTH, false, "_", false);
EXPECT_EQ(1, PullAll(r_.op_, *dba, symbol_table));
}
TEST(QueryPlan, EdgeFilter) {
Dbms dbms;
auto dba = dbms.active();

View File

@ -202,6 +202,19 @@ TEST(RecordAccessor, EdgeType) {
EXPECT_NE(edge.edge_type(), hates);
}
TEST(RecordAccessor, EdgeIsCycle) {
Dbms dbms;
auto dba = dbms.active();
auto v1 = dba->insert_vertex();
auto v2 = dba->insert_vertex();
auto likes = dba->edge_type("edge_type");
EXPECT_TRUE(dba->insert_edge(v1, v1, likes).is_cycle());
EXPECT_TRUE(dba->insert_edge(v2, v2, likes).is_cycle());
EXPECT_FALSE(dba->insert_edge(v1, v2, likes).is_cycle());
EXPECT_FALSE(dba->insert_edge(v2, v1, likes).is_cycle());
}
TEST(RecordAccessor, VertexEdgeConnections) {
Dbms dbms;
auto dba = dbms.active();