Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/binder/expression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use crate::types::ScalarValue;
pub enum BoundExpr {
Constant(ScalarValue),
ColumnRef(BoundColumnRef),
/// InputRef represents an index of the RecordBatch, which is resolved in optimizer.
InputRef(BoundInputRef),
BinaryOp(BoundBinaryOp),
TypeCast(BoundTypeCast),
AggFunc(BoundAggFunc),
Alias(BoundAlias),
}

impl BoundExpr {
Expand All @@ -34,6 +34,7 @@ impl BoundExpr {
BoundExpr::BinaryOp(binary_op) => binary_op.return_type.clone(),
BoundExpr::TypeCast(tc) => Some(tc.cast_type.clone()),
BoundExpr::AggFunc(agg) => Some(agg.return_type.clone()),
BoundExpr::Alias(alias) => alias.expr.return_type(),
}
}

Expand All @@ -47,6 +48,7 @@ impl BoundExpr {
}
BoundExpr::TypeCast(tc) => tc.expr.contains_column_ref(),
BoundExpr::AggFunc(agg) => agg.exprs.iter().any(|arg| arg.contains_column_ref()),
BoundExpr::Alias(alias) => alias.expr.contains_column_ref(),
}
}

Expand All @@ -67,6 +69,7 @@ impl BoundExpr {
.iter()
.flat_map(|arg| arg.get_column_catalog())
.collect::<Vec<_>>(),
BoundExpr::Alias(alias) => alias.expr.get_column_catalog(),
}
}
}
Expand All @@ -90,6 +93,12 @@ pub struct BoundTypeCast {
pub cast_type: DataType,
}

#[derive(Clone, PartialEq, Eq)]
pub struct BoundAlias {
pub expr: Box<BoundExpr>,
pub alias: String,
}

impl Binder {
/// bind sqlparser Expr into BoundExpr
pub fn bind_expr(&mut self, expr: &Expr) -> Result<BoundExpr, BindError> {
Expand Down Expand Up @@ -150,6 +159,12 @@ impl Binder {
got_column = Some(column_catalog);
}
}
// handle col alias
if got_column.is_none() {
if let Some(expr) = self.context.aliases.get(column_name) {
return Ok(expr.clone());
}
}
let column_catalog =
got_column.ok_or_else(|| BindError::InvalidColumn(column_name.clone()))?;
Ok(BoundExpr::ColumnRef(BoundColumnRef { column_catalog }))
Expand All @@ -166,6 +181,7 @@ impl fmt::Debug for BoundExpr {
BoundExpr::BinaryOp(binary_op) => write!(f, "{:?}", binary_op),
BoundExpr::TypeCast(type_cast) => write!(f, "{:?}", type_cast),
BoundExpr::AggFunc(agg_func) => write!(f, "{:?}", agg_func),
BoundExpr::Alias(alias) => write!(f, "{:?} as {}", alias.expr, alias.alias),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/binder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct BinderContext {
/// table_name == table_id
/// table_id -> table_catalog
tables: HashMap<String, TableCatalog>,
aliases: HashMap<String, BoundExpr>,
}

impl Binder {
Expand Down
11 changes: 9 additions & 2 deletions src/binder/statement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use sqlparser::ast::{Query, SelectItem};

use super::expression::BoundExpr;
use super::table::BoundTableRef;
use super::{BindError, Binder, BoundColumnRef};
use super::{BindError, Binder, BoundAlias, BoundColumnRef};

#[derive(Debug)]
pub enum BoundStatement {
Expand Down Expand Up @@ -50,7 +50,14 @@ impl Binder {
let expr = self.bind_expr(expr)?;
select_list.push(expr);
}
SelectItem::ExprWithAlias { expr: _, alias: _ } => todo!(),
SelectItem::ExprWithAlias { expr, alias } => {
let expr = self.bind_expr(expr)?;
self.context.aliases.insert(alias.to_string(), expr.clone());
select_list.push(BoundExpr::Alias(BoundAlias {
expr: Box::new(expr),
alias: alias.to_string().to_lowercase(),
}));
}
SelectItem::QualifiedWildcard(object_name) => {
let qualifier = format!("{}", object_name);
select_list.extend_from_slice(
Expand Down
6 changes: 6 additions & 0 deletions src/executor/evaluator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ impl BoundExpr {
BoundExpr::ColumnRef(_) => panic!("column ref should be resolved"),
BoundExpr::TypeCast(tc) => Ok(cast(&tc.expr.eval_column(batch)?, &tc.cast_type)?),
BoundExpr::AggFunc(_) => todo!(),
BoundExpr::Alias(alias) => alias.expr.eval_column(batch),
}
}

Expand Down Expand Up @@ -52,6 +53,11 @@ impl BoundExpr {
let new_name = format!("{}({})", agg.func, inner_name);
Field::new(new_name.as_str(), agg.return_type.clone(), true)
}
BoundExpr::Alias(alias) => {
let new_name = alias.alias.to_string();
let data_type = alias.expr.return_type().unwrap();
Field::new(new_name.as_str(), data_type, true)
}
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/optimizer/expr_rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub trait ExprRewriter {
BoundExpr::BinaryOp(_) => self.rewrite_binary_op(expr),
BoundExpr::TypeCast(_) => self.rewrite_type_cast(expr),
BoundExpr::AggFunc(_) => self.rewrite_agg_func(expr),
BoundExpr::Alias(_) => self.rewrite_alias(expr),
}
}

Expand Down Expand Up @@ -40,4 +41,13 @@ pub trait ExprRewriter {
_ => unreachable!(),
}
}

fn rewrite_alias(&self, expr: &mut BoundExpr) {
match expr {
BoundExpr::Alias(e) => {
self.rewrite_expr(&mut e.expr);
}
_ => unreachable!(),
}
}
}
8 changes: 7 additions & 1 deletion src/optimizer/expr_visitor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::binder::{
BoundAggFunc, BoundBinaryOp, BoundColumnRef, BoundExpr, BoundInputRef, BoundTypeCast,
BoundAggFunc, BoundAlias, BoundBinaryOp, BoundColumnRef, BoundExpr, BoundInputRef,
BoundTypeCast,
};
use crate::types::ScalarValue;

Expand All @@ -15,6 +16,7 @@ pub trait ExprVisitor {
BoundExpr::BinaryOp(expr) => self.visit_binary_op(expr),
BoundExpr::TypeCast(expr) => self.visit_type_cast(expr),
BoundExpr::AggFunc(expr) => self.visit_agg_func(expr),
BoundExpr::Alias(expr) => self.visit_alias(expr),
}
}

Expand All @@ -38,4 +40,8 @@ pub trait ExprVisitor {
self.visit_expr(arg);
}
}

fn visit_alias(&mut self, expr: &BoundAlias) {
self.visit_expr(&expr.expr);
}
}
5 changes: 5 additions & 0 deletions src/optimizer/input_ref_rewriter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl InputRefRewriter {
self.rewrite_expr(arg);
}
}
BoundExpr::Alias(e) => self.rewrite_expr(e.expr.as_mut()),
_ => unreachable!(
"unexpected expr type {:?} for InputRefRewriter, binding: {:?}",
expr, self.bindings
Expand All @@ -62,6 +63,10 @@ impl ExprRewriter for InputRefRewriter {
fn rewrite_agg_func(&self, expr: &mut BoundExpr) {
self.rewrite_internal(expr);
}

fn rewrite_alias(&self, expr: &mut BoundExpr) {
self.rewrite_internal(expr);
}
}

impl PlanRewriter for InputRefRewriter {
Expand Down
35 changes: 30 additions & 5 deletions src/optimizer/rules/column_pruning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ impl Rule for PushProjectIntoTableScan {
.get_plan_ref()
.as_logical_project()
.unwrap();

// only push down the project when the exprs are all column refs
for expr in project_node.exprs().iter() {
match expr {
BoundExpr::ColumnRef(_) => {}
_ => return,
}
}

let table_scan_node = table_scan_opt_expr
.root
.get_plan_ref()
Expand Down Expand Up @@ -234,11 +243,27 @@ mod tests {

#[test]
fn test_push_project_into_table_scan_rule() {
let tests = vec![RuleTest {
name: "push_project_into_table_scan_rule",
sql: "select a from t1",
expect: "LogicalTableScan: table: #t1, columns: [a]",
}];
let tests = vec![
RuleTest {
name: "push_project_into_table_scan_rule",
sql: "select a from t1",
expect: "LogicalTableScan: table: #t1, columns: [a]",
},
RuleTest {
name: "should not push when project has alias",
sql: "select a as c1 from t1",
expect: r"
LogicalProject: exprs [t1.a:Nullable(Int32) as c1]
LogicalTableScan: table: #t1, columns: [a, b, c]",
},
RuleTest {
name: "should not push when project expr is not column",
sql: "select a + 1 from t1",
expect: r"
LogicalProject: exprs [t1.a:Nullable(Int32) + 1]
LogicalTableScan: table: #t1, columns: [a, b, c]",
},
];

for t in tests {
let logical_plan = build_plan(t.sql);
Expand Down
16 changes: 16 additions & 0 deletions tests/slt/alias.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
query I
select a as c1 from t1 order by c1 desc limit 1;
----
2

query I
select a as c1 from t1 where c1 = 1;
----
1

query II
select sum(b) as c1, a as c2 from t1 group by c2 order by c1 desc;
----
15 2
5 1
4 0