-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-26151][SQL] Return partial results for bad CSV records #23120
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 all commits
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 |
---|---|---|
|
@@ -33,26 +33,21 @@ class FailureSafeParser[IN]( | |
private val corruptFieldIndex = schema.getFieldIndex(columnNameOfCorruptRecord) | ||
private val actualSchema = StructType(schema.filterNot(_.name == columnNameOfCorruptRecord)) | ||
private val resultRow = new GenericInternalRow(schema.length) | ||
private val nullResult = new GenericInternalRow(schema.length) | ||
|
||
// This function takes 2 parameters: an optional partial result, and the bad record. If the given | ||
// schema doesn't contain a field for corrupted record, we just return the partial result or a | ||
// row with all fields null. If the given schema contains a field for corrupted record, we will | ||
// set the bad record to this field, and set other fields according to the partial result or null. | ||
private val toResultRow: (Option[InternalRow], () => UTF8String) => InternalRow = { | ||
if (corruptFieldIndex.isDefined) { | ||
(row, badRecord) => { | ||
var i = 0 | ||
while (i < actualSchema.length) { | ||
val from = actualSchema(i) | ||
resultRow(schema.fieldIndex(from.name)) = row.map(_.get(i, from.dataType)).orNull | ||
i += 1 | ||
} | ||
resultRow(corruptFieldIndex.get) = badRecord() | ||
resultRow | ||
(row, badRecord) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. without this change in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For now JSON does not support this. Need additional changes in JacksonParser to return partial results. |
||
var i = 0 | ||
while (i < actualSchema.length) { | ||
val from = actualSchema(i) | ||
resultRow(schema.fieldIndex(from.name)) = row.map(_.get(i, from.dataType)).orNull | ||
i += 1 | ||
} | ||
} else { | ||
(row, _) => row.getOrElse(nullResult) | ||
corruptFieldIndex.foreach(index => resultRow(index) = badRecord()) | ||
resultRow | ||
} | ||
} | ||
|
||
|
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.
shall we stop parsing when we hit the first exception?
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.
but we will lose field values that could be converted successfully after the exception.
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.
I know it's doable for CSV, as the tokens are separated ahead, and we can keep parsing after an exception. Is it also doable for other text based data sources?
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 depends on what kind of error we face to. If a parser is still in normal state and ready to continue, we could skip current error. In case of JSON, we parse input in stream fashion, and convert values to desired type on the fly. If JacksonParser is able to recognize next token, why we should stop on the first error?