Skip to content

Commit 9eed2d8

Browse files
committed
Add validation to ToUndirected to make sure relationship type is not already undirected
1 parent e9ecce9 commit 9eed2d8

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

proc/beta/src/main/java/org/neo4j/gds/beta/undirected/ToUndirectedSpec.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.neo4j.gds.MutateComputationResultConsumer;
2323
import org.neo4j.gds.RelationshipType;
24+
import org.neo4j.gds.api.schema.Direction;
2425
import org.neo4j.gds.config.ElementTypeValidator;
2526
import org.neo4j.gds.core.loading.SingleTypeRelationshipImportResult;
2627
import org.neo4j.gds.executor.AlgorithmSpec;
@@ -36,6 +37,7 @@
3637
import org.neo4j.gds.results.StandardMutateResult;
3738

3839
import java.util.List;
40+
import java.util.Locale;
3941
import java.util.Map;
4042
import java.util.stream.Stream;
4143

@@ -93,6 +95,21 @@ public List<AfterLoadValidation<ToUndirectedConfig>> afterLoadValidations() {
9395
(graphStore, graphProjectConfig, config) -> {
9496
var relationshipType = RelationshipType.of(config.relationshipType());
9597
ElementTypeValidator.validateTypes(graphStore, List.of(relationshipType), "`relationshipType`");
98+
},
99+
(graphStore, graphProjectConfig, config) -> {
100+
var relationshipType = RelationshipType.of(config.relationshipType());
101+
Direction direction = graphStore
102+
.schema()
103+
.relationshipSchema()
104+
.get(relationshipType)
105+
.direction();
106+
if (direction == Direction.UNDIRECTED) {
107+
throw new UnsupportedOperationException(String.format(
108+
Locale.US,
109+
"The specified relationship type `%s` is already undirected.",
110+
relationshipType.name
111+
));
112+
}
96113
}
97114
);
98115
}

proc/beta/src/test/java/org/neo4j/gds/beta/undirected/ToUndirectedProcTest.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class ToUndirectedProcTest extends BaseProcTest {
4646

4747
@Neo4jGraph
4848
public static final String DB = "CREATE " +
49-
"(a:A) " +
49+
" (a:A) " +
5050
",(b:B) " +
5151
",(c:C) " +
5252
",(a)-[:REL {prop1: 1.0}]->(b)" +
@@ -66,6 +66,16 @@ void setup() throws Exception {
6666
.withRelationshipProperty("prop1")
6767
.yields()
6868
);
69+
70+
runQuery(GdsCypher.call("undirected_graph")
71+
.graphProject()
72+
.withNodeLabel("A")
73+
.withNodeLabel("B")
74+
.withNodeLabel("C")
75+
.withRelationshipType("REL", Orientation.UNDIRECTED)
76+
.withRelationshipProperty("prop1")
77+
.yields()
78+
);
6979
}
7080

7181
@Test
@@ -113,6 +123,15 @@ void shouldFailIfRelationshipTypeDoesNotExists() {
113123
assertEquals(expectedMessage, throwable.getMessage());
114124
}
115125

126+
@Test
127+
void shouldFailIfRelationshipTypeIsAlreadyUndirected() {
128+
String query = "CALL gds.beta.graph.relationships.toUndirected('undirected_graph', {relationshipType: 'REL', mutateRelationshipType: 'REL2'})";
129+
130+
Throwable throwable = rootCause(assertThrows(QueryExecutionException.class, () -> runQuery(query)));
131+
assertEquals(UnsupportedOperationException.class, throwable.getClass());
132+
assertEquals("The specified relationship type `REL` is already undirected.", throwable.getMessage());
133+
}
134+
116135
@Test
117136
void memoryEstimation() {
118137
String query = "CALL gds.beta.graph.relationships.toUndirected.estimate('graph', {relationshipType: 'REL', mutateRelationshipType: 'REL2'})";

0 commit comments

Comments
 (0)