Skip to content

Commit 052dee0

Browse files
committed
[SPARK-6686][SQL] Use resolved output instead of names for toDF rename
This is a workaround for a problem reported on the user list. This doesn't fix the core problem, but in general is a more robust way to do renames. Author: Michael Armbrust <michael@databricks.com> Closes #5337 from marmbrus/toDFrename and squashes the following commits: 6a3159d [Michael Armbrust] [SPARK-6686][SQL] Use resolved output instead of names for toDF rename
1 parent 947802c commit 052dee0

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

sql/core/src/main/scala/org/apache/spark/sql/DataFrame.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,8 @@ class DataFrame private[sql](
240240
s"Old column names (${schema.size}): " + schema.fields.map(_.name).mkString(", ") + "\n" +
241241
s"New column names (${colNames.size}): " + colNames.mkString(", "))
242242

243-
val newCols = schema.fieldNames.zip(colNames).map { case (oldName, newName) =>
244-
apply(oldName).as(newName)
243+
val newCols = logicalPlan.output.zip(colNames).map { case (oldAttribute, newName) =>
244+
Column(oldAttribute).as(newName)
245245
}
246246
select(newCols :_*)
247247
}

sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,14 @@ class DataFrameSuite extends QueryTest {
6060
assert($"test".toString === "test")
6161
}
6262

63+
test("rename nested groupby") {
64+
val df = Seq((1,(1,1))).toDF()
65+
66+
checkAnswer(
67+
df.groupBy("_1").agg(col("_1"), sum("_2._1")).toDF("key", "total"),
68+
Row(1, 1) :: Nil)
69+
}
70+
6371
test("invalid plan toString, debug mode") {
6472
val oldSetting = TestSQLContext.conf.dataFrameEagerAnalysis
6573
TestSQLContext.setConf(SQLConf.DATAFRAME_EAGER_ANALYSIS, "true")

0 commit comments

Comments
 (0)