krishiv-sql 0.1.0-nightly.202607030148

Krishiv — hybrid batch and streaming compute engine
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! File-system (Hadoop-style) Iceberg catalog (Phase J1).
//!
//! [`LocalCatalog`] implements [`iceberg::Catalog`] over a local warehouse
//! directory.  It is intended for development and embedded single-process use
//! where a network catalog (REST / Postgres) is not warranted but real
//! Iceberg-spec metadata on disk is still required.
//!
//! # Layout
//!
//! ```text
//! warehouse/
//!   {namespace}/
//!     {table}/
//!       metadata/
//!         00000-<uuid>.metadata.json
//!         version-hint.text       # absolute metadata-location of the latest commit
//!       data/
//!         *.parquet
//! ```
//!
//! All Iceberg-spec metadata writes (manifests, manifest lists, table-metadata
//! JSON) are delegated to an inner [`iceberg::MemoryCatalog`] backed by
//! [`LocalFsStorageFactory`], which already produces correct on-disk Iceberg
//! files.  The only thing the memory catalog does *not* do is survive a process
//! restart: it keeps the namespace → table → metadata-location registry in RAM.
//!
//! `LocalCatalog` adds durability on top of that by writing a
//! `version-hint.text` next to every table's metadata and, on construction,
//! scanning the warehouse and re-registering every table it finds back into the
//! in-memory registry.  This mirrors the recovery logic already used by
//! `krishiv_connectors::lakehouse::IcebergNativeTwoPhaseCommit`.

#![cfg(feature = "local-catalog")]

use std::collections::HashMap;
use std::fs;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use async_trait::async_trait;
use iceberg::io::LocalFsStorageFactory;
use iceberg::memory::{MEMORY_CATALOG_WAREHOUSE, MemoryCatalog, MemoryCatalogBuilder};
use iceberg::table::Table;
use iceberg::{
    Catalog, CatalogBuilder, Namespace, NamespaceIdent, Result as IcebergResult, TableCommit,
    TableCreation, TableIdent,
};
use krishiv_common::validate::validate_safe_id;

use crate::catalog::LakehouseError;

const VERSION_HINT: &str = "version-hint.text";
const METADATA_DIR: &str = "metadata";

/// File-system backed Iceberg catalog rooted at a local warehouse directory.
#[derive(Debug)]
pub struct LocalCatalog {
    inner: Arc<MemoryCatalog>,
    warehouse: PathBuf,
}

impl LocalCatalog {
    /// Open (or initialise) a file-system catalog rooted at `warehouse`.
    ///
    /// The directory is created if it does not exist.  Any tables already
    /// present under the warehouse (detected via their `version-hint.text`
    /// files) are re-registered so the catalog is usable immediately after a
    /// restart.
    pub async fn new(warehouse: &Path) -> Result<Self, LakehouseError> {
        fs::create_dir_all(warehouse).map_err(|e| LakehouseError::Io(e.to_string()))?;
        let warehouse = warehouse
            .canonicalize()
            .map_err(|e| LakehouseError::Io(e.to_string()))?;

        let warehouse_uri = path_to_uri(&warehouse)?;

        let inner = MemoryCatalogBuilder::default()
            .with_storage_factory(Arc::new(LocalFsStorageFactory))
            .load(
                "local",
                HashMap::from([(MEMORY_CATALOG_WAREHOUSE.to_string(), warehouse_uri)]),
            )
            .await
            .map_err(|e| LakehouseError::Iceberg(e.to_string()))?;
        let inner = Arc::new(inner);

        let catalog = Self {
            inner,
            warehouse: warehouse.clone(),
        };
        catalog.recover_from_disk().await?;
        Ok(catalog)
    }

    /// The warehouse root directory.
    pub fn warehouse(&self) -> &Path {
        &self.warehouse
    }

    /// Default table location (`file://` URI) for `{namespace}/{table}`.
    fn table_location_uri(
        &self,
        namespace: &NamespaceIdent,
        table: &str,
    ) -> Result<String, LakehouseError> {
        let dir = self.table_dir(namespace, table)?;
        // The directory must exist for canonicalize-free URI formatting; create it.
        fs::create_dir_all(&dir).map_err(|e| LakehouseError::Io(e.to_string()))?;
        path_to_uri(&dir)
    }

    fn table_dir(
        &self,
        namespace: &NamespaceIdent,
        table: &str,
    ) -> Result<PathBuf, LakehouseError> {
        validate_namespace(namespace)?;
        validate_path_component("table name", table)?;
        let mut dir = self.warehouse.clone();
        for part in namespace.clone().inner() {
            dir.push(part);
        }
        dir.push(table);
        Ok(dir)
    }

    /// Local metadata directory for `{namespace}/{table}`.
    fn table_metadata_dir(
        &self,
        namespace: &NamespaceIdent,
        table: &str,
    ) -> Result<PathBuf, LakehouseError> {
        let mut dir = self.table_dir(namespace, table)?;
        dir.push(METADATA_DIR);
        Ok(dir)
    }

    /// Persist the latest metadata location for a table to `version-hint.text`.
    fn write_version_hint(
        &self,
        namespace: &NamespaceIdent,
        table: &str,
        metadata_location: &str,
    ) -> Result<(), LakehouseError> {
        let dir = self.table_metadata_dir(namespace, table)?;
        fs::create_dir_all(&dir).map_err(|e| LakehouseError::Io(e.to_string()))?;
        fs::write(dir.join(VERSION_HINT), metadata_location)
            .map_err(|e| LakehouseError::Io(e.to_string()))
    }

    /// Scan the warehouse directory and re-register every table found.
    ///
    /// A directory is treated as a table when it contains
    /// `metadata/version-hint.text`.  Its parent path (relative to the
    /// warehouse) is the namespace.  Namespaces are created idempotently before
    /// their tables are registered.
    async fn recover_from_disk(&self) -> Result<(), LakehouseError> {
        let mut discovered: Vec<(NamespaceIdent, String, String)> = Vec::new();
        discover_tables(&self.warehouse, &self.warehouse, &mut discovered)?;

        for (namespace, table_name, metadata_location) in discovered {
            // Create namespace chain (idempotent).
            let _ = self
                .inner
                .create_namespace(&namespace, HashMap::new())
                .await;
            let ident = TableIdent::new(namespace, table_name);
            // Register only if not already known (defensive against double scan).
            if !self.inner.table_exists(&ident).await.unwrap_or(false) {
                self.inner
                    .register_table(&ident, metadata_location)
                    .await
                    .map_err(|e| LakehouseError::Iceberg(e.to_string()))?;
            }
        }
        Ok(())
    }
}

#[async_trait]
impl Catalog for LocalCatalog {
    async fn list_namespaces(
        &self,
        parent: Option<&NamespaceIdent>,
    ) -> IcebergResult<Vec<NamespaceIdent>> {
        self.inner.list_namespaces(parent).await
    }

    async fn create_namespace(
        &self,
        namespace: &NamespaceIdent,
        properties: HashMap<String, String>,
    ) -> IcebergResult<Namespace> {
        // Materialise the namespace directory so the layout is observable on disk
        // even before any table is created.
        validate_namespace(namespace).map_err(to_iceberg_err)?;
        let mut dir = self.warehouse.clone();
        for part in namespace.clone().inner() {
            dir.push(part);
        }
        fs::create_dir_all(&dir).map_err(|e| to_iceberg_err(LakehouseError::Io(e.to_string())))?;
        self.inner.create_namespace(namespace, properties).await
    }

    async fn get_namespace(&self, namespace: &NamespaceIdent) -> IcebergResult<Namespace> {
        self.inner.get_namespace(namespace).await
    }

    async fn namespace_exists(&self, namespace: &NamespaceIdent) -> IcebergResult<bool> {
        self.inner.namespace_exists(namespace).await
    }

    async fn update_namespace(
        &self,
        namespace: &NamespaceIdent,
        properties: HashMap<String, String>,
    ) -> IcebergResult<()> {
        self.inner.update_namespace(namespace, properties).await
    }

    async fn drop_namespace(&self, namespace: &NamespaceIdent) -> IcebergResult<()> {
        self.inner.drop_namespace(namespace).await
    }

    async fn list_tables(&self, namespace: &NamespaceIdent) -> IcebergResult<Vec<TableIdent>> {
        self.inner.list_tables(namespace).await
    }

    async fn create_table(
        &self,
        namespace: &NamespaceIdent,
        creation: TableCreation,
    ) -> IcebergResult<Table> {
        // Ensure an explicit location under the warehouse so on-disk layout is
        // deterministic (namespace/table) rather than catalog-default.
        let creation = if creation.location.is_some() {
            creation
        } else {
            let location = self
                .table_location_uri(namespace, &creation.name)
                .map_err(to_iceberg_err)?;
            TableCreation {
                location: Some(location),
                ..creation
            }
        };
        let table_name = creation.name.clone();
        let table = self.inner.create_table(namespace, creation).await?;
        if let Some(loc) = table.metadata_location() {
            self.write_version_hint(namespace, &table_name, loc)
                .map_err(to_iceberg_err)?;
        }
        Ok(table)
    }

    async fn load_table(&self, table: &TableIdent) -> IcebergResult<Table> {
        self.inner.load_table(table).await
    }

    async fn drop_table(&self, table: &TableIdent) -> IcebergResult<()> {
        let dir = self
            .table_metadata_dir(table.namespace(), table.name())
            .map_err(to_iceberg_err)?;
        self.inner.drop_table(table).await?;
        // Remove the version hint so a later recovery does not resurrect the table.
        remove_file_if_exists(&dir.join(VERSION_HINT)).map_err(to_iceberg_err)?;
        Ok(())
    }

    async fn table_exists(&self, table: &TableIdent) -> IcebergResult<bool> {
        self.inner.table_exists(table).await
    }

    async fn rename_table(&self, src: &TableIdent, dest: &TableIdent) -> IcebergResult<()> {
        let src_hint = self
            .table_metadata_dir(src.namespace(), src.name())
            .map_err(to_iceberg_err)?
            .join(VERSION_HINT);
        // Validate the destination path before mutating the in-memory catalog.
        self.table_metadata_dir(dest.namespace(), dest.name())
            .map_err(to_iceberg_err)?;

        self.inner.rename_table(src, dest).await?;
        // Mirror the version hint to the destination metadata dir for recovery.
        let table = self.inner.load_table(dest).await?;
        let loc = table.metadata_location().ok_or_else(|| {
            iceberg::Error::new(
                iceberg::ErrorKind::Unexpected,
                format!(
                    "renamed table {}.{} has no metadata location",
                    dest.namespace().clone().inner().join("."),
                    dest.name()
                ),
            )
        })?;
        self.write_version_hint(dest.namespace(), dest.name(), loc)
            .map_err(to_iceberg_err)?;
        remove_file_if_exists(&src_hint).map_err(to_iceberg_err)?;
        Ok(())
    }

    async fn register_table(
        &self,
        table: &TableIdent,
        metadata_location: String,
    ) -> IcebergResult<Table> {
        let registered = self
            .inner
            .register_table(table, metadata_location.clone())
            .await?;
        self.write_version_hint(table.namespace(), table.name(), &metadata_location)
            .map_err(to_iceberg_err)?;
        Ok(registered)
    }

    async fn update_table(&self, commit: TableCommit) -> IcebergResult<Table> {
        let ident = commit.identifier().clone();
        let updated = self.inner.update_table(commit).await?;
        if let Some(loc) = updated.metadata_location() {
            self.write_version_hint(ident.namespace(), ident.name(), loc)
                .map_err(to_iceberg_err)?;
        }
        Ok(updated)
    }
}

// ── helpers ─────────────────────────────────────────────────────────────────

fn to_iceberg_err(e: LakehouseError) -> iceberg::Error {
    iceberg::Error::new(iceberg::ErrorKind::Unexpected, e.to_string())
}

fn validate_namespace(namespace: &NamespaceIdent) -> Result<(), LakehouseError> {
    for part in namespace.clone().inner() {
        validate_path_component("namespace component", &part)?;
    }
    Ok(())
}

fn validate_path_component(label: &str, value: &str) -> Result<(), LakehouseError> {
    validate_safe_id(value, label).map_err(|error| LakehouseError::Io(error.to_string()))
}

fn remove_file_if_exists(path: &Path) -> Result<(), LakehouseError> {
    match fs::remove_file(path) {
        Ok(()) => Ok(()),
        Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
        Err(error) => Err(LakehouseError::Io(error.to_string())),
    }
}

/// Convert an absolute local path to a `file://` URI.
fn path_to_uri(path: &Path) -> Result<String, LakehouseError> {
    url::Url::from_file_path(path)
        .map(|u| u.to_string())
        .map_err(|()| LakehouseError::Io(format!("cannot convert path to URI: {}", path.display())))
}

/// Recursively walk `dir`, collecting `(namespace, table_name, metadata_location)`
/// for every directory that contains `metadata/version-hint.text`.
fn discover_tables(
    warehouse_root: &Path,
    dir: &Path,
    out: &mut Vec<(NamespaceIdent, String, String)>,
) -> Result<(), LakehouseError> {
    let hint = dir.join(METADATA_DIR).join(VERSION_HINT);
    if hint.is_file() {
        // `dir` is a table directory. Its path relative to the warehouse root is
        // {namespace parts...}/{table}.
        let rel = dir
            .strip_prefix(warehouse_root)
            .map_err(|e| LakehouseError::Io(e.to_string()))?;
        let parts: Vec<String> = rel
            .components()
            .map(|c| c.as_os_str().to_string_lossy().into_owned())
            .collect();
        if let Some((table_name, ns_parts)) = parts.split_last()
            && !ns_parts.is_empty()
        {
            let namespace = NamespaceIdent::from_vec(ns_parts.to_vec())
                .map_err(|e| LakehouseError::Iceberg(e.to_string()))?;
            let metadata_location = fs::read_to_string(&hint)
                .map_err(|e| LakehouseError::Io(e.to_string()))?
                .trim()
                .to_string();
            if !metadata_location.is_empty() {
                out.push((namespace, table_name.clone(), metadata_location));
            }
        }
        // A table directory is a leaf for discovery purposes.
        return Ok(());
    }

    // Otherwise recurse into subdirectories.
    let entries = match fs::read_dir(dir) {
        Ok(e) => e,
        Err(_) => return Ok(()),
    };
    for entry in entries.flatten() {
        let path = entry.path();
        if path.is_dir() {
            // Skip the conventional data/metadata dirs at non-table levels.
            let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
            if name == METADATA_DIR || name == "data" {
                continue;
            }
            discover_tables(warehouse_root, &path, out)?;
        }
    }
    Ok(())
}

// ── tests ─────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use iceberg::spec::{NestedField, PrimitiveType, Schema, Type};

    fn sample_schema() -> Schema {
        Schema::builder()
            .with_schema_id(0)
            .with_fields(vec![
                Arc::new(NestedField::required(
                    1,
                    "id",
                    Type::Primitive(PrimitiveType::Long),
                )),
                Arc::new(NestedField::optional(
                    2,
                    "name",
                    Type::Primitive(PrimitiveType::String),
                )),
            ])
            .build()
            .unwrap()
    }

    async fn create_table(catalog: &LocalCatalog, ns: &str, table: &str) -> Table {
        let namespace = NamespaceIdent::new(ns.to_string());
        let _ = catalog.create_namespace(&namespace, HashMap::new()).await;
        let creation = TableCreation::builder()
            .name(table.to_string())
            .schema(sample_schema())
            .build();
        catalog.create_table(&namespace, creation).await.unwrap()
    }

    #[tokio::test]
    async fn local_catalog_create_and_load_table() {
        let dir = tempfile::tempdir().unwrap();
        let catalog = LocalCatalog::new(dir.path()).await.unwrap();

        let created = create_table(&catalog, "sales", "orders").await;
        assert_eq!(created.identifier().name(), "orders");

        let ident = TableIdent::new(
            NamespaceIdent::new("sales".to_string()),
            "orders".to_string(),
        );
        let loaded = catalog.load_table(&ident).await.unwrap();
        assert_eq!(
            loaded
                .metadata()
                .current_schema()
                .as_ref()
                .field_id_by_name("id"),
            Some(1)
        );
        assert_eq!(
            loaded
                .metadata()
                .current_schema()
                .as_ref()
                .field_id_by_name("name"),
            Some(2)
        );

        // version-hint.text must have been written.
        let hint = catalog
            .table_metadata_dir(&NamespaceIdent::new("sales".to_string()), "orders")
            .unwrap()
            .join(VERSION_HINT);
        assert!(hint.is_file(), "version-hint.text should be persisted");
    }

    #[tokio::test]
    async fn local_catalog_list_namespaces() {
        let dir = tempfile::tempdir().unwrap();
        let catalog = LocalCatalog::new(dir.path()).await.unwrap();

        catalog
            .create_namespace(&NamespaceIdent::new("alpha".to_string()), HashMap::new())
            .await
            .unwrap();
        catalog
            .create_namespace(&NamespaceIdent::new("beta".to_string()), HashMap::new())
            .await
            .unwrap();

        let mut names: Vec<String> = catalog
            .list_namespaces(None)
            .await
            .unwrap()
            .into_iter()
            .map(|n| n.inner().join("."))
            .collect();
        names.sort();
        assert_eq!(names, vec!["alpha", "beta"]);
    }

    #[tokio::test]
    async fn local_catalog_list_tables() {
        let dir = tempfile::tempdir().unwrap();
        let catalog = LocalCatalog::new(dir.path()).await.unwrap();

        create_table(&catalog, "sales", "orders").await;
        create_table(&catalog, "sales", "customers").await;

        let namespace = NamespaceIdent::new("sales".to_string());
        let mut tables: Vec<String> = catalog
            .list_tables(&namespace)
            .await
            .unwrap()
            .into_iter()
            .map(|t| t.name().to_string())
            .collect();
        tables.sort();
        assert_eq!(tables, vec!["customers", "orders"]);
    }

    #[tokio::test]
    async fn local_catalog_recovers_tables_after_restart() {
        let dir = tempfile::tempdir().unwrap();

        // Session 1: create a table.
        {
            let catalog = LocalCatalog::new(dir.path()).await.unwrap();
            create_table(&catalog, "sales", "orders").await;
        }

        // Session 2: a fresh catalog over the same warehouse must rediscover it.
        {
            let catalog = LocalCatalog::new(dir.path()).await.unwrap();
            let namespace = NamespaceIdent::new("sales".to_string());
            let tables = catalog.list_tables(&namespace).await.unwrap();
            assert_eq!(tables.len(), 1, "table should survive a restart");
            assert_eq!(tables[0].name(), "orders");
            // And it must be loadable.
            let loaded = catalog.load_table(&tables[0]).await.unwrap();
            assert!(
                loaded
                    .metadata()
                    .current_schema()
                    .as_ref()
                    .field_id_by_name("id")
                    .is_some()
            );
        }
    }

    #[tokio::test]
    async fn local_catalog_drop_table_removes_it() {
        let dir = tempfile::tempdir().unwrap();
        let catalog = LocalCatalog::new(dir.path()).await.unwrap();
        create_table(&catalog, "sales", "orders").await;

        let ident = TableIdent::new(
            NamespaceIdent::new("sales".to_string()),
            "orders".to_string(),
        );
        assert!(catalog.table_exists(&ident).await.unwrap());
        catalog.drop_table(&ident).await.unwrap();
        assert!(!catalog.table_exists(&ident).await.unwrap());
    }

    #[tokio::test]
    async fn local_catalog_rejects_path_traversal_identifiers() {
        let dir = tempfile::tempdir().unwrap();
        let catalog = LocalCatalog::new(dir.path()).await.unwrap();

        let bad_ns = NamespaceIdent::new("..".to_string());
        assert!(catalog.table_location_uri(&bad_ns, "orders").is_err());

        let good_ns = NamespaceIdent::new("sales".to_string());
        assert!(catalog.table_location_uri(&good_ns, "../orders").is_err());
        assert!(catalog.table_metadata_dir(&good_ns, "orders/2026").is_err());
    }
}