snowflake.snowpark.functions.map_insert

snowflake.snowpark.functions.map_insert(map_col: Union[snowflake.snowpark.column.Column, str], key: Union[snowflake.snowpark.column.Column, str], value: Union[snowflake.snowpark.column.Column, str], update_flag: Optional[Union[snowflake.snowpark.column.Column, str]] = None) Column[source]

Returns a map containing all key-value pairs from the source map as well as the new key-value pair. If the key already exists in the map, the value is updated with the new value unless update_flag is False.

Parameters:
  • map_col (ColumnOrName) – The source map

  • key (ColumnOrName) – The key to insert or update

  • value (ColumnOrName) – The value to associate with the key

  • update_flag (Optional[ColumnOrName]) – A boolean flag indicating whether to update existing keys. If None or True, existing keys are updated. If False, existing keys are not updated.

Returns:

A new map with the key-value pair inserted or updated

Return type:

Column

Examples::
>>> from snowflake.snowpark.functions import lit, to_variant, col
>>> df = session.sql("SELECT {'a': 1, 'b': 2}::MAP(VARCHAR, NUMBER) as MAP_COL")
>>> df.select(to_variant(map_insert(col("MAP_COL"), lit("c"), lit(3))).alias("RESULT")).collect()
[Row(RESULT='{\n  "a": 1,\n  "b": 2,\n  "c": 3\n}')]
Copy

# Example using update flag >>> from snowflake.snowpark.functions import lit, to_variant, col >>> df = session.sql(“SELECT {‘a’: 1, ‘b’: 2}::MAP(VARCHAR, NUMBER) as MAP_COL”) >>> df.select(to_variant(map_insert(col(“MAP_COL”), lit(“a”), lit(20), lit(True))).alias(“RESULT”)).collect() [Row(RESULT=’{n “a”: 20,n “b”: 2n}’)]