Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
17 changes: 17 additions & 0 deletions core/src/main/scala/org/graphframes/GraphFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ class GraphFrame private (
* @group conversions
*/
def toGraphX: Graph[Row, Row] = {
if (hasIntegralIdType) {
// 1. Null IDs
val badVertices = vertices.filter(col(ID).isNull).limit(1).count()

if (badVertices > 0) {
throw new IllegalArgumentException(
s"Vertex ID cannot be null. Found null in column '$ID'.")
}

// 2. Null Src/Dst
val badEdges = edges.filter(col(SRC).isNull || col(DST).isNull).limit(1).count()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh, I do not like it. It is an expensive operation. I would prefer just to raise an exception instead. If no match: an exception that graph is malformed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, I have rebuilt the PR, could you check ?


if (badEdges > 0) {
throw new IllegalArgumentException(s"Edge '$SRC' and '$DST' cannot be null.")
}
}

if (hasIntegralIdType) {
val vv = vertices.select(col(ID).cast(LongType), nestAsCol(vertices, ATTR)).rdd.map {
case Row(id: Long, attr: Row) => (id, attr)
Expand Down
43 changes: 43 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,49 @@ 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[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")

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[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