Skip to content

[SPARK-52516][SQL] Don't hold previous iterator reference after advancing to next file in ParquetPartitionReaderFactory #51528

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 2 commits into from
Closed
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 @@ -84,6 +84,8 @@ case class ParquetPartitionReaderFactory(
private val datetimeRebaseModeInRead = options.datetimeRebaseModeInRead
private val int96RebaseModeInRead = options.int96RebaseModeInRead

private val parquetReaderCallback = new ParquetReaderCallback()

private def getFooter(file: PartitionedFile): ParquetMetadata = {
val conf = broadcastedConf.value.value
if (aggregation.isDefined || enableVectorizedReader) {
Expand Down Expand Up @@ -309,7 +311,8 @@ case class ParquetPartitionReaderFactory(
reader, readDataSchema)
val iter = new RecordReaderIterator(readerWithRowIndexes)
// SPARK-23457 Register a task completion listener before `initialization`.
taskContext.foreach(_.addTaskCompletionListener[Unit](_ => iter.close()))
parquetReaderCallback.advanceFile(iter)
taskContext.foreach(parquetReaderCallback.initIfNotAlready)
readerWithRowIndexes
}

Expand Down Expand Up @@ -337,8 +340,39 @@ case class ParquetPartitionReaderFactory(
capacity)
val iter = new RecordReaderIterator(vectorizedReader)
// SPARK-23457 Register a task completion listener before `initialization`.
taskContext.foreach(_.addTaskCompletionListener[Unit](_ => iter.close()))
parquetReaderCallback.advanceFile(iter)
taskContext.foreach(parquetReaderCallback.initIfNotAlready)
logDebug(s"Appending $partitionSchema $partitionValues")
vectorizedReader
}
}

/**
* A callback class to handle the cleanup of Parquet readers.
*
* This class is used to ensure that the Parquet readers are closed properly when the task
* completes, and it also allows for the initialization of the reader callback only once per task.
*/
private class ParquetReaderCallback extends Serializable {
private var init: Boolean = false
private var iter: RecordReaderIterator[_] = null

def initIfNotAlready(taskContext: TaskContext): Unit = {
if (!init) {
taskContext.addTaskCompletionListener[Unit](_ => closeCurrent())
init = true
}
}

def advanceFile(iter: RecordReaderIterator[_]): Unit = {
closeCurrent()

this.iter = iter
}

def closeCurrent(): Unit = {
if (iter != null) {
iter.close()
}
}
}