<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to update value of a column with MAP data-type in a delta table using a python dictionary and SQL UPDATE command? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7091#M3067</link>
    <description>&lt;P&gt;Hi @Mohammad Saber​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for your question! To assist you better, please take a moment to review the answer and let me know if it best fits your needs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please help us select the best solution by clicking on "Select As Best" if it does.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your feedback will help us ensure that we are providing the best possible service to you. Thank you!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 04 Apr 2023 06:36:21 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2023-04-04T06:36:21Z</dc:date>
    <item>
      <title>How to update value of a column with MAP data-type in a delta table using a python dictionary and SQL UPDATE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7089#M3065</link>
      <description>&lt;P&gt;I have a delta table created by:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;%sql
&amp;nbsp;
CREATE TABLE IF NOT EXISTS dev.bronze.test_map (
    id INT,
    table_updates MAP&amp;lt;STRING, TIMESTAMP&amp;gt;,
&amp;nbsp;
  CONSTRAINT test_map_pk PRIMARY KEY(id)
  
  ) USING DELTA
LOCATION "abfss://bronze@Table Path"&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;With initial values:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;INSERT INTO dev.bronze.test_map 
VALUES (1, null), 
       (2, null), 
       (3, null);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Note that there is no value in column "table_updates".&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/484iA4128E08B73CABAA/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;After processing other tables in our platform, I have table updates info as a python dictionary like below:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;table_updates_id1  =  
&amp;nbsp;
{'id1_table_1': datetime.datetime(2023, 3, 26, 4, 33, 22, 323000),
&amp;nbsp;
 'id1_table_2': datetime.datetime(2023, 3, 26, 4, 33, 22, 323000)}&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image.png"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/490i3122F1EF35B3F6ED/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now, I want to update the value of column "table_update&amp;nbsp;" where id=1 using SQL UPDATE command (note that I want to update the table not dataframe). &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried different methods but failed. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;1st trial:&lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;spark.sql(f"""
UPDATE dev.bronze.test_map
SET
    table_updates = map({table_updates_id1})
WHERE
    id = 1
""")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;B&gt;Error:&lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/482i182F5F669C16DC9A/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;B&gt;2nd trial:&lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;spark.sql(f"""
UPDATE dev.bronze.test_map
SET
    table_updates = map('{','.join([f'{k},{v}' for k,v in table_updates_id1.items()])}')
WHERE
    id = 1
""")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;B&gt;Error:&lt;/B&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/494i94E868071BDF5F93/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any idea how to solve this issue? Thanks. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 26 Mar 2023 04:46:46 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7089#M3065</guid>
      <dc:creator>Mado</dc:creator>
      <dc:date>2023-03-26T04:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to update value of a column with MAP data-type in a delta table using a python dictionary and SQL UPDATE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7090#M3066</link>
      <description>&lt;P&gt;@Mohammad Saber​&amp;nbsp;:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The error messages you received indicate that the SQL UPDATE command is expecting a column of type map&amp;lt;string,timestamp&amp;gt;, but is instead receiving a string or a non-matching datatype. To resolve this issue, you can convert the Python dictionary to a valid SQL map format using the map_from_entries function in Spark SQL.&lt;/P&gt;&lt;P&gt;Here's an example of how you can use the map_from_entries function to update the table_updates column in your delta table:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;from pyspark.sql.functions import map_from_entries
&amp;nbsp;
# Convert Python dictionary to list of key-value pairs
table_updates_list = list(table_updates_id1.items())
&amp;nbsp;
# Convert list of key-value pairs to SQL map format
table_updates_map = map_from_entries(table_updates_list)
&amp;nbsp;
# Use SQL UPDATE command to update delta table
spark.sql(f"""
    UPDATE dev.bronze.test_map
    SET
        table_updates = {table_updates_map}
    WHERE
        id = 1
""")&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In this example, the map_from_entries function converts the table_updates_id1 dictionary to a list of key-value pairs, which is then passed to the map_from_entries function to create a SQL map. The resulting SQL map is then used in the SQL UPDATE command to update the table_updates column for rows where id = 1,&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2023 14:18:22 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7090#M3066</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-04-02T14:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to update value of a column with MAP data-type in a delta table using a python dictionary and SQL UPDATE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7091#M3067</link>
      <description>&lt;P&gt;Hi @Mohammad Saber​&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you for your question! To assist you better, please take a moment to review the answer and let me know if it best fits your needs.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Please help us select the best solution by clicking on "Select As Best" if it does.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your feedback will help us ensure that we are providing the best possible service to you. Thank you!&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2023 06:36:21 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7091#M3067</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2023-04-04T06:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to update value of a column with MAP data-type in a delta table using a python dictionary and SQL UPDATE command?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7092#M3068</link>
      <description>&lt;P&gt;Thanks for your help. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I get the following error when running the following code:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;table_updates_map = map_from_entries(table_updates_list)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/480i76A8D3C1BC4BEECF/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Apr 2023 23:32:20 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-update-value-of-a-column-with-map-data-type-in-a-delta/m-p/7092#M3068</guid>
      <dc:creator>Mado</dc:creator>
      <dc:date>2023-04-09T23:32:20Z</dc:date>
    </item>
  </channel>
</rss>

