hirn-storage 0.1.0

Cognitive storage engine for hirn — lance 4.0 + lance-namespace
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
use std::sync::Arc;

use arrow_array::RecordBatch;
use arrow_schema::SchemaRef;
use async_trait::async_trait;
use datafusion::catalog::TableProvider;

use crate::error::HirnDbError;
use crate::reranker::Reranker;

// ── Distance Metrics ──

/// Re-exported from `hirn-core` — single canonical definition across the codebase.
pub use hirn_core::DistanceMetric;

// ── Normalize Method ──

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum NormalizeMethod {
    #[default]
    Score,
    Rank,
}

// ── Index Types ──

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IndexType {
    IvfHnswSq,
    IvfHnswPq,
    IvfPq,
    IvfRq,
    Bm25,
    BTree,
    Bitmap,
    LabelList,
}

// ── Index Parameters ──

#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)]
pub struct IndexParams {
    pub num_partitions: Option<u32>,
    pub num_sub_vectors: Option<u32>,
    pub num_edges: Option<u32>,
    pub ef_construction: Option<u32>,
    pub sample_rate: Option<u32>,
    pub num_bits: Option<u32>,
}

// ── Index Config ──

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexConfig {
    pub columns: Vec<String>,
    pub index_type: IndexType,
    pub params: IndexParams,
    pub replace: bool,
}

// ── Scan Ordering ──

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScanOrdering {
    pub column: String,
    pub ascending: bool,
    pub nulls_first: bool,
}

impl ScanOrdering {
    #[must_use]
    pub fn asc(column: impl Into<String>) -> Self {
        Self {
            column: column.into(),
            ascending: true,
            nulls_first: false,
        }
    }

    #[must_use]
    pub fn desc(column: impl Into<String>) -> Self {
        Self {
            column: column.into(),
            ascending: false,
            nulls_first: false,
        }
    }
}

// ── Scan Options ──

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExactMatchFilter {
    Utf8In {
        column: String,
        values: Vec<String>,
    },
    /// Matches rows where `column_a = value` OR `column_b = value`.
    /// Used for bidirectional edge lookup (source OR target equals a given id).
    Utf8MultiColumnOr {
        columns: Vec<String>,
        value: String,
    },
}

impl ExactMatchFilter {
    /// Validate that a column name is safe to interpolate into a SQL predicate.
    ///
    /// Column names in hirn are always statically known lowercase snake_case
    /// identifiers. This assertion ensures no user-controlled string can reach
    /// the SQL interpolation path and create an injection vector.
    fn assert_safe_column(col: &str) {
        debug_assert!(
            !col.is_empty()
                && col.len() <= 64
                && col
                    .chars()
                    .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_'),
            "column name '{col}' contains unsafe characters — only [a-z0-9_] are allowed"
        );
    }

    #[must_use]
    pub fn utf8_value(column: impl Into<String>, value: impl Into<String>) -> Self {
        let column = column.into();
        Self::assert_safe_column(&column);
        Self::Utf8In {
            column,
            values: vec![value.into()],
        }
    }

    #[must_use]
    pub fn utf8_values<I, S>(column: impl Into<String>, values: I) -> Option<Self>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let values: Vec<String> = values.into_iter().map(Into::into).collect();
        if values.is_empty() {
            return None;
        }

        let column = column.into();
        Self::assert_safe_column(&column);
        Some(Self::Utf8In {
            column,
            values,
        })
    }

    #[must_use]
    pub fn utf8_multi_column_or(columns: Vec<String>, value: impl Into<String>) -> Self {
        for col in &columns {
            Self::assert_safe_column(col);
        }
        Self::Utf8MultiColumnOr {
            columns,
            value: value.into(),
        }
    }

    #[must_use]
    pub fn to_predicate_sql(&self) -> String {
        match self {
            Self::Utf8In { column, values } => {
                if values.is_empty() {
                    return "1 = 0".to_string();
                }

                let in_list = values
                    .iter()
                    .map(|value| format!("'{}'", value.replace('\'', "''")))
                    .collect::<Vec<_>>()
                    .join(", ");
                format!("{column} IN ({in_list})")
            }
            Self::Utf8MultiColumnOr { columns, value } => {
                if columns.is_empty() {
                    return "1 = 0".to_string();
                }
                let escaped = value.replace('\'', "''");
                columns
                    .iter()
                    .map(|col| format!("{col} = '{escaped}'"))
                    .collect::<Vec<_>>()
                    .join(" OR ")
            }
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct ScanOptions {
    pub filter: Option<String>,
    pub exact_filter: Option<ExactMatchFilter>,
    pub columns: Option<Vec<String>>,
    pub order_by: Option<Vec<ScanOrdering>>,
    pub limit: Option<usize>,
    pub offset: Option<usize>,
}

// ── Vector Search Options ──

#[derive(Debug, Clone)]
pub struct VectorSearchOptions {
    pub column: String,
    pub query: Vec<f32>,
    pub metric: DistanceMetric,
    pub limit: usize,
    pub filter: Option<String>,
    pub nprobes: Option<usize>,
    pub refine_factor: Option<u32>,
}

impl Default for VectorSearchOptions {
    fn default() -> Self {
        Self {
            column: String::new(),
            query: Vec::new(),
            metric: DistanceMetric::default(),
            limit: 10,
            filter: None,
            nprobes: None,
            refine_factor: None,
        }
    }
}

// ── FTS Search Options ──

#[derive(Debug, Clone)]
pub struct FtsSearchOptions {
    pub columns: Vec<String>,
    pub query: String,
    pub limit: usize,
    pub filter: Option<String>,
}

// ── Hybrid Search Options ──

#[derive(Clone)]
pub struct HybridSearchOptions {
    pub vector_column: String,
    pub query_vector: Vec<f32>,
    pub fts_columns: Vec<String>,
    pub fts_query: String,
    pub normalize: NormalizeMethod,
    pub metric: DistanceMetric,
    pub limit: usize,
    pub filter: Option<String>,
    /// Optional reranker. Defaults to [`RRFReranker`](crate::reranker::RRFReranker) if `None`.
    pub reranker: Option<Arc<dyn Reranker>>,
}

impl std::fmt::Debug for HybridSearchOptions {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HybridSearchOptions")
            .field("vector_column", &self.vector_column)
            .field("fts_columns", &self.fts_columns)
            .field("fts_query", &self.fts_query)
            .field("normalize", &self.normalize)
            .field("metric", &self.metric)
            .field("limit", &self.limit)
            .field("filter", &self.filter)
            .field("reranker", &self.reranker.as_ref().map(|_| ".."))
            .finish()
    }
}

// ── Multivector Search ──

#[derive(Debug, Clone)]
pub enum MultivectorQuery {
    Single(Vec<f32>),
    Multi(Vec<Vec<f32>>),
}

#[derive(Debug, Clone)]
pub struct MultivectorSearchOptions {
    /// Multivector column (`List<FixedSizeList<Float32>>`) for MaxSim scoring.
    pub column: String,
    pub query: MultivectorQuery,
    pub metric: DistanceMetric,
    pub limit: usize,
    pub filter: Option<String>,
    /// Optional dense embedding column for first-stage ANN retrieval.
    /// When set, enables two-stage search: ANN over this column → MaxSim
    /// re-scoring using `column`. When `None`, falls back to brute-force scan.
    pub dense_column: Option<String>,
    /// Number of candidates to retrieve in the first stage (default: `limit * 10`).
    pub first_stage_limit: Option<usize>,
}

// ── Compact Options / Result ──

#[derive(Debug, Clone, Default)]
pub struct CompactOptions {
    pub max_rows_per_group: Option<usize>,
    pub target_rows_per_fragment: Option<usize>,
}

#[derive(Debug, Clone, Default)]
pub struct CompactResult {
    pub fragments_removed: u64,
    pub fragments_added: u64,
    pub rows_removed: u64,
}

// ── Version Tag ──

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VersionTag {
    pub name: String,
    pub version: u64,
    pub created_at: i64,
}

// ── Dataset Info ──

#[derive(Debug, Clone)]
pub struct DatasetInfo {
    pub name: String,
    pub version: u64,
    pub row_count: u64,
    pub schema: SchemaRef,
}

pub type RecordBatchStream =
    std::pin::Pin<Box<dyn futures::Stream<Item = Result<RecordBatch, HirnDbError>> + Send>>;

// ── Column Transform ──

#[derive(Debug, Clone)]
pub enum ColumnTransform {
    AddColumn {
        name: String,
        data_type: arrow_schema::DataType,
        nullable: bool,
        default_value: Option<String>,
    },
    RenameColumn {
        old_name: String,
        new_name: String,
    },
}

// ── PhysicalStore Trait ──

/// Physical storage operations on Lance datasets.
///
/// `LancePhysicalStore` implements this directly against lance 4.0 Dataset + LanceNamespace.
/// `MemoryStore` implements this for tests with real Arrow data, brute-force search, etc.
#[async_trait]
pub trait PhysicalStore: Send + Sync {
    // ── CRUD ──

    /// Append rows to a dataset. Creates the dataset if it doesn't exist.
    async fn append(&self, dataset: &str, batch: RecordBatch) -> Result<(), HirnDbError>;

    /// Append multiple record batches in one logical storage operation.
    async fn append_batches(
        &self,
        dataset: &str,
        batches: Vec<RecordBatch>,
    ) -> Result<(), HirnDbError>;

    /// Append a streaming sequence of record batches to a dataset.
    ///
    /// Batches are buffered up to `MAX_STREAM_BATCH_ROWS` rows before each
    /// flush to `append_batches`, bounding peak memory for large streams.
    /// This is the correct API for pipeline or operator-driven writes where
    /// the total row count is not known up front.
    ///
    /// The default implementation collects bounded buffers and calls
    /// `append_batches`. Store implementations may override to stream
    /// directly into the underlying storage engine without intermediate
    /// materialization.
    async fn append_stream(
        &self,
        dataset: &str,
        mut stream: RecordBatchStream,
    ) -> Result<(), HirnDbError> {
        use futures::StreamExt as _;
        const MAX_STREAM_BATCH_ROWS: usize = 50_000;
        let mut buffer: Vec<RecordBatch> = Vec::new();
        let mut buffered_rows: usize = 0;
        while let Some(result) = stream.next().await {
            let batch = result?;
            if batch.num_rows() == 0 {
                continue;
            }
            buffered_rows += batch.num_rows();
            buffer.push(batch);
            if buffered_rows >= MAX_STREAM_BATCH_ROWS {
                self.append_batches(dataset, std::mem::take(&mut buffer))
                    .await?;
                buffered_rows = 0;
            }
        }
        if !buffer.is_empty() {
            self.append_batches(dataset, buffer).await?;
        }
        Ok(())
    }

    /// Scan with predicate pushdown, projection, and optional limit/offset.
    async fn scan(&self, dataset: &str, opts: ScanOptions)
    -> Result<Vec<RecordBatch>, HirnDbError>;

    /// Stream batches incrementally instead of materializing the whole scan.
    async fn scan_stream(
        &self,
        dataset: &str,
        opts: ScanOptions,
    ) -> Result<RecordBatchStream, HirnDbError>;

    /// Delete rows by predicate. Returns count of deleted rows.
    ///
    /// # Security note
    /// This method accepts a raw SQL predicate string. All callers **must** ensure
    /// values are constructed from system-generated identifiers (ULIDs, integers) or
    /// properly escaped via `str::replace('\'', "''")`. Prefer [`Self::delete_exact`]
    /// for single-column exact matches.
    #[doc(hidden)]
    async fn delete(&self, dataset: &str, predicate: &str) -> Result<u64, HirnDbError>;

    /// Delete rows by structured exact-match filter. Returns count of deleted rows.
    async fn delete_exact(
        &self,
        dataset: &str,
        filter: &ExactMatchFilter,
    ) -> Result<u64, HirnDbError> {
        let predicate = filter.to_predicate_sql();
        self.delete(dataset, &predicate).await
    }

    /// Merge-insert (upsert): insert new rows, update matching rows.
    async fn merge_insert(
        &self,
        dataset: &str,
        on: &[&str],
        batch: RecordBatch,
    ) -> Result<(), HirnDbError>;

    /// Targeted in-place column update.
    ///
    /// Executes a narrow `SET col = expr [, …] WHERE filter` statement — no
    /// full-row read-modify-write.  `updates` is a slice of `(column, sql_expr)`
    /// pairs where `sql_expr` is a SQL literal or expression understood by the
    /// backing store (e.g. `"true"`, `"'hello'"`, `"42"`).
    ///
    /// This avoids the RMW race inherent in scan → modify → merge_insert.
    async fn update_where(
        &self,
        dataset: &str,
        filter: &str,
        updates: &[(&str, &str)],
    ) -> Result<u64, HirnDbError>;

    /// Count rows (optionally filtered). Uses fast metadata path when no filter.
    async fn count(&self, dataset: &str, filter: Option<&str>) -> Result<u64, HirnDbError>;

    // ── Search ──

    /// Vector ANN search.
    async fn vector_search(
        &self,
        dataset: &str,
        opts: VectorSearchOptions,
    ) -> Result<Vec<RecordBatch>, HirnDbError>;

    /// Batched vector ANN search preserving query order.
    async fn vector_search_many(
        &self,
        dataset: &str,
        queries: Vec<VectorSearchOptions>,
    ) -> Result<Vec<Vec<RecordBatch>>, HirnDbError>;

    /// Full-text search (BM25).
    async fn fts_search(
        &self,
        dataset: &str,
        opts: FtsSearchOptions,
    ) -> Result<Vec<RecordBatch>, HirnDbError>;

    /// Hybrid search (vector + FTS fusion with configurable reranker + normalization).
    async fn hybrid_search(
        &self,
        dataset: &str,
        opts: HybridSearchOptions,
    ) -> Result<Vec<RecordBatch>, HirnDbError>;

    /// Multivector search (ColBERT/ColPaLi-style late interaction with MaxSim).
    async fn multivector_search(
        &self,
        dataset: &str,
        opts: MultivectorSearchOptions,
    ) -> Result<Vec<RecordBatch>, HirnDbError>;

    // ── Indexing ──

    /// Create or replace an index (vector, scalar, FTS).
    async fn create_index(&self, dataset: &str, config: IndexConfig) -> Result<(), HirnDbError>;

    /// Optimize existing indices.
    async fn optimize_indices(&self, dataset: &str) -> Result<(), HirnDbError>;

    // ── Compaction ──

    /// Compact fragments + prune deleted rows.
    async fn compact(
        &self,
        dataset: &str,
        opts: CompactOptions,
    ) -> Result<CompactResult, HirnDbError>;

    // ── Versioning ──

    /// Get current dataset version.
    async fn version(&self, dataset: &str) -> Result<u64, HirnDbError>;

    /// Snapshot (tag) the current version.
    async fn tag(&self, dataset: &str, tag: &str) -> Result<(), HirnDbError>;

    /// Checkout a historical version (read-only).
    async fn checkout(&self, dataset: &str, version: u64) -> Result<(), HirnDbError>;

    /// List all tags.
    async fn list_tags(&self, dataset: &str) -> Result<Vec<VersionTag>, HirnDbError>;

    // ── Dataset management ──

    /// List all datasets in the current namespace.
    async fn list_datasets(&self) -> Result<Vec<DatasetInfo>, HirnDbError>;

    /// Check existence.
    async fn exists(&self, dataset: &str) -> Result<bool, HirnDbError>;

    // ── Namespace ──

    /// List sub-namespaces.
    async fn list_namespaces(&self) -> Result<Vec<String>, HirnDbError>;

    /// Create a new namespace.
    async fn create_namespace(&self, name: &str) -> Result<(), HirnDbError>;

    /// Drop a namespace and all its tables.
    async fn drop_namespace(&self, name: &str) -> Result<(), HirnDbError>;

    // ── Schema evolution ──

    /// Add columns to a dataset.
    async fn add_columns(
        &self,
        dataset: &str,
        transforms: Vec<ColumnTransform>,
    ) -> Result<(), HirnDbError>;

    /// Drop columns from a dataset.
    async fn drop_columns(&self, dataset: &str, columns: &[&str]) -> Result<(), HirnDbError>;

    // ── DataFusion Integration ──

    /// Return a DataFusion `TableProvider` for the named dataset.
    ///
    /// Lance-backed stores return a `LanceTableProvider` with native projection
    /// and filter pushdown. Non-Lance stores (e.g. `MemoryStore`) return `None`,
    /// triggering a fallback to empty `MemTable` stubs.
    ///
    /// Wrapper stores (e.g. `PolicyEnforcedStore`) delegate to their inner store.
    async fn table_provider(&self, dataset: &str) -> Option<Arc<dyn TableProvider>>;
}