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

snowflake.snowpark.functions.when_not_matched¶

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

Specifies a not-matched clause for the Table.merge action. See WhenNotMatchedClause for details.

Convenience function to create a new WhenNotMatchedClause 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:

>>> from snowflake.snowpark.types import IntegerType, StringType, StructField, StructType
>>> schema = StructType([StructField("key", IntegerType()), StructField("value", StringType())])
>>> target_df = session.create_dataframe([(10, "old"), (10, "too_old"), (11, "old")], schema=schema)
>>> 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=schema)
>>> target.merge(source, (target["key"] == source["key"]) & (target["value"] == "too_old"),
...              [when_not_matched().insert({"key": source["key"]})])
MergeResult(rows_inserted=2, rows_updated=0, rows_deleted=0)
>>> target.sort(col("key"), col("value")).collect()
[Row(KEY=10, VALUE='old'), Row(KEY=10, VALUE='too_old'), Row(KEY=11, VALUE='old'), Row(KEY=12, VALUE=None), Row(KEY=13, VALUE=None)]
Copy