MERGE INTO
Upsert and conditional delete on Iceberg tables.
Overview
MERGE INTO applies upsert and conditional delete logic to an Iceberg table using copy-on-write semantics. The target table must be managed by a registered KrishivCatalog Iceberg catalog.
Preview: Copy-on-write semantics are implemented. Merge-on-read and distributed atomic commit certification are ongoing.
Syntax
MERGE INTO <target_table> [AS <alias>]
USING <source_table_or_subquery> [AS <alias>]
ON <join_condition>
WHEN MATCHED [AND <condition>] THEN UPDATE SET <col> = <expr> [, ...]
WHEN MATCHED [AND <condition>] THEN DELETE
WHEN NOT MATCHED [AND <condition>] THEN INSERT (<cols>) VALUES (<exprs>)
Example
MERGE INTO inventory AS tgt
USING incoming_stock AS src
ON tgt.product_id = src.product_id
WHEN MATCHED AND src.quantity = 0 THEN DELETE
WHEN MATCHED THEN UPDATE SET tgt.quantity = tgt.quantity + src.quantity
WHEN NOT MATCHED THEN INSERT (product_id, quantity) VALUES (src.product_id, src.quantity)
Return
The statement returns a single-row result with a merged_rows integer count.
Related DML
For Iceberg tables, DELETE FROM <table> WHERE <predicate> and UPDATE <table> SET <col> = <expr> WHERE <predicate> are also intercepted and routed through copy-on-write Iceberg delete/update paths when the table is registered under a KrishivCatalog.