Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -505,10 +505,11 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging {
StructType(schema.filterNot(_.name == parsedOptions.columnNameOfCorruptRecord))

val linesWithoutHeader: RDD[String] = maybeFirstLine.map { firstLine =>
CSVDataSource.checkHeader(
firstLine,
new CsvParser(parsedOptions.asParserSettings),
val parser = new CsvParser(parsedOptions.asParserSettings)
val columnNames = parser.parseLine(firstLine)
CSVDataSource.checkHeaderColumnNames(
actualSchema,
columnNames,
csvDataset.getClass.getCanonicalName,
parsedOptions.enforceSchema,
sparkSession.sessionState.conf.caseSensitiveAnalysis)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ abstract class CSVDataSource extends Serializable {
requiredSchema: StructType,
// Actual schema of data in the csv file
dataSchema: StructType,
caseSensitive: Boolean): Iterator[InternalRow]
caseSensitive: Boolean,
columnPruning: Boolean): Iterator[InternalRow]

/**
* Infers the schema from `inputPaths` files.
Expand Down Expand Up @@ -181,25 +182,6 @@ object CSVDataSource extends Logging {
}
}
}

/**
* Checks that CSV header contains the same column names as fields names in the given schema
* by taking into account case sensitivity.
*/
def checkHeader(
header: String,
parser: CsvParser,
schema: StructType,
fileName: String,
enforceSchema: Boolean,
caseSensitive: Boolean): Unit = {
checkHeaderColumnNames(
schema,
parser.parseLine(header),
fileName,
enforceSchema,
caseSensitive)
}
}

object TextInputCSVDataSource extends CSVDataSource {
Expand All @@ -211,7 +193,8 @@ object TextInputCSVDataSource extends CSVDataSource {
parser: UnivocityParser,
requiredSchema: StructType,
dataSchema: StructType,
caseSensitive: Boolean): Iterator[InternalRow] = {
caseSensitive: Boolean,
columnPruning: Boolean): Iterator[InternalRow] = {
val lines = {
val linesReader = new HadoopFileLinesReader(file, conf)
Option(TaskContext.get()).foreach(_.addTaskCompletionListener[Unit](_ => linesReader.close()))
Expand All @@ -227,10 +210,11 @@ object TextInputCSVDataSource extends CSVDataSource {
// Note: if there are only comments in the first block, the header would probably
// be not extracted.
CSVUtils.extractHeader(lines, parser.options).foreach { header =>
CSVDataSource.checkHeader(
header,
parser.tokenizer,
dataSchema,
val schema = if (columnPruning) requiredSchema else dataSchema
val columnNames = parser.tokenizer.parseLine(header)
CSVDataSource.checkHeaderColumnNames(
schema,
columnNames,
file.filePath,
parser.options.enforceSchema,
caseSensitive)
Expand Down Expand Up @@ -308,10 +292,12 @@ object MultiLineCSVDataSource extends CSVDataSource {
parser: UnivocityParser,
requiredSchema: StructType,
dataSchema: StructType,
caseSensitive: Boolean): Iterator[InternalRow] = {
caseSensitive: Boolean,
columnPruning: Boolean): Iterator[InternalRow] = {
def checkHeader(header: Array[String]): Unit = {
val schema = if (columnPruning) requiredSchema else dataSchema
CSVDataSource.checkHeaderColumnNames(
dataSchema,
schema,
header,
file.filePath,
parser.options.enforceSchema,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
)
}
val caseSensitive = sparkSession.sessionState.conf.caseSensitiveAnalysis
val columnPruning = sparkSession.sessionState.conf.csvColumnPruning

(file: PartitionedFile) => {
val conf = broadcastedHadoopConf.value.value
Expand All @@ -144,7 +145,8 @@ class CSVFileFormat extends TextBasedFileFormat with DataSourceRegister {
parser,
requiredSchema,
dataSchema,
caseSensitive)
caseSensitive,
columnPruning)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1603,6 +1603,39 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils with Te
.exists(msg => msg.getRenderedMessage.contains("CSV header does not conform to the schema")))
}

test("SPARK-25134: check header on parsing of dataset with projection and column pruning") {
Copy link
Member

Choose a reason for hiding this comment

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

Also need a test case for checking enforceSchema works well when column pruning is on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it seems enforceSchema always kind of "works" because it simply means it ignores the headers.
what do we expect to verify in the test?

withSQLConf(SQLConf.CSV_PARSER_COLUMN_PRUNING.key -> "true") {
Seq(false, true).foreach { multiLine =>
withTempPath { path =>
val dir = path.getAbsolutePath
Seq(("a", "b")).toDF("columnA", "columnB").write
.format("csv")
.option("header", true)
.save(dir)

// schema with one column
checkAnswer(spark.read
.format("csv")
.option("header", true)
.option("enforceSchema", false)
.option("multiLine", multiLine)
.load(dir)
.select("columnA"),
Row("a"))

// empty schema
assert(spark.read
.format("csv")
.option("header", true)
.option("enforceSchema", false)
.option("multiLine", multiLine)
.load(dir)
.count() === 1L)
}
}
}
}

test("SPARK-24645 skip parsing when columnPruning enabled and partitions scanned only") {
withSQLConf(SQLConf.CSV_PARSER_COLUMN_PRUNING.key -> "true") {
withTempPath { path =>
Expand Down