Skip to content

Commit 764e31e

Browse files
authored
fix(interactive): Correct Error Handling for Nonexistent Edge Kind in Groot When Writing Edges (#4479)
<!-- Thanks for your contribution! please review https://github.com/alibaba/GraphScope/blob/main/CONTRIBUTING.md before opening an issue. --> ## What do these changes do? As titled. ## Related issue number <!-- Are there any issues opened that will be resolved by merging this change? --> Fixes #4478
1 parent 10ed3b3 commit 764e31e

File tree

2 files changed

+57
-6
lines changed
  • interactive_engine
    • groot-client/src/test/java/com/alibaba/graphscope/groot/sdk
    • groot-module/src/main/java/com/alibaba/graphscope/groot/frontend/write

2 files changed

+57
-6
lines changed

interactive_engine/groot-client/src/test/java/com/alibaba/graphscope/groot/sdk/ClientTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package com.alibaba.graphscope.groot.sdk;
1515

16+
import static org.junit.jupiter.api.Assertions.assertThrows;
17+
1618
import com.alibaba.graphscope.groot.sdk.schema.Edge;
1719
import com.alibaba.graphscope.groot.sdk.schema.Vertex;
1820
import com.alibaba.graphscope.proto.groot.GraphDefPb;
@@ -96,5 +98,22 @@ void testAddData() {
9698
Collections.singletonMap("id", "12345"),
9799
Collections.singletonMap("id", "88888"),
98100
Collections.singletonMap("weight", "20201111")));
101+
102+
// add an edge with invalid relation should throw exception
103+
Exception exception =
104+
assertThrows(
105+
Exception.class,
106+
() -> {
107+
client.addEdge(
108+
new Edge(
109+
"knows",
110+
"person",
111+
"software",
112+
Collections.singletonMap("id", "12345"),
113+
Collections.singletonMap("id", "88888"),
114+
Collections.singletonMap("weight", "20201111")));
115+
});
116+
117+
System.out.println("Exception was thrown as expected: " + exception.getMessage());
99118
}
100119
}

interactive_engine/groot-module/src/main/java/com/alibaba/graphscope/groot/frontend/write/GraphWriter.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import com.alibaba.graphscope.groot.common.config.Configs;
55
import com.alibaba.graphscope.groot.common.exception.InvalidArgumentException;
66
import com.alibaba.graphscope.groot.common.exception.NotFoundException;
7+
import com.alibaba.graphscope.groot.common.exception.TypeNotFoundException;
8+
import com.alibaba.graphscope.groot.common.schema.api.EdgeRelation;
79
import com.alibaba.graphscope.groot.common.schema.api.GraphElement;
810
import com.alibaba.graphscope.groot.common.schema.api.GraphProperty;
911
import com.alibaba.graphscope.groot.common.schema.api.GraphSchema;
12+
import com.alibaba.graphscope.groot.common.schema.impl.DefaultGraphEdge;
1013
import com.alibaba.graphscope.groot.common.schema.wrapper.DataType;
1114
import com.alibaba.graphscope.groot.common.schema.wrapper.EdgeKind;
1215
import com.alibaba.graphscope.groot.common.schema.wrapper.LabelId;
@@ -394,8 +397,9 @@ private long getEdgeInnerId(
394397

395398
private EdgeKind getEdgeKind(GraphSchema schema, DataRecord dataRecord) {
396399
EdgeTarget edgeTarget = dataRecord.getEdgeTarget();
400+
EdgeKind edgeKind;
397401
if (edgeTarget != null) {
398-
return edgeTarget.getEdgeKind();
402+
edgeKind = edgeTarget.getEdgeKind();
399403
} else {
400404
EdgeRecordKey edgeRecordKey = dataRecord.getEdgeRecordKey();
401405
String label = edgeRecordKey.getLabel();
@@ -407,12 +411,40 @@ private EdgeKind getEdgeKind(GraphSchema schema, DataRecord dataRecord) {
407411
GraphElement srcVertexDef = schema.getElement(srcVertexRecordKey.getLabel());
408412
GraphElement dstVertexDef = schema.getElement(dstVertexRecordKey.getLabel());
409413
int labelId = edgeDef.getLabelId();
414+
edgeKind =
415+
EdgeKind.newBuilder()
416+
.setEdgeLabelId(new LabelId(labelId))
417+
.setSrcVertexLabelId(new LabelId(srcVertexDef.getLabelId()))
418+
.setDstVertexLabelId(new LabelId(dstVertexDef.getLabelId()))
419+
.build();
420+
}
410421

411-
return EdgeKind.newBuilder()
412-
.setEdgeLabelId(new LabelId(labelId))
413-
.setSrcVertexLabelId(new LabelId(srcVertexDef.getLabelId()))
414-
.setDstVertexLabelId(new LabelId(dstVertexDef.getLabelId()))
415-
.build();
422+
// make sure the edgeKind exists in the schema
423+
GraphElement edgeDef = schema.getElement(edgeKind.getEdgeLabelId().getId());
424+
boolean edgeKindExists = false;
425+
if (edgeDef instanceof DefaultGraphEdge) {
426+
DefaultGraphEdge defaultGraphEdge = (DefaultGraphEdge) edgeDef;
427+
List<EdgeRelation> relations = defaultGraphEdge.getRelationList();
428+
for (EdgeRelation relation : relations) {
429+
if (relation.getSource().getLabelId() == edgeKind.getSrcVertexLabelId().getId()
430+
&& relation.getTarget().getLabelId()
431+
== edgeKind.getDstVertexLabelId().getId()) {
432+
edgeKindExists = true;
433+
break;
434+
}
435+
}
436+
}
437+
if (!edgeKindExists) {
438+
throw new TypeNotFoundException(
439+
"schema element not found for edgeKind with {edgeLabel="
440+
+ schema.getElement(edgeKind.getEdgeLabelId().getId()).getLabel()
441+
+ ", sourceLabel="
442+
+ schema.getElement(edgeKind.getSrcVertexLabelId().getId()).getLabel()
443+
+ ", targetLabel="
444+
+ schema.getElement(edgeKind.getDstVertexLabelId().getId()).getLabel()
445+
+ "}");
446+
} else {
447+
return edgeKind;
416448
}
417449
}
418450

0 commit comments

Comments
 (0)