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
18 changes: 15 additions & 3 deletions core/src/main/scala/org/graphframes/GraphFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,24 @@ class GraphFrame private (
/**
* Returns triplets: (source vertex)-[edge]->(destination vertex) for all edges in the graph.
* The DataFrame returned has 3 columns, with names: [[GraphFrame.SRC]], [[GraphFrame.EDGE]],
* and [[GraphFrame.DST]]. The 2 vertex columns have schema matching [[GraphFrame.vertices]],
* and the edge column has a schema matching [[GraphFrame.edges]].
* and [[GraphFrame.DST]]. Each column is a struct. The 2 vertex columns have schema matching
* [[GraphFrame.vertices]], and the edge column has a schema matching [[GraphFrame.edges]]. For
* example, `triplets.select(col(SRC)(ID))` selects ID of the source column.
*
* @group structure
*/
lazy val triplets: DataFrame = find(s"($SRC)-[$EDGE]->($DST)")
lazy val triplets: DataFrame = {
vertices
.select(col(ID).alias("src_id"), nestAsCol(vertices, SRC))
.join(
edges
.select(col(SRC).alias("edge_src"), col(DST).alias("edge_dst"), nestAsCol(edges, EDGE)),
col("src_id") === col("edge_src"))
.join(
vertices.select(col(ID).alias("dst_id"), nestAsCol(vertices, DST)),
col("dst_id") === col("edge_dst"))
.drop("src_id", "edge_src", "dst_id", "edge_dst")
}

// ============================ Conversions ========================================

Expand Down
38 changes: 38 additions & 0 deletions core/src/test/scala/org/graphframes/GraphFrameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,44 @@ class GraphFrameSuite extends SparkFunSuite with GraphFrameTestSparkContext {
}
}

test("test triplets") {
// Basic triplets test
val g = GraphFrame(vertices, edges)
val triplets = g.triplets.collect()
assert(triplets.length === localEdges.size)
triplets.foreach {
case Row(src: Row, edge: Row, dst: Row) =>
assert(src.getLong(0) === edge.getLong(0)) // src.id === edge.src
assert(dst.getLong(0) === edge.getLong(1)) // dst.id === edge.dst
assert(localEdges((edge.getLong(0), edge.getLong(1))) === edge.getString(2))
case _ => throw new GraphFramesUnreachableException()
}

// Test with attributes
val v2 = vertices.withColumn("age", lit(10))
val e2 = edges.withColumn("weight", lit(2.0))
val g2 = GraphFrame(v2, e2)
val triplets2 = g2.triplets.collect()
triplets2.foreach {
case Row(src: Row, edge: Row, _: Row) =>
assert(src.getInt(2) === 10) // Check vertex attribute
assert(edge.getDouble(3) === 2.0) // Check edge attribute
case _ => throw new GraphFramesUnreachableException()
}

// Test with dots in column names
val v3 = v2.withColumnRenamed("age", "person.age")
val e3 = e2.withColumnRenamed("weight", "edge.weight")
val g3 = GraphFrame(v3, e3)
val triplets3 = g3.triplets.collect()
triplets3.foreach {
case Row(src: Row, edge: Row, _: Row) =>
assert(src.getInt(2) === 10) // Check vertex attribute
assert(edge.getDouble(3) === 2.0) // Check edge attribute
case _ => throw new GraphFramesUnreachableException()
}
}

test("nestAsCol with dots in column names") {
val df = vertices.withColumnRenamed("name", "a.name")
val col = nestAsCol(df, "attr")
Expand Down