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
Prev Previous commit
Next Next commit
Support columns containing backquotes
  • Loading branch information
EnricoMi committed Mar 28, 2025
commit dca14279778503adb9270b24d29be6e54a7a294f
3 changes: 2 additions & 1 deletion src/main/scala/org/graphframes/GraphFrame.scala
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,8 @@ object GraphFrame extends Serializable with Logging {
* Note: This can be replaced with org.apache.spark.sql.catalyst.util.QuotingUtils.quoteIfNeeded
* once support for Spark 3 has been dropped
*/
private[graphframes] def quote(column: String): String = s"`$column`"
private[graphframes] def quote(column: String): String =
s"`${column.replace("`", "``")}`"

/**
* Helper for column names containing a dot. Quotes the given column name with backticks to
Expand Down
27 changes: 27 additions & 0 deletions src/test/scala/org/graphframes/GraphFrameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ class GraphFrameSuite extends SparkFunSuite with GraphFrameTestSparkContext {
g.pageRank.maxIter(10).run()
}

test("construction from DataFrames with backquote in column names") {
val g = GraphFrame(
vertices.withColumnRenamed("name", "a `name`"),
edges.withColumnRenamed("action", "the `action`"))
g.vertices.collect().foreach { case Row(id: Long, name: String) =>
assert(localVertices(id) === name)
}
g.edges.collect().foreach { case Row(src: Long, dst: Long, action: String) =>
assert(localEdges((src, dst)) === action)
}
g.pageRank.maxIter(10).run()
}

test("construction from edge DataFrame") {
val g = GraphFrame.fromEdges(edges)
assert(g.vertices.columns === Array("id"))
Expand Down Expand Up @@ -312,6 +325,20 @@ class GraphFrameSuite extends SparkFunSuite with GraphFrameTestSparkContext {
nullable = false))))
}

test("nestAsCol with backquote in column names") {
val df = vertices.withColumnRenamed("name", "a `name`")
val col = nestAsCol(df, "attr")
assert(
df.select(col).schema === StructType(
Seq(
StructField(
"attr",
StructType(Seq(
StructField("id", LongType, nullable = false),
StructField("a `name`", StringType, nullable = true))),
nullable = false))))
}

test("skewed long ID assignments") {
val spark = this.spark
import spark.implicits._
Expand Down
7 changes: 7 additions & 0 deletions src/test/scala/org/graphframes/lib/ShortestPathsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,11 @@ class ShortestPathsSuite extends SparkFunSuite with GraphFrameTestSparkContext {
doTest(Some(vertices))
}

test("Test vertices with backquote in column name") {
val verticeSeq =
Seq((1L, "one"), (2L, "two"), (3L, "three"), (4L, "four"), (5L, "five"), (6L, "six"))
val vertices = sqlContext.createDataFrame(verticeSeq).toDF("id", "a `name`")
doTest(Some(vertices))
}

}
14 changes: 14 additions & 0 deletions src/test/scala/org/graphframes/lib/TriangleCountSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ class TriangleCountSuite extends SparkFunSuite with GraphFrameTestSparkContext {
.foreach { case Row(vid: Long, count: Long, _) => assert(count === 1) }
}

test("Count with backquote in column name") {
val edges = sqlContext.createDataFrame(Array(0L -> 1L, 1L -> 2L, 2L -> 0L)).toDF("src", "dst")
val vertices = sqlContext
.createDataFrame(Seq((0L, "a"), (1L, "b"), (2L, "c")))
.toDF("id", "a `column`")
val g = GraphFrame(vertices, edges)
val v2 = g.triangleCount.run()
TestUtils.testSchemaInvariants(g, v2)
TestUtils.checkColumnType(v2.schema, "count", DataTypes.LongType)
v2.select("id", "count", quote("a `column`"))
.collect()
.foreach { case Row(vid: Long, count: Long, _) => assert(count === 1) }
}

test("no triangle") {
val edges = spark.createDataFrame(Array(0L -> 1L, 1L -> 2L)).toDF("src", "dst")
val g = GraphFrame.fromEdges(edges)
Expand Down