Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose cleanup_metadata in Python #1826

Merged
merged 2 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions python/deltalake/_internal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class RawDeltaTable:
schema: pyarrow.Schema,
partitions_filters: Optional[FilterType],
) -> None: ...
def cleanup_metadata(self) -> None: ...

def rust_core_version() -> str: ...
def write_new_deltalake(
Expand Down
7 changes: 7 additions & 0 deletions python/deltalake/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,13 @@ def update_incremental(self) -> None:
def create_checkpoint(self) -> None:
self._table.create_checkpoint()

def cleanup_metadata(self) -> None:
"""
Delete expired log files before current version from table. The table log retention is based on
the `configuration.logRetentionDuration` value, 30 days by default.
"""
self._table.cleanup_metadata()

def __stringify_partition_values(
self, partition_filters: Optional[List[Tuple[str, str, Any]]]
) -> Optional[List[Tuple[str, str, Union[str, List[str]]]]]:
Expand Down
10 changes: 9 additions & 1 deletion python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use deltalake::arrow::ffi_stream::ArrowArrayStreamReader;
use deltalake::arrow::record_batch::RecordBatch;
use deltalake::arrow::record_batch::RecordBatchReader;
use deltalake::arrow::{self, datatypes::Schema as ArrowSchema};
use deltalake::checkpoints::create_checkpoint;
use deltalake::checkpoints::{cleanup_metadata, create_checkpoint};
use deltalake::datafusion::datasource::memory::MemTable;
use deltalake::datafusion::datasource::provider::TableProvider;
use deltalake::datafusion::prelude::SessionContext;
Expand Down Expand Up @@ -854,6 +854,14 @@ impl RawDeltaTable {
Ok(())
}

pub fn cleanup_metadata(&self) -> PyResult<()> {
rt()?
.block_on(cleanup_metadata(&self._table))
.map_err(PythonError::from)?;

Ok(())
}

pub fn get_add_actions(&self, flatten: bool) -> PyResult<PyArrowType<RecordBatch>> {
Ok(PyArrowType(
self._table
Expand Down
36 changes: 36 additions & 0 deletions python/tests/test_checkpoint.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime as dt
import os
import pathlib

import pyarrow as pa
Expand All @@ -23,3 +25,37 @@ def test_checkpoint(tmp_path: pathlib.Path, sample_data: pa.Table):

assert last_checkpoint_path.exists()
assert checkpoint_path.exists()


def test_cleanup_metadata(tmp_path: pathlib.Path, sample_data: pa.Table):
tmp_table_path = tmp_path / "path" / "to" / "table"
first_log_path = tmp_table_path / "_delta_log" / "00000000000000000000.json"
second_log_path = tmp_table_path / "_delta_log" / "00000000000000000001.json"
third_log_path = tmp_table_path / "_delta_log" / "00000000000000000002.json"

# TODO: Include binary after fixing issue "Json error: binary type is not supported"
sample_data = sample_data.drop(["binary"])

# Create few log files
write_deltalake(str(tmp_table_path), sample_data)
write_deltalake(str(tmp_table_path), sample_data, mode="overwrite")
delta_table = DeltaTable(str(tmp_table_path))
delta_table.delete()

# Move first log entry timestamp back in time for more than 30 days
old_ts = (dt.datetime.now() - dt.timedelta(days=31)).timestamp()
os.utime(first_log_path, (old_ts, old_ts))

# Move second log entry timestamp back in time for a minute
near_ts = (dt.datetime.now() - dt.timedelta(minutes=1)).timestamp()
os.utime(second_log_path, (near_ts, near_ts))

assert first_log_path.exists()
assert second_log_path.exists()
assert third_log_path.exists()

delta_table.cleanup_metadata()

assert not first_log_path.exists()
assert second_log_path.exists()
assert third_log_path.exists()
Loading