infino 0.5.6

A fast retrieval engine that stores data on object storage and runs SQL, full-text search, and vector search over it from a single system — search-on-Parquet.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Infino Authors

//! The single public error type for the curated infino API.
//!
//! Public methods return `Result<T, InfinoError>`. The internal
//! per-stage error enums (`OpenError`, `BuildError`, `ReadError`,
//! `QueryError`, `MutationError`, `CommitError`, `StorageError`)
//! convert inward via `From`. The mappings are intentionally **coarse**
//! — they collapse many internal variants onto a small, stable public
//! set. `InfinoError` is `#[non_exhaustive]`, so finer variants (or
//! structured source chaining) can be added later without a breaking
//! change. Named `InfinoError` (not `Error`) to avoid colliding with
//! the `std::error::Error` trait at call sites and to read consistently
//! alongside `DataFusionError` / `ArrowError`.
//!
//! ## Boundary context
//!
//! Public API methods prefix the message with the operation (and catalog
//! table name when known), e.g. `not found: open_table(posts): posts`,
//! via [`InfinoError::with_context`]. Structured payload / `source()`
//! chaining can follow in later PRs.

use crate::{
    storage::StorageError,
    superfile::{BuildError as SuperfileBuildError, ReadError as SuperfileReadError},
    supertable::{
        error::{
            BuildError as SupertableBuildError, CommitError as SupertableCommitError, OpenError,
            QueryError,
        },
        manifest::ManifestLoadError,
        mutations::{CommitError as MutationCommitError, MutationError},
    },
};

/// Coarse, stable error type returned by every public infino method.
///
/// Each variant carries a human-readable message (the originating
/// error's `Display`). The set is deliberately small; `#[non_exhaustive]`
/// keeps it open to growth without breaking downstream `match`es.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum InfinoError {
    /// A named table, object, or column was not found.
    #[error("not found: {0}")]
    NotFound(String),

    /// A create conflicted with an existing name / object.
    #[error("already exists: {0}")]
    AlreadyExists(String),

    /// Schema or column validation failed.
    #[error("schema: {0}")]
    Schema(String),

    /// A predicate matched a different row count than required, or
    /// exceeded the mutation cap.
    #[error("cardinality: {0}")]
    Cardinality(String),

    /// Storage / I/O failure.
    #[error("io: {0}")]
    Io(String),

    /// SQL planning or execution failure.
    #[error("query: {0}")]
    Query(String),

    /// A query exceeded the connection's memory budget (see
    /// [`ConnectOptions::with_connection_memory_budget_bytes`]). For SQL the
    /// engine spills first and only raises this when it still can't fit.
    ///
    /// [`ConnectOptions::with_connection_memory_budget_bytes`]: crate::ConnectOptions::with_connection_memory_budget_bytes
    #[error("over budget: {0}")]
    OverBudget(String),

    /// A concurrent writer won the race: an optimistic-concurrency
    /// (compare-and-set) precondition failed and the operation's own retry
    /// budget was exhausted.
    ///
    /// **Retryable.** Nothing partial is left visible — the losing writer's
    /// manifest swap never published, and a mutation whose WAL did become
    /// durable is completed idempotently by the recovery sweep. Reissuing
    /// `append` / `update` / `delete` (ideally with backoff) resolves the
    /// predicate against fresh state and can succeed. Persistent conflicts
    /// mean genuine multi-writer contention on one table, not a fault.
    #[error("conflict: {0}")]
    Conflict(String),

    /// Backend / internal failure that doesn't map to a more specific
    /// variant.
    #[error("backend: {0}")]
    Backend(String),

    /// An invalid or conflicting configuration was supplied.
    #[error("config: {0}")]
    Config(String),
}

impl InfinoError {
    /// Prefix this error's message with `operation` or `operation(table)`.
    ///
    /// Used at public API boundaries so Display carries enough context
    /// without changing the variant shape. Example:
    /// `not found: open_table(posts): posts`.
    ///  not found: Kind of failure (the InfinoError variant)
    ///  open_table(posts): Public operation that failed (Operation open_table, catalog table posts)
    ///  posts: Detail / original message.
    pub(crate) fn with_context(self, operation: &'static str, table: Option<&str>) -> Self {
        let prefix = match table {
            Some(t) => format!("{operation}({t})"),
            None => operation.to_string(),
        };
        match self {
            Self::NotFound(m) => Self::NotFound(format!("{prefix}: {m}")),
            Self::AlreadyExists(m) => Self::AlreadyExists(format!("{prefix}: {m}")),
            Self::Schema(m) => Self::Schema(format!("{prefix}: {m}")),
            Self::Cardinality(m) => Self::Cardinality(format!("{prefix}: {m}")),
            Self::Io(m) => Self::Io(format!("{prefix}: {m}")),
            Self::Query(m) => Self::Query(format!("{prefix}: {m}")),
            Self::OverBudget(m) => Self::OverBudget(format!("{prefix}: {m}")),
            Self::Conflict(m) => Self::Conflict(format!("{prefix}: {m}")),
            Self::Backend(m) => Self::Backend(format!("{prefix}: {m}")),
            Self::Config(m) => Self::Config(format!("{prefix}: {m}")),
        }
    }
}

impl From<StorageError> for InfinoError {
    fn from(e: StorageError) -> Self {
        let msg = e.to_string();
        match e {
            StorageError::NotFound { .. } => InfinoError::NotFound(msg),
            StorageError::PreconditionFailed { .. } => InfinoError::Conflict(msg),
            StorageError::TransientExhausted { .. } | StorageError::Permanent { .. } => {
                InfinoError::Io(msg)
            }
        }
    }
}

impl From<QueryError> for InfinoError {
    fn from(e: QueryError) -> Self {
        if let Some(msg) = e.over_budget() {
            return InfinoError::OverBudget(msg.to_string());
        }
        InfinoError::Query(e.to_string())
    }
}

impl From<ManifestLoadError> for InfinoError {
    fn from(e: ManifestLoadError) -> Self {
        let msg = e.to_string();
        match e {
            // The table this handle was reading has been dropped and purged, so
            // the name it was opened under no longer resolves to anything —
            // `NotFound`, not a backend fault, is what a caller must react to.
            ManifestLoadError::PointerVanished => InfinoError::NotFound(msg),
            // A storage fault reading the manifest — the pointer probe or a
            // part load — is a transient I/O hiccup, not a permanent failure.
            // Surface it as `Io` so a caller can retry (e.g. against another
            // copy of the data) rather than treat it as a hard backend fault.
            ManifestLoadError::Storage(_) => InfinoError::Io(msg),
            _ => InfinoError::Backend(msg),
        }
    }
}

impl From<SuperfileReadError> for InfinoError {
    fn from(e: SuperfileReadError) -> Self {
        if let Some(msg) = e.over_budget() {
            return InfinoError::OverBudget(msg.to_string());
        }
        InfinoError::Query(e.to_string())
    }
}

impl From<SuperfileBuildError> for InfinoError {
    fn from(e: SuperfileBuildError) -> Self {
        InfinoError::Schema(e.to_string())
    }
}

impl From<SupertableBuildError> for InfinoError {
    fn from(e: SupertableBuildError) -> Self {
        if let Some(msg) = e.over_budget() {
            return InfinoError::OverBudget(msg.to_string());
        }
        if e.is_conflict() {
            return InfinoError::Conflict(e.to_string());
        }
        // A commit that found its table dropped and purged is not a schema
        // problem; it is the name no longer resolving. Same answer the read
        // path gives, so a caller can match one condition, not three.
        if matches!(e, SupertableBuildError::TableGone) {
            return InfinoError::NotFound(e.to_string());
        }
        InfinoError::Schema(e.to_string())
    }
}

impl From<SupertableCommitError> for InfinoError {
    fn from(e: SupertableCommitError) -> Self {
        let msg = e.to_string();
        match e {
            // Reached by commit paths that surface the typed error directly
            // (the append path converts to `BuildError::TableGone` first).
            SupertableCommitError::PointerVanished => InfinoError::NotFound(msg),
            // The OCC retry budget ran out on a contended pointer / part CAS.
            e if e.is_conflict() => InfinoError::Conflict(msg),
            _ => InfinoError::Backend(msg),
        }
    }
}

impl From<OpenError> for InfinoError {
    fn from(e: OpenError) -> Self {
        if e.is_conflict() {
            return InfinoError::Conflict(e.to_string());
        }
        InfinoError::Backend(e.to_string())
    }
}

impl From<MutationError> for InfinoError {
    fn from(e: MutationError) -> Self {
        let msg = e.to_string();
        if e.is_conflict() {
            return InfinoError::Conflict(msg);
        }
        match e {
            // Routes over-budget through From<QueryError> when the predicate
            // eval was the budget refusal.
            MutationError::PredicateEval(q) => InfinoError::from(q),
            MutationError::Storage(s) => InfinoError::from(s),
            MutationError::CardinalityMismatch { .. }
            | MutationError::MatchCountExceedsCap { .. } => InfinoError::Cardinality(msg),
            MutationError::SchemaMismatch(_) => InfinoError::Schema(msg),
            // Matches the read path: a purged table's name resolves to nothing.
            MutationError::TableGone => InfinoError::NotFound(msg),
            _ => InfinoError::Backend(msg),
        }
    }
}

impl From<MutationCommitError> for InfinoError {
    fn from(e: MutationCommitError) -> Self {
        if let Some(msg) = e.over_budget() {
            return InfinoError::OverBudget(msg.to_string());
        }
        if e.is_conflict() {
            return InfinoError::Conflict(e.to_string());
        }
        // `Supertable::append` lands here, so this is the arm that decides what
        // appending to a purged table reports. Narrow on purpose: every other
        // append-flush failure keeps its existing `Backend` shape.
        if matches!(
            &e,
            MutationCommitError::AppendFlush(SupertableBuildError::TableGone)
        ) {
            return InfinoError::NotFound(e.to_string());
        }
        InfinoError::Backend(e.to_string())
    }
}

#[cfg(test)]
mod tests {
    use uuid::Uuid;

    use super::*;
    use crate::{
        storage::StorageError,
        supertable::wal::{
            WalStoreError,
            pipeline::{AppendPhaseError, TombstonePhaseError},
        },
    };

    #[test]
    fn display_messages_are_prefixed() {
        assert_eq!(
            InfinoError::NotFound("t".into()).to_string(),
            "not found: t"
        );
        assert_eq!(
            InfinoError::AlreadyExists("t".into()).to_string(),
            "already exists: t"
        );
        assert_eq!(InfinoError::Schema("t".into()).to_string(), "schema: t");
        assert_eq!(
            InfinoError::Cardinality("t".into()).to_string(),
            "cardinality: t"
        );
        assert_eq!(InfinoError::Io("t".into()).to_string(), "io: t");
        assert_eq!(InfinoError::Query("t".into()).to_string(), "query: t");
        assert_eq!(InfinoError::Conflict("t".into()).to_string(), "conflict: t");
        assert_eq!(InfinoError::Backend("t".into()).to_string(), "backend: t");
        assert_eq!(InfinoError::Config("t".into()).to_string(), "config: t");
    }

    #[test]
    fn with_context_prefixes_operation_and_table() {
        let err = InfinoError::NotFound("posts".into()).with_context("open_table", Some("posts"));
        assert_eq!(err.to_string(), "not found: open_table(posts): posts");

        let err = InfinoError::Cardinality("mismatch".into()).with_context("update", None);
        assert_eq!(err.to_string(), "cardinality: update: mismatch");

        let err = InfinoError::Conflict("lost the CAS".into()).with_context("delete", None);
        assert_eq!(err.to_string(), "conflict: delete: lost the CAS");
    }

    #[test]
    fn from_storage_error_maps_each_variant() {
        assert!(matches!(
            InfinoError::from(StorageError::NotFound { uri: "u".into() }),
            InfinoError::NotFound(_)
        ));
        assert!(matches!(
            InfinoError::from(StorageError::PreconditionFailed { uri: "u".into() }),
            InfinoError::Conflict(_)
        ));
        assert!(matches!(
            InfinoError::from(StorageError::TransientExhausted {
                uri: "u".into(),
                source: "x".into()
            }),
            InfinoError::Io(_)
        ));
        assert!(matches!(
            InfinoError::from(StorageError::Permanent {
                uri: "u".into(),
                source: "x".into()
            }),
            InfinoError::Io(_)
        ));
    }

    #[test]
    fn from_query_read_and_build_errors() {
        assert!(matches!(
            InfinoError::from(QueryError::Plan("p".into())),
            InfinoError::Query(_)
        ));
        // A budget refusal keeps its own variant rather than collapsing to Query.
        assert!(matches!(
            InfinoError::from(QueryError::OverBudget("b".into())),
            InfinoError::OverBudget(_)
        ));
        assert!(matches!(
            InfinoError::from(SuperfileReadError::MissingKv("k")),
            InfinoError::Query(_)
        ));
        assert!(matches!(
            InfinoError::from(SuperfileBuildError::MissingIdColumn("c".into())),
            InfinoError::Schema(_)
        ));
        assert!(matches!(
            InfinoError::from(SupertableBuildError::NoDocsToBuild),
            InfinoError::Schema(_)
        ));
    }

    #[test]
    fn from_commit_and_open_errors_are_backend() {
        assert!(matches!(
            InfinoError::from(SupertableCommitError::Encode("e".into())),
            InfinoError::Backend(_)
        ));
        assert!(matches!(
            InfinoError::from(OpenError::ManifestListParse("m".into())),
            InfinoError::Backend(_)
        ));
    }

    #[test]
    fn manifest_pointer_vanished_is_not_found_but_a_storage_fault_is_retryable_io() {
        // A dropped-and-purged pointer is a hard "gone" — NotFound.
        assert!(matches!(
            InfinoError::from(ManifestLoadError::PointerVanished),
            InfinoError::NotFound(_)
        ));
        // A storage fault reading the manifest is transient I/O, so a caller
        // can retry — Io (a retryable status at the serving layer), not a hard
        // backend fault.
        assert!(matches!(
            InfinoError::from(ManifestLoadError::Storage(
                StorageError::TransientExhausted {
                    uri: "p".into(),
                    source: "blip".into(),
                }
            )),
            InfinoError::Io(_)
        ));
    }

    #[test]
    fn over_budget_routes_through_wrappers() {
        // A budget refusal nested under a wrapper (here the commit's
        // append-flush phase) still routes to OverBudget: each wrapper's
        // over_budget() delegates to the inner error's.
        let nested =
            MutationCommitError::AppendFlush(SupertableBuildError::OverBudget("deep".into()));
        assert!(matches!(
            InfinoError::from(nested),
            InfinoError::OverBudget(_)
        ));
        // A non-budget error in the same wrapper stays a generic backend error.
        assert!(matches!(
            InfinoError::from(MutationCommitError::AppendFlush(
                SupertableBuildError::NoDocsToBuild
            )),
            InfinoError::Backend(_)
        ));
    }

    /// Every CAS-loss shape a public mutation can hit must arrive as the
    /// retryable `Conflict`, not as an opaque `Backend`. One assertion per
    /// path a caller can actually reach:
    ///
    /// - `append`  → append flush → manifest OCC exhausted;
    /// - `delete`  → WAL state-doc CAS lost;
    /// - `delete`  → tombstone-sidecar CAS budget exhausted;
    /// - `update`  → append phase's manifest commit lost the race.
    #[test]
    fn cas_loss_maps_to_conflict_on_every_mutation_path() {
        // The commit layer's own OCC exhaustion.
        assert!(matches!(
            InfinoError::from(SupertableCommitError::WriteContentionExhausted),
            InfinoError::Conflict(_)
        ));
        // Commit → build conversion keeps the contention typed rather than
        // stringifying it into `Store`, which is what let it read as a
        // backend fault before.
        assert!(matches!(
            SupertableBuildError::from(SupertableCommitError::WriteContentionExhausted),
            SupertableBuildError::WriteContention
        ));
        // `append`: writer flush → commit → OCC exhausted.
        assert!(matches!(
            InfinoError::from(MutationCommitError::AppendFlush(
                SupertableBuildError::WriteContention
            )),
            InfinoError::Conflict(_)
        ));
        // `delete`: the WAL state doc lost its CAS mid-commit.
        assert!(matches!(
            InfinoError::from(MutationCommitError::PartialCommit {
                committed_wal_ids: Vec::new(),
                committed: 0,
                total: 1,
                cause: Box::new(MutationError::WalStore(WalStoreError::CasFailed {
                    path: "wal/mutations/1.json".into()
                })),
            }),
            InfinoError::Conflict(_)
        ));
        // `delete`: the per-superfile tombstone sidecar CAS budget ran out.
        assert!(matches!(
            InfinoError::from(MutationError::TombstonePhase(
                TombstonePhaseError::CasRetryExhausted {
                    superfile_id: Uuid::nil(),
                    attempts: 8,
                }
            )),
            InfinoError::Conflict(_)
        ));
        // `update`: the append phase's manifest commit lost the race.
        assert!(matches!(
            InfinoError::from(MutationError::AppendPhase(
                AppendPhaseError::ManifestCommit(Box::new(
                    SupertableCommitError::WriteContentionExhausted
                ))
            )),
            InfinoError::Conflict(_)
        ));
        // A raw storage precondition failure anywhere under a mutation.
        assert!(matches!(
            InfinoError::from(MutationError::Storage(StorageError::PreconditionFailed {
                uri: "u".into()
            })),
            InfinoError::Conflict(_)
        ));
        // Open bootstraps through the same CAS-fenced commit.
        assert!(matches!(
            InfinoError::from(OpenError::Commit(
                SupertableCommitError::WriteContentionExhausted
            )),
            InfinoError::Conflict(_)
        ));
    }

    /// The classifier has to stay narrow: failures that retrying cannot fix
    /// must keep their existing variants.
    #[test]
    fn non_cas_failures_are_not_conflicts() {
        // A duplicate WAL id is a create collision, not a lost race.
        assert!(matches!(
            InfinoError::from(MutationError::WalStore(WalStoreError::AlreadyExists {
                path: "wal/mutations/1.json".into()
            })),
            InfinoError::Backend(_)
        ));
        assert!(matches!(
            InfinoError::from(MutationError::TombstonePhase(
                TombstonePhaseError::IdLookupFailed {
                    target_id: "7".into(),
                    message: "boom".into(),
                }
            )),
            InfinoError::Backend(_)
        ));
        assert!(matches!(
            InfinoError::from(SupertableCommitError::Encode("e".into())),
            InfinoError::Backend(_)
        ));
        // A vanished pointer still outranks the conflict check.
        assert!(matches!(
            InfinoError::from(SupertableCommitError::PointerVanished),
            InfinoError::NotFound(_)
        ));
    }

    #[test]
    fn from_mutation_error_maps_each_arm() {
        assert!(matches!(
            InfinoError::from(MutationError::PredicateEval(QueryError::Plan("p".into()))),
            InfinoError::Query(_)
        ));
        assert!(matches!(
            InfinoError::from(MutationError::Storage(StorageError::NotFound {
                uri: "u".into()
            })),
            InfinoError::NotFound(_)
        ));
        assert!(matches!(
            InfinoError::from(MutationError::CardinalityMismatch {
                matched: 1,
                new_rows: 2
            }),
            InfinoError::Cardinality(_)
        ));
        assert!(matches!(
            InfinoError::from(MutationError::MatchCountExceedsCap { matched: 9, cap: 5 }),
            InfinoError::Cardinality(_)
        ));
        assert!(matches!(
            InfinoError::from(MutationError::SchemaMismatch("s".into())),
            InfinoError::Schema(_)
        ));
        assert!(matches!(
            InfinoError::from(MutationError::NoStorageAttached),
            InfinoError::Backend(_)
        ));
    }
}