szehon-ho commented on code in PR #60559:
URL: https://github.com/apache/spark/pull/60559#discussion_r4660190312


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteMergeIntoTable.scala:
##########
@@ -171,6 +145,56 @@ object RewriteMergeIntoTable extends 
RewriteRowLevelCommand with PredicateHelper
     case _ => false
   }
 
+  /**
+   * Tries to build a Project/Filter plan for a single INSERT action.
+   *
+   * Note: Pushing the action condition below the anti join can evaluate it 
for source rows that
+   * match the target. Failures deferred inside IF/CASE in that condition must 
stay in MergeRows
+   * so matching rows skip the condition.
+   *
+   * Note: Later optimizer rules may introduce failing constants that this 
check cannot see.
+   */
+  private def tryBuildInsertOnlyMergeProject(
+      relation: DataSourceV2Relation,
+      source: LogicalPlan,
+      cond: Expression,
+      insertAction: InsertAction): Option[Project] = {
+    // Run ReplaceExpressions, ComputeCurrentTime and SpecialDatetimeValues 
before ConstantFolding
+    // to detect failures that Project/Filter would expose. A failed fold can 
throw directly or
+    // be tagged for runtime evaluation. MergeRows evaluates instructions 
conditionally, like
+    // IF/CASE branches, so failures occur only if the failing expression is 
reached.
+    val folded = try {
+      val replaced = ReplaceExpressions.replace(insertAction.clone())
+      val withCurrentTime = ComputeCurrentTime.applyForExpression(replaced)
+      val rewritten = SpecialDatetimeValues.applyForExpression(withCurrentTime)
+      ConstantFolding.constantFolding(rewritten).asInstanceOf[InsertAction]
+    } catch {
+      case NonFatal(e) =>
+        logInfo(log"Keeping MergeRows for insert-only MERGE because expression 
preparation " +
+          log"or constant folding failed.", e)
+        return None
+    }
+
+    if (folded.condition.exists(

Review Comment:
   Single-insert actions with subquery or window values now retain the existing 
Project/Filter plan before any folding probe. Added coverage combining these 
values with a deferred failure in the action condition, and with a failing 
constant in another value. This preserves their existing behavior; deferred 
failures for these Project-only forms remain outside this patch's scope.
   
   ---
   <!-- GITHUB_MCP_FOOTER: This attribution is automatically appended by GitHub 
MCP. -->
   _This comment was generated with [GitHub MCP](http://go/mcps)._



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteMergeIntoTable.scala:
##########
@@ -171,6 +145,56 @@ object RewriteMergeIntoTable extends 
RewriteRowLevelCommand with PredicateHelper
     case _ => false
   }
 
+  /**
+   * Tries to build a Project/Filter plan for a single INSERT action.
+   *
+   * Note: Pushing the action condition below the anti join can evaluate it 
for source rows that
+   * match the target. Failures deferred inside IF/CASE in that condition must 
stay in MergeRows
+   * so matching rows skip the condition.
+   *
+   * Note: Later optimizer rules may introduce failing constants that this 
check cannot see.
+   */
+  private def tryBuildInsertOnlyMergeProject(
+      relation: DataSourceV2Relation,
+      source: LogicalPlan,
+      cond: Expression,
+      insertAction: InsertAction): Option[Project] = {
+    // Run ReplaceExpressions, ComputeCurrentTime and SpecialDatetimeValues 
before ConstantFolding
+    // to detect failures that Project/Filter would expose. A failed fold can 
throw directly or
+    // be tagged for runtime evaluation. MergeRows evaluates instructions 
conditionally, like
+    // IF/CASE branches, so failures occur only if the failing expression is 
reached.
+    val folded = try {
+      val replaced = ReplaceExpressions.replace(insertAction.clone())
+      val withCurrentTime = ComputeCurrentTime.applyForExpression(replaced)

Review Comment:
   After ComputeCurrentTime, expressions that still contain CURRENT_LIKE now 
retain conditional evaluation in MergeRows. This covers catalog/user functions 
without adding CatalogManager to the analyzer rule. Added skipped and selected 
SQL regressions for current_database, current_catalog, current_user and 
current_path, plus current_timezone.
   
   ---
   <!-- GITHUB_MCP_FOOTER: This attribution is automatically appended by GitHub 
MCP. -->
   _This comment was generated with [GitHub MCP](http://go/mcps)._



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/RewriteMergeIntoTable.scala:
##########
@@ -119,7 +87,13 @@ object RewriteMergeIntoTable extends RewriteRowLevelCommand 
with PredicateHelper
             output = generateExpandOutput(dataAttrs(r), outputs),
             joinPlan)
 
-          InsertOnlyMerge(r, mergeRows)
+          val query = dataNotMatchedActions match {
+            case Seq(insertAction: InsertAction) =>
+              tryBuildInsertOnlyMergeProject(r, source, cond, 
insertAction).getOrElse(mergeRows)

Review Comment:
   The condition and values are now checked separately. When only a value fails 
to fold, a condition that passes its check is pushed below the anti join and 
the MergeRows instruction uses TRUE. Added Catalyst and SQL plan assertions for 
that shape.
   
   ---
   <!-- GITHUB_MCP_FOOTER: This attribution is automatically appended by GitHub 
MCP. -->
   _This comment was generated with [GitHub MCP](http://go/mcps)._



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

Reply via email to