Skip to content

Commit 3692099

Browse files
committed
Take the case of null into account
1 parent f151bd1 commit 3692099

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVInferSchema.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ private[csv] object CSVTypeCast {
232232
nullable: Boolean = true,
233233
options: CSVOptions = CSVOptions()): Any = {
234234

235-
if (nullable && datum == options.nullValue) {
235+
if (datum == null || nullable && datum == options.nullValue) {
236236
null
237237
} else {
238238
castType match {

sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/csv/CSVSuite.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,4 +890,20 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils {
890890
}
891891
}
892892
}
893+
894+
test("load null when the schema is larger than parsed tokens ") {
895+
withTempPath { path =>
896+
val schema = StructType(Array(
897+
StructField("a", IntegerType, nullable = true),
898+
StructField("b", IntegerType, nullable = true)
899+
))
900+
Seq("1").toDF().write.text(path.getAbsolutePath)
901+
val df = spark.read
902+
.schema(schema)
903+
.option("header", "false")
904+
.csv(path.getAbsolutePath)
905+
906+
checkAnswer(df, Row(1, null))
907+
}
908+
}
893909
}

sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/csv/CSVTypeCastSuite.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ class CSVTypeCastSuite extends SparkFunSuite {
8888
CSVTypeCast.castTo("-", DateType, nullable = true, CSVOptions("nullValue", "-")))
8989
assertNull(
9090
CSVTypeCast.castTo("-", StringType, nullable = true, CSVOptions("nullValue", "-")))
91+
assertNull(
92+
CSVTypeCast.castTo(null, IntegerType, nullable = true, CSVOptions("nullValue", "-")))
9193
}
9294

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

0 commit comments

Comments
 (0)