<?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: SQL merge to update one of the nested column in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/sql-merge-to-update-one-of-the-nested-column/m-p/92730#M38521</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/120818"&gt;@SagarJi&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;According to &lt;A href="https://kb.databricks.com/delta/delta-merge-cannot-resolve-field" target="_self"&gt;the documentation&lt;/A&gt; updates to the nested columns are not supported:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_0-1728023452259.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/11682iD92ABA00C7726E3B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="filipniziol_0-1728023452259.png" alt="filipniziol_0-1728023452259.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What you can do you can construct the whole struct and update the parent:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;MERGE INTO mydataset AS target
USING CURRENT_BATCH AS incoming
ON target.parent1.companyId = incoming.parent1.companyId 
   AND target.parent1.id = incoming.parent1.id 
   AND incoming.is_deleted &amp;lt;&amp;gt; 'N'
WHEN MATCHED AND incoming.parent2.dateTimeUpdated &amp;gt; target.parent2.dateTimeUpdated
THEN UPDATE SET
    target.is_deleted = incoming.is_deleted,
    target.parent2 = struct(
        target.parent2.field1 AS field1, -- Replace 'field1' with actual field names
        target.parent2.field2 AS field2, -- Replace 'field2' with actual field names
        -- Include all other fields from parent2 here, keeping their values unchanged
        incoming.parent2.dateTimeUpdated AS dateTimeUpdated -- Update this field
    )
WHEN NOT MATCHED THEN INSERT *&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 04 Oct 2024 06:32:50 GMT</pubDate>
    <dc:creator>filipniziol</dc:creator>
    <dc:date>2024-10-04T06:32:50Z</dc:date>
    <item>
      <title>SQL merge to update one of the nested column</title>
      <link>https://community.databricks.com/t5/data-engineering/sql-merge-to-update-one-of-the-nested-column/m-p/92704#M38515</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am having existing delta-lake as target, and the small set of records at hand as CURRENT_BATCH,&lt;BR /&gt;I have a requirement to update dateTimeUpdated column inside parent2, using following merge query.&lt;BR /&gt;========&lt;/P&gt;&lt;P&gt;MERGE INTO mydataset AS target&lt;BR /&gt;USING CURRENT_BATCH AS incoming&lt;BR /&gt;ON target.parent1.companyId=incoming.parent1.companyId AND target.parent1.id=incoming.parent1.id AND incoming.is_deleted &amp;lt;&amp;gt; 'N'&lt;BR /&gt;WHEN MATCHED AND incoming.parent2.dateTimeUpdated &amp;gt; target.parent2.dateTimeUpdated&lt;BR /&gt;THEN UPDATE SET&lt;BR /&gt;target.is_deleted = incoming.is_deleted,&lt;BR /&gt;target.parent2.dateTimeUpdated = incoming.parent2.dateTimeUpdated&lt;BR /&gt;WHEN NOT MATCHED THEN INSERT *&lt;BR /&gt;======&lt;/P&gt;&lt;P&gt;However this query fails giving me following error&lt;/P&gt;&lt;P&gt;[DELTA_MERGE_UNEXPECTED_ASSIGNMENT_KEY]&lt;BR /&gt;Unexpected assignment key: class org.apache.spark.sql.catalyst.plans.logical.Assignment -&lt;BR /&gt;assignment(parent2#6623.dateTimeUpdated, parent2#6664.dateTimeUpdated)&lt;/P&gt;&lt;P&gt;It works fine if I update directly parent i.e target.parent2 = incoming.parent2, but I just need to update one of the nested columns, how do I do that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 01:17:38 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sql-merge-to-update-one-of-the-nested-column/m-p/92704#M38515</guid>
      <dc:creator>SagarJi</dc:creator>
      <dc:date>2024-10-04T01:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: SQL merge to update one of the nested column</title>
      <link>https://community.databricks.com/t5/data-engineering/sql-merge-to-update-one-of-the-nested-column/m-p/92707#M38518</link>
      <description>&lt;P&gt;I am using Apache Spark 3.5.1 and deltalake version 3.1.0&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 02:04:01 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sql-merge-to-update-one-of-the-nested-column/m-p/92707#M38518</guid>
      <dc:creator>SagarJi</dc:creator>
      <dc:date>2024-10-04T02:04:01Z</dc:date>
    </item>
    <item>
      <title>Re: SQL merge to update one of the nested column</title>
      <link>https://community.databricks.com/t5/data-engineering/sql-merge-to-update-one-of-the-nested-column/m-p/92730#M38521</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/120818"&gt;@SagarJi&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;According to &lt;A href="https://kb.databricks.com/delta/delta-merge-cannot-resolve-field" target="_self"&gt;the documentation&lt;/A&gt; updates to the nested columns are not supported:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="filipniziol_0-1728023452259.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/11682iD92ABA00C7726E3B/image-size/medium?v=v2&amp;amp;px=400" role="button" title="filipniziol_0-1728023452259.png" alt="filipniziol_0-1728023452259.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;What you can do you can construct the whole struct and update the parent:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;MERGE INTO mydataset AS target
USING CURRENT_BATCH AS incoming
ON target.parent1.companyId = incoming.parent1.companyId 
   AND target.parent1.id = incoming.parent1.id 
   AND incoming.is_deleted &amp;lt;&amp;gt; 'N'
WHEN MATCHED AND incoming.parent2.dateTimeUpdated &amp;gt; target.parent2.dateTimeUpdated
THEN UPDATE SET
    target.is_deleted = incoming.is_deleted,
    target.parent2 = struct(
        target.parent2.field1 AS field1, -- Replace 'field1' with actual field names
        target.parent2.field2 AS field2, -- Replace 'field2' with actual field names
        -- Include all other fields from parent2 here, keeping their values unchanged
        incoming.parent2.dateTimeUpdated AS dateTimeUpdated -- Update this field
    )
WHEN NOT MATCHED THEN INSERT *&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 04 Oct 2024 06:32:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/sql-merge-to-update-one-of-the-nested-column/m-p/92730#M38521</guid>
      <dc:creator>filipniziol</dc:creator>
      <dc:date>2024-10-04T06:32:50Z</dc:date>
    </item>
  </channel>
</rss>

