1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors
use lance::dataset::Dataset;
use lance::dataset::builder::DatasetBuilder;
use lance_core::Result;
use lance_namespace::LanceNamespace;
use lance_namespace::error::NamespaceError;
use lance_namespace::models::{DescribeTableRequest, DescribeTableResponse};
use lance_namespace::schema::arrow_schema_to_json;
use super::{DirectoryNamespace, manifest, manifest_feature_flags};
impl DirectoryNamespace {
/// Internal describe_table implementation that doesn't record metrics.
/// Used by both the public describe_table (which records metrics) and
/// internal callers like resolve_table_location (which shouldn't).
pub(super) async fn describe_table_impl(
&self,
request: DescribeTableRequest,
) -> Result<DescribeTableResponse> {
let is_root_level = request.id.as_ref().is_some_and(|id| id.len() == 1);
let is_child_table = request.id.as_ref().is_some_and(|id| id.len() > 1);
let skip_manifest_for_root = self.dir_listing_enabled
&& is_root_level
&& !self.dir_listing_to_manifest_migration_enabled;
// Self-heal the manifest wherever it can be authoritative: a child table
// (no dir-listing fallback), a manifest-only namespace, or migration mode
// (manifest-first -- it can hold registered_table -> external .lance
// aliases that dir-listing cannot resolve, so a reader built before
// __manifest must re-probe to see them). The bypass -- skipping the probe
// -- applies ONLY to migration-disabled directory-backed root reads,
// which are served entirely from the directory listing.
if is_child_table
|| !self.dir_listing_enabled
|| self.dir_listing_to_manifest_migration_enabled
{
self.ensure_read_manifest().await?;
}
if let Some(manifest_ns) = self.manifest_ns_for_read()
&& !skip_manifest_for_root
{
match manifest_ns.describe_table(request.clone()).await {
Ok(mut response) => {
if let Some(ref table_uri) = response.table_uri {
// For backwards compatibility, only skip vending credentials when explicitly set to false
let vend = request.vend_credentials.unwrap_or(true);
let identity = request.identity.as_deref();
response.storage_options = self
.get_storage_options_for_table(table_uri, vend, identity)
.await?;
}
// Set managed_versioning flag when table_version_tracking_enabled
if self.table_version_tracking_enabled {
response.managed_versioning = Some(true);
}
return Ok(response);
}
Err(e) if manifest_feature_flags::is_incompatible_manifest_error(&e) => {
// An incompatible manifest must surface "please upgrade"
// rather than degrading to a directory-listing view.
return Err(e);
}
Err(e) if self.dir_listing_enabled && is_root_level => {
// Only a genuinely-absent table (e.g. an unmigrated on-disk
// table) may fall through to the directory check; any other
// manifest error must propagate rather than be read as missing.
if !Self::is_manifest_table_absent_error(&e) {
return Err(Self::classify_storage_error(e));
}
}
Err(e) => return Err(e),
}
}
if is_child_table {
return Err(self.child_namespace_requires_manifest_error());
}
let table_name = Self::table_name_from_id(&request.id)?;
let table_id = Self::format_table_id_from_request(&request.id);
if !self.dir_listing_enabled {
return Err(NamespaceError::TableNotFound { message: table_id }.into());
}
let table_uri = self.table_full_uri(&table_name);
// Atomically check table existence and deregistration status
let status = self.check_table_status(&table_name).await?;
if !status.exists {
return Err(NamespaceError::TableNotFound {
message: table_id.clone(),
}
.into());
}
if status.is_deregistered {
return Err(NamespaceError::TableNotFound {
message: format!("Table is deregistered: {}", table_id),
}
.into());
}
let load_detailed_metadata = request.load_detailed_metadata.unwrap_or(false);
let should_check_declared =
load_detailed_metadata || request.check_declared.unwrap_or(false);
// For backwards compatibility, only skip vending credentials when explicitly set to false
let vend_credentials = request.vend_credentials.unwrap_or(true);
let identity = request.identity.as_deref();
let is_only_declared = if should_check_declared {
if status.has_reserved_file {
Some(!self.table_has_actual_manifests(&table_name).await?)
} else {
Some(false)
}
} else {
None
};
if !load_detailed_metadata {
let storage_options = self
.get_storage_options_for_table(&table_uri, vend_credentials, identity)
.await?;
return Ok(DescribeTableResponse {
table: Some(table_name),
namespace: request.id.as_ref().map(|id| {
if id.len() > 1 {
id[..id.len() - 1].to_vec()
} else {
vec![]
}
}),
location: Some(table_uri.clone()),
table_uri: Some(table_uri),
storage_options,
is_only_declared,
managed_versioning: if self.table_version_tracking_enabled {
Some(true)
} else {
None
},
..Default::default()
});
}
if is_only_declared == Some(true) {
let storage_options = self
.get_storage_options_for_table(&table_uri, vend_credentials, identity)
.await?;
return Ok(DescribeTableResponse {
table: Some(table_name),
namespace: request.id.as_ref().map(|id| {
if id.len() > 1 {
id[..id.len() - 1].to_vec()
} else {
vec![]
}
}),
location: Some(table_uri.clone()),
table_uri: Some(table_uri),
storage_options,
is_only_declared,
managed_versioning: if self.table_version_tracking_enabled {
Some(true)
} else {
None
},
..Default::default()
});
}
// Try to load the dataset to get real information
// Use DatasetBuilder with storage options to support S3 with custom endpoints
let mut builder = DatasetBuilder::from_uri(&table_uri);
if let Some(opts) = &self.storage_options {
builder = builder.with_storage_options(opts.clone());
}
if let Some(sess) = &self.session {
builder = builder.with_session(sess.clone());
}
match builder.load().await {
Ok(mut dataset) => {
// If a specific version is requested, checkout that version
if let Some(requested_version) = request.version {
dataset = dataset
.checkout_version(requested_version as u64)
.await
.map_err(|e| {
let message = format!(
"Version {} not found for table '{}': {}",
requested_version, table_name, e
);
Self::map_open_error(
e,
NamespaceError::TableVersionNotFound { message },
)
})?;
}
let version_info = dataset.version();
let lance_schema = dataset.schema();
let arrow_schema: arrow_schema::Schema = lance_schema.into();
let json_schema = arrow_schema_to_json(&arrow_schema)?;
let storage_options = self
.get_storage_options_for_table(&table_uri, vend_credentials, identity)
.await?;
// Convert BTreeMap to HashMap for the response
let metadata: std::collections::HashMap<String, String> =
version_info.metadata.into_iter().collect();
Ok(DescribeTableResponse {
table: Some(table_name),
namespace: request.id.as_ref().map(|id| {
if id.len() > 1 {
id[..id.len() - 1].to_vec()
} else {
vec![]
}
}),
version: Some(version_info.version as i64),
location: Some(table_uri.clone()),
table_uri: Some(table_uri),
schema: Some(Box::new(json_schema)),
storage_options,
metadata: Some(metadata),
is_only_declared,
managed_versioning: if self.table_version_tracking_enabled {
Some(true)
} else {
None
},
..Default::default()
})
}
Err(err) => {
if manifest::ManifestNamespace::is_not_found_load_error(&err)
&& is_only_declared == Some(true)
{
let storage_options = self
.get_storage_options_for_table(&table_uri, vend_credentials, identity)
.await?;
Ok(DescribeTableResponse {
table: Some(table_name),
namespace: request.id.as_ref().map(|id| {
if id.len() > 1 {
id[..id.len() - 1].to_vec()
} else {
vec![]
}
}),
location: Some(table_uri.clone()),
table_uri: Some(table_uri),
storage_options,
is_only_declared,
managed_versioning: if self.table_version_tracking_enabled {
Some(true)
} else {
None
},
..Default::default()
})
} else {
Err(NamespaceError::Internal {
message: format!(
"Table directory exists but cannot load dataset {}: {:?}",
table_name, err
),
}
.into())
}
}
}
}
/// Build a `DatasetBuilder` for `table_uri` with this namespace's storage
/// options and session applied. Callers add version/branch scoping.
pub(super) fn configured_builder(&self, table_uri: &str) -> DatasetBuilder {
let mut builder = DatasetBuilder::from_uri(table_uri);
if let Some(opts) = &self.storage_options {
builder = builder.with_storage_options(opts.clone());
}
if let Some(sess) = &self.session {
builder = builder.with_session(sess.clone());
}
builder
}
pub(super) async fn load_dataset(
&self,
table_uri: &str,
version: Option<i64>,
operation: &str,
) -> Result<Dataset> {
self.load_branch_dataset(table_uri, None, version, operation)
.await
}
/// Like [`Self::load_dataset`], on `branch` when it names one.
pub(super) async fn load_branch_dataset(
&self,
table_uri: &str,
branch: Option<&str>,
version: Option<i64>,
operation: &str,
) -> Result<Dataset> {
if let Some(version) = version
&& version < 0
{
return Err(NamespaceError::InvalidInput {
message: format!(
"Table version for {} must be non-negative, got {}",
operation, version
),
}
.into());
}
let dataset = match Self::normalized_branch(branch)? {
Some(branch) => self.open_validated_branch(table_uri, branch).await?,
None => self
.configured_builder(table_uri)
.load()
.await
.map_err(|e| {
let message = format!(
"Failed to open table at '{}' for {}: {}",
table_uri, operation, e
);
Self::map_open_error(e, NamespaceError::TableNotFound { message })
})?,
};
if let Some(version) = version {
return dataset.checkout_version(version as u64).await.map_err(|e| {
let message = format!(
"Failed to checkout version {} for table at '{}' during {}: {}",
version, table_uri, operation, e
);
Self::map_open_error(e, NamespaceError::TableVersionNotFound { message })
});
}
Ok(dataset)
}
/// Migrate directory-based tables to the manifest.
///
/// This is a one-time migration operation that:
/// 1. Scans the directory for existing `.lance` tables
/// 2. Registers any unmigrated tables in the manifest
/// 3. Returns the count of tables that were migrated
///
/// This method is safe to run multiple times - it will skip tables that are already
/// registered in the manifest.
///
/// # Usage
///
/// After creating tables in directory-only mode or dual mode, you can migrate them
/// to the manifest to enable manifest-only mode:
///
/// ```no_run
/// #![recursion_limit = "256"]
/// # use lance_namespace_impls::DirectoryNamespaceBuilder;
/// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
/// // Create namespace with dual mode (manifest + directory listing)
/// let namespace = DirectoryNamespaceBuilder::new("/path/to/data")
/// .manifest_enabled(true)
/// .dir_listing_enabled(true)
/// .build()
/// .await?;
///
/// // ... tables are created and used ...
///
/// // Migrate existing directory tables to manifest
/// let migrated_count = namespace.migrate().await?;
/// println!("Migrated {} tables", migrated_count);
///
/// // Now you can disable directory listing for better performance:
/// // (requires rebuilding the namespace)
/// let namespace = DirectoryNamespaceBuilder::new("/path/to/data")
/// .manifest_enabled(true)
/// .dir_listing_enabled(false) // All tables now in manifest
/// .build()
/// .await?;
/// # Ok(())
/// # }
/// ```
///
/// # Returns
///
/// Returns the number of tables that were migrated to the manifest.
///
/// # Errors
///
/// Returns an error if:
/// - Manifest is not enabled
/// - Directory listing fails
/// - Manifest registration fails
pub async fn migrate(&self) -> Result<usize> {
// We only care about tables in the root namespace
let Some(manifest_ns) = self.manifest_ns_for_write().await? else {
return Ok(0); // No manifest, nothing to migrate
};
// Get all table locations already in the manifest
let manifest_locations = manifest_ns.list_manifest_table_locations().await?;
// Get all tables from directory and skip declared-only tables that have not
// written any actual version manifests yet.
let dir_tables = self
.filter_declared_tables(self.list_directory_tables().await?, false)
.await?;
// Register each directory table that doesn't have an overlapping location
// If a directory name already exists in the manifest,
// that means the table must have already been migrated or created
// in the manifest, so we can skip it.
let mut migrated_count = 0;
for table_name in dir_tables {
// For root namespace tables, the directory name is "table_name.lance"
let dir_name = format!("{}.lance", table_name);
if !manifest_locations.contains(&dir_name) {
manifest_ns.register_table(&table_name, dir_name).await?;
migrated_count += 1;
}
}
Ok(migrated_count)
}
}