Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Fix: Throw IllegalArgumentException for null IDs/Src/Dst in toGraphX …
…(for #765)
  • Loading branch information
jeganvg
jeganvg committed Jan 2, 2026
commit 2b7b6dfb11c2fb4492bead033de1bc6eb00628b5
2 changes: 2 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,15 @@ 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
41 changes: 41 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,47 @@ class GraphFrameSuite extends SparkFunSuite with GraphFrameTestSparkContext {
assert(edges(3).getLong(1) === 2L)
}

test("toGraphX should throw IllegalArgumentException for null IDs") {
import org.apache.spark.sql.functions._
// Creating vertices with a null ID
val vertices = spark.createDataFrame(Seq(
(1L, "a"),
(null.asInstanceOf[java.lang.Long], "b")
)).toDF("id", "attr")

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

val g = GraphFrame(vertices, edges)

// GraphFramesUnreachableException will be thrown as per issue #765
// IllegalArgumentException expected with the fix
val e = intercept[IllegalArgumentException] {
g.toGraphX
}

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")

// Edge with a NULL destination
val edges = spark.createDataFrame(Seq(
(1L, null.asInstanceOf[java.lang.Long], "friend")
)).toDF("src", "dst", "relationship")

val g = GraphFrame(vertices, edges)

val e = intercept[IllegalArgumentException] {
g.toGraphX
}

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
Loading