Skip to content
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
22 changes: 13 additions & 9 deletions python/python/lance/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -2021,7 +2021,7 @@ def delete(
*,
conflict_retries: int = 10,
retry_timeout: timedelta = timedelta(seconds=30),
):
) -> DeleteResult:
"""
Delete rows from the dataset.

Expand All @@ -2042,24 +2042,24 @@ def delete(
regardless of how long it takes to complete. Subsequent attempts will be
cancelled once this timeout is reached. Default is 30 seconds.

Returns
-------
dict
A dictionary containing the number of rows deleted, with the key
``num_deleted_rows``.

Examples
--------
>>> import lance
>>> import pyarrow as pa
>>> table = pa.table({"a": [1, 2, 3], "b": ["a", "b", "c"]})
>>> dataset = lance.write_dataset(table, "example")
>>> dataset.delete("a = 1 or b in ('a', 'b')")
>>> dataset.to_table()
pyarrow.Table
a: int64
b: string
----
a: [[3]]
b: [["c"]]
{'num_deleted_rows': 2}
"""
if isinstance(predicate, pa.compute.Expression):
predicate = str(predicate)
self._ds.delete(predicate, conflict_retries, retry_timeout)
return self._ds.delete(predicate, conflict_retries, retry_timeout)

def truncate_table(self) -> None:
"""
Expand Down Expand Up @@ -4172,6 +4172,10 @@ class UpdateResult(TypedDict):
num_rows_updated: int


class DeleteResult(TypedDict):
num_deleted_rows: int


class AlterColumn(TypedDict):
path: str
name: Optional[str]
Expand Down
11 changes: 7 additions & 4 deletions python/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1392,10 +1392,11 @@ impl Dataset {
#[pyo3(signature=(predicate, conflict_retries=None, retry_timeout=None))]
fn delete(
&mut self,
py: Python<'_>,
predicate: String,
conflict_retries: Option<u32>,
retry_timeout: Option<std::time::Duration>,
) -> PyResult<()> {
) -> PyResult<Py<PyAny>> {
let mut builder = DeleteBuilder::new(self.ds.clone(), predicate);

if let Some(retries) = conflict_retries {
Expand All @@ -1406,11 +1407,13 @@ impl Dataset {
builder = builder.retry_timeout(timeout);
}

let new_dataset = rt()
let result = rt()
.block_on(None, builder.execute())?
.map_err(|err| PyIOError::new_err(err.to_string()))?;
self.ds = new_dataset;
Ok(())
self.ds = result.new_dataset;
let dict = PyDict::new(py);
dict.set_item("num_deleted_rows", result.num_deleted_rows)?;
Ok(dict.into())
}

#[pyo3(signature=(updates, predicate=None, conflict_retries=None, retry_timeout=None))]
Expand Down
6 changes: 3 additions & 3 deletions rust/lance/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ use crate::dataset::index::LanceIndexStoreExt;
pub use write::update::{UpdateBuilder, UpdateJob};
#[allow(deprecated)]
pub use write::{
write_fragments, AutoCleanupParams, CommitBuilder, DeleteBuilder, InsertBuilder,
write_fragments, AutoCleanupParams, CommitBuilder, DeleteBuilder, DeleteResult, InsertBuilder,
WriteDestination, WriteMode, WriteParams,
};

Expand Down Expand Up @@ -1550,14 +1550,14 @@ impl Dataset {
}

/// Delete rows based on a predicate.
pub async fn delete(&mut self, predicate: &str) -> Result<()> {
pub async fn delete(&mut self, predicate: &str) -> Result<write::delete::DeleteResult> {
info!(target: TRACE_DATASET_EVENTS, event=DATASET_DELETING_EVENT, uri = &self.uri, predicate=predicate);
write::delete::delete(self, predicate).await
}

/// Truncate the dataset by deleting all rows.
pub async fn truncate_table(&mut self) -> Result<()> {
self.delete("true").await
self.delete("true").await.map(|_| ())
}

/// Add new base paths to the dataset.
Expand Down
2 changes: 1 addition & 1 deletion rust/lance/src/dataset/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod retry;
pub mod update;

pub use commit::CommitBuilder;
pub use delete::DeleteBuilder;
pub use delete::{DeleteBuilder, DeleteResult};
pub use insert::InsertBuilder;

/// The destination to write data to.
Expand Down
Loading