Jayadeep Jayaraman created SPARK-62238:
------------------------------------------

             Summary: [SQL] Insert-only MERGE INTO bypasses 
SupportsRowLevelOperations, breaking DSv2 isolation & conflict detection
                 Key: SPARK-62238
                 URL: https://issues.apache.org/jira/browse/SPARK-62238
             Project: Spark
          Issue Type: Bug
          Components: Spark Core
    Affects Versions: 3.5.9, 4.2.0, 4.0.0, 4.1.0
            Reporter: Jayadeep Jayaraman
             Fix For: 4.3.1


In `{{{}RewriteMergeIntoTable{}}}, {{{}MERGE INTO{}}}` statements with only 
{{WHEN NOT MATCHED THEN INSERT}} actions (and no {{WHEN MATCHED}} / {{WHEN NOT 
MATCHED BY SOURCE}} clauses) are rewritten into {{InsertOnlyMerge}} (a 
{{LeftAnti}} join + plain append) directly against the raw 
{{{}DataSourceV2Relation{}}}, completely bypassing 
{{SupportsRowLevelOperations}} / {{{}RowLevelOperationTable{}}}.

Because the target {{LeftAnti}} scan ({{{}table.newScanBuilder{}}}) and the 
append write ({{{}table.newWriteBuilder{}}}) are disconnected, DSv2 connectors 
(such as Apache Iceberg) never receive the read {{Scan}} (e.g., starting 
snapshot ID and pushed filters) or the {{MERGE}} operation context during write 
planning/commit. As a result, insert-only {{MERGE}} commits as an unvalidated 
blind append, violating {{SERIALIZABLE}} isolation.



With a DSv2 connector supporting {{SupportsRowLevelOperations}} (e.g., Apache 
Iceberg with {{{}write.merge.isolation-level = 'serializable'{}}}):
 # *Duplicate keys (phantom insert):* An insert-only {{MERGE}} inserting {{(2, 
'eng')}} if {{id = 2}} is absent runs concurrently with {{{}INSERT INTO t 
VALUES (2, 'sales'){}}}. Both commit without a conflict error, leaving 
duplicate {{{}id = 2{}}}.
 # *Non-serializable write skew (lost insert):* Starting from {{{}[(1, 
'platinum'), (2, 'gold')]{}}}, an insert-only {{MERGE}} with source {{[(2, 
'bronze'), (9, 'bronze')]}} runs concurrently with {{{}DELETE FROM t WHERE id 
IN (2, 9){}}}. Both commit, leaving {{[(1, 'platinum'), (9, 'bronze')]}} — 
impossible under either serial order ({{{}MERGE → DELETE{}}} gives {{{}[(1, 
'platinum')]{}}}; {{DELETE → MERGE}} gives {{{}[(1, 'platinum'), (2, 'bronze'), 
(9, 'bronze')]{}}}).

Under the exact same interleaving, a regular {{MERGE}} (with any {{WHEN 
MATCHED}} clause) goes through {{RowLevelOperation}} and properly fails with 
{{{}ValidationException{}}}.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to