iyashk-DB
Databricks Employee
Databricks Employee

This behaviour is expected with Delta MERGE in your runtime and is due to how duplicate matches are detected.

The error DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE occurs because Delta requires exactly one source row to match a target row during a MERGE. In DBR 16.0+ (including 17.3), Delta checks for duplicates using both the ON clause and the WHEN MATCHED conditions. So even if each WHEN clause seems to apply to a different row, if multiple source rows still match the same target row overall, the MERGE is considered ambiguous and fails.

In your case, the ON condition is only key1, and all source rows share key1 = 1. This causes all of them to match the same target row, triggering the error under the stricter semantics in newer runtimes.

To fix this safely, you have two options. Either strengthen the ON clause so it uniquely identifies the target row (for example, include all key columns), or preprocess the source data to ensure only one source row exists per target row before running MERGE (for example, by ranking and picking a single “best” row).

The key takeaway is that MERGE must be unambiguous. In newer DBRs, separating logic into multiple WHEN clauses is no longer enough—your source data or join condition must guarantee a single match per target row.