DataBricks:有什么方法可以重置 Generated IDENTITY 列?

DataBricks: Any way to reset the Generated IDENTITY column?

Delta tables 可以生成标识列,如下所示:

CREATE TABLE TestMe (
  pKey bigint GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
  id bigint,
  commentary string
  )
 USING DELTA
 OPTIONS (PATH "/mnt/Delta/Testing/TestMe")

但是,如果不手动编辑更改日志文件,似乎无法重置计数器,这似乎有风险。

如果我想截断 table 并在其中放置一组新数据,我该怎么做而不慢慢积累数亿/数十亿的标识列(每个数据都插入计数器只是上升,永远不会重置)?

请尝试这种方式

CREATE TABLE TestMe (
  pKey bigint GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
  id bigint,
  commentary string
);
  
insert into TestMe (id,commentary) values (1,'a'),(2,'b'),(3,'c');
  
select * from TestMe;
  
truncate table TestMe;

describe history TestMe;
  
Restore Table TestMe to version as of 0;

insert into TestMe (id,commentary) values (1,'a'),(2,'b'),(3,'c');

找到另一种解决方法

CREATE or REPLACE TABLE TestMe (
  pKey bigint GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
  id bigint,
  commentary string
);
  
insert into TestMe (id,commentary) values (1,'a'),(2,'b'),(3,'c');

select * from TestMe;

-- Rerun Create or Replace resets the identity

CREATE or REPLACE TABLE TestMe (
  pKey bigint GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
  id bigint,
  commentary string
);