aokolnychyi commented on a change in pull request #2189:
URL: https://github.com/apache/iceberg/pull/2189#discussion_r567561900



##########
File path: 
spark3-extensions/src/main/scala/org/apache/spark/sql/catalyst/optimizer/RewriteMergeInto.scala
##########
@@ -158,6 +144,43 @@ case class RewriteMergeInto(spark: SparkSession) extends 
Rule[LogicalPlan] with
     }
   }
 
+  // when there are no matched actions, use a left anti join to remove any 
matching rows and rewrite to use
+  // append instead of replace. only unmatched source rows are passed to the 
merge and actions are all inserts.
+  private def buildInsertOnlyMergePlan(
+      targetTableScan: LogicalPlan,
+      targetTableOutput: Seq[AttributeReference],
+      source: LogicalPlan,
+      cond: Expression,
+      notMatchedActions: Seq[MergeAction]): LogicalPlan = {
+
+    notMatchedActions match {
+      case Seq(insertAction: InsertAction) =>
+        val filteredSource = insertAction.condition match {
+          case Some(insertCond) => Filter(insertCond, source)
+          case None => source
+        }
+        val outputExprs = insertAction.assignments.map(_.value)
+        val outputColNames = targetTableOutput.map(_.name)
+        val outputCols = outputExprs.zip(outputColNames).map { case (expr, 
name) => Alias(expr, name)() }
+        val joinPlan = Join(filteredSource, targetTableScan, LeftAnti, 
Some(cond), JoinHint.NONE)
+        Project(outputCols, joinPlan)
+
+      case _ =>
+        val joinPlan = Join(source, targetTableScan, LeftAnti, Some(cond), 
JoinHint.NONE)

Review comment:
       We may optimize this case in the future using CASE WHEN.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to