Skip to content

Commit c53e95c

Browse files
johanl-dballisonport-db
authored andcommitted
Enable SQL support for WHEN NOT MATCHED BY SOURCE
# Description The SQL syntax for merge with WHEN NOT MATCHED BY SOURCE clauses was shipped with Spark 3.4. Now that Delta picked up Spark 3.4, we can enable SQL support and mix in SQL tests for WHEN NOT MATCHED BY SOURCE. Existing tests for WHEN NOT MATCHED BY SOURCE are now run in the Merge SQL suite. Closes #1740 GitOrigin-RevId: 1ddd1216e13f854901da47896936527618ea4dca
1 parent c180d15 commit c53e95c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

core/src/main/scala/org/apache/spark/sql/delta/DeltaAnalysis.scala

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,18 @@ class DeltaAnalysis(session: SparkSession)
459459
s"${other.prettyName} clauses cannot be part of the WHEN NOT MATCHED clause in MERGE " +
460460
"INTO.")
461461
}
462+
val notMatchedBySourceActions = merge.notMatchedBySourceActions.map {
463+
case update: UpdateAction =>
464+
DeltaMergeIntoNotMatchedBySourceUpdateClause(
465+
update.condition,
466+
DeltaMergeIntoClause.toActions(update.assignments))
467+
case delete: DeleteAction =>
468+
DeltaMergeIntoNotMatchedBySourceDeleteClause(delete.condition)
469+
case other =>
470+
throw new IllegalArgumentException(
471+
s"${other.prettyName} clauses cannot be part of the WHEN NOT MATCHED BY SOURCE " +
472+
"clause in MERGE INTO.")
473+
}
462474
// rewrites Delta from V2 to V1
463475
val newTarget =
464476
stripTempViewForMergeWrapper(merge.targetTable).transformUp { case DeltaRelation(lr) => lr }
@@ -468,7 +480,7 @@ class DeltaAnalysis(session: SparkSession)
468480
newTarget,
469481
merge.sourceTable,
470482
merge.mergeCondition,
471-
matchedActions ++ notMatchedActions
483+
matchedActions ++ notMatchedActions ++ notMatchedBySourceActions
472484
)
473485

474486
DeltaMergeInto.resolveReferencesAndSchema(deltaMerge, conf)(tryResolveReferences(session))

core/src/test/scala/org/apache/spark/sql/delta/MergeIntoSQLSuite.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ import org.apache.spark.sql.functions.udf
3030
import org.apache.spark.sql.internal.SQLConf
3131
import org.apache.spark.sql.types.{IntegerType, StructField, StructType}
3232

33-
class MergeIntoSQLSuite extends MergeIntoSuiteBase with DeltaSQLCommandTest
33+
class MergeIntoSQLSuite extends MergeIntoSuiteBase with MergeIntoNotMatchedBySourceSuite
34+
with DeltaSQLCommandTest
3435
with DeltaTestUtilsForTempViews {
3536

3637
import testImplicits._

0 commit comments

Comments
 (0)