<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Strange DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE error in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143024#M52078</link>
    <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/112558"&gt;@iyashk-DB&lt;/a&gt;&amp;nbsp;for explanation and solution to my my problem.&lt;BR /&gt;Option 2 is what I need.&lt;/P&gt;</description>
    <pubDate>Mon, 05 Jan 2026 12:27:00 GMT</pubDate>
    <dc:creator>lziolkow2</dc:creator>
    <dc:date>2026-01-05T12:27:00Z</dc:date>
    <item>
      <title>Strange DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE error</title>
      <link>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/142981#M52063</link>
      <description>&lt;P&gt;I use databricks 17.3 runtime.&lt;BR /&gt;I try to run following code.&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;CREATE OR REPLACE TABLE default.target_table (&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;key1 INT,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;key2 INT,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;key3 INT,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;val STRING&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;) USING DELTA;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;INSERT INTO target_table(key1, key2, key3, val) VALUES&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;(1, 1, 1, 'a');&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;CREATE OR REPLACE TABLE default.source_table (&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;key1 INT,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;key2 INT,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;key3 INT,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;val STRING,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;rn1 INT,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;rn2 INT,&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;rn3 INT&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;) USING DELTA;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;INSERT INTO source_table(key1, key2, key3, val, rn1, rn2, rn3) VALUES&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;(1, 1, 1, 'a1', 1, 0, 0),&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;(1, 1, 2, 'a2', 0, 1, 0),&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;(1, 3, 3, 'a3', 0, 0, 1);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;MERGE INTO target_table AS target&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;USING source_table AS source&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;ON target.key1 = source.key1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;WHEN MATCHED AND target.key2 = source.key2 AND target.key3 = source.key3 AND source.rn1 = 1 THEN&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;UPDATE SET val = source.val&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;WHEN MATCHED AND target.key2 = source.key2 AND source.rn2 = 1 THEN&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;UPDATE SET val = source.val&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;WHEN MATCHED AND source.rn3 = 1 THEN&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;UPDATE SET val = source.val&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;and got following error:&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;[DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE] Cannot perform Merge as multiple source rows matched and attempted to modify the same target row in the Delta table in possibly conflicting ways. By SQL semantics of Merge, when multiple source rows match on the same target row, the result may be ambiguous as it is unclear which source row should be used to update or delete the matching target row. You can preprocess the source table to eliminate the possibility of multiple matches. Please refer to &lt;A href="https://docs.microsoft.com/azure/databricks/delta/merge#merge-error" target="_blank"&gt;https://docs.microsoft.com/azure/databricks/delta/merge#merge-error&lt;/A&gt; SQLSTATE: 21506&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;which is a strange, because for each of “MATCHED AND” clause there is exactly only one row matching.&lt;BR /&gt;Above code I created to demonstrate the real-life problem I spotted.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jan 2026 07:59:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/142981#M52063</guid>
      <dc:creator>lziolkow2</dc:creator>
      <dc:date>2026-01-05T07:59:31Z</dc:date>
    </item>
    <item>
      <title>Re: Strange DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE error</title>
      <link>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143017#M52075</link>
      <description>&lt;P&gt;This behaviour is expected with Delta MERGE in your runtime and is due to how duplicate matches are detected.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;
&lt;P&gt;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).&lt;/P&gt;
&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jan 2026 11:41:54 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143017#M52075</guid>
      <dc:creator>iyashk-DB</dc:creator>
      <dc:date>2026-01-05T11:41:54Z</dc:date>
    </item>
    <item>
      <title>Re: Strange DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE error</title>
      <link>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143018#M52076</link>
      <description>&lt;P&gt;Option 1: Strengthen the ON clause to uniquely identify the target row&lt;BR /&gt;If you intend to update only when the full key matches, make the ON clause include all keys:&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;MERGE INTO target_table AS target
USING source_table AS source
ON target.key1 = source.key1
AND target.key2 = source.key2
AND target.key3 = source.key3
WHEN MATCHED AND source.rn1 = 1 THEN
UPDATE SET val = source.val;&lt;/LI-CODE&gt;
&lt;P&gt;This guarantees at most one source row can match a given target row (assuming your keys are unique), preventing the ambiguity error.&lt;/P&gt;
&lt;P&gt;Option 2: Preselect exactly one source row per target row (fallback priority)&lt;BR /&gt;If you want “priority” matching (exact triplet if present; else same key2; else any rn3), compute the single best source row per target.key1 first, then MERGE with an ON that still only joins by key1.&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;WITH candidates AS (
SELECT
t.key1,
t.key2,
t.key3,
s.val,
CASE
WHEN t.key2 = s.key2 AND t.key3 = s.key3 AND s.rn1 = 1 THEN 3
WHEN t.key2 = s.key2 AND s.rn2 = 1 THEN 2
WHEN s.rn3 = 1 THEN 1
ELSE 0
END AS priority
FROM target_table t
JOIN source_table s
ON t.key1 = s.key1
),
picked AS (
SELECT key1, val
FROM (
SELECT
key1, val, priority,
ROW_NUMBER() OVER (PARTITION BY key1 ORDER BY priority DESC) AS rnk
FROM candidates
WHERE priority &amp;gt; 0
) q
WHERE rnk = 1
)
MERGE INTO target_table AS target
USING picked AS source
ON target.key1 = source.key1
WHEN MATCHED THEN
UPDATE SET target.val = source.val;&lt;/LI-CODE&gt;
&lt;P&gt;This ensures there’s exactly one source row per target.key1 before MERGE, so MERGE can’t see multiple matches and won’t error.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jan 2026 11:43:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143018#M52076</guid>
      <dc:creator>iyashk-DB</dc:creator>
      <dc:date>2026-01-05T11:43:34Z</dc:date>
    </item>
    <item>
      <title>Re: Strange DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE error</title>
      <link>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143019#M52077</link>
      <description>&lt;P&gt;Hi, you need to put all of the keys in the ON part of the clause rather then in the where condition. This code works:&lt;/P&gt;
&lt;LI-CODE lang="python"&gt;MERGE INTO target_table AS target
USING source_table AS source
ON target.key1 = source.key1
AND target.key2 = source.key2
AND target.key3 = source.key3
WHEN MATCHED AND source.rn1 = 1 THEN
  UPDATE SET val = source.val
WHEN MATCHED AND source.rn2 = 1 THEN
  UPDATE SET val = source.val
WHEN MATCHED AND source.rn3 = 1 THEN
  UPDATE SET val = source.val&lt;/LI-CODE&gt;</description>
      <pubDate>Mon, 05 Jan 2026 11:49:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143019#M52077</guid>
      <dc:creator>emma_s</dc:creator>
      <dc:date>2026-01-05T11:49:46Z</dc:date>
    </item>
    <item>
      <title>Re: Strange DELTA_MULTIPLE_SOURCE_ROW_MATCHING_TARGET_ROW_IN_MERGE error</title>
      <link>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143024#M52078</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/112558"&gt;@iyashk-DB&lt;/a&gt;&amp;nbsp;for explanation and solution to my my problem.&lt;BR /&gt;Option 2 is what I need.&lt;/P&gt;</description>
      <pubDate>Mon, 05 Jan 2026 12:27:00 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/strange-delta-multiple-source-row-matching-target-row-in-merge/m-p/143024#M52078</guid>
      <dc:creator>lziolkow2</dc:creator>
      <dc:date>2026-01-05T12:27:00Z</dc:date>
    </item>
  </channel>
</rss>

