You are viewing documentation about an older version (1.13.0). View latest version

snowflake.snowpark.functions.when_matched¶

snowflake.snowpark.functions.when_matched(condition: Optional[Column] = None) → WhenMatchedClause[source]¶

Specifies a matched clause for the Table.merge action. See WhenMatchedClause for details.

Convenience function to create a new WhenMatchedClause instance which is required together with an action when merging a Snowpark table with a Snowpark DataFrame (see snowflake.snowpark.Table.merge for more details).

Example:

>>> target_df = session.create_dataframe([(10, "old"), (10, "too_old"), (11, "old")], schema=["key", "value"])
>>> target_df.write.save_as_table("my_table", mode="overwrite", table_type="temporary")
>>> target = session.table("my_table")
>>> source = session.create_dataframe([(10, "new"), (12, "new"), (13, "old")], schema=["key", "value"])
>>> target.merge(source, (target["key"] == source["key"]) & (target["value"] == "too_old"),
...              [when_matched().update({"value": source["value"]})])
MergeResult(rows_inserted=0, rows_updated=1, rows_deleted=0)
>>> target.collect()
[Row(KEY=10, VALUE='old'), Row(KEY=10, VALUE='new'), Row(KEY=11, VALUE='old')]
Copy