Skip to content

Spark 4.1: Add Merge WriteSummary to Snapshot Summary - #15014

Merged
szehon-ho merged 4 commits into
apache:mainfrom
szehon-ho:merge_metrics
Jan 24, 2026
Merged

szehon-ho merged 4 commits into
apache:mainfrom
szehon-ho:merge_metrics

Conversation

@szehon-ho

@szehon-ho szehon-ho commented Jan 10, 2026 •

Copy link
Copy Markdown
Member

Spark 4.1 added "WriteSummary" API for certain DML operations, see https://issues.apache.org/jira/browse/SPARK-54309. Merge Into is the main one so far, hopefully more will follow.

This adds these operation metadata to Iceberg Snapshot Summary. It can be useful to debug data corruption issues, etc.

It is equivalent to the metadata stored in Delta Lake for debugging: https://docs.delta.io/delta-utility/#retrieve-delta-table-history

Welcome suggestions how to organize this summary

@github-actions github-actions Bot added the spark label Jan 10, 2026

@singhpk234 singhpk234 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This would be a nice info to have !

should we namespace theme like
spark.merge-into.num-target-rows-matched-rows-updated

Comment on lines +259 to +274
setIfPositive(operation, "numTargetRowsCopied", mergeSummary.numTargetRowsCopied());
setIfPositive(operation, "numTargetRowsDeleted", mergeSummary.numTargetRowsDeleted());
setIfPositive(operation, "numTargetRowsUpdated", mergeSummary.numTargetRowsUpdated());
setIfPositive(operation, "numTargetRowsInserted", mergeSummary.numTargetRowsInserted());
setIfPositive(
operation, "numTargetRowsMatchedUpdated", mergeSummary.numTargetRowsMatchedUpdated());
setIfPositive(
operation, "numTargetRowsMatchedDeleted", mergeSummary.numTargetRowsMatchedDeleted());
setIfPositive(
operation,
"numTargetRowsNotMatchedBySourceUpdated",
mergeSummary.numTargetRowsNotMatchedBySourceUpdated());
setIfPositive(
operation,
"numTargetRowsNotMatchedBySourceDeleted",
mergeSummary.numTargetRowsNotMatchedBySourceDeleted());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

changed to snake case (its actually kebab case?)

Is it ok to document in another pr?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is it ok to document in another pr?

ofc :) !, thank you for changing it to kebab case.

@szehon-ho

szehon-ho commented Jan 10, 2026 •

Copy link
Copy Markdown
Member Author

should we namespace theme like
spark.merge-into.num-target-rows-matched-rows-updated

yea, this works for me. looks like there is already spark.xxx for other things.

thanks for the feedback, welcome other thoughts as well !

@singhpk234 singhpk234 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

mostly LGTM, thanks @szehon-ho !
I have a minor suggestion please let me WDYT ?

Comment on lines +363 to +400
private void setMergeSummaryProperties(SnapshotUpdate<?> operation, MergeSummary mergeSummary) {
setIfPositive(
operation, "spark.merge-into.num-target-rows-copied", mergeSummary.numTargetRowsCopied());
setIfPositive(
operation,
"spark.merge-into.num-target-rows-deleted",
mergeSummary.numTargetRowsDeleted());
setIfPositive(
operation,
"spark.merge-into.num-target-rows-updated",
mergeSummary.numTargetRowsUpdated());
setIfPositive(
operation,
"spark.merge-into.num-target-rows-inserted",
mergeSummary.numTargetRowsInserted());
setIfPositive(
operation,
"spark.merge-into.num-target-rows-matched-updated",
mergeSummary.numTargetRowsMatchedUpdated());
setIfPositive(
operation,
"spark.merge-into.num-target-rows-matched-deleted",
mergeSummary.numTargetRowsMatchedDeleted());
setIfPositive(
operation,
"spark.merge-into.num-target-rows-not-matched-by-source-updated",
mergeSummary.numTargetRowsNotMatchedBySourceUpdated());
setIfPositive(
operation,
"spark.merge-into.num-target-rows-not-matched-by-source-deleted",
mergeSummary.numTargetRowsNotMatchedBySourceDeleted());
}

private void setIfPositive(SnapshotUpdate<?> operation, String key, long value) {
if (value >= 0) {
operation.set(key, String.valueOf(value));
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

minor : I wonder if we should just move this to a util to have reuse and incase we add new metrics we just add in one place, thoughts ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

yea good call, i made a new base class and put the duplicate method there

@szehon-ho

Copy link
Copy Markdown
Member Author

@singhpk234 can you take another look when you get a chance? thanks

import org.apache.spark.sql.connector.write.MergeSummary;

/** Base class for Spark write implementations with shared utility methods. */
abstract class BaseSparkWrite {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you so much for creating this base class !

@singhpk234 singhpk234 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me, thank you @szehon-ho !

@huaxingao huaxingao left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@szehon-ho
szehon-ho merged commit 829b641 into apache:main Jan 24, 2026
22 checks passed
@szehon-ho

Copy link
Copy Markdown
Member Author

Thanks @huaxingao @singhpk234 for reviews!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants