-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(binder): bind project. * plan projection. * support multiple tables. * use reverse_map for BindContext. * move plan_projection to select.rs. * add bind_all_columns. * add test. * comment one test.
- Loading branch information
Showing
10 changed files
with
186 additions
and
12 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,34 @@ | ||
use std::collections::HashMap; | ||
|
||
use risingwave_common::types::DataType; | ||
|
||
pub struct ColumnBinding { | ||
pub table_name: String, | ||
pub index: usize, | ||
pub data_type: DataType, | ||
} | ||
|
||
impl ColumnBinding { | ||
pub fn new(table_name: String, index: usize, data_type: DataType) -> Self { | ||
ColumnBinding { | ||
table_name, | ||
index, | ||
data_type, | ||
} | ||
} | ||
} | ||
|
||
pub struct BindContext { | ||
// Mapping column name to `ColumnBinding` | ||
pub columns: HashMap<String, Vec<ColumnBinding>>, | ||
} | ||
|
||
impl BindContext { | ||
#[allow(clippy::new_without_default)] | ||
pub fn new() -> Self { | ||
BindContext { | ||
// tables: HashMap::new(), | ||
columns: HashMap::new(), | ||
} | ||
} | ||
} |
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,67 @@ | ||
use risingwave_common::array::RwError; | ||
use risingwave_common::error::{ErrorCode, Result}; | ||
use risingwave_sqlparser::ast::Ident; | ||
|
||
use crate::binder::Binder; | ||
use crate::expr::{ExprImpl, InputRef}; | ||
|
||
impl Binder { | ||
pub fn bind_column(&mut self, idents: &[Ident]) -> Result<ExprImpl> { | ||
// TODO: check quote style of `ident`. | ||
let (_schema_name, table_name, column_name) = match idents { | ||
[column] => (None, None, &column.value), | ||
[table, column] => (None, Some(&table.value), &column.value), | ||
[schema, table, column] => (Some(&schema.value), Some(&table.value), &column.value), | ||
_ => { | ||
return Err( | ||
ErrorCode::InternalError(format!("Too many idents: {:?}", idents)).into(), | ||
) | ||
} | ||
}; | ||
let columns = self.context.columns.get(column_name).ok_or_else(|| { | ||
RwError::from(ErrorCode::ItemNotFound(format!( | ||
"Invalid column: {}", | ||
column_name | ||
))) | ||
})?; | ||
match table_name { | ||
Some(table_name) => { | ||
match columns | ||
.iter() | ||
.find(|column| column.table_name == *table_name) | ||
{ | ||
Some(column) => Ok(ExprImpl::InputRef(Box::new(InputRef::new( | ||
column.index, | ||
column.data_type.clone(), | ||
)))), | ||
None => Err( | ||
ErrorCode::ItemNotFound(format!("Invalid table: {}", table_name)).into(), | ||
), | ||
} | ||
} | ||
None => { | ||
if columns.len() > 1 { | ||
Err(ErrorCode::InternalError("Ambiguous column name".into()).into()) | ||
} else { | ||
Ok(ExprImpl::InputRef(Box::new(InputRef::new( | ||
columns[0].index, | ||
columns[0].data_type.clone(), | ||
)))) | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub fn bind_all_columns(&mut self) -> Result<Vec<ExprImpl>> { | ||
let mut bound_columns = vec![]; | ||
self.context.columns.values().for_each(|columns| { | ||
columns.iter().for_each(|column| { | ||
bound_columns.push(ExprImpl::InputRef(Box::new(InputRef::new( | ||
column.index, | ||
column.data_type.clone(), | ||
)))); | ||
}); | ||
}); | ||
Ok(bound_columns) | ||
} | ||
} |
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,25 @@ | ||
use risingwave_common::error::Result; | ||
use risingwave_sqlparser::ast::SelectItem; | ||
|
||
use crate::binder::Binder; | ||
use crate::expr::ExprImpl; | ||
|
||
impl Binder { | ||
pub fn bind_projection(&mut self, projection: Vec<SelectItem>) -> Result<Vec<ExprImpl>> { | ||
let mut select_list = vec![]; | ||
for item in projection { | ||
match item { | ||
SelectItem::UnnamedExpr(expr) => { | ||
let expr = self.bind_expr(expr)?; | ||
select_list.push(expr); | ||
} | ||
SelectItem::ExprWithAlias { .. } => todo!(), | ||
SelectItem::QualifiedWildcard(_) => todo!(), | ||
SelectItem::Wildcard => { | ||
select_list.extend(self.bind_all_columns()?.into_iter()); | ||
} | ||
} | ||
} | ||
Ok(select_list) | ||
} | ||
} |
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