Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: impl ReferenceSerialization #227

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ chrono = { version = "0.4" }
clap = { version = "4.5", features = ["derive"], optional = true }
comfy-table = { version = "7" }
csv = { version = "1" }
encode_unicode = { version = "1" }
dirs = { version = "5" }
env_logger = { version = "0.11", optional = true }
futures = { version = "0.3", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions src/binder/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
false,
false,
None,
);
)?;
let mut nullable = true;

// TODO: 这里可以对更多字段可设置内容进行补充
Expand Down Expand Up @@ -182,7 +182,7 @@ mod tests {
debug_assert_eq!(op.columns[0].nullable, false);
debug_assert_eq!(
op.columns[0].desc,
ColumnDesc::new(LogicalType::Integer, true, false, None)
ColumnDesc::new(LogicalType::Integer, true, false, None)?
);
debug_assert_eq!(op.columns[1].name(), "name");
debug_assert_eq!(op.columns[1].nullable, true);
Expand All @@ -193,7 +193,7 @@ mod tests {
false,
false,
None
)
)?
);
}
_ => unreachable!(),
Expand Down
2 changes: 1 addition & 1 deletion src/binder/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
sub_query: LogicalPlan,
) -> Result<(ScalarExpression, LogicalPlan), DatabaseError> {
let mut alias_column = ColumnCatalog::clone(&column);
alias_column.set_table_name(self.context.temp_table());
alias_column.set_ref_table(self.context.temp_table(), 0);

let alias_expr = ScalarExpression::Alias {
expr: Box::new(ScalarExpression::ColumnRef(column)),
Expand Down
8 changes: 4 additions & 4 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,12 +409,12 @@ pub mod test {
ColumnCatalog::new(
"c1".to_string(),
false,
ColumnDesc::new(Integer, true, false, None),
ColumnDesc::new(Integer, true, false, None)?,
),
ColumnCatalog::new(
"c2".to_string(),
false,
ColumnDesc::new(Integer, false, true, None),
ColumnDesc::new(Integer, false, true, None)?,
),
],
false,
Expand All @@ -427,12 +427,12 @@ pub mod test {
ColumnCatalog::new(
"c3".to_string(),
false,
ColumnDesc::new(Integer, true, false, None),
ColumnDesc::new(Integer, true, false, None)?,
),
ColumnCatalog::new(
"c4".to_string(),
false,
ColumnDesc::new(Integer, false, false, None),
ColumnDesc::new(Integer, false, false, None)?,
),
],
false,
Expand Down
2 changes: 1 addition & 1 deletion src/binder/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
for (alias, column) in aliases_with_columns {
let mut alias_column = ColumnCatalog::clone(&column);
alias_column.set_name(alias.clone());
alias_column.set_table_name(table_alias.clone());
alias_column.set_ref_table(table_alias.clone(), column.id().unwrap_or(0));

let alias_column_expr = ScalarExpression::Alias {
expr: Box::new(ScalarExpression::ColumnRef(column)),
Expand Down
68 changes: 43 additions & 25 deletions src/catalog/column.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,44 @@
use crate::catalog::TableName;
use crate::errors::DatabaseError;
use crate::expression::ScalarExpression;
use crate::types::tuple::EMPTY_TUPLE;
use crate::types::value::ValueRef;
use crate::types::{ColumnId, LogicalType};
use serde::{Deserialize, Serialize};
use sqlparser::ast::CharLengthUnits;
use std::hash::Hash;
use std::sync::Arc;

use crate::types::tuple::EMPTY_TUPLE;
use crate::types::value::ValueRef;
use crate::types::{ColumnId, LogicalType};

pub type ColumnRef = Arc<ColumnCatalog>;

#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct ColumnCatalog {
pub summary: ColumnSummary,
pub nullable: bool,
pub desc: ColumnDesc,
}

#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub enum ColumnRelation {
None,
Table {
column_id: ColumnId,
table_name: TableName,
},
}

#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
pub struct ColumnSummary {
pub id: Option<ColumnId>,
pub name: String,
pub table_name: Option<TableName>,
pub relation: ColumnRelation,
}

impl ColumnCatalog {
pub fn new(column_name: String, nullable: bool, column_desc: ColumnDesc) -> ColumnCatalog {
ColumnCatalog {
summary: ColumnSummary {
id: None,
name: column_name,
table_name: None,
relation: ColumnRelation::None,
},
nullable,
desc: column_desc,
Expand All @@ -42,17 +48,18 @@ impl ColumnCatalog {
pub(crate) fn new_dummy(column_name: String) -> ColumnCatalog {
ColumnCatalog {
summary: ColumnSummary {
id: None,
name: column_name,
table_name: None,
relation: ColumnRelation::None,
},
nullable: true,
// SAFETY: default expr must not be [`ScalarExpression::ColumnRef`]
desc: ColumnDesc::new(
LogicalType::Varchar(None, CharLengthUnits::Characters),
false,
false,
None,
),
)
.unwrap(),
}
}

Expand All @@ -61,7 +68,10 @@ impl ColumnCatalog {
}

pub(crate) fn id(&self) -> Option<ColumnId> {
self.summary.id
match &self.summary.relation {
ColumnRelation::None => None,
ColumnRelation::Table { column_id, .. } => Some(*column_id),
}
}

pub fn name(&self) -> &str {
Expand All @@ -76,19 +86,21 @@ impl ColumnCatalog {
}

pub fn table_name(&self) -> Option<&TableName> {
self.summary.table_name.as_ref()
match &self.summary.relation {
ColumnRelation::None => None,
ColumnRelation::Table { table_name, .. } => Some(table_name),
}
}

pub fn set_name(&mut self, name: String) {
self.summary.name = name;
}

pub fn set_id(&mut self, id: ColumnId) {
self.summary.id = Some(id);
}

pub fn set_table_name(&mut self, table_name: TableName) {
self.summary.table_name = Some(table_name);
pub fn set_ref_table(&mut self, table_name: TableName, column_id: ColumnId) {
self.summary.relation = ColumnRelation::Table {
column_id,
table_name,
};
}

pub fn datatype(&self) -> &LogicalType {
Expand All @@ -110,7 +122,7 @@ impl ColumnCatalog {
}

/// The descriptor of a column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ColumnDesc {
pub(crate) column_datatype: LogicalType,
pub(crate) is_primary: bool,
Expand All @@ -119,17 +131,23 @@ pub struct ColumnDesc {
}

impl ColumnDesc {
pub const fn new(
pub fn new(
column_datatype: LogicalType,
is_primary: bool,
is_unique: bool,
default: Option<ScalarExpression>,
) -> ColumnDesc {
ColumnDesc {
) -> Result<ColumnDesc, DatabaseError> {
if let Some(expr) = &default {
if expr.has_table_ref_column() {
return Err(DatabaseError::DefaultNotColumnRef);
}
}

Ok(ColumnDesc {
column_datatype,
is_primary,
is_unique,
default,
}
})
}
}
36 changes: 27 additions & 9 deletions src/catalog/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::collections::BTreeMap;
use std::sync::Arc;
use std::{slice, vec};

use crate::catalog::{ColumnCatalog, ColumnRef};
use crate::catalog::{ColumnCatalog, ColumnRef, ColumnRelation};
use crate::errors::DatabaseError;
use crate::types::index::{IndexMeta, IndexMetaRef, IndexType};
use crate::types::tuple::SchemaRef;
Expand Down Expand Up @@ -99,8 +99,10 @@ impl TableCatalog {
.map(|(column_id, _)| column_id + 1)
.unwrap_or(0);

col.summary.table_name = Some(self.name.clone());
col.summary.id = Some(col_id);
col.summary.relation = ColumnRelation::Table {
column_id: col_id,
table_name: self.name.clone(),
};

self.column_idxs
.insert(col.name().to_string(), (col_id, self.schema_ref.len()));
Expand Down Expand Up @@ -162,13 +164,29 @@ impl TableCatalog {

pub(crate) fn reload(
name: TableName,
columns: Vec<ColumnCatalog>,
column_refs: Vec<ColumnRef>,
indexes: Vec<IndexMetaRef>,
) -> Result<TableCatalog, DatabaseError> {
let mut catalog = TableCatalog::new(name, columns)?;
catalog.indexes = indexes;
let mut column_idxs = BTreeMap::new();
let mut columns = BTreeMap::new();

for (i, column_ref) in column_refs.iter().enumerate() {
let column_id = column_ref.id().ok_or(DatabaseError::InvalidColumn(
"column does not belong to table".to_string(),
))?;

Ok(catalog)
column_idxs.insert(column_ref.name().to_string(), (column_id, i));
columns.insert(column_id, i);
}
let schema_ref = Arc::new(column_refs.clone());

Ok(TableCatalog {
name,
column_idxs,
columns,
indexes,
schema_ref,
})
}
}

Expand All @@ -193,12 +211,12 @@ mod tests {
let col0 = ColumnCatalog::new(
"a".into(),
false,
ColumnDesc::new(LogicalType::Integer, false, false, None),
ColumnDesc::new(LogicalType::Integer, false, false, None).unwrap(),
);
let col1 = ColumnCatalog::new(
"b".into(),
false,
ColumnDesc::new(LogicalType::Boolean, false, false, None),
ColumnDesc::new(LogicalType::Boolean, false, false, None).unwrap(),
);
let col_catalogs = vec![col0, col1];
let table_catalog = TableCatalog::new(Arc::new("test".to_string()), col_catalogs).unwrap();
Expand Down
13 changes: 6 additions & 7 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,17 @@ pub(crate) mod test {
ColumnCatalog::new(
"c1".to_string(),
false,
ColumnDesc::new(LogicalType::Integer, true, false, None),
ColumnDesc::new(LogicalType::Integer, true, false, None).unwrap(),
),
ColumnCatalog::new(
"c2".to_string(),
false,
ColumnDesc::new(LogicalType::Boolean, false, false, None),
ColumnDesc::new(LogicalType::Boolean, false, false, None).unwrap(),
),
ColumnCatalog::new(
"c3".to_string(),
false,
ColumnDesc::new(LogicalType::Integer, false, false, None),
ColumnDesc::new(LogicalType::Integer, false, false, None).unwrap(),
),
];
let _ =
Expand Down Expand Up @@ -369,7 +369,7 @@ pub(crate) mod test {
Arc::new(vec![Arc::new(ColumnCatalog::new(
"current_date()".to_string(),
true,
ColumnDesc::new(LogicalType::Date, false, false, None)
ColumnDesc::new(LogicalType::Date, false, false, None).unwrap()
))])
);
debug_assert_eq!(
Expand Down Expand Up @@ -397,10 +397,9 @@ pub(crate) mod test {
let mut column = ColumnCatalog::new(
"number".to_string(),
true,
ColumnDesc::new(LogicalType::Integer, false, false, None),
ColumnDesc::new(LogicalType::Integer, false, false, None).unwrap(),
);
column.set_table_name(Arc::new("a".to_string()));
column.set_id(0);
column.set_ref_table(Arc::new("a".to_string()), 0);

debug_assert_eq!(schema, Arc::new(vec![Arc::new(column)]));
debug_assert_eq!(
Expand Down
2 changes: 2 additions & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ pub enum DatabaseError {
#[source]
csv::Error,
),
#[error("default cannot be a column related to the table")]
DefaultNotColumnRef,
#[error("default does not exist")]
DefaultNotExist,
#[error("column: {0} already exists")]
Expand Down
Loading
Loading