mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
feat: add table.delta for version-to-version row changes (#5135)
Adds native `Table::delta(from_version, to_version=None)` and Python
sync/async `table.delta(...)` for version-to-version row changes.
`TableDelta` exposes streaming `inserted_rows()`, `updated_rows()`,
`upserted_rows()`, `deleted_row_ids()`, and `list_transactions()`.
Endpoints are resolved once on independent dataset handles.
`to_version=None` uses the table handle's visible snapshot, and the
handle's checkout is not changed. Only versions `>= 1` and tags on the
current branch are accepted; cross-branch tags, reversed ranges, version
0, and missing endpoints are rejected. Equal valid endpoints return
empty readers with the same schemas.
Row-level readers require stable row ids at both endpoints and report
the `storage_options={"new_table_enable_stable_row_ids": "true"}`
creation guidance when they are absent. `list_transactions()` does not
require stable row ids.
Row-level readers also fail closed when the range contains a `Restore`,
or when transaction history cannot prove the range is restore-free,
because restored rows can retain older row-version metadata that the
delta readers omit. The error directs consumers to a full rescan and
reinitializing the checkpoint at the range end version.
`list_transactions()` remains available for readable history. The
Versioning guide includes Python and Rust checkpoint-delta examples with
stable-row-ID setup and this Embedded limitation.
Embedded `branches.diff()` remains a separate API.
Refs OSS-2860
This commit is contained in:
@@ -98,6 +98,7 @@ pub fn _lancedb(py: Python, m: &Bound<'_, PyModule>) -> PyResult<()> {
|
||||
m.add_function(wrap_pyfunction!(catalog::connect_catalog, m)?)?;
|
||||
m.add_class::<Session>()?;
|
||||
m.add_class::<Table>()?;
|
||||
m.add_class::<table::TableDelta>()?;
|
||||
m.add_class::<crate::oauth::PyOAuthSession>()?;
|
||||
m.add_class::<crate::oauth::PySessionStatus>()?;
|
||||
m.add_class::<crate::oauth::PySessionLogout>()?;
|
||||
|
||||
+115
-2
@@ -29,10 +29,10 @@ use lancedb::blob::{BlobDescriptor, BlobFile, BlobRangeRequest};
|
||||
use lancedb::connection::LanceFileVersion;
|
||||
use lancedb::index::scalar::FtsIndexBuilder;
|
||||
use lancedb::table::{
|
||||
AddDataMode, ColumnAlteration, CompactionMode, CompactionOptions, Duration,
|
||||
AddDataMode, ColumnAlteration, CompactionMode, CompactionOptions, DeltaRef, Duration,
|
||||
FieldMetadataUpdate, FtsToken as LanceDbFtsToken, FunctionRefreshPlanningConfig,
|
||||
FunctionRefreshPodResources, FunctionWorkerResourceBudget, IndexRemapMode, NewColumnTransform,
|
||||
Ref, RefreshColumnOptions, Table as LanceDbTable,
|
||||
Ref, RefreshColumnOptions, Table as LanceDbTable, TableDelta as LanceDbTableDelta,
|
||||
};
|
||||
use lancedb::tokenize as lancedb_tokenize;
|
||||
use pyo3::{
|
||||
@@ -2075,6 +2075,22 @@ impl Table {
|
||||
)
|
||||
}
|
||||
|
||||
#[pyo3(signature = (from_version, to_version=None))]
|
||||
pub fn delta(
|
||||
self_: PyRef<'_, Self>,
|
||||
from_version: LanceVersion,
|
||||
to_version: Option<LanceVersion>,
|
||||
) -> PyResult<Bound<'_, PyAny>> {
|
||||
let inner = self_.inner_ref()?.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
let delta = inner
|
||||
.delta(from_version, to_version.map(Into::into))
|
||||
.await
|
||||
.infer_error()?;
|
||||
Ok(TableDelta::new(delta))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn list_versions(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
|
||||
let inner = self_.inner_ref()?.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
@@ -3244,6 +3260,103 @@ pub enum LanceVersion {
|
||||
Tag(String),
|
||||
}
|
||||
|
||||
impl From<LanceVersion> for DeltaRef {
|
||||
fn from(version: LanceVersion) -> Self {
|
||||
match version {
|
||||
LanceVersion::Version(version) => DeltaRef::Version(version),
|
||||
LanceVersion::Tag(tag) => DeltaRef::Tag(tag),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Streaming row changes between two table versions.
|
||||
#[pyclass]
|
||||
pub struct TableDelta {
|
||||
inner: Arc<LanceDbTableDelta>,
|
||||
}
|
||||
|
||||
impl TableDelta {
|
||||
fn new(inner: LanceDbTableDelta) -> Self {
|
||||
Self {
|
||||
inner: Arc::new(inner),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pymethods]
|
||||
impl TableDelta {
|
||||
/// The version this delta starts from (exclusive).
|
||||
pub fn begin_version(&self) -> u64 {
|
||||
self.inner.begin_version()
|
||||
}
|
||||
|
||||
/// The version this delta ends at (inclusive).
|
||||
pub fn end_version(&self) -> u64 {
|
||||
self.inner.end_version()
|
||||
}
|
||||
|
||||
pub fn inserted_rows(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
|
||||
let inner = self_.inner.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
let stream = inner.inserted_rows().await.infer_error()?;
|
||||
Ok(crate::arrow::RecordBatchStream::new(stream.into()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn updated_rows(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
|
||||
let inner = self_.inner.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
let stream = inner.updated_rows().await.infer_error()?;
|
||||
Ok(crate::arrow::RecordBatchStream::new(stream.into()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn upserted_rows(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
|
||||
let inner = self_.inner.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
let stream = inner.upserted_rows().await.infer_error()?;
|
||||
Ok(crate::arrow::RecordBatchStream::new(stream.into()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn deleted_row_ids(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
|
||||
let inner = self_.inner.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
let stream = inner.deleted_row_ids().await.infer_error()?;
|
||||
Ok(crate::arrow::RecordBatchStream::new(stream.into()))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn list_transactions(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
|
||||
let inner = self_.inner.clone();
|
||||
future_into_py(self_.py(), async move {
|
||||
let transactions = inner.list_transactions().await.infer_error()?;
|
||||
Python::attach(|py| {
|
||||
let output = PyList::empty(py);
|
||||
for transaction in transactions {
|
||||
let item = PyDict::new(py);
|
||||
item.set_item("read_version", transaction.read_version)?;
|
||||
item.set_item("uuid", transaction.uuid)?;
|
||||
item.set_item("operation", transaction.operation.name())?;
|
||||
item.set_item("tag", transaction.tag)?;
|
||||
match transaction.transaction_properties {
|
||||
Some(properties) => {
|
||||
let py_properties = PyDict::new(py);
|
||||
for (key, value) in properties.iter() {
|
||||
py_properties.set_item(key, value)?;
|
||||
}
|
||||
item.set_item("transaction_properties", py_properties)?;
|
||||
}
|
||||
None => item.set_item("transaction_properties", py.None())?,
|
||||
}
|
||||
output.append(item)?;
|
||||
}
|
||||
Ok(output.unbind())
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(FromPyObject)]
|
||||
#[pyo3(from_item_all)]
|
||||
pub struct MergeInsertParams {
|
||||
|
||||
Reference in New Issue
Block a user