Files
lancedb/docs/web-tests/py/test_table_delta_docs.py
T
Xuanwo 8dc03bd288 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
2026-11-21 06:41:38 +08:00

42 lines
1.5 KiB
Python

# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The LanceDB Authors
def test_versioning_checkpoint_delta(tmp_db):
db = tmp_db
# --8<-- [start:versioning_checkpoint_delta]
table = db.create_table(
"checkpoint_delta_example",
[
{"id": 1, "author": "Richard", "quote": "Wubba Lubba Dub Dub!"},
{"id": 2, "author": "Morty", "quote": "Rick, what's going on?"},
],
storage_options={"new_table_enable_stable_row_ids": "true"},
mode="overwrite",
)
table.tags.create("baseline", table.version)
table.add([{"id": 3, "author": "Summer", "quote": "Whatever, Grandpa"}])
table.update(where="id = 2", values={"quote": "Aww geez, Rick!"})
table.delete("id = 1")
delta = table.delta("baseline")
print("inserted:", delta.inserted_rows().read_all()["id"].to_pylist())
print("updated:", delta.updated_rows().read_all()["id"].to_pylist())
print("deleted:", delta.deleted_row_ids().read_all()["_rowid"].to_pylist())
print(
"transactions:",
[transaction["operation"] for transaction in delta.list_transactions()],
)
# --8<-- [end:versioning_checkpoint_delta]
assert table.version == 4
assert delta.inserted_rows().read_all()["id"].to_pylist() == [3]
assert delta.updated_rows().read_all()["id"].to_pylist() == [2]
assert delta.deleted_row_ids().read_all().num_rows == 1
assert [transaction["operation"] for transaction in delta.list_transactions()] == [
"Append",
"Update",
"Delete",
]