Skip to content

Commit 770b505

Browse files
authored
refactor: impl ReferenceSerialization (#227)
* refactor: impl `ReferenceSerialization` * chore: codefmt * chore: codefmt
1 parent 7409bd7 commit 770b505

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+2695
-195
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ chrono = { version = "0.4" }
4141
clap = { version = "4.5", features = ["derive"], optional = true }
4242
comfy-table = { version = "7" }
4343
csv = { version = "1" }
44+
encode_unicode = { version = "1" }
4445
dirs = { version = "5" }
4546
env_logger = { version = "0.11", optional = true }
4647
futures = { version = "0.3", optional = true }

src/binder/create_table.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
9696
false,
9797
false,
9898
None,
99-
);
99+
)?;
100100
let mut nullable = true;
101101

102102
// TODO: 这里可以对更多字段可设置内容进行补充
@@ -182,7 +182,7 @@ mod tests {
182182
debug_assert_eq!(op.columns[0].nullable, false);
183183
debug_assert_eq!(
184184
op.columns[0].desc,
185-
ColumnDesc::new(LogicalType::Integer, true, false, None)
185+
ColumnDesc::new(LogicalType::Integer, true, false, None)?
186186
);
187187
debug_assert_eq!(op.columns[1].name(), "name");
188188
debug_assert_eq!(op.columns[1].nullable, true);
@@ -193,7 +193,7 @@ mod tests {
193193
false,
194194
false,
195195
None
196-
)
196+
)?
197197
);
198198
}
199199
_ => unreachable!(),

src/binder/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
231231
sub_query: LogicalPlan,
232232
) -> Result<(ScalarExpression, LogicalPlan), DatabaseError> {
233233
let mut alias_column = ColumnCatalog::clone(&column);
234-
alias_column.set_table_name(self.context.temp_table());
234+
alias_column.set_ref_table(self.context.temp_table(), 0);
235235

236236
let alias_expr = ScalarExpression::Alias {
237237
expr: Box::new(ScalarExpression::ColumnRef(column)),

src/binder/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,12 @@ pub mod test {
409409
ColumnCatalog::new(
410410
"c1".to_string(),
411411
false,
412-
ColumnDesc::new(Integer, true, false, None),
412+
ColumnDesc::new(Integer, true, false, None)?,
413413
),
414414
ColumnCatalog::new(
415415
"c2".to_string(),
416416
false,
417-
ColumnDesc::new(Integer, false, true, None),
417+
ColumnDesc::new(Integer, false, true, None)?,
418418
),
419419
],
420420
false,
@@ -427,12 +427,12 @@ pub mod test {
427427
ColumnCatalog::new(
428428
"c3".to_string(),
429429
false,
430-
ColumnDesc::new(Integer, true, false, None),
430+
ColumnDesc::new(Integer, true, false, None)?,
431431
),
432432
ColumnCatalog::new(
433433
"c4".to_string(),
434434
false,
435-
ColumnDesc::new(Integer, false, false, None),
435+
ColumnDesc::new(Integer, false, false, None)?,
436436
),
437437
],
438438
false,

src/binder/select.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
352352
for (alias, column) in aliases_with_columns {
353353
let mut alias_column = ColumnCatalog::clone(&column);
354354
alias_column.set_name(alias.clone());
355-
alias_column.set_table_name(table_alias.clone());
355+
alias_column.set_ref_table(table_alias.clone(), column.id().unwrap_or(0));
356356

357357
let alias_column_expr = ScalarExpression::Alias {
358358
expr: Box::new(ScalarExpression::ColumnRef(column)),

src/catalog/column.rs

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
use crate::catalog::TableName;
22
use crate::errors::DatabaseError;
33
use crate::expression::ScalarExpression;
4+
use crate::types::tuple::EMPTY_TUPLE;
5+
use crate::types::value::ValueRef;
6+
use crate::types::{ColumnId, LogicalType};
47
use serde::{Deserialize, Serialize};
58
use sqlparser::ast::CharLengthUnits;
69
use std::hash::Hash;
710
use std::sync::Arc;
811

9-
use crate::types::tuple::EMPTY_TUPLE;
10-
use crate::types::value::ValueRef;
11-
use crate::types::{ColumnId, LogicalType};
12-
1312
pub type ColumnRef = Arc<ColumnCatalog>;
1413

15-
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
14+
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
1615
pub struct ColumnCatalog {
1716
pub summary: ColumnSummary,
1817
pub nullable: bool,
1918
pub desc: ColumnDesc,
2019
}
2120

21+
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
22+
pub enum ColumnRelation {
23+
None,
24+
Table {
25+
column_id: ColumnId,
26+
table_name: TableName,
27+
},
28+
}
29+
2230
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
2331
pub struct ColumnSummary {
24-
pub id: Option<ColumnId>,
2532
pub name: String,
26-
pub table_name: Option<TableName>,
33+
pub relation: ColumnRelation,
2734
}
2835

2936
impl ColumnCatalog {
3037
pub fn new(column_name: String, nullable: bool, column_desc: ColumnDesc) -> ColumnCatalog {
3138
ColumnCatalog {
3239
summary: ColumnSummary {
33-
id: None,
3440
name: column_name,
35-
table_name: None,
41+
relation: ColumnRelation::None,
3642
},
3743
nullable,
3844
desc: column_desc,
@@ -42,17 +48,18 @@ impl ColumnCatalog {
4248
pub(crate) fn new_dummy(column_name: String) -> ColumnCatalog {
4349
ColumnCatalog {
4450
summary: ColumnSummary {
45-
id: None,
4651
name: column_name,
47-
table_name: None,
52+
relation: ColumnRelation::None,
4853
},
4954
nullable: true,
55+
// SAFETY: default expr must not be [`ScalarExpression::ColumnRef`]
5056
desc: ColumnDesc::new(
5157
LogicalType::Varchar(None, CharLengthUnits::Characters),
5258
false,
5359
false,
5460
None,
55-
),
61+
)
62+
.unwrap(),
5663
}
5764
}
5865

@@ -61,7 +68,10 @@ impl ColumnCatalog {
6168
}
6269

6370
pub(crate) fn id(&self) -> Option<ColumnId> {
64-
self.summary.id
71+
match &self.summary.relation {
72+
ColumnRelation::None => None,
73+
ColumnRelation::Table { column_id, .. } => Some(*column_id),
74+
}
6575
}
6676

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

7888
pub fn table_name(&self) -> Option<&TableName> {
79-
self.summary.table_name.as_ref()
89+
match &self.summary.relation {
90+
ColumnRelation::None => None,
91+
ColumnRelation::Table { table_name, .. } => Some(table_name),
92+
}
8093
}
8194

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

86-
pub fn set_id(&mut self, id: ColumnId) {
87-
self.summary.id = Some(id);
88-
}
89-
90-
pub fn set_table_name(&mut self, table_name: TableName) {
91-
self.summary.table_name = Some(table_name);
99+
pub fn set_ref_table(&mut self, table_name: TableName, column_id: ColumnId) {
100+
self.summary.relation = ColumnRelation::Table {
101+
column_id,
102+
table_name,
103+
};
92104
}
93105

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

112124
/// The descriptor of a column.
113-
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Hash)]
125+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
114126
pub struct ColumnDesc {
115127
pub(crate) column_datatype: LogicalType,
116128
pub(crate) is_primary: bool,
@@ -119,17 +131,23 @@ pub struct ColumnDesc {
119131
}
120132

121133
impl ColumnDesc {
122-
pub const fn new(
134+
pub fn new(
123135
column_datatype: LogicalType,
124136
is_primary: bool,
125137
is_unique: bool,
126138
default: Option<ScalarExpression>,
127-
) -> ColumnDesc {
128-
ColumnDesc {
139+
) -> Result<ColumnDesc, DatabaseError> {
140+
if let Some(expr) = &default {
141+
if expr.has_table_ref_column() {
142+
return Err(DatabaseError::DefaultNotColumnRef);
143+
}
144+
}
145+
146+
Ok(ColumnDesc {
129147
column_datatype,
130148
is_primary,
131149
is_unique,
132150
default,
133-
}
151+
})
134152
}
135153
}

src/catalog/table.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::BTreeMap;
44
use std::sync::Arc;
55
use std::{slice, vec};
66

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

102-
col.summary.table_name = Some(self.name.clone());
103-
col.summary.id = Some(col_id);
102+
col.summary.relation = ColumnRelation::Table {
103+
column_id: col_id,
104+
table_name: self.name.clone(),
105+
};
104106

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

163165
pub(crate) fn reload(
164166
name: TableName,
165-
columns: Vec<ColumnCatalog>,
167+
column_refs: Vec<ColumnRef>,
166168
indexes: Vec<IndexMetaRef>,
167169
) -> Result<TableCatalog, DatabaseError> {
168-
let mut catalog = TableCatalog::new(name, columns)?;
169-
catalog.indexes = indexes;
170+
let mut column_idxs = BTreeMap::new();
171+
let mut columns = BTreeMap::new();
172+
173+
for (i, column_ref) in column_refs.iter().enumerate() {
174+
let column_id = column_ref.id().ok_or(DatabaseError::InvalidColumn(
175+
"column does not belong to table".to_string(),
176+
))?;
170177

171-
Ok(catalog)
178+
column_idxs.insert(column_ref.name().to_string(), (column_id, i));
179+
columns.insert(column_id, i);
180+
}
181+
let schema_ref = Arc::new(column_refs.clone());
182+
183+
Ok(TableCatalog {
184+
name,
185+
column_idxs,
186+
columns,
187+
indexes,
188+
schema_ref,
189+
})
172190
}
173191
}
174192

@@ -193,12 +211,12 @@ mod tests {
193211
let col0 = ColumnCatalog::new(
194212
"a".into(),
195213
false,
196-
ColumnDesc::new(LogicalType::Integer, false, false, None),
214+
ColumnDesc::new(LogicalType::Integer, false, false, None).unwrap(),
197215
);
198216
let col1 = ColumnCatalog::new(
199217
"b".into(),
200218
false,
201-
ColumnDesc::new(LogicalType::Boolean, false, false, None),
219+
ColumnDesc::new(LogicalType::Boolean, false, false, None).unwrap(),
202220
);
203221
let col_catalogs = vec![col0, col1];
204222
let table_catalog = TableCatalog::new(Arc::new("test".to_string()), col_catalogs).unwrap();

src/db.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,17 +322,17 @@ pub(crate) mod test {
322322
ColumnCatalog::new(
323323
"c1".to_string(),
324324
false,
325-
ColumnDesc::new(LogicalType::Integer, true, false, None),
325+
ColumnDesc::new(LogicalType::Integer, true, false, None).unwrap(),
326326
),
327327
ColumnCatalog::new(
328328
"c2".to_string(),
329329
false,
330-
ColumnDesc::new(LogicalType::Boolean, false, false, None),
330+
ColumnDesc::new(LogicalType::Boolean, false, false, None).unwrap(),
331331
),
332332
ColumnCatalog::new(
333333
"c3".to_string(),
334334
false,
335-
ColumnDesc::new(LogicalType::Integer, false, false, None),
335+
ColumnDesc::new(LogicalType::Integer, false, false, None).unwrap(),
336336
),
337337
];
338338
let _ =
@@ -369,7 +369,7 @@ pub(crate) mod test {
369369
Arc::new(vec![Arc::new(ColumnCatalog::new(
370370
"current_date()".to_string(),
371371
true,
372-
ColumnDesc::new(LogicalType::Date, false, false, None)
372+
ColumnDesc::new(LogicalType::Date, false, false, None).unwrap()
373373
))])
374374
);
375375
debug_assert_eq!(
@@ -397,10 +397,9 @@ pub(crate) mod test {
397397
let mut column = ColumnCatalog::new(
398398
"number".to_string(),
399399
true,
400-
ColumnDesc::new(LogicalType::Integer, false, false, None),
400+
ColumnDesc::new(LogicalType::Integer, false, false, None).unwrap(),
401401
);
402-
column.set_table_name(Arc::new("a".to_string()));
403-
column.set_id(0);
402+
column.set_ref_table(Arc::new("a".to_string()), 0);
404403

405404
debug_assert_eq!(schema, Arc::new(vec![Arc::new(column)]));
406405
debug_assert_eq!(

src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ pub enum DatabaseError {
3030
#[source]
3131
csv::Error,
3232
),
33+
#[error("default cannot be a column related to the table")]
34+
DefaultNotColumnRef,
3335
#[error("default does not exist")]
3436
DefaultNotExist,
3537
#[error("column: {0} already exists")]

0 commit comments

Comments
 (0)