-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathalter_table.rs
More file actions
77 lines (71 loc) · 2.67 KB
/
alter_table.rs
File metadata and controls
77 lines (71 loc) · 2.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use sqlparser::ast::{AlterTableOperation, ObjectName};
use std::sync::Arc;
use super::{is_valid_identifier, Binder};
use crate::binder::lower_case_name;
use crate::errors::DatabaseError;
use crate::planner::operator::alter_table::add_column::AddColumnOperator;
use crate::planner::operator::alter_table::drop_column::DropColumnOperator;
use crate::planner::operator::table_scan::TableScanOperator;
use crate::planner::operator::Operator;
use crate::planner::{Childrens, LogicalPlan};
use crate::storage::Transaction;
use crate::types::value::DataValue;
impl<T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'_, '_, T, A> {
pub(crate) fn bind_alter_table(
&mut self,
name: &ObjectName,
operation: &AlterTableOperation,
) -> Result<LogicalPlan, DatabaseError> {
let table_name: Arc<String> = Arc::new(lower_case_name(name)?);
let table = self
.context
.table(table_name.clone())?
.ok_or(DatabaseError::TableNotFound)?;
let plan = match operation {
AlterTableOperation::AddColumn {
column_keyword: _,
if_not_exists,
column_def,
} => {
let plan = TableScanOperator::build(table_name.clone(), table, true);
let column = self.bind_column(column_def, None)?;
if !is_valid_identifier(column.name()) {
return Err(DatabaseError::InvalidColumn(
"illegal column naming".to_string(),
));
}
LogicalPlan::new(
Operator::AddColumn(AddColumnOperator {
table_name,
if_not_exists: *if_not_exists,
column,
}),
Childrens::Only(plan),
)
}
AlterTableOperation::DropColumn {
column_name,
if_exists,
..
} => {
let plan = TableScanOperator::build(table_name.clone(), table, true);
let column_name = column_name.value.clone();
LogicalPlan::new(
Operator::DropColumn(DropColumnOperator {
table_name,
if_exists: *if_exists,
column_name,
}),
Childrens::Only(plan),
)
}
op => {
return Err(DatabaseError::UnsupportedStmt(format!(
"AlertOperation: {:?}",
op
)))
}
};
Ok(plan)
}
}