1use crate::{
5 RetryPolicy,
6 config::{self, CredsSet},
7 handlers::NamespaceIdent,
8 sessions::{self},
9};
10use anyhow::{Context, Result, anyhow, bail};
11use lance::Dataset;
12use lance::dataset::builder::DatasetBuilder;
13use lance::dataset::index::DatasetIndexRemapperOptions;
14use lance::dataset::optimize::{
15 CompactionMode, CompactionOptions, commit_compaction, plan_compaction,
16};
17pub use lance::dataset::write::merge_insert::MergeStats;
18use lance::dataset::write::merge_insert::SourceDedupeBehavior;
19use lance::dataset::{InsertBuilder, MergeInsertBuilder, WhenMatched, WhenNotMatched, WriteMode};
20pub use lance::dataset::{WriteParams, WriteStats};
21use lance::deps::arrow_array::{Array, RecordBatch, RecordBatchIterator, StringArray};
22use lance::deps::datafusion::physical_plan::SendableRecordBatchStream;
23use lance::index::DatasetIndexExt;
24use lance::index::DatasetIndexInternalExt;
25use lance::index::vector::VectorIndexParams;
26use lance::session::Session;
27use lance_index::IndexType;
28use lance_index::optimize::OptimizeOptions;
29use lance_index::scalar::{BuiltinIndexType, InvertedIndexParams, ScalarIndexParams};
30use lance_index::vector::ivf::IvfBuildParams;
31use lance_index::vector::sq::builder::SQBuildParams;
32use lance_io::object_store::{
33 ChainedWrappingObjectStore, ObjectStore, ObjectStoreParams, ObjectStoreRegistry,
34 StorageOptionsAccessor, WrappingObjectStore, uri_to_url,
35};
36use lance_linalg::distance::MetricType;
37use lance_namespace::LanceNamespace;
38use lance_namespace::error::{ErrorCode, NamespaceError};
39use lance_namespace::models::DescribeTableRequest;
40use lance_namespace_impls::ConnectBuilder;
41use std::{
42 collections::{BTreeMap, HashMap},
43 path::PathBuf,
44 sync::Arc,
45 time::{Duration, Instant},
46};
47use tokio::sync::{Mutex, OnceCell};
48use tokio_stream::StreamExt;
49use url::Url;
50pub const VECTOR_INDEX_ACTIVATION_ROWS: usize = 100_000;
55
56pub const DELTA_MERGE_THRESHOLD: usize = 4;
63
64#[derive(Debug, Clone, PartialEq)]
74pub struct StorageUrl {
75 canonical: Url,
79 lance: Url,
81 scheme_options: Vec<(&'static str, String)>,
83 query_options: Vec<(&'static str, String)>,
85 creds_pointer: Option<String>,
87 endpoint: Option<S3Endpoint>,
92}
93
94#[derive(Debug, Clone, PartialEq)]
95struct S3Endpoint {
96 scheme: &'static str,
97 authority: String,
99 bucket: String,
100}
101
102const RECOGNIZED_QUERY_PARAMS: [&str; 3] = ["creds", "region", "virtual_hosted_style_request"];
106
107impl StorageUrl {
108 pub fn parse(input: &str) -> Result<Self> {
112 let trimmed = input.trim();
113 if trimmed.is_empty() {
114 bail!("storage path is empty");
115 }
116 if !trimmed.contains("://") || trimmed.starts_with("file://") {
119 let url =
120 uri_to_url(trimmed).with_context(|| format!("invalid storage path {trimmed:?}"))?;
121 if url.query().is_some() {
126 bail!("storage URL {trimmed:?} carries query params; local URLs take none");
127 }
128 return Ok(Self::plain(url));
129 }
130 let url =
131 Url::parse(trimmed).with_context(|| format!("invalid storage URL {trimmed:?}"))?;
132 if !url.username().is_empty() || url.password().is_some() {
134 bail!(
135 "storage URL {trimmed:?} embeds credentials; put them in [creds.*] (or POND_CREDS_*) instead"
136 );
137 }
138 match url.scheme() {
139 "memory" | "shared-memory" => {
140 if url.query().is_some() {
141 bail!(
142 "storage URL {trimmed:?} carries query params; {}:// URLs take none",
143 url.scheme(),
144 );
145 }
146 Ok(Self::plain(url))
147 }
148 "s3" | "gs" => {
149 let (canonical, query_options, creds_pointer) = strip_query(url)?;
150 let mut lance = canonical.clone();
151 lance.set_query(None);
152 Ok(Self {
153 canonical,
154 lance,
155 scheme_options: Vec::new(),
156 query_options,
157 creds_pointer,
158 endpoint: None,
159 })
160 }
161 "s3+https" | "s3+http" => {
162 let (mut canonical, query_options, creds_pointer) = strip_query(url)?;
163 let tls = canonical.scheme() == "s3+https";
164 if canonical.port() == Some(if tls { 443 } else { 80 }) {
168 let _ = canonical.set_port(None);
169 }
170 let host = canonical
171 .host_str()
172 .ok_or_else(|| anyhow!("storage URL {trimmed:?} has no endpoint host"))?;
173 let endpoint_authority = match canonical.port() {
174 Some(port) => format!("{host}:{port}"),
175 None => host.to_owned(),
176 };
177 let mut segments = canonical.path().trim_start_matches('/').splitn(2, '/');
178 let bucket = segments.next().unwrap_or_default().to_owned();
179 let prefix = segments.next().unwrap_or_default().to_owned();
180 if bucket.is_empty() {
181 bail!(
182 "storage URL {trimmed:?} is missing the bucket: the form is {}://host/bucket/prefix",
183 canonical.scheme(),
184 );
185 }
186 let lance = Url::parse(&format!("s3://{bucket}/{prefix}")).with_context(|| {
187 format!("storage URL {trimmed:?}: bucket/prefix do not form a valid s3:// URL")
188 })?;
189 let scheme = if tls { "https" } else { "http" };
190 let virtual_hosted = host.parse::<std::net::IpAddr>().is_err()
199 && !matches!(canonical.host(), Some(url::Host::Ipv6(_)));
200 let scheme_options = vec![
201 ("allow_http", (!tls).to_string()),
202 ("virtual_hosted_style_request", virtual_hosted.to_string()),
203 ("region", "us-east-1".to_owned()),
210 ];
211 Ok(Self {
212 canonical,
213 lance,
214 scheme_options,
215 query_options,
216 creds_pointer,
217 endpoint: Some(S3Endpoint {
218 scheme,
219 authority: endpoint_authority,
220 bucket,
221 }),
222 })
223 }
224 "az" => {
225 let (canonical, query_options, creds_pointer) = strip_query(url)?;
226 let account = canonical
227 .host_str()
228 .ok_or_else(|| anyhow!("storage URL {trimmed:?} has no account: the form is az://account/container/prefix"))?
229 .to_owned();
230 let mut segments = canonical.path().trim_start_matches('/').splitn(2, '/');
231 let container = segments.next().unwrap_or_default();
232 if container.is_empty() {
233 bail!(
234 "storage URL {trimmed:?} is missing the container: the form is az://account/container/prefix"
235 );
236 }
237 let prefix = segments.next().unwrap_or_default();
238 let lance = Url::parse(&format!("az://{container}/{prefix}"))
239 .with_context(|| format!("storage URL {trimmed:?}: container/prefix do not form a valid az:// URL"))?;
240 Ok(Self {
241 canonical,
242 lance,
243 scheme_options: vec![("account_name", account)],
244 query_options,
245 creds_pointer,
246 endpoint: None,
247 })
248 }
249 other => bail!(
250 "storage URL scheme {other:?} not recognized; use a local path, s3://, s3+https://, s3+http://, gs://, or az://"
251 ),
252 }
253 }
254
255 fn plain(url: Url) -> Self {
257 Self {
258 canonical: url.clone(),
259 lance: url,
260 scheme_options: Vec::new(),
261 query_options: Vec::new(),
262 creds_pointer: None,
263 endpoint: None,
264 }
265 }
266
267 pub fn lance_url(&self) -> &Url {
269 &self.lance
270 }
271
272 pub fn canonical(&self) -> &Url {
275 &self.canonical
276 }
277
278 pub fn is_local(&self) -> bool {
279 config::is_local(&self.canonical)
280 }
281
282 pub fn display(&self) -> String {
284 config::display(&self.canonical)
285 }
286
287 fn takes_credentials(&self) -> bool {
290 !matches!(
291 self.canonical.scheme(),
292 "file" | "file+uring" | "memory" | "shared-memory"
293 )
294 }
295
296 pub fn resolve(&self, creds: &BTreeMap<String, CredsSet>) -> Result<ResolvedStorage> {
303 if !self.takes_credentials() {
304 return Ok(ResolvedStorage {
305 storage: self.clone(),
306 options: HashMap::new(),
307 binding: CredsBinding::NotApplicable,
308 });
309 }
310 let matched: Option<(&String, &CredsSet, BindVia)> = match &self.creds_pointer {
311 Some(name) => {
312 let set = creds.get(name).ok_or_else(|| {
313 anyhow!(
314 "URL names ?creds={name} but no [creds.{name}] set is configured; define it or drop the pointer"
315 )
316 })?;
317 Some((name, set, BindVia::Pointer))
318 }
319 None => {
320 let mut best: Option<(&String, &CredsSet, String)> = None;
321 for (name, set) in creds {
322 let Some(scope) = &set.scope else { continue };
323 let scope_url = parse_scope(scope).with_context(|| {
324 format!("[creds.{name}] scope {scope:?} is not a valid URL prefix")
325 })?;
326 if scope_matches(&scope_url, &self.canonical)
327 && best
328 .as_ref()
329 .is_none_or(|(_, _, len)| scope_url.as_str().len() > len.len())
330 {
331 best = Some((name, set, scope_url.as_str().to_owned()));
332 }
333 }
334 match best {
335 Some((name, set, _)) => Some((name, set, BindVia::Scope)),
336 None => creds
337 .iter()
338 .find(|(_, set)| set.scope.is_none())
339 .map(|(name, set)| (name, set, BindVia::CatchAll)),
340 }
341 }
342 };
343 let mut options: HashMap<String, String> = self
344 .scheme_options
345 .iter()
346 .map(|(key, value)| ((*key).to_owned(), value.clone()))
347 .collect();
348 let binding = match matched {
349 None => CredsBinding::Ambient,
350 Some((name, set, via)) => {
351 if let Some(region) = &set.region {
352 options.insert("region".to_owned(), region.clone());
353 }
354 if let Some(virtual_hosted) = set.virtual_hosted_style_request {
355 options.insert(
356 "virtual_hosted_style_request".to_owned(),
357 virtual_hosted.to_string(),
358 );
359 }
360 for (key, value) in &set.extra {
361 options.insert(key.clone(), value.clone());
362 }
363 if let Some(value) = materialize_secret(
364 name,
365 "access_key_id",
366 set.access_key_id.as_deref(),
367 set.access_key_id_file.as_deref(),
368 None,
369 )? {
370 options.insert("access_key_id".to_owned(), value);
371 }
372 if let Some(value) = materialize_secret(
373 name,
374 "secret_access_key",
375 set.secret_access_key.as_deref(),
376 set.secret_access_key_file.as_deref(),
377 set.secret_access_key_command.as_deref(),
378 )? {
379 options.insert("secret_access_key".to_owned(), value);
380 }
381 CredsBinding::Set {
382 name: name.clone(),
383 via,
384 }
385 }
386 };
387 for (key, value) in &self.query_options {
388 options.insert((*key).to_owned(), value.clone());
389 }
390 if let Some(endpoint) = &self.endpoint
395 && !options.keys().any(|key| {
396 key.eq_ignore_ascii_case("endpoint") || key.eq_ignore_ascii_case("aws_endpoint")
397 })
398 {
399 let virtual_hosted = options
400 .get("virtual_hosted_style_request")
401 .is_some_and(|value| value == "true");
402 let url = if virtual_hosted {
403 format!(
404 "{}://{}.{}",
405 endpoint.scheme, endpoint.bucket, endpoint.authority
406 )
407 } else {
408 format!("{}://{}", endpoint.scheme, endpoint.authority)
409 };
410 options.insert("endpoint".to_owned(), url);
411 }
412 Ok(ResolvedStorage {
413 storage: self.clone(),
414 options,
415 binding,
416 })
417 }
418}
419
420type StrippedQuery = (Url, Vec<(&'static str, String)>, Option<String>);
422
423fn strip_query(url: Url) -> Result<StrippedQuery> {
425 let mut query_options = Vec::new();
426 let mut creds_pointer = None;
427 for (key, value) in url.query_pairs() {
428 match RECOGNIZED_QUERY_PARAMS
429 .iter()
430 .find(|known| **known == key.as_ref())
431 {
432 Some(&"creds") => creds_pointer = Some(value.into_owned()),
433 Some(known) => query_options.push((*known, value.into_owned())),
434 None => bail!(
435 "storage URL query param {key:?} not recognized (known: {})",
436 RECOGNIZED_QUERY_PARAMS.join(", "),
437 ),
438 }
439 }
440 let mut canonical = url;
441 canonical.set_query(None);
442 Ok((canonical, query_options, creds_pointer))
443}
444
445pub(crate) fn parse_scope(scope: &str) -> Result<Url> {
448 let mut url = Url::parse(scope.trim())?;
449 if !url.username().is_empty() || url.password().is_some() {
450 bail!("scope embeds credentials");
451 }
452 if url.query().is_some() {
453 bail!("scope carries query params; scopes are plain URL prefixes");
454 }
455 match (url.scheme(), url.port()) {
456 ("s3+https", Some(443)) | ("s3+http", Some(80)) => {
457 let _ = url.set_port(None);
458 }
459 _ => {}
460 }
461 Ok(url)
462}
463
464fn scope_matches(scope: &Url, address: &Url) -> bool {
469 if scope.scheme() != address.scheme()
470 || scope.host_str() != address.host_str()
471 || scope.port() != address.port()
472 {
473 return false;
474 }
475 let scope_path = scope.path().trim_end_matches('/');
476 let address_path = address.path().trim_end_matches('/');
477 address_path == scope_path
478 || address_path
479 .strip_prefix(scope_path)
480 .is_some_and(|rest| rest.starts_with('/'))
481}
482
483#[derive(Debug, Clone, Copy, PartialEq, Eq)]
486pub enum BindVia {
487 Pointer,
489 Scope,
491 CatchAll,
493}
494
495#[derive(Debug, Clone, PartialEq)]
496pub enum CredsBinding {
497 Set { name: String, via: BindVia },
499 Ambient,
504 NotApplicable,
506}
507
508impl CredsBinding {
509 pub fn describe(&self) -> String {
511 match self {
512 Self::Set { name, via } => {
513 let via = match via {
514 BindVia::Pointer => "?creds",
515 BindVia::Scope => "scope match",
516 BindVia::CatchAll => "catch-all",
517 };
518 format!("creds {name} ({via})")
519 }
520 Self::Ambient => "ambient chain".to_owned(),
521 Self::NotApplicable => "local (no credentials)".to_owned(),
522 }
523 }
524}
525
526#[derive(Debug, Clone)]
530pub struct ResolvedStorage {
531 storage: StorageUrl,
532 pub options: HashMap<String, String>,
533 pub binding: CredsBinding,
534}
535
536impl ResolvedStorage {
537 pub fn lance_url(&self) -> &Url {
538 self.storage.lance_url()
539 }
540
541 pub fn display(&self) -> String {
542 self.storage.display()
543 }
544}
545
546pub fn unmatched_creds_sets<'c>(
551 resolved: &[&ResolvedStorage],
552 creds: &'c BTreeMap<String, CredsSet>,
553) -> Vec<&'c str> {
554 if resolved
555 .iter()
556 .all(|entry| matches!(entry.binding, CredsBinding::NotApplicable))
557 {
558 return Vec::new();
559 }
560 creds
561 .keys()
562 .filter(|name| {
563 !resolved.iter().any(|entry| {
564 matches!(&entry.binding, CredsBinding::Set { name: bound, .. } if bound == *name)
565 })
566 })
567 .map(String::as_str)
568 .collect()
569}
570
571fn materialize_secret(
574 set: &str,
575 field: &str,
576 inline: Option<&str>,
577 file: Option<&std::path::Path>,
578 command: Option<&str>,
579) -> Result<Option<String>> {
580 if let Some(value) = inline {
581 return Ok(Some(value.to_owned()));
582 }
583 if let Some(path) = file {
584 let text = std::fs::read_to_string(path).with_context(|| {
585 format!(
586 "[creds.{set}] {field}_file: failed to read {}",
587 path.display()
588 )
589 })?;
590 return Ok(Some(strip_one_newline(text)));
591 }
592 if let Some(command) = command {
593 return Ok(Some(run_secret_command(set, field, command)?));
594 }
595 Ok(None)
596}
597
598fn run_secret_command(set: &str, field: &str, command: &str) -> Result<String> {
601 static CACHE: std::sync::OnceLock<std::sync::Mutex<HashMap<String, String>>> =
602 std::sync::OnceLock::new();
603 let cache = CACHE.get_or_init(Default::default);
604 if let Some(hit) = cache
605 .lock()
606 .unwrap_or_else(std::sync::PoisonError::into_inner)
607 .get(command)
608 {
609 return Ok(hit.clone());
610 }
611 let output = std::process::Command::new("sh")
612 .arg("-c")
613 .arg(command)
614 .output()
615 .with_context(|| format!("[creds.{set}] {field}_command failed to spawn: {command}"))?;
616 if !output.status.success() {
617 bail!(
618 "[creds.{set}] {field}_command exited {}: {command}\n{}",
619 output.status,
620 String::from_utf8_lossy(&output.stderr).trim_end(),
621 );
622 }
623 let value = strip_one_newline(
624 String::from_utf8(output.stdout)
625 .with_context(|| format!("[creds.{set}] {field}_command output is not UTF-8"))?,
626 );
627 cache
628 .lock()
629 .unwrap_or_else(std::sync::PoisonError::into_inner)
630 .insert(command.to_owned(), value.clone());
631 Ok(value)
632}
633
634fn strip_one_newline(mut text: String) -> String {
637 if text.ends_with('\n') {
638 text.pop();
639 if text.ends_with('\r') {
640 text.pop();
641 }
642 }
643 text
644}
645
646#[derive(Debug, thiserror::Error)]
654pub enum CheckFailure {
655 #[error(
656 "authentication failed and no creds set matched this URL; add one with `pond creds add` (or set POND_CREDS_*), or provide ambient AWS_* credentials"
657 )]
658 NoCreds { source: anyhow::Error },
659 #[error("authentication failed using creds set {set:?}; check its keys and scope")]
660 Auth { set: String, source: anyhow::Error },
661 #[error(
662 "backend does not enforce conditional writes (If-None-Match); concurrent pond writers would corrupt each other - {detail}"
663 )]
664 OccUnsupported { detail: String },
665 #[error("storage probe failed")]
666 Io { source: anyhow::Error },
667}
668
669impl CheckFailure {
670 pub fn concise_cause(&self) -> Option<String> {
676 let source = match self {
677 Self::NoCreds { source } | Self::Auth { source, .. } | Self::Io { source } => source,
678 Self::OccUnsupported { .. } => return None,
679 };
680 Some(condense_error_chain(source))
681 }
682}
683
684fn condense_error_chain(error: &anyhow::Error) -> String {
691 let mut text = error
692 .chain()
693 .last()
694 .map(ToString::to_string)
695 .unwrap_or_else(|| format!("{error:#}"));
696 if let Some(pos) = text.find(", <WORKSPACE>") {
697 text.truncate(pos);
698 }
699 text = text.replace(
700 "Encountered internal error. Please file a bug report at https://github.com/lance-format/lance/issues. ",
701 "",
702 );
703 let line = text.split_whitespace().collect::<Vec<_>>().join(" ");
704 const HEAD: usize = 120;
705 const TAIL: usize = 120;
706 let chars: Vec<char> = line.chars().collect();
707 if chars.len() > HEAD + TAIL + 5 {
708 let head: String = chars[..HEAD].iter().collect();
709 let tail: String = chars[chars.len() - TAIL..].iter().collect();
710 format!("{head} ... {tail}")
711 } else {
712 line
713 }
714}
715
716pub async fn storage_check(resolved: &ResolvedStorage) -> std::result::Result<(), CheckFailure> {
721 use object_store::{Error as OsError, ObjectStoreExt, PutMode, PutOptions, PutPayload};
722
723 let classify =
724 |error: OsError, step: &str| classify_check_error(error, &resolved.binding, step);
725
726 let probe_uri = format!(
727 "{}/_config-check/{}",
728 resolved.lance_url().as_str().trim_end_matches('/'),
729 uuid::Uuid::now_v7(),
730 );
731 let params = ObjectStoreParams {
732 storage_options_accessor: (!resolved.options.is_empty()).then(|| {
733 Arc::new(StorageOptionsAccessor::with_static_options(
734 resolved.options.clone(),
735 ))
736 }),
737 ..Default::default()
738 };
739 let registry = Arc::new(ObjectStoreRegistry::default());
740 let (store, path) = ObjectStore::from_uri_and_params(registry, &probe_uri, ¶ms)
741 .await
742 .map_err(|error| CheckFailure::Io {
743 source: anyhow!(error).context(format!("failed to open object store for {probe_uri}")),
744 })?;
745
746 let body: &[u8] = b"pond storage check";
747 let create = PutOptions::from(PutMode::Create);
748 store
749 .inner
750 .put_opts(&path, PutPayload::from_static(body), create.clone())
751 .await
752 .map_err(|error| classify(error, "initial conditional put"))?;
753 let outcome = async {
757 match store
762 .inner
763 .put_opts(&path, PutPayload::from_static(body), create)
764 .await
765 {
766 Err(OsError::AlreadyExists { .. }) => {}
767 Ok(_) => {
768 return Err(CheckFailure::OccUnsupported {
769 detail: "a second create over an existing key succeeded".to_owned(),
770 });
771 }
772 Err(OsError::NotImplemented { .. }) => {
773 return Err(CheckFailure::OccUnsupported {
774 detail: "the backend rejects conditional puts as unimplemented".to_owned(),
775 });
776 }
777 Err(error) => return Err(classify(error, "conditional-put probe")),
778 }
779 let read_back = store
780 .inner
781 .get(&path)
782 .await
783 .map_err(|error| classify(error, "read-back"))?
784 .bytes()
785 .await
786 .map_err(|error| classify(error, "read-back body"))?;
787 if read_back.as_ref() != body {
788 return Err(CheckFailure::Io {
789 source: anyhow!("read-back returned different bytes than written"),
790 });
791 }
792 Ok(())
793 }
794 .await;
795 let cleanup = store.inner.delete(&path).await;
796 outcome?;
797 cleanup.map_err(|error| classify(error, "cleanup delete"))?;
798 Ok(())
799}
800
801fn classify_check_error(
805 error: object_store::Error,
806 binding: &CredsBinding,
807 step: &str,
808) -> CheckFailure {
809 use object_store::Error as OsError;
810 let auth_class = matches!(
815 error,
816 OsError::Unauthenticated { .. } | OsError::PermissionDenied { .. }
817 ) || {
818 let rendered = error.to_string();
819 rendered.contains("CredentialsNotLoaded")
820 || rendered.contains("no providers in chain provided credentials")
821 };
822 match (auth_class, binding) {
823 (true, CredsBinding::Set { name, .. }) => CheckFailure::Auth {
824 set: name.clone(),
825 source: anyhow!(error).context(step.to_owned()),
826 },
827 (true, _) => CheckFailure::NoCreds {
828 source: anyhow!(error).context(step.to_owned()),
829 },
830 (false, _) => CheckFailure::Io {
831 source: anyhow!(error).context(step.to_owned()),
832 },
833 }
834}
835
836pub const DEFAULT_COMPACTION_FRAGMENT_CAP: usize = 64;
840
841pub const TARGET_FRAGMENT_BYTES: u64 = 256 * 1024 * 1024;
845
846const MIN_TARGET_ROWS_PER_FRAGMENT: u64 = 50_000;
847const MAX_TARGET_ROWS_PER_FRAGMENT: u64 = 1024 * 1024;
849
850pub const COMPACTION_ABSORB_FACTOR: u64 = 4;
853
854pub fn default_cleanup_older_than() -> chrono::Duration {
860 chrono::Duration::hours(1)
864}
865
866pub const DEFAULT_SYNC_CLEANUP_INTERVAL: u64 = 16;
873
874pub const DEFAULT_SYNC_SCALAR_FOLD_ROWS: usize = 50_000;
883
884pub const DEFAULT_SYNC_INDEX_FOLD_ROWS: usize = 5_000;
894
895#[derive(Debug, Clone, Copy)]
900pub struct MaintenancePolicy {
901 pub compaction_fragment_cap: usize,
903 pub cleanup_older_than: chrono::Duration,
905 pub cleanup_interval: u64,
910 pub scalar_fold_row_threshold: usize,
915 pub index_fold_row_threshold: usize,
921}
922
923impl MaintenancePolicy {
924 pub fn always_compact() -> Self {
926 Self {
927 compaction_fragment_cap: 0,
928 cleanup_older_than: default_cleanup_older_than(),
929 cleanup_interval: 1,
930 scalar_fold_row_threshold: 0,
931 index_fold_row_threshold: 0,
932 }
933 }
934
935 #[must_use]
938 pub fn with_cleanup_interval(mut self, interval: u64) -> Self {
939 self.cleanup_interval = interval.max(1);
940 self
941 }
942
943 #[must_use]
947 pub fn with_scalar_fold_row_threshold(mut self, threshold: usize) -> Self {
948 self.scalar_fold_row_threshold = threshold;
949 self
950 }
951
952 #[must_use]
956 pub fn with_index_fold_row_threshold(mut self, threshold: usize) -> Self {
957 self.index_fold_row_threshold = threshold;
958 self
959 }
960
961 fn fold_thresholds(&self) -> FoldThresholds {
964 FoldThresholds {
965 scalar: self.scalar_fold_row_threshold,
966 index: self.index_fold_row_threshold,
967 }
968 }
969}
970
971#[derive(Debug, Clone, Copy)]
974struct FoldThresholds {
975 scalar: usize,
976 index: usize,
977}
978
979struct FragmentStat {
980 bytes: Option<u64>,
982 rows: u64,
983 deleted_rows: u64,
984}
985
986fn fragment_bytes(fragment: &lance::table::format::Fragment) -> Option<u64> {
989 fragment.files.iter().try_fold(0u64, |total, file| {
990 Some(total + file.file_size_bytes.get()?.get())
991 })
992}
993
994fn fragment_stat(fragment: &lance::table::format::Fragment) -> FragmentStat {
995 FragmentStat {
996 bytes: fragment_bytes(fragment),
997 rows: fragment.physical_rows.unwrap_or(0) as u64,
998 deleted_rows: fragment
999 .deletion_file
1000 .as_ref()
1001 .and_then(|deletions| deletions.num_deleted_rows)
1002 .unwrap_or(0) as u64,
1003 }
1004}
1005
1006fn derived_target_rows(stats: &[FragmentStat]) -> usize {
1017 let (mut bytes, mut rows) = (0u64, 0u64);
1018 for stat in stats {
1019 if let Some(fragment_bytes) = stat.bytes
1020 && stat.rows > 0
1021 {
1022 bytes += fragment_bytes;
1023 rows += stat.rows;
1024 }
1025 }
1026 if bytes == 0 || rows == 0 {
1027 return MAX_TARGET_ROWS_PER_FRAGMENT as usize;
1028 }
1029 let avg_row_bytes = (bytes / rows).max(1);
1030 (TARGET_FRAGMENT_BYTES / 2 / avg_row_bytes)
1031 .clamp(MIN_TARGET_ROWS_PER_FRAGMENT, MAX_TARGET_ROWS_PER_FRAGMENT) as usize
1032}
1033
1034fn keep_task(stats: &[FragmentStat], cap: usize, deletion_threshold: f32) -> bool {
1039 if stats.iter().any(|stat| {
1040 stat.rows > 0 && (stat.deleted_rows as f32 / stat.rows as f32) > deletion_threshold
1041 }) {
1042 return true;
1043 }
1044 if stats.len() >= cap {
1045 return true;
1046 }
1047 let weights: Vec<u64> = if stats.iter().all(|stat| stat.bytes.is_some()) {
1048 stats.iter().filter_map(|stat| stat.bytes).collect()
1049 } else {
1050 stats.iter().map(|stat| stat.rows).collect()
1051 };
1052 let total: u64 = weights.iter().sum();
1053 let largest = weights.iter().copied().max().unwrap_or(0);
1054 (total - largest) * COMPACTION_ABSORB_FACTOR >= largest
1055}
1056
1057#[derive(Debug, Clone)]
1060pub struct IndexIntent {
1061 pub name: &'static str,
1064 pub column: &'static str,
1066 pub trigger: IndexTrigger,
1068 pub params: IndexParamsKind,
1071}
1072
1073#[derive(Debug, Clone)]
1075pub enum IndexTrigger {
1076 OnAnyRows,
1079 OnNonNullCount {
1082 column: &'static str,
1083 threshold: usize,
1084 },
1085}
1086
1087#[derive(Debug, Clone)]
1090pub enum IndexParamsKind {
1091 Scalar(BuiltinIndexType),
1094 InvertedFtsWord,
1100 IvfSqCosine { num_bits: u16, max_iters: usize },
1109}
1110
1111impl IndexTrigger {
1112 async fn should_create(&self, dataset: &Dataset) -> Result<bool> {
1113 match self {
1114 Self::OnAnyRows => Ok(dataset.count_rows(None).await? > 0),
1115 Self::OnNonNullCount { column, threshold } => {
1116 let count = dataset
1117 .count_rows(Some(format!("{column} IS NOT NULL")))
1118 .await?;
1119 Ok(count >= *threshold)
1120 }
1121 }
1122 }
1123}
1124
1125impl IndexParamsKind {
1126 fn index_type(&self) -> IndexType {
1127 match self {
1128 Self::Scalar(BuiltinIndexType::Bitmap) => IndexType::Bitmap,
1129 Self::Scalar(BuiltinIndexType::ZoneMap) => IndexType::ZoneMap,
1130 Self::Scalar(_) => IndexType::BTree,
1131 Self::InvertedFtsWord => IndexType::Inverted,
1132 Self::IvfSqCosine { .. } => IndexType::Vector,
1133 }
1134 }
1135
1136 async fn build(&self, dataset: &Dataset) -> Result<Box<dyn lance::index::IndexParams>> {
1137 match self {
1138 Self::Scalar(kind) => Ok(Box::new(ScalarIndexParams::for_builtin(kind.clone()))),
1139 Self::InvertedFtsWord => Ok(Box::new(
1140 InvertedIndexParams::default()
1141 .base_tokenizer("simple".to_owned())
1142 .stem(true)
1143 .remove_stop_words(false),
1144 )),
1145 Self::IvfSqCosine {
1146 num_bits,
1147 max_iters,
1148 } => {
1149 let count = dataset
1150 .count_rows(Some("vector IS NOT NULL".to_owned()))
1151 .await?;
1152 let partitions = count.checked_div(4096).unwrap_or(0).max(1);
1153 let mut ivf = IvfBuildParams::new(partitions);
1154 ivf.max_iters = *max_iters;
1155 let sq = SQBuildParams {
1156 num_bits: *num_bits,
1157 ..Default::default()
1158 };
1159 Ok(Box::new(VectorIndexParams::with_ivf_sq_params(
1160 MetricType::Cosine,
1161 ivf,
1162 sq,
1163 )))
1164 }
1165 }
1166 }
1167}
1168
1169#[derive(Debug, Clone, PartialEq, Eq)]
1170pub struct IndexStatus {
1171 pub table: Table,
1172 pub intent_name: String,
1173 pub fragments_covered: usize,
1174 pub unindexed_fragments: usize,
1175 pub unindexed_rows: usize,
1176 pub exists: bool,
1177}
1178
1179#[derive(Debug, Clone, Copy)]
1184pub struct ConflictExhausted {
1185 pub attempts: u8,
1186}
1187
1188impl std::fmt::Display for ConflictExhausted {
1189 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1190 write!(
1191 formatter,
1192 "commit conflict exhausted after {} attempt(s)",
1193 self.attempts
1194 )
1195 }
1196}
1197
1198impl std::error::Error for ConflictExhausted {}
1199
1200#[derive(Debug)]
1205pub enum PhaseOutcome {
1206 Ok,
1208 Noop,
1210 SkippedConflict,
1213 Failed(anyhow::Error),
1215 NotAttempted,
1218}
1219
1220impl PhaseOutcome {
1221 pub fn is_failed(&self) -> bool {
1222 matches!(self, Self::Failed(_))
1223 }
1224}
1225
1226#[derive(Debug)]
1228pub struct TableOptimizeOutcome {
1229 pub table: Table,
1230 pub indices: PhaseOutcome,
1231 pub compaction: PhaseOutcome,
1232}
1233
1234#[derive(Debug, Clone)]
1237pub enum OptimizeEvent {
1238 PhaseStart {
1239 table: Table,
1240 phase: OptimizePhase,
1241 detail: Option<String>,
1242 },
1243 PhaseDone {
1244 table: Table,
1245 phase: OptimizePhase,
1246 elapsed_ms: u64,
1247 },
1248 IndexStage {
1253 table: Table,
1254 index: String,
1255 stage: String,
1256 completed: u64,
1257 total: Option<u64>,
1258 unit: String,
1259 },
1260}
1261
1262#[derive(Debug, Clone, Copy)]
1263pub enum OptimizePhase {
1264 Compact,
1265 Cleanup,
1266 IndexCreate,
1267 IndexRebuild,
1268 IndexAppend,
1269}
1270
1271impl OptimizePhase {
1272 pub fn label(self) -> &'static str {
1273 match self {
1274 Self::Compact => "compact",
1275 Self::Cleanup => "cleanup",
1276 Self::IndexCreate => "index-create",
1277 Self::IndexRebuild => "index-rebuild",
1278 Self::IndexAppend => "index-append",
1279 }
1280 }
1281}
1282
1283pub type OptimizeProgressFn = Arc<dyn Fn(OptimizeEvent) + Send + Sync>;
1287
1288fn emit(progress: Option<&OptimizeProgressFn>, event: OptimizeEvent) {
1289 if let Some(callback) = progress {
1290 callback(event);
1291 }
1292}
1293
1294struct PondIndexProgress {
1303 callback: OptimizeProgressFn,
1304 table: Table,
1305 index: String,
1306 state: std::sync::Mutex<PondIndexStageState>,
1307}
1308
1309impl std::fmt::Debug for PondIndexProgress {
1312 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1313 f.debug_struct("PondIndexProgress")
1314 .field("table", &self.table)
1315 .field("index", &self.index)
1316 .finish_non_exhaustive()
1317 }
1318}
1319
1320#[derive(Debug, Default)]
1321struct PondIndexStageState {
1322 total: Option<u64>,
1323 unit: String,
1324 last_emit: Option<Instant>,
1325}
1326
1327impl PondIndexProgress {
1328 fn new(callback: OptimizeProgressFn, table: Table, index: String) -> Arc<Self> {
1329 Arc::new(Self {
1330 callback,
1331 table,
1332 index,
1333 state: std::sync::Mutex::new(PondIndexStageState::default()),
1334 })
1335 }
1336}
1337
1338#[async_trait::async_trait]
1339impl lance_index::progress::IndexBuildProgress for PondIndexProgress {
1340 async fn stage_start(&self, stage: &str, total: Option<u64>, unit: &str) -> lance::Result<()> {
1341 if let Ok(mut state) = self.state.lock() {
1342 state.total = total;
1343 state.unit = unit.to_owned();
1344 state.last_emit = Some(Instant::now());
1345 }
1346 (self.callback)(OptimizeEvent::IndexStage {
1347 table: self.table,
1348 index: self.index.clone(),
1349 stage: stage.to_owned(),
1350 completed: 0,
1351 total,
1352 unit: unit.to_owned(),
1353 });
1354 Ok(())
1355 }
1356
1357 async fn stage_progress(&self, stage: &str, completed: u64) -> lance::Result<()> {
1358 let (total, unit) = {
1359 let Ok(mut state) = self.state.lock() else {
1360 return Ok(());
1361 };
1362 let now = Instant::now();
1363 if let Some(prev) = state.last_emit
1364 && now.duration_since(prev) < Duration::from_millis(100)
1365 {
1366 return Ok(());
1367 }
1368 state.last_emit = Some(now);
1369 (state.total, state.unit.clone())
1370 };
1371 (self.callback)(OptimizeEvent::IndexStage {
1372 table: self.table,
1373 index: self.index.clone(),
1374 stage: stage.to_owned(),
1375 completed,
1376 total,
1377 unit,
1378 });
1379 Ok(())
1380 }
1381
1382 async fn stage_complete(&self, stage: &str) -> lance::Result<()> {
1383 let (total, unit) = {
1384 let Ok(state) = self.state.lock() else {
1385 return Ok(());
1386 };
1387 (state.total, state.unit.clone())
1388 };
1389 (self.callback)(OptimizeEvent::IndexStage {
1390 table: self.table,
1391 index: self.index.clone(),
1392 stage: stage.to_owned(),
1393 completed: total.unwrap_or(0),
1394 total,
1395 unit,
1396 });
1397 Ok(())
1398 }
1399}
1400
1401fn lance_progress(
1402 progress: Option<&OptimizeProgressFn>,
1403 table: Table,
1404 index: &str,
1405) -> Arc<dyn lance_index::progress::IndexBuildProgress> {
1406 match progress {
1407 Some(callback) => PondIndexProgress::new(callback.clone(), table, index.to_owned()),
1408 None => Arc::new(lance_index::progress::NoopIndexBuildProgress),
1409 }
1410}
1411
1412pub fn is_commit_conflict(error: &anyhow::Error) -> bool {
1416 error.downcast_ref::<lance::Error>().is_some_and(|err| {
1417 matches!(
1418 err,
1419 lance::Error::CommitConflict { .. }
1420 | lance::Error::RetryableCommitConflict { .. }
1421 | lance::Error::TooMuchWriteContention { .. }
1422 )
1423 })
1424}
1425
1426fn is_conflict_exhausted(error: &anyhow::Error) -> bool {
1429 error.chain().any(|cause| cause.is::<ConflictExhausted>())
1430}
1431
1432pub fn is_index_error(error: &anyhow::Error) -> bool {
1436 error
1437 .downcast_ref::<lance::Error>()
1438 .is_some_and(|err| matches!(err, lance::Error::Index { .. }))
1439}
1440
1441#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
1445pub struct TableSizes {
1446 pub sessions: u64,
1447 pub messages: u64,
1448 pub parts: u64,
1449 pub other: u64,
1450 pub sessions_data: DataLiveness,
1451 pub messages_data: DataLiveness,
1452 pub parts_data: DataLiveness,
1453}
1454
1455#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
1458pub struct DataLiveness {
1459 pub on_disk: u64,
1460 pub live: Option<u64>,
1462}
1463
1464impl DataLiveness {
1465 pub fn dead(&self) -> Option<u64> {
1466 self.live.map(|live| self.on_disk.saturating_sub(live))
1467 }
1468}
1469
1470#[derive(Debug, Clone, PartialEq, Eq)]
1471pub enum ScalarValue {
1472 String(String),
1473 Int32(i32),
1474 Raw(String),
1475}
1476impl From<&str> for ScalarValue {
1477 fn from(value: &str) -> Self {
1478 Self::String(value.to_owned())
1479 }
1480}
1481impl From<String> for ScalarValue {
1482 fn from(value: String) -> Self {
1483 Self::String(value)
1484 }
1485}
1486impl From<i32> for ScalarValue {
1487 fn from(value: i32) -> Self {
1488 Self::Int32(value)
1489 }
1490}
1491#[derive(Debug, Clone, PartialEq, Eq)]
1492pub enum Predicate {
1493 Eq(&'static str, ScalarValue),
1494 Ne(&'static str, ScalarValue),
1495 IsNull(&'static str),
1496 IsNotNull(&'static str),
1497 In(&'static str, Vec<ScalarValue>),
1498 LikeContains(&'static str, String),
1499 Regex(&'static str, String),
1504 Gte(&'static str, ScalarValue),
1505 Lte(&'static str, ScalarValue),
1506 And(Vec<Predicate>),
1507 Or(Vec<Predicate>),
1508 Not(Box<Predicate>),
1509}
1510impl Predicate {
1511 pub fn to_lance(&self) -> String {
1512 match self {
1513 Self::Eq(column, value) => format!("{column} = {}", value.to_lance()),
1514 Self::Ne(column, value) => format!("{column} <> {}", value.to_lance()),
1515 Self::IsNull(column) => format!("{column} IS NULL"),
1516 Self::IsNotNull(column) => format!("{column} IS NOT NULL"),
1517 Self::In(column, values) => {
1518 let values = values
1519 .iter()
1520 .map(ScalarValue::to_lance)
1521 .collect::<Vec<_>>()
1522 .join(", ");
1523 format!("{column} IN ({values})")
1524 }
1525 Self::LikeContains(column, value) => {
1526 format!("{column} LIKE {} ESCAPE '\\'", like_contains(value))
1527 }
1528 Self::Regex(column, pattern) => {
1529 format!("regexp_like({column}, {})", quoted_string(pattern))
1530 }
1531 Self::Gte(column, value) => format!("{column} >= {}", value.to_lance()),
1532 Self::Lte(column, value) => format!("{column} <= {}", value.to_lance()),
1533 Self::And(predicates) => predicates
1534 .iter()
1535 .map(Self::to_lance)
1536 .filter(|predicate| !predicate.is_empty())
1537 .collect::<Vec<_>>()
1538 .join(" AND "),
1539 Self::Or(predicates) => {
1540 let body = predicates
1543 .iter()
1544 .map(Self::to_lance)
1545 .filter(|predicate| !predicate.is_empty())
1546 .collect::<Vec<_>>()
1547 .join(" OR ");
1548 if body.is_empty() {
1549 String::new()
1550 } else {
1551 format!("({body})")
1552 }
1553 }
1554 Self::Not(inner) => {
1555 let body = inner.to_lance();
1556 if body.is_empty() {
1557 String::new()
1558 } else {
1559 format!("NOT ({body})")
1560 }
1561 }
1562 }
1563 }
1564}
1565#[derive(Default)]
1568pub struct ScanOpts<'a> {
1569 pub predicate: Option<&'a Predicate>,
1570 pub projection: Option<&'a [&'a str]>,
1571}
1572
1573impl<'a> ScanOpts<'a> {
1574 pub fn project_only(projection: &'a [&'a str]) -> Self {
1575 Self {
1576 predicate: None,
1577 projection: Some(projection),
1578 }
1579 }
1580 pub fn with_predicate_and_projection(
1581 predicate: &'a Predicate,
1582 projection: &'a [&'a str],
1583 ) -> Self {
1584 Self {
1585 predicate: Some(predicate),
1586 projection: Some(projection),
1587 }
1588 }
1589}
1590
1591impl ScalarValue {
1592 fn to_lance(&self) -> String {
1593 match self {
1594 Self::String(value) => quoted_string(value),
1595 Self::Int32(value) => value.to_string(),
1596 Self::Raw(value) => value.clone(),
1597 }
1598 }
1599}
1600#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
1604pub struct RuntimeCaps {
1605 pub index_cache_bytes: Option<usize>,
1606 pub metadata_cache_bytes: Option<usize>,
1607}
1608
1609impl RuntimeCaps {
1610 pub fn from_config(config: &crate::config::RuntimeConfig) -> Self {
1611 Self {
1612 index_cache_bytes: config.index_cache_bytes,
1613 metadata_cache_bytes: config.metadata_cache_bytes,
1614 }
1615 }
1616}
1617
1618const LOCAL_INDEX_CACHE_BYTES: usize = 256 * 1024 * 1024;
1622const LOCAL_METADATA_CACHE_BYTES: usize = 128 * 1024 * 1024;
1623const REMOTE_INDEX_CACHE_BYTES: usize = 1024 * 1024 * 1024;
1628const REMOTE_METADATA_CACHE_BYTES: usize = 512 * 1024 * 1024;
1629
1630fn resolve_cache_caps(location: &Url, caps: RuntimeCaps) -> (usize, usize) {
1631 let (index_default, metadata_default) = if config::is_local(location) {
1632 (LOCAL_INDEX_CACHE_BYTES, LOCAL_METADATA_CACHE_BYTES)
1633 } else {
1634 (REMOTE_INDEX_CACHE_BYTES, REMOTE_METADATA_CACHE_BYTES)
1635 };
1636 (
1637 caps.index_cache_bytes.unwrap_or(index_default),
1638 caps.metadata_cache_bytes.unwrap_or(metadata_default),
1639 )
1640}
1641
1642pub struct Handle {
1643 datasets: DatasetSet,
1644 retry: RetryPolicy,
1645 #[allow(dead_code)]
1653 session: Arc<Session>,
1654 nm: Arc<dyn LanceNamespace>,
1658 nm_ident: NamespaceIdent,
1662 storage_options: HashMap<String, String>,
1667 location: Url,
1671 lazy_refresh_after: Duration,
1675 index_wrapper: Option<Arc<dyn WrappingObjectStore>>,
1678}
1679
1680impl std::fmt::Debug for Handle {
1681 fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
1682 formatter
1683 .debug_struct("Handle")
1684 .field("datasets", &self.datasets)
1685 .field("retry", &self.retry)
1686 .field("nm_ident", &self.nm_ident)
1687 .field("storage_options", &self.storage_options)
1688 .field("location", &self.location)
1689 .finish()
1690 }
1691}
1692
1693#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1694pub enum Table {
1695 Sessions,
1696 Messages,
1697 Parts,
1698}
1699impl Table {
1700 pub fn as_str(self) -> &'static str {
1701 self.label()
1702 }
1703
1704 fn label(self) -> &'static str {
1705 match self {
1706 Self::Sessions => "sessions",
1707 Self::Messages => "messages",
1708 Self::Parts => "parts",
1709 }
1710 }
1711}
1712#[derive(Debug)]
1713struct DatasetSet {
1714 sessions: OnceCell<Mutex<CachedDataset>>,
1718 messages: Mutex<CachedDataset>,
1719 parts: OnceCell<Mutex<CachedDataset>>,
1727}
1728#[derive(Debug)]
1729struct CachedDataset {
1730 dataset: Dataset,
1731 last_refresh: Instant,
1732 refresh_after: Duration,
1733}
1734impl CachedDataset {
1735 fn new(dataset: Dataset, refresh_after: Duration) -> Self {
1736 Self {
1737 dataset,
1738 last_refresh: Instant::now(),
1739 refresh_after,
1740 }
1741 }
1742 async fn latest(&mut self) -> Result<Dataset> {
1743 if self.last_refresh.elapsed() >= self.refresh_after {
1744 self.dataset.checkout_latest().await?;
1745 self.last_refresh = Instant::now();
1746 }
1747 Ok(self.dataset.clone())
1748 }
1749 fn replace(&mut self, dataset: Dataset) {
1750 self.dataset = dataset;
1751 self.last_refresh = Instant::now();
1752 }
1753}
1754
1755#[derive(Debug, Clone, Copy, Default)]
1760pub struct AppendStats {
1761 pub rows: u64,
1762 pub bytes_written: u64,
1763 pub files_written: u64,
1764 pub attempts: u32,
1765}
1766
1767#[derive(Default)]
1773struct WriteAccum {
1774 rows: std::sync::atomic::AtomicU64,
1775 bytes: std::sync::atomic::AtomicU64,
1776 files: std::sync::atomic::AtomicU64,
1777}
1778
1779impl WriteAccum {
1780 fn observe(&self, stats: &WriteStats) {
1781 use std::sync::atomic::Ordering::Relaxed;
1782 self.rows.fetch_max(stats.rows_written, Relaxed);
1783 self.bytes.fetch_max(stats.bytes_written, Relaxed);
1784 self.files.fetch_max(stats.files_written as u64, Relaxed);
1785 }
1786 fn rows(&self) -> u64 {
1787 self.rows.load(std::sync::atomic::Ordering::Relaxed)
1788 }
1789 fn bytes(&self) -> u64 {
1790 self.bytes.load(std::sync::atomic::Ordering::Relaxed)
1791 }
1792 fn files(&self) -> u64 {
1793 self.files.load(std::sync::atomic::Ordering::Relaxed)
1794 }
1795}
1796
1797fn append_write_params() -> WriteParams {
1802 let mut params = sessions::write_params_for_create();
1803 params.mode = WriteMode::Append;
1804 params.max_bytes_per_file = TARGET_FRAGMENT_BYTES as usize;
1805 params
1806}
1807
1808impl Handle {
1809 pub async fn open(location: &Url) -> Result<Self> {
1812 Self::open_with_options(location, HashMap::new(), RuntimeCaps::default()).await
1813 }
1814
1815 pub fn lance_cache_bytes(&self) -> u64 {
1818 self.session.size_bytes()
1819 }
1820
1821 pub async fn open_with_options(
1828 location: &Url,
1829 storage_options: HashMap<String, String>,
1830 caps: RuntimeCaps,
1831 ) -> Result<Self> {
1832 Self::open_with_options_cached(location, storage_options, caps, None).await
1833 }
1834
1835 pub async fn open_with_options_cached(
1839 location: &Url,
1840 mut storage_options: HashMap<String, String>,
1841 caps: RuntimeCaps,
1842 index_cache_dir: Option<PathBuf>,
1843 ) -> Result<Self> {
1844 if let Some(path) = config::local_path(location) {
1845 tokio::fs::create_dir_all(&path).await.with_context(|| {
1846 format!(
1847 "failed to create data dir {}; fix the storage destination ([storage].path in config) or re-run `pond init`",
1848 path.display()
1849 )
1850 })?;
1851 } else {
1852 apply_remote_storage_defaults(&mut storage_options);
1853 }
1854 let (index_cache_bytes, metadata_cache_bytes) = resolve_cache_caps(location, caps);
1860 let session = Arc::new(Session::new(
1861 index_cache_bytes,
1862 metadata_cache_bytes,
1863 Arc::new(ObjectStoreRegistry::default()),
1864 ));
1865 let root = location.as_str().trim_end_matches('/').to_string();
1871 let mut connect = ConnectBuilder::new("dir")
1872 .property("root", root)
1873 .session(session.clone());
1874 for (key, value) in &storage_options {
1878 connect = connect.property(format!("storage.{key}"), value.clone());
1879 }
1880 let nm: Arc<dyn LanceNamespace> = connect
1881 .connect()
1882 .await
1883 .context("failed to connect lance Directory namespace")?;
1884 let nm_ident = NamespaceIdent::root();
1885 let refresh_after = if config::is_local(location) {
1891 Duration::ZERO
1892 } else {
1893 Duration::from_secs(5)
1894 };
1895 let index_wrapper = index_store_wrapper(location, index_cache_dir.as_deref());
1896 let handle = Self {
1897 datasets: DatasetSet {
1898 sessions: OnceCell::new(),
1899 messages: Mutex::new(CachedDataset::new(
1900 open_or_create_via_ns(
1901 &nm,
1902 &nm_ident,
1903 sessions::MESSAGES,
1904 sessions::message_schema(),
1905 &session,
1906 &storage_options,
1907 index_wrapper.clone(),
1908 )
1909 .await?,
1910 refresh_after,
1911 )),
1912 parts: OnceCell::new(),
1913 },
1914 retry: RetryPolicy::default(),
1915 session,
1916 nm,
1917 nm_ident,
1918 storage_options,
1919 location: location.clone(),
1920 lazy_refresh_after: refresh_after,
1921 index_wrapper,
1922 };
1923 Ok(handle)
1924 }
1925
1926 pub fn location(&self) -> &Url {
1927 &self.location
1928 }
1929
1930 pub fn storage_options(&self) -> &HashMap<String, String> {
1934 &self.storage_options
1935 }
1936
1937 fn export_uri(&self, name: &str) -> String {
1943 format!(
1944 "{}/exports/{name}",
1945 self.location.as_str().trim_end_matches('/')
1946 )
1947 }
1948
1949 fn object_store_params(&self) -> ObjectStoreParams {
1953 ObjectStoreParams {
1954 storage_options_accessor: (!self.storage_options.is_empty()).then(|| {
1955 Arc::new(StorageOptionsAccessor::with_static_options(
1956 self.storage_options.clone(),
1957 ))
1958 }),
1959 ..Default::default()
1960 }
1961 }
1962
1963 pub(crate) async fn export_write(&self, name: &str, bytes: &[u8]) -> Result<()> {
1966 let uri = self.export_uri(name);
1967 let registry = Arc::new(ObjectStoreRegistry::default());
1968 let (store, path) =
1969 ObjectStore::from_uri_and_params(registry, &uri, &self.object_store_params())
1970 .await
1971 .with_context(|| format!("failed to open object store for {uri}"))?;
1972 store
1973 .put(&path, bytes)
1974 .await
1975 .with_context(|| format!("failed to write export {uri}"))?;
1976 Ok(())
1977 }
1978
1979 pub(crate) async fn export_read(&self, name: &str) -> Result<Vec<u8>> {
1982 let uri = self.export_uri(name);
1983 let registry = Arc::new(ObjectStoreRegistry::default());
1984 let (store, path) =
1985 ObjectStore::from_uri_and_params(registry, &uri, &self.object_store_params())
1986 .await
1987 .with_context(|| format!("failed to open object store for {uri}"))?;
1988 let bytes = store
1989 .read_one_all(&path)
1990 .await
1991 .with_context(|| format!("failed to read export {uri}"))?;
1992 Ok(bytes.to_vec())
1993 }
1994
1995 pub(crate) fn export_local_path(&self, name: &str) -> Option<std::path::PathBuf> {
2000 if self.location.scheme() != "file" {
2001 return None;
2002 }
2003 let dir = self.location.to_file_path().ok()?;
2004 Some(dir.join("exports").join(name))
2005 }
2006
2007 pub async fn row_counts(&self) -> Result<(usize, usize, usize)> {
2008 Ok((
2009 self.count_rows(Table::Sessions).await?,
2010 self.count_rows(Table::Messages).await?,
2011 self.count_rows(Table::Parts).await?,
2012 ))
2013 }
2014
2015 pub(crate) async fn merge_insert(
2019 &self,
2020 table: Table,
2021 batch: RecordBatch,
2022 row_count: usize,
2023 ) -> Result<u64> {
2024 self.merge_insert_stats(table, batch, row_count)
2025 .await
2026 .map(|stats| stats.num_inserted_rows + stats.num_updated_rows)
2027 }
2028
2029 pub(crate) async fn merge_insert_stats(
2034 &self,
2035 table: Table,
2036 batch: RecordBatch,
2037 row_count: usize,
2038 ) -> Result<MergeStats> {
2039 self.merge(
2040 table,
2041 batch,
2042 row_count,
2043 "merge_insert",
2044 WhenMatched::DoNothing,
2045 WhenNotMatched::InsertAll,
2046 )
2047 .await
2048 }
2049
2050 pub(crate) async fn merge_update(
2053 &self,
2054 table: Table,
2055 batch: RecordBatch,
2056 row_count: usize,
2057 ) -> Result<u64> {
2058 self.merge(
2059 table,
2060 batch,
2061 row_count,
2062 "merge_update",
2063 WhenMatched::UpdateAll,
2064 WhenNotMatched::DoNothing,
2065 )
2066 .await
2067 .map(|stats| stats.num_inserted_rows + stats.num_updated_rows)
2068 }
2069
2070 async fn write_committed<E, Fut, P>(&self, table: Table, execute: E) -> Result<P>
2079 where
2080 E: Fn(Arc<Dataset>) -> Fut,
2081 Fut: std::future::Future<Output = Result<(Dataset, P)>>,
2082 {
2083 self.write_committed_with(table, |_| true, execute).await
2084 }
2085
2086 async fn write_committed_with<E, Fut, P, R>(
2092 &self,
2093 table: Table,
2094 should_retry: R,
2095 execute: E,
2096 ) -> Result<P>
2097 where
2098 E: Fn(Arc<Dataset>) -> Fut,
2099 Fut: std::future::Future<Output = Result<(Dataset, P)>>,
2100 R: Fn(&anyhow::Error) -> bool,
2101 {
2102 self.retry_lance_filtered(table.label(), should_retry, || {
2103 let execute = &execute;
2104 async move {
2105 let mut cached = self.cached(table).await?.lock().await;
2106 let existing = cached.latest().await?;
2107 let (dataset, payload) = execute(Arc::new(existing)).await?;
2108 cached.replace(dataset);
2109 Ok(payload)
2110 }
2111 })
2112 .await
2113 }
2114
2115 async fn merge(
2121 &self,
2122 table: Table,
2123 batch: RecordBatch,
2124 row_count: usize,
2125 op: &'static str,
2126 when_matched: WhenMatched,
2127 when_not_matched: WhenNotMatched,
2128 ) -> Result<MergeStats> {
2129 if row_count == 0 {
2130 return Ok(MergeStats::default());
2131 }
2132 let started = Instant::now();
2133 let result = self
2134 .write_committed(table, |existing| {
2135 let batch = batch.clone();
2136 let when_matched = when_matched.clone();
2137 let when_not_matched = when_not_matched.clone();
2138 async move {
2139 let schema = batch.schema();
2140 let reader = RecordBatchIterator::new([Ok(batch)], schema);
2141 let mut builder = MergeInsertBuilder::try_new(existing, Vec::new())?;
2142 builder.when_matched(when_matched);
2143 builder.when_not_matched(when_not_matched);
2144 builder.source_dedupe_behavior(SourceDedupeBehavior::FirstSeen);
2147 builder.skip_auto_cleanup(true);
2151 let (dataset, stats) = builder
2152 .try_build()?
2153 .execute_reader(Box::new(reader))
2154 .await?;
2155 Ok((dataset.as_ref().clone(), stats))
2156 }
2157 })
2158 .await;
2159 let skipped = result
2160 .as_ref()
2161 .map(|s| s.num_skipped_duplicates)
2162 .unwrap_or(0);
2163 tracing::info!(
2164 target: "pond::perf",
2165 op,
2166 table = %table.label(),
2167 rows = row_count,
2168 elapsed_ms = started.elapsed().as_millis() as u64,
2169 skipped,
2170 "merge",
2171 );
2172 result
2173 }
2174
2175 pub(crate) async fn append_stream<F, Fut>(
2196 &self,
2197 table: Table,
2198 make_source: F,
2199 ) -> Result<AppendStats>
2200 where
2201 F: Fn() -> Fut,
2202 Fut: std::future::Future<Output = Result<SendableRecordBatchStream>>,
2203 {
2204 let cum = Arc::new(WriteAccum::default());
2205 let attempts = Arc::new(std::sync::atomic::AtomicU32::new(0));
2206 let started = Instant::now();
2207 self.write_committed(table, |existing| {
2208 let make_source = &make_source;
2209 let cum = cum.clone();
2210 let attempts = attempts.clone();
2211 async move {
2212 attempts.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2213 let stream = make_source().await?;
2214 let dataset = InsertBuilder::new(existing)
2215 .with_params(&append_write_params())
2216 .progress(move |stats| cum.observe(&stats))
2217 .execute_stream(stream)
2218 .await?;
2219 Ok((dataset, ()))
2220 }
2221 })
2222 .await?;
2223
2224 let attempts = attempts.load(std::sync::atomic::Ordering::Relaxed);
2225 let stats = AppendStats {
2226 rows: cum.rows(),
2227 bytes_written: cum.bytes(),
2228 files_written: cum.files(),
2229 attempts,
2230 };
2231 tracing::info!(
2232 target: "pond::perf",
2233 op = "append",
2234 table = %table.label(),
2235 rows = stats.rows,
2236 files = stats.files_written,
2237 attempts,
2238 elapsed_ms = started.elapsed().as_millis() as u64,
2239 "append",
2240 );
2241 Ok(stats)
2242 }
2243
2244 pub(crate) async fn append_batches(
2255 &self,
2256 table: Table,
2257 batches: Vec<RecordBatch>,
2258 ) -> Result<AppendStats> {
2259 let total_rows: u64 = batches.iter().map(|batch| batch.num_rows() as u64).sum();
2260 if total_rows == 0 {
2261 return Ok(AppendStats::default());
2262 }
2263 let cum = Arc::new(WriteAccum::default());
2264 let attempts = Arc::new(std::sync::atomic::AtomicU32::new(0));
2265 let started = Instant::now();
2266 self.write_committed_with(table, is_commit_conflict, |existing| {
2267 let cum = cum.clone();
2268 let attempts = attempts.clone();
2269 let batches = batches.clone();
2270 async move {
2271 attempts.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
2272 let dataset = InsertBuilder::new(existing)
2273 .with_params(&append_write_params())
2274 .progress(move |stats| cum.observe(&stats))
2275 .execute(batches)
2276 .await?;
2277 Ok((dataset, ()))
2278 }
2279 })
2280 .await?;
2281
2282 let attempts = attempts.load(std::sync::atomic::Ordering::Relaxed);
2283 let stats = AppendStats {
2284 rows: total_rows,
2285 bytes_written: cum.bytes(),
2286 files_written: cum.files(),
2287 attempts,
2288 };
2289 tracing::info!(
2290 target: "pond::perf",
2291 op = "append_batches",
2292 table = %table.label(),
2293 rows = stats.rows,
2294 files = stats.files_written,
2295 attempts,
2296 elapsed_ms = started.elapsed().as_millis() as u64,
2297 "append",
2298 );
2299 Ok(stats)
2300 }
2301
2302 pub async fn optimize_table(
2311 &self,
2312 table: Table,
2313 intents: &[IndexIntent],
2314 progress: Option<&OptimizeProgressFn>,
2315 policy: &MaintenancePolicy,
2316 ) -> TableOptimizeOutcome {
2317 let compaction = self
2318 .run_optimize_compact_phase(table, progress, policy)
2319 .await;
2320 let indices = self
2321 .run_optimize_indices_phase(table, intents, progress, policy.fold_thresholds())
2322 .await;
2323 TableOptimizeOutcome {
2324 table,
2325 indices,
2326 compaction,
2327 }
2328 }
2329
2330 pub async fn optimize_table_indices_only(
2335 &self,
2336 table: Table,
2337 intents: &[IndexIntent],
2338 progress: Option<&OptimizeProgressFn>,
2339 ) -> PhaseOutcome {
2340 self.run_optimize_indices_phase(
2344 table,
2345 intents,
2346 progress,
2347 FoldThresholds {
2348 scalar: 0,
2349 index: 0,
2350 },
2351 )
2352 .await
2353 }
2354
2355 async fn run_optimize_indices_phase(
2356 &self,
2357 table: Table,
2358 intents: &[IndexIntent],
2359 progress: Option<&OptimizeProgressFn>,
2360 folds: FoldThresholds,
2361 ) -> PhaseOutcome {
2362 if intents.is_empty() {
2363 return PhaseOutcome::Noop;
2364 }
2365 let result = self
2366 .retry_lance(table.label(), || async {
2367 let mut guard = self.cached(table).await?.lock().await;
2368 let mut dataset = guard.latest().await?;
2369 let did_work =
2370 optimize_table_indices(&mut dataset, intents, table, progress, folds).await?;
2371 guard.replace(dataset);
2372 Ok::<_, anyhow::Error>(did_work)
2373 })
2374 .await;
2375 match result {
2376 Ok(true) => PhaseOutcome::Ok,
2377 Ok(false) => PhaseOutcome::Noop,
2378 Err(error) if is_conflict_exhausted(&error) => PhaseOutcome::SkippedConflict,
2379 Err(error) => PhaseOutcome::Failed(error),
2380 }
2381 }
2382
2383 async fn run_optimize_compact_phase(
2384 &self,
2385 table: Table,
2386 progress: Option<&OptimizeProgressFn>,
2387 policy: &MaintenancePolicy,
2388 ) -> PhaseOutcome {
2389 let result = self
2390 .retry_lance(table.label(), || async {
2391 let mut guard = self.cached(table).await?.lock().await;
2392 let mut dataset = guard.latest().await?;
2393 optimize_table_compact(&mut dataset, table, progress, policy).await?;
2394 guard.replace(dataset);
2395 Ok::<_, anyhow::Error>(())
2396 })
2397 .await;
2398 match result {
2399 Ok(()) => PhaseOutcome::Ok,
2400 Err(error) if is_conflict_exhausted(&error) => PhaseOutcome::SkippedConflict,
2401 Err(error) => PhaseOutcome::Failed(error),
2402 }
2403 }
2404
2405 pub async fn rebuild_index(
2406 &self,
2407 table: Table,
2408 intent: &IndexIntent,
2409 progress: Option<&OptimizeProgressFn>,
2410 ) -> Result<()> {
2411 emit(
2412 progress,
2413 OptimizeEvent::PhaseStart {
2414 table,
2415 phase: OptimizePhase::IndexRebuild,
2416 detail: Some(intent.name.to_owned()),
2417 },
2418 );
2419 let started = Instant::now();
2420 let result = self
2421 .retry_lance(table.label(), || async {
2422 let mut guard = self.cached(table).await?.lock().await;
2423 let mut dataset = guard.latest().await?;
2424 rebuild_index(&mut dataset, intent, progress, table).await?;
2425 guard.replace(dataset);
2426 Ok(())
2427 })
2428 .await;
2429 emit(
2430 progress,
2431 OptimizeEvent::PhaseDone {
2432 table,
2433 phase: OptimizePhase::IndexRebuild,
2434 elapsed_ms: started.elapsed().as_millis() as u64,
2435 },
2436 );
2437 result
2438 }
2439
2440 pub async fn cleanup_table_versions(
2444 &self,
2445 table: Table,
2446 older_than: chrono::Duration,
2447 ) -> Result<()> {
2448 let mut guard = self.cached(table).await?.lock().await;
2449 let dataset = guard.latest().await?;
2450 dataset
2451 .cleanup_old_versions(older_than, Some(false), Some(false))
2452 .await
2453 .with_context(|| format!("cleanup_old_versions failed for {}", table.label()))?;
2454 Ok(())
2455 }
2456
2457 pub async fn index_status(
2458 &self,
2459 table: Table,
2460 intents: &[IndexIntent],
2461 indexable_only: bool,
2462 ) -> Result<Vec<IndexStatus>> {
2463 let dataset = self.dataset(table).await?;
2464 index_status(table, &dataset, intents, indexable_only).await
2465 }
2466
2467 pub(crate) async fn dataset(&self, table: Table) -> Result<Dataset> {
2468 let mut cached = self.cached(table).await?.lock().await;
2469 cached.latest().await
2470 }
2471 pub(crate) async fn scanner(
2476 &self,
2477 table: Table,
2478 predicate: Option<&Predicate>,
2479 ) -> Result<lance::dataset::scanner::Scanner> {
2480 let dataset = self.dataset(table).await?;
2481 scanner_with_prefilter(&dataset, predicate)
2482 }
2483 pub async fn scan(
2486 &self,
2487 table: Table,
2488 opts: ScanOpts<'_>,
2489 ) -> Result<lance::dataset::scanner::Scanner> {
2490 let mut scanner = self.scanner(table, opts.predicate).await?;
2491 if let Some(projection) = opts.projection {
2492 scanner.project(projection)?;
2493 }
2494 Ok(scanner)
2495 }
2496 pub(crate) async fn scan_batch(
2497 &self,
2498 table: Table,
2499 predicate: Option<&Predicate>,
2500 projection: &[&str],
2501 ) -> Result<RecordBatch> {
2502 let opts = ScanOpts {
2503 predicate,
2504 projection: (!projection.is_empty()).then_some(projection),
2505 };
2506 self.scan(table, opts)
2507 .await?
2508 .try_into_batch()
2509 .await
2510 .context("scan failed")
2511 }
2512 pub async fn count_rows(&self, table: Table) -> Result<usize> {
2513 self.dataset(table)
2514 .await?
2515 .count_rows(None)
2516 .await
2517 .map_err(Into::into)
2518 }
2519 pub async fn collect_ids(&self, table: Table) -> Result<std::collections::HashSet<String>> {
2525 let batch = self.scan_batch(table, None, &["id"]).await?;
2526 let ids = batch
2527 .column_by_name("id")
2528 .context("scan projection dropped the id column")?
2529 .as_any()
2530 .downcast_ref::<StringArray>()
2531 .context("id column is not Utf8")?;
2532 Ok(ids.iter().flatten().map(str::to_owned).collect())
2533 }
2534 #[cfg(test)]
2536 pub(crate) async fn messages_index_names(&self) -> Result<Vec<String>> {
2537 let dataset = self.dataset(Table::Messages).await?;
2538 let indices = dataset.load_indices().await?;
2539 Ok(indices.iter().map(|index| index.name.clone()).collect())
2540 }
2541
2542 pub(crate) async fn messages_has_index(&self, name: &str) -> Result<bool> {
2548 let dataset = self.dataset(Table::Messages).await?;
2549 let indices = dataset.load_indices().await?;
2550 Ok(indices.iter().any(|index| index.name == name))
2551 }
2552
2553 pub(crate) async fn messages_fast_search_ready(&self, name: &str) -> Result<bool> {
2560 let dataset = self.dataset(Table::Messages).await?;
2561 if !dataset
2562 .load_indices()
2563 .await?
2564 .iter()
2565 .any(|index| index.name == name)
2566 {
2567 return Ok(false);
2568 }
2569 let unindexed = dataset
2570 .unindexed_fragments(name)
2571 .await
2572 .with_context(|| format!("unindexed_fragments failed for {name}"))?;
2573 Ok(unindexed.is_empty())
2574 }
2575
2576 pub(crate) async fn prune_index_cache(&self, cache_dir: &std::path::Path) {
2581 if config::is_local(&self.location) {
2582 return;
2583 }
2584 let root = cache_dir.join(store_key(&self.location)).join("indices");
2585 if !root.exists() {
2586 return;
2587 }
2588 let mut keep = std::collections::HashSet::new();
2589 for table in [Table::Sessions, Table::Messages, Table::Parts] {
2590 let Ok(dataset) = self.dataset(table).await else {
2591 return;
2592 };
2593 let Ok(indices) = dataset.load_indices().await else {
2594 return;
2595 };
2596 keep.extend(indices.iter().map(|index| index.uuid.to_string()));
2597 }
2598 prune_stale_uuid_dirs(&root, &keep);
2599 }
2600
2601 pub(crate) async fn unindexed_row_count(
2604 &self,
2605 table: Table,
2606 index_name: &str,
2607 ) -> Result<usize> {
2608 let dataset = self.dataset(table).await?;
2609 let fragments = dataset
2610 .unindexed_fragments(index_name)
2611 .await
2612 .with_context(|| format!("unindexed_fragments failed for {}", table.label()))?;
2613 Ok(fragments
2614 .iter()
2615 .map(|fragment| fragment.num_rows().unwrap_or(0))
2616 .sum())
2617 }
2618
2619 pub(crate) async fn find_index_owner(&self, name: &str) -> Result<Option<Table>> {
2626 let list = |table: Table| async move {
2627 let dataset = self.dataset(table).await?;
2628 let names: Vec<String> = dataset
2629 .load_indices()
2630 .await
2631 .with_context(|| format!("load_indices failed for {}", table.label()))?
2632 .iter()
2633 .map(|index| index.name.clone())
2634 .collect();
2635 Ok::<_, anyhow::Error>(names)
2636 };
2637 let (sessions, messages, parts) = tokio::try_join!(
2638 list(Table::Sessions),
2639 list(Table::Messages),
2640 list(Table::Parts),
2641 )?;
2642 for (table, names) in [
2643 (Table::Sessions, sessions),
2644 (Table::Messages, messages),
2645 (Table::Parts, parts),
2646 ] {
2647 if names.iter().any(|n| n == name) {
2648 return Ok(Some(table));
2649 }
2650 }
2651 Ok(None)
2652 }
2653
2654 pub(crate) async fn drop_index(&self, table: Table, name: &str) -> Result<()> {
2660 let mut guard = self.cached(table).await?.lock().await;
2661 let mut dataset = guard.latest().await?;
2662 dataset
2663 .drop_index(name)
2664 .await
2665 .with_context(|| format!("drop_index({name}) failed for {}", table.label()))?;
2666 guard.replace(dataset);
2667 Ok(())
2668 }
2669
2670 async fn table_location(&self, table_name: &str) -> Result<String> {
2673 let request = DescribeTableRequest {
2674 id: Some(self.nm_ident.as_table_id(table_name)),
2675 ..Default::default()
2676 };
2677 let response = self
2678 .nm
2679 .describe_table(request)
2680 .await
2681 .with_context(|| format!("failed to describe table {table_name}"))?;
2682 response
2683 .location
2684 .with_context(|| format!("namespace returned no location for table {table_name}"))
2685 }
2686
2687 pub async fn initialized(&self) -> Result<bool> {
2693 let request = DescribeTableRequest {
2694 id: Some(self.nm_ident.as_table_id(sessions::PARTS)),
2695 ..Default::default()
2696 };
2697 match self.nm.describe_table(request).await {
2698 Ok(_) => Ok(true),
2699 Err(error) if is_namespace_error_code(&error, ErrorCode::TableNotFound) => Ok(false),
2700 Err(error) => {
2701 Err(anyhow::Error::from(error)).context("failed to probe table existence")
2702 }
2703 }
2704 }
2705
2706 pub async fn table_sizes(&self) -> Result<TableSizes> {
2710 let registry = Arc::new(ObjectStoreRegistry::default());
2711 let params = self.object_store_params();
2712
2713 let sessions = self
2714 .listed_size(
2715 ®istry,
2716 ¶ms,
2717 &self.table_location(sessions::SESSIONS).await?,
2718 )
2719 .await?;
2720 let messages = self
2721 .listed_size(
2722 ®istry,
2723 ¶ms,
2724 &self.table_location(sessions::MESSAGES).await?,
2725 )
2726 .await?;
2727 let parts = self
2728 .listed_size(
2729 ®istry,
2730 ¶ms,
2731 &self.table_location(sessions::PARTS).await?,
2732 )
2733 .await?;
2734 let root_total = self
2737 .listed_size(®istry, ¶ms, self.location.as_str())
2738 .await?;
2739 let other = root_total.saturating_sub(sessions + messages + parts);
2740 let sessions_data = self
2741 .data_liveness(®istry, ¶ms, Table::Sessions, sessions::SESSIONS)
2742 .await?;
2743 let messages_data = self
2744 .data_liveness(®istry, ¶ms, Table::Messages, sessions::MESSAGES)
2745 .await?;
2746 let parts_data = self
2747 .data_liveness(®istry, ¶ms, Table::Parts, sessions::PARTS)
2748 .await?;
2749 Ok(TableSizes {
2750 sessions,
2751 messages,
2752 parts,
2753 other,
2754 sessions_data,
2755 messages_data,
2756 parts_data,
2757 })
2758 }
2759
2760 async fn data_liveness(
2761 &self,
2762 registry: &Arc<ObjectStoreRegistry>,
2763 params: &ObjectStoreParams,
2764 table: Table,
2765 table_name: &str,
2766 ) -> Result<DataLiveness> {
2767 let location = self.table_location(table_name).await?;
2768 let data_dir = format!("{}/data", location.trim_end_matches('/'));
2769 let on_disk = self.listed_size(registry, params, &data_dir).await?;
2770 let dataset = self.dataset(table).await?;
2771 let live = dataset
2772 .get_fragments()
2773 .iter()
2774 .try_fold(0u64, |total, fragment| {
2775 Some(total + fragment_bytes(fragment.metadata())?)
2776 });
2777 Ok(DataLiveness { on_disk, live })
2778 }
2779
2780 async fn listed_size(
2782 &self,
2783 registry: &Arc<ObjectStoreRegistry>,
2784 params: &ObjectStoreParams,
2785 uri: &str,
2786 ) -> Result<u64> {
2787 let (store, base) = ObjectStore::from_uri_and_params(registry.clone(), uri, params)
2788 .await
2789 .with_context(|| format!("failed to open object store for {uri}"))?;
2790 let mut listing = store.list(Some(base));
2791 let mut total = 0u64;
2792 while let Some(meta) = listing.next().await {
2793 let meta = meta.with_context(|| format!("listing {uri} failed"))?;
2794 total += meta.size;
2795 }
2796 Ok(total)
2797 }
2798 async fn cached(&self, table: Table) -> Result<&Mutex<CachedDataset>> {
2799 match table {
2800 Table::Sessions => self.sessions_cached().await,
2801 Table::Messages => Ok(&self.datasets.messages),
2802 Table::Parts => self.parts_cached().await,
2803 }
2804 }
2805
2806 async fn sessions_cached(&self) -> Result<&Mutex<CachedDataset>> {
2811 self.lazy_cached(
2812 &self.datasets.sessions,
2813 sessions::SESSIONS,
2814 sessions::session_schema,
2815 )
2816 .await
2817 }
2818
2819 async fn parts_cached(&self) -> Result<&Mutex<CachedDataset>> {
2822 self.lazy_cached(&self.datasets.parts, sessions::PARTS, sessions::part_schema)
2823 .await
2824 }
2825
2826 async fn lazy_cached<'a>(
2830 &self,
2831 cell: &'a OnceCell<Mutex<CachedDataset>>,
2832 table_name: &str,
2833 schema: fn() -> lance::deps::arrow_schema::SchemaRef,
2834 ) -> Result<&'a Mutex<CachedDataset>> {
2835 cell.get_or_try_init(|| async {
2836 let dataset = open_or_create_via_ns(
2837 &self.nm,
2838 &self.nm_ident,
2839 table_name,
2840 schema(),
2841 &self.session,
2842 &self.storage_options,
2843 self.index_wrapper.clone(),
2844 )
2845 .await?;
2846 Ok::<_, anyhow::Error>(Mutex::new(CachedDataset::new(
2847 dataset,
2848 self.lazy_refresh_after,
2849 )))
2850 })
2851 .await
2852 }
2853 async fn retry_lance<T, Fut, Op>(&self, label: &str, operation: Op) -> Result<T>
2854 where
2855 Fut: std::future::Future<Output = Result<T>>,
2856 Op: FnMut() -> Fut,
2857 {
2858 self.retry_lance_filtered(label, |_| true, operation).await
2860 }
2861
2862 async fn retry_lance_filtered<T, Fut, Op, R>(
2872 &self,
2873 label: &str,
2874 should_retry: R,
2875 mut operation: Op,
2876 ) -> Result<T>
2877 where
2878 Fut: std::future::Future<Output = Result<T>>,
2879 Op: FnMut() -> Fut,
2880 R: Fn(&anyhow::Error) -> bool,
2881 {
2882 let mut attempt = 0u8;
2883 loop {
2884 attempt = attempt.saturating_add(1);
2885 match operation().await {
2886 Ok(value) => return Ok(value),
2887 Err(error) if attempt < self.retry.attempts && should_retry(&error) => {
2888 let backoff = self.backoff(attempt);
2889 let error_chain = format!("{error:#}");
2892 tracing::warn!(
2893 label,
2894 attempt,
2895 ?backoff,
2896 error = %error_chain,
2897 "retrying Lance operation"
2898 );
2899 tokio::time::sleep(backoff).await;
2900 }
2901 Err(error) => {
2902 let error_chain = format!("{error:#}");
2903 tracing::warn!(
2904 label,
2905 attempt,
2906 error = %error_chain,
2907 "Lance operation exhausted retries"
2908 );
2909 if is_commit_conflict(&error) {
2916 return Err(error.context(ConflictExhausted { attempts: attempt }));
2917 }
2918 return Err(error);
2919 }
2920 }
2921 }
2922 }
2923 fn backoff(&self, attempt: u8) -> Duration {
2924 let shift = u32::from(attempt.saturating_sub(1));
2925 let multiplier = 1u32.checked_shl(shift).unwrap_or(u32::MAX);
2926 let base = self.retry.initial_backoff.saturating_mul(multiplier);
2927 let factor = (1.0 + self.retry.jitter * (fastrand::f64() * 2.0 - 1.0)).max(0.0);
2930 base.mul_f64(factor).min(self.retry.max_backoff)
2931 }
2932}
2933async fn optimize_table_compact(
2954 dataset: &mut Dataset,
2955 table: Table,
2956 progress: Option<&OptimizeProgressFn>,
2957 policy: &MaintenancePolicy,
2958) -> Result<()> {
2959 let stats: Vec<FragmentStat> = dataset
2960 .get_fragments()
2961 .iter()
2962 .map(|fragment| fragment_stat(fragment.metadata()))
2963 .collect();
2964 let compaction = CompactionOptions {
2965 target_rows_per_fragment: derived_target_rows(&stats),
2966 max_bytes_per_file: Some(TARGET_FRAGMENT_BYTES as usize),
2967 defer_index_remap: false,
2968 compaction_mode: Some(CompactionMode::TryBinaryCopy),
2973 ..CompactionOptions::default()
2974 };
2975
2976 let mut plan = plan_compaction(dataset, &compaction).await?;
2977 if policy.compaction_fragment_cap > 0 {
2978 plan.tasks.retain(|task| {
2979 let task_stats: Vec<FragmentStat> = task.fragments.iter().map(fragment_stat).collect();
2980 let keep = keep_task(
2981 &task_stats,
2982 policy.compaction_fragment_cap,
2983 compaction.materialize_deletions_threshold,
2984 );
2985 if !keep {
2986 tracing::debug!(
2987 target: "pond::perf",
2988 table = table.as_str(),
2989 fragments = task_stats.len(),
2990 "compaction task vetoed: merge dominated by one large fragment",
2991 );
2992 }
2993 keep
2994 });
2995 }
2996 if plan.tasks.is_empty() {
2997 tracing::debug!(
2998 target: "pond::perf",
2999 table = table.as_str(),
3000 "compaction skipped: no task to run",
3001 );
3002 } else {
3003 emit(
3004 progress,
3005 OptimizeEvent::PhaseStart {
3006 table,
3007 phase: OptimizePhase::Compact,
3008 detail: None,
3009 },
3010 );
3011 let started = Instant::now();
3012 let mut completed = Vec::with_capacity(plan.tasks.len());
3013 for task in plan.compaction_tasks() {
3014 completed.push(task.execute(dataset).await?);
3015 }
3016 commit_compaction(
3017 dataset,
3018 completed,
3019 Arc::new(DatasetIndexRemapperOptions::default()),
3020 &compaction,
3021 )
3022 .await?;
3023 emit(
3024 progress,
3025 OptimizeEvent::PhaseDone {
3026 table,
3027 phase: OptimizePhase::Compact,
3028 elapsed_ms: started.elapsed().as_millis() as u64,
3029 },
3030 );
3031 }
3032
3033 if cleanup_due(dataset.version_id(), policy.cleanup_interval) {
3044 emit(
3045 progress,
3046 OptimizeEvent::PhaseStart {
3047 table,
3048 phase: OptimizePhase::Cleanup,
3049 detail: None,
3050 },
3051 );
3052 let started = Instant::now();
3053 dataset
3059 .cleanup_old_versions(policy.cleanup_older_than, Some(false), Some(false))
3060 .await
3061 .context("cleanup_old_versions failed during index optimize")?;
3062 emit(
3063 progress,
3064 OptimizeEvent::PhaseDone {
3065 table,
3066 phase: OptimizePhase::Cleanup,
3067 elapsed_ms: started.elapsed().as_millis() as u64,
3068 },
3069 );
3070 }
3071
3072 Ok(())
3073}
3074
3075fn cleanup_due(version: u64, interval: u64) -> bool {
3081 interval <= 1 || version.is_multiple_of(interval)
3082}
3083
3084async fn optimize_table_indices(
3089 dataset: &mut Dataset,
3090 intents: &[IndexIntent],
3091 table: Table,
3092 progress: Option<&OptimizeProgressFn>,
3093 folds: FoldThresholds,
3094) -> Result<bool> {
3095 let existing = dataset.load_indices().await?;
3096 let existing_names: std::collections::HashSet<String> =
3097 existing.iter().map(|index| index.name.clone()).collect();
3098
3099 let mut append_indices: Vec<String> = Vec::new();
3100 let mut did_work = false;
3101
3102 for intent in intents {
3103 let exists = existing_names.contains(intent.name);
3104
3105 if !exists {
3106 if !intent.trigger.should_create(dataset).await? {
3107 continue;
3108 }
3109 let params = intent.params.build(dataset).await?;
3110 let index_type = intent.params.index_type();
3111 tracing::info!(
3112 index = intent.name,
3113 column = intent.column,
3114 "creating Lance index (trigger fired)",
3115 );
3116 emit(
3117 progress,
3118 OptimizeEvent::PhaseStart {
3119 table,
3120 phase: OptimizePhase::IndexCreate,
3121 detail: Some(intent.name.to_owned()),
3122 },
3123 );
3124 let started = Instant::now();
3125 dataset
3126 .create_index_builder(&[intent.column], index_type, params.as_ref())
3127 .name(intent.name.to_owned())
3128 .replace(false)
3129 .progress(lance_progress(progress, table, intent.name))
3130 .await
3131 .with_context(|| format!("failed to create index {}", intent.name))?;
3132 emit(
3133 progress,
3134 OptimizeEvent::PhaseDone {
3135 table,
3136 phase: OptimizePhase::IndexCreate,
3137 elapsed_ms: started.elapsed().as_millis() as u64,
3138 },
3139 );
3140 did_work = true;
3141 continue;
3142 }
3143
3144 let unindexed = dataset.unindexed_fragments(intent.name).await?;
3150 if unindexed.is_empty() {
3151 continue;
3152 }
3153 let tail_rows: usize = unindexed
3154 .iter()
3155 .map(|fragment| fragment.num_rows().unwrap_or(0))
3156 .sum();
3157 let fold_threshold = match intent.params {
3166 IndexParamsKind::Scalar(_) => folds.scalar,
3167 IndexParamsKind::InvertedFtsWord | IndexParamsKind::IvfSqCosine { .. } => folds.index,
3168 };
3169 if fold_threshold > 0 && tail_rows < fold_threshold {
3170 tracing::debug!(
3171 target: "pond::perf",
3172 index = intent.name,
3173 tail_rows,
3174 threshold = fold_threshold,
3175 "deferring index fold (unindexed tail below threshold)",
3176 );
3177 continue;
3178 }
3179 if matches!(intent.params, IndexParamsKind::InvertedFtsWord)
3187 && !column_has_values(dataset, intent.column, &unindexed).await?
3188 {
3189 tracing::debug!(
3190 target: "pond::perf",
3191 index = intent.name,
3192 tail_rows,
3193 "skipping FTS fold (tail has no indexable values)",
3194 );
3195 continue;
3196 }
3197 append_indices.push(intent.name.to_owned());
3203 }
3204
3205 if !append_indices.is_empty() {
3206 let segment_count = |name: &str| {
3212 existing
3213 .iter()
3214 .filter(|index| index.name.as_str() == name)
3215 .count()
3216 };
3217 let (consolidate, to_append): (Vec<String>, Vec<String>) = append_indices
3218 .iter()
3219 .cloned()
3220 .partition(|name| segment_count(name) >= DELTA_MERGE_THRESHOLD);
3221 let mut fts_rebuilds: Vec<&IndexIntent> = Vec::new();
3230 let mut to_merge: Vec<String> = Vec::new();
3231 for name in consolidate {
3232 let fts_intent = intents.iter().find(|intent| {
3233 intent.name == name && matches!(intent.params, IndexParamsKind::InvertedFtsWord)
3234 });
3235 match fts_intent {
3236 Some(intent) => fts_rebuilds.push(intent),
3237 None => to_merge.push(name),
3238 }
3239 }
3240
3241 emit(
3242 progress,
3243 OptimizeEvent::PhaseStart {
3244 table,
3245 phase: OptimizePhase::IndexAppend,
3246 detail: Some(append_indices.join(", ")),
3247 },
3248 );
3249 let started = Instant::now();
3250 if !to_append.is_empty() {
3251 dataset
3252 .optimize_indices(&OptimizeOptions::append().index_names(to_append))
3253 .await
3254 .context("optimize_indices(append) failed during index optimize")?;
3255 }
3256 if !to_merge.is_empty() {
3257 dataset
3258 .optimize_indices(
3259 &OptimizeOptions::merge(DELTA_MERGE_THRESHOLD).index_names(to_merge),
3260 )
3261 .await
3262 .context("optimize_indices(merge) failed during index optimize")?;
3263 }
3264 emit(
3265 progress,
3266 OptimizeEvent::PhaseDone {
3267 table,
3268 phase: OptimizePhase::IndexAppend,
3269 elapsed_ms: started.elapsed().as_millis() as u64,
3270 },
3271 );
3272 for intent in &fts_rebuilds {
3273 emit(
3274 progress,
3275 OptimizeEvent::PhaseStart {
3276 table,
3277 phase: OptimizePhase::IndexRebuild,
3278 detail: Some(intent.name.to_owned()),
3279 },
3280 );
3281 let rebuild_started = Instant::now();
3282 rebuild_index(dataset, intent, progress, table).await?;
3283 emit(
3284 progress,
3285 OptimizeEvent::PhaseDone {
3286 table,
3287 phase: OptimizePhase::IndexRebuild,
3288 elapsed_ms: rebuild_started.elapsed().as_millis() as u64,
3289 },
3290 );
3291 }
3292 tracing::debug!(
3293 target: "pond::perf",
3294 indices = ?append_indices,
3295 rebuilt = ?fts_rebuilds,
3296 "folded trailing fragments into indices",
3297 );
3298 did_work = true;
3299 }
3300
3301 Ok(did_work)
3302}
3303
3304fn non_null_scanner(
3307 dataset: &Dataset,
3308 column: &'static str,
3309 fragments: &[lance::table::format::Fragment],
3310) -> Result<lance::dataset::scanner::Scanner> {
3311 let mut scanner = dataset.scan();
3312 scanner.with_fragments(fragments.to_vec());
3313 scanner.filter(&Predicate::IsNotNull(column).to_lance())?;
3314 Ok(scanner)
3315}
3316
3317async fn column_has_values(
3322 dataset: &Dataset,
3323 column: &'static str,
3324 fragments: &[lance::table::format::Fragment],
3325) -> Result<bool> {
3326 let mut scanner = non_null_scanner(dataset, column, fragments)?;
3327 scanner.project(&[column])?;
3328 scanner.limit(Some(1), None)?;
3329 let batch = scanner
3330 .try_into_batch()
3331 .await
3332 .with_context(|| format!("non-null probe on {column} failed"))?;
3333 Ok(batch.num_rows() > 0)
3334}
3335
3336async fn column_value_count(
3339 dataset: &Dataset,
3340 column: &'static str,
3341 fragments: &[lance::table::format::Fragment],
3342) -> Result<usize> {
3343 let count = non_null_scanner(dataset, column, fragments)?
3344 .count_rows()
3345 .await
3346 .with_context(|| format!("non-null count on {column} failed"))?;
3347 Ok(count as usize)
3348}
3349
3350async fn rebuild_index(
3351 dataset: &mut Dataset,
3352 intent: &IndexIntent,
3353 progress: Option<&OptimizeProgressFn>,
3354 table: Table,
3355) -> Result<()> {
3356 if !intent.trigger.should_create(dataset).await? {
3357 return Ok(());
3358 }
3359 let params = intent.params.build(dataset).await?;
3360 dataset
3361 .create_index_builder(
3362 &[intent.column],
3363 intent.params.index_type(),
3364 params.as_ref(),
3365 )
3366 .name(intent.name.to_owned())
3367 .replace(true)
3368 .progress(lance_progress(progress, table, intent.name))
3369 .await
3370 .with_context(|| format!("failed to rebuild index {}", intent.name))?;
3371 Ok(())
3372}
3373
3374async fn index_status(
3375 table: Table,
3376 dataset: &Dataset,
3377 intents: &[IndexIntent],
3378 indexable_only: bool,
3379) -> Result<Vec<IndexStatus>> {
3380 let existing = dataset.load_indices().await?;
3381 let existing_names: std::collections::HashSet<String> =
3382 existing.iter().map(|index| index.name.clone()).collect();
3383 let total_fragments = dataset.get_fragments().len();
3384 let total_rows = dataset.count_rows(None).await?;
3385 let mut statuses = Vec::with_capacity(intents.len());
3386 for intent in intents {
3387 let exists = existing_names.contains(intent.name);
3388 if !exists {
3389 statuses.push(IndexStatus {
3390 table,
3391 intent_name: intent.name.to_owned(),
3392 fragments_covered: 0,
3393 unindexed_fragments: total_fragments,
3394 unindexed_rows: total_rows,
3395 exists,
3396 });
3397 continue;
3398 }
3399 let unindexed = dataset
3400 .unindexed_fragments(intent.name)
3401 .await
3402 .with_context(|| format!("unindexed_fragments failed for {}", table.label()))?;
3403 let unindexed_fragments = unindexed.len();
3404 let mut unindexed_rows: usize = unindexed
3405 .iter()
3406 .map(|fragment| fragment.num_rows().unwrap_or(0))
3407 .sum();
3408 if indexable_only
3416 && unindexed_rows > 0
3417 && matches!(
3418 intent.params,
3419 IndexParamsKind::InvertedFtsWord | IndexParamsKind::IvfSqCosine { .. }
3420 )
3421 {
3422 unindexed_rows = column_value_count(dataset, intent.column, &unindexed).await?;
3423 }
3424 statuses.push(IndexStatus {
3425 table,
3426 intent_name: intent.name.to_owned(),
3427 fragments_covered: total_fragments.saturating_sub(unindexed_fragments),
3428 unindexed_fragments,
3429 unindexed_rows,
3430 exists,
3431 });
3432 }
3433 Ok(statuses)
3434}
3435
3436pub mod io_trace {
3454 use lance_io::utils::tracking_store::{IOTracker, IoStats};
3455 use std::sync::{Arc, OnceLock};
3456
3457 static TRACKER: OnceLock<IOTracker> = OnceLock::new();
3458
3459 pub fn enable() {
3462 let _ = TRACKER.set(IOTracker::default());
3463 }
3464
3465 pub(super) fn wrapper() -> Option<Arc<IOTracker>> {
3467 TRACKER.get().map(|tracker| Arc::new(tracker.clone()))
3468 }
3469
3470 pub fn take() -> Option<IoStats> {
3472 TRACKER.get().map(IOTracker::incremental_stats)
3473 }
3474}
3475
3476pub mod index_cache {
3484 use object_store::local::LocalFileSystem;
3485 use object_store::path::Path as ObjPath;
3486 use object_store::{
3487 CopyOptions, GetOptions, GetResult, ListResult, MultipartUpload, ObjectMeta, ObjectStore,
3488 ObjectStoreExt, PutMultipartOptions, PutOptions, PutPayload, PutResult, Result as OsResult,
3489 };
3490 use std::collections::HashMap;
3491 use std::ops::Range;
3492 use std::path::PathBuf;
3493 use std::sync::{Arc, Mutex};
3494
3495 use bytes::Bytes;
3496 use futures::stream::BoxStream;
3497 use lance_io::object_store::WrappingObjectStore;
3498
3499 fn is_index_path(location: &ObjPath) -> bool {
3500 AsRef::<str>::as_ref(location).contains("_indices/")
3501 }
3502
3503 fn local_opts(options: &GetOptions) -> GetOptions {
3506 GetOptions {
3507 range: options.range.clone(),
3508 head: options.head,
3509 ..Default::default()
3510 }
3511 }
3512
3513 #[derive(Debug)]
3516 pub struct IndexDiskCache {
3517 local: Arc<LocalFileSystem>,
3518 inflight: Arc<Mutex<HashMap<ObjPath, Arc<tokio::sync::Mutex<()>>>>>,
3519 }
3520
3521 impl IndexDiskCache {
3522 pub fn new(root: PathBuf) -> std::io::Result<Self> {
3524 std::fs::create_dir_all(&root)?;
3525 Ok(Self {
3526 local: Arc::new(LocalFileSystem::new_with_prefix(&root)?),
3527 inflight: Arc::new(Mutex::new(HashMap::new())),
3528 })
3529 }
3530 }
3531
3532 impl WrappingObjectStore for IndexDiskCache {
3533 fn wrap(&self, _store_prefix: &str, inner: Arc<dyn ObjectStore>) -> Arc<dyn ObjectStore> {
3534 Arc::new(CachingStore {
3535 inner,
3536 local: self.local.clone(),
3537 inflight: self.inflight.clone(),
3538 })
3539 }
3540 }
3541
3542 #[derive(Debug)]
3543 struct CachingStore {
3544 inner: Arc<dyn ObjectStore>,
3545 local: Arc<LocalFileSystem>,
3546 inflight: Arc<Mutex<HashMap<ObjPath, Arc<tokio::sync::Mutex<()>>>>>,
3547 }
3548
3549 impl std::fmt::Display for CachingStore {
3550 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
3551 write!(f, "CachingStore({})", self.inner)
3552 }
3553 }
3554
3555 impl CachingStore {
3556 fn flight_lock(&self, location: &ObjPath) -> Arc<tokio::sync::Mutex<()>> {
3557 self.inflight
3558 .lock()
3559 .unwrap_or_else(|poison| poison.into_inner())
3560 .entry(location.clone())
3561 .or_default()
3562 .clone()
3563 }
3564
3565 async fn populate_and_serve(
3571 &self,
3572 location: &ObjPath,
3573 options: GetOptions,
3574 ) -> OsResult<GetResult> {
3575 let lock = self.flight_lock(location);
3576 let _guard = lock.lock().await;
3577 let result = self.fetch_under_flight(location, options).await;
3578 self.inflight
3583 .lock()
3584 .unwrap_or_else(|p| p.into_inner())
3585 .remove(location);
3586 result
3587 }
3588
3589 async fn fetch_under_flight(
3590 &self,
3591 location: &ObjPath,
3592 options: GetOptions,
3593 ) -> OsResult<GetResult> {
3594 if let Ok(result) = self.local.get_opts(location, local_opts(&options)).await {
3595 return Ok(result);
3596 }
3597 let bytes = self.inner.get(location).await?.bytes().await?;
3598 if self
3599 .local
3600 .put(location, PutPayload::from_bytes(bytes))
3601 .await
3602 .is_ok()
3603 && let Ok(result) = self.local.get_opts(location, local_opts(&options)).await
3604 {
3605 return Ok(result);
3606 }
3607 self.inner.get_opts(location, options).await
3609 }
3610 }
3611
3612 #[async_trait::async_trait]
3613 impl ObjectStore for CachingStore {
3614 async fn get_opts(&self, location: &ObjPath, options: GetOptions) -> OsResult<GetResult> {
3615 if !is_index_path(location) {
3616 return self.inner.get_opts(location, options).await;
3617 }
3618 match self.local.get_opts(location, local_opts(&options)).await {
3619 Ok(result) => Ok(result),
3620 Err(object_store::Error::NotFound { .. }) => {
3621 self.populate_and_serve(location, options).await
3622 }
3623 Err(_) => self.inner.get_opts(location, options).await,
3624 }
3625 }
3626
3627 async fn put_opts(
3628 &self,
3629 location: &ObjPath,
3630 payload: PutPayload,
3631 opts: PutOptions,
3632 ) -> OsResult<PutResult> {
3633 self.inner.put_opts(location, payload, opts).await
3634 }
3635
3636 async fn put_multipart_opts(
3637 &self,
3638 location: &ObjPath,
3639 opts: PutMultipartOptions,
3640 ) -> OsResult<Box<dyn MultipartUpload>> {
3641 self.inner.put_multipart_opts(location, opts).await
3642 }
3643
3644 async fn get_ranges(
3645 &self,
3646 location: &ObjPath,
3647 ranges: &[Range<u64>],
3648 ) -> OsResult<Vec<Bytes>> {
3649 if is_index_path(location) {
3650 let mut out = Vec::with_capacity(ranges.len());
3652 for range in ranges {
3653 let opts = GetOptions {
3654 range: Some(range.clone().into()),
3655 ..Default::default()
3656 };
3657 out.push(self.get_opts(location, opts).await?.bytes().await?);
3658 }
3659 return Ok(out);
3660 }
3661 self.inner.get_ranges(location, ranges).await
3662 }
3663
3664 fn delete_stream(
3665 &self,
3666 locations: BoxStream<'static, OsResult<ObjPath>>,
3667 ) -> BoxStream<'static, OsResult<ObjPath>> {
3668 self.inner.delete_stream(locations)
3669 }
3670
3671 fn list(&self, prefix: Option<&ObjPath>) -> BoxStream<'static, OsResult<ObjectMeta>> {
3672 self.inner.list(prefix)
3673 }
3674
3675 fn list_with_offset(
3676 &self,
3677 prefix: Option<&ObjPath>,
3678 offset: &ObjPath,
3679 ) -> BoxStream<'static, OsResult<ObjectMeta>> {
3680 self.inner.list_with_offset(prefix, offset)
3681 }
3682
3683 async fn list_with_delimiter(&self, prefix: Option<&ObjPath>) -> OsResult<ListResult> {
3684 self.inner.list_with_delimiter(prefix).await
3685 }
3686
3687 async fn copy_opts(&self, from: &ObjPath, to: &ObjPath, opts: CopyOptions) -> OsResult<()> {
3688 self.inner.copy_opts(from, to, opts).await
3689 }
3690 }
3691
3692 #[cfg(test)]
3693 mod tests {
3694 #![allow(clippy::unwrap_used)]
3695 use super::*;
3696 use object_store::memory::InMemory;
3697
3698 async fn read(store: &Arc<dyn ObjectStore>, path: &ObjPath) -> Option<Vec<u8>> {
3699 store
3700 .get(path)
3701 .await
3702 .ok()?
3703 .bytes()
3704 .await
3705 .ok()
3706 .map(|b| b.to_vec())
3707 }
3708
3709 #[tokio::test]
3710 async fn caches_index_files_and_passes_data_through() {
3711 let temp = tempfile::tempdir().unwrap();
3712 let inner: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
3713 let index_path = ObjPath::from("d/messages.lance/_indices/uuid1/index.idx");
3714 let data_path = ObjPath::from("d/messages.lance/data/x.lance");
3715 inner
3716 .put(&index_path, PutPayload::from_static(b"INDEX"))
3717 .await
3718 .unwrap();
3719 inner
3720 .put(&data_path, PutPayload::from_static(b"DATA"))
3721 .await
3722 .unwrap();
3723
3724 let cache = IndexDiskCache::new(temp.path().join("indices")).unwrap();
3725 let store = cache.wrap("test", inner.clone());
3726
3727 assert_eq!(
3728 read(&store, &index_path).await.as_deref(),
3729 Some(&b"INDEX"[..])
3730 );
3731 assert_eq!(
3732 read(&store, &data_path).await.as_deref(),
3733 Some(&b"DATA"[..])
3734 );
3735
3736 inner.delete(&index_path).await.unwrap();
3739 inner.delete(&data_path).await.unwrap();
3740 assert_eq!(
3741 read(&store, &index_path).await.as_deref(),
3742 Some(&b"INDEX"[..])
3743 );
3744 assert_eq!(read(&store, &data_path).await, None);
3745
3746 let slice = store.get_range(&index_path, 1..4).await.unwrap();
3748 assert_eq!(slice.as_ref(), b"NDE");
3749 }
3750 }
3751}
3752
3753pub fn store_key(location: &Url) -> String {
3758 blake3::hash(location.as_str().as_bytes()).to_hex()[..16].to_owned()
3759}
3760
3761fn prune_stale_uuid_dirs(dir: &std::path::Path, keep: &std::collections::HashSet<String>) {
3765 let Ok(entries) = std::fs::read_dir(dir) else {
3766 return;
3767 };
3768 for entry in entries.flatten() {
3769 let path = entry.path();
3770 if !path.is_dir() {
3771 continue;
3772 }
3773 if entry.file_name() == "_indices" {
3774 let Ok(children) = std::fs::read_dir(&path) else {
3775 continue;
3776 };
3777 for child in children.flatten() {
3778 if child.path().is_dir()
3779 && !keep.contains(child.file_name().to_string_lossy().as_ref())
3780 {
3781 let _ = std::fs::remove_dir_all(child.path());
3782 }
3783 }
3784 } else {
3785 prune_stale_uuid_dirs(&path, keep);
3786 }
3787 }
3788}
3789
3790fn index_store_wrapper(
3794 location: &Url,
3795 index_cache_dir: Option<&std::path::Path>,
3796) -> Option<Arc<dyn WrappingObjectStore>> {
3797 let mut wrappers: Vec<Arc<dyn WrappingObjectStore>> = Vec::new();
3798 if let Some(dir) = index_cache_dir
3799 && !config::is_local(location)
3800 {
3801 let root = dir.join(store_key(location)).join("indices");
3802 match index_cache::IndexDiskCache::new(root) {
3803 Ok(cache) => wrappers.push(Arc::new(cache)),
3804 Err(error) => tracing::warn!(%error, "index disk cache disabled; reads hit the store"),
3805 }
3806 }
3807 if let Some(tracker) = io_trace::wrapper() {
3808 wrappers.push(tracker);
3809 }
3810 match wrappers.len() {
3811 0 => None,
3812 1 => Some(wrappers.remove(0)),
3813 _ => Some(Arc::new(ChainedWrappingObjectStore::new(wrappers))),
3814 }
3815}
3816
3817async fn open_or_create_via_ns(
3818 nm: &Arc<dyn LanceNamespace>,
3819 nm_ident: &NamespaceIdent,
3820 table_name: &str,
3821 schema: lance::deps::arrow_schema::SchemaRef,
3822 session: &Arc<Session>,
3823 storage_options: &HashMap<String, String>,
3824 wrapper: Option<Arc<dyn WrappingObjectStore>>,
3825) -> Result<Dataset> {
3826 let table_id = nm_ident.as_table_id(table_name);
3827
3828 let request = DescribeTableRequest {
3829 id: Some(table_id.clone()),
3830 ..Default::default()
3831 };
3832 match nm.describe_table(request).await {
3833 Ok(response) => {
3834 let location = response.location.with_context(|| {
3835 format!("namespace returned no location for table {table_name}")
3836 })?;
3837 let mut builder = DatasetBuilder::from_uri(&location).with_session(session.clone());
3838 match wrapper {
3839 Some(wrapper) => {
3840 builder = builder.with_store_params(ObjectStoreParams {
3841 object_store_wrapper: Some(wrapper),
3842 storage_options_accessor: (!storage_options.is_empty()).then(|| {
3843 Arc::new(StorageOptionsAccessor::with_static_options(
3844 storage_options.clone(),
3845 ))
3846 }),
3847 ..Default::default()
3848 });
3849 }
3850 None => {
3851 if !storage_options.is_empty() {
3852 builder = builder.with_storage_options(storage_options.clone());
3853 }
3854 }
3855 }
3856 let dataset = builder
3857 .load()
3858 .await
3859 .with_context(|| format!("failed to open table {table_name}"))?;
3860 ensure_schema_matches(&dataset, schema.as_ref(), table_name)?;
3861 return Ok(dataset);
3862 }
3863 Err(error) => match &error {
3864 error if is_namespace_error_code(error, ErrorCode::TableNotFound) => {
3865 }
3867 _ => {
3868 return Err(anyhow::Error::from(error))
3869 .with_context(|| format!("failed to describe table {table_name}"));
3870 }
3871 },
3872 }
3873
3874 let mut write_params = sessions::write_params_for_create();
3877 write_params.session = Some(session.clone());
3878 write_params.mode = WriteMode::Create;
3879 if !storage_options.is_empty() {
3880 write_params.store_params = Some(ObjectStoreParams {
3881 storage_options_accessor: Some(Arc::new(StorageOptionsAccessor::with_static_options(
3882 storage_options.clone(),
3883 ))),
3884 ..Default::default()
3885 });
3886 }
3887 let reader = sessions::empty_reader(schema)?;
3888 Dataset::write_into_namespace(reader, nm.clone(), table_id, Some(write_params))
3889 .await
3890 .with_context(|| format!("failed to create table {table_name}"))
3891}
3892
3893fn is_namespace_error_code(error: &lance::Error, code: ErrorCode) -> bool {
3897 if !matches!(error, lance::Error::Namespace { .. }) {
3898 return false;
3899 }
3900 std::iter::successors(Some(error as &(dyn std::error::Error + 'static)), |link| {
3901 link.source()
3902 })
3903 .filter_map(|link| link.downcast_ref::<NamespaceError>())
3904 .any(|inner| inner.code() == code)
3905}
3906
3907fn scanner_with_prefilter(
3908 dataset: &Dataset,
3909 predicate: Option<&Predicate>,
3910) -> Result<lance::dataset::scanner::Scanner> {
3911 let mut scanner = dataset.scan();
3912 scanner.prefilter(true);
3913 if let Some(predicate) = predicate {
3914 let filter = predicate.to_lance();
3915 if !filter.is_empty() {
3916 scanner.filter(&filter)?;
3917 }
3918 }
3919 Ok(scanner)
3920}
3921fn ensure_schema_matches(
3922 dataset: &Dataset,
3923 expected: &lance::deps::arrow_schema::Schema,
3924 table_name: &str,
3925) -> Result<()> {
3926 use lance::deps::arrow_schema::DataType;
3927 use std::collections::BTreeSet;
3928 let actual = lance::deps::arrow_schema::Schema::from(dataset.schema());
3929 let actual_names: BTreeSet<&str> = actual.fields().iter().map(|f| f.name().as_str()).collect();
3930 let expected_names: BTreeSet<&str> = expected
3931 .fields()
3932 .iter()
3933 .map(|f| f.name().as_str())
3934 .collect();
3935 if actual_names != expected_names {
3936 anyhow::bail!(
3937 "table {table_name} has columns {actual_names:?} but this pond build expects \
3938 {expected_names:?} - the on-disk store predates a schema change; delete the \
3939 data directory and re-run `pond ingest`",
3940 );
3941 }
3942 for actual_field in actual.fields() {
3947 let Some(expected_field) = expected.field_with_name(actual_field.name()).ok() else {
3948 continue;
3949 };
3950 if let (DataType::FixedSizeList(_, actual_dim), DataType::FixedSizeList(_, expected_dim)) =
3951 (actual_field.data_type(), expected_field.data_type())
3952 && actual_dim != expected_dim
3953 {
3954 tracing::warn!(
3955 table = table_name,
3956 column = actual_field.name(),
3957 actual_dim,
3958 expected_dim,
3959 "embedding dimension differs from config; open proceeds because model swaps are operator-driven",
3960 );
3961 }
3962 }
3963 Ok(())
3964}
3965fn apply_remote_storage_defaults(options: &mut HashMap<String, String>) {
3972 fn set_default(options: &mut HashMap<String, String>, aliases: &[&str], value: &str) {
3973 if aliases
3974 .iter()
3975 .any(|alias| options.keys().any(|k| k.eq_ignore_ascii_case(alias)))
3976 {
3977 return;
3978 }
3979 options.insert(aliases[0].to_owned(), value.to_owned());
3980 }
3981 set_default(options, &["pool_idle_timeout"], "300 seconds");
3982 set_default(options, &["connect_timeout"], "10 seconds");
3983 set_default(options, &["request_timeout"], "60 seconds");
3990 let has_custom_endpoint = ["aws_endpoint", "endpoint"]
3991 .iter()
3992 .any(|alias| options.keys().any(|k| k.eq_ignore_ascii_case(alias)));
3993 if has_custom_endpoint {
3994 set_default(
3995 options,
3996 &["aws_unsigned_payload", "unsigned_payload"],
3997 "true",
3998 );
3999 }
4000}
4001
4002fn quoted_string(value: &str) -> String {
4003 format!("'{}'", value.replace('\'', "''"))
4004}
4005fn like_contains(value: &str) -> String {
4006 let escaped = value
4007 .replace('\\', "\\\\")
4008 .replace('%', "\\%")
4009 .replace('_', "\\_")
4010 .replace('\'', "''");
4011 format!("'%{escaped}%'")
4012}
4013
4014#[cfg(test)]
4015mod tests {
4016 #![allow(clippy::expect_used, clippy::unwrap_used)]
4017
4018 use super::*;
4019 use tempfile::TempDir;
4020
4021 #[test]
4022 fn is_index_error_matches_lance_index_class_through_context() {
4023 let index_fault = anyhow::Error::from(lance::Error::index(
4024 "cannot merge inverted index segments with different posting tail codecs",
4025 ))
4026 .context("optimize_indices(merge) failed during index optimize");
4027 assert!(is_index_error(&index_fault));
4028
4029 let io_fault = anyhow::anyhow!("connection reset").context("optimize_indices failed");
4030 assert!(!is_index_error(&io_fault));
4031 }
4032
4033 #[test]
4034 fn prune_keeps_live_uuid_dirs_and_drops_dead_ones() {
4035 let temp = TempDir::new().unwrap();
4036 let indices = temp.path().join("bkt/messages.lance/_indices");
4037 for uuid in ["live", "dead"] {
4038 std::fs::create_dir_all(indices.join(uuid)).unwrap();
4039 std::fs::write(indices.join(uuid).join("index.idx"), b"x").unwrap();
4040 }
4041 let keep = std::collections::HashSet::from(["live".to_owned()]);
4042 prune_stale_uuid_dirs(temp.path(), &keep);
4043 assert!(indices.join("live").exists());
4044 assert!(!indices.join("dead").exists());
4045 }
4046
4047 fn set(scope: Option<&str>) -> CredsSet {
4048 CredsSet {
4049 scope: scope.map(str::to_owned),
4050 access_key_id: Some("AKIA".to_owned()),
4051 secret_access_key: Some("shh".to_owned()),
4052 ..CredsSet::default()
4053 }
4054 }
4055
4056 fn opts(resolved: &ResolvedStorage, key: &str) -> Option<String> {
4057 resolved.options.get(key).cloned()
4058 }
4059
4060 #[test]
4061 fn storage_url_translation_table() {
4062 let local = StorageUrl::parse("/srv/pond").unwrap();
4065 assert_eq!(local.lance_url().as_str(), "file:///srv/pond/");
4066 assert!(local.is_local());
4067 assert!(local.scheme_options.is_empty());
4068 let aws = StorageUrl::parse("s3://bucket/prefix").unwrap();
4070 assert_eq!(aws.lance_url().as_str(), "s3://bucket/prefix");
4071 assert!(aws.scheme_options.is_empty());
4072 let fat = StorageUrl::parse("s3+https://nbg1.example.com/my-pond/sub").unwrap();
4077 assert_eq!(fat.lance_url().as_str(), "s3://my-pond/sub");
4078 assert_eq!(
4079 fat.scheme_options,
4080 vec![
4081 ("allow_http", "false".to_owned()),
4082 ("virtual_hosted_style_request", "true".to_owned()),
4083 ("region", "us-east-1".to_owned()),
4084 ],
4085 );
4086 let resolved = fat.resolve(&BTreeMap::new()).unwrap();
4087 assert_eq!(
4088 opts(&resolved, "endpoint").as_deref(),
4089 Some("https://my-pond.nbg1.example.com"),
4090 );
4091 assert_eq!(opts(&resolved, "region").as_deref(), Some("us-east-1"));
4092 let plain = StorageUrl::parse("s3+http://127.0.0.1:9000/pond").unwrap();
4095 assert_eq!(plain.lance_url().as_str(), "s3://pond/");
4096 assert_eq!(plain.scheme_options[0], ("allow_http", "true".to_owned()));
4097 assert_eq!(
4098 plain.scheme_options[1],
4099 ("virtual_hosted_style_request", "false".to_owned()),
4100 );
4101 let resolved = plain.resolve(&BTreeMap::new()).unwrap();
4102 assert_eq!(
4103 opts(&resolved, "endpoint").as_deref(),
4104 Some("http://127.0.0.1:9000"),
4105 );
4106 let mut pinned = BTreeMap::new();
4108 pinned.insert(
4109 "default".to_owned(),
4110 CredsSet {
4111 extra: [(
4112 "endpoint".to_owned(),
4113 "https://pinned.example.com".to_owned(),
4114 )]
4115 .into_iter()
4116 .collect(),
4117 ..CredsSet::default()
4118 },
4119 );
4120 let resolved = fat.resolve(&pinned).unwrap();
4121 assert_eq!(
4122 opts(&resolved, "endpoint").as_deref(),
4123 Some("https://pinned.example.com"),
4124 );
4125 let gcs = StorageUrl::parse("gs://bucket/p").unwrap();
4127 assert_eq!(gcs.lance_url().as_str(), "gs://bucket/p");
4128 let azure = StorageUrl::parse("az://acct/container/p").unwrap();
4130 assert_eq!(azure.lance_url().as_str(), "az://container/p");
4131 assert_eq!(
4132 azure.scheme_options,
4133 vec![("account_name", "acct".to_owned())]
4134 );
4135 let shared = StorageUrl::parse("shared-memory://pond-test-x/").unwrap();
4137 assert_eq!(shared.lance_url().as_str(), "shared-memory://pond-test-x/");
4138 }
4139
4140 #[test]
4141 fn storage_url_rejects_bad_shapes() {
4142 let err = StorageUrl::parse("s3+https://user:pass@host/bucket")
4144 .expect_err("userinfo must be rejected")
4145 .to_string();
4146 assert!(
4147 err.contains("creds"),
4148 "error must name the alternative: {err}"
4149 );
4150 assert!(StorageUrl::parse("s3+https://host").is_err());
4152 assert!(StorageUrl::parse("az://acct").is_err());
4153 let err = StorageUrl::parse("ftp://host/x")
4155 .expect_err("ftp")
4156 .to_string();
4157 assert!(err.contains("s3+https"), "got: {err}");
4158 let err = StorageUrl::parse("s3://b/p?regoin=x")
4160 .expect_err("typo")
4161 .to_string();
4162 assert!(err.contains("regoin"), "got: {err}");
4163 let err = StorageUrl::parse("memory://x?creds=y")
4166 .expect_err("memory query")
4167 .to_string();
4168 assert!(err.contains("query params"), "got: {err}");
4169 let err = StorageUrl::parse("file:///x?creds=y")
4170 .expect_err("file query")
4171 .to_string();
4172 assert!(err.contains("query params"), "got: {err}");
4173 assert!(StorageUrl::parse("/tmp/a?b").is_ok());
4175 }
4176
4177 #[test]
4178 fn storage_url_canonicalizes_ports_and_keeps_percent_encoding() {
4179 let with_port = StorageUrl::parse("s3+https://host:443/bucket/p").unwrap();
4181 let without = StorageUrl::parse("s3+https://host/bucket/p").unwrap();
4182 assert_eq!(with_port.canonical(), without.canonical());
4183 let odd = StorageUrl::parse("s3+https://host:8443/bucket").unwrap();
4185 let resolved = odd.resolve(&BTreeMap::new()).unwrap();
4186 assert_eq!(
4187 resolved.options.get("endpoint").map(String::as_str),
4188 Some("https://bucket.host:8443"),
4189 );
4190 let encoded = StorageUrl::parse("s3+https://host/bucket/pre%20fix").unwrap();
4192 assert_eq!(encoded.lance_url().as_str(), "s3://bucket/pre%20fix");
4193 }
4194
4195 #[test]
4196 fn query_params_strip_and_apply_over_set_fields() {
4197 let mut creds = BTreeMap::new();
4198 creds.insert(
4199 "default".to_owned(),
4200 CredsSet {
4201 region: Some("from-set".to_owned()),
4202 virtual_hosted_style_request: Some(false),
4203 ..set(None)
4204 },
4205 );
4206 let url = StorageUrl::parse(
4207 "s3+https://host/bucket/p?region=from-query&virtual_hosted_style_request=true",
4208 )
4209 .unwrap();
4210 assert_eq!(url.lance_url().as_str(), "s3://bucket/p");
4212 assert!(url.canonical().query().is_none());
4213 let resolved = url.resolve(&creds).unwrap();
4214 assert_eq!(opts(&resolved, "region").as_deref(), Some("from-query"));
4216 assert_eq!(
4217 opts(&resolved, "virtual_hosted_style_request").as_deref(),
4218 Some("true"),
4219 );
4220 assert_eq!(
4222 opts(&resolved, "endpoint").as_deref(),
4223 Some("https://bucket.host"),
4224 );
4225 }
4226
4227 #[test]
4228 fn scope_matching_binds_by_longest_prefix_at_segment_boundaries() {
4229 let mut creds = BTreeMap::new();
4230 creds.insert("all".to_owned(), set(None));
4231 creds.insert("bucket".to_owned(), set(Some("s3+https://host/pond/")));
4232 creds.insert("deep".to_owned(), set(Some("s3+https://host/pond/sub")));
4233
4234 let bind = |input: &str| {
4235 StorageUrl::parse(input)
4236 .unwrap()
4237 .resolve(&creds)
4238 .unwrap()
4239 .binding
4240 };
4241 assert_eq!(
4243 bind("s3+https://host/pond/sub/x"),
4244 CredsBinding::Set {
4245 name: "deep".to_owned(),
4246 via: BindVia::Scope
4247 },
4248 );
4249 assert_eq!(
4250 bind("s3+https://host/pond/other"),
4251 CredsBinding::Set {
4252 name: "bucket".to_owned(),
4253 via: BindVia::Scope
4254 },
4255 );
4256 assert_eq!(
4258 bind("s3+https://host/pond-2"),
4259 CredsBinding::Set {
4260 name: "all".to_owned(),
4261 via: BindVia::CatchAll
4262 },
4263 );
4264 assert_eq!(
4266 bind("s3://pond/sub"),
4267 CredsBinding::Set {
4268 name: "all".to_owned(),
4269 via: BindVia::CatchAll
4270 },
4271 );
4272 assert_eq!(
4274 bind("s3+https://host:443/pond/x"),
4275 CredsBinding::Set {
4276 name: "bucket".to_owned(),
4277 via: BindVia::Scope
4278 },
4279 );
4280 assert_eq!(
4282 bind("s3+https://host/pond/sub/x?creds=all"),
4283 CredsBinding::Set {
4284 name: "all".to_owned(),
4285 via: BindVia::Pointer
4286 },
4287 );
4288 let err = StorageUrl::parse("s3://b/p?creds=nope")
4290 .unwrap()
4291 .resolve(&creds)
4292 .expect_err("missing set")
4293 .to_string();
4294 assert!(err.contains("creds=nope"), "got: {err}");
4295
4296 let empty = BTreeMap::new();
4298 assert_eq!(
4299 StorageUrl::parse("s3://b/p")
4300 .unwrap()
4301 .resolve(&empty)
4302 .unwrap()
4303 .binding,
4304 CredsBinding::Ambient,
4305 );
4306 assert_eq!(
4307 StorageUrl::parse("/srv/pond")
4308 .unwrap()
4309 .resolve(&creds)
4310 .unwrap()
4311 .binding,
4312 CredsBinding::NotApplicable,
4313 );
4314 }
4315
4316 #[test]
4317 fn unmatched_sets_are_reported_only_on_remote_invocations() {
4318 let mut creds = BTreeMap::new();
4319 creds.insert("used".to_owned(), set(Some("s3://bucket/")));
4320 creds.insert("idle".to_owned(), set(Some("s3://other/")));
4321
4322 let remote = StorageUrl::parse("s3://bucket/p")
4323 .unwrap()
4324 .resolve(&creds)
4325 .unwrap();
4326 assert_eq!(unmatched_creds_sets(&[&remote], &creds), vec!["idle"]);
4327
4328 let local = StorageUrl::parse("/srv/pond")
4330 .unwrap()
4331 .resolve(&creds)
4332 .unwrap();
4333 assert!(unmatched_creds_sets(&[&local], &creds).is_empty());
4334 }
4335
4336 #[test]
4337 fn secrets_materialize_from_file_and_command() {
4338 let dir = TempDir::new().unwrap();
4339 let key_path = dir.path().join("key");
4340 std::fs::write(&key_path, "from-file\n").unwrap();
4341 let mut creds = BTreeMap::new();
4342 creds.insert(
4343 "default".to_owned(),
4344 CredsSet {
4345 access_key_id_file: Some(key_path),
4346 secret_access_key_command: Some("printf 'from-command\\n\\n'".to_owned()),
4348 ..CredsSet::default()
4349 },
4350 );
4351 let url = StorageUrl::parse("s3://bucket/p").unwrap();
4352 let resolved = url.resolve(&creds).unwrap();
4353 assert_eq!(
4354 opts(&resolved, "access_key_id").as_deref(),
4355 Some("from-file")
4356 );
4357 assert_eq!(
4358 opts(&resolved, "secret_access_key").as_deref(),
4359 Some("from-command\n"),
4360 );
4361
4362 let mut failing = BTreeMap::new();
4364 failing.insert(
4365 "default".to_owned(),
4366 CredsSet {
4367 secret_access_key_command: Some("exit 3".to_owned()),
4368 ..CredsSet::default()
4369 },
4370 );
4371 let err = url
4372 .resolve(&failing)
4373 .expect_err("command must fail")
4374 .to_string();
4375 assert!(err.contains("exit 3"), "got: {err}");
4376
4377 let marker = dir.path().join("runs");
4379 let command = format!("echo run >> {} && echo secret", marker.display());
4380 let mut counted = BTreeMap::new();
4381 counted.insert(
4382 "default".to_owned(),
4383 CredsSet {
4384 secret_access_key_command: Some(command),
4385 ..CredsSet::default()
4386 },
4387 );
4388 url.resolve(&counted).unwrap();
4389 url.resolve(&counted).unwrap();
4390 let runs = std::fs::read_to_string(&marker).unwrap();
4391 assert_eq!(runs.lines().count(), 1, "command must run exactly once");
4392 }
4393
4394 #[test]
4395 fn check_errors_classify_by_kind_and_binding() {
4396 let auth_error = || object_store::Error::Unauthenticated {
4397 path: "k".to_owned(),
4398 source: "denied".into(),
4399 };
4400 let bound = CredsBinding::Set {
4401 name: "work".to_owned(),
4402 via: BindVia::Scope,
4403 };
4404 match classify_check_error(auth_error(), &bound, "put") {
4406 CheckFailure::Auth { set, .. } => assert_eq!(set, "work"),
4407 other => panic!("want Auth, got {other:?}"),
4408 }
4409 assert!(matches!(
4411 classify_check_error(auth_error(), &CredsBinding::Ambient, "put"),
4412 CheckFailure::NoCreds { .. },
4413 ));
4414 let denied = object_store::Error::PermissionDenied {
4415 path: "k".to_owned(),
4416 source: "403".into(),
4417 };
4418 assert!(matches!(
4419 classify_check_error(denied, &bound, "put"),
4420 CheckFailure::Auth { .. },
4421 ));
4422 let missing = object_store::Error::NotFound {
4424 path: "k".to_owned(),
4425 source: "404".into(),
4426 };
4427 assert!(matches!(
4428 classify_check_error(missing, &bound, "get"),
4429 CheckFailure::Io { .. },
4430 ));
4431 let no_creds = || object_store::Error::Generic {
4435 store: "S3",
4436 source: "Failed to get AWS credentials: CredentialsNotLoaded".into(),
4437 };
4438 assert!(matches!(
4439 classify_check_error(no_creds(), &bound, "put"),
4440 CheckFailure::Auth { .. },
4441 ));
4442 assert!(matches!(
4443 classify_check_error(no_creds(), &CredsBinding::Ambient, "put"),
4444 CheckFailure::NoCreds { .. },
4445 ));
4446 }
4447
4448 #[test]
4449 fn concise_cause_strips_upstream_noise_to_one_line() {
4450 let inner = "Encountered internal error. Please file a bug report at \
4453 https://github.com/lance-format/lance/issues. Failed to get AWS \
4454 credentials: CredentialsNotLoaded, <WORKSPACE>/src/object_store/providers/aws.rs:401:21: \
4455 Encountered internal error. Please file a bug report at \
4456 https://github.com/lance-format/lance/issues. Failed to get AWS \
4457 credentials: CredentialsNotLoaded";
4458 let failure = CheckFailure::NoCreds {
4459 source: anyhow!(inner.to_owned()).context("initial conditional put"),
4460 };
4461 let cause = failure.concise_cause().expect("auth-class carries a cause");
4462 assert_eq!(cause, "Failed to get AWS credentials: CredentialsNotLoaded");
4463 assert!(
4465 !failure.to_string().contains("file a bug report"),
4466 "lead must not trail the chain: {failure}"
4467 );
4468 let occ = CheckFailure::OccUnsupported {
4470 detail: "put-if-none-match ignored".to_owned(),
4471 };
4472 assert!(occ.concise_cause().is_none());
4473 let long = CheckFailure::Io {
4476 source: anyhow!(format!("{} dns error: lookup failed", "x".repeat(500))),
4477 };
4478 let cause = long.concise_cause().expect("io carries a cause");
4479 assert!(cause.contains(" ... "), "long causes truncate: {cause}");
4480 assert!(
4481 cause.ends_with("dns error: lookup failed"),
4482 "the tail survives: {cause}"
4483 );
4484 }
4485
4486 #[tokio::test]
4487 async fn storage_check_passes_on_memory_backend() {
4488 let resolved = StorageUrl::parse("memory://check/probe")
4489 .unwrap()
4490 .resolve(&BTreeMap::new())
4491 .unwrap();
4492 storage_check(&resolved).await.expect("memory probe passes");
4493 }
4494
4495 fn stat(bytes: u64) -> FragmentStat {
4496 FragmentStat {
4497 bytes: Some(bytes),
4498 rows: bytes / 1_000,
4499 deleted_rows: 0,
4500 }
4501 }
4502
4503 #[test]
4504 fn compaction_veto_blocks_absorb_keeps_peers() {
4505 let absorb = [stat(665_000_000), stat(1_000_000), stat(2_000_000)];
4507 assert!(!keep_task(&absorb, 64, 0.1));
4508 let peers = [stat(300_000_000), stat(300_000_000)];
4510 assert!(keep_task(&peers, 64, 0.1));
4511 let tiered = [stat(400_000), stat(60_000), stat(40_000)];
4513 assert!(keep_task(&tiered, 64, 0.1));
4514 }
4515
4516 #[test]
4517 fn compaction_veto_passes_deletions_and_cap() {
4518 let mut deleting = stat(665_000_000);
4519 deleting.deleted_rows = deleting.rows / 5;
4520 assert!(keep_task(&[deleting, stat(1_000)], 64, 0.1));
4521
4522 let wide: Vec<FragmentStat> = std::iter::once(stat(665_000_000))
4523 .chain(std::iter::repeat_with(|| stat(1_000)).take(63))
4524 .collect();
4525 assert!(keep_task(&wide, 64, 0.1));
4526 }
4527
4528 #[test]
4529 fn compaction_veto_falls_back_to_rows_on_unknown_sizes() {
4530 let mut unknown = stat(665_000_000);
4531 unknown.bytes = None;
4532 assert!(!keep_task(
4534 &[unknown, stat(1_000_000), stat(2_000_000)],
4535 64,
4536 0.1
4537 ));
4538 }
4539
4540 #[test]
4541 fn cleanup_due_gates_on_version_interval() {
4542 assert!(cleanup_due(0, 1));
4544 assert!(cleanup_due(7, 1));
4545 assert!(cleanup_due(5, 0));
4546 assert!(cleanup_due(0, 16));
4548 assert!(cleanup_due(16, 16));
4549 assert!(cleanup_due(48, 16));
4550 assert!(!cleanup_due(15, 16));
4551 assert!(!cleanup_due(17, 16));
4552 assert!(!cleanup_due(31, 16));
4553 }
4554
4555 #[test]
4556 fn derived_target_rows_tracks_row_size_and_clamps() {
4557 let parts_like = [FragmentStat {
4560 bytes: Some(665_000_000),
4561 rows: 511_000,
4562 deleted_rows: 0,
4563 }];
4564 let target = derived_target_rows(&parts_like);
4565 assert!((80_000..150_000).contains(&target), "{target}");
4566 let unknown = [FragmentStat {
4568 bytes: None,
4569 rows: 511_000,
4570 deleted_rows: 0,
4571 }];
4572 assert_eq!(
4573 derived_target_rows(&unknown),
4574 MAX_TARGET_ROWS_PER_FRAGMENT as usize
4575 );
4576 let tiny = [FragmentStat {
4578 bytes: Some(1_000_000),
4579 rows: 100_000,
4580 deleted_rows: 0,
4581 }];
4582 assert_eq!(
4583 derived_target_rows(&tiny),
4584 MAX_TARGET_ROWS_PER_FRAGMENT as usize
4585 );
4586 let huge = [FragmentStat {
4587 bytes: Some(1_000_000_000),
4588 rows: 100,
4589 deleted_rows: 0,
4590 }];
4591 assert_eq!(
4592 derived_target_rows(&huge),
4593 MIN_TARGET_ROWS_PER_FRAGMENT as usize
4594 );
4595 }
4596
4597 #[test]
4598 fn namespace_error_code_walks_wrapped_chain() {
4599 let direct = lance::Error::namespace_source(Box::new(NamespaceError::TableNotFound {
4600 message: "missing".into(),
4601 }));
4602 assert!(is_namespace_error_code(&direct, ErrorCode::TableNotFound));
4603
4604 let wrapped = lance::Error::namespace_source(Box::new(direct));
4605 assert!(is_namespace_error_code(&wrapped, ErrorCode::TableNotFound));
4606
4607 let other_code =
4608 lance::Error::namespace_source(Box::new(NamespaceError::NamespaceNotFound {
4609 message: "nope".into(),
4610 }));
4611 assert!(!is_namespace_error_code(
4612 &other_code,
4613 ErrorCode::TableNotFound
4614 ));
4615
4616 let not_namespace = lance::Error::internal("unrelated");
4617 assert!(!is_namespace_error_code(
4618 ¬_namespace,
4619 ErrorCode::TableNotFound
4620 ));
4621 }
4622
4623 #[tokio::test]
4627 async fn store_opens_via_namespace_and_scan_works() -> Result<()> {
4628 let temp = TempDir::new()?;
4629 let url = Url::from_directory_path(temp.path())
4630 .map_err(|()| anyhow::anyhow!("temp path is not absolute"))?;
4631 let handle = Handle::open(&url).await?;
4632 let cases: [(Table, &[&str]); 3] = [
4635 (Table::Sessions, &["id"]),
4636 (Table::Messages, &["id"]),
4637 (Table::Parts, &["id"]),
4638 ];
4639 for (table, projection) in cases {
4640 let scanner = handle
4641 .scan(table, ScanOpts::project_only(projection))
4642 .await?;
4643 let batch = scanner.try_into_batch().await?;
4644 assert_eq!(batch.num_rows(), 0, "fresh table should be empty");
4645 }
4646 Ok(())
4647 }
4648}