Skip to content

Commit 7bae884

Browse files
committed
feat: impl View Encode & Decode
1 parent d584167 commit 7bae884

File tree

28 files changed

+439
-180
lines changed

28 files changed

+439
-180
lines changed

src/binder/copy.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,7 @@ use serde::{Deserialize, Serialize};
1111
use serde_macros::ReferenceSerialization;
1212
use sqlparser::ast::{CopyOption, CopySource, CopyTarget};
1313

14-
#[derive(
15-
Debug,
16-
PartialEq,
17-
PartialOrd,
18-
Ord,
19-
Hash,
20-
Eq,
21-
Clone,
22-
Serialize,
23-
Deserialize,
24-
ReferenceSerialization,
25-
)]
14+
#[derive(Debug, PartialEq, PartialOrd, Ord, Hash, Eq, Clone, ReferenceSerialization)]
2615
pub struct ExtSource {
2716
pub path: PathBuf,
2817
pub format: FileFormat,

src/binder/create_table.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
6262
.find(|column| column.name() == column_name)
6363
{
6464
if *is_primary {
65-
column.desc.is_primary = true;
65+
column.desc_mut().is_primary = true;
6666
} else {
67-
column.desc.is_unique = true;
67+
column.desc_mut().is_unique = true;
6868
}
6969
}
7070
}
@@ -73,7 +73,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
7373
}
7474
}
7575

76-
if columns.iter().filter(|col| col.desc.is_primary).count() != 1 {
76+
if columns.iter().filter(|col| col.desc().is_primary).count() != 1 {
7777
return Err(DatabaseError::InvalidTable(
7878
"The primary key field must exist and have at least one".to_string(),
7979
));
@@ -179,16 +179,16 @@ mod tests {
179179
Operator::CreateTable(op) => {
180180
debug_assert_eq!(op.table_name, Arc::new("t1".to_string()));
181181
debug_assert_eq!(op.columns[0].name(), "id");
182-
debug_assert_eq!(op.columns[0].nullable, false);
182+
debug_assert_eq!(op.columns[0].nullable(), false);
183183
debug_assert_eq!(
184-
op.columns[0].desc,
185-
ColumnDesc::new(LogicalType::Integer, true, false, None)?
184+
op.columns[0].desc(),
185+
&ColumnDesc::new(LogicalType::Integer, true, false, None)?
186186
);
187187
debug_assert_eq!(op.columns[1].name(), "name");
188-
debug_assert_eq!(op.columns[1].nullable, true);
188+
debug_assert_eq!(op.columns[1].nullable(), true);
189189
debug_assert_eq!(
190-
op.columns[1].desc,
191-
ColumnDesc::new(
190+
op.columns[1].desc(),
191+
&ColumnDesc::new(
192192
LogicalType::Varchar(Some(10), CharLengthUnits::Characters),
193193
false,
194194
false,

src/binder/delete.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> {
2828
.table_and_bind(table_name.clone(), table_alias.clone(), None)?;
2929
let primary_key_column = table_catalog
3030
.columns()
31-
.find(|column| column.desc.is_primary)
31+
.find(|column| column.desc().is_primary)
3232
.cloned()
3333
.unwrap();
3434
let mut plan = TableScanOperator::build(table_name.clone(), table_catalog);

src/binder/select.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -734,10 +734,9 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
734734
.iter()
735735
.find(|(table, _)| table.contains_column(col.name()))
736736
.map(|(_, nullable)| {
737-
let mut new_col = ColumnCatalog::clone(col);
738-
new_col.nullable = *nullable;
739-
740-
*col = ColumnRef::from(new_col);
737+
if let Some(new_column) = col.nullable_for_join(*nullable) {
738+
*col = new_column;
739+
}
741740
});
742741
}
743742
}
@@ -852,7 +851,7 @@ impl<'a: 'b, 'b, T: Transaction> Binder<'a, 'b, T> {
852851
right_schema: &Schema,
853852
) -> Result<(), DatabaseError> {
854853
let fn_contains = |schema: &Schema, summary: &ColumnSummary| {
855-
schema.iter().any(|column| summary == &column.summary)
854+
schema.iter().any(|column| summary == column.summary())
856855
};
857856
let fn_or_contains =
858857
|left_schema: &Schema, right_schema: &Schema, summary: &ColumnSummary| {

src/catalog/column.rs

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use crate::expression::ScalarExpression;
44
use crate::types::tuple::EMPTY_TUPLE;
55
use crate::types::value::ValueRef;
66
use crate::types::{ColumnId, LogicalType};
7-
use serde::{Deserialize, Serialize};
87
use serde_macros::ReferenceSerialization;
98
use sqlparser::ast::CharLengthUnits;
109
use std::hash::Hash;
@@ -30,12 +29,13 @@ impl From<ColumnCatalog> for ColumnRef {
3029

3130
#[derive(Debug, Clone, Hash, Eq, PartialEq, ReferenceSerialization)]
3231
pub struct ColumnCatalog {
33-
pub summary: ColumnSummary,
34-
pub nullable: bool,
35-
pub desc: ColumnDesc,
32+
summary: ColumnSummary,
33+
nullable: bool,
34+
desc: ColumnDesc,
35+
in_join: bool,
3636
}
3737

38-
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
38+
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
3939
pub enum ColumnRelation {
4040
None,
4141
Table {
@@ -44,12 +44,24 @@ pub enum ColumnRelation {
4444
},
4545
}
4646

47-
#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, ReferenceSerialization)]
47+
#[derive(Debug, Clone, Hash, Eq, PartialEq, ReferenceSerialization)]
4848
pub struct ColumnSummary {
4949
pub name: String,
5050
pub relation: ColumnRelation,
5151
}
5252

53+
impl ColumnRef {
54+
pub(crate) fn nullable_for_join(&self, nullable: bool) -> Option<ColumnRef> {
55+
if self.nullable == nullable {
56+
return None;
57+
}
58+
let mut temp = ColumnCatalog::clone(self);
59+
temp.nullable = nullable;
60+
temp.in_join = true;
61+
Some(ColumnRef::from(temp))
62+
}
63+
}
64+
5365
impl ColumnCatalog {
5466
pub fn new(column_name: String, nullable: bool, column_desc: ColumnDesc) -> ColumnCatalog {
5567
ColumnCatalog {
@@ -59,6 +71,21 @@ impl ColumnCatalog {
5971
},
6072
nullable,
6173
desc: column_desc,
74+
in_join: false,
75+
}
76+
}
77+
78+
pub(crate) fn direct_new(
79+
summary: ColumnSummary,
80+
nullable: bool,
81+
column_desc: ColumnDesc,
82+
in_join: bool,
83+
) -> ColumnCatalog {
84+
ColumnCatalog {
85+
summary,
86+
nullable,
87+
desc: column_desc,
88+
in_join,
6289
}
6390
}
6491

@@ -77,13 +104,18 @@ impl ColumnCatalog {
77104
None,
78105
)
79106
.unwrap(),
107+
in_join: false,
80108
}
81109
}
82110

83111
pub(crate) fn summary(&self) -> &ColumnSummary {
84112
&self.summary
85113
}
86114

115+
pub(crate) fn summary_mut(&mut self) -> &mut ColumnSummary {
116+
&mut self.summary
117+
}
118+
87119
pub fn id(&self) -> Option<ColumnId> {
88120
match &self.summary.relation {
89121
ColumnRelation::None => None,
@@ -120,6 +152,14 @@ impl ColumnCatalog {
120152
};
121153
}
122154

155+
pub fn in_join(&self) -> bool {
156+
self.in_join
157+
}
158+
159+
pub fn nullable(&self) -> bool {
160+
self.nullable
161+
}
162+
123163
pub fn datatype(&self) -> &LogicalType {
124164
&self.desc.column_datatype
125165
}
@@ -132,10 +172,13 @@ impl ColumnCatalog {
132172
.transpose()
133173
}
134174

135-
#[allow(dead_code)]
136175
pub(crate) fn desc(&self) -> &ColumnDesc {
137176
&self.desc
138177
}
178+
179+
pub(crate) fn desc_mut(&mut self) -> &mut ColumnDesc {
180+
&mut self.desc
181+
}
139182
}
140183

141184
/// The descriptor of a column.

src/catalog/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ pub(crate) use self::table::*;
55

66
pub mod column;
77
pub mod table;
8+
pub mod view;

src/catalog/table.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::types::index::{IndexMeta, IndexMetaRef, IndexType};
44
use crate::types::tuple::SchemaRef;
55
use crate::types::{ColumnId, LogicalType};
66
use itertools::Itertools;
7-
use serde::{Deserialize, Serialize};
7+
use serde_macros::ReferenceSerialization;
88
use std::collections::BTreeMap;
99
use std::sync::Arc;
1010
use std::{slice, vec};
@@ -24,7 +24,7 @@ pub struct TableCatalog {
2424
}
2525

2626
//TODO: can add some like Table description and other information as attributes
27-
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
27+
#[derive(Debug, Clone, PartialEq, ReferenceSerialization)]
2828
pub struct TableMeta {
2929
pub(crate) table_name: TableName,
3030
}
@@ -76,7 +76,7 @@ impl TableCatalog {
7676
self.schema_ref
7777
.iter()
7878
.enumerate()
79-
.find(|(_, column)| column.desc.is_primary)
79+
.find(|(_, column)| column.desc().is_primary)
8080
.ok_or(DatabaseError::PrimaryKeyNotFound)
8181
}
8282

@@ -97,7 +97,7 @@ impl TableCatalog {
9797
}
9898
let col_id = generator.generate().unwrap();
9999

100-
col.summary.relation = ColumnRelation::Table {
100+
col.summary_mut().relation = ColumnRelation::Table {
101101
column_id: col_id,
102102
table_name: self.name.clone(),
103103
};

src/catalog/view.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
use crate::planner::LogicalPlan;
2+
use serde_macros::ReferenceSerialization;
3+
4+
#[derive(Debug, Clone, Hash, Eq, PartialEq, ReferenceSerialization)]
5+
pub struct View {
6+
pub name: String,
7+
pub plan: LogicalPlan,
8+
}

src/execution/ddl/drop_column.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for DropColumn {
4141
.iter()
4242
.enumerate()
4343
.find(|(_, column)| column.name() == column_name)
44-
.map(|(i, column)| (i, column.desc.is_primary))
44+
.map(|(i, column)| (i, column.desc().is_primary))
4545
{
4646
if is_primary {
4747
throw!(Err(DatabaseError::InvalidColumn(

src/execution/dml/copy_from_file.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,45 +131,47 @@ mod tests {
131131
write!(file, "{}", csv).expect("failed to write file");
132132

133133
let columns = vec![
134-
ColumnRef::from(ColumnCatalog {
135-
summary: ColumnSummary {
134+
ColumnRef::from(ColumnCatalog::direct_new(
135+
ColumnSummary {
136136
name: "a".to_string(),
137137
relation: ColumnRelation::Table {
138138
column_id: Ulid::new(),
139139
table_name: Arc::new("t1".to_string()),
140140
},
141141
},
142-
nullable: false,
143-
desc: ColumnDesc::new(LogicalType::Integer, true, false, None).unwrap(),
144-
}),
145-
ColumnRef::from(ColumnCatalog {
146-
summary: ColumnSummary {
142+
false,
143+
ColumnDesc::new(LogicalType::Integer, true, false, None)?,
144+
false,
145+
)),
146+
ColumnRef::from(ColumnCatalog::direct_new(
147+
ColumnSummary {
147148
name: "b".to_string(),
148149
relation: ColumnRelation::Table {
149150
column_id: Ulid::new(),
150151
table_name: Arc::new("t1".to_string()),
151152
},
152153
},
153-
nullable: false,
154-
desc: ColumnDesc::new(LogicalType::Float, false, false, None).unwrap(),
155-
}),
156-
ColumnRef::from(ColumnCatalog {
157-
summary: ColumnSummary {
154+
false,
155+
ColumnDesc::new(LogicalType::Float, false, false, None)?,
156+
false,
157+
)),
158+
ColumnRef::from(ColumnCatalog::direct_new(
159+
ColumnSummary {
158160
name: "c".to_string(),
159161
relation: ColumnRelation::Table {
160162
column_id: Ulid::new(),
161163
table_name: Arc::new("t1".to_string()),
162164
},
163165
},
164-
nullable: false,
165-
desc: ColumnDesc::new(
166+
false,
167+
ColumnDesc::new(
166168
LogicalType::Varchar(Some(10), CharLengthUnits::Characters),
167169
false,
168170
false,
169171
None,
170-
)
171-
.unwrap(),
172-
}),
172+
)?,
173+
false,
174+
)),
173175
];
174176

175177
let op = CopyFromFileOperator {

0 commit comments

Comments
 (0)