Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/src/main/scala/org/graphframes/GraphFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,18 @@ class GraphFrame private (
if (hasIntegralIdType) {
val vv = vertices.select(col(ID).cast(LongType), nestAsCol(vertices, ATTR)).rdd.map {
case Row(id: Long, attr: Row) => (id, attr)
case Row(null, _) =>
throw new IllegalArgumentException(
s"Vertex ID cannot be null. Found null in column '$ID'.")
case _ => throw new GraphFramesUnreachableException()
}
val ee = edges
.select(col(SRC).cast(LongType), col(DST).cast(LongType), nestAsCol(edges, ATTR))
.rdd
.map {
case Row(srcId: Long, dstId: Long, attr: Row) => Edge(srcId, dstId, attr)
case Row(null, _, _) | Row(_, null, _) =>
throw new IllegalArgumentException(s"Edge '$SRC' and '$DST' cannot be null.")
case _ => throw new GraphFramesUnreachableException()
}
Graph[Row, Row](vv, ee)
Expand Down
47 changes: 47 additions & 0 deletions core/src/test/scala/org/graphframes/GraphFrameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,53 @@ class GraphFrameSuite extends SparkFunSuite with GraphFrameTestSparkContext {
assert(edges(3).getLong(1) === 2L)
}

test("toGraphX should throw IllegalArgumentException for null IDs") {
val schema = StructType(
Seq(
StructField("id", LongType, nullable = true),
StructField("attr", StringType, nullable = true)))

val data = spark.sparkContext.parallelize(Seq(Row(1L, "a"), Row(null, "b")))

val vertices = spark.createDataFrame(data, schema)
val edges = spark.createDataFrame(Seq((1L, 1L, "friend"))).toDF("src", "dst", "relationship")

val g = GraphFrame(vertices, edges)

val e = intercept[org.apache.spark.SparkException] {
// Trigger an action to hit the lazy exception in the .map
g.toGraphX.vertices.collect()
}

assert(e.getCause.isInstanceOf[IllegalArgumentException])
assert(e.getMessage.contains("Vertex ID cannot be null"))
}

test("toGraphX should throw IllegalArgumentException for null Edge Src/Dst") {
val vertices = spark.createDataFrame(Seq((1L, "a"))).toDF("id", "attr")

val edgeSchema = StructType(
Seq(
StructField("src", LongType, nullable = true),
StructField("dst", LongType, nullable = true),
StructField("relationship", StringType, nullable = true)))

val edgeData = spark.sparkContext.parallelize(Seq(Row(1L, null, "friend")))

val edges = spark.createDataFrame(edgeData, edgeSchema)

val g = GraphFrame(vertices, edges)

val e = intercept[org.apache.spark.SparkException] {
// Trigger action on edges
g.toGraphX.edges.collect()
}

assert(e.getCause.isInstanceOf[IllegalArgumentException])
assert(e.getMessage.contains("Edge"))
assert(e.getMessage.contains("cannot be null"))
}

test("convert directed graph with edge attributes to undirected") {
val v = spark.createDataFrame(Seq((1L, "a"), (2L, "b"))).toDF("id", "name")
val e = spark.createDataFrame(Seq((1L, 2L, "edge1"))).toDF("src", "dst", "attr")
Expand Down