Github user HyukjinKwon commented on a diff in the pull request:
https://github.com/apache/spark/pull/16928#discussion_r101911173
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/UnivocityParser.scala
---
@@ -202,21 +214,30 @@ private[csv] class UnivocityParser(
}
numMalformedRecords += 1
None
- } else if (options.failFast && schema.length != tokens.length) {
+ } else if (options.failFast && inputSchema.length != tokens.length) {
throw new RuntimeException(s"Malformed line in FAILFAST mode: " +
s"${tokens.mkString(options.delimiter.toString)}")
} else {
- val checkedTokens = if (options.permissive && schema.length >
tokens.length) {
- tokens ++ new Array[String](schema.length - tokens.length)
- } else if (options.permissive && schema.length < tokens.length) {
- tokens.take(schema.length)
+ if (options.permissive && shouldHandleCorruptRecord) {
+ row.setNullAt(corruptIndex)
+ }
+ val checkedTokens = if (options.permissive && inputSchema.length >
tokens.length) {
+ tokens ++ new Array[String](inputSchema.length - tokens.length)
+ } else if (options.permissive && inputSchema.length < tokens.length)
{
+ tokens.take(inputSchema.length)
} else {
tokens
}
try {
Some(convert(checkedTokens))
} catch {
+ // We only catch exceptions about malformed values here and pass
over other exceptions
+ // (e.g., SparkException about unsupported types).
+ case _: NumberFormatException | _: IllegalArgumentException
+ if options.permissive && shouldHandleCorruptRecord =>
+ row(corruptIndex) =
UTF8String.fromString(tokens.mkString(options.delimiter.toString))
--- End diff --
Then,.. we would be able to do as below:
```scala
case NonFatal(e) if options.permissive =>
val row = new GenericInternalRow(requiredSchema.length)
corruptFieldIndex.foreach { idx =>
row(idx) = UTF8String.fromString(input)
}
```
For unsupported type, could we utilize `CSVUtils.verifySchema`? I proposed
related change in
https://github.com/apache/spark/pull/15751/files#diff-a549ac2e19ee7486911e2e6403444d9d
If you add the change here, I will take this out in my PR.
---
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]