Skip to content

Commit

Permalink
Take the case of null into account
Browse files Browse the repository at this point in the history
  • Loading branch information
HyukjinKwon committed Nov 4, 2016
1 parent f151bd1 commit 3692099
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
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) {
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

0 comments on commit 3692099

Please sign in to comment.