Github user JoshRosen commented on a diff in the pull request:
https://github.com/apache/spark/pull/15813#discussion_r90766169
--- Diff:
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/csv/CSVFileFormat.scala
---
@@ -173,51 +179,37 @@ class CSVFileFormat extends TextBasedFileFormat with
DataSourceRegister {
}
}
- private def baseRdd(
- sparkSession: SparkSession,
- options: CSVOptions,
- inputPaths: Seq[String]): RDD[String] = {
- readText(sparkSession, options, inputPaths.mkString(","))
- }
-
- private def tokenRdd(
- sparkSession: SparkSession,
- options: CSVOptions,
- header: Array[String],
- inputPaths: Seq[String]): RDD[Array[String]] = {
- val rdd = baseRdd(sparkSession, options, inputPaths)
- // Make sure firstLine is materialized before sending to executors
- val firstLine = if (options.headerFlag) findFirstLine(options, rdd)
else null
- CSVRelation.univocityTokenizer(rdd, firstLine, options)
- }
-
/**
* Returns the first line of the first non-empty file in path
*/
- private def findFirstLine(options: CSVOptions, rdd: RDD[String]): String
= {
+ private def findFirstLine(options: CSVOptions, lines: Dataset[String]):
String = {
+ import lines.sqlContext.implicits._
+ val nonEmptyLines = lines.filter(length(trim($"value")) > 0)
if (options.isCommentSet) {
- val comment = options.comment.toString
- rdd.filter { line =>
- line.trim.nonEmpty && !line.startsWith(comment)
- }.first()
+
nonEmptyLines.filter(!$"value".startsWith(options.comment.toString)).first()
} else {
- rdd.filter { line =>
- line.trim.nonEmpty
- }.first()
+ nonEmptyLines.first()
}
}
private def readText(
sparkSession: SparkSession,
options: CSVOptions,
- location: String): RDD[String] = {
+ inputPaths: Seq[String]): Dataset[String] = {
if (Charset.forName(options.charset) == StandardCharsets.UTF_8) {
- sparkSession.sparkContext.textFile(location)
+ sparkSession.baseRelationToDataFrame(
+ DataSource.apply(
+ sparkSession,
+ paths = inputPaths,
+ className = classOf[TextFileFormat].getName
+ ).resolveRelation(checkFilesExist = false))
+ .select("value").as[String](Encoders.STRING)
} else {
val charset = options.charset
- sparkSession.sparkContext
- .hadoopFile[LongWritable, Text, TextInputFormat](location)
+ val rdd = sparkSession.sparkContext
--- End diff --
I'm not sure; I think this was a carryover from `spark-csv`.
---
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]