I have a databricks table with the below DDL:
CREATE TABLE default.Test (
ID BIGINT GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
StopFromDateTime TIMESTAMP,
StopToDateTime TIMESTAMP,
User STRING)
USING delta
TBLPROPERTIES (
'delta.autoOptimize.autoCompact' = 'true',
'delta.autoOptimize.optimizeWrite' = 'true',
'delta.columnMapping.mode' = 'name',
'delta.minReaderVersion' = '2',
'delta.minWriterVersion' = '6')
I have inserted 3 rows of data into the table and the table with the data looks like below:
ID StartDateTime StopDateTime User
1 2020-01-01 2020-01-02 Josh
2 2021-01-01 2021-01-02 Alex
3 2022-01-01 2022-01-02 Ross
When i look at the history of the table it has 4 versions, 1(Create),2,3&4(Insert). I had to delete the records for ID 2&3 so instead of deleting it i did a restore of the table to version 2 which has the first insert so my table now has the data like this
ID StartDateTime StopDateTime User
1 2020-01-01 2020-01-02 Josh
I have inserted another record into the table now for and after inserting the new record my table looks like this:
I was hoping the new ID would be 2 as i restored it to a version before ID 2&3 are inserted however its getting 4.
ID StartDateTime StopDateTime User
1 2020-01-01 2020-01-02 Josh
4 2023-01-01 2023-01-02 Steve
Can someone provide any inputs on how can i reset the identity on this table?
The last used identity value is stored in the "_delta_log" folder in the json files. Restoring the table to a previous version doesn't really update this value.
There is currently no supported API or method to update the state in the *"_delta_*log" folder.
There are ways to reset the identity only if you intend to truncate and reload from scratch like shown here.
Generated identity ensures that every id is unique and increasing, but it does not guarantee that there won't be gaps in between. So if NO gaps are anyways not ensured by design, why is having gaps a problem in your usecase? As per the official documentation it states
Values assigned by identity columns are unique and increment in the direction of the specified step, and in multiples of the specified step size, but are not guaranteed to be contiguous. For example, with a starting value of
0
and a step size of2
, all values are positive even numbers but some even numbers might be skipped.