0
votes

Is there a simple way to choose a previous delta table version as the current/working version?

Instead of creating another version by overwrite/truncate, can we just designate a version as the "current/latest" version?

This operation is more like undo, which completely remove some steps and make the data to a previous stage. Then when doing slecet * from MYTABLE this MYTABLE can point to the restored version.

both SQL or PySpark would be appreicated!

1

1 Answers

3
votes

You can find History of delta table by using

DESCRIBE HISTORY yourTblName

It will give you history of table which includes Version, TimesStamp, UserId/Name ,Operation

enter image description here

To get previous version , you can do few steps, as

SELECT max(version) -1 as perviousVersion  FROM (DESCRIBE HISTORY yourTblName)

It will give you previous version(you can save that in some variable) and then use that in Version As Of

select * from yourTblName Version as of 7

You will have records of previous version.

Restore data table to earlier version:

RESTORE TABLE yourTblName  TO VERSION AS OF 7

Databrciks Documentation : https://docs.databricks.com/delta/delta-utility.html#restore-a-delta-table-to-an-earlier-state