Skip to content
Closed
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
2 changes: 1 addition & 1 deletion python/pyspark/sql/readwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def csv(self, path, schema=None, sep=None, encoding=None, quote=None, escape=Non

>>> df = sqlContext.read.csv('python/test_support/sql/ages.csv')
>>> df.dtypes
[('C0', 'string'), ('C1', 'string')]
[('_c0', 'string'), ('_c1', 'string')]
"""
if schema is not None:
self.schema(schema)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ class DefaultSource extends FileFormat with DataSourceRegister {
val firstRow = new LineCsvReader(csvOptions).parseLine(firstLine)

val header = if (csvOptions.headerFlag) {
firstRow
firstRow.zipWithIndex.map { case (value, index) =>
if (value == null || value.isEmpty || value == csvOptions.nullValue) s"_c$index" else value
}
} else {
firstRow.zipWithIndex.map { case (value, index) => s"C$index" }
firstRow.zipWithIndex.map { case (value, index) => s"_c$index" }
Copy link
Member

@HyukjinKwon HyukjinKwon May 11, 2016

Choose a reason for hiding this comment

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

Why should this be _c?

Copy link
Contributor

Choose a reason for hiding this comment

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

to be consistent with what Spark does with unnamed columns. See my comment above.

Copy link
Member

Choose a reason for hiding this comment

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

I see. Thanks.

}

val parsedRdd = tokenRdd(sparkSession, csvOptions, header, paths)
Expand Down
3 changes: 3 additions & 0 deletions sql/core/src/test/resources/cars-blank-column-name.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"",,make,customer,comment
2012,"Tesla","S","bill","blank"
2013,"Tesla","S","c","something"
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils {
private val carsAltFile = "cars-alternative.csv"
private val carsUnbalancedQuotesFile = "cars-unbalanced-quotes.csv"
private val carsNullFile = "cars-null.csv"
private val carsBlankColName = "cars-blank-column-name.csv"
private val emptyFile = "empty.csv"
private val commentsFile = "comments.csv"
private val disableCommentsFile = "disable_comments.csv"
Expand Down Expand Up @@ -71,14 +72,14 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils {
if (withHeader) {
assert(df.schema.fieldNames === Array("year", "make", "model", "comment", "blank"))
} else {
assert(df.schema.fieldNames === Array("C0", "C1", "C2", "C3", "C4"))
assert(df.schema.fieldNames === Array("_c0", "_c1", "_c2", "_c3", "_c4"))
}
}

if (checkValues) {
val yearValues = List("2012", "1997", "2015")
val actualYears = if (!withHeader) "year" :: yearValues else yearValues
val years = if (withHeader) df.select("year").collect() else df.select("C0").collect()
val years = if (withHeader) df.select("year").collect() else df.select("_c0").collect()

years.zipWithIndex.foreach { case (year, index) =>
if (checkTypes) {
Expand Down Expand Up @@ -224,6 +225,17 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils {
assert(cars.select("year").collect().size === 2)
}

test("test for blank column names on read and select columns") {
val cars = spark.read
.format("csv")
.options(Map("header" -> "true", "inferSchema" -> "true"))
.load(testFile(carsBlankColName))

assert(cars.select("customer").collect().size == 2)
assert(cars.select("_c0").collect().size == 2)
assert(cars.select("_c1").collect().size == 2)
}

test("test for FAILFAST parsing mode") {
val exception = intercept[SparkException]{
spark.read
Expand Down