Skip to content

Commit 747e829

Browse files
authored
Cherry-pick: Fix apoc.meta.schema adding incoming rels to outgoing list (#824)
1 parent 7fa66de commit 747e829

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

core/src/main/java/apoc/meta/Meta.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -893,8 +893,14 @@ private void setIncomingRelationships(
893893
(Map<String, Object>) relationshipNameToRelationshipMap.get(relationshipName);
894894
Map<String, Object> existingRel =
895895
(Map<String, Object>) actualRelationshipsList.get(relationshipName);
896-
List<String> labels = (List<String>) existingRel.get("labels");
897-
labels.addAll((List<String>) relToAdd.get("labels"));
896+
// If the map is showing outgoing rels, do not add incoming ones. The incoming ones for
897+
// that node are meant to be inferred by the user (this is a side effect of having the
898+
// type name as the key, it only gives one option of direction)
899+
if (relToAdd.getOrDefault("direction", "")
900+
.equals(existingRel.getOrDefault("direction", ""))) {
901+
List<String> labels = (List<String>) existingRel.get("labels");
902+
labels.addAll((List<String>) relToAdd.get("labels"));
903+
}
898904
} else
899905
actualRelationshipsList.put(
900906
relationshipName, relationshipNameToRelationshipMap.get(relationshipName));

core/src/test/java/apoc/meta/MetaTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,58 @@ private void assertRowMetaData(Map<String, Object> row, long count, long left, l
592592
assertEquals(type.name(), row.get("type"));
593593
}
594594

595+
@Test
596+
public void testOutgoingRelsAreCorrectMetaSchema() {
597+
db.executeTransactionally("CREATE (:A)-[:HAS]->(:B), (:A)<-[:HAS]-(:B), (:A)<-[:HAS]-(:C), (:D)<-[:HAS]-(:E)");
598+
599+
testCall(db, "CALL apoc.meta.schema", (row) -> {
600+
Map<String, Object> o = (Map<String, Object>) row.get("value");
601+
// HAS: count 3, no props, type relationship
602+
// A: count 3. no props, relationship out as B
603+
// B: count 2. no props, relationship out as A
604+
// C: count 1. no props, relationship out as A
605+
// D: count 1. no props, relationship in as E
606+
// E: count 1. no props, relationship out as D
607+
Map<String, Object> relHas = (Map<String, Object>) o.get("HAS");
608+
Map<String, Object> nodeA = (Map<String, Object>) o.get("A");
609+
Map<String, Object> nodeB = (Map<String, Object>) o.get("B");
610+
Map<String, Object> nodeC = (Map<String, Object>) o.get("C");
611+
Map<String, Object> nodeD = (Map<String, Object>) o.get("D");
612+
Map<String, Object> nodeE = (Map<String, Object>) o.get("E");
613+
// Check counts
614+
assertEquals(4L, relHas.get("count"));
615+
assertEquals(3L, nodeA.get("count"));
616+
assertEquals(2L, nodeB.get("count"));
617+
assertEquals(1L, nodeC.get("count"));
618+
assertEquals(1L, nodeD.get("count"));
619+
assertEquals(1L, nodeE.get("count"));
620+
621+
Map<String, Object> nodeARels =
622+
(Map<String, Object>) ((Map<String, Object>) nodeA.get("relationships")).get("HAS");
623+
Map<String, Object> nodeBRels =
624+
(Map<String, Object>) ((Map<String, Object>) nodeB.get("relationships")).get("HAS");
625+
Map<String, Object> nodeCRels =
626+
(Map<String, Object>) ((Map<String, Object>) nodeC.get("relationships")).get("HAS");
627+
Map<String, Object> nodeDRels =
628+
(Map<String, Object>) ((Map<String, Object>) nodeD.get("relationships")).get("HAS");
629+
Map<String, Object> nodeERels =
630+
(Map<String, Object>) ((Map<String, Object>) nodeE.get("relationships")).get("HAS");
631+
// Check directions
632+
assertEquals("out", nodeARels.get("direction"));
633+
assertEquals("out", nodeBRels.get("direction"));
634+
assertEquals("out", nodeCRels.get("direction"));
635+
assertEquals("in", nodeDRels.get("direction"));
636+
assertEquals("out", nodeERels.get("direction"));
637+
638+
// Check end nodes
639+
assertEquals(List.of("B"), nodeARels.get("labels"));
640+
assertEquals(List.of("A"), nodeBRels.get("labels"));
641+
assertEquals(List.of("A"), nodeCRels.get("labels"));
642+
assertEquals(List.of("E"), nodeDRels.get("labels"));
643+
assertEquals(List.of("D"), nodeERels.get("labels"));
644+
});
645+
}
646+
595647
@Test
596648
public void testMetaSchema() {
597649
db.executeTransactionally("create index for (n:Movie) on (n.title)");

0 commit comments

Comments
 (0)