Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 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 @@ -232,7 +232,11 @@ private[csv] object CSVTypeCast {
nullable: Boolean = true,
options: CSVOptions = CSVOptions()): Any = {

if (nullable && datum == options.nullValue) {
// datum can be null if the number of fields found is less than the length of the schema
if (datum == options.nullValue || datum == null) {
if (!nullable) {
throw new RuntimeException("null value found but the field is not nullable.")
Copy link
Member

Choose a reason for hiding this comment

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

Nit: This could be require(nullable, ...) which would throw a better exception, IllegalArgumentException, too. (Even NPE would be reasonable.) But I don't feel strongly about it.

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, I think this sounds good.

Copy link
Contributor

@rxin rxin Nov 6, 2016

Choose a reason for hiding this comment

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

Sorry I thought more about this - the current error message doesn't give the user a way to know which field is causing the problem. Can you add at least the field name to the error msg?

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, I can. Will try to make this neat up.

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

test("load null when the schema is larger than parsed tokens ") {
withTempPath { path =>
Seq("1").toDF().write.text(path.getAbsolutePath)
val schema = StructType(
StructField("a", IntegerType, true) ::
StructField("b", IntegerType, true) :: Nil)
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,14 +88,24 @@ 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", "-")))

// casting a null to not nullable field should throw an exception.
var message = intercept[RuntimeException] {
CSVTypeCast.castTo(null, IntegerType, nullable = false, CSVOptions("nullValue", "-"))
}.getMessage
assert(message.contains("null value found but the field is not nullable."))

message = intercept[RuntimeException] {
CSVTypeCast.castTo("-", StringType, nullable = false, CSVOptions("nullValue", "-"))
}.getMessage
assert(message.contains("null value found but the field is not nullable."))
}

test("String type should also respect `nullValue`") {
assertNull(
CSVTypeCast.castTo("", StringType, nullable = true, CSVOptions()))
assert(
CSVTypeCast.castTo("", StringType, nullable = false, CSVOptions()) ==
UTF8String.fromString(""))

assert(
CSVTypeCast.castTo("", StringType, nullable = true, CSVOptions("nullValue", "null")) ==
Expand All @@ -109,10 +119,10 @@ class CSVTypeCastSuite extends SparkFunSuite {
}

test("Throws exception for empty string with non null type") {
val exception = intercept[NumberFormatException]{
val exception = intercept[RuntimeException]{
CSVTypeCast.castTo("", IntegerType, nullable = false, CSVOptions())
}
assert(exception.getMessage.contains("For input string: \"\""))
assert(exception.getMessage.contains("null value found but the field is not nullable."))
}

test("Types are cast correctly") {
Expand Down