cobble-table 0.5.0

Schema and table APIs for Cobble
Documentation
use super::file_catalog::CatalogRuntimeContext;
use crate::TableSchema;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
use std::sync::Arc;

/// Stable identity of a table, independent of its catalog name.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct TableId(u32);

impl TableId {
    pub(crate) fn new(value: u32) -> Self {
        Self(value)
    }

    pub fn as_u32(self) -> u32 {
        self.0
    }
}

impl Display for TableId {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.0, formatter)
    }
}

/// Per-table catalog schema identity, starting at zero and increasing monotonically.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct CatalogSchemaId(u32);

impl CatalogSchemaId {
    pub(crate) const INITIAL: Self = Self(0);

    pub(crate) fn next(self) -> Option<Self> {
        self.0.checked_add(1).map(Self)
    }

    pub fn as_u32(self) -> u32 {
        self.0
    }
}

impl From<u32> for CatalogSchemaId {
    fn from(value: u32) -> Self {
        Self(value)
    }
}

impl Display for CatalogSchemaId {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        Display::fmt(&self.0, formatter)
    }
}

/// A semantic table name with an extensible, multi-component namespace.
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct TableIdentifier {
    namespace: Vec<String>,
    name: String,
}

impl TableIdentifier {
    pub fn new<I, S>(namespace: I, name: impl Into<String>) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        Self {
            namespace: namespace.into_iter().map(Into::into).collect(),
            name: name.into(),
        }
    }

    pub fn namespace(&self) -> &[String] {
        &self.namespace
    }

    pub fn name(&self) -> &str {
        &self.name
    }

    pub(crate) fn renamed(&self, name: String) -> Self {
        Self {
            namespace: self.namespace.clone(),
            name,
        }
    }
}

/// Semantic table descriptor returned by a catalog.
#[derive(Clone)]
pub struct CatalogTable {
    pub(crate) identifier: TableIdentifier,
    pub(crate) table_id: TableId,
    pub(crate) catalog_schema_id: CatalogSchemaId,
    pub(crate) schema: TableSchema,
    pub(crate) runtime_context: Arc<CatalogRuntimeContext>,
}

impl std::fmt::Debug for CatalogTable {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("CatalogTable")
            .field("identifier", &self.identifier)
            .field("table_id", &self.table_id)
            .field("catalog_schema_id", &self.catalog_schema_id)
            .field("schema", &self.schema)
            .finish()
    }
}

impl CatalogTable {
    pub fn identifier(&self) -> &TableIdentifier {
        &self.identifier
    }

    pub fn table_id(&self) -> TableId {
        self.table_id
    }

    pub fn catalog_schema_id(&self) -> CatalogSchemaId {
        self.catalog_schema_id
    }

    pub fn schema(&self) -> &TableSchema {
        &self.schema
    }
}