Skip to content
Closed
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
Next Next commit
Take the case of null into account
  • Loading branch information
HyukjinKwon committed Nov 4, 2016
commit 36920996e42ccda514e69b9aae0cf3bfe13242ce
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ private[csv] object CSVTypeCast {
nullable: Boolean = true,
options: CSVOptions = CSVOptions()): Any = {

if (nullable && datum == options.nullValue) {
if (datum == null || nullable && datum == options.nullValue) {
Copy link
Member

Choose a reason for hiding this comment

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

This might be clearer with parentheses, though I think it's correct. It is this right?
datum == null || (nullable && datum == options.nullValue)

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, makes sense.

null
} else {
castType match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -890,4 +890,20 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils {
}
}
}

test("load null when the schema is larger than parsed tokens ") {
withTempPath { path =>
val schema = StructType(Array(
StructField("a", IntegerType, nullable = true),
StructField("b", IntegerType, nullable = true)
))
Seq("1").toDF().write.text(path.getAbsolutePath)
val df = spark.read
.schema(schema)
.option("header", "false")
.csv(path.getAbsolutePath)

checkAnswer(df, Row(1, null))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class CSVTypeCastSuite extends SparkFunSuite {
CSVTypeCast.castTo("-", DateType, nullable = true, CSVOptions("nullValue", "-")))
assertNull(
CSVTypeCast.castTo("-", StringType, nullable = true, CSVOptions("nullValue", "-")))
assertNull(
CSVTypeCast.castTo(null, IntegerType, nullable = true, CSVOptions("nullValue", "-")))
}

test("String type should also respect `nullValue`") {
Expand Down