Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -376,25 +376,7 @@ case class DataSource(

val (dataSchema, partitionSchema) = getOrInferFileFormatSchema(format)

val fileCatalog = if (sparkSession.sqlContext.conf.manageFilesourcePartitions &&
catalogTable.isDefined && catalogTable.get.tracksPartitionsInCatalog) {
val defaultTableSize = sparkSession.sessionState.conf.defaultSizeInBytes
new CatalogFileIndex(
sparkSession,
catalogTable.get,
catalogTable.get.stats.map(_.sizeInBytes.toLong).getOrElse(defaultTableSize))
} else {
new InMemoryFileIndex(sparkSession, globbedPaths, options, Some(partitionSchema))
}

HadoopFsRelation(
fileCatalog,
partitionSchema = partitionSchema,
dataSchema = dataSchema.asNullable,
bucketSpec = bucketSpec,
format,
caseInsensitiveOptions)(sparkSession)

createHadoopRelation(format, globbedPaths)
case _ =>
throw new AnalysisException(
s"$className is not a valid Spark SQL Data Source.")
Expand All @@ -403,6 +385,35 @@ case class DataSource(
relation
}

/**
* Creates Hadoop relation based on format and globbed file paths
* @param format format of the data source file
* @param globPaths Path to the file resolved by Hadoop library
* @return Hadoop relation object
*/
def createHadoopRelation(format: FileFormat,
globPaths: Array[Path]): BaseRelation = {
Copy link
Member

Choose a reason for hiding this comment

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

Let's make this inlined.

val (dataSchema, partitionSchema) = getOrInferFileFormatSchema(format)
Copy link
Member

Choose a reason for hiding this comment

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

You do twice getOrInferFileFormatSchema. One is before calling createHadoopRelation.

Copy link
Author

Choose a reason for hiding this comment

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

@viirya I will fix this. Looks like merge issue.
@maropu I will add tests.

val fileCatalog = if (sparkSession.sqlContext.conf.manageFilesourcePartitions &&
catalogTable.isDefined && catalogTable.get.tracksPartitionsInCatalog) {
val defaultTableSize = sparkSession.sessionState.conf.defaultSizeInBytes
new CatalogFileIndex(
sparkSession,
catalogTable.get,
catalogTable.get.stats.map(_.sizeInBytes.toLong).getOrElse(defaultTableSize))
} else {
new InMemoryFileIndex(sparkSession, globPaths, options, Some(partitionSchema))
}

HadoopFsRelation(
fileCatalog,
partitionSchema = partitionSchema,
dataSchema = dataSchema.asNullable,
bucketSpec = bucketSpec,
format,
caseInsensitiveOptions)(sparkSession)
}

/**
* Writes the given [[DataFrame]] out in this [[FileFormat]].
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
require(files.nonEmpty, "Cannot infer schema from an empty set of files")

val csvOptions = new CSVOptions(options, sparkSession.sessionState.conf.sessionLocalTimeZone)
val paths = files.map(_.getPath.toString)
val lines: Dataset[String] = createBaseDataset(sparkSession, csvOptions, paths)
val lines: Dataset[String] = createBaseDataset(sparkSession, csvOptions, files)
val caseSensitive = sparkSession.sessionState.conf.caseSensitiveAnalysis
Some(CSVInferSchema.infer(lines, caseSensitive, csvOptions))
}
Expand Down Expand Up @@ -128,14 +127,16 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
private def createBaseDataset(
sparkSession: SparkSession,
options: CSVOptions,
inputPaths: Seq[String]): Dataset[String] = {
inputPaths: Seq[FileStatus]): Dataset[String] = {
if (Charset.forName(options.charset) == StandardCharsets.UTF_8) {
// Fix for SPARK-19340. resolveRelation replaces with createHadoopRelation
// to avoid pattern resolution for already resolved file path
sparkSession.baseRelationToDataFrame(
DataSource.apply(
sparkSession,
paths = inputPaths,
paths = inputPaths.map(_.getPath().toString),
className = classOf[TextFileFormat].getName
).resolveRelation(checkFilesExist = false))
).createHadoopRelation(new TextFileFormat, inputPaths.map(_.getPath).toArray))
.select("value").as[String](Encoders.STRING)
} else {
val charset = options.charset
Expand Down