Github user HyukjinKwon commented on a diff in the pull request: https://github.com/apache/spark/pull/16351#discussion_r93814350 --- Diff: sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/csv/CSVTypeCastSuite.scala --- @@ -66,144 +66,141 @@ class CSVTypeCastSuite extends SparkFunSuite { } test("Nullable types are handled") { - assertNull( - CSVTypeCast.castTo("-", "_1", ByteType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", ShortType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", IntegerType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", LongType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", FloatType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", DoubleType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", BooleanType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", DecimalType.DoubleDecimal, true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", TimestampType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", DateType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo("-", "_1", StringType, nullable = true, CSVOptions("nullValue", "-"))) - assertNull( - CSVTypeCast.castTo(null, "_1", IntegerType, nullable = true, CSVOptions("nullValue", "-"))) - - // casting a null to not nullable field should throw an exception. - var message = intercept[RuntimeException] { - CSVTypeCast.castTo(null, "_1", IntegerType, nullable = false, CSVOptions("nullValue", "-")) - }.getMessage - assert(message.contains("null value found but field _1 is not nullable.")) - - message = intercept[RuntimeException] { - CSVTypeCast.castTo("-", "_1", StringType, nullable = false, CSVOptions("nullValue", "-")) - }.getMessage - assert(message.contains("null value found but field _1 is not nullable.")) + val types = Seq(ByteType, ShortType, IntegerType, LongType, FloatType, DoubleType, + BooleanType, DecimalType.DoubleDecimal, TimestampType, DateType, StringType) + + types.foreach { t => + val converterOne = + CSVTypeCast.makeConverter("_1", t, nullable = true, CSVOptions("nullValue", "-")) + assertNull(converterOne.apply("-")) + assertNull(converterOne.apply(null)) + + // casting a null to not nullable field should throw an exception. + val converterTwo = + CSVTypeCast.makeConverter("_1", t, nullable = false, CSVOptions("nullValue", "-")) + + var message = intercept[RuntimeException] { + converterTwo.apply("-") + }.getMessage + assert(message.contains("null value found but field _1 is not nullable.")) + + message = intercept[RuntimeException] { + converterTwo.apply(null) + }.getMessage + assert(message.contains("null value found but field _1 is not nullable.")) + } } test("String type should also respect `nullValue`") { --- End diff -- I see, some of tests are overlapped. Let me try to clean up with some comments.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org