Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion rust/lance/src/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3444,6 +3444,7 @@ impl Dataset {
/// Collect all (relative_path, path) of the dataset files.
async fn collect_paths(&self) -> Result<Vec<(String, Path)>> {
let mut file_paths: Vec<(String, Path)> = Vec::new();
let mut blob_dirs = HashSet::new();
for fragment in self.manifest.fragments.iter() {
if let Some(RowIdMeta::External(external_file)) = &fragment.row_id_meta {
return Err(Error::internal(format!(
Expand All @@ -3463,8 +3464,36 @@ impl Dataset {
};
file_paths.push((
format!("{}/{}", DATA_DIR, data_file.path.clone()),
base_root,
base_root.clone(),
));

if !data_file
.schema(self.schema())
.fields_pre_order()
.any(|field| field.is_blob_v2())
{
continue;
}

// Blob v2 sidecars are not listed in the manifest. Their directory is
// derived from the owning data file, so enumerate it to copy packed and
// dedicated payloads without decoding blob descriptors. External blobs
// remain caller-owned references and are deliberately not collected.
let data_file_key = blob::data_file_key_from_path(data_file.path.as_str());
let relative_blob_dir = format!("{}/{}", DATA_DIR, data_file_key);
let blob_dir = base_root.clone().join(DATA_DIR).join(data_file_key);
// Overlays can make the same data file reachable from multiple fragments.
if blob_dirs.insert(blob_dir.clone()) {
let mut stream = self.object_store.read_dir_all(&blob_dir, None);
while let Some(meta) = stream.next().await.transpose()? {
if let Some(filename) = meta.location.filename() {
file_paths.push((
format!("{}/{}", relative_blob_dir, filename),
base_root.clone(),
));
}
}
}
}
if let Some(deletion_file) = &fragment.deletion_file {
let base_root = if let Some(base_id) = deletion_file.base_id {
Expand Down
2 changes: 1 addition & 1 deletion rust/lance/src/dataset/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4427,7 +4427,7 @@ async fn resolve_blob_read_location(
Ok(location)
}

fn data_file_key_from_path(path: &str) -> &str {
pub(super) fn data_file_key_from_path(path: &str) -> &str {
let filename = path.rsplit('/').next().unwrap_or(path);
filename.strip_suffix(".lance").unwrap_or(filename)
}
Expand Down
80 changes: 79 additions & 1 deletion rust/lance/src/dataset/tests/dataset_io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-FileCopyrightText: Copyright The Lance Authors

use std::collections::HashMap;
use std::num::NonZeroUsize;
use std::sync::{
Arc,
atomic::{AtomicUsize, Ordering},
Expand All @@ -22,7 +23,7 @@ use crate::dataset::{
};
use crate::session::Session;
use crate::session::caches::ManifestKey;
use crate::{Dataset, Error, Result};
use crate::{BlobArrayBuilder, BlobFieldOptions, Dataset, Error, Result, blob_field_with_options};
use lance_table::format::DataStorageFormat;

use crate::dataset::write::{CommitBuilder, InsertBuilder, WriteMode, WriteParams};
Expand Down Expand Up @@ -1917,6 +1918,83 @@ async fn test_deep_clone(
assert_eq!(count_files(store, &dst_root, "_deletions").await, 0);
}

#[tokio::test]
async fn test_deep_clone_copies_blob_v2_sidecars() {
let test_dir = TempStdDir::default();
let source_dir = test_dir.join("blob_source");
let clone_dir = test_dir.join("blob_clone");
let expected_blobs: [&[u8]; 2] = [b"packed!!", b"this payload uses a dedicated sidecar"];
let blob_field = blob_field_with_options(
"blob",
false,
BlobFieldOptions::default()
.with_inline_size_threshold(4)
.with_dedicated_size_threshold(NonZeroUsize::new(12).unwrap()),
);
let schema = Arc::new(ArrowSchema::new(vec![
ArrowField::new("id", DataType::Int32, false),
blob_field,
]));
let mut blobs = BlobArrayBuilder::new(2);
for value in expected_blobs {
blobs.push_bytes(value).unwrap();
}
let batch = RecordBatch::try_new(
schema.clone(),
vec![
Arc::new(Int32Array::from(vec![0, 1])),
blobs.finish().unwrap(),
],
)
.unwrap();

let mut source = Dataset::write(
RecordBatchIterator::new([Ok(batch)], schema),
source_dir.to_str().unwrap(),
Some(WriteParams {
max_rows_per_file: 1,
max_rows_per_group: 1,
data_storage_version: Some(LanceFileVersion::V2_2),
..Default::default()
}),
)
.await
.unwrap();
assert_eq!(source.count_fragments(), 2);
source
.create_index(
&["id"],
IndexType::Scalar,
Some("id_idx".to_string()),
&ScalarIndexParams::default(),
false,
)
.await
.unwrap();
let source_index_file_count =
count_files(source.object_store.as_ref(), &source.base, "_indices").await;

let cloned = Arc::new(
source
.deep_clone(clone_dir.to_str().unwrap(), source.version().version, None)
.await
.unwrap(),
);
let cloned_indices = cloned.load_indices().await.unwrap();
assert_eq!(cloned_indices.len(), 1);
assert_eq!(cloned_indices[0].name, "id_idx");
assert_eq!(
count_files(cloned.object_store.as_ref(), &cloned.base, "_indices").await,
source_index_file_count
);

let cloned_blobs = cloned.take_blobs_by_indices(&[0, 1], "blob").await.unwrap();
for (blob, expected) in cloned_blobs.into_iter().zip(expected_blobs) {
let actual = blob.unwrap().read().await.unwrap();
assert_eq!(actual.as_ref(), expected);
}
}

#[tokio::test]
async fn test_deep_clone_rejects_unsupported_writer_before_copying() {
let test_dir = TempStdDir::default();
Expand Down
Loading