Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -20,6 +20,7 @@ package org.apache.spark.sql.execution.datasources.csv
import java.math.BigDecimal

import scala.util.control.Exception._
import scala.util.{Failure, Success, Try}

import com.univocity.parsers.csv.CsvParser

Expand All @@ -40,7 +41,19 @@ private[csv] object CSVInferSchema {
csv: Dataset[String],
caseSensitive: Boolean,
options: CSVOptions): StructType = {
val firstLine: String = CSVUtils.filterCommentAndEmpty(csv, options).first()
val lines = CSVUtils.filterCommentAndEmpty(csv, options)
Copy link
Member

Choose a reason for hiding this comment

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

Hi @wojtek-szymanski I think we should not rely on exception handling. I can think of take(1).headOption but we could use shorten one if you know any other good way. What do you think about this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You are absolutely right. Relying on exception handling is smelly, while Option gives more opportunities. I also see no difference from performance point of view, since both first() and take(1) call the the same function head(1).

Try(lines.first()) match {
case Success(firstLine) => infer(csv, caseSensitive, options, firstLine)
case Failure(e: NoSuchElementException) => StructType(Seq())
case Failure(e) => throw e
}
}

private def infer(
csv: Dataset[String],
caseSensitive: Boolean,
options: CSVOptions,
firstLine: String): StructType = {
val firstRow = new CsvParser(options.asParserSettings).parseLine(firstLine)
val header = makeSafeHeader(firstRow, caseSensitive, options)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,13 +298,21 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils {
test("test with empty file and known schema") {
val result = spark.read
.format("csv")
.schema(StructType(List(StructField("column", StringType, false))))
.schema(StructType(List(StructField("column", StringType, nullable = false))))
.load(testFile(emptyFile))

assert(result.collect.size === 0)
assert(result.collect().isEmpty)
assert(result.schema.fieldNames.size === 1)
}

test("test with empty file without schema") {
Copy link
Member

@HyukjinKwon HyukjinKwon Mar 4, 2017

Choose a reason for hiding this comment

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

Let's re-use the test in CSVSuite.scala#L1083.

We could..

  test("Empty file produces empty dataframe with empty schema") {
    Seq(false, true).foreach { wholeFile =>
      val df = spark.read.format("csv")
        .option("header", true)
        .option("wholeFile", wholeFile)
        .load(testFile(emptyFile))

        assert(df.schema === spark.emptyDataFrame.schema)
        checkAnswer(df, spark.emptyDataFrame)
      }
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, done

val result = spark.read
.csv(testFile(emptyFile))

assert(result.collect().isEmpty)
assert(result.schema.fieldNames.isEmpty)
}

test("DDL test with empty file") {
withView("carsTable") {
spark.sql(
Expand Down