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에서 유사한 기능을 구현할 수 있습니다.