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 @@ -54,10 +54,21 @@ abstract class CSVDataSource extends Serializable {
/**
* Infers the schema from `inputPaths` files.
*/
def infer(
final def infer(
Copy link
Member Author

Choose a reason for hiding this comment

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

This resembles JsonDataSource's one.

sparkSession: SparkSession,
inputPaths: Seq[FileStatus],
parsedOptions: CSVOptions): Option[StructType]
parsedOptions: CSVOptions): Option[StructType] = {
if (inputPaths.nonEmpty) {
Some(inferSchema(sparkSession, inputPaths, parsedOptions))
} else {
None
}
}

protected def inferSchema(
sparkSession: SparkSession,
inputPaths: Seq[FileStatus],
parsedOptions: CSVOptions): StructType

/**
* Generates a header from the given row which is null-safe and duplicate-safe.
Expand Down Expand Up @@ -128,13 +139,13 @@ object TextInputCSVDataSource extends CSVDataSource {
UnivocityParser.parseIterator(lines, shouldDropHeader, parser)
}

override def infer(
override def inferSchema(
sparkSession: SparkSession,
inputPaths: Seq[FileStatus],
parsedOptions: CSVOptions): Option[StructType] = {
parsedOptions: CSVOptions): StructType = {
val csv = createBaseDataset(sparkSession, inputPaths, parsedOptions)
val maybeFirstLine = CSVUtils.filterCommentAndEmpty(csv, parsedOptions).take(1).headOption
Some(inferFromDataset(sparkSession, csv, maybeFirstLine, parsedOptions))
inferFromDataset(sparkSession, csv, maybeFirstLine, parsedOptions)
}

/**
Expand Down Expand Up @@ -199,10 +210,10 @@ object WholeFileCSVDataSource extends CSVDataSource {
parser)
}

override def infer(
override def inferSchema(
sparkSession: SparkSession,
inputPaths: Seq[FileStatus],
parsedOptions: CSVOptions): Option[StructType] = {
parsedOptions: CSVOptions): StructType = {
val csv = createBaseRdd(sparkSession, inputPaths, parsedOptions)
csv.flatMap { lines =>
UnivocityParser.tokenizeStream(
Expand All @@ -221,10 +232,10 @@ object WholeFileCSVDataSource extends CSVDataSource {
parsedOptions.headerFlag,
new CsvParser(parsedOptions.asParserSettings))
}
Some(CSVInferSchema.infer(tokenRDD, header, parsedOptions))
CSVInferSchema.infer(tokenRDD, header, parsedOptions)
case None =>
// If the first row could not be read, just return the empty schema.
Some(StructType(Nil))
StructType(Nil)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
sparkSession: SparkSession,
options: Map[String, String],
files: Seq[FileStatus]): Option[StructType] = {
require(files.nonEmpty, "Cannot infer schema from an empty set of files")

val parsedOptions =
new CSVOptions(options, sparkSession.sessionState.conf.sessionLocalTimeZone)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,9 +370,11 @@ class DataFrameReaderWriterSuite extends QueryTest with SharedSQLContext with Be
val schema = df.schema

// Reader, without user specified schema
intercept[IllegalArgumentException] {
val message = intercept[AnalysisException] {
testRead(spark.read.csv(), Seq.empty, schema)
}
}.getMessage
assert(message.contains("Unable to infer schema for CSV. It must be specified manually."))

testRead(spark.read.csv(dir), data, schema)
testRead(spark.read.csv(dir, dir), data ++ data, schema)
testRead(spark.read.csv(Seq(dir, dir): _*), data ++ data, schema)
Expand Down