Skip to content

Commit 84f8492

Browse files
xguo27rxin
authored andcommitted
[SPARK-12562][SQL] DataFrame.write.format(text) requires the column name to be called value
Author: Xiu Guo <xguo27@gmail.com> Closes apache#10515 from xguo27/SPARK-12562.
1 parent 13dab9c commit 84f8492

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/text/DefaultSource.scala

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class DefaultSource extends HadoopFsRelationProvider with DataSourceRegister {
4848
partitionColumns: Option[StructType],
4949
parameters: Map[String, String]): HadoopFsRelation = {
5050
dataSchema.foreach(verifySchema)
51-
new TextRelation(None, partitionColumns, paths)(sqlContext)
51+
new TextRelation(None, dataSchema, partitionColumns, paths)(sqlContext)
5252
}
5353

5454
override def shortName(): String = "text"
@@ -68,15 +68,16 @@ class DefaultSource extends HadoopFsRelationProvider with DataSourceRegister {
6868

6969
private[sql] class TextRelation(
7070
val maybePartitionSpec: Option[PartitionSpec],
71+
val textSchema: Option[StructType],
7172
override val userDefinedPartitionColumns: Option[StructType],
7273
override val paths: Array[String] = Array.empty[String],
7374
parameters: Map[String, String] = Map.empty[String, String])
7475
(@transient val sqlContext: SQLContext)
7576
extends HadoopFsRelation(maybePartitionSpec, parameters) {
7677

77-
/** Data schema is always a single column, named "value". */
78-
override def dataSchema: StructType = new StructType().add("value", StringType)
79-
78+
/** Data schema is always a single column, named "value" if original Data source has no schema. */
79+
override def dataSchema: StructType =
80+
textSchema.getOrElse(new StructType().add("value", StringType))
8081
/** This is an internal data source that outputs internal row format. */
8182
override val needConversion: Boolean = false
8283

sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/text/TextSuite.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class TextSuite extends QueryTest with SharedSQLContext {
3333
verifyFrame(sqlContext.read.text(testFile))
3434
}
3535

36-
test("writing") {
37-
val df = sqlContext.read.text(testFile)
36+
test("SPARK-12562 verify write.text() can handle column name beyond `value`") {
37+
val df = sqlContext.read.text(testFile).withColumnRenamed("value", "adwrasdf")
3838

3939
val tempFile = Utils.createTempDir()
4040
tempFile.delete()

0 commit comments

Comments
 (0)