Skip to content
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 @@ -27,6 +27,11 @@
@Evolving
public interface MergeSummary extends WriteSummary {

/**
* Returns the number of source rows.
*/
long numSourceRows();

/**
* Returns the number of target rows copied unmodified because they did not match any action.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package org.apache.spark.sql.connector.write
* Implementation of [[MergeSummary]] that provides MERGE operation summary.
*/
private[sql] case class MergeSummaryImpl(
numSourceRows: Long,
numTargetRowsCopied: Long,
numTargetRowsDeleted: Long,
numTargetRowsUpdated: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ import org.apache.spark.sql.catalyst.util.RowDeltaUtils.{DELETE_OPERATION, INSER
import org.apache.spark.sql.connector.catalog.{CatalogV2Util, Column, Identifier, StagedTable, StagingTableCatalog, Table, TableCatalog, TableInfo, TableWritePrivilege}
import org.apache.spark.sql.connector.expressions.Transform
import org.apache.spark.sql.connector.metric.CustomMetric
import org.apache.spark.sql.connector.write.{BatchWrite, DataWriter, DataWriterFactory, DeltaWrite, DeltaWriter, MergeSummaryImpl, PhysicalWriteInfoImpl, Write, WriterCommitMessage, WriteSummary}
import org.apache.spark.sql.connector.write.{BatchWrite, DataWriter, DataWriterFactory, DeltaWrite, DeltaWriter, MergeSummaryImpl, PhysicalWriteInfoImpl, RowLevelOperationTable, Write, WriterCommitMessage, WriteSummary}
import org.apache.spark.sql.errors.{QueryCompilationErrors, QueryExecutionErrors}
import org.apache.spark.sql.execution.{SparkPlan, SQLExecution, UnaryExecNode}
import org.apache.spark.sql.execution.adaptive.AdaptiveSparkPlanHelper
import org.apache.spark.sql.execution.joins.BaseJoinExec
import org.apache.spark.sql.execution.metric.{CustomMetrics, SQLMetric, SQLMetrics}
import org.apache.spark.sql.types.StructType
import org.apache.spark.util.{LongAccumulator, Utils}
Expand Down Expand Up @@ -483,7 +484,9 @@ trait V2TableWriteExec extends V2CommandExec with UnaryExecNode with AdaptiveSpa
private def getWriteSummary(query: SparkPlan): Option[WriteSummary] = {
collectFirst(query) { case m: MergeRowsExec => m }.map { n =>
val metrics = n.metrics
val numSourceRows = getNumSourceRows(n)
MergeSummaryImpl(
numSourceRows,
metrics.get("numTargetRowsCopied").map(_.value).getOrElse(-1L),
metrics.get("numTargetRowsDeleted").map(_.value).getOrElse(-1L),
metrics.get("numTargetRowsUpdated").map(_.value).getOrElse(-1L),
Expand All @@ -495,6 +498,40 @@ trait V2TableWriteExec extends V2CommandExec with UnaryExecNode with AdaptiveSpa
)
}
}

private def getNumSourceRows(mergeRowsExec: MergeRowsExec): Long = {
def hasTargetTable(plan: SparkPlan): Boolean = {
collectFirst(plan) {
case scan @ BatchScanExec(_, _, _, _, _: RowLevelOperationTable, _) => scan
}.isDefined
}

def findSourceScan(join: BaseJoinExec): Option[SparkPlan] = {
Copy link
Member

Choose a reason for hiding this comment

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

The method name should reflect that its finding the source side child, not the source itself.

val leftHasTarget = hasTargetTable(join.left)
val rightHasTarget = hasTargetTable(join.right)

val sourceSide = if (leftHasTarget) {
Some(join.right)
} else if (rightHasTarget) {
Some(join.left)
} else {
None
}

sourceSide.flatMap { side =>
Copy link
Member

Choose a reason for hiding this comment

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

Maybe we don't need this, we want to find the first join child (on source side) with numOutputRows, else -1?

collectFirst(side) {
case source if source.metrics.contains("numOutputRows") =>
source
}
}
}

(for {
join <- collectFirst(mergeRowsExec.child) { case j: BaseJoinExec => j }
sourceScan <- findSourceScan(join)
metric <- sourceScan.metrics.get("numOutputRows")
} yield metric.value).getOrElse(-1L)
}
}

trait WritingSparkTask[W <: DataWriter[InternalRow]] extends Logging with Serializable {
Expand Down
Loading