You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "pan3793 (via GitHub)" <gi...@apache.org> on 2025/09/18 08:31:20 UTC
[PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
pan3793 opened a new pull request, #52384:
URL: https://github.com/apache/spark/pull/52384
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines: https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR: https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a faster review.
7. If you want to add a new configuration, please read the guideline first for naming configurations in
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
Reuse InputStream in vectorized Parquet reader between reading the footer and row groups, on the executor side.
This PR is part of SPARK-52011, you can check more details at https://github.com/apache/spark/pull/50765
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
Reduce unnecessary RPCs of NameNode to improve performance and stability for large Hadoop clusters.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as new features, bug fixes, or other behavior changes. Documentation-only updates are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes - provide the console output, description and/or an example to show the behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No.
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some test cases that check the changes thoroughly including negative and positive cases if possible.
If it was tested in a way different from regular unit tests, please clarify how you tested step by step, ideally copy and paste-able, so that other reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions for the consistent environment, and the instructions could accord to: https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
See https://github.com/apache/spark/pull/50765
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
No.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361989186
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
@cloud-fan I created a `case class OpenedParquetFooter` to replace the tuple, please let me know if you have better idea.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "sunchao (via GitHub)" <gi...@apache.org>.
sunchao commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2370445101
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,150 +214,199 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
- // When there are vectorized reads, we can avoid reading the footer twice by reading
- // all row groups in advance and filter row groups according to filters that require
- // push down (no need to read the footer metadata again).
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.WITH_ROW_GROUPS)
- } else {
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.SKIP_ROW_GROUPS)
- }
-
- val footerFileMetaData = fileFooter.getFileMetaData
- val datetimeRebaseSpec = DataSourceUtils.datetimeRebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get,
- datetimeRebaseModeInRead)
- val int96RebaseSpec = DataSourceUtils.int96RebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get, int96RebaseModeInRead)
-
- // Try to push down filters when filter push-down is enabled.
- val pushed = if (enableParquetFilterPushDown) {
- val parquetSchema = footerFileMetaData.getSchema
- val parquetFilters = new ParquetFilters(
- parquetSchema,
- pushDownDate,
- pushDownTimestamp,
- pushDownDecimal,
- pushDownStringPredicate,
- pushDownInFilterThreshold,
- isCaseSensitive,
- datetimeRebaseSpec)
- filters
- // Collects all converted Parquet filter predicates. Notice that not all predicates can be
- // converted (`ParquetFilters.createFilter` returns an `Option`). That's why a `flatMap`
- // is used here.
- .flatMap(parquetFilters.createFilter(_))
- .reduceOption(FilterApi.and)
- } else {
- None
- }
-
- // PARQUET_INT96_TIMESTAMP_CONVERSION says to apply timezone conversions to int96 timestamps'
- // *only* if the file was created by something other than "parquet-mr", so check the actual
- // writer here for this file. We have to do this per-file, as each file in the table may
- // have different writers.
- // Define isCreatedByParquetMr as function to avoid unnecessary parquet footer reads.
- def isCreatedByParquetMr: Boolean =
- footerFileMetaData.getCreatedBy().startsWith("parquet-mr")
-
- val convertTz =
- if (timestampConversion && !isCreatedByParquetMr) {
- Some(DateTimeUtils.getZoneId(sharedConf.get(SQLConf.SESSION_LOCAL_TIMEZONE.key)))
+ // When there are vectorized reads, we can avoid
+ // 1. opening the file twice by transfering the SeekableInputStream
+ // 2. reading the footer twice by reading all row groups in advance and filter row groups
+ // according to filters that require push down
+ val openedFooter =
+ ParquetFooterReader.openFileAndReadFooter(sharedConf, file, enableVectorizedReader)
+ assert(openedFooter.inputStreamOpt.isPresent == enableVectorizedReader)
+
+ // Before transferring the ownership of inputStream to the vectorizedReader,
+ // we must take responsibility to close the inputStream if something goes wrong
+ // to avoid resource leak.
+ val shouldCloseInputStream = new AtomicBoolean(openedFooter.inputStreamOpt.isPresent)
Review Comment:
curious why this needs to be an `AtomicBoolean`? also is an boolean flag needed? can we just do
```
openedFooter.inputStreamOpt.ifPresent(Utils.closeQuietly)
```
##########
sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.java:
##########
@@ -37,53 +36,71 @@
*/
public class ParquetFooterReader {
- public static final boolean SKIP_ROW_GROUPS = true;
- public static final boolean WITH_ROW_GROUPS = false;
-
/**
- * Reads footer for the input Parquet file 'split'. If 'skipRowGroup' is true,
- * this will skip reading the Parquet row group metadata.
+ * Build a filter for reading footer of the input Parquet file 'split'.
+ * If 'skipRowGroup' is true, this will skip reading the Parquet row group metadata.
*
* @param file a part (i.e. "block") of a single file that should be read
- * @param configuration hadoop configuration of file
+ * @param hadoopConf hadoop configuration of file
* @param skipRowGroup If true, skip reading row groups;
* if false, read row groups according to the file split range
*/
- public static ParquetMetadata readFooter(
- Configuration configuration,
- PartitionedFile file,
- boolean skipRowGroup) throws IOException {
- long fileStart = file.start();
- ParquetMetadataConverter.MetadataFilter filter;
+ public static ParquetMetadataConverter.MetadataFilter buildFilter(
+ Configuration hadoopConf, PartitionedFile file, boolean skipRowGroup) {
if (skipRowGroup) {
- filter = ParquetMetadataConverter.SKIP_ROW_GROUPS;
+ return ParquetMetadataConverter.SKIP_ROW_GROUPS;
} else {
- filter = HadoopReadOptions.builder(configuration, file.toPath())
+ long fileStart = file.start();
+ return HadoopReadOptions.builder(hadoopConf, file.toPath())
.withRange(fileStart, fileStart + file.length())
.build()
.getMetadataFilter();
}
- return readFooter(configuration, file.toPath(), filter);
- }
-
- public static ParquetMetadata readFooter(Configuration configuration,
- Path file, ParquetMetadataConverter.MetadataFilter filter) throws IOException {
- return readFooter(HadoopInputFile.fromPath(file, configuration), filter);
- }
-
- public static ParquetMetadata readFooter(Configuration configuration,
- FileStatus fileStatus, ParquetMetadataConverter.MetadataFilter filter) throws IOException {
- return readFooter(HadoopInputFile.fromStatus(fileStatus, configuration), filter);
}
- private static ParquetMetadata readFooter(HadoopInputFile inputFile,
+ public static ParquetMetadata readFooter(
+ HadoopInputFile inputFile,
ParquetMetadataConverter.MetadataFilter filter) throws IOException {
- ParquetReadOptions readOptions =
- HadoopReadOptions.builder(inputFile.getConfiguration(), inputFile.getPath())
+ ParquetReadOptions readOptions = HadoopReadOptions
+ .builder(inputFile.getConfiguration(), inputFile.getPath())
.withMetadataFilter(filter).build();
- // Use try-with-resources to ensure fd is closed.
- try (ParquetFileReader fileReader = ParquetFileReader.open(inputFile, readOptions)) {
+ try (var fileReader = ParquetFileReader.open(inputFile, readOptions)) {
return fileReader.getFooter();
}
}
+
+ /**
+ * Decoding Parquet files generally involves two steps:
+ * 1. read and resolve the metadata (footer),
+ * 2. read and decode the row groups/column chunks.
+ * <p>
+ * It's possible to avoid opening the file twice by resuing the SeekableInputStream.
+ * When keepInputStreamOpen is true, the caller takes responsibility to close the
+ * SeekableInputStream. Currently, this is only supported by parquet vectorized reader.
+ *
+ * @param hadoopConf hadoop configuration of file
+ * @param file a part (i.e. "block") of a single file that should be read
+ * @param keepInputStreamOpen when true, keep the SeekableInputStream of file being open
+ * @return if keepInputStreamOpen is true, the returned OpenedParquetFooter carries
+ * Some(SeekableInputStream), otherwise None.
+ */
+ public static OpenedParquetFooter openFileAndReadFooter(
+ Configuration hadoopConf,
+ PartitionedFile file,
+ boolean keepInputStreamOpen) throws IOException {
+ var readOptions = HadoopReadOptions.builder(hadoopConf, file.toPath())
+ .withMetadataFilter(buildFilter(hadoopConf, file, !keepInputStreamOpen))
Review Comment:
maybe worth adding some comments here to explain why we choose to skip row groups when `keepInputStreamOpen` is false
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,150 +214,199 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
- // When there are vectorized reads, we can avoid reading the footer twice by reading
- // all row groups in advance and filter row groups according to filters that require
- // push down (no need to read the footer metadata again).
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.WITH_ROW_GROUPS)
- } else {
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.SKIP_ROW_GROUPS)
- }
-
- val footerFileMetaData = fileFooter.getFileMetaData
- val datetimeRebaseSpec = DataSourceUtils.datetimeRebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get,
- datetimeRebaseModeInRead)
- val int96RebaseSpec = DataSourceUtils.int96RebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get, int96RebaseModeInRead)
-
- // Try to push down filters when filter push-down is enabled.
- val pushed = if (enableParquetFilterPushDown) {
- val parquetSchema = footerFileMetaData.getSchema
- val parquetFilters = new ParquetFilters(
- parquetSchema,
- pushDownDate,
- pushDownTimestamp,
- pushDownDecimal,
- pushDownStringPredicate,
- pushDownInFilterThreshold,
- isCaseSensitive,
- datetimeRebaseSpec)
- filters
- // Collects all converted Parquet filter predicates. Notice that not all predicates can be
- // converted (`ParquetFilters.createFilter` returns an `Option`). That's why a `flatMap`
- // is used here.
- .flatMap(parquetFilters.createFilter(_))
- .reduceOption(FilterApi.and)
- } else {
- None
- }
-
- // PARQUET_INT96_TIMESTAMP_CONVERSION says to apply timezone conversions to int96 timestamps'
- // *only* if the file was created by something other than "parquet-mr", so check the actual
- // writer here for this file. We have to do this per-file, as each file in the table may
- // have different writers.
- // Define isCreatedByParquetMr as function to avoid unnecessary parquet footer reads.
- def isCreatedByParquetMr: Boolean =
- footerFileMetaData.getCreatedBy().startsWith("parquet-mr")
-
- val convertTz =
- if (timestampConversion && !isCreatedByParquetMr) {
- Some(DateTimeUtils.getZoneId(sharedConf.get(SQLConf.SESSION_LOCAL_TIMEZONE.key)))
+ // When there are vectorized reads, we can avoid
+ // 1. opening the file twice by transfering the SeekableInputStream
+ // 2. reading the footer twice by reading all row groups in advance and filter row groups
+ // according to filters that require push down
+ val openedFooter =
+ ParquetFooterReader.openFileAndReadFooter(sharedConf, file, enableVectorizedReader)
+ assert(openedFooter.inputStreamOpt.isPresent == enableVectorizedReader)
+
+ // Before transferring the ownership of inputStream to the vectorizedReader,
+ // we must take responsibility to close the inputStream if something goes wrong
+ // to avoid resource leak.
+ val shouldCloseInputStream = new AtomicBoolean(openedFooter.inputStreamOpt.isPresent)
+ try {
+ val footerFileMetaData = openedFooter.footer.getFileMetaData
+ val datetimeRebaseSpec = DataSourceUtils.datetimeRebaseSpec(
+ footerFileMetaData.getKeyValueMetaData.get,
+ datetimeRebaseModeInRead)
+ val int96RebaseSpec = DataSourceUtils.int96RebaseSpec(
+ footerFileMetaData.getKeyValueMetaData.get, int96RebaseModeInRead)
+
+ // Try to push down filters when filter push-down is enabled.
+ val pushed = if (enableParquetFilterPushDown) {
+ val parquetSchema = footerFileMetaData.getSchema
+ val parquetFilters = new ParquetFilters(
+ parquetSchema,
+ pushDownDate,
+ pushDownTimestamp,
+ pushDownDecimal,
+ pushDownStringPredicate,
+ pushDownInFilterThreshold,
+ isCaseSensitive,
+ datetimeRebaseSpec)
+ filters
+ // Collects all converted Parquet filter predicates. Notice that not all predicates
+ // can be converted (`ParquetFilters.createFilter` returns an `Option`). That's why
+ // a `flatMap` is used here.
+ .flatMap(parquetFilters.createFilter)
+ .reduceOption(FilterApi.and)
} else {
None
}
-
- val attemptId = new TaskAttemptID(new TaskID(new JobID(), TaskType.MAP, 0), 0)
- val hadoopAttemptContext =
- new TaskAttemptContextImpl(broadcastedHadoopConf.value.value, attemptId)
-
- // Try to push down filters when filter push-down is enabled.
- // Notice: This push-down is RowGroups level, not individual records.
- if (pushed.isDefined) {
- ParquetInputFormat.setFilterPredicate(hadoopAttemptContext.getConfiguration, pushed.get)
- }
- val taskContext = Option(TaskContext.get())
- if (enableVectorizedReader) {
- val vectorizedReader = new VectorizedParquetRecordReader(
- convertTz.orNull,
- datetimeRebaseSpec.mode.toString,
- datetimeRebaseSpec.timeZone,
- int96RebaseSpec.mode.toString,
- int96RebaseSpec.timeZone,
- enableOffHeapColumnVector && taskContext.isDefined,
- capacity)
- // SPARK-37089: We cannot register a task completion listener to close this iterator here
- // because downstream exec nodes have already registered their listeners. Since listeners
- // are executed in reverse order of registration, a listener registered here would close the
- // iterator while downstream exec nodes are still running. When off-heap column vectors are
- // enabled, this can cause a use-after-free bug leading to a segfault.
- //
- // Instead, we use FileScanRDD's task completion listener to close this iterator.
- val iter = new RecordReaderIterator(vectorizedReader)
- try {
- vectorizedReader.initialize(split, hadoopAttemptContext, Option.apply(fileFooter))
- logDebug(s"Appending $partitionSchema ${file.partitionValues}")
- vectorizedReader.initBatch(partitionSchema, file.partitionValues)
- if (returningBatch) {
- vectorizedReader.enableReturningBatches()
+ // PARQUET_INT96_TIMESTAMP_CONVERSION says to apply timezone conversions to int96
+ // timestamps *only* if the file was created by something other than "parquet-mr",
+ // so check the actual writer here for this file. We have to do this per-file,
+ // as each file in the table may have different writers.
+ // Define isCreatedByParquetMr as function to avoid unnecessary parquet footer reads.
+ def isCreatedByParquetMr: Boolean =
+ footerFileMetaData.getCreatedBy.startsWith("parquet-mr")
+
+ val convertTz =
+ if (timestampConversion && !isCreatedByParquetMr) {
+ Some(DateTimeUtils.getZoneId(sharedConf.get(SQLConf.SESSION_LOCAL_TIMEZONE.key)))
+ } else {
+ None
}
- // UnsafeRowParquetRecordReader appends the columns internally to avoid another copy.
- iter.asInstanceOf[Iterator[InternalRow]]
- } catch {
- case e: Throwable =>
- // SPARK-23457: In case there is an exception in initialization, close the iterator to
- // avoid leaking resources.
- iter.close()
- throw e
+ val attemptId = new TaskAttemptID(new TaskID(new JobID(), TaskType.MAP, 0), 0)
+ val hadoopAttemptContext =
+ new TaskAttemptContextImpl(broadcastedHadoopConf.value.value, attemptId)
+
+ // Try to push down filters when filter push-down is enabled.
+ // Notice: This push-down is RowGroups level, not individual records.
+ pushed.foreach {
+ ParquetInputFormat.setFilterPredicate(hadoopAttemptContext.getConfiguration, _)
}
- } else {
- logDebug(s"Falling back to parquet-mr")
- // ParquetRecordReader returns InternalRow
- val readSupport = new ParquetReadSupport(
- convertTz,
- enableVectorizedReader = false,
- datetimeRebaseSpec,
- int96RebaseSpec)
- val reader = if (pushed.isDefined && enableRecordFilter) {
- val parquetFilter = FilterCompat.get(pushed.get, null)
- new ParquetRecordReader[InternalRow](readSupport, parquetFilter)
+ if (enableVectorizedReader) {
+ buildVectorizedIterator(
+ hadoopAttemptContext, split, file.partitionValues, partitionSchema, convertTz,
+ datetimeRebaseSpec, int96RebaseSpec, enableOffHeapColumnVector, returningBatch,
+ capacity, openedFooter, shouldCloseInputStream)
} else {
- new ParquetRecordReader[InternalRow](readSupport)
+ logDebug(s"Falling back to parquet-mr")
+ buildRowBasedIterator(
+ hadoopAttemptContext, split, file.partitionValues, partitionSchema, convertTz,
+ datetimeRebaseSpec, int96RebaseSpec, requiredSchema, pushed, enableRecordFilter)
}
- val readerWithRowIndexes = ParquetRowIndexUtil.addRowIndexToRecordReaderIfNeeded(reader,
- requiredSchema)
- val iter = new RecordReaderIterator[InternalRow](readerWithRowIndexes)
- try {
- readerWithRowIndexes.initialize(split, hadoopAttemptContext)
-
- val fullSchema = toAttributes(requiredSchema) ++ toAttributes(partitionSchema)
- val unsafeProjection = GenerateUnsafeProjection.generate(fullSchema, fullSchema)
-
- if (partitionSchema.length == 0) {
- // There is no partition columns
- iter.map(unsafeProjection)
- } else {
- val joinedRow = new JoinedRow()
- iter.map(d => unsafeProjection(joinedRow(d, file.partitionValues)))
- }
- } catch {
- case e: Throwable =>
- // SPARK-23457: In case there is an exception in initialization, close the iterator to
- // avoid leaking resources.
- iter.close()
- throw e
+ } finally {
+ if (shouldCloseInputStream.get) {
+ openedFooter.inputStreamOpt.ifPresent(Utils.closeQuietly)
}
}
}
}
+ // scalastyle:off argcount
Review Comment:
I assume the following changes are just refactoring?
##########
sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.java:
##########
@@ -37,53 +36,71 @@
*/
public class ParquetFooterReader {
- public static final boolean SKIP_ROW_GROUPS = true;
- public static final boolean WITH_ROW_GROUPS = false;
-
/**
- * Reads footer for the input Parquet file 'split'. If 'skipRowGroup' is true,
- * this will skip reading the Parquet row group metadata.
+ * Build a filter for reading footer of the input Parquet file 'split'.
Review Comment:
nit: I think the doc is out-dated - there is no `'split'`
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2370829017
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,150 +214,199 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
- // When there are vectorized reads, we can avoid reading the footer twice by reading
- // all row groups in advance and filter row groups according to filters that require
- // push down (no need to read the footer metadata again).
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.WITH_ROW_GROUPS)
- } else {
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.SKIP_ROW_GROUPS)
- }
-
- val footerFileMetaData = fileFooter.getFileMetaData
- val datetimeRebaseSpec = DataSourceUtils.datetimeRebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get,
- datetimeRebaseModeInRead)
- val int96RebaseSpec = DataSourceUtils.int96RebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get, int96RebaseModeInRead)
-
- // Try to push down filters when filter push-down is enabled.
- val pushed = if (enableParquetFilterPushDown) {
- val parquetSchema = footerFileMetaData.getSchema
- val parquetFilters = new ParquetFilters(
- parquetSchema,
- pushDownDate,
- pushDownTimestamp,
- pushDownDecimal,
- pushDownStringPredicate,
- pushDownInFilterThreshold,
- isCaseSensitive,
- datetimeRebaseSpec)
- filters
- // Collects all converted Parquet filter predicates. Notice that not all predicates can be
- // converted (`ParquetFilters.createFilter` returns an `Option`). That's why a `flatMap`
- // is used here.
- .flatMap(parquetFilters.createFilter(_))
- .reduceOption(FilterApi.and)
- } else {
- None
- }
-
- // PARQUET_INT96_TIMESTAMP_CONVERSION says to apply timezone conversions to int96 timestamps'
- // *only* if the file was created by something other than "parquet-mr", so check the actual
- // writer here for this file. We have to do this per-file, as each file in the table may
- // have different writers.
- // Define isCreatedByParquetMr as function to avoid unnecessary parquet footer reads.
- def isCreatedByParquetMr: Boolean =
- footerFileMetaData.getCreatedBy().startsWith("parquet-mr")
-
- val convertTz =
- if (timestampConversion && !isCreatedByParquetMr) {
- Some(DateTimeUtils.getZoneId(sharedConf.get(SQLConf.SESSION_LOCAL_TIMEZONE.key)))
+ // When there are vectorized reads, we can avoid
+ // 1. opening the file twice by transfering the SeekableInputStream
+ // 2. reading the footer twice by reading all row groups in advance and filter row groups
+ // according to filters that require push down
+ val openedFooter =
+ ParquetFooterReader.openFileAndReadFooter(sharedConf, file, enableVectorizedReader)
+ assert(openedFooter.inputStreamOpt.isPresent == enableVectorizedReader)
+
+ // Before transferring the ownership of inputStream to the vectorizedReader,
+ // we must take responsibility to close the inputStream if something goes wrong
+ // to avoid resource leak.
+ val shouldCloseInputStream = new AtomicBoolean(openedFooter.inputStreamOpt.isPresent)
Review Comment:
we pass `shouldCloseInputStream` to `def buildVectorizedIterator` and the flag will be updated by that method, so we must use a reference instead of a primitive type.
the suggestion works but will introduce many unnecessary `close()` in normal cases, so I add a flag to avoid that as much as possible.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2368216840
##########
sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/OpenedParquetFooter.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet;
+
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.SeekableInputStream;
+
+import java.util.Optional;
Review Comment:
fixed.
> Seems we don't run style check for java files.
seems right.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361512432
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,145 +203,156 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
Review Comment:
the actual change is relatively small if you enable the "Hide whitespace"
<img width="243" height="264" alt="image" src="https://github.com/user-attachments/assets/5a8ebda9-b356-4b5f-93d2-2fa2cc4cf4bd" />
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2367978374
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
@cloud-fan done, I converted it to Java now.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361532102
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,145 +203,156 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
Review Comment:
yea we can use this trick to help review, but my point is that this method is too long and we need to split it sooner or later. Since we are changing it now, maybe we should also split it now?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2367858728
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
Okay, let me convert it back.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2368198029
##########
sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/OpenedParquetFooter.java:
##########
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet;
+
+import org.apache.parquet.hadoop.metadata.ParquetMetadata;
+import org.apache.parquet.hadoop.util.HadoopInputFile;
+import org.apache.parquet.io.SeekableInputStream;
+
+import java.util.Optional;
Review Comment:
nit: this import should be put at the beginning. Seems we don't run style check for java files.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361516742
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
over 2/3 of the code in the original file is removed, and the newly added method `openFileAndReadFooter ` uses Scala `Tuple`, `Option`, which is ugly if written in Java
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361516742
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
over 2/3 of the code in the original file is removed, and the newly added methods use Scala `Tuple`, `Option`, which is ugly if written in Java
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361516742
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
over 2/3 of the code in the original file is removed, and the newly added method `openFileAndReadFooter` uses Scala `Tuple`, `Option`, which is ugly if written in Java
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on PR #52384:
URL: https://github.com/apache/spark/pull/52384#issuecomment-3306224851
cc @sunchao @cloud-fan @LuciferYang @viirya
as explained in https://github.com/apache/spark/pull/50765#discussion_r2357607758, I spilt the executor-side changes to a dedicated PR, please take a look when you have time, thank you in advance.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361505424
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,145 +203,156 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
Review Comment:
This `buildReaderWithPartitionValues` method is super long now, can we create some smaller methods to split it, so that this PR is easier to review?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361529835
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
we should create a java record to wrap it instead of using a tuple...
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361494420
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
is there a strong reason to rewrite it from java to scala?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #52384: [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader
URL: https://github.com/apache/spark/pull/52384
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2361847568
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,145 +203,156 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
Review Comment:
3 code blocks were extracted from this method as independent methods.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "pan3793 (via GitHub)" <gi...@apache.org>.
pan3793 commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2370809841
##########
sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.java:
##########
@@ -37,53 +36,71 @@
*/
public class ParquetFooterReader {
- public static final boolean SKIP_ROW_GROUPS = true;
- public static final boolean WITH_ROW_GROUPS = false;
-
/**
- * Reads footer for the input Parquet file 'split'. If 'skipRowGroup' is true,
- * this will skip reading the Parquet row group metadata.
+ * Build a filter for reading footer of the input Parquet file 'split'.
Review Comment:
thing has not changed, the 'split' represents `PartitionedFile` here
##########
sql/core/src/main/java/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.java:
##########
@@ -37,53 +36,71 @@
*/
public class ParquetFooterReader {
- public static final boolean SKIP_ROW_GROUPS = true;
- public static final boolean WITH_ROW_GROUPS = false;
-
/**
- * Reads footer for the input Parquet file 'split'. If 'skipRowGroup' is true,
- * this will skip reading the Parquet row group metadata.
+ * Build a filter for reading footer of the input Parquet file 'split'.
+ * If 'skipRowGroup' is true, this will skip reading the Parquet row group metadata.
*
* @param file a part (i.e. "block") of a single file that should be read
- * @param configuration hadoop configuration of file
+ * @param hadoopConf hadoop configuration of file
* @param skipRowGroup If true, skip reading row groups;
* if false, read row groups according to the file split range
*/
- public static ParquetMetadata readFooter(
- Configuration configuration,
- PartitionedFile file,
- boolean skipRowGroup) throws IOException {
- long fileStart = file.start();
- ParquetMetadataConverter.MetadataFilter filter;
+ public static ParquetMetadataConverter.MetadataFilter buildFilter(
+ Configuration hadoopConf, PartitionedFile file, boolean skipRowGroup) {
if (skipRowGroup) {
- filter = ParquetMetadataConverter.SKIP_ROW_GROUPS;
+ return ParquetMetadataConverter.SKIP_ROW_GROUPS;
} else {
- filter = HadoopReadOptions.builder(configuration, file.toPath())
+ long fileStart = file.start();
+ return HadoopReadOptions.builder(hadoopConf, file.toPath())
.withRange(fileStart, fileStart + file.length())
.build()
.getMetadataFilter();
}
- return readFooter(configuration, file.toPath(), filter);
- }
-
- public static ParquetMetadata readFooter(Configuration configuration,
- Path file, ParquetMetadataConverter.MetadataFilter filter) throws IOException {
- return readFooter(HadoopInputFile.fromPath(file, configuration), filter);
- }
-
- public static ParquetMetadata readFooter(Configuration configuration,
- FileStatus fileStatus, ParquetMetadataConverter.MetadataFilter filter) throws IOException {
- return readFooter(HadoopInputFile.fromStatus(fileStatus, configuration), filter);
}
- private static ParquetMetadata readFooter(HadoopInputFile inputFile,
+ public static ParquetMetadata readFooter(
+ HadoopInputFile inputFile,
ParquetMetadataConverter.MetadataFilter filter) throws IOException {
- ParquetReadOptions readOptions =
- HadoopReadOptions.builder(inputFile.getConfiguration(), inputFile.getPath())
+ ParquetReadOptions readOptions = HadoopReadOptions
+ .builder(inputFile.getConfiguration(), inputFile.getPath())
.withMetadataFilter(filter).build();
- // Use try-with-resources to ensure fd is closed.
- try (ParquetFileReader fileReader = ParquetFileReader.open(inputFile, readOptions)) {
+ try (var fileReader = ParquetFileReader.open(inputFile, readOptions)) {
return fileReader.getFooter();
}
}
+
+ /**
+ * Decoding Parquet files generally involves two steps:
+ * 1. read and resolve the metadata (footer),
+ * 2. read and decode the row groups/column chunks.
+ * <p>
+ * It's possible to avoid opening the file twice by resuing the SeekableInputStream.
+ * When keepInputStreamOpen is true, the caller takes responsibility to close the
+ * SeekableInputStream. Currently, this is only supported by parquet vectorized reader.
+ *
+ * @param hadoopConf hadoop configuration of file
+ * @param file a part (i.e. "block") of a single file that should be read
+ * @param keepInputStreamOpen when true, keep the SeekableInputStream of file being open
+ * @return if keepInputStreamOpen is true, the returned OpenedParquetFooter carries
+ * Some(SeekableInputStream), otherwise None.
+ */
+ public static OpenedParquetFooter openFileAndReadFooter(
+ Configuration hadoopConf,
+ PartitionedFile file,
+ boolean keepInputStreamOpen) throws IOException {
+ var readOptions = HadoopReadOptions.builder(hadoopConf, file.toPath())
+ .withMetadataFilter(buildFilter(hadoopConf, file, !keepInputStreamOpen))
Review Comment:
added a comment
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,150 +214,199 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
- // When there are vectorized reads, we can avoid reading the footer twice by reading
- // all row groups in advance and filter row groups according to filters that require
- // push down (no need to read the footer metadata again).
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.WITH_ROW_GROUPS)
- } else {
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.SKIP_ROW_GROUPS)
- }
-
- val footerFileMetaData = fileFooter.getFileMetaData
- val datetimeRebaseSpec = DataSourceUtils.datetimeRebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get,
- datetimeRebaseModeInRead)
- val int96RebaseSpec = DataSourceUtils.int96RebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get, int96RebaseModeInRead)
-
- // Try to push down filters when filter push-down is enabled.
- val pushed = if (enableParquetFilterPushDown) {
- val parquetSchema = footerFileMetaData.getSchema
- val parquetFilters = new ParquetFilters(
- parquetSchema,
- pushDownDate,
- pushDownTimestamp,
- pushDownDecimal,
- pushDownStringPredicate,
- pushDownInFilterThreshold,
- isCaseSensitive,
- datetimeRebaseSpec)
- filters
- // Collects all converted Parquet filter predicates. Notice that not all predicates can be
- // converted (`ParquetFilters.createFilter` returns an `Option`). That's why a `flatMap`
- // is used here.
- .flatMap(parquetFilters.createFilter(_))
- .reduceOption(FilterApi.and)
- } else {
- None
- }
-
- // PARQUET_INT96_TIMESTAMP_CONVERSION says to apply timezone conversions to int96 timestamps'
- // *only* if the file was created by something other than "parquet-mr", so check the actual
- // writer here for this file. We have to do this per-file, as each file in the table may
- // have different writers.
- // Define isCreatedByParquetMr as function to avoid unnecessary parquet footer reads.
- def isCreatedByParquetMr: Boolean =
- footerFileMetaData.getCreatedBy().startsWith("parquet-mr")
-
- val convertTz =
- if (timestampConversion && !isCreatedByParquetMr) {
- Some(DateTimeUtils.getZoneId(sharedConf.get(SQLConf.SESSION_LOCAL_TIMEZONE.key)))
+ // When there are vectorized reads, we can avoid
+ // 1. opening the file twice by transfering the SeekableInputStream
+ // 2. reading the footer twice by reading all row groups in advance and filter row groups
+ // according to filters that require push down
+ val openedFooter =
+ ParquetFooterReader.openFileAndReadFooter(sharedConf, file, enableVectorizedReader)
+ assert(openedFooter.inputStreamOpt.isPresent == enableVectorizedReader)
+
+ // Before transferring the ownership of inputStream to the vectorizedReader,
+ // we must take responsibility to close the inputStream if something goes wrong
+ // to avoid resource leak.
+ val shouldCloseInputStream = new AtomicBoolean(openedFooter.inputStreamOpt.isPresent)
Review Comment:
we pass `shouldCloseInputStream` to `def buildVectorizedIterator` and will be updated by that method, so we must use a reference instead of a primitive type.
the suggestion works but will introduce many unnecessary `close()` in normal cases, so I add a flag to avoid that as much as possible.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala:
##########
@@ -202,150 +214,199 @@ class ParquetFileFormat
(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
- val filePath = file.toPath
- val split = new FileSplit(filePath, file.start, file.length, Array.empty[String])
-
+ val split = new FileSplit(file.toPath, file.start, file.length, Array.empty[String])
val sharedConf = broadcastedHadoopConf.value.value
- val fileFooter = if (enableVectorizedReader) {
- // When there are vectorized reads, we can avoid reading the footer twice by reading
- // all row groups in advance and filter row groups according to filters that require
- // push down (no need to read the footer metadata again).
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.WITH_ROW_GROUPS)
- } else {
- ParquetFooterReader.readFooter(sharedConf, file, ParquetFooterReader.SKIP_ROW_GROUPS)
- }
-
- val footerFileMetaData = fileFooter.getFileMetaData
- val datetimeRebaseSpec = DataSourceUtils.datetimeRebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get,
- datetimeRebaseModeInRead)
- val int96RebaseSpec = DataSourceUtils.int96RebaseSpec(
- footerFileMetaData.getKeyValueMetaData.get, int96RebaseModeInRead)
-
- // Try to push down filters when filter push-down is enabled.
- val pushed = if (enableParquetFilterPushDown) {
- val parquetSchema = footerFileMetaData.getSchema
- val parquetFilters = new ParquetFilters(
- parquetSchema,
- pushDownDate,
- pushDownTimestamp,
- pushDownDecimal,
- pushDownStringPredicate,
- pushDownInFilterThreshold,
- isCaseSensitive,
- datetimeRebaseSpec)
- filters
- // Collects all converted Parquet filter predicates. Notice that not all predicates can be
- // converted (`ParquetFilters.createFilter` returns an `Option`). That's why a `flatMap`
- // is used here.
- .flatMap(parquetFilters.createFilter(_))
- .reduceOption(FilterApi.and)
- } else {
- None
- }
-
- // PARQUET_INT96_TIMESTAMP_CONVERSION says to apply timezone conversions to int96 timestamps'
- // *only* if the file was created by something other than "parquet-mr", so check the actual
- // writer here for this file. We have to do this per-file, as each file in the table may
- // have different writers.
- // Define isCreatedByParquetMr as function to avoid unnecessary parquet footer reads.
- def isCreatedByParquetMr: Boolean =
- footerFileMetaData.getCreatedBy().startsWith("parquet-mr")
-
- val convertTz =
- if (timestampConversion && !isCreatedByParquetMr) {
- Some(DateTimeUtils.getZoneId(sharedConf.get(SQLConf.SESSION_LOCAL_TIMEZONE.key)))
+ // When there are vectorized reads, we can avoid
+ // 1. opening the file twice by transfering the SeekableInputStream
+ // 2. reading the footer twice by reading all row groups in advance and filter row groups
+ // according to filters that require push down
+ val openedFooter =
+ ParquetFooterReader.openFileAndReadFooter(sharedConf, file, enableVectorizedReader)
+ assert(openedFooter.inputStreamOpt.isPresent == enableVectorizedReader)
+
+ // Before transferring the ownership of inputStream to the vectorizedReader,
+ // we must take responsibility to close the inputStream if something goes wrong
+ // to avoid resource leak.
+ val shouldCloseInputStream = new AtomicBoolean(openedFooter.inputStreamOpt.isPresent)
+ try {
+ val footerFileMetaData = openedFooter.footer.getFileMetaData
+ val datetimeRebaseSpec = DataSourceUtils.datetimeRebaseSpec(
+ footerFileMetaData.getKeyValueMetaData.get,
+ datetimeRebaseModeInRead)
+ val int96RebaseSpec = DataSourceUtils.int96RebaseSpec(
+ footerFileMetaData.getKeyValueMetaData.get, int96RebaseModeInRead)
+
+ // Try to push down filters when filter push-down is enabled.
+ val pushed = if (enableParquetFilterPushDown) {
+ val parquetSchema = footerFileMetaData.getSchema
+ val parquetFilters = new ParquetFilters(
+ parquetSchema,
+ pushDownDate,
+ pushDownTimestamp,
+ pushDownDecimal,
+ pushDownStringPredicate,
+ pushDownInFilterThreshold,
+ isCaseSensitive,
+ datetimeRebaseSpec)
+ filters
+ // Collects all converted Parquet filter predicates. Notice that not all predicates
+ // can be converted (`ParquetFilters.createFilter` returns an `Option`). That's why
+ // a `flatMap` is used here.
+ .flatMap(parquetFilters.createFilter)
+ .reduceOption(FilterApi.and)
} else {
None
}
-
- val attemptId = new TaskAttemptID(new TaskID(new JobID(), TaskType.MAP, 0), 0)
- val hadoopAttemptContext =
- new TaskAttemptContextImpl(broadcastedHadoopConf.value.value, attemptId)
-
- // Try to push down filters when filter push-down is enabled.
- // Notice: This push-down is RowGroups level, not individual records.
- if (pushed.isDefined) {
- ParquetInputFormat.setFilterPredicate(hadoopAttemptContext.getConfiguration, pushed.get)
- }
- val taskContext = Option(TaskContext.get())
- if (enableVectorizedReader) {
- val vectorizedReader = new VectorizedParquetRecordReader(
- convertTz.orNull,
- datetimeRebaseSpec.mode.toString,
- datetimeRebaseSpec.timeZone,
- int96RebaseSpec.mode.toString,
- int96RebaseSpec.timeZone,
- enableOffHeapColumnVector && taskContext.isDefined,
- capacity)
- // SPARK-37089: We cannot register a task completion listener to close this iterator here
- // because downstream exec nodes have already registered their listeners. Since listeners
- // are executed in reverse order of registration, a listener registered here would close the
- // iterator while downstream exec nodes are still running. When off-heap column vectors are
- // enabled, this can cause a use-after-free bug leading to a segfault.
- //
- // Instead, we use FileScanRDD's task completion listener to close this iterator.
- val iter = new RecordReaderIterator(vectorizedReader)
- try {
- vectorizedReader.initialize(split, hadoopAttemptContext, Option.apply(fileFooter))
- logDebug(s"Appending $partitionSchema ${file.partitionValues}")
- vectorizedReader.initBatch(partitionSchema, file.partitionValues)
- if (returningBatch) {
- vectorizedReader.enableReturningBatches()
+ // PARQUET_INT96_TIMESTAMP_CONVERSION says to apply timezone conversions to int96
+ // timestamps *only* if the file was created by something other than "parquet-mr",
+ // so check the actual writer here for this file. We have to do this per-file,
+ // as each file in the table may have different writers.
+ // Define isCreatedByParquetMr as function to avoid unnecessary parquet footer reads.
+ def isCreatedByParquetMr: Boolean =
+ footerFileMetaData.getCreatedBy.startsWith("parquet-mr")
+
+ val convertTz =
+ if (timestampConversion && !isCreatedByParquetMr) {
+ Some(DateTimeUtils.getZoneId(sharedConf.get(SQLConf.SESSION_LOCAL_TIMEZONE.key)))
+ } else {
+ None
}
- // UnsafeRowParquetRecordReader appends the columns internally to avoid another copy.
- iter.asInstanceOf[Iterator[InternalRow]]
- } catch {
- case e: Throwable =>
- // SPARK-23457: In case there is an exception in initialization, close the iterator to
- // avoid leaking resources.
- iter.close()
- throw e
+ val attemptId = new TaskAttemptID(new TaskID(new JobID(), TaskType.MAP, 0), 0)
+ val hadoopAttemptContext =
+ new TaskAttemptContextImpl(broadcastedHadoopConf.value.value, attemptId)
+
+ // Try to push down filters when filter push-down is enabled.
+ // Notice: This push-down is RowGroups level, not individual records.
+ pushed.foreach {
+ ParquetInputFormat.setFilterPredicate(hadoopAttemptContext.getConfiguration, _)
}
- } else {
- logDebug(s"Falling back to parquet-mr")
- // ParquetRecordReader returns InternalRow
- val readSupport = new ParquetReadSupport(
- convertTz,
- enableVectorizedReader = false,
- datetimeRebaseSpec,
- int96RebaseSpec)
- val reader = if (pushed.isDefined && enableRecordFilter) {
- val parquetFilter = FilterCompat.get(pushed.get, null)
- new ParquetRecordReader[InternalRow](readSupport, parquetFilter)
+ if (enableVectorizedReader) {
+ buildVectorizedIterator(
+ hadoopAttemptContext, split, file.partitionValues, partitionSchema, convertTz,
+ datetimeRebaseSpec, int96RebaseSpec, enableOffHeapColumnVector, returningBatch,
+ capacity, openedFooter, shouldCloseInputStream)
} else {
- new ParquetRecordReader[InternalRow](readSupport)
+ logDebug(s"Falling back to parquet-mr")
+ buildRowBasedIterator(
+ hadoopAttemptContext, split, file.partitionValues, partitionSchema, convertTz,
+ datetimeRebaseSpec, int96RebaseSpec, requiredSchema, pushed, enableRecordFilter)
}
- val readerWithRowIndexes = ParquetRowIndexUtil.addRowIndexToRecordReaderIfNeeded(reader,
- requiredSchema)
- val iter = new RecordReaderIterator[InternalRow](readerWithRowIndexes)
- try {
- readerWithRowIndexes.initialize(split, hadoopAttemptContext)
-
- val fullSchema = toAttributes(requiredSchema) ++ toAttributes(partitionSchema)
- val unsafeProjection = GenerateUnsafeProjection.generate(fullSchema, fullSchema)
-
- if (partitionSchema.length == 0) {
- // There is no partition columns
- iter.map(unsafeProjection)
- } else {
- val joinedRow = new JoinedRow()
- iter.map(d => unsafeProjection(joinedRow(d, file.partitionValues)))
- }
- } catch {
- case e: Throwable =>
- // SPARK-23457: In case there is an exception in initialization, close the iterator to
- // avoid leaking resources.
- iter.close()
- throw e
+ } finally {
+ if (shouldCloseInputStream.get) {
+ openedFooter.inputStreamOpt.ifPresent(Utils.closeQuietly)
}
}
}
}
+ // scalastyle:off argcount
Review Comment:
effective code at L338-344, others are just method extraction
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-53633][SQL] Reuse InputStream in vectorized Parquet reader [spark]
Posted by "cloud-fan (via GitHub)" <gi...@apache.org>.
cloud-fan commented on code in PR #52384:
URL: https://github.com/apache/spark/pull/52384#discussion_r2367839781
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFooterReader.scala:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.execution.datasources.parquet
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.parquet.HadoopReadOptions
+import org.apache.parquet.format.converter.ParquetMetadataConverter
+import org.apache.parquet.hadoop.ParquetFileReader
+import org.apache.parquet.hadoop.metadata.ParquetMetadata
+import org.apache.parquet.hadoop.util.HadoopInputFile
+import org.apache.parquet.io.SeekableInputStream
+
+import org.apache.spark.sql.execution.datasources.PartitionedFile
+import org.apache.spark.util.Utils
+
+object ParquetFooterReader {
Review Comment:
Then why not keep it as Java then? Java is more AI friendly and I'm a bit hesitant to turn existing Java code to Scala.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org