From 5214e8a281e77da5d4451a29a553352959af84ab Mon Sep 17 00:00:00 2001 From: hyukjinkwon Date: Tue, 2 Feb 2016 17:39:00 +0900 Subject: [PATCH] NullPoingException in schema inference for CSV when the first line is empty --- .../sql/execution/datasources/csv/CSVOptions.scala | 3 --- .../sql/execution/datasources/csv/CSVRelation.scala | 12 +++++++----- sql/core/src/test/resources/cars.csv | 1 + 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVOptions.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVOptions.scala index 709daccbbef5..3c1ad4977155 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVOptions.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVOptions.scala @@ -62,9 +62,6 @@ private[sql] class CSVOptions( val ignoreLeadingWhiteSpaceFlag = getBool("ignoreLeadingWhiteSpace") val ignoreTrailingWhiteSpaceFlag = getBool("ignoreTrailingWhiteSpace") - // Limit the number of lines we'll search for a header row that isn't comment-prefixed - val MAX_COMMENT_LINES_IN_HEADER = 10 - // Parse mode flags if (!ParseModes.isValidMode(parseMode)) { logWarning(s"$parseMode is not a valid parse mode. Using ${ParseModes.DEFAULT}.") diff --git a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVRelation.scala b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVRelation.scala index dc449fea956f..a0f557cefae0 100644 --- a/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVRelation.scala +++ b/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVRelation.scala @@ -154,12 +154,14 @@ private[csv] class CSVRelation( */ private def findFirstLine(rdd: RDD[String]): String = { if (params.isCommentSet) { - rdd.take(params.MAX_COMMENT_LINES_IN_HEADER) - .find(!_.startsWith(params.comment.toString)) - .getOrElse(sys.error(s"No uncommented header line in " + - s"first ${params.MAX_COMMENT_LINES_IN_HEADER} lines")) + val comment = params.comment.toString + rdd.filter { line => + line.trim.nonEmpty && !line.startsWith(comment) + }.first() } else { - rdd.first() + rdd.filter { line => + line.trim.nonEmpty + }.first() } } } diff --git a/sql/core/src/test/resources/cars.csv b/sql/core/src/test/resources/cars.csv index 2b9d74ca607a..40ded573ade5 100644 --- a/sql/core/src/test/resources/cars.csv +++ b/sql/core/src/test/resources/cars.csv @@ -1,3 +1,4 @@ + year,make,model,comment,blank "2012","Tesla","S","No comment",