mirror of
https://github.com/lancedb/lancedb.git
synced 2026-11-26 12:33:59 +00:00
fix(python): preserve blob query snapshots and row-id guidance (#5129)
## Summary - Preserve the query snapshot carried by a remote query result when `fetch_blobs` or `fetch_blob_files` receives that result table, instead of reading row IDs from the table's current snapshot. - Send the expected `x-lancedb-query-snapshot` on the descriptor take used by `fetch_blob_files` and verify the response echo before decoding any descriptor. NULL and empty descriptors cannot escape unverified. - Point missing `_rowid` and missing row-id errors at `take_offsets(offsets, with_row_id=True)` for callers that have row positions, while keeping the existing missing/deleted row-id details. - Keep `fetch_blobs` row-ID based. Positional access and seekable video/DataLoader handle design remain separate work aligned with ENT-4059; this PR does not add `offsets=` or change that contract. - Document the behavior and keep empty row-id requests at zero I/O. ## Compatibility Server 0.5.0 is the basic remote Blob API floor. Snapshot-bound descriptor reads need the actual query identity echo from the serving node (Sophon #10211, present in tag v1.13.0). If identity cannot be verified, `fetch_blob_files` fails explicitly instead of accepting descriptors. Metadata-free row-id input keeps the existing current-snapshot behavior. This does not claim production rollout or real server testing. ## Testing - `python/tests/test_blob.py`: 249 passed, 1 skipped - `python/tests/test_remote_db.py -k blob`: 133 passed - Rust remote blob-file unit tests: 13 passed - `cargo check --features remote --tests --examples` - `cargo fmt --all --check`, `ruff format` and `ruff check` Refs OSS-2881 (non-closing).
This commit is contained in:
@@ -1958,6 +1958,18 @@ def test_fetch_blob_apis_reject_missing_fragment_row_addr():
|
||||
_assert_fetch_apis_reject_missing_row_ids(table, [1 << 32, live])
|
||||
|
||||
|
||||
def test_fetch_blobs_missing_row_ids_suggest_take_offsets():
|
||||
table = _blob_table(
|
||||
"position_hint",
|
||||
[{"id": i, "image": b"x"} for i in range(3)],
|
||||
)
|
||||
with pytest.raises(ValueError, match="take_offsets") as exc:
|
||||
table.fetch_blobs("image", [10])
|
||||
message = str(exc.value)
|
||||
assert "first missing row ids: [10]" in message
|
||||
assert "with_row_id=True" in message
|
||||
|
||||
|
||||
def test_fetch_blob_apis_reject_deleted_row_ids():
|
||||
table = _blob_table(
|
||||
"deleted_rows",
|
||||
@@ -2216,8 +2228,11 @@ def test_fetch_blobs_from_query_result_without_row_id_raises():
|
||||
table = _blob_table("no_rowid", [{"id": 1, "image": b"x"}])
|
||||
hits = table.search().select(["id"]).to_arrow()
|
||||
assert "_rowid" not in hits.column_names
|
||||
with pytest.raises(ValueError, match="_rowid"):
|
||||
with pytest.raises(ValueError, match="take_offsets") as exc:
|
||||
table.fetch_blobs("image", hits)
|
||||
message = str(exc.value)
|
||||
assert "with_row_id=True" in message
|
||||
assert "list of row ids" in message
|
||||
|
||||
|
||||
_HYBRID_BLOB_SCHEMA = pa.schema(
|
||||
|
||||
@@ -6007,6 +6007,9 @@ def blob_descriptor_take_table():
|
||||
)
|
||||
|
||||
|
||||
_DEFAULT_DESCRIPTOR_IDENTITY = object()
|
||||
|
||||
|
||||
@contextlib.contextmanager
|
||||
def blob_remote_table(
|
||||
*,
|
||||
@@ -6022,6 +6025,7 @@ def blob_remote_table(
|
||||
search_scores=False,
|
||||
snapshot_supported=True,
|
||||
identity_header="v1:" + "a" * 64,
|
||||
descriptor_identity_header=_DEFAULT_DESCRIPTOR_IDENTITY,
|
||||
verify_identity=True,
|
||||
):
|
||||
if state is None:
|
||||
@@ -6066,6 +6070,11 @@ def blob_remote_table(
|
||||
request.send_header(
|
||||
"x-lancedb-version", str(BLOB_DESCRIBE_RESPONSE["version"])
|
||||
)
|
||||
if verify_identity and request.headers.get("x-lancedb-query-snapshot"):
|
||||
request.send_header(
|
||||
"x-lancedb-query-snapshot",
|
||||
request.headers["x-lancedb-query-snapshot"],
|
||||
)
|
||||
request.end_headers()
|
||||
request.wfile.write(chunk)
|
||||
elif request.path == "/v1/table/test/query/":
|
||||
@@ -6132,11 +6141,20 @@ def blob_remote_table(
|
||||
request.send_header("x-lancedb-version", header)
|
||||
if snapshot_supported:
|
||||
request.send_header("x-lancedb-query-version", header)
|
||||
identity = (
|
||||
identity_header(body)
|
||||
if callable(identity_header)
|
||||
else identity_header
|
||||
descriptor_query = body.get("filter", "").startswith("_rowid IN")
|
||||
use_descriptor_identity = (
|
||||
descriptor_query
|
||||
and descriptor_identity_header
|
||||
is not _DEFAULT_DESCRIPTOR_IDENTITY
|
||||
)
|
||||
if use_descriptor_identity:
|
||||
identity = descriptor_identity_header
|
||||
else:
|
||||
identity = (
|
||||
identity_header(body)
|
||||
if callable(identity_header)
|
||||
else identity_header
|
||||
)
|
||||
if identity is not None:
|
||||
request.send_header("x-lancedb-query-snapshot", identity)
|
||||
request.end_headers()
|
||||
@@ -6684,13 +6702,102 @@ def test_remote_blob_file_matches_file_error_and_range_contract():
|
||||
handle.read(1)
|
||||
|
||||
|
||||
def test_remote_blob_fetch_accepts_query_table():
|
||||
@pytest.mark.parametrize("api", ["fetch_blobs", "fetch_blob_files"])
|
||||
def test_remote_blob_fetch_accepts_metadata_free_query_table(api):
|
||||
hits = pa.table({"_rowid": pa.array([10, 20, 30], type=pa.uint64())})
|
||||
|
||||
with blob_remote_table() as table:
|
||||
blobs = table.fetch_blobs("image", hits)
|
||||
with blob_remote_table(descriptor_identity_header=None) as table:
|
||||
if api == "fetch_blobs":
|
||||
assert table.fetch_blobs("image", hits).to_pylist() == [
|
||||
b"alpha",
|
||||
None,
|
||||
b"gamma",
|
||||
]
|
||||
else:
|
||||
handles = table.fetch_blob_files("image", hits)
|
||||
try:
|
||||
assert [
|
||||
handle.read() if handle is not None else None for handle in handles
|
||||
] == [b"alpha", None, b"gamma"]
|
||||
finally:
|
||||
for handle in handles:
|
||||
if handle is not None:
|
||||
handle.close()
|
||||
|
||||
assert blobs.to_pylist() == [b"alpha", None, b"gamma"]
|
||||
|
||||
@pytest.mark.parametrize("api", ["fetch_blobs", "fetch_blob_files"])
|
||||
def test_remote_blob_fetch_from_query_result_keeps_query_snapshot(api):
|
||||
requests = []
|
||||
state = {"version": 1}
|
||||
with blob_remote_table(requests=requests, state=state) as table:
|
||||
hits = table.search().select(["id", "image"]).limit(3).to_arrow()
|
||||
assert hits.schema.metadata[b"lancedb:query_version"] == b"1"
|
||||
table.add([{"id": 4, "image": b"new-version"}])
|
||||
assert table.version == 2
|
||||
|
||||
if api == "fetch_blobs":
|
||||
assert table.fetch_blobs("image", hits).to_pylist() == [
|
||||
b"alpha",
|
||||
None,
|
||||
b"gamma",
|
||||
]
|
||||
fetches = [body for path, body in requests if path == "fetch_blobs"]
|
||||
assert fetches
|
||||
assert all(body["version"] == 1 for body in fetches)
|
||||
else:
|
||||
handles = table.fetch_blob_files("image", hits)
|
||||
try:
|
||||
assert [
|
||||
handle.read() if handle is not None else None for handle in handles
|
||||
] == [b"alpha", None, b"gamma"]
|
||||
finally:
|
||||
for handle in handles:
|
||||
if handle is not None:
|
||||
handle.close()
|
||||
descriptor_queries = [
|
||||
body
|
||||
for path, body in requests
|
||||
if path == "query" and body.get("filter", "").startswith("_rowid IN")
|
||||
]
|
||||
assert descriptor_queries
|
||||
assert all(body["version"] == 1 for body in descriptor_queries)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("descriptor_identity", [None, "v1:" + "b" * 64])
|
||||
def test_remote_blob_files_reject_unverified_descriptor_identity(descriptor_identity):
|
||||
requested = "v1:" + "a" * 64
|
||||
hits = pa.table(
|
||||
{"_rowid": pa.array([20, 40], type=pa.uint64())}
|
||||
).replace_schema_metadata(
|
||||
{
|
||||
b"lancedb:query_version": b"1",
|
||||
b"lancedb:query_snapshot": requested.encode(),
|
||||
}
|
||||
)
|
||||
requests = []
|
||||
|
||||
with blob_remote_table(
|
||||
requests=requests, descriptor_identity_header=descriptor_identity
|
||||
) as table:
|
||||
with pytest.raises(
|
||||
NotImplementedError, match="did not verify the query snapshot identity"
|
||||
):
|
||||
table.fetch_blob_files("image", hits)
|
||||
|
||||
assert not any(path.startswith("/v1/table/test/blob/") for path, _ in requests)
|
||||
assert not any(path == "/v1/table/test/fetch_blobs/" for path, _ in requests)
|
||||
|
||||
|
||||
@pytest.mark.parametrize("api", ["fetch_blobs", "fetch_blob_files"])
|
||||
def test_remote_blob_fetch_rejects_incomplete_query_snapshot(api):
|
||||
requests = []
|
||||
hits = pa.table(
|
||||
{"_rowid": pa.array([10], type=pa.uint64())}
|
||||
).replace_schema_metadata({b"lancedb:query_version": b"1"})
|
||||
with blob_remote_table(requests=requests) as table:
|
||||
with pytest.raises(NotImplementedError, match="x-lancedb-query-snapshot"):
|
||||
getattr(table, api)("image", hits)
|
||||
assert not any(path == "fetch_blobs" for path, _ in requests)
|
||||
|
||||
|
||||
def test_remote_blob_query_stashes_row_ids_for_fetch():
|
||||
|
||||
Reference in New Issue
Block a user