-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(planner): support logical filter
Signed-off-by: Fedomn <fedomn.ma@gmail.com>
Showing
7 changed files
with
156 additions
and
5 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
32 changes: 32 additions & 0 deletions
32
src/planner_v2/binder/expression_binder/column_alias_binder.rs
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,32 @@ | ||
use std::collections::HashMap; | ||
|
||
use derive_new::new; | ||
use expression_binder::ExpressionBinder; | ||
|
||
use crate::planner_v2::{expression_binder, BindError, BoundExpression}; | ||
|
||
/// A helper binder for WhereBinder and HavingBinder which support alias as a columnref. | ||
#[derive(new)] | ||
pub struct ColumnAliasBinder<'a> { | ||
pub(crate) original_select_items: &'a [sqlparser::ast::Expr], | ||
pub(crate) alias_map: &'a HashMap<String, usize>, | ||
} | ||
|
||
impl<'a> ColumnAliasBinder<'a> { | ||
pub fn bind_alias( | ||
&self, | ||
expression_binder: &mut ExpressionBinder, | ||
expr: &sqlparser::ast::Expr, | ||
) -> Result<BoundExpression, BindError> { | ||
if let sqlparser::ast::Expr::Identifier(ident) = expr { | ||
let alias = ident.to_string(); | ||
if let Some(alias_entry) = self.alias_map.get(&alias) { | ||
let expr = self.original_select_items[*alias_entry].clone(); | ||
let bound_expr = | ||
expression_binder.bind_expression(&expr, &mut vec![], &mut vec![])?; | ||
return Ok(bound_expr); | ||
} | ||
} | ||
Err(BindError::Internal(format!("column not found: {}", expr))) | ||
} | ||
} |
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,4 @@ | ||
mod column_alias_binder; | ||
mod where_binder; | ||
pub use column_alias_binder::*; | ||
pub use where_binder::*; |
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,49 @@ | ||
use derive_new::new; | ||
|
||
use super::ColumnAliasBinder; | ||
use crate::planner_v2::{BindError, BoundExpression, ExpressionBinder}; | ||
use crate::types_v2::LogicalType; | ||
|
||
/// The WHERE binder is responsible for binding an expression within the WHERE clause of a SQL | ||
/// statement | ||
#[derive(new)] | ||
pub struct WhereBinder<'a> { | ||
internal_binder: ExpressionBinder<'a>, | ||
column_alias_binder: ColumnAliasBinder<'a>, | ||
} | ||
|
||
impl<'a> WhereBinder<'a> { | ||
pub fn bind_expression( | ||
&mut self, | ||
expr: &sqlparser::ast::Expr, | ||
result_names: &mut Vec<String>, | ||
result_types: &mut Vec<LogicalType>, | ||
) -> Result<BoundExpression, BindError> { | ||
match expr { | ||
sqlparser::ast::Expr::Identifier(..) | sqlparser::ast::Expr::CompoundIdentifier(..) => { | ||
self.bind_column_ref_expr(expr, result_names, result_types) | ||
} | ||
other => self | ||
.internal_binder | ||
.bind_expression(other, result_names, result_types), | ||
} | ||
} | ||
|
||
fn bind_column_ref_expr( | ||
&mut self, | ||
expr: &sqlparser::ast::Expr, | ||
result_names: &mut Vec<String>, | ||
result_types: &mut Vec<LogicalType>, | ||
) -> Result<BoundExpression, BindError> { | ||
// bind column ref expr first | ||
let bind_res = self | ||
.internal_binder | ||
.bind_expression(expr, result_names, result_types); | ||
if bind_res.is_ok() { | ||
return bind_res; | ||
} | ||
// try to bind as alias | ||
self.column_alias_binder | ||
.bind_alias(&mut self.internal_binder, expr) | ||
} | ||
} |
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