Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 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,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 @@ -211,7 +212,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 @@ -230,7 +232,7 @@ object TextInputCSVDataSource extends CSVDataSource {
CSVDataSource.checkHeader(
Copy link
Member

@HyukjinKwon HyukjinKwon Aug 20, 2018

Choose a reason for hiding this comment

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

Can we remove CSVDataSource.checkHeader and switch to CSVDataSource.checkHeaderColumnNames? Looks CSVDataSource.checkHeader is an overkill and makes hard to read the code.

header,
parser.tokenizer,
dataSchema,
if (columnPruning) requiredSchema else dataSchema,
file.filePath,
parser.options.enforceSchema,
caseSensitive)
Expand Down Expand Up @@ -308,10 +310,11 @@ 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 = {
CSVDataSource.checkHeaderColumnNames(
dataSchema,
if (columnPruning) requiredSchema else dataSchema,
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,25 @@ 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") {
withTempPath { path =>
val dir = path.getAbsolutePath
Seq(("a", "b")).toDF("columnA", "columnB").write
.format("csv")
.option("header", true)
.save(dir)
checkAnswer(spark.read
.format("csv")
.option("header", true)
.option("enforceSchema", false)
.load(dir)
.select("columnA"),
Copy link
Member

Choose a reason for hiding this comment

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

Could you check a corner case when required Schema is empty. For example, .option("enforceSchema", false) + count().

Row("a"))
}
}
}

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