@@ -29,25 +29,33 @@ object TypeCast {
2929 * Casts given string datum to specified type.
3030 * Currently we do not support complex types (ArrayType, MapType, StructType).
3131 *
32+ * For string types, this is simply the datum. For other types.
33+ * For other nullable types, this is null if the string datum is empty.
34+ *
3235 * @param datum string value
3336 * @param castType SparkSQL type
3437 */
35- private [csv] def castTo (datum : String , castType : DataType ): Any = {
36- castType match {
37- case _ : ByteType => datum.toByte
38- case _ : ShortType => datum.toShort
39- case _ : IntegerType => datum.toInt
40- case _ : LongType => datum.toLong
41- case _ : FloatType => datum.toFloat
42- case _ : DoubleType => datum.toDouble
43- case _ : BooleanType => datum.toBoolean
44- case _ : DecimalType => new BigDecimal (datum.replaceAll(" ," , " " ))
45- // TODO(hossein): would be good to support other common timestamp formats
46- case _ : TimestampType => Timestamp .valueOf(datum)
47- // TODO(hossein): would be good to support other common date formats
48- case _ : DateType => Date .valueOf(datum)
49- case _ : StringType => datum
50- case _ => throw new RuntimeException (s " Unsupported type: ${castType.typeName}" )
38+ private [csv] def castTo (datum : String , castType : DataType , nullable : Boolean = true ): Any = {
39+ if (castType.isInstanceOf [StringType ]){
40+ datum
41+ } else if (nullable && datum == " " ){
42+ null
43+ } else {
44+ castType match {
45+ case _ : ByteType => datum.toByte
46+ case _ : ShortType => datum.toShort
47+ case _ : IntegerType => datum.toInt
48+ case _ : LongType => datum.toLong
49+ case _ : FloatType => datum.toFloat
50+ case _ : DoubleType => datum.toDouble
51+ case _ : BooleanType => datum.toBoolean
52+ case _ : DecimalType => new BigDecimal (datum.replaceAll(" ," , " " ))
53+ // TODO(hossein): would be good to support other common timestamp formats
54+ case _ : TimestampType => Timestamp .valueOf(datum)
55+ // TODO(hossein): would be good to support other common date formats
56+ case _ : DateType => Date .valueOf(datum)
57+ case _ => throw new RuntimeException (s " Unsupported type: ${castType.typeName}" )
58+ }
5159 }
5260 }
5361
0 commit comments