-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* alter table * refactor(column): remove field: `table_name` * feat(create_table): add `default` on `CreateTable` * feat(cast): support `CAST` Function * feat: Constraints for CreateTable and Fix `Or` BinaryOperator in scope_aggregation cause index scan error * version up * feat(create_table): support `if not exists` * version up * code fmt * support add column * fix conflict * split operator * optimize * optimize * fix tuple decode err * fmt --------- Co-authored-by: Kould <[email protected]>
- Loading branch information
Showing
15 changed files
with
242 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
use sqlparser::ast::{AlterTableOperation, ObjectName}; | ||
|
||
use std::sync::Arc; | ||
|
||
use super::Binder; | ||
use crate::binder::{lower_case_name, split_name, BindError}; | ||
use crate::planner::operator::alter_table::add_column::AddColumnOperator; | ||
use crate::planner::operator::scan::ScanOperator; | ||
use crate::planner::operator::Operator; | ||
use crate::planner::LogicalPlan; | ||
use crate::storage::Transaction; | ||
|
||
impl<'a, T: Transaction> Binder<'a, T> { | ||
pub(crate) fn bind_alter_table( | ||
&mut self, | ||
name: &ObjectName, | ||
operation: &AlterTableOperation, | ||
) -> Result<LogicalPlan, BindError> { | ||
let table_name: Arc<String> = Arc::new(split_name(&lower_case_name(name))?.1.to_string()); | ||
|
||
let plan = match operation { | ||
AlterTableOperation::AddColumn { | ||
column_keyword: _, | ||
if_not_exists, | ||
column_def, | ||
} => { | ||
if let Some(table) = self.context.table(table_name.clone()) { | ||
let plan = ScanOperator::build(table_name.clone(), table); | ||
|
||
LogicalPlan { | ||
operator: Operator::AddColumn(AddColumnOperator { | ||
table_name, | ||
if_not_exists: *if_not_exists, | ||
column: self.bind_column(column_def)?, | ||
}), | ||
childrens: vec![plan], | ||
} | ||
} else { | ||
return Err(BindError::InvalidTable(format!( | ||
"not found table {}", | ||
table_name | ||
))); | ||
} | ||
} | ||
AlterTableOperation::DropColumn { | ||
column_name: _, | ||
if_exists: _, | ||
cascade: _, | ||
} => todo!(), | ||
AlterTableOperation::DropPrimaryKey => todo!(), | ||
AlterTableOperation::RenameColumn { | ||
old_column_name: _, | ||
new_column_name: _, | ||
} => todo!(), | ||
AlterTableOperation::RenameTable { table_name: _ } => todo!(), | ||
AlterTableOperation::ChangeColumn { | ||
old_name: _, | ||
new_name: _, | ||
data_type: _, | ||
options: _, | ||
} => todo!(), | ||
AlterTableOperation::AlterColumn { | ||
column_name: _, | ||
op: _, | ||
} => todo!(), | ||
_ => todo!(), | ||
}; | ||
|
||
Ok(plan) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use crate::execution::executor::BoxedExecutor; | ||
use crate::types::tuple::Tuple; | ||
use crate::types::value::DataValue; | ||
use crate::{execution::ExecutorError, types::tuple_builder::TupleBuilder}; | ||
use futures_async_stream::try_stream; | ||
use std::cell::RefCell; | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
execution::executor::Executor, planner::operator::alter_table::add_column::AddColumnOperator, | ||
storage::Transaction, | ||
}; | ||
|
||
pub struct AddColumn { | ||
op: AddColumnOperator, | ||
input: BoxedExecutor, | ||
} | ||
|
||
impl From<(AddColumnOperator, BoxedExecutor)> for AddColumn { | ||
fn from((op, input): (AddColumnOperator, BoxedExecutor)) -> Self { | ||
Self { op, input } | ||
} | ||
} | ||
|
||
impl<T: Transaction> Executor<T> for AddColumn { | ||
fn execute(self, transaction: &RefCell<T>) -> crate::execution::executor::BoxedExecutor { | ||
unsafe { self._execute(transaction.as_ptr().as_mut().unwrap()) } | ||
} | ||
} | ||
|
||
impl AddColumn { | ||
#[try_stream(boxed, ok = Tuple, error = ExecutorError)] | ||
async fn _execute<T: Transaction>(self, transaction: &mut T) { | ||
let _ = transaction.add_column(&self.op)?; | ||
|
||
let AddColumnOperator { | ||
table_name, column, .. | ||
} = &self.op; | ||
|
||
#[for_await] | ||
for tuple in self.input { | ||
let mut tuple: Tuple = tuple?; | ||
let is_overwrite = true; | ||
|
||
tuple.columns.push(Arc::new(column.clone())); | ||
if let Some(value) = column.default_value() { | ||
tuple.values.push(value); | ||
} else { | ||
tuple.values.push(Arc::new(DataValue::Null)); | ||
} | ||
|
||
transaction.append(table_name, tuple, is_overwrite)?; | ||
} | ||
transaction.remove_cache(&table_name)?; | ||
|
||
let tuple_builder = TupleBuilder::new_result(); | ||
let tuple = tuple_builder.push_result("ALTER TABLE SUCCESS", "1")?; | ||
|
||
yield tuple; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod add_column; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod alter_table; | ||
pub(crate) mod create_table; | ||
pub(crate) mod drop_table; | ||
pub(crate) mod truncate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use crate::catalog::{ColumnCatalog, TableName}; | ||
|
||
#[derive(Debug, PartialEq, Clone)] | ||
pub struct AddColumnOperator { | ||
pub table_name: TableName, | ||
pub if_not_exists: bool, | ||
pub column: ColumnCatalog, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod add_column; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
statement ok | ||
create table alter_table(id int primary key, v1 int) | ||
|
||
statement ok | ||
insert into alter_table values (1,1), (2,2), (3,3), (4,4) | ||
|
||
statement ok | ||
alter table alter_table add column da int null | ||
|
||
query IIII rowsort | ||
select * from alter_table | ||
---- | ||
1 1 null | ||
2 2 null | ||
3 3 null | ||
4 4 null | ||
|
||
statement ok | ||
drop table alter_table |