<?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 Using DeltaTable.merge() and generating surrogate keys on insert? in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/using-deltatable-merge-and-generating-surrogate-keys-on-insert/m-p/38432#M5507</link>
    <description>&lt;P&gt;I'm using merge to upsert data into a table:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;DeltaTable&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.forName(&lt;/SPAN&gt;&lt;SPAN&gt;DESTINATION_TABLE&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;as&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"target"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.merge(merge_df.&lt;/SPAN&gt;&lt;SPAN&gt;as&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"source"&lt;/SPAN&gt;&lt;SPAN&gt;) ,&lt;/SPAN&gt;&lt;SPAN&gt;"source.topic = target.topic and source.key = target.key"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.whenMatched()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.updateAll()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.whenNotMatched()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.insertAll()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.execute()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN&gt;Id like to use one of the following generated columns to create surrogate keys on insert:&lt;/SPAN&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;SPAN&gt;surrogate_guid string generated always as (uuid())&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;surrogate_id bigint generated always as identity&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;SPAN&gt;#1 doesn't work, I get the error "A generated column cannot use a non-deterministic expression".&lt;BR /&gt;#2 doesn't work, I get the error "cannot resolve surrogate_id in UPDATE clause"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;What's best practice for merging and getting a unique identifier (preferably uuid) assigned?&lt;/P&gt;&lt;/DIV&gt;</description>
    <pubDate>Wed, 26 Jul 2023 00:28:31 GMT</pubDate>
    <dc:creator>Dekova</dc:creator>
    <dc:date>2023-07-26T00:28:31Z</dc:date>
    <item>
      <title>Using DeltaTable.merge() and generating surrogate keys on insert?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/using-deltatable-merge-and-generating-surrogate-keys-on-insert/m-p/38432#M5507</link>
      <description>&lt;P&gt;I'm using merge to upsert data into a table:&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;DeltaTable&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.forName(&lt;/SPAN&gt;&lt;SPAN&gt;DESTINATION_TABLE&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;as&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"target"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.merge(merge_df.&lt;/SPAN&gt;&lt;SPAN&gt;as&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;"source"&lt;/SPAN&gt;&lt;SPAN&gt;) ,&lt;/SPAN&gt;&lt;SPAN&gt;"source.topic = target.topic and source.key = target.key"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.whenMatched()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.updateAll()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.whenNotMatched()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.insertAll()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;.execute()&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&lt;SPAN&gt;Id like to use one of the following generated columns to create surrogate keys on insert:&lt;/SPAN&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;&lt;SPAN&gt;surrogate_guid string generated always as (uuid())&lt;/SPAN&gt;&lt;/LI&gt;&lt;LI&gt;&lt;SPAN&gt;surrogate_id bigint generated always as identity&lt;/SPAN&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;SPAN&gt;#1 doesn't work, I get the error "A generated column cannot use a non-deterministic expression".&lt;BR /&gt;#2 doesn't work, I get the error "cannot resolve surrogate_id in UPDATE clause"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;What's best practice for merging and getting a unique identifier (preferably uuid) assigned?&lt;/P&gt;&lt;/DIV&gt;</description>
      <pubDate>Wed, 26 Jul 2023 00:28:31 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/using-deltatable-merge-and-generating-surrogate-keys-on-insert/m-p/38432#M5507</guid>
      <dc:creator>Dekova</dc:creator>
      <dc:date>2023-07-26T00:28:31Z</dc:date>
    </item>
    <item>
      <title>Re: Using DeltaTable.merge() and generating surrogate keys on insert?</title>
      <link>https://community.databricks.com/t5/get-started-discussions/using-deltatable-merge-and-generating-surrogate-keys-on-insert/m-p/38442#M5508</link>
      <description>&lt;P data-unlink="true"&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/84813"&gt;@Dekova&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;1) uuid() is non-deterministic meaning that it will give you different result each time you run this function&lt;BR /&gt;2) Per the &lt;A href="https://docs.databricks.com/delta/generated-columns.html#learn-how-to-use-generated-columns" target="_self"&gt;documentation&lt;/A&gt;&amp;nbsp;"For Databricks Runtime 9.1 and above, MERGE operations support generated columns when you set spark.databricks.delta.schema.autoMerge.enabled to true."&lt;BR /&gt;&lt;BR /&gt;What i would do in this situtaion is:&lt;BR /&gt;- Create a column&amp;nbsp;surrogate_id bigint GENERATED ALWAYS AS identity,&lt;BR /&gt;- Create a column surrogate_guid and hash it based on surrogate_id column. Ex.&amp;nbsp;surrogate_guid string GENERATED ALWAYS AS (sha2(cast(surrogate_id AS STRING), 512))&lt;/P&gt;</description>
      <pubDate>Wed, 26 Jul 2023 05:36:32 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/using-deltatable-merge-and-generating-surrogate-keys-on-insert/m-p/38442#M5508</guid>
      <dc:creator>daniel_sahal</dc:creator>
      <dc:date>2023-07-26T05:36:32Z</dc:date>
    </item>
  </channel>
</rss>

