diff --git a/python/deltalake/_internal.pyi b/python/deltalake/_internal.pyi index 945bbc8d60..7a5d84a0cb 100644 --- a/python/deltalake/_internal.pyi +++ b/python/deltalake/_internal.pyi @@ -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: ... diff --git a/python/deltalake/table.py b/python/deltalake/table.py index ebebc685f3..aa91f33b15 100644 --- a/python/deltalake/table.py +++ b/python/deltalake/table.py @@ -9,6 +9,7 @@ TYPE_CHECKING, Any, Dict, + Generator, Iterable, List, NamedTuple, @@ -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, diff --git a/python/src/lib.rs b/python/src/lib.rs index 3fb2c3de24..6118b4d81d 100644 --- a/python/src/lib.rs +++ b/python/src/lib.rs @@ -171,6 +171,12 @@ impl RawDeltaTable { .map_err(PythonError::from)?) } + pub fn get_latest_version(&mut self) -> PyResult { + 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::::from(DateTime::::parse_from_rfc3339(ds).map_err( diff --git a/python/tests/test_table_read.py b/python/tests/test_table_read.py index f56c5876cd..b7de2ba1ae 100644 --- a/python/tests/test_table_read.py +++ b/python/tests/test_table_read.py @@ -299,6 +299,7 @@ def test_history_partitioned_table_metadata(): "numOutputBytes": "2477", "numOutputRows": "7", }, + "version": 0, } diff --git a/rust/src/table/mod.rs b/rust/src/table/mod.rs index 667b2239aa..a9e3899abf 100644 --- a/rust/src/table/mod.rs +++ b/rust/src/table/mod.rs @@ -415,8 +415,8 @@ impl DeltaTable { Ok(()) } - - async fn get_latest_version(&mut self) -> Result { + /// returns the latest available version of the table + pub async fn get_latest_version(&mut self) -> Result { let version_start = match get_last_checkpoint(&self.storage).await { Ok(last_check_point) => last_check_point.version, Err(ProtocolError::CheckpointNotFound) => {