Skip to content

Commit

Permalink
feat: add version number in .history() and display in reversed chro…
Browse files Browse the repository at this point in the history
…nological order (#1710)

# Description
Adds the version number to each commit info.

# Related Issue(s)
<!---
For example:

- closes #106 
--->
- Closes #1561
- Closes #1680

---------

Co-authored-by: R. Tyler Croy <[email protected]>
  • Loading branch information
ion-elgreco and rtyler authored Oct 11, 2023
1 parent dfb04e9 commit f92d9e5
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
1 change: 1 addition & 0 deletions python/deltalake/_internal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RawDeltaTable:
) -> str: ...
def table_uri(self) -> str: ...
def version(self) -> int: ...
def get_latest_version(self) -> int: ...
def metadata(self) -> RawDeltaTableMetaData: ...
def protocol_versions(self) -> List[int]: ...
def load_version(self, version: int) -> None: ...
Expand Down
24 changes: 20 additions & 4 deletions python/deltalake/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
TYPE_CHECKING,
Any,
Dict,
Generator,
Iterable,
List,
NamedTuple,
Expand Down Expand Up @@ -428,10 +429,25 @@ def history(self, limit: Optional[int] = None) -> List[Dict[str, Any]]:
:param limit: the commit info limit to return
:return: list of the commit infos registered in the transaction log
"""
return [
json.loads(commit_info_raw)
for commit_info_raw in self._table.history(limit)
]

def _backwards_enumerate(
iterable: List[str], start_end: int
) -> Generator[Tuple[int, str], None, None]:
n = start_end
for elem in iterable:
yield n, elem
n -= 1

commits = list(reversed(self._table.history(limit)))

history = []
for version, commit_info_raw in _backwards_enumerate(
commits, start_end=self._table.get_latest_version()
):
commit = json.loads(commit_info_raw)
commit["version"] = version
history.append(commit)
return history

def vacuum(
self,
Expand Down
6 changes: 6 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ impl RawDeltaTable {
.map_err(PythonError::from)?)
}

pub fn get_latest_version(&mut self) -> PyResult<i64> {
Ok(rt()?
.block_on(self._table.get_latest_version())
.map_err(PythonError::from)?)
}

pub fn load_with_datetime(&mut self, ds: &str) -> PyResult<()> {
let datetime =
DateTime::<Utc>::from(DateTime::<FixedOffset>::parse_from_rfc3339(ds).map_err(
Expand Down
1 change: 1 addition & 0 deletions python/tests/test_table_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ def test_history_partitioned_table_metadata():
"numOutputBytes": "2477",
"numOutputRows": "7",
},
"version": 0,
}


Expand Down
4 changes: 2 additions & 2 deletions rust/src/table/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,8 @@ impl DeltaTable {

Ok(())
}

async fn get_latest_version(&mut self) -> Result<i64, DeltaTableError> {
/// returns the latest available version of the table
pub async fn get_latest_version(&mut self) -> Result<i64, DeltaTableError> {
let version_start = match get_last_checkpoint(&self.storage).await {
Ok(last_check_point) => last_check_point.version,
Err(ProtocolError::CheckpointNotFound) => {
Expand Down

0 comments on commit f92d9e5

Please sign in to comment.