-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-16462][SPARK-16460][SPARK-15144][SQL] Make CSV cast null values properly #14118
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
e782616
bf01cea
f58e33d
74b4dd8
d5357f9
365cbfb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,59 +238,55 @@ 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 { | ||
| if (datum == options.nullValue && nullable && (!castType.isInstanceOf[StringType])) { | ||
| null | ||
| } else { | ||
| castType match { | ||
| case _: ByteType => datum.toByte | ||
| case _: ShortType => datum.toShort | ||
| case _: IntegerType => datum.toInt | ||
| case _: LongType => datum.toLong | ||
| case _: FloatType => | ||
| 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.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 => | ||
| 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}") | ||
| 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}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind if I ask why
StringTypeis excluded?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be great to document why string type is ignored here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @HyukjinKwon, it's just to keep consistency with we did in
spark-csvfor 1.6. Actually I don't have strong preference here -- maybe we should not ignoreStringType? @rxin could you share some thoughts? Thanks!