[
https://issues.apache.org/jira/browse/SPARK-62238?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Jayadeep Jayaraman updated SPARK-62238:
---------------------------------------
Description:
h3. Problem
In {{RewriteMergeIntoTable}}, {{MERGE INTO}} statements that contain only
{{WHEN NOT MATCHED THEN INSERT}} clauses (and no {{WHEN MATCHED}} or {{WHEN NOT
MATCHED BY SOURCE}} clauses) are rewritten into {{InsertOnlyMerge}} — a
{{LeftAnti}} join followed by a plain append directly against the raw
{{DataSourceV2Relation}}, completely bypassing {{SupportsRowLevelOperations}}
and {{RowLevelOperationTable}}.
Because the target {{LeftAnti}} scan ({{SupportsRead#newScanBuilder}}) and the
append write ({{SupportsWrite#newWriteBuilder}}) are disconnected, DSv2
connectors (such as Apache Iceberg) never receive the read {{Scan}} state
(e.g., starting snapshot ID and pushed filters) or the {{MERGE}} operation
context during write planning and commit. As a result, insert-only {{MERGE}}
commits as an unvalidated blind append, violating {{SERIALIZABLE}} isolation.
h3. Concrete Anomalies
On a DSv2 table with {{SERIALIZABLE}} merge isolation (e.g., Apache Iceberg
with {{write.merge.isolation-level = 'serializable'}}):
* *1. Duplicate keys (phantom insert)*
** Initial table state: {{[(1, 'hr')]}}
** *Txn A* (insert-only {{MERGE}}): inserts {{(2, 'eng')}} if {{id = 2}} is
absent.
** *Txn B* (concurrent {{INSERT}}): {{INSERT INTO t VALUES (2, 'sales')}}
commits while Txn A is in flight.
** *Expected*: Txn A fails with a conflict error ({{ValidationException}}).
** *Actual*: Both commit, leaving duplicate {{id = 2}}: {{[(1, 'hr'), (2,
'eng'), (2, 'sales')]}}.
* *2. Non-serializable write skew (lost insert)*
** Initial table state: {{[(1, 'platinum'), (2, 'gold')]}}
** *Txn A* (insert-only {{MERGE}}): source {{[(2, 'bronze'), (9, 'bronze')]}},
inserting unmatched rows on {{t.id = s.id}}.
** *Txn B* (concurrent {{DELETE}}): {{DELETE FROM t WHERE id IN (2, 9)}}
commits while Txn A is in flight.
** *Valid serial outcomes*:
*** {{Txn A -> Txn B}}: {{[(1, 'platinum')]}}
*** {{Txn B -> Txn A}}: {{[(1, 'platinum'), (2, 'bronze'), (9, 'bronze')]}}
** *Actual*: Both commit, leaving {{[(1, 'platinum'), (9, 'bronze')]}} — an
impossible state under any serial execution.
Under the exact same interleaving, a regular {{MERGE}} (with any {{WHEN
MATCHED}} clause) goes through {{RowLevelOperation}} and properly aborts with
{{ValidationException}}.
h3. Proposed Solution
Add an opt-in {{default Optional<RowLevelOperationBuilder>
newInsertOnlyMergeOperationBuilder(RowLevelOperationInfo info)}} method to
{{SupportsRowLevelOperations}} (defaulting to {{Optional.empty()}}):
* Connectors that opt in receive a shared {{RowLevelOperationTable}}
({{isInsertOnly = true}}) connecting the {{LeftAnti}} target scan and the
append write, preserving the {{LeftAnti}} append fast path (no row/group
rewrites, intra-file residual filtering and runtime filtering remain enabled)
while allowing scan-to-write handoff for conflict validation.
* Connectors that do not opt in (or tables using {{V1_BATCH_WRITE}}) keep the
existing {{SupportsRead}} / {{SupportsWrite}} path with zero behavior change.
* Guard {{PushDownLeftSemiAntiJoin}} so a {{RowLevelOperationTable}} is not
cloned across {{Union}} branches and {{DataSourceV2RelationBase#computeStats}}
is not invoked before {{V2ScanRelationPushDown}}, and fall back to
{{SupportsWrite#newWriteBuilder}} in {{V2Writes}} if the optimizer eliminates
the target scan (e.g., {{ON false}} or empty source).
was:
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{}}}.
> [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: 4.1.0, 4.0.0, 4.2.0, 3.5.9
> Reporter: Jayadeep Jayaraman
> Priority: Major
> Labels: pull-request-available
> Fix For: 4.3.1
>
>
> h3. Problem
> In {{RewriteMergeIntoTable}}, {{MERGE INTO}} statements that contain only
> {{WHEN NOT MATCHED THEN INSERT}} clauses (and no {{WHEN MATCHED}} or {{WHEN
> NOT MATCHED BY SOURCE}} clauses) are rewritten into {{InsertOnlyMerge}} — a
> {{LeftAnti}} join followed by a plain append directly against the raw
> {{DataSourceV2Relation}}, completely bypassing {{SupportsRowLevelOperations}}
> and {{RowLevelOperationTable}}.
> Because the target {{LeftAnti}} scan ({{SupportsRead#newScanBuilder}}) and
> the append write ({{SupportsWrite#newWriteBuilder}}) are disconnected, DSv2
> connectors (such as Apache Iceberg) never receive the read {{Scan}} state
> (e.g., starting snapshot ID and pushed filters) or the {{MERGE}} operation
> context during write planning and commit. As a result, insert-only {{MERGE}}
> commits as an unvalidated blind append, violating {{SERIALIZABLE}} isolation.
> h3. Concrete Anomalies
> On a DSv2 table with {{SERIALIZABLE}} merge isolation (e.g., Apache Iceberg
> with {{write.merge.isolation-level = 'serializable'}}):
> * *1. Duplicate keys (phantom insert)*
> ** Initial table state: {{[(1, 'hr')]}}
> ** *Txn A* (insert-only {{MERGE}}): inserts {{(2, 'eng')}} if {{id = 2}} is
> absent.
> ** *Txn B* (concurrent {{INSERT}}): {{INSERT INTO t VALUES (2, 'sales')}}
> commits while Txn A is in flight.
> ** *Expected*: Txn A fails with a conflict error ({{ValidationException}}).
> ** *Actual*: Both commit, leaving duplicate {{id = 2}}: {{[(1, 'hr'), (2,
> 'eng'), (2, 'sales')]}}.
> * *2. Non-serializable write skew (lost insert)*
> ** Initial table state: {{[(1, 'platinum'), (2, 'gold')]}}
> ** *Txn A* (insert-only {{MERGE}}): source {{[(2, 'bronze'), (9,
> 'bronze')]}}, inserting unmatched rows on {{t.id = s.id}}.
> ** *Txn B* (concurrent {{DELETE}}): {{DELETE FROM t WHERE id IN (2, 9)}}
> commits while Txn A is in flight.
> ** *Valid serial outcomes*:
> *** {{Txn A -> Txn B}}: {{[(1, 'platinum')]}}
> *** {{Txn B -> Txn A}}: {{[(1, 'platinum'), (2, 'bronze'), (9, 'bronze')]}}
> ** *Actual*: Both commit, leaving {{[(1, 'platinum'), (9, 'bronze')]}} — an
> impossible state under any serial execution.
> Under the exact same interleaving, a regular {{MERGE}} (with any {{WHEN
> MATCHED}} clause) goes through {{RowLevelOperation}} and properly aborts with
> {{ValidationException}}.
> h3. Proposed Solution
> Add an opt-in {{default Optional<RowLevelOperationBuilder>
> newInsertOnlyMergeOperationBuilder(RowLevelOperationInfo info)}} method to
> {{SupportsRowLevelOperations}} (defaulting to {{Optional.empty()}}):
> * Connectors that opt in receive a shared {{RowLevelOperationTable}}
> ({{isInsertOnly = true}}) connecting the {{LeftAnti}} target scan and the
> append write, preserving the {{LeftAnti}} append fast path (no row/group
> rewrites, intra-file residual filtering and runtime filtering remain enabled)
> while allowing scan-to-write handoff for conflict validation.
> * Connectors that do not opt in (or tables using {{V1_BATCH_WRITE}}) keep the
> existing {{SupportsRead}} / {{SupportsWrite}} path with zero behavior change.
> * Guard {{PushDownLeftSemiAntiJoin}} so a {{RowLevelOperationTable}} is not
> cloned across {{Union}} branches and
> {{DataSourceV2RelationBase#computeStats}} is not invoked before
> {{V2ScanRelationPushDown}}, and fall back to
> {{SupportsWrite#newWriteBuilder}} in {{V2Writes}} if the optimizer eliminates
> the target scan (e.g., {{ON false}} or empty source).
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]