Snowpark Migration Accelerator: Zusammenführung¶
Beschreibung¶
The MERGE statement combines data from one or more source tables with a target table, allowing you to perform updates and inserts in a single operation. Based on conditions you define, it determines whether to update existing rows or insert new ones in the target table. This makes it more efficient than using separate INSERT, UPDATE, and DELETE statements. The MERGE statement always produces consistent results when run multiple times with the same data.
In Spark, you can find the MERGE syntax in the Spark documentation.
In Snowflake, the MERGE statement follows this syntax (For additional details, refer to the Snowflake documentation):
The key distinction is that Snowflake lacks a direct equivalent to the WHEN NOT MATCHED BY SOURCE clause. A workaround solution is required to achieve similar functionality in Snowflake.
Beispielhafte Quellcode-Muster¶
Beispielhafte Hilfsdaten¶
Bemerkung
Die folgenden Codebeispiele wurden ausgeführt, damit Sie besser verstehen, wie sie funktionieren:
MERGE-Anweisung - Fall einfügen und aktualisieren¶
Spark¶
Snowflake¶
The INSERT and UPDATE operations work the same way in Snowflake. In both SQL dialects, you can use DEFAULT as an expression to set a column to its default value.
Spark erlaubt Einfüge- und Aktualisierungsoperationen, ohne die Spalten explizit aufzulisten. Wenn keine Spalten angegeben sind, betrifft die Operation alle Spalten der Tabelle. Damit dies korrekt funktioniert, müssen die Quell- und die Zieltabelle identische Spaltenstrukturen haben. Wenn die Spaltenstrukturen nicht übereinstimmen, erhalten Sie einen Parsing-Fehler.
Snowflake¶
The DELETE action in Snowflake works the same way as in other databases. You can also add additional conditions to the MATCHED and NOT MATCHED clauses.
WHEN NOT MATCHED BY TARGET and WHEN NOT MATCHED are equivalent clauses that can be used interchangeably in SQL merge statements.
MERGE-Anweisung - WHENNOTMATCHEDBYSOURCE¶
WHEN NOT MATCHED BY SOURCE clauses are triggered when a row in the target table has no matching rows in the source table. This occurs when both the merge_condition and the optional not_match_by_source_condition evaluate to true. For more details, see the Spark documentation.
Snowflake does not support this clause directly. To handle this limitation, you can use the following workaround for both DELETE and UPDATE actions.
Snowflake¶
The DELETE action in Snowflake works the same way as in other databases. You can also add additional conditions to the MATCHED and NOT MATCHED clauses.
Bekannte Probleme¶
1. MERGE ist in beiden Sprachen sehr ähnlich¶
Apache Spark bietet zwar zusätzliche Features, aber Sie können ähnliche Funktionen in Snowflake mit alternativen Ansätzen erreichen, wie in den vorherigen Beispielen gezeigt.