Skip to content

Commit f96475c

Browse files
Add SQL Support for MERGE INTO In Presto #20578 (documentation)
Added MERGE INTO statement documentation.
1 parent 489b2fb commit f96475c

File tree

3 files changed

+88
-4
lines changed

3 files changed

+88
-4
lines changed

presto-docs/src/main/sphinx/connector/iceberg.rst

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,8 @@ SQL Operation Presto Java Presto C++ Comments
12771277
``DESCRIBE`` Yes Yes
12781278

12791279
``UPDATE`` Yes No
1280+
1281+
``MERGE`` Yes No
12801282
============================== ============= ============ ============================================================================
12811283

12821284
The Iceberg connector supports querying and manipulating Iceberg tables and schemas
@@ -1727,11 +1729,11 @@ For example, ``DESCRIBE`` from the partitioned Iceberg table ``customer``:
17271729
comment | varchar | |
17281730
(3 rows)
17291731

1730-
UPDATE
1731-
^^^^^^
1732+
UPDATE and MERGE
1733+
^^^^^^^^^^^^^^^^
17321734

1733-
The Iceberg connector supports :doc:`../sql/update` operations on Iceberg
1734-
tables. Only some tables support updates. These tables must be at minimum format
1735+
The Iceberg connector supports :doc:`../sql/update` and :doc:`../sql/merge` operations on Iceberg
1736+
tables. Only some tables support them. These tables must be at minimum format
17351737
version 2, and the ``write.update.mode`` must be set to `merge-on-read`.
17361738

17371739
.. code-block:: sql
@@ -1751,6 +1753,16 @@ updates.
17511753

17521754
Query 20250204_010445_00022_ymwi5 failed: Iceberg table updates require at least format version 2 and update mode must be merge-on-read
17531755

1756+
Iceberg tables do not support running multiple ``MERGE`` statements on the same table in parallel. If two or more ``MERGE`` operations are executed concurrently on the same Iceberg table:
1757+
1758+
* The first operation to complete will succeed.
1759+
* Subsequent operations will fail due to conflicting writes and will return the following error:
1760+
1761+
.. code-block:: text
1762+
1763+
Failed to commit Iceberg update to table: <table name>
1764+
Found conflicting files that can contain records matching true
1765+
17541766
Schema Evolution
17551767
----------------
17561768

presto-docs/src/main/sphinx/sql.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ This chapter describes the SQL syntax used in Presto.
3838
sql/grant
3939
sql/grant-roles
4040
sql/insert
41+
sql/merge
4142
sql/prepare
4243
sql/refresh-materialized-view
4344
sql/reset-session
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
=====
2+
MERGE
3+
=====
4+
5+
Synopsis
6+
--------
7+
8+
.. code-block:: text
9+
10+
MERGE INTO target_table [ [ AS ] target_alias ]
11+
USING { source_table | query } [ [ AS ] source_alias ]
12+
ON search_condition
13+
WHEN MATCHED THEN
14+
UPDATE SET ( column = expression [, ...] )
15+
WHEN NOT MATCHED THEN
16+
INSERT [ column_list ]
17+
VALUES (expression, ...)
18+
19+
Description
20+
-----------
21+
22+
The ``MERGE`` statement inserts or updates rows in a ``target_table`` based on the contents of the ``source_table``.
23+
The ``search_condition`` defines a relation between the source and target tables.
24+
When the condition is met, the target row is updated. When the condition is not met, a new row is inserted into the target table.
25+
In the ``MATCHED`` case, the ``UPDATE`` column value expressions can depend on any field of the target or the source.
26+
In the ``NOT MATCHED`` case, the ``INSERT`` expressions can depend on any field of the source.
27+
28+
The ``MERGE`` command requires each target row to match at most one source row. An exception is raised when a single target table row matches more than one source row.
29+
If a source row is not matched by the ``WHEN`` clause and there is no ``WHEN NOT MATCHED`` clause, the source row is ignored.
30+
31+
The ``MERGE`` statement is commonly used to integrate data from two tables with different contents but similar structures.
32+
For example, the source table could be part of a production transactional system, while the target table might be located in a data warehouse for analytics.
33+
Regularly, MERGE operations are performed to update the analytics warehouse with the latest production data.
34+
You can also use MERGE with tables that have different structures, as long as you can define a condition to match the rows between them.
35+
36+
MERGE Command Privileges
37+
------------------------
38+
39+
The ``MERGE`` statement does not have a dedicated privilege. Instead, executing a ``MERGE`` statement requires the privileges associated with the individual actions it performs:
40+
41+
* ``UPDATE`` actions: require the ``UPDATE`` privilege on the target table columns referenced in the ``SET`` clause.
42+
* ``INSERT`` actions: require the ``INSERT`` privilege on the target table.
43+
44+
Each privilege must be granted to the user executing the ``MERGE`` command, based on the specific operations included in the statement.
45+
46+
Example
47+
-------
48+
49+
Update the sales information for existing products and insert the sales information for the new products in the market.
50+
51+
.. code-block:: text
52+
53+
MERGE INTO product_sales AS s
54+
USING monthly_sales AS ms
55+
ON s.product_id = ms.product_id
56+
WHEN MATCHED THEN
57+
UPDATE SET
58+
sales = sales + ms.sales
59+
, last_sale = ms.sale_date
60+
, current_price = ms.price
61+
WHEN NOT MATCHED THEN
62+
INSERT (product_id, sales, last_sale, current_price)
63+
VALUES (ms.product_id, ms.sales, ms.sale_date, ms.price)
64+
65+
Limitations
66+
-----------
67+
68+
Any connector can be used as a source table for a ``MERGE`` statement.
69+
Only connectors which support the ``MERGE`` statement can be the target of a merge operation.
70+
See the :doc:`connector documentation </connector/>` for more information.
71+
The ``MERGE`` statement is currently supported only by the iceberg connector.

0 commit comments

Comments
 (0)