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:
@@ -656,7 +656,9 @@ fn missing_blob_row_ids_error(column: &str, requested: usize, missing: &[u64]) -
|
||||
Error::InvalidInput {
|
||||
message: format!(
|
||||
"blob read for column '{column}' requested {requested} row ids but some \
|
||||
do not exist in the table{detail}; pass row ids collected from this table"
|
||||
do not exist in the table{detail}; pass row ids collected from this table. \
|
||||
If these were row positions, call take_offsets(offsets, with_row_id=True) \
|
||||
first and pass the resulting row ids"
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1156,7 +1156,9 @@ impl<S: HttpSend> RemoteTable<S> {
|
||||
return Err(Error::InvalidInput {
|
||||
message: format!(
|
||||
"blob read for column '{column}' requested {} row ids but only {found} exist \
|
||||
in the table; pass row ids collected from this table",
|
||||
in the table; pass row ids collected from this table. If these were row \
|
||||
positions, call take_offsets(offsets, with_row_id=True) first and pass the \
|
||||
resulting row ids",
|
||||
unique_row_ids.len()
|
||||
),
|
||||
});
|
||||
@@ -1190,9 +1192,20 @@ impl<S: HttpSend> RemoteTable<S> {
|
||||
.client
|
||||
.post(&format!("/v1/table/{}/query/", self.identifier))
|
||||
.json(&body);
|
||||
// The query route opens the requested version and reports the identity of
|
||||
// the dataset it actually read. It does not validate the request identity,
|
||||
// so accept descriptors only after comparing the response echo.
|
||||
let request = if let Some(identity) = &self.expected_query_snapshot {
|
||||
request.header(QUERY_SNAPSHOT_HEADER, identity)
|
||||
} else {
|
||||
request
|
||||
};
|
||||
let (request_id, response) = self
|
||||
.send_with_freshness(request, true, read_snapshot.freshness)
|
||||
.await?;
|
||||
if response.status().is_success() {
|
||||
check_query_snapshot_response(self.expected_query_snapshot.as_deref(), &response)?;
|
||||
}
|
||||
let response_version = response
|
||||
.headers()
|
||||
.get(&VERSION_HEADER)
|
||||
@@ -2046,6 +2059,80 @@ mod tests {
|
||||
assert!(requests.lock().unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn remote_blob_files_reject_unverified_descriptor_identity_before_values() {
|
||||
const EXPECTED: &str =
|
||||
"v1:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb";
|
||||
const OTHER: &str = "v1:cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc";
|
||||
for response_identity in [None, Some(OTHER)] {
|
||||
let range_requests = Arc::new(AtomicUsize::new(0));
|
||||
let captured_ranges = range_requests.clone();
|
||||
let table = RemoteTable::new_mock(
|
||||
"my_table".to_string(),
|
||||
move |request| {
|
||||
let path = request.url().path().to_string();
|
||||
if path == "/v1/table/my_table/query/" {
|
||||
// Row 20 is NULL and row 40 is empty, so an unverified
|
||||
// result could escape without any payload request.
|
||||
assert_eq!(request.headers()[QUERY_SNAPSHOT_HEADER], EXPECTED);
|
||||
let body: serde_json::Value =
|
||||
serde_json::from_slice(request.body().unwrap().as_bytes().unwrap())
|
||||
.unwrap();
|
||||
assert_eq!(filter_row_ids(body["filter"].as_str().unwrap()), [20, 40]);
|
||||
let batch = RecordBatch::try_from_iter([
|
||||
(
|
||||
"image",
|
||||
Arc::new(descriptors(&[None, Some((BlobKind::Inline, 0))]))
|
||||
as Arc<dyn Array>,
|
||||
),
|
||||
(
|
||||
ROW_ID,
|
||||
Arc::new(UInt64Array::from_iter_values([20, 40])) as Arc<dyn Array>,
|
||||
),
|
||||
])
|
||||
.unwrap();
|
||||
let mut body = Vec::new();
|
||||
let mut writer =
|
||||
arrow_ipc::writer::FileWriter::try_new(&mut body, &batch.schema())
|
||||
.unwrap();
|
||||
writer.write(&batch).unwrap();
|
||||
writer.finish().unwrap();
|
||||
drop(writer);
|
||||
let mut response = http::Response::builder()
|
||||
.status(200)
|
||||
.header(VERSION_HEADER, "42");
|
||||
if let Some(identity) = response_identity {
|
||||
response = response.header(QUERY_SNAPSHOT_HEADER, identity);
|
||||
}
|
||||
return response.body(body).unwrap();
|
||||
}
|
||||
if path.starts_with("/v1/table/my_table/blob/") {
|
||||
captured_ranges.fetch_add(1, Ordering::SeqCst);
|
||||
panic!("descriptor verification must fail before any payload read");
|
||||
}
|
||||
panic!("unexpected path: {path}");
|
||||
},
|
||||
Some(Version::new(0, 5, 0)),
|
||||
);
|
||||
let snapshot = table
|
||||
.query_snapshot_with_identity(42, EXPECTED)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let error = snapshot
|
||||
.fetch_blob_files("image", &[20, 40])
|
||||
.await
|
||||
.unwrap_err();
|
||||
assert!(
|
||||
error
|
||||
.to_string()
|
||||
.contains("did not verify the query snapshot identity"),
|
||||
"{error}"
|
||||
);
|
||||
assert_eq!(range_requests.load(Ordering::SeqCst), 0);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn remote_blob_ranges_accept_empty_requests_without_http() {
|
||||
let (table, requests) = mock_take_blob_table(vec![], Some("7"));
|
||||
|
||||
Reference in New Issue
Block a user