Github user HyukjinKwon commented on a diff in the pull request:
https://github.com/apache/spark/pull/12921#discussion_r62159750
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVInferSchema.scala
---
@@ -192,59 +192,59 @@ private[csv] object CSVTypeCast {
nullable: Boolean = true,
options: CSVOptions = CSVOptions()): Any = {
- castType match {
- case _: ByteType => if (datum == options.nullValue && nullable) null
else datum.toByte
- case _: ShortType => if (datum == options.nullValue && nullable)
null else datum.toShort
- case _: IntegerType => if (datum == options.nullValue && nullable)
null else datum.toInt
- case _: LongType => if (datum == options.nullValue && nullable) null
else datum.toLong
- case _: FloatType =>
- if (datum == options.nullValue && nullable) {
- null
- } else if (datum == options.nanValue) {
- Float.NaN
- } else if (datum == options.negativeInf) {
- Float.NegativeInfinity
- } else if (datum == options.positiveInf) {
- Float.PositiveInfinity
- } else {
- Try(datum.toFloat)
-
.getOrElse(NumberFormat.getInstance(Locale.getDefault).parse(datum).floatValue())
- }
- case _: DoubleType =>
- if (datum == options.nullValue && nullable) {
- null
- } else if (datum == options.nanValue) {
- Double.NaN
- } else if (datum == options.negativeInf) {
- Double.NegativeInfinity
- } else if (datum == options.positiveInf) {
- Double.PositiveInfinity
- } else {
- Try(datum.toDouble)
-
.getOrElse(NumberFormat.getInstance(Locale.getDefault).parse(datum).doubleValue())
- }
- case _: BooleanType => datum.toBoolean
- case dt: DecimalType =>
- if (datum == options.nullValue && nullable) {
- null
- } else {
- val value = new BigDecimal(datum.replaceAll(",", ""))
- Decimal(value, dt.precision, dt.scale)
- }
- case _: TimestampType if options.dateFormat != null =>
- // This one will lose microseconds parts.
- // See https://issues.apache.org/jira/browse/SPARK-10681.
- options.dateFormat.parse(datum).getTime * 1000L
- case _: TimestampType =>
- // This one will lose microseconds parts.
- // See https://issues.apache.org/jira/browse/SPARK-10681.
- DateTimeUtils.stringToTime(datum).getTime * 1000L
- case _: DateType if options.dateFormat != null =>
- DateTimeUtils.millisToDays(options.dateFormat.parse(datum).getTime)
- case _: DateType =>
-
DateTimeUtils.millisToDays(DateTimeUtils.stringToTime(datum).getTime)
- case _: StringType => UTF8String.fromString(datum)
- case _ => throw new RuntimeException(s"Unsupported type:
${castType.typeName}")
+ if (datum == null || (datum == options.nullValue && nullable)) {
--- End diff --
Simply the logic below was added just like `inferField()`:
```scala
if (datum == null || (datum == options.nullValue && nullable)) {
null
} else {
...
```
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]