- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Sunday
I use databricks 17.3 runtime.
I try to run following code.
CREATE OR REPLACE TABLE default.target_table (
key1 INT,
key2 INT,
key3 INT,
val STRING
) USING DELTA;
INSERT INTO target_table(key1, key2, key3, val) VALUES
(1, 1, 1, 'a');
CREATE OR REPLACE TABLE default.source_table (
key1 INT,
key2 INT,
key3 INT,
val STRING,
rn1 INT,
rn2 INT,
rn3 INT
) USING DELTA;
INSERT INTO source_table(key1, key2, key3, val, rn1, rn2, rn3) VALUES
(1, 1, 1, 'a1', 1, 0, 0),
(1, 1, 2, 'a2', 0, 1, 0),
(1, 3, 3, 'a3', 0, 0, 1);
MERGE INTO target_table AS target
USING source_table AS source
ON target.key1 = source.key1
WHEN MATCHED AND target.key2 = source.key2 AND target.key3 = source.key3 AND source.rn1 = 1 THEN
UPDATE SET val = source.val
WHEN MATCHED AND target.key2 = source.key2 AND source.rn2 = 1 THEN
UPDATE SET val = source.val
WHEN MATCHED AND source.rn3 = 1 THEN
UPDATE SET val = source.val
;
and got following error:
[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 https://docs.microsoft.com/azure/databricks/delta/merge#merge-error SQLSTATE: 21506
which is a strange, because for each of “MATCHED AND” clause there is exactly only one row matching.
Above code I created to demonstrate the real-life problem I spotted.