-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-32030][SPARK-32127][SQL] Support unlimited MATCHED and NOT MATCHED clauses in MERGE INTO #28875
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
Closed
Closed
[SPARK-32030][SPARK-32127][SQL] Support unlimited MATCHED and NOT MATCHED clauses in MERGE INTO #28875
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e18a7a5
[SPARK-32030][SQL] Support unlimited MATCHED and NOT MATCHED clauses …
a6ac363
Preserve the order of the MATCHED and NOT MATCHED clauses
1d39c92
Optimize the code
ab97e31
Update according to review comments
d5edef3
Update accordding to comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1134,58 +1134,74 @@ class DDLParserSuite extends AnalysisTest { | |
| } | ||
| } | ||
|
|
||
| test("merge into table: at most two matched clauses") { | ||
| val exc = intercept[ParseException] { | ||
| parsePlan( | ||
| """ | ||
| |MERGE INTO testcat1.ns1.ns2.tbl AS target | ||
| |USING testcat2.ns1.ns2.tbl AS source | ||
| |ON target.col1 = source.col1 | ||
| |WHEN MATCHED AND (target.col2='delete') THEN DELETE | ||
| |WHEN MATCHED AND (target.col2='update1') THEN UPDATE SET target.col2 = source.col2 | ||
| |WHEN MATCHED AND (target.col2='update2') THEN UPDATE SET target.col2 = source.col2 | ||
| |WHEN NOT MATCHED AND (target.col2='insert') | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, source.col2) | ||
| """.stripMargin) | ||
| } | ||
|
|
||
| assert(exc.getMessage.contains("There should be at most 2 'WHEN MATCHED' clauses.")) | ||
| test("merge into table: multi matched and not matched clauses") { | ||
| parseCompare( | ||
| """ | ||
| |MERGE INTO testcat1.ns1.ns2.tbl AS target | ||
| |USING testcat2.ns1.ns2.tbl AS source | ||
| |ON target.col1 = source.col1 | ||
| |WHEN MATCHED AND (target.col2='delete') THEN DELETE | ||
| |WHEN MATCHED AND (target.col2='update1') THEN UPDATE SET target.col2 = 1 | ||
| |WHEN MATCHED AND (target.col2='update2') THEN UPDATE SET target.col2 = 2 | ||
| |WHEN NOT MATCHED AND (target.col2='insert1') | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, 1) | ||
| |WHEN NOT MATCHED AND (target.col2='insert2') | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, 2) | ||
| """.stripMargin, | ||
| MergeIntoTable( | ||
| SubqueryAlias("target", UnresolvedRelation(Seq("testcat1", "ns1", "ns2", "tbl"))), | ||
| SubqueryAlias("source", UnresolvedRelation(Seq("testcat2", "ns1", "ns2", "tbl"))), | ||
| EqualTo(UnresolvedAttribute("target.col1"), UnresolvedAttribute("source.col1")), | ||
| Seq(DeleteAction(Some(EqualTo(UnresolvedAttribute("target.col2"), Literal("delete")))), | ||
| UpdateAction(Some(EqualTo(UnresolvedAttribute("target.col2"), Literal("update1"))), | ||
| Seq(Assignment(UnresolvedAttribute("target.col2"), Literal(1)))), | ||
| UpdateAction(Some(EqualTo(UnresolvedAttribute("target.col2"), Literal("update2"))), | ||
| Seq(Assignment(UnresolvedAttribute("target.col2"), Literal(2))))), | ||
| Seq(InsertAction(Some(EqualTo(UnresolvedAttribute("target.col2"), Literal("insert1"))), | ||
| Seq(Assignment(UnresolvedAttribute("target.col1"), UnresolvedAttribute("source.col1")), | ||
| Assignment(UnresolvedAttribute("target.col2"), Literal(1)))), | ||
| InsertAction(Some(EqualTo(UnresolvedAttribute("target.col2"), Literal("insert2"))), | ||
| Seq(Assignment(UnresolvedAttribute("target.col1"), UnresolvedAttribute("source.col1")), | ||
| Assignment(UnresolvedAttribute("target.col2"), Literal(2))))))) | ||
| } | ||
|
|
||
| test("merge into table: at most one not matched clause") { | ||
| test("merge into table: only the last matched clause can omit the condition") { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shall we test the same thing for not matched clause?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
| val exc = intercept[ParseException] { | ||
| parsePlan( | ||
| """ | ||
| |MERGE INTO testcat1.ns1.ns2.tbl AS target | ||
| |USING testcat2.ns1.ns2.tbl AS source | ||
| |ON target.col1 = source.col1 | ||
| |WHEN MATCHED AND (target.col2='delete') THEN DELETE | ||
| |WHEN MATCHED AND (target.col2='update1') THEN UPDATE SET target.col2 = source.col2 | ||
| |WHEN NOT MATCHED AND (target.col2='insert1') | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, source.col2) | ||
| |WHEN NOT MATCHED AND (target.col2='insert2') | ||
| |WHEN MATCHED AND (target.col2 == 'update1') THEN UPDATE SET target.col2 = 1 | ||
| |WHEN MATCHED THEN UPDATE SET target.col2 = 2 | ||
| |WHEN MATCHED THEN DELETE | ||
| |WHEN NOT MATCHED AND (target.col2='insert') | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, source.col2) | ||
| """.stripMargin) | ||
| } | ||
|
|
||
| assert(exc.getMessage.contains("There should be at most 1 'WHEN NOT MATCHED' clause.")) | ||
| assert(exc.getMessage.contains("only the last MATCHED clause can omit the condition")) | ||
| } | ||
|
|
||
| test("merge into table: the first matched clause must have a condition if there's a second") { | ||
| test("merge into table: only the last not matched clause can omit the condition") { | ||
| val exc = intercept[ParseException] { | ||
| parsePlan( | ||
| """ | ||
| |MERGE INTO testcat1.ns1.ns2.tbl AS target | ||
| |USING testcat2.ns1.ns2.tbl AS source | ||
| |ON target.col1 = source.col1 | ||
| |WHEN MATCHED AND (target.col2 == 'update') THEN UPDATE SET target.col2 = source.col2 | ||
| |WHEN MATCHED THEN DELETE | ||
| |WHEN MATCHED THEN UPDATE SET target.col2 = source.col2 | ||
| |WHEN NOT MATCHED AND (target.col2='insert') | ||
| |WHEN NOT MATCHED AND (target.col2='insert1') | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, 1) | ||
| |WHEN NOT MATCHED | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, 2) | ||
| |WHEN NOT MATCHED | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, source.col2) | ||
| """.stripMargin) | ||
| } | ||
|
|
||
| assert(exc.getMessage.contains("the first MATCHED clause must have a condition")) | ||
| assert(exc.getMessage.contains("only the last NOT MATCHED clause can omit the condition")) | ||
| } | ||
|
|
||
| test("merge into table: there must be a when (not) matched condition") { | ||
|
|
@@ -1201,26 +1217,6 @@ class DDLParserSuite extends AnalysisTest { | |
| assert(exc.getMessage.contains("There must be at least one WHEN clause in a MERGE statement")) | ||
| } | ||
|
|
||
| test("merge into table: there can be only a single use DELETE or UPDATE") { | ||
| Seq("UPDATE SET *", "DELETE").foreach { op => | ||
| val exc = intercept[ParseException] { | ||
| parsePlan( | ||
| s""" | ||
| |MERGE INTO testcat1.ns1.ns2.tbl AS target | ||
| |USING testcat2.ns1.ns2.tbl AS source | ||
| |ON target.col1 = source.col1 | ||
| |WHEN MATCHED AND (target.col2='delete') THEN $op | ||
| |WHEN MATCHED THEN $op | ||
| |WHEN NOT MATCHED AND (target.col2='insert') | ||
| |THEN INSERT (target.col1, target.col2) values (source.col1, source.col2) | ||
| """.stripMargin) | ||
| } | ||
|
|
||
| assert(exc.getMessage.contains( | ||
| "UPDATE and DELETE can appear at most once in MATCHED clauses")) | ||
| } | ||
| } | ||
|
|
||
| test("show tables") { | ||
| comparePlans( | ||
| parsePlan("SHOW TABLES"), | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can still write
.children.isEmpty, then we don't need to changev2Commands.scalaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so, because the
childrenofInsertActionandUpdateActionactually includeconditionandassignments. There may be cases where there'reassignmentsandconditionbeing ignored butchildrenis nonEmpty.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
then this is an existing bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was a bug.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you send a new PR against branch 3.0 to fix this bug?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Submitted a pr at #28943.