You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "anuragmantri (via GitHub)" <gi...@apache.org> on 2026/07/13 16:15:07 UTC
[PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
anuragmantri opened a new pull request, #57227:
URL: https://github.com/apache/spark/pull/57227
## What changes were proposed in this pull request?
This PR adds the dynamic table options syntax to MERGE statements. MERGE requires options on both the target table and the source table.
## Why are the changes needed?
This is a continuation to adding options syntax in DMLs SPARK-49098
## Does this PR introduce any user-facing change?
Yes, it adds new SQL syntax. For example:
```sql
MERGE INTO catalog.db.target t WITH (`write.split-size` = 10)
USING catalog.db.source WITH (`split-size` = 5) s
ON t.id = s.id
WHEN MATCHED THEN UPDATE SET t.status = 'archived'
WHEN NOT MATCHED THEN INSERT *
```
## How was this patch tested?
Added tests to the following suites.
- Parser (DDLParserSuite)
- End-to-end (MergeIntoTableSuiteBase)
## Was this patch authored or co-authored using generative AI tooling?
I used Claude Code (Claude Opus 4.8) to generate the code and tests and verified manually.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "anuragmantri (via GitHub)" <gi...@apache.org>.
anuragmantri commented on PR #57227:
URL: https://github.com/apache/spark/pull/57227#issuecomment-4960464461
@dongjoon-hyun, @peter-toth - Could you please take a look?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #57227:
URL: https://github.com/apache/spark/pull/57227#issuecomment-5036141377
I merged this to master/branch-4.x Today because Apache Spark 4.3 Feature Freeze is scheduled on 1st August. We need to hurry up in order to finish testing and polishing this features for Apache Spark 4.3.0 release.
Please proceed to the next steps, @anuragmantri .
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #57227:
URL: https://github.com/apache/spark/pull/57227#issuecomment-4960478802
Sure, @anuragmantri . Thank you for pinging me. Please allow me some time because I'm on-call this week.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun commented on PR #57227:
URL: https://github.com/apache/spark/pull/57227#issuecomment-5036127243
**Merge Summary:**
- merged into master https://github.com/apache/spark/commit/8a33fb73fa520ba04be46a1c350e46d79234b53c
- merged into branch-4.x https://github.com/apache/spark/commit/58e2554111f1311556dde32c7c26ec7d4fe96471
*Posted by `merge_spark_pr.py`*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "dongjoon-hyun (via GitHub)" <gi...@apache.org>.
dongjoon-hyun closed pull request #57227: [SPARK-58007][SQL] Support dynamic table options for MERGE
URL: https://github.com/apache/spark/pull/57227
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "anuragmantri (via GitHub)" <gi...@apache.org>.
anuragmantri commented on PR #57227:
URL: https://github.com/apache/spark/pull/57227#issuecomment-4960543758
There is one case I'm not really sure about, I would like to know what you all think. There could be self-merge (same with self-insert) where relation cache will load the `t` with target options only and does not load the source options.
```sql
MERGE INTO t WITH (`write.split-size`=10) USING t WITH (`split-size`=5) s ON ...
```
`split-size` will be lost in this case for source read. I don't think this a common case to self merge. So I have not addressed it in this PR. Do you think we should address this?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on code in PR #57227:
URL: https://github.com/apache/spark/pull/57227#discussion_r3604863220
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/MergeIntoTableSuiteBase.scala:
##########
@@ -2851,6 +2853,127 @@ abstract class MergeIntoTableSuiteBase extends RowLevelOperationSuiteBase
assert(catalog.lastTransaction.isClosed)
}
+ test("SPARK-58007: merge with dynamic options on the target") {
+ withTable(sourceNameAsString) {
+ createAndInitTable("pk INT NOT NULL, salary INT, dep STRING",
+ """{ "pk": 1, "salary": 100, "dep": "hr" }
+ |{ "pk": 2, "salary": 200, "dep": "software" }
+ |""".stripMargin)
+ sql(s"CREATE TABLE $sourceNameAsString (pk INT NOT NULL, salary INT, dep STRING)")
+ sql(s"INSERT INTO $sourceNameAsString VALUES (1, 150, 'hr'), (3, 300, 'hr')")
+
+ // the target options become the row-level operation options: they reach the rewritten
+ // DataSourceV2Relation, the RowLevelOperationInfo, and the write builder's LogicalWriteInfo
+ val Seq(qe) = withQueryExecutionsCaptured(spark) {
+ sql(
+ s"""MERGE INTO $tableNameAsString t WITH (`write.split-size` = 10)
+ |USING $sourceNameAsString s
+ |ON t.pk = s.pk
+ |WHEN MATCHED THEN UPDATE SET t.salary = s.salary
Review Comment:
Every one of the three new tests has a `WHEN MATCHED` action, so they all take the full row-level rewrite (`ReplaceData`/`WriteDelta`). The MATCHED-free fast path is untested with options: `RewriteMergeIntoTable` turns a MERGE with only `WHEN NOT MATCHED THEN INSERT` into an `InsertOnlyMerge` (`RewriteMergeIntoTable.scala:77,119`), whose write is built from `r.options` at a separate site (`V2Writes.scala:55`) that these tests never reach. Worth one more test: target options on a `WHEN NOT MATCHED THEN INSERT *`-only merge, asserting the option lands on `table.lastWriteInfo`.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/MergeIntoTableSuiteBase.scala:
##########
@@ -2851,6 +2853,127 @@ abstract class MergeIntoTableSuiteBase extends RowLevelOperationSuiteBase
assert(catalog.lastTransaction.isClosed)
}
+ test("SPARK-58007: merge with dynamic options on the target") {
+ withTable(sourceNameAsString) {
+ createAndInitTable("pk INT NOT NULL, salary INT, dep STRING",
+ """{ "pk": 1, "salary": 100, "dep": "hr" }
+ |{ "pk": 2, "salary": 200, "dep": "software" }
+ |""".stripMargin)
+ sql(s"CREATE TABLE $sourceNameAsString (pk INT NOT NULL, salary INT, dep STRING)")
+ sql(s"INSERT INTO $sourceNameAsString VALUES (1, 150, 'hr'), (3, 300, 'hr')")
+
+ // the target options become the row-level operation options: they reach the rewritten
+ // DataSourceV2Relation, the RowLevelOperationInfo, and the write builder's LogicalWriteInfo
+ val Seq(qe) = withQueryExecutionsCaptured(spark) {
+ sql(
+ s"""MERGE INTO $tableNameAsString t WITH (`write.split-size` = 10)
+ |USING $sourceNameAsString s
+ |ON t.pk = s.pk
+ |WHEN MATCHED THEN UPDATE SET t.salary = s.salary
+ |WHEN NOT MATCHED THEN INSERT *
+ |""".stripMargin)
+ }
+ val writeRelation = qe.optimizedPlan.collectFirst {
+ case rd: ReplaceData => rd.table
+ case wd: WriteDelta => wd.table
+ }.getOrElse(fail("couldn't find row-level operation in optimized plan"))
+ .asInstanceOf[DataSourceV2Relation]
+ val operation = writeRelation.table.asInstanceOf[RowLevelOperationTable].operation
+ .asInstanceOf[RowLevelOperationWithOptions]
+ assert(writeRelation.options.get("write.split-size") === "10", "relation option")
Review Comment:
These three lines are exactly `UpdateTableSuiteBase.checkRowLevelOperationOptions` (`UpdateTableSuiteBase.scala:1242`), and the "both" test repeats them again at 2955-2958. Since both `MergeIntoTableSuiteBase` and `UpdateTableSuiteBase` extend `RowLevelOperationSuiteBase`, consider lifting that helper into the shared base and calling it here:
```scala
checkRowLevelOperationOptions(
sql(s"""MERGE INTO ... """.stripMargin),
"write.split-size" -> "10")
```
That keeps the target-side verification identical across UPDATE and MERGE and drops the duplicated block (the helper already wraps the `collectFirst`/capture).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "anuragmantri (via GitHub)" <gi...@apache.org>.
anuragmantri commented on code in PR #57227:
URL: https://github.com/apache/spark/pull/57227#discussion_r3607007161
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/MergeIntoTableSuiteBase.scala:
##########
@@ -2851,6 +2853,127 @@ abstract class MergeIntoTableSuiteBase extends RowLevelOperationSuiteBase
assert(catalog.lastTransaction.isClosed)
}
+ test("SPARK-58007: merge with dynamic options on the target") {
+ withTable(sourceNameAsString) {
+ createAndInitTable("pk INT NOT NULL, salary INT, dep STRING",
+ """{ "pk": 1, "salary": 100, "dep": "hr" }
+ |{ "pk": 2, "salary": 200, "dep": "software" }
+ |""".stripMargin)
+ sql(s"CREATE TABLE $sourceNameAsString (pk INT NOT NULL, salary INT, dep STRING)")
+ sql(s"INSERT INTO $sourceNameAsString VALUES (1, 150, 'hr'), (3, 300, 'hr')")
+
+ // the target options become the row-level operation options: they reach the rewritten
+ // DataSourceV2Relation, the RowLevelOperationInfo, and the write builder's LogicalWriteInfo
+ val Seq(qe) = withQueryExecutionsCaptured(spark) {
+ sql(
+ s"""MERGE INTO $tableNameAsString t WITH (`write.split-size` = 10)
+ |USING $sourceNameAsString s
+ |ON t.pk = s.pk
+ |WHEN MATCHED THEN UPDATE SET t.salary = s.salary
Review Comment:
I added a test for the insert only fast path and the options survived. Thanks for the suggestion.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/MergeIntoTableSuiteBase.scala:
##########
@@ -2851,6 +2853,127 @@ abstract class MergeIntoTableSuiteBase extends RowLevelOperationSuiteBase
assert(catalog.lastTransaction.isClosed)
}
+ test("SPARK-58007: merge with dynamic options on the target") {
+ withTable(sourceNameAsString) {
+ createAndInitTable("pk INT NOT NULL, salary INT, dep STRING",
+ """{ "pk": 1, "salary": 100, "dep": "hr" }
+ |{ "pk": 2, "salary": 200, "dep": "software" }
+ |""".stripMargin)
+ sql(s"CREATE TABLE $sourceNameAsString (pk INT NOT NULL, salary INT, dep STRING)")
+ sql(s"INSERT INTO $sourceNameAsString VALUES (1, 150, 'hr'), (3, 300, 'hr')")
+
+ // the target options become the row-level operation options: they reach the rewritten
+ // DataSourceV2Relation, the RowLevelOperationInfo, and the write builder's LogicalWriteInfo
+ val Seq(qe) = withQueryExecutionsCaptured(spark) {
+ sql(
+ s"""MERGE INTO $tableNameAsString t WITH (`write.split-size` = 10)
+ |USING $sourceNameAsString s
+ |ON t.pk = s.pk
+ |WHEN MATCHED THEN UPDATE SET t.salary = s.salary
+ |WHEN NOT MATCHED THEN INSERT *
+ |""".stripMargin)
+ }
+ val writeRelation = qe.optimizedPlan.collectFirst {
+ case rd: ReplaceData => rd.table
+ case wd: WriteDelta => wd.table
+ }.getOrElse(fail("couldn't find row-level operation in optimized plan"))
+ .asInstanceOf[DataSourceV2Relation]
+ val operation = writeRelation.table.asInstanceOf[RowLevelOperationTable].operation
+ .asInstanceOf[RowLevelOperationWithOptions]
+ assert(writeRelation.options.get("write.split-size") === "10", "relation option")
Review Comment:
Done.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "anuragmantri (via GitHub)" <gi...@apache.org>.
anuragmantri commented on PR #57227:
URL: https://github.com/apache/spark/pull/57227#issuecomment-5039947283
Thank for the reviews and merging this. Could you also review DELETE counterpart [PR](https://github.com/apache/spark/pull/57161), that should complete all DML operations and I will create a docs PR for all.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org
Re: [PR] [SPARK-58007][SQL] Support dynamic table options for MERGE [spark]
Posted by "peter-toth (via GitHub)" <gi...@apache.org>.
peter-toth commented on PR #57227:
URL: https://github.com/apache/spark/pull/57227#issuecomment-5035775437
cc @szehon-ho
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org