Snowpark Migration Accelerator: マージ¶
説明¶
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.
サンプルソースパターン¶
サンプル補助データ¶
注釈
以下のコード例は、どのように動作するかをより理解しやすくするために実行されています。
MERGE ステートメント - 挿入および更新の場合¶
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では、列を明示的にリストすることなく、挿入や更新の操作を行うことができます。列が指定されていない場合、操作はテーブル内のすべての列に影響します。これが正しく機能するためには、ソーステーブルと宛先テーブルが同一の列構造を持っている必要があります。列構造が一致しない場合、解析エラーが発生します。
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 ステートメント - WHEN NOT MATCHED BY SOURCE¶
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.
既知の問題¶
1.MERGE はどちらの言語でもよく似ています。¶
Apache Sparkは追加機能を提供しますが、前の例で示したように、別のアプローチを使用してSnowflakeで同様の機能を実現できます。