Skip to content

[SPARK-26745][SQL] Revert count optimization in JSON datasource by SPARK-24959 #23667

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

Closed
wants to merge 1 commit into from
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 @@ -188,11 +188,19 @@ class UnivocityParser(
}
}

private val doParse = if (requiredSchema.nonEmpty) {
(input: String) => convert(tokenizer.parseLine(input))
} else {
// If `columnPruning` enabled and partition attributes scanned only,
// `schema` gets empty.
(_: String) => InternalRow.empty
Copy link
Contributor

Choose a reason for hiding this comment

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

It's too long ago and I can't remember the details. Does it mean we still have this count optimization for CSV? does it work in multiline mode?

Copy link
Member Author

@HyukjinKwon HyukjinKwon Jan 31, 2019

Choose a reason for hiding this comment

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

Yes, it does for CSV when multiline is off and, for miltiline mode it executes a different code path.

UnivocityParser.parseStream -> UnivocityParser.convert

}

/**
* Parses a single CSV string and turns it into either one resulting row or no row (if the
* the record is malformed).
*/
def parse(input: String): InternalRow = convert(tokenizer.parseLine(input))
def parse(input: String): InternalRow = doParse(input)

private val getToken = if (options.columnPruning) {
(tokens: Array[String], index: Int) => tokens(index)
Expand Down Expand Up @@ -282,8 +290,7 @@ private[sql] object UnivocityParser {
input => Seq(parser.convert(input)),
parser.options.parseMode,
schema,
parser.options.columnNameOfCorruptRecord,
parser.options.multiLine)
parser.options.columnNameOfCorruptRecord)

val handleHeader: () => Unit =
() => headerChecker.checkHeaderColumnNames(tokenizer)
Expand Down Expand Up @@ -336,8 +343,7 @@ private[sql] object UnivocityParser {
input => Seq(parser.parse(input)),
parser.options.parseMode,
schema,
parser.options.columnNameOfCorruptRecord,
parser.options.multiLine)
parser.options.columnNameOfCorruptRecord)
filteredLines.flatMap(safeParser.parse)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ case class CsvToStructs(
input => Seq(rawParser.parse(input)),
mode,
nullableSchema,
parsedOptions.columnNameOfCorruptRecord,
parsedOptions.multiLine)
parsedOptions.columnNameOfCorruptRecord)
}

override def dataType: DataType = nullableSchema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,8 +582,7 @@ case class JsonToStructs(
input => rawParser.parse(input, createParser, identity[UTF8String]),
mode,
parserSchema,
parsedOptions.columnNameOfCorruptRecord,
parsedOptions.multiLine)
parsedOptions.columnNameOfCorruptRecord)
}

override def dataType: DataType = nullableSchema
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ class FailureSafeParser[IN](
rawParser: IN => Seq[InternalRow],
mode: ParseMode,
schema: StructType,
columnNameOfCorruptRecord: String,
isMultiLine: Boolean) {
columnNameOfCorruptRecord: String) {

private val corruptFieldIndex = schema.getFieldIndex(columnNameOfCorruptRecord)
private val actualSchema = StructType(schema.filterNot(_.name == columnNameOfCorruptRecord))
Expand Down Expand Up @@ -56,15 +55,9 @@ class FailureSafeParser[IN](
}
}

private val skipParsing = !isMultiLine && mode == PermissiveMode && schema.isEmpty

def parse(input: IN): Iterator[InternalRow] = {
try {
if (skipParsing) {
Iterator.single(InternalRow.empty)
} else {
rawParser.apply(input).toIterator.map(row => toResultRow(Some(row), () => null))
}
rawParser.apply(input).toIterator.map(row => toResultRow(Some(row), () => null))
} catch {
case e: BadRecordException => mode match {
case PermissiveMode =>
Expand Down
1 change: 0 additions & 1 deletion sql/core/benchmarks/JSONBenchmark-results.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Select a subset of 10 columns: Best/Avg Time(ms) Rate(M/s) Per Ro
------------------------------------------------------------------------------------------------
Select 10 columns + count() 19539 / 19896 0.5 1953.9 1.0X
Select 1 column + count() 16412 / 16445 0.6 1641.2 1.2X
count() 2783 / 2801 3.6 278.3 7.0X

Preparing data for benchmarking ...
Java HotSpot(TM) 64-Bit Server VM 1.8.0_191-b12 on Linux 3.16.0-31-generic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,7 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging {
input => rawParser.parse(input, createParser, UTF8String.fromString),
parsedOptions.parseMode,
schema,
parsedOptions.columnNameOfCorruptRecord,
parsedOptions.multiLine)
parsedOptions.columnNameOfCorruptRecord)
iter.flatMap(parser.parse)
}
sparkSession.internalCreateDataFrame(parsed, schema, isStreaming = jsonDataset.isStreaming)
Expand Down Expand Up @@ -538,8 +537,7 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging {
input => Seq(rawParser.parse(input)),
parsedOptions.parseMode,
schema,
parsedOptions.columnNameOfCorruptRecord,
parsedOptions.multiLine)
parsedOptions.columnNameOfCorruptRecord)
iter.flatMap(parser.parse)
}
sparkSession.internalCreateDataFrame(parsed, schema, isStreaming = csvDataset.isStreaming)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,7 @@ object TextInputJsonDataSource extends JsonDataSource {
input => parser.parse(input, textParser, textToUTF8String),
parser.options.parseMode,
schema,
parser.options.columnNameOfCorruptRecord,
parser.options.multiLine)
parser.options.columnNameOfCorruptRecord)
linesReader.flatMap(safeParser.parse)
}

Expand Down Expand Up @@ -225,8 +224,7 @@ object MultiLineJsonDataSource extends JsonDataSource {
input => parser.parse[InputStream](input, streamParser, partitionedFileString),
parser.options.parseMode,
schema,
parser.options.columnNameOfCorruptRecord,
parser.options.multiLine)
parser.options.columnNameOfCorruptRecord)

safeParser.parse(
CodecStreams.createInputStreamWithCloseResource(conf, new Path(new URI(file.filePath))))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,6 @@ object JSONBenchmark extends SqlBasedBenchmark {
benchmark.addCase(s"Select 1 column + count()", numIters) { _ =>
ds.select($"col1").filter((_: Row) => true).count()
}
benchmark.addCase(s"count()", numIters) { _ =>
ds.count()
}

benchmark.run()
}
Expand Down