-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR is a part of #796, adds support for creating, querying and dropping views in memory. The key implementations are: 1. When creating a view, bind the query and store the logical plan with the view in catalog. 2. When querying from a view, build executors for all views and then build other plan nodes on top of them. Given that a view can be consumed by multiple downstream nodes, we introduce `StreamSubscriber` to allow multiple consumers of a stream. Limitations: 1. We don't persist views in disk storage. 2. We don't support inferring schema from the query. Columns must be defined explicitly when creating a view. 3. We don't maintain dependency relationship between tables and views. --------- Signed-off-by: Runji Wang <[email protected]>
- Loading branch information
1 parent
41d545f
commit d492a45
Showing
28 changed files
with
630 additions
and
207 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,61 @@ | ||
use std::collections::HashSet; | ||
|
||
use super::*; | ||
use crate::catalog::{ColumnCatalog, ColumnDesc, ColumnId}; | ||
|
||
impl Binder { | ||
pub(super) fn bind_create_view( | ||
&mut self, | ||
name: ObjectName, | ||
columns: Vec<Ident>, | ||
query: Query, | ||
) -> Result { | ||
let name = lower_case_name(&name); | ||
let (schema_name, table_name) = split_name(&name)?; | ||
let schema = self | ||
.catalog | ||
.get_schema_by_name(schema_name) | ||
.ok_or_else(|| BindError::InvalidSchema(schema_name.into()))?; | ||
if schema.get_table_by_name(table_name).is_some() { | ||
return Err(BindError::TableExists(table_name.into())); | ||
} | ||
|
||
// check duplicated column names | ||
let mut set = HashSet::new(); | ||
for col in &columns { | ||
if !set.insert(col.value.to_lowercase()) { | ||
return Err(BindError::ColumnExists(col.value.to_lowercase())); | ||
} | ||
} | ||
|
||
let (query, _) = self.bind_query(query)?; | ||
let query_type = self.type_(query)?; | ||
let output_types = query_type.as_struct(); | ||
|
||
// TODO: support inferring column names from query | ||
if columns.len() != output_types.len() { | ||
return Err(BindError::ViewAliasesMismatch); | ||
} | ||
|
||
let columns: Vec<ColumnCatalog> = columns | ||
.into_iter() | ||
.zip(output_types) | ||
.enumerate() | ||
.map(|(idx, (name, ty))| { | ||
ColumnCatalog::new( | ||
idx as ColumnId, | ||
ColumnDesc::new(name.value, ty.clone(), true), | ||
) | ||
}) | ||
.collect(); | ||
|
||
let table = self.egraph.add(Node::CreateTable(CreateTable { | ||
schema_id: schema.id(), | ||
table_name: table_name.into(), | ||
columns, | ||
ordered_pk_ids: vec![], | ||
})); | ||
let create_view = self.egraph.add(Node::CreateView([table, query])); | ||
Ok(create_view) | ||
} | ||
} |
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
Oops, something went wrong.