Skip to content

[SPARK-53146][CONNECT][SQL] Make MergeIntoTable in SparkConnectPlanner side effect free #51876

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

Open
wants to merge 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -2651,6 +2651,8 @@ class SparkConnectPlanner(
Some(transformWriteOperation(command.getWriteOperation))
case proto.Command.CommandTypeCase.WRITE_OPERATION_V2 =>
Some(transformWriteOperationV2(command.getWriteOperationV2))
case proto.Command.CommandTypeCase.MERGE_INTO_TABLE_COMMAND =>
Some(transformMergeIntoTableCommand(command.getMergeIntoTableCommand))
case _ =>
None
}
Expand Down Expand Up @@ -2700,8 +2702,6 @@ class SparkConnectPlanner(
handleCheckpointCommand(command.getCheckpointCommand, responseObserver)
case proto.Command.CommandTypeCase.REMOVE_CACHED_REMOTE_RELATION_COMMAND =>
handleRemoveCachedRemoteRelationCommand(command.getRemoveCachedRemoteRelationCommand)
case proto.Command.CommandTypeCase.MERGE_INTO_TABLE_COMMAND =>
handleMergeIntoTableCommand(command.getMergeIntoTableCommand)
case proto.Command.CommandTypeCase.ML_COMMAND =>
handleMlCommand(command.getMlCommand, responseObserver)
case proto.Command.CommandTypeCase.PIPELINE_COMMAND =>
Expand Down Expand Up @@ -3759,15 +3759,16 @@ class SparkConnectPlanner(
executeHolder.eventsManager.postFinished()
}

private def handleMergeIntoTableCommand(cmd: proto.MergeIntoTableCommand): Unit = {
private def transformMergeIntoTableCommand(cmd: proto.MergeIntoTableCommand)(
tracker: QueryPlanningTracker): LogicalPlan = {
def transformActions(actions: java.util.List[proto.Expression]): Seq[MergeAction] =
actions.asScala.map(transformExpression).map(_.asInstanceOf[MergeAction]).toSeq

val matchedActions = transformActions(cmd.getMatchActionsList)
val notMatchedActions = transformActions(cmd.getNotMatchedActionsList)
val notMatchedBySourceActions = transformActions(cmd.getNotMatchedBySourceActionsList)

val sourceDs = Dataset.ofRows(session, transformRelation(cmd.getSourceTablePlan))
val sourceDs = Dataset.ofRows(session, transformRelation(cmd.getSourceTablePlan), tracker)
val mergeInto = sourceDs
.mergeInto(cmd.getTargetTableName, Column(transformExpression(cmd.getMergeCondition)))
.asInstanceOf[MergeIntoWriter[Row]]
Expand All @@ -3777,8 +3778,7 @@ class SparkConnectPlanner(
if (cmd.getWithSchemaEvolution) {
mergeInto.withSchemaEvolution()
}
mergeInto.merge()
executeHolder.eventsManager.postFinished()
mergeInto.mergeCommand()
}

private val emptyLocalRelation = LocalRelation(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ class MergeIntoWriter[T] private[sql](table: String, ds: Dataset[T], on: Column)

/** @inheritdoc */
def merge(): Unit = {
val qe = sparkSession.sessionState.executePlan(mergeCommand())
qe.assertCommandExecuted()
}

private[sql] def mergeCommand(): LogicalPlan = {
if (matchedActions.isEmpty && notMatchedActions.isEmpty && notMatchedBySourceActions.isEmpty) {
throw new SparkRuntimeException(
errorClass = "NO_MERGE_ACTION_SPECIFIED",
messageParameters = Map.empty)
}

val merge = MergeIntoTable(
MergeIntoTable(
UnresolvedRelation(tableName).requireWritePrivileges(MergeIntoTable.getWritePrivileges(
matchedActions, notMatchedActions, notMatchedBySourceActions)),
logicalPlan,
Expand All @@ -72,8 +77,6 @@ class MergeIntoWriter[T] private[sql](table: String, ds: Dataset[T], on: Column)
notMatchedActions.toSeq,
notMatchedBySourceActions.toSeq,
schemaEvolutionEnabled)
val qe = sparkSession.sessionState.executePlan(merge)
qe.assertCommandExecuted()
}

override protected[sql] def insertAll(condition: Option[Column]): this.type = {
Expand Down