-
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 2 commits
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) { | ||
|
||
| 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}") | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -68,16 +68,48 @@ class CSVTypeCastSuite extends SparkFunSuite { | |
| } | ||
|
|
||
| test("Nullable types are handled") { | ||
| assert(CSVTypeCast.castTo("", IntegerType, nullable = true, CSVOptions()) == null) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", ByteType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", ShortType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", IntegerType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", LongType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", FloatType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", DoubleType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", BooleanType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", DecimalType.DoubleDecimal, true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", TimestampType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", DateType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", StringType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| } | ||
|
|
||
| test("String type should always return the same as the input") { | ||
| test("String type should also respect `nullValue`") { | ||
| assert( | ||
| CSVTypeCast.castTo("", StringType, nullable = true, CSVOptions()) == | ||
| UTF8String.fromString("")) | ||
| null) | ||
| assert( | ||
| CSVTypeCast.castTo("", StringType, nullable = false, CSVOptions()) == | ||
| UTF8String.fromString("")) | ||
|
|
||
| assert( | ||
| CSVTypeCast.castTo("", StringType, nullable = true, CSVOptions("nullValue", "null")) == | ||
| UTF8String.fromString("")) | ||
| assert( | ||
| CSVTypeCast.castTo("", StringType, nullable = false, CSVOptions("nullValue", "null")) == | ||
| UTF8String.fromString("")) | ||
|
|
||
| assert( | ||
| CSVTypeCast.castTo(null, StringType, nullable = true, CSVOptions("nullValue", "null")) == | ||
| null) | ||
|
||
| } | ||
|
|
||
| test("Throws exception for empty string with non null type") { | ||
|
|
@@ -165,20 +197,4 @@ class CSVTypeCastSuite extends SparkFunSuite { | |
| assert(doubleVal2 == Double.PositiveInfinity) | ||
| } | ||
|
|
||
| test("Type-specific null values are used for casting") { | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", ByteType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", ShortType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", IntegerType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", LongType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", FloatType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", DoubleType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", DecimalType.DoubleDecimal, true, CSVOptions("nullValue", "-"))) | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
BTW, there would be the same documentation in
readwriter.py. I guess we should fix them too.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.
And
streaming.pyas well if I remember correctly.Uh oh!
There was an error while loading. Please reload this page.
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.
Oh thanks! Indeed there are two occurrences (one in
readwriter.py/ one instreaming.py) needs fixing. I'll fix them.