Skip to content

Commit 846aa0c

Browse files
knutwalkers1ck
andcommitted
Validate that a relationship property exists when constructing a CSRGraph
Co-Authored-By: Martin Junghanns <[email protected]>
1 parent 792fbfd commit 846aa0c

File tree

2 files changed

+50
-30
lines changed

2 files changed

+50
-30
lines changed

core/src/main/java/org/neo4j/gds/core/loading/CSRGraphStoreUtil.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,38 @@ public static CSRGraphStore createFromGraph(
5656
NamedDatabaseId databaseId,
5757
HugeGraph graph,
5858
String relationshipTypeString,
59-
Optional<String> relationshipProperty,
59+
Optional<String> relationshipPropertyKey,
6060
int concurrency
6161
) {
62-
Relationships relationships = graph.relationships();
63-
6462
var relationshipType = RelationshipType.of(relationshipTypeString);
65-
var topology = Map.of(relationshipType, relationships.topology());
63+
var relationships = graph.relationships();
64+
var relationshipSchemaBuilder = RelationshipSchema.builder().addRelationshipType(relationshipType);
65+
66+
relationshipPropertyKey.ifPresent(property -> {
67+
68+
if (!graph.hasRelationshipProperty()) {
69+
throw new IllegalArgumentException(formatWithLocale(
70+
"Relationship property name '%s' does not exist in the graph.",
71+
property
72+
));
73+
}
6674

75+
relationshipSchemaBuilder.addProperty(
76+
relationshipType,
77+
property,
78+
ValueType.DOUBLE
79+
);
80+
});
81+
82+
var topology = Map.of(relationshipType, relationships.topology());
6783
var nodeProperties = constructNodePropertiesFromGraph(graph);
6884
var relationshipProperties = constructRelationshipPropertiesFromGraph(
6985
graph,
70-
relationshipProperty,
86+
relationshipPropertyKey,
7187
relationships,
7288
relationshipType
7389
);
7490

75-
RelationshipSchema.Builder relationshipSchemaBuilder = RelationshipSchema.builder().addRelationshipType(relationshipType);
76-
relationshipProperty.ifPresent(property -> relationshipSchemaBuilder.addProperty(relationshipType, property, ValueType.DOUBLE));
77-
7891
var schema = GraphSchema.of(graph.schema().nodeSchema(), relationshipSchemaBuilder.build(), Map.of());
7992

8093
return new CSRGraphStore(

core/src/test/java/org/neo4j/gds/core/loading/CSRGraphStoreUtilTest.java

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,41 +22,32 @@
2222
import org.junit.jupiter.api.Test;
2323
import org.neo4j.gds.NodeLabel;
2424
import org.neo4j.gds.RelationshipType;
25-
import org.neo4j.gds.api.Graph;
26-
import org.neo4j.gds.api.GraphStore;
25+
import org.neo4j.gds.TestSupport;
2726
import org.neo4j.gds.core.huge.HugeGraph;
28-
import org.neo4j.gds.extension.GdlExtension;
29-
import org.neo4j.gds.extension.GdlGraph;
30-
import org.neo4j.gds.extension.Inject;
3127
import org.neo4j.kernel.database.DatabaseIdFactory;
3228

3329
import java.util.Optional;
3430
import java.util.UUID;
3531

3632
import static org.assertj.core.api.Assertions.assertThat;
33+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3734
import static org.neo4j.gds.TestSupport.assertGraphEquals;
3835

39-
@GdlExtension
4036
class CSRGraphStoreUtilTest {
4137

42-
@GdlGraph
43-
public static final String GDL =
44-
" CREATE" +
45-
" (a:A { foo: 42, bar: 1337 })" +
46-
", (b:A { foo: 84, bar: 1234 })" +
47-
", (c:B { foo: 23 })" +
48-
// Add one relationship type with a single property
49-
// to ensure that the graph is a HugeGraph.
50-
", (a)-[:REL1 { prop1: 42 }]->(b)";
51-
52-
@Inject
53-
private GraphStore graphStore;
54-
55-
@Inject
56-
private Graph graph;
57-
5838
@Test
5939
void fromGraph() {
40+
String GDL =
41+
" CREATE" +
42+
" (a:A { foo: 42, bar: 1337 })" +
43+
", (b:A { foo: 84, bar: 1234 })" +
44+
", (c:B { foo: 23 })" +
45+
// Add one relationship type with a single property
46+
// to ensure that the graph is a HugeGraph.
47+
", (a)-[:REL1 { prop1: 42 }]->(b)";
48+
49+
var graphStore = TestSupport.graphStoreFromGDL(GDL);
50+
var graph = graphStore.getUnion();
6051
assertThat(graph).isInstanceOf(HugeGraph.class);
6152
assertThat(graph.availableNodeLabels()).contains(NodeLabel.of("A"), NodeLabel.of("B"));
6253
assertThat(graph.availableNodeProperties()).contains("foo", "bar");
@@ -74,4 +65,20 @@ void fromGraph() {
7465
assertGraphEquals(graphStore.getUnion(), convertedGraphStore.getUnion());
7566
}
7667

68+
@Test
69+
void shouldValidateRelationshipPropertyKey() {
70+
var graph = TestSupport.fromGdl("()-[:REL]->()");
71+
72+
assertThatThrownBy(() -> {
73+
CSRGraphStoreUtil.createFromGraph(
74+
DatabaseIdFactory.from("dummy", UUID.fromString("42-42-42-42-42")),
75+
(HugeGraph) graph.innerGraph(),
76+
"REL",
77+
Optional.of("prop1"),
78+
1
79+
);
80+
})
81+
.hasMessage("Relationship property name 'prop1' does not exist in the graph.");
82+
}
83+
7784
}

0 commit comments

Comments
 (0)