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:
Xuanwo
2026-11-21 06:41:38 +08:00
committed by GitHub
parent baea02eebd
commit 8dc03bd288
14 changed files with 1892 additions and 3 deletions
+115 -2
View File
@@ -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 {