-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-24676][SQL] Project required data from CSV parsed data when column pruning disabled #21657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,24 +38,28 @@ class UnivocityParser( | |
| requiredSchema: StructType, | ||
| val options: CSVOptions) extends Logging { | ||
| require(requiredSchema.toSet.subsetOf(dataSchema.toSet), | ||
| "requiredSchema should be the subset of schema.") | ||
| "requiredSchema should be the subset of dataSchema.") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: generally, we should consider printing out the schemas.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
|
|
||
| def this(schema: StructType, options: CSVOptions) = this(schema, schema, options) | ||
|
|
||
| // A `ValueConverter` is responsible for converting the given value to a desired type. | ||
| private type ValueConverter = String => Any | ||
|
|
||
| // This index is used to reorder parsed tokens | ||
| private val tokenIndexArr = | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you convert it to
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any side-effect?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have O(n) instead of O(1) for getting a value from the collection by an index.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I see. I'll recheck. Thanks!
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, fixed. |
||
| requiredSchema.map(f => java.lang.Integer.valueOf(dataSchema.indexOf(f))).toArray | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just in case, we can do an optimization by memory here. The array is used under the flag
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This array is used in both cases: line 56 (
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, I see |
||
|
|
||
| val tokenizer = { | ||
| val parserSetting = options.asParserSettings | ||
| if (options.columnPruning && requiredSchema.length < dataSchema.length) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can be simplified to
|
||
| val tokenIndexArr = requiredSchema.map(f => java.lang.Integer.valueOf(dataSchema.indexOf(f))) | ||
| parserSetting.selectIndexes(tokenIndexArr: _*) | ||
| } | ||
| new CsvParser(parserSetting) | ||
| } | ||
| private val schema = if (options.columnPruning) requiredSchema else dataSchema | ||
|
|
||
| private val row = new GenericInternalRow(schema.length) | ||
| private val parsedSchema = if (options.columnPruning) requiredSchema else dataSchema | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
|
|
||
| private val row = new GenericInternalRow(requiredSchema.length) | ||
|
|
||
| // Retrieve the raw record string. | ||
| private def getCurrentInput: UTF8String = { | ||
|
|
@@ -82,7 +86,7 @@ class UnivocityParser( | |
| // | ||
| // output row - ["A", 2] | ||
| private val valueConverters: Array[ValueConverter] = { | ||
| schema.map(f => makeConverter(f.name, f.dataType, f.nullable, options)).toArray | ||
| requiredSchema.map(f => makeConverter(f.name, f.dataType, f.nullable, options)).toArray | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -183,7 +187,7 @@ class UnivocityParser( | |
| } | ||
| } | ||
|
|
||
| private val doParse = if (schema.nonEmpty) { | ||
| private val doParse = if (requiredSchema.nonEmpty) { | ||
| (input: String) => convert(tokenizer.parseLine(input)) | ||
| } else { | ||
| // If `columnPruning` enabled and partition attributes scanned only, | ||
|
|
@@ -197,15 +201,21 @@ class UnivocityParser( | |
| */ | ||
| def parse(input: String): InternalRow = doParse(input) | ||
|
|
||
| private val getToken = if (options.columnPruning) { | ||
| (tokens: Array[String], index: Int) => tokens(index) | ||
| } else { | ||
| (tokens: Array[String], index: Int) => tokens(tokenIndexArr(index)) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| } | ||
|
|
||
| private def convert(tokens: Array[String]): InternalRow = { | ||
| if (tokens.length != schema.length) { | ||
| if (tokens.length != parsedSchema.length) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If possible, could you add a test case that satisfy
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will do |
||
| // If the number of tokens doesn't match the schema, we should treat it as a malformed record. | ||
| // However, we still have chance to parse some of the tokens, by adding extra null tokens in | ||
| // the tail if the number is smaller, or by dropping extra tokens if the number is larger. | ||
| val checkedTokens = if (schema.length > tokens.length) { | ||
| tokens ++ new Array[String](schema.length - tokens.length) | ||
| val checkedTokens = if (parsedSchema.length > tokens.length) { | ||
| tokens ++ new Array[String](parsedSchema.length - tokens.length) | ||
| } else { | ||
| tokens.take(schema.length) | ||
| tokens.take(parsedSchema.length) | ||
| } | ||
| def getPartialResult(): Option[InternalRow] = { | ||
| try { | ||
|
|
@@ -223,8 +233,8 @@ class UnivocityParser( | |
| } else { | ||
| try { | ||
| var i = 0 | ||
| while (i < schema.length) { | ||
| row(i) = valueConverters(i).apply(tokens(i)) | ||
| while (i < requiredSchema.length) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add the comment like
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| row(i) = valueConverters(i).apply(getToken(tokens, i)) | ||
| i += 1 | ||
| } | ||
| row | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1579,4 +1579,20 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils with Te | |
| } | ||
| } | ||
| } | ||
|
|
||
| test("SPARK-24676 project required data from parsed data when columnPruning disabled") { | ||
| withSQLConf(SQLConf.CSV_PARSER_COLUMN_PRUNING.key -> "false") { | ||
| withTempPath { path => | ||
| val dir = path.getAbsolutePath | ||
| spark.range(10).selectExpr("id % 2 AS p", "id AS c0", "id AS c1").write.partitionBy("p") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just in case, if required schema is empty, the fix works too?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| .option("header", "true").csv(dir) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "true" -> true ?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
| var df = spark.read.option("header", true).csv(dir).selectExpr("sum(p)", "count(c0)") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normally, we do not use |
||
| checkAnswer(df, Row(5, 10)) | ||
|
|
||
| // empty required column case | ||
| df = spark.read.option("header", true).csv(dir).selectExpr("sum(p)") | ||
| checkAnswer(df, Row(5)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add the parameter descriptions of
dataSchemaandrequiredSchemaabove classUnivocityParser?