mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
test(python): cover embedded plans over enterprise bases (#5150)
## Summary Clarify in the persistence guide and `persist` docstring that only the plan destination must be Embedded. The base table can be Embedded or Enterprise; an Enterprise base is pinned through the plan's recorded base branch/version. Add a focused loopback regression that persists a plan from a mock remote base into an Embedded database, reopens the plan by destination with `db.open_table`, advances the mock base's latest version to v2, and verifies a fresh remote handle observes v2 while `Permutation` still reads the plan's recorded main/v1 coordinates. Sequential iteration and duplicate/unsorted offsets follow the stored plan order, and the mock records nonempty v1 scan/gather requests. ## Verification - Focused test plus affected remote mock tests: 3 passed. - `ruff format --check` and `ruff check` passed. - `git diff --check` passed. This is a docs/test-only follow-up. It does not add Enterprise-resident plan persistence or change plan ordering.
This commit is contained in:
@@ -294,10 +294,12 @@ class PermutationBuilder:
|
||||
"""
|
||||
Store the permutation table in an embedded database.
|
||||
|
||||
Persisting a plan makes the same split available to another process or
|
||||
another machine with access to the destination storage. The plan is an
|
||||
ordinary LanceDB table containing row ids and split ids; it can be
|
||||
opened with [open_table][lancedb.db.DBConnection.open_table] and passed to
|
||||
The base table can be Embedded or Enterprise; this connection is only the
|
||||
plan destination. Persisting a plan makes the same split available to
|
||||
another process or another machine with access to the destination storage.
|
||||
The plan is an ordinary LanceDB table containing row ids and split ids; it
|
||||
can be opened with [open_table][lancedb.db.DBConnection.open_table] and
|
||||
passed to
|
||||
[Permutation.from_tables][lancedb.permutation.Permutation.from_tables].
|
||||
The table returned by ``execute`` keeps the destination connection and
|
||||
namespace context, so it can be pickled and reopened directly.
|
||||
@@ -305,8 +307,10 @@ class PermutationBuilder:
|
||||
Parameters
|
||||
----------
|
||||
db: DBConnection
|
||||
An embedded LanceDB connection. Local files and object storage are
|
||||
supported; LanceDB Enterprise connections are not supported yet.
|
||||
An embedded LanceDB connection for the plan destination. Local files
|
||||
and object storage are supported. The base table may be Embedded or
|
||||
Enterprise; persisting the plan itself to Enterprise is not supported
|
||||
yet.
|
||||
table_name: str
|
||||
The name of the permutation table to create.
|
||||
namespace_path: list[str], optional
|
||||
|
||||
@@ -222,6 +222,55 @@ def test_base_branch_property_reads_plan_metadata(tmp_path):
|
||||
assert permutation.num_rows == 20
|
||||
|
||||
|
||||
def test_persist_embedded_plan_for_remote_base(tmp_path):
|
||||
from lancedb import deserialize_conn
|
||||
|
||||
server = MockPermutationServer(num_rows=8)
|
||||
db = connect(tmp_path)
|
||||
|
||||
with mock_remote_table(server) as remote:
|
||||
permutation_builder(remote).shuffle(seed=7).persist(db, "plan").execute()
|
||||
plan = db.open_table("plan")
|
||||
expected_ids = plan.to_arrow().to_pydict()["row_id"]
|
||||
assert expected_ids != list(range(8))
|
||||
|
||||
# Advance the mock's latest version. A fresh handle must see v2, while
|
||||
# the plan's recorded v1 still pins every read to the old snapshot.
|
||||
server.publish_version(2, list(range(100, 108)))
|
||||
fresh_remote = deserialize_conn(
|
||||
remote._serialized_connection_state()
|
||||
).open_table(server.name)
|
||||
assert fresh_remote.version == 2
|
||||
|
||||
permutation = Permutation.from_tables(fresh_remote, plan)
|
||||
assert permutation.base_version == 1
|
||||
assert permutation.base_branch is None
|
||||
assert permutation._plan_version == 1
|
||||
assert permutation.permutation_table.version == 1
|
||||
|
||||
sequential_ids = [
|
||||
row["id"]
|
||||
for batch in permutation.iter(3, skip_last_batch=False)
|
||||
for row in batch
|
||||
]
|
||||
assert sequential_ids == expected_ids
|
||||
assert [row["id"] for row in permutation.__getitems__([5, 1, 5, 3])] == [
|
||||
expected_ids[i] for i in [5, 1, 5, 3]
|
||||
]
|
||||
|
||||
# The plan is local, but its build and gathers address the remote base
|
||||
# at the recorded main/v1 snapshot. Latest describes carry no version.
|
||||
assert server.scans
|
||||
assert server.takes
|
||||
for body in [*server.scans, *server.takes]:
|
||||
assert body.get("version") == 1
|
||||
assert body.get("branch") is None
|
||||
describe_versions = [body.get("version") for body in server.describe_bodies]
|
||||
assert None in describe_versions
|
||||
assert 1 in describe_versions
|
||||
assert all(body.get("branch") is None for body in server.describe_bodies)
|
||||
|
||||
|
||||
def test_persisted_plan_pickle_reopens_same_snapshot_after_overwrite(tmp_path):
|
||||
from datetime import timedelta
|
||||
import pickle
|
||||
|
||||
@@ -51,21 +51,42 @@ class MockPermutationServer:
|
||||
the server, which is the part that has to stay compatible.
|
||||
"""
|
||||
|
||||
def __init__(self, name="remote_data", num_rows=8, vary_scan_order=False):
|
||||
def __init__(
|
||||
self, name="remote_data", num_rows=8, vary_scan_order=False, latest_version=1
|
||||
):
|
||||
self.name = name
|
||||
self.num_rows = num_rows
|
||||
self.latest_version = latest_version
|
||||
self.version_rows = {1: list(range(num_rows))}
|
||||
self.describe_bodies = []
|
||||
self.query_bodies = []
|
||||
# Stand in for a distributed scan that answers in no fixed order.
|
||||
self.vary_scan_order = vary_scan_order
|
||||
self.scan_calls = 0
|
||||
|
||||
def publish_version(self, version, row_ids):
|
||||
"""Advance the mock's latest version without changing its schema."""
|
||||
self.latest_version = version
|
||||
self.version_rows[version] = list(row_ids)
|
||||
|
||||
def _rows_for_version(self, requested_version):
|
||||
version = (
|
||||
self.latest_version if requested_version is None else requested_version
|
||||
)
|
||||
return self.version_rows.get(version, self.version_rows[1])
|
||||
|
||||
def __call__(self, request):
|
||||
path = request.path
|
||||
if path == f"/v1/table/{self.name}/describe/":
|
||||
body = self._read_body(request)
|
||||
self.describe_bodies.append(body)
|
||||
version = (
|
||||
self.latest_version if body.get("version") is None else body["version"]
|
||||
)
|
||||
return self._json(
|
||||
request,
|
||||
{
|
||||
"version": 1,
|
||||
"version": version,
|
||||
"schema": {
|
||||
"fields": [
|
||||
{"name": "id", "type": {"type": "int64"}, "nullable": False}
|
||||
@@ -78,8 +99,8 @@ class MockPermutationServer:
|
||||
# Null spec: this table has no RTE write path.
|
||||
return self._json(request, {"rte_spec": None})
|
||||
if path == f"/v1/table/{self.name}/count_rows/":
|
||||
self._read_body(request)
|
||||
return self._json(request, self.num_rows)
|
||||
body = self._read_body(request)
|
||||
return self._json(request, len(self._rows_for_version(body.get("version"))))
|
||||
if path == f"/v1/table/{self.name}/query/":
|
||||
return self._query(request, self._read_body(request))
|
||||
|
||||
@@ -124,10 +145,15 @@ class MockPermutationServer:
|
||||
|
||||
def _query(self, request, body):
|
||||
self.query_bodies.append(body)
|
||||
version_rows = self._rows_for_version(body.get("version"))
|
||||
|
||||
if is_row_id_take(body):
|
||||
# A row-id take. Answer ascending, so tests prove the client reorders.
|
||||
row_ids = sorted(parse_in_list(body["filter"]))
|
||||
row_ids = sorted(
|
||||
row_id
|
||||
for row_id in parse_in_list(body["filter"])
|
||||
if row_id in version_rows
|
||||
)
|
||||
return self._arrow(
|
||||
request,
|
||||
pa.table(
|
||||
@@ -140,7 +166,7 @@ class MockPermutationServer:
|
||||
|
||||
if body.get("columns") == ["_rowid"]:
|
||||
# The permutation build scan: row ids and nothing else.
|
||||
row_ids = list(range(self.num_rows))
|
||||
row_ids = list(version_rows)
|
||||
if self.vary_scan_order and self.scan_calls % 2:
|
||||
row_ids.reverse()
|
||||
self.scan_calls += 1
|
||||
|
||||
Reference in New Issue
Block a user