use crate::types::{DataType, Value};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Column {
pub name: String,
pub data_type: DataType,
pub nullable: bool,
pub default: Option<Value>,
pub primary_key: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Schema {
pub columns: Vec<Column>,
}
impl Schema {
pub fn new(columns: Vec<Column>) -> Self {
Self { columns }
}
pub fn column_index(&self, name: &str) -> Option<usize> {
self.columns.iter().position(|c| c.name == name)
}
pub fn column(&self, name: &str) -> Option<&Column> {
self.column_index(name).map(|idx| &self.columns[idx])
}
pub fn num_columns(&self) -> usize {
self.columns.len()
}
pub fn tuple_size_estimate(&self) -> usize {
self.columns
.iter()
.map(|c| match c.data_type {
DataType::Boolean => 1,
DataType::Int16 => 2,
DataType::Int32 => 4,
DataType::Int64 => 8,
DataType::Float32 => 4,
DataType::Float64 => 8,
DataType::Timestamp => 8,
DataType::Vector(dim) => (dim as usize) * 4,
DataType::Varchar(n) => n as usize,
DataType::Text => 24,
DataType::Bm25Query => 24,
DataType::Null => 0,
})
.sum()
}
}