Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ 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 == "" || value == null) s"C$index" else value
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 Spark allows a empty string as a field. So, I wonder if we should rename this with the index and prefix, C. Also, I think "" will throw an NPE whereas empty string without quotes will produce a correct field because the default of nullValue is "".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code does rename it with the index and prefix, C.

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.

I mean if one of values in the header is a empty string then, I think the field name should be a empty string since apparently it works with fields named empty strings. I tested this by manually giving a schema.

Also, If the header is used for schema, then I think the names should be as they are. We don't really change field names specified in ORC, Parquet or JSON.

}
} else {
firstRow.zipWithIndex.map { case (value, index) => s"C$index" }
Copy link
Contributor

Choose a reason for hiding this comment

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

I talked to @marmbrus offline. Elsewhere we use _c0 instead of C0, so we should make that consistent. You're gonna have to change the name both here and in L65.

}
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 @@ -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