The MERGE statement is used to select rows from one or more sources to update or insert into a target table. You can specify conditions to determine whether to update or insert into the target table. This statement is a convenient way to combine multiple operations. Allows you to avoid multiple INSERT, UPDATE, and DELETE DML statements. MERGE is a deterministic statement.
In Spark, the MERGE syntax is (For more information check Spark documentation):
The main difference is that Snowflake does not have an equivalence for the WHEN NOT MATCHED BY SOURCE clause, one workaround is needed for its equivalence.
Sample Source Patterns
Sample auxiliary data
This code was executed for a better understanding of the examples:
CREATEORREPLACE people_source ( person_id INTEGERNOT NULLPRIMARY KEY, first_name STRING NOT NULL, last_name STRING NOT NULL, title STRING NOT NULL,);CREATEORREPLACE people_target ( person_id INTEGERNOT NULLPRIMARY KEY, first_name STRING NOT NULL, last_name STRING NOT NULL, title STRING NOT NULLDEFAULT'NONE');INSERT INTO people_target VALUES (1, 'John', 'Smith', 'Mr');INSERT INTO people_target VALUES (2, 'alice', 'jones', 'Mrs');INSERT INTO people_source VALUES (2, 'Alice', 'Jones', 'Mrs.');INSERT INTO people_source VALUES (3, 'Jane', 'Doe', 'Miss');INSERT INTO people_source VALUES (4, 'Dave', 'Brown', 'Mr');
CREATE OR REPLACETABLEpeople_source ( person_id INTEGERNOT NULLPRIMARY KEY, first_name VARCHAR(20) NOT NULL, last_name VARCHAR(20) NOT NULL, title VARCHAR(10) NOT NULL);CREATE OR REPLACETABLEpeople_target ( person_id INTEGERNOT NULLPRIMARY KEY, first_name VARCHAR(20) NOT NULL, last_name VARCHAR(20) NOT NULL, title VARCHAR(10) NOT NULLDEFAULT'NONE');INSERT INTO people_target VALUES (1, 'John', 'Smith', 'Mr');INSERT INTO people_target VALUES (2, 'alice', 'jones', 'Mrs');INSERT INTO people_source VALUES (2, 'Alice', 'Jones', 'Mrs.');INSERT INTO people_source VALUES (3, 'Jane', 'Doe', 'Miss');INSERT INTO people_source VALUES (4, 'Dave', 'Brown', 'Mr');
The INSERT and UPDATE actions are equivalently functional in Snowflake. Likewise in both languages, you can specify DEFAULT as expr to explicitly update the column to its default value.
Spark also has the option to insert and update without specifying the affected columns. In this case, the action is applied to all columns. This action assumes that the source table has the same columns as the destination table; otherwise, the query will return a parsing error.
UPDATESET*-- This is equivalent to UPDATE SET col1 = source.col1 [, col2 = source.col2 ...]INSERT*-- This is equivalent to INSERT (col1 [, col2 ...]) VALUES (source.col1 [, source.col2 ...]
Snowflake does not have these options, so for these scenarios, the equivalent of listing all the columns of the target table is applied.
MERGE Statement - Delete Case
MERGEINTO people_target pt USING people_source ps ON (pt.person_id = ps.person_id)WHENMATCHEDAND pt.person_id <3THENDELETEWHENNOTMATCHEDBYTARGETTHENINSERT*;SELECT*FROM people_target;
The DELETE action is equivalently functional in Snowflake, as is the option to include an extra condition for the MATCHED/NOT MATCHED.
WHEN NOT MATCHED BY TARGET can be used as an alias for WHEN NOT MATCHED.
MERGE Statement - WHEN NOT MATCHED BY SOURCE
WHEN NOT MATCHED BY SOURCE clauses are executed when a target row does not match any rows in the source table based on the merge_condition and not_match_by_source_condition (if applied) evaluates to true (Spark documentation).
Snowflake does not support this clause, so a workaround is necessary to support it. Below is the workaround recommended for this case with the DELETE action, the same can be used for the UPDATE action
MERGEINTO people_target pt USING people_source ps ON pt.person_id = ps.person_idWHENNOTMATCHEDBY SOURCE THENDELETE;SELECT*FROM people_target;