-
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.
* feat: impl dml(update) * docs: add `Bloom` Banner
- Loading branch information
Showing
18 changed files
with
224 additions
and
50 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
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,72 @@ | ||
use std::slice; | ||
use sqlparser::ast::{Assignment, Expr, TableFactor, TableWithJoins}; | ||
use crate::binder::{Binder, BindError, lower_case_name, split_name}; | ||
use crate::expression::ScalarExpression; | ||
use crate::planner::LogicalPlan; | ||
use crate::planner::operator::Operator; | ||
use crate::planner::operator::update::UpdateOperator; | ||
use crate::types::value::ValueRef; | ||
|
||
impl Binder { | ||
pub(crate) fn bind_update( | ||
&mut self, | ||
to: &TableWithJoins, | ||
selection: &Option<Expr>, | ||
assignments: &[Assignment] | ||
) -> Result<LogicalPlan, BindError> { | ||
if let TableFactor::Table { name, .. } = &to.relation { | ||
let name = lower_case_name(&name); | ||
let (_, table_name) = split_name(&name)?; | ||
|
||
let mut plan = self.bind_table_ref(slice::from_ref(to))?; | ||
|
||
if let Some(predicate) = selection { | ||
plan = self.bind_where(plan, predicate)?; | ||
} | ||
|
||
if let Some(table) = self.context.catalog.get_table_by_name(table_name) { | ||
let table_id = table.id; | ||
let bind_table_name = Some(table_name.to_string()); | ||
|
||
let mut columns = Vec::with_capacity(assignments.len()); | ||
let mut row = Vec::with_capacity(assignments.len()); | ||
|
||
|
||
for assignment in assignments { | ||
let value = match self.bind_expr(&assignment.value)? { | ||
ScalarExpression::Constant(value) => Ok::<ValueRef, BindError>(value), | ||
_ => unreachable!(), | ||
}?; | ||
|
||
for ident in &assignment.id { | ||
match self.bind_column_ref_from_identifiers( | ||
slice::from_ref(&ident), | ||
bind_table_name.as_ref() | ||
)? { | ||
ScalarExpression::ColumnRef(catalog) => { | ||
columns.push(catalog); | ||
row.push(value.clone()); | ||
}, | ||
_ => unreachable!() | ||
} | ||
} | ||
} | ||
|
||
let values_plan = self.bind_values(vec![row], columns); | ||
|
||
Ok(LogicalPlan { | ||
operator: Operator::Update( | ||
UpdateOperator { | ||
table_id, | ||
} | ||
), | ||
childrens: vec![plan, values_plan], | ||
}) | ||
} else { | ||
Err(BindError::InvalidTable(format!("not found table {}", table_name))) | ||
} | ||
} else { | ||
unreachable!("only table") | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
pub(crate) mod insert; | ||
pub(crate) mod update; |
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,45 @@ | ||
use std::collections::HashMap; | ||
use futures_async_stream::try_stream; | ||
use crate::catalog::CatalogError; | ||
use crate::execution::executor::BoxedExecutor; | ||
use crate::execution::ExecutorError; | ||
use crate::storage::{Storage, Table}; | ||
use crate::types::TableId; | ||
use crate::types::tuple::Tuple; | ||
|
||
pub struct Update { } | ||
|
||
impl Update { | ||
#[try_stream(boxed, ok = Tuple, error = ExecutorError)] | ||
pub async fn execute(table_id: TableId, input: BoxedExecutor, values: BoxedExecutor, storage: impl Storage) { | ||
if let Some(table_catalog) = storage.get_catalog().get_table(&table_id) { | ||
let mut value_map = HashMap::new(); | ||
|
||
// only once | ||
#[for_await] | ||
for tuple in values { | ||
let Tuple { columns, values, .. } = tuple?; | ||
for i in 0..columns.len() { | ||
value_map.insert(columns[i].id, values[i].clone()); | ||
} | ||
} | ||
|
||
let table = storage.get_table(&table_catalog.id)?; | ||
|
||
#[for_await] | ||
for tuple in input { | ||
let mut tuple = tuple?; | ||
|
||
for (i, column) in tuple.columns.iter().enumerate() { | ||
if let Some(value) = value_map.get(&column.id) { | ||
tuple.values[i] = value.clone(); | ||
} | ||
} | ||
|
||
table.append(tuple)?; | ||
} | ||
} else { | ||
Err(CatalogError::NotFound("root", table_id.to_string()))?; | ||
} | ||
} | ||
} |
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,9 @@ | ||
use crate::execution::physical_plan::PhysicalPlan; | ||
use crate::types::TableId; | ||
|
||
#[derive(Debug)] | ||
pub struct PhysicalUpdate { | ||
pub(crate) table_id: TableId, | ||
pub(crate) input: Box<PhysicalPlan>, | ||
pub(crate) values: Box<PhysicalPlan> | ||
} |
Oops, something went wrong.