Skip to content

Commit

Permalink
perf: grab file size in rust
Browse files Browse the repository at this point in the history
  • Loading branch information
ion-elgreco committed Aug 6, 2024
1 parent b11b35a commit 6bd2a91
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
1 change: 1 addition & 0 deletions python/deltalake/_internal.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class RawDeltaTable:
) -> bool: ...
def table_uri(self) -> str: ...
def version(self) -> int: ...
def get_add_file_sizes(self) -> Dict[str, int]: ...
def get_latest_version(self) -> int: ...
def get_num_index_cols(self) -> int: ...
def get_stats_columns(self) -> Optional[List[str]]: ...
Expand Down
6 changes: 1 addition & 5 deletions python/deltalake/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1046,15 +1046,11 @@ def to_pyarrow_dataset(
)

if not filesystem:
file_sizes = self.get_add_actions().to_pydict()
file_sizes = {
x: y for x, y in zip(file_sizes["path"], file_sizes["size_bytes"])
}
filesystem = pa_fs.PyFileSystem(
DeltaStorageHandler.from_table(
self._table,
self._storage_options,
file_sizes,
self._table.get_add_file_sizes(),
)
)
format = ParquetFileFormat(
Expand Down
14 changes: 14 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use deltalake::datafusion::datasource::memory::MemTable;
use deltalake::datafusion::datasource::provider::TableProvider;
use deltalake::datafusion::physical_plan::ExecutionPlan;
use deltalake::datafusion::prelude::SessionContext;
use deltalake::delta_datafusion::cdf::FileAction;
use deltalake::delta_datafusion::DeltaDataChecker;
use deltalake::errors::DeltaTableError;
use deltalake::kernel::{
Expand Down Expand Up @@ -1236,6 +1237,19 @@ impl RawDeltaTable {
))
}

pub fn get_add_file_sizes(&self) -> PyResult<HashMap<String, i64>> {
let actions = self
._table
.snapshot()
.map_err(PythonError::from)?
.file_actions()
.map_err(PythonError::from)?;

Ok(actions
.iter()
.map(|action| (action.path(), action.size() as i64))
.collect::<HashMap<String, i64>>())
}
/// Run the delete command on the delta table: delete records following a predicate and return the delete metrics.
#[pyo3(signature = (predicate = None, writer_properties=None, custom_metadata=None, post_commithook_properties=None))]
pub fn delete(
Expand Down
13 changes: 6 additions & 7 deletions python/tests/test_table_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path
from random import random
from threading import Barrier, Thread
from types import SimpleNamespace
from typing import Any, List, Tuple
from unittest.mock import Mock

Expand Down Expand Up @@ -169,18 +168,18 @@ def test_read_simple_table_update_incremental():
assert dt.to_pyarrow_dataset().to_table().to_pydict() == {"id": [5, 7, 9]}


def test_read_simple_table_file_sizes_failure():
def test_read_simple_table_file_sizes_failure(mocker):
table_path = "../crates/test/tests/data/simple_table"
dt = DeltaTable(table_path)
add_actions = dt.get_add_actions().to_pydict()

# set all sizes to -1, the idea is to break the reading, to check
# that input file sizes are actually used
add_actions_modified = {
x: [-1 for item in x] if x == "size_bytes" else y
for x, y in add_actions.items()
}
dt.get_add_actions = lambda: SimpleNamespace(to_pydict=lambda: add_actions_modified) # type:ignore
add_actions_modified = {x: -1 for x in add_actions["path"]}
mocker.patch(
"deltalake._internal.RawDeltaTable.get_add_file_sizes",
return_value=add_actions_modified,
)

with pytest.raises(OSError, match="Cannot seek past end of file."):
dt.to_pyarrow_dataset().to_table().to_pydict()
Expand Down

0 comments on commit 6bd2a91

Please sign in to comment.