diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVInferSchema.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVInferSchema.scala index c63aae9d8385..526d9196c680 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVInferSchema.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVInferSchema.scala @@ -246,9 +246,9 @@ private[csv] object CSVTypeCast { } else { castType match { case _: ByteType => datum.toByte - case _: ShortType => datum.toShort - case _: IntegerType => datum.toInt - case _: LongType => datum.toLong + case _: ShortType => Try(datum.toShort).getOrElse(null) + case _: IntegerType => Try(datum.toInt).getOrElse(null) + case _: LongType => Try(datum.toLong).getOrElse(null) case _: FloatType => datum match { case options.nanValue => Float.NaN