gengliangwang commented on code in PR #36445:
URL: https://github.com/apache/spark/pull/36445#discussion_r870503967
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/command/PlanResolutionSuite.scala:
##########
@@ -1510,6 +1510,152 @@ class PlanResolutionSuite extends AnalysisTest {
case other => fail("Expect MergeIntoTable, but got:\n" +
other.treeString)
}
+
+ // default columns (implicit)
+ val sql6 =
+ s"""
+ |MERGE INTO $target AS target
+ |USING $source AS source
+ |ON target.i = source.i
+ |WHEN MATCHED AND (target.s='delete') THEN DELETE
+ |WHEN MATCHED AND (target.s='update')
+ | THEN UPDATE SET target.s = DEFAULT
+ |WHEN NOT MATCHED AND (source.s='insert')
+ | THEN INSERT (target.i, target.s) values (DEFAULT, DEFAULT)
+ """.stripMargin
+ parseAndResolve(sql6) match {
+ case m: MergeIntoTable =>
+ val source = m.sourceTable
+ val target = m.targetTable
+ val ti = target.output.find(_.name ==
"i").get.asInstanceOf[AttributeReference]
+ val si = source.output.find(_.name ==
"i").get.asInstanceOf[AttributeReference]
+ m.mergeCondition match {
+ case EqualTo(l: AttributeReference, r: AttributeReference) =>
+ assert(l.sameRef(ti) && r.sameRef(si))
+ case Literal(_, BooleanType) => // this is acceptable as a merge
condition
+ case other => fail("unexpected merge condition " + other)
+ }
+ assert(m.matchedActions.length == 2)
+ val first = m.matchedActions(0)
+ first match {
+ case DeleteAction(Some(EqualTo(_: AttributeReference,
StringLiteral("delete")))) =>
+ case other => fail("unexpected first matched action " + other)
+ }
+ val second = m.matchedActions(1)
+ second match {
+ case UpdateAction(Some(EqualTo(_: AttributeReference,
StringLiteral("update"))),
+ Seq(Assignment(
+ _: AttributeReference, AnsiCast(Literal(null, _),
StringType, _)))) =>
+ case other => fail("unexpected second matched action " + other)
+ }
+ assert(m.notMatchedActions.length == 1)
+ val negative = m.notMatchedActions(0)
+ negative match {
+ case InsertAction(Some(EqualTo(_: AttributeReference,
StringLiteral("insert"))),
+ Seq(Assignment(i: AttributeReference, AnsiCast(Literal(null, _),
IntegerType, _)),
+ Assignment(s: AttributeReference, AnsiCast(Literal(null, _),
StringType, _)))) =>
+ assert(i.name == "i")
+ assert(s.name == "s")
+ case other => fail("unexpected not matched action " + other)
+ }
+
+ case other =>
+ fail("Expect MergeIntoTable, but got:\n" + other.treeString)
+ }
+ }
+
+
+ // default columns (explicit)
+ val mergeDefault1 =
+ s"""
+ |MERGE INTO defaultvalues AS target
+ |USING v2Table1 AS source
+ |ON target.i = source.i
+ |WHEN MATCHED AND (target.s='delete') THEN DELETE
+ |WHEN MATCHED AND (target.s='update')
+ | THEN UPDATE SET target.s = DEFAULT
+ |WHEN NOT MATCHED AND (source.s='insert')
+ | THEN INSERT (target.i, target.s) values (DEFAULT, DEFAULT)
+ """.stripMargin
+ parseAndResolve(mergeDefault1, true) match {
+ case m: MergeIntoTable =>
+ val cond = m.mergeCondition
+ cond match {
+ case EqualTo(l: UnresolvedAttribute, r: UnresolvedAttribute) =>
+ assert(l.nameParts.last == "i")
+ assert(r.nameParts.last == "i")
+ case Literal(_, BooleanType) => // this is acceptable as a merge
condition
+ case other => fail("unexpected merge condition " + other)
+ }
+ assert(m.matchedActions.length == 2)
+ val first = m.matchedActions(0)
+ first match {
+ case DeleteAction(Some(EqualTo(_: UnresolvedAttribute,
StringLiteral("delete")))) =>
+ case other => fail("unexpected first matched action " + other)
+ }
+ val second = m.matchedActions(1)
+ second match {
+ case UpdateAction(Some(EqualTo(_: UnresolvedAttribute,
StringLiteral("update"))),
+ Seq(Assignment(_: UnresolvedAttribute, Literal(42, IntegerType)))) =>
+ case other => fail("unexpected second matched action " + other)
+ }
+ assert(m.notMatchedActions.length == 1)
+ val negative = m.notMatchedActions(0)
+ negative match {
+ case InsertAction(Some(EqualTo(_: UnresolvedAttribute,
StringLiteral("insert"))),
+ Seq(
+ Assignment(_: UnresolvedAttribute, Literal(true, BooleanType)),
+ Assignment(_: UnresolvedAttribute, Literal(42, IntegerType)))) =>
+ case other => fail("unexpected not matched action " + other)
+ }
+
+ case other =>
+ fail("Expect MergeIntoTable, but got:\n" + other.treeString)
+ }
+ val mergeDefault2 =
Review Comment:
What's this statement for? It would be great to have a simple comment
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]