Skip to content

Commit e9ad922

Browse files
authored
Merge pull request #28 from KKould/main
feat(binder): implement `Binder::bind_expr`
2 parents d284291 + 60ddf01 commit e9ad922

6 files changed

Lines changed: 518 additions & 28 deletions

File tree

src/binder/aggregate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ impl Binder {
206206
};
207207

208208
let mut select_item = &mut select_list[i];
209-
let return_type = select_item.return_type().unwrap();
209+
let return_type = select_item.return_type();
210210
self.context.group_by_exprs.push(std::mem::replace(
211211
&mut select_item,
212212
ScalarExpression::InputRef {
@@ -234,7 +234,7 @@ impl Binder {
234234
expr,
235235
ScalarExpression::InputRef {
236236
index,
237-
ty: expr.return_type().unwrap(),
237+
ty: expr.return_type(),
238238
},
239239
))
240240
}

src/binder/expr.rs

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
use crate::binder::BindError;
22
use anyhow::Result;
33
use itertools::Itertools;
4-
use sqlparser::ast::{Expr, Ident};
4+
use sqlparser::ast::{BinaryOperator, Expr, Function, FunctionArg, FunctionArgExpr, Ident};
55
use std::slice;
6+
use crate::expression::agg::AggKind;
67

78
use super::Binder;
89
use crate::expression::ScalarExpression;
10+
use crate::types::LogicalType;
911

1012
impl Binder {
1113
pub(crate) fn bind_expr(&mut self, expr: &Expr) -> Result<ScalarExpression> {
1214
match expr {
1315
Expr::Identifier(ident) => {
1416
self.bind_column_ref_from_identifiers(slice::from_ref(ident))
1517
}
18+
Expr::CompoundIdentifier(idents) => {
19+
self.bind_column_ref_from_identifiers(idents)
20+
}
21+
Expr::BinaryOp { left, right, op} => {
22+
self.bind_binary_op_internal(left, right, op)
23+
}
24+
Expr::Value(v) => Ok(ScalarExpression::Constant(v.into())),
25+
Expr::Function(func) => self.bind_agg_call(func),
26+
Expr::Nested(expr) => self.bind_expr(expr),
1627
_ => {
1728
todo!()
1829
}
@@ -75,4 +86,71 @@ impl Binder {
7586
Ok(ScalarExpression::ColumnRef(column_catalog.clone()))
7687
}
7788
}
89+
90+
fn bind_binary_op_internal(
91+
&mut self,
92+
left: &Expr,
93+
right: &Expr,
94+
op: &BinaryOperator,
95+
) -> Result<ScalarExpression> {
96+
let left_expr = Box::new(self.bind_expr(left)?);
97+
let right_expr = Box::new(self.bind_expr(right)?);
98+
let ty = LogicalType::max_logical_type(
99+
&left_expr.return_type(),
100+
&right_expr.return_type()
101+
)?;
102+
103+
Ok(ScalarExpression::Binary {
104+
op: (op.clone()).into(),
105+
left_expr,
106+
right_expr,
107+
ty,
108+
})
109+
}
110+
111+
fn bind_agg_call(&mut self, func: &Function) -> Result<ScalarExpression> {
112+
let args: Vec<ScalarExpression> = func.args
113+
.iter()
114+
.map(|arg| {
115+
let arg_expr = match arg {
116+
FunctionArg::Named { arg, .. } => arg,
117+
FunctionArg::Unnamed(arg) => arg,
118+
};
119+
match arg_expr {
120+
FunctionArgExpr::Expr(expr) => self.bind_expr(expr),
121+
_ => todo!()
122+
}
123+
})
124+
.try_collect()?;
125+
let ty = args[0].return_type();
126+
127+
Ok(match func.name.to_string().to_lowercase().as_str() {
128+
"count" => ScalarExpression::AggCall{
129+
kind: AggKind::Count,
130+
args,
131+
ty: LogicalType::UInteger,
132+
},
133+
"sum" => ScalarExpression::AggCall{
134+
kind: AggKind::Sum,
135+
args,
136+
ty,
137+
},
138+
"min" => ScalarExpression::AggCall{
139+
kind: AggKind::Min,
140+
args,
141+
ty,
142+
},
143+
"max" => ScalarExpression::AggCall{
144+
kind: AggKind::Max,
145+
args,
146+
ty,
147+
},
148+
"avg" => ScalarExpression::AggCall{
149+
kind: AggKind::Avg,
150+
args,
151+
ty,
152+
},
153+
_ => todo!(),
154+
})
155+
}
78156
}

0 commit comments

Comments
 (0)