mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 20:44:00 +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:
@@ -428,6 +428,8 @@ code that used the former LanceDB Enterprise-only argument order.
|
||||
|
||||
::: lancedb.table.Branches
|
||||
|
||||
::: lancedb.table.TableDelta
|
||||
|
||||
::: lancedb.RTEWriteSpec
|
||||
|
||||
## Functions and Jobs
|
||||
@@ -1317,6 +1319,8 @@ Table hold your actual data as a collection of records / rows.
|
||||
|
||||
::: lancedb.table.AsyncBranches
|
||||
|
||||
::: lancedb.table.AsyncTableDelta
|
||||
|
||||
## Materialized Views (Asynchronous)
|
||||
|
||||
::: lancedb.materialized_view.AsyncMaterializedView
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
# 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",
|
||||
]
|
||||
@@ -87,6 +87,10 @@ import {
|
||||
RsVersioningTags as RsVersioningTags,
|
||||
RsVersioningMakeQuotesReader as RsVersioningMakeQuotesReader,
|
||||
} from '/snippets/tables.mdx';
|
||||
import {
|
||||
PyVersioningCheckpointDelta as VersioningCheckpointDelta,
|
||||
RsVersioningCheckpointDelta as RsVersioningCheckpointDelta,
|
||||
} from '/snippets/table_delta_docs.mdx';
|
||||
import {
|
||||
PyMultimodalImports as MultimodalImports,
|
||||
TsMultimodalImports as TsMultimodalImports,
|
||||
@@ -1766,6 +1770,46 @@ Deleting a tag only removes the label, not the version it points to. After
|
||||
deletion, the underlying table version becomes eligible for cleanup again.
|
||||
</Note>
|
||||
|
||||
{/*
|
||||
Draft until the checkpoint-delta API is released.
|
||||
|
||||
### Compute changes since a checkpoint {#versioning-checkpoint-delta}
|
||||
|
||||
Use `table.delta(from_version, to_version=None)` to stream the rows inserted,
|
||||
updated, or deleted since a checkpoint. Tag the checkpoint version, then create
|
||||
a delta from that tag. Omitting `to_version` uses the table's current visible
|
||||
snapshot. The endpoints are fixed when the delta is created, so later writes or
|
||||
tag moves do not change the comparison.
|
||||
|
||||
The row-level readers require stable row IDs at both endpoints. Create the table
|
||||
with stable row IDs enabled, as shown below. If a table does not have them, the
|
||||
row-level readers explain how to recreate it with the option. `list_transactions()`
|
||||
reports the transactions in the range and works without stable row IDs.
|
||||
|
||||
This preview supports Python and Rust for LanceDB Embedded. TypeScript support
|
||||
is a follow-up. The API accepts versions and tags on the current branch only,
|
||||
and rejects tags that belong to another branch. LanceDB Enterprise tables do
|
||||
not support row-level deltas.
|
||||
|
||||
A `Restore` can reintroduce rows with older row-version metadata that the delta
|
||||
readers do not report as changed. For that reason, the row-level readers reject
|
||||
a nonempty range that contains a `Restore`. They also reject a range whose
|
||||
transaction history is incomplete, because they cannot rule out a hidden
|
||||
`Restore`. Recompute the checkpoint with a full rescan and reinitialize it at
|
||||
the range end version. `list_transactions()` remains available for the same
|
||||
range. Branch-vs-main summaries remain a separate `branches.diff()` API.
|
||||
|
||||
<CodeGroup>
|
||||
<CodeBlock filename="Python" language="Python" icon="python">
|
||||
{VersioningCheckpointDelta}
|
||||
</CodeBlock>
|
||||
|
||||
<CodeBlock filename="Rust" language="Rust" icon="rust">
|
||||
{RsVersioningCheckpointDelta}
|
||||
</CodeBlock>
|
||||
</CodeGroup>
|
||||
*/}
|
||||
|
||||
{/*
|
||||
Draft until branching reaches early access.
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
{/* Auto-generated by scripts/mdx_snippets_gen.py. Do not edit manually. */}
|
||||
|
||||
export const PyVersioningCheckpointDelta = "table = db.create_table(\n \"checkpoint_delta_example\",\n [\n {\"id\": 1, \"author\": \"Richard\", \"quote\": \"Wubba Lubba Dub Dub!\"},\n {\"id\": 2, \"author\": \"Morty\", \"quote\": \"Rick, what's going on?\"},\n ],\n storage_options={\"new_table_enable_stable_row_ids\": \"true\"},\n mode=\"overwrite\",\n)\ntable.tags.create(\"baseline\", table.version)\ntable.add([{\"id\": 3, \"author\": \"Summer\", \"quote\": \"Whatever, Grandpa\"}])\ntable.update(where=\"id = 2\", values={\"quote\": \"Aww geez, Rick!\"})\ntable.delete(\"id = 1\")\n\ndelta = table.delta(\"baseline\")\nprint(\"inserted:\", delta.inserted_rows().read_all()[\"id\"].to_pylist())\nprint(\"updated:\", delta.updated_rows().read_all()[\"id\"].to_pylist())\nprint(\"deleted:\", delta.deleted_row_ids().read_all()[\"_rowid\"].to_pylist())\nprint(\n \"transactions:\",\n [transaction[\"operation\"] for transaction in delta.list_transactions()],\n)\n";
|
||||
|
||||
export const RsVersioningCheckpointDelta = "use futures::TryStreamExt;\n\nlet table = db\n .create_table(\n \"checkpoint_delta_example\",\n make_quotes_reader(vec![\n (1, \"Richard\", \"Wubba Lubba Dub Dub!\"),\n (2, \"Morty\", \"Rick, what's going on?\"),\n ]),\n )\n .storage_option(\"new_table_enable_stable_row_ids\", \"true\")\n .execute()\n .await?;\n\nlet mut tags = table.tags().await?;\ntags.create(\"baseline\", table.version().await?).await?;\ntable\n .add(make_quotes_reader(vec![(3, \"Summer\", \"Whatever, Grandpa\")]))\n .execute()\n .await?;\ntable\n .update()\n .only_if(\"id = 2\")\n .column(\"quote\", \"'Aww geez, Rick!'\")\n .execute()\n .await?;\ntable.delete(\"id = 1\").await?;\n\nlet delta = table.delta(\"baseline\", None).await?;\nlet mut inserted = delta.inserted_rows().await?;\nwhile let Some(batch) = inserted.try_next().await? {\n println!(\"inserted: {:?}\", batch.column_by_name(\"id\"));\n}\nlet mut deleted = delta.deleted_row_ids().await?;\nwhile let Some(batch) = deleted.try_next().await? {\n println!(\"deleted row ids: {batch:?}\");\n}\nfor transaction in delta.list_transactions().await? {\n println!(\"{}\", transaction.operation.name());\n}\n";
|
||||
Reference in New Issue
Block a user