From 7eb83fefd19e137d80a23b5174b66b14831c291a Mon Sep 17 00:00:00 2001
From: hyukjinkwon <gurwls223@gmail.com>
Date: Sun, 21 Feb 2016 13:21:59 -0800
Subject: [PATCH] [SPARK-13137][SQL] NullPoingException in schema inference for
 CSV when the first line is empty

https://issues.apache.org/jira/browse/SPARK-13137

This PR adds a filter in schema inference so that it does not emit NullPointException.

Also, I removed `MAX_COMMENT_LINES_IN_HEADER `but instead used a monad chaining with `filter()` and `first()`.

Lastly, I simply added a newline rather than adding a new file for this so that this is covered with the original tests.

Author: hyukjinkwon <gurwls223@gmail.com>

Closes #11023 from HyukjinKwon/SPARK-13137.
---
 .../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 bea8e97a9a..38aa2dd80a 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
@@ -75,9 +75,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 f8e3a1b6d4..471ed0d560 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 2b9d74ca60..40ded573ad 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",
 
-- 
GitLab