-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
252 additions
and
19 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,62 @@ | ||
use crate::binder::{lower_case_name, lower_ident, Binder}; | ||
use crate::catalog::view::View; | ||
use crate::catalog::{ColumnCatalog, ColumnRef}; | ||
use crate::errors::DatabaseError; | ||
use crate::expression::{AliasType, ScalarExpression}; | ||
use crate::planner::operator::create_view::CreateViewOperator; | ||
use crate::planner::operator::Operator; | ||
use crate::planner::LogicalPlan; | ||
use crate::storage::Transaction; | ||
use itertools::Itertools; | ||
use sqlparser::ast::{Ident, ObjectName, Query}; | ||
use std::sync::Arc; | ||
use ulid::Ulid; | ||
|
||
impl<'a, 'b, T: Transaction> Binder<'a, 'b, T> { | ||
pub(crate) fn bind_create_view( | ||
&mut self, | ||
or_replace: &bool, | ||
name: &ObjectName, | ||
columns: &[Ident], | ||
query: &Query, | ||
) -> Result<LogicalPlan, DatabaseError> { | ||
let view_name = Arc::new(lower_case_name(name)?); | ||
let mut plan = self.bind_query(query)?; | ||
|
||
if !columns.is_empty() { | ||
let mapping_schema = plan.output_schema(); | ||
let exprs = columns | ||
.iter() | ||
.enumerate() | ||
.map(|(i, ident)| { | ||
let mapping_column = &mapping_schema[i]; | ||
let mut column = ColumnCatalog::new( | ||
lower_ident(ident), | ||
mapping_column.nullable(), | ||
mapping_column.desc().clone(), | ||
); | ||
column.set_ref_table(view_name.clone(), Ulid::new(), true); | ||
|
||
ScalarExpression::Alias { | ||
expr: Box::new(ScalarExpression::ColumnRef(mapping_column.clone())), | ||
alias: AliasType::Expr(Box::new(ScalarExpression::ColumnRef( | ||
ColumnRef::from(column), | ||
))), | ||
} | ||
}) | ||
.collect_vec(); | ||
plan = self.bind_project(plan, exprs)?; | ||
} | ||
|
||
Ok(LogicalPlan::new( | ||
Operator::CreateView(CreateViewOperator { | ||
view: View { | ||
name: view_name, | ||
plan: Box::new(plan), | ||
}, | ||
or_replace: *or_replace, | ||
}), | ||
vec![], | ||
)) | ||
} | ||
} |
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,8 +1,19 @@ | ||
use crate::catalog::TableName; | ||
use crate::planner::LogicalPlan; | ||
use serde_macros::ReferenceSerialization; | ||
use std::fmt; | ||
use std::fmt::Formatter; | ||
|
||
#[derive(Debug, Clone, Hash, Eq, PartialEq, ReferenceSerialization)] | ||
pub struct View { | ||
pub name: String, | ||
pub plan: LogicalPlan, | ||
pub name: TableName, | ||
pub plan: Box<LogicalPlan>, | ||
} | ||
|
||
impl fmt::Display for View { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!(f, "View {}: {}", self.name, self.plan.explain(0))?; | ||
|
||
Ok(()) | ||
} | ||
} |
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,37 @@ | ||
use crate::execution::{Executor, WriteExecutor}; | ||
use crate::planner::operator::create_view::CreateViewOperator; | ||
use crate::storage::{StatisticsMetaCache, TableCache, Transaction, ViewCache}; | ||
use crate::throw; | ||
use crate::types::tuple_builder::TupleBuilder; | ||
use std::sync::Arc; | ||
|
||
pub struct CreateView { | ||
op: CreateViewOperator, | ||
view_cache: Arc<ViewCache>, | ||
} | ||
|
||
impl From<(CreateViewOperator, Arc<ViewCache>)> for CreateView { | ||
fn from((op, view_cache): (CreateViewOperator, Arc<ViewCache>)) -> Self { | ||
CreateView { op, view_cache } | ||
} | ||
} | ||
|
||
impl<'a, T: Transaction + 'a> WriteExecutor<'a, T> for CreateView { | ||
fn execute_mut( | ||
self, | ||
_: (&'a TableCache, &'a StatisticsMetaCache), | ||
transaction: &'a mut T, | ||
) -> Executor<'a> { | ||
Box::new( | ||
#[coroutine] | ||
move || { | ||
let CreateViewOperator { view, or_replace } = self.op; | ||
|
||
let result_tuple = TupleBuilder::build_result(format!("{}", view.name)); | ||
throw!(transaction.create_view(&self.view_cache, view, or_replace)); | ||
|
||
yield Ok(result_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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
pub mod add_column; | ||
pub(crate) mod create_index; | ||
pub(crate) mod create_table; | ||
pub(crate) mod create_view; | ||
pub mod drop_column; | ||
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
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,22 @@ | ||
use crate::catalog::view::View; | ||
use serde_macros::ReferenceSerialization; | ||
use std::fmt; | ||
use std::fmt::Formatter; | ||
|
||
#[derive(Debug, PartialEq, Eq, Clone, Hash, ReferenceSerialization)] | ||
pub struct CreateViewOperator { | ||
pub view: View, | ||
pub or_replace: bool, | ||
} | ||
|
||
impl fmt::Display for CreateViewOperator { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
write!( | ||
f, | ||
"Create View as {}, Or Replace: {}", | ||
self.view, self.or_replace | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
} |
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
Oops, something went wrong.