mirror of
https://github.com/lancedb/lancedb.git
synced 2026-12-03 07:45:17 +00:00
fix(python): reject pylance writes on checked-out versions (#5252)
## What changed
`LanceTable` gets a small `_ensure_mutable()` helper in
`python/python/lancedb/table.py`. It raises the same `ValueError` as the
native `ensure_mutable()` ("Invalid input, table cannot be modified when
a specific version is checked out") when the handle is pinned with
`checkout()`. It is called before the write is handed to pylance in
`compact_files()`, `merge()`, `cleanup_old_versions()` and both
accelerator branches of `create_index()`.
## Why
Fixes #5251
These methods write through `self.to_lance()` and then call
`checkout_latest()`, so they skip the read-only check that `add()`,
`optimize()` and `merge_insert()` (#4851) apply. On a pinned handle,
`compact_files()` committed to the live table and unpinned the handle,
`merge()` failed with a retryable commit conflict, and
`cleanup_old_versions()` pruned versions while `optimize()` refused to.
The pinned state is only known on the Python side (`_checkout_version`),
so the check is done there.
The TypeScript SDK and Rust have no pylance write path; their writes
already go through `ensure_mutable()`. No public API changes, so no docs
update.
## How it was tested
Run in a `python:3.12-slim` container with the `0.42.0b38` nightly
wheel, pylance `15.1.0b2`, and the Python sources from this branch
copied over the wheel (no Rust change, so no `maturin develop`).
- New tests in `test_table.py`:
`test_pylance_writes_rejected_on_checked_out_version` (parametrized over
`compact_files`, `cleanup_old_versions`, `merge`) checks the error, that
the handle stays on its version, and that the latest version is
untouched. `test_accelerated_index_rejected_on_checked_out_version`
covers both accelerator branches with the mocked `to_lance` used by
`test_create_ivfrq_index_accelerator`.
- On `main` the tests fail: `DID NOT RAISE ValueError` for
`compact_files` and the accelerator index, and `OSError: Retryable
commit conflict for version 3` for `merge`.
- With the fix: the 4 new tests pass in 5 consecutive runs, and
`test_table.py` passes (563 passed, 17 skipped).
- With the `_ensure_mutable()` calls removed, exactly the 4 new tests
fail and no other test in `test_table.py` changes.
- `ruff format --check` and `ruff check` on the two changed files:
clean. No Rust or TypeScript files are touched.
Written with help from Claude Code.
---------
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a3ad6bd882
commit
00bf2a0c0b
@@ -5042,6 +5042,15 @@ class LanceTable(Table):
|
||||
self._checkout_version = None
|
||||
self._branch = self.current_branch()
|
||||
|
||||
def _ensure_mutable(self):
|
||||
# Writes routed through pylance bypass the native pinned-version check,
|
||||
# so reject them here with the same error the Rust write paths raise.
|
||||
if self._checkout_version is not None:
|
||||
raise ValueError(
|
||||
"Invalid input, table cannot be modified when a specific version "
|
||||
"is checked out"
|
||||
)
|
||||
|
||||
def restore(self, version: Optional[Union[int, str]] = None):
|
||||
"""Restore a version of the table. This is an in-place operation.
|
||||
|
||||
@@ -5368,6 +5377,7 @@ class LanceTable(Table):
|
||||
|
||||
# Handle accelerator through pylance
|
||||
if accelerator is not None:
|
||||
self._ensure_mutable()
|
||||
dataset = self.to_lance()
|
||||
_create_accelerated_index(
|
||||
dataset,
|
||||
@@ -5397,6 +5407,7 @@ class LanceTable(Table):
|
||||
acc = getattr(config, "accelerator", None)
|
||||
if acc is not None:
|
||||
# Dispatch to pylance for GPU acceleration
|
||||
self._ensure_mutable()
|
||||
dataset = self.to_lance()
|
||||
index_type_map = {
|
||||
"IvfFlat": "IVF_FLAT",
|
||||
@@ -5919,6 +5930,7 @@ class LanceTable(Table):
|
||||
1 2 b e
|
||||
2 3 c f
|
||||
"""
|
||||
self._ensure_mutable()
|
||||
if isinstance(other_table, LanceTable):
|
||||
other_table = other_table.to_lance()
|
||||
else:
|
||||
@@ -6474,6 +6486,7 @@ class LanceTable(Table):
|
||||
The stats of the cleanup operation, including how many bytes were
|
||||
freed.
|
||||
"""
|
||||
self._ensure_mutable()
|
||||
return self.to_lance().cleanup_old_versions(
|
||||
older_than, delete_unverified=delete_unverified
|
||||
)
|
||||
@@ -6494,6 +6507,7 @@ class LanceTable(Table):
|
||||
(see Lance documentation for more details) For most cases, the default
|
||||
should be fine.
|
||||
"""
|
||||
self._ensure_mutable()
|
||||
stats = self.to_lance().optimize.compact_files(*args, **kwargs)
|
||||
self.checkout_latest()
|
||||
return stats
|
||||
|
||||
@@ -2482,6 +2482,33 @@ def test_create_ivfrq_index_accelerator(mock_to_lance, mem_db: DBConnection, rq_
|
||||
assert kwargs["num_bits"] == rq_kwargs["num_bits"]
|
||||
|
||||
|
||||
@patch("lancedb.table.LanceTable.to_lance")
|
||||
def test_accelerated_index_rejected_on_checked_out_version(
|
||||
mock_to_lance, mem_db: DBConnection
|
||||
):
|
||||
from lancedb.index import IvfPq
|
||||
|
||||
table = mem_db.create_table("test", [{"vector": [1.0] * 8}])
|
||||
table.add([{"vector": [2.0] * 8}])
|
||||
table.checkout(1)
|
||||
torch = MagicMock()
|
||||
torch.cuda.is_available.return_value = True
|
||||
with patch.dict(sys.modules, {"torch": torch}):
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="table cannot be modified when a specific version is checked out",
|
||||
):
|
||||
table.create_index("vector", config=IvfPq(accelerator="cuda"))
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="table cannot be modified when a specific version is checked out",
|
||||
):
|
||||
table.create_index("l2", vector_column_name="vector", accelerator="cuda")
|
||||
|
||||
mock_to_lance.return_value.create_index.assert_not_called()
|
||||
assert table.version == 1
|
||||
|
||||
|
||||
@pytest.mark.parametrize("method", ["create_index", "create_index_async"])
|
||||
@pytest.mark.parametrize("wait_timeout", ["invalid", 1, []])
|
||||
def test_create_index_invalid_wait_timeout(mem_db: DBConnection, method, wait_timeout):
|
||||
@@ -5760,6 +5787,42 @@ def test_compact_cleanup(tmp_db: DBConnection):
|
||||
table.checkout(3)
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"write",
|
||||
[
|
||||
pytest.param(lambda table: table.compact_files(), id="compact_files"),
|
||||
pytest.param(
|
||||
lambda table: table.cleanup_old_versions(), id="cleanup_old_versions"
|
||||
),
|
||||
pytest.param(
|
||||
lambda table: table.merge(
|
||||
pa.table({"id": [0, 1, 2], "doc": ["a", "b", "c"]}), left_on="id"
|
||||
),
|
||||
id="merge",
|
||||
),
|
||||
],
|
||||
)
|
||||
def test_pylance_writes_rejected_on_checked_out_version(tmp_db: DBConnection, write):
|
||||
pytest.importorskip("lance")
|
||||
table = tmp_db.create_table("my_table", data=[{"id": 0}])
|
||||
table.add([{"id": 1}])
|
||||
table.add([{"id": 2}])
|
||||
table.checkout(2)
|
||||
|
||||
with pytest.raises(
|
||||
ValueError,
|
||||
match="table cannot be modified when a specific version is checked out",
|
||||
):
|
||||
write(table)
|
||||
|
||||
# The handle stays pinned and the latest version is untouched
|
||||
assert table.version == 2
|
||||
assert table.count_rows() == 2
|
||||
latest = tmp_db.open_table("my_table")
|
||||
assert latest.version == 3
|
||||
assert latest.schema.names == ["id"]
|
||||
|
||||
|
||||
def test_count_rows(mem_db: DBConnection):
|
||||
table = mem_db.create_table(
|
||||
"my_table",
|
||||
|
||||
Reference in New Issue
Block a user