aokolnychyi commented on code in PR #41448:
URL: https://github.com/apache/spark/pull/41448#discussion_r1216251279


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteMergeIntoTable.scala:
##########
@@ -0,0 +1,347 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.sql.catalyst.analysis
+
+import org.apache.spark.sql.AnalysisException
+import org.apache.spark.sql.catalyst.expressions.{Alias, And, Attribute, 
AttributeReference, Expression, IsNotNull, MetadataAttribute, 
MonotonicallyIncreasingID, OuterReference, PredicateHelper}
+import org.apache.spark.sql.catalyst.expressions.Literal.{FalseLiteral, 
TrueLiteral}
+import org.apache.spark.sql.catalyst.plans.{FullOuter, Inner, JoinType, 
LeftAnti, LeftOuter, RightOuter}
+import org.apache.spark.sql.catalyst.plans.logical.{AppendData, DeleteAction, 
Filter, HintInfo, InsertAction, Join, JoinHint, LogicalPlan, MergeAction, 
MergeIntoTable, MergeRows, NO_BROADCAST_AND_REPLICATION, Project, UpdateAction, 
WriteDelta}
+import org.apache.spark.sql.catalyst.plans.logical.MergeRows.{Instruction, 
Keep, ROW_ID, Split}
+import org.apache.spark.sql.catalyst.util.RowDeltaUtils.OPERATION_COLUMN
+import org.apache.spark.sql.connector.catalog.SupportsRowLevelOperations
+import org.apache.spark.sql.connector.write.{RowLevelOperationTable, 
SupportsDelta}
+import org.apache.spark.sql.connector.write.RowLevelOperation.Command.MERGE
+import org.apache.spark.sql.execution.datasources.v2.DataSourceV2Relation
+import org.apache.spark.sql.types.IntegerType
+import org.apache.spark.sql.util.CaseInsensitiveStringMap
+
+/**
+ * A rule that rewrites MERGE operations using plans that operate on 
individual or groups of rows.
+ *
+ * This rule assumes the commands have been fully resolved and all assignments 
have been aligned.
+ */
+object RewriteMergeIntoTable extends RewriteRowLevelCommand with 
PredicateHelper {
+
+  private final val ROW_FROM_SOURCE = "__row_from_source"
+  private final val ROW_FROM_TARGET = "__row_from_target"
+
+  override def apply(plan: LogicalPlan): LogicalPlan = plan resolveOperators {
+    case m @ MergeIntoTable(aliasedTable, source, cond, matchedActions, 
notMatchedActions,
+        notMatchedBySourceActions) if m.resolved && m.rewritable && m.aligned 
&&
+        matchedActions.isEmpty && notMatchedActions.size == 1 &&
+        notMatchedBySourceActions.isEmpty =>
+
+      EliminateSubqueryAliases(aliasedTable) match {
+        case r: DataSourceV2Relation =>
+          // NOT MATCHED conditions may only refer to columns in source so 
they can be pushed down
+          val insertAction = notMatchedActions.head.asInstanceOf[InsertAction]
+          val filteredSource = insertAction.condition match {
+            case Some(insertCond) => Filter(insertCond, source)
+            case None => source
+          }
+
+          // there is only one NOT MATCHED action, use a left anti join to 
remove any matching rows
+          // and switch to using a regular append instead of a row-level MERGE 
operation
+          // only unmatched source rows that match the condition are appended 
to the table
+          val joinPlan = Join(filteredSource, r, LeftAnti, Some(cond), 
JoinHint.NONE)
+
+          val output = insertAction.assignments.map(_.value)
+          val outputColNames = r.output.map(_.name)
+          val projectList = output.zip(outputColNames).map { case (expr, name) 
=>
+            Alias(expr, name)()
+          }
+          val project = Project(projectList, joinPlan)
+
+          AppendData.byPosition(r, project)
+
+        case _ =>
+          m
+      }
+
+    case m @ MergeIntoTable(aliasedTable, source, cond, matchedActions, 
notMatchedActions,

Review Comment:
   This is a special case when there are only NOT MATCHED actions (just 1 is 
handled above).



-- 
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-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to