feat(rust): open a native table at a retained version (#5218)

Opening a table by URI and then checking out a retained version requires
the latest manifest to be readable. A caller that persisted a version
can now open that snapshot directly with
`NativeTable::open_at_version(uri, name, version, params)`. The caller’s
`ReadParams` apply during the open, and the returned table is pinned
with the usual read-only checkout semantics.

The regression covers reopening a retained version after a later
manifest becomes unreadable.
This commit is contained in:
Xuanwo
2026-11-26 06:11:51 +08:00
committed by GitHub
parent 7e1478b547
commit cb7f1bad67
+109 -3
View File
@@ -3618,6 +3618,43 @@ impl NativeTable {
.await
}
/// Open a native table at a retained version of the dataset at `uri`.
///
/// The version is selected during the dataset open. The returned handle is
/// read-only until it checks out the latest version. `params` supplies the
/// session, storage options, and commit handler for the dataset.
///
/// ```
/// use lancedb::table::{BaseTable, NativeTable};
///
/// async fn count_snapshot(uri: &str, version: u64) -> lancedb::Result<usize> {
/// let table = NativeTable::open_at_version(uri, "events", version, Default::default()).await?;
/// let rows = table.count_rows(None).await?;
/// table.checkout_latest().await?;
/// Ok(rows)
/// }
/// ```
pub async fn open_at_version(
uri: &str,
name: &str,
version: u64,
params: ReadParams,
) -> Result<Self> {
Self::open_native(
uri,
name,
Vec::new(),
None,
Some(params),
None,
None,
HashSet::new(),
None,
Some(version),
)
.await
}
/// Opens an existing Table
///
/// # Arguments
@@ -3644,6 +3681,34 @@ impl NativeTable {
namespace_client: Option<Arc<dyn LanceNamespace>>,
pushdown_operations: HashSet<NamespaceClientPushdownOperation>,
managed_versioning: Option<bool>,
) -> Result<Self> {
Self::open_native(
uri,
name,
namespace,
write_store_wrapper,
params,
read_consistency_interval,
namespace_client,
pushdown_operations,
managed_versioning,
None,
)
.await
}
#[allow(clippy::too_many_arguments)]
async fn open_native(
uri: &str,
name: &str,
namespace: Vec<String>,
write_store_wrapper: Option<Arc<dyn WrappingObjectStore>>,
params: Option<ReadParams>,
read_consistency_interval: Option<std::time::Duration>,
namespace_client: Option<Arc<dyn LanceNamespace>>,
pushdown_operations: HashSet<NamespaceClientPushdownOperation>,
managed_versioning: Option<bool>,
version: Option<u64>,
) -> Result<Self> {
let params = params.unwrap_or_default();
// patch the params if we have a write store wrapper
@@ -3682,6 +3747,9 @@ impl NativeTable {
};
let mut builder = DatasetBuilder::from_uri(uri).with_read_params(params);
if let Some(version) = version {
builder = builder.with_version(version);
}
// Set up commit handler when managed_versioning is enabled
if managed_versioning && let Some(ref ns_client) = namespace_client {
@@ -3709,9 +3777,12 @@ impl NativeTable {
Err(e) => return Err(e.into()),
};
crate::docs::check_table_maintenance_warnings(&dataset, uri).await;
let dataset = DatasetConsistencyWrapper::new_latest(dataset, read_consistency_interval);
let dataset = if version.is_some() {
DatasetConsistencyWrapper::new_time_travel(dataset, read_consistency_interval)
} else {
crate::docs::check_table_maintenance_warnings(&dataset, uri).await;
DatasetConsistencyWrapper::new_latest(dataset, read_consistency_interval)
};
let id = Self::build_id(&namespace, name);
Ok(Self {
@@ -5308,6 +5379,41 @@ mod tests {
assert_eq!(table.name, "test")
}
#[tokio::test]
async fn opens_a_retained_native_version_with_an_unreadable_latest_manifest() {
let dir = tempdir().unwrap();
let path = dir.path().join("events.lance");
let uri = path.to_str().unwrap();
let batch = make_test_batches();
let rows = batch.num_rows();
let mut dataset = Dataset::write(
RecordBatchIterator::new([Ok(batch.clone())], batch.schema()),
uri,
None,
)
.await
.unwrap();
let retained = dataset.version().version;
dataset
.append(
RecordBatchIterator::new([Ok(batch.clone())], batch.schema()),
None,
)
.await
.unwrap();
assert_eq!(dataset.count_rows(None).await.unwrap(), rows * 2);
let latest = dataset.manifest_location().path.filename().unwrap();
std::fs::write(path.join("_versions").join(latest), b"unreadable manifest").unwrap();
assert!(NativeTable::open(uri).await.is_err());
let pinned = NativeTable::open_at_version(uri, "events", retained, ReadParams::default())
.await
.unwrap();
assert_eq!(pinned.version().await.unwrap(), retained);
assert_eq!(pinned.count_rows(None).await.unwrap(), rows);
assert!(pinned.dataset.ensure_mutable().is_err());
}
/// The per-row error store is a server feature; a LanceDB Embedded table says so
/// rather than answering with an empty listing.
#[tokio::test]