feat(python): persist permutation plans and reopen pinned snapshots (#5137)

## Summary

Add `permutation_builder(base).persist(db, table_name, *,
namespace_path=None, mode="create").execute()` for embedded local and
object-store destinations. The native create request carries the
namespace and create/overwrite mode, with no drop-before-create. The
returned table keeps the destination connection context so it can be
pickled and reopened directly, including namespaced and manifest-enabled
connections.

Persisted plans pickle by reference to their own branch and version, so
a later overwrite or cross-branch checkout does not change what an
existing pickle reads. In-memory plans, old serialized state, and
shallow copies keep their existing behavior. `Permutation.base_version`
and `base_branch` expose the recorded base snapshot from loaded
metadata. Enterprise destinations remain rejected in v1 because
reproducible remote plan ordering is follow-up work.

## Verification

- `test_permutation.py`: 93 passed.
- `test_torch.py`: 23 passed in a compatible torch 2.15 / NumPy 2.2 /
PyArrow 26 environment using the abi3 extension.
- S3 direct-result persistence/pickle test passed against a disposable
loopback Moto server using the existing fixture's fake keys and explicit
storage options.
- Earlier targeted checks predate the live snapshot pin repair: Rust
builder tests 12 passed; Rust check/clippy and Node type-check passed.
- The live snapshot pin repair added private PyO3 glue and was verified
with a rebuilt extension: `test_permutation.py` 93 passed,
`test_torch.py` 23 passed, and one loopback S3 direct-result test
passed.
- `ruff format --check`, `ruff check`, `pyright`, `git diff --check`:
passed.
This commit is contained in:
Xuanwo
2026-11-21 06:27:27 +08:00
committed by GitHub
parent 8b8778938f
commit 8fd1c716d9
9 changed files with 844 additions and 47 deletions
+36 -2
View File
@@ -9,7 +9,10 @@ use crate::{
use arrow::pyarrow::{PyArrowType, ToPyArrow};
use lancedb::{
dataloader::permutation::{
builder::{PermutationBuilder as LancePermutationBuilder, ShuffleStrategy},
builder::{
PermutationBuilder as LancePermutationBuilder, PermutationPersistMode,
PermutationPersistOptions, ShuffleStrategy,
},
reader::PermutationReader,
split::{SplitSizes, SplitStrategy},
},
@@ -17,7 +20,7 @@ use lancedb::{
};
use pyo3::{
Bound, PyAny, PyRef, PyRefMut, PyResult, Python,
exceptions::PyRuntimeError,
exceptions::{PyRuntimeError, PyValueError},
pyclass, pymethods,
types::{PyAnyMethods, PyDict, PyDictMethods, PyType},
};
@@ -247,6 +250,37 @@ impl PyAsyncPermutationBuilder {
slf.modify(|builder| builder.with_filter(filter))
}
/// Store the permutation table in a database.
///
/// This is only supported for embedded databases. The destination table is
/// created in the selected namespace, and an existing table is replaced only
/// when ``mode="overwrite"`` is explicit.
#[pyo3(signature = (db, table_name, *, namespace_path=None, mode="create"))]
pub fn persist(
slf: PyRefMut<'_, Self>,
db: PyRef<'_, crate::connection::Connection>,
table_name: String,
namespace_path: Option<Vec<String>>,
mode: &str,
) -> PyResult<Self> {
let mode = match mode {
"create" => PermutationPersistMode::Create,
"overwrite" => PermutationPersistMode::Overwrite,
other => {
return Err(PyValueError::new_err(format!(
"Invalid mode {other}; expected 'create' or 'overwrite'"
)));
}
};
let database = db.database()?;
let options = PermutationPersistOptions {
namespace_path: namespace_path.unwrap_or_default(),
mode,
};
slf.modify(|builder| builder.persist_with_options(database, table_name, options))
}
pub fn execute(slf: PyRefMut<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
let mut state = slf.state.lock().unwrap();
let builder = state
+9
View File
@@ -2120,6 +2120,15 @@ impl Table {
})
}
/// Return an independent handle pinned to the currently selected snapshot.
pub fn _checkout_current(self_: PyRef<'_, Self>) -> PyResult<Bound<'_, PyAny>> {
let inner = self_.inner_ref()?.clone();
future_into_py(self_.py(), async move {
let current = inner.checkout_current().await.infer_error()?;
Ok(Table::new(current))
})
}
#[pyo3(signature = (version=None))]
pub fn restore(
self_: PyRef<'_, Self>,