- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 05:28 PM
I'm using merge to upsert data into a table:
Id like to use one of the following generated columns to create surrogate keys on insert:
- surrogate_guid string generated always as (uuid())
- surrogate_id bigint generated always as identity
#1 doesn't work, I get the error "A generated column cannot use a non-deterministic expression".
#2 doesn't work, I get the error "cannot resolve surrogate_id in UPDATE clause"
What's best practice for merging and getting a unique identifier (preferably uuid) assigned?
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 10:36 PM
@Dekova
1) uuid() is non-deterministic meaning that it will give you different result each time you run this function
2) Per the documentation "For Databricks Runtime 9.1 and above, MERGE operations support generated columns when you set spark.databricks.delta.schema.autoMerge.enabled to true."
What i would do in this situtaion is:
- Create a column surrogate_id bigint GENERATED ALWAYS AS identity,
- Create a column surrogate_guid and hash it based on surrogate_id column. Ex. surrogate_guid string GENERATED ALWAYS AS (sha2(cast(surrogate_id AS STRING), 512))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-25-2023 10:36 PM
@Dekova
1) uuid() is non-deterministic meaning that it will give you different result each time you run this function
2) Per the documentation "For Databricks Runtime 9.1 and above, MERGE operations support generated columns when you set spark.databricks.delta.schema.autoMerge.enabled to true."
What i would do in this situtaion is:
- Create a column surrogate_id bigint GENERATED ALWAYS AS identity,
- Create a column surrogate_guid and hash it based on surrogate_id column. Ex. surrogate_guid string GENERATED ALWAYS AS (sha2(cast(surrogate_id AS STRING), 512))

