Skip to content
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

[SPARK-48458][SQL] Data loss in dynamic override fix #49295

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions common/utils/src/main/resources/error/error-conditions.json
Original file line number Diff line number Diff line change
Expand Up @@ -8892,6 +8892,11 @@
"No enough memory for aggregation"
]
},
"_LEGACY_ERROR_TEMP_3303" : {
"message" : [
"The command <command> is unsafe to run after spark session stopped"
]
},
"_LEGACY_ERROR_USER_RAISED_EXCEPTION" : {
"message" : [
"<errorMessage>"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2901,4 +2901,10 @@ private[sql] object QueryExecutionErrors extends QueryErrorsBase with ExecutionE
)
)
}

def unsafeCommandWhenSparkSessionStopped(command: String): SparkRuntimeException = {
new SparkRuntimeException(
errorClass = "_LEGACY_ERROR_TEMP_3303",
messageParameters = Map("command" -> command))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,28 @@ case class InsertIntoHadoopFsRelationCommand(
}

val jobId = java.util.UUID.randomUUID().toString

val activeSession = Option(sparkSession).filter(_.isUsable)
val committer = FileCommitProtocol.instantiate(
sparkSession.sessionState.conf.fileCommitProtocolClass,
jobId = jobId,
outputPath = outputPath.toString,
dynamicPartitionOverwrite = dynamicPartitionOverwrite)

def isSessionActiveAndUnchanged: Boolean = {
SparkSession.getActiveSession.zip(activeSession).exists {
case (sessionBefore, sessionAfter) => sessionBefore eq sessionAfter
}
}

// SPARK-48458: we need to ensure we have the same spark session that had been used
// during dynamicPartitionOverwrite initialization. Otherwise, there is a data loss risk
// if a fallback to the default static mode happens.
if (!isSessionActiveAndUnchanged) {
throw QueryExecutionErrors.unsafeCommandWhenSparkSessionStopped(command =
getClass.getSimpleName)
}

val doInsertion = if (mode == SaveMode.Append) {
true
} else {
Expand Down
Loading