Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,15 @@ class UnivocityParser(
* Parses a single CSV string and turns it into either one resulting row or no row (if the
* the record is malformed).
*/
def parse(input: String): InternalRow = convert(tokenizer.parseLine(input))
def parse(input: String): InternalRow = {
val parsedTokens = try {
tokenizer.parseLine(input)
} catch {
case NonFatal(e) =>
throw BadRecordException(() => getCurrentInput, () => None, e)
}
convert(parsedTokens)
}

private def convert(tokens: Array[String]): InternalRow = {
if (tokens.length != schema.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1174,4 +1174,33 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils {
}
}
}

test("SPARK-21024 CSV parser mode controls parser exceptions") {
withTempPath { path =>
Seq("0,1", "0,1,2,3").toDF().write.text(path.getAbsolutePath)

Seq(false).foreach { wholeFile =>
val msg = intercept[SparkException] {
spark.read.format("csv")
.schema("a INT, b INT")
.option("maxColumns", "2")
.option("mode", "FAILFAST")
.option("wholeFile", wholeFile)
.load(path.getAbsolutePath)
.collect
}.getMessage
assert(msg.contains("Number of columns processed may have exceeded limit of 2 columns."))

val columnNameOfCorruptRecord = "_unparsed"
val df = spark.read.format("csv")
.schema(s"a INT, b INT, $columnNameOfCorruptRecord STRING")
.option("maxColumns", "2")
.option("mode", "PERMISSIVE")
.option("columnNameOfCorruptRecord", columnNameOfCorruptRecord)
.option("wholeFile", wholeFile)
.load(path.getAbsolutePath)
checkAnswer(df, Row(0, 1, null) :: Row(null, null, "0,1,2,") :: Nil)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@HyukjinKwon weird behaviour..., when we set maxColumns in a Univocity parser, it seems currentParsedContent returns the (maxColumns + 1) elements in inputs.

}
}
}
}