Skip to content

Commit 071732e

Browse files
committed
chore: codefmt
1 parent edc5ad6 commit 071732e

File tree

3 files changed

+63
-68
lines changed

3 files changed

+63
-68
lines changed

src/binder/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ impl<'a, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'a, '
572572

573573
pub fn bind_set_expr(&mut self, set_expr: &SetExpr) -> Result<LogicalPlan, DatabaseError> {
574574
match set_expr {
575-
SetExpr::Select(select) => self.bind_select(select, &[]),
575+
SetExpr::Select(select) => self.bind_select(select, None),
576576
SetExpr::Query(query) => self.bind_query(query),
577577
SetExpr::SetOperation {
578578
op,

src/binder/select.rs

Lines changed: 52 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ use crate::types::{ColumnId, LogicalType};
5454
use itertools::Itertools;
5555
use sqlparser::ast::{
5656
CharLengthUnits, Distinct, Expr, GroupByExpr, Join, JoinConstraint, JoinOperator, LimitClause,
57-
Offset, OffsetRows, OrderByExpr, OrderByKind, Query, Select, SelectInto, SelectItem,
57+
OrderByExpr, OrderByKind, Query, Select, SelectInto, SelectItem,
5858
SelectItemQualifiedWildcardKind, SetExpr, SetOperator, SetQuantifier, TableAlias,
5959
TableAliasColumnDef, TableFactor, TableWithJoins,
6060
};
@@ -69,18 +69,18 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
6969

7070
let order_by_exprs = if let Some(order_by) = &query.order_by {
7171
match &order_by.kind {
72-
OrderByKind::Expressions(exprs) => exprs.clone(),
72+
OrderByKind::Expressions(exprs) => Some(exprs.as_slice()),
7373
OrderByKind::All(_) => {
7474
return Err(DatabaseError::UnsupportedStmt(
7575
"ORDER BY ALL is not supported".to_string(),
7676
))
7777
}
7878
}
7979
} else {
80-
vec![]
80+
None
8181
};
8282
let mut plan = match query.body.borrow() {
83-
SetExpr::Select(select) => self.bind_select(select, &order_by_exprs),
83+
SetExpr::Select(select) => self.bind_select(select, order_by_exprs),
8484
SetExpr::Query(query) => self.bind_query(query),
8585
SetExpr::SetOperation {
8686
op,
@@ -96,31 +96,8 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
9696
}
9797
}?;
9898

99-
let (limit, offset) = match &query.limit_clause {
100-
Some(LimitClause::LimitOffset {
101-
limit,
102-
offset,
103-
limit_by,
104-
}) => {
105-
if !limit_by.is_empty() {
106-
return Err(DatabaseError::UnsupportedStmt(
107-
"LIMIT BY is not supported".to_string(),
108-
));
109-
}
110-
(limit.clone(), offset.clone())
111-
}
112-
Some(LimitClause::OffsetCommaLimit { offset, limit }) => (
113-
Some(limit.clone()),
114-
Some(Offset {
115-
value: offset.clone(),
116-
rows: OffsetRows::None,
117-
}),
118-
),
119-
None => (None, None),
120-
};
121-
122-
if limit.is_some() || offset.is_some() {
123-
plan = self.bind_limit(plan, &limit, &offset)?;
99+
if let Some(limit_clause) = query.limit_clause.clone() {
100+
plan = self.bind_limit(plan, limit_clause)?;
124101
}
125102

126103
self.context.step(origin_step);
@@ -130,7 +107,7 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
130107
pub(crate) fn bind_select(
131108
&mut self,
132109
select: &Select,
133-
orderby: &[OrderByExpr],
110+
orderby: Option<&[OrderByExpr]>,
134111
) -> Result<LogicalPlan, DatabaseError> {
135112
let mut plan = if select.from.is_empty() {
136113
LogicalPlan::new(Operator::Dummy, Childrens::None)
@@ -177,8 +154,9 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
177154

178155
let mut having_orderby = (None, None);
179156

180-
if select.having.is_some() || !orderby.is_empty() {
181-
having_orderby = self.extract_having_orderby_aggregate(&select.having, orderby)?;
157+
if select.having.is_some() || orderby.is_some() {
158+
having_orderby =
159+
self.extract_having_orderby_aggregate(&select.having, orderby.unwrap_or(&[]))?;
182160
}
183161

184162
if !self.context.agg_calls.is_empty() || !self.context.group_by_exprs.is_empty() {
@@ -953,51 +931,60 @@ impl<'a: 'b, 'b, T: Transaction, A: AsRef<[(&'static str, DataValue)]>> Binder<'
953931
)
954932
}
955933

934+
fn bind_non_negative_limit_value(&mut self, expr: &Expr) -> Result<usize, DatabaseError> {
935+
let bound_expr = self.bind_expr(expr)?;
936+
match bound_expr {
937+
ScalarExpression::Constant(dv) => match &dv {
938+
DataValue::Int32(v) if *v >= 0 => Ok(*v as usize),
939+
DataValue::Int64(v) if *v >= 0 => Ok(*v as usize),
940+
_ => Err(DatabaseError::InvalidType),
941+
},
942+
_ => Err(attach_span_if_absent(
943+
DatabaseError::invalid_column("invalid limit expression.".to_owned()),
944+
expr,
945+
)),
946+
}
947+
}
948+
956949
fn bind_limit(
957950
&mut self,
958951
children: LogicalPlan,
959-
limit_expr: &Option<Expr>,
960-
offset_expr: &Option<Offset>,
952+
limit: LimitClause,
961953
) -> Result<LogicalPlan, DatabaseError> {
962954
self.context.step(QueryBindStep::Limit);
963955

964-
let mut limit = None;
965-
let mut offset = None;
966-
if let Some(limit_ast) = limit_expr {
967-
let bound_limit = self.bind_expr(limit_ast)?;
968-
match bound_limit {
969-
ScalarExpression::Constant(dv) => match &dv {
970-
DataValue::Int32(v) if *v >= 0 => limit = Some(*v as usize),
971-
DataValue::Int64(v) if *v >= 0 => limit = Some(*v as usize),
972-
_ => return Err(DatabaseError::InvalidType),
973-
},
974-
_ => {
975-
return Err(attach_span_if_absent(
976-
DatabaseError::invalid_column("invalid limit expression.".to_owned()),
977-
limit_ast,
978-
))
956+
let mut limit_value = None;
957+
let mut offset_value = None;
958+
match limit {
959+
LimitClause::LimitOffset {
960+
limit: limit_expr,
961+
offset: offset_expr,
962+
limit_by,
963+
} => {
964+
if !limit_by.is_empty() {
965+
return Err(DatabaseError::UnsupportedStmt(
966+
"LIMIT BY is not supported".to_string(),
967+
));
979968
}
980-
}
981-
}
982969

983-
if let Some(offset_ast) = offset_expr {
984-
let bound_offset = self.bind_expr(&offset_ast.value)?;
985-
match bound_offset {
986-
ScalarExpression::Constant(dv) => match &dv {
987-
DataValue::Int32(v) if *v >= 0 => offset = Some(*v as usize),
988-
DataValue::Int64(v) if *v >= 0 => offset = Some(*v as usize),
989-
_ => return Err(DatabaseError::InvalidType),
990-
},
991-
_ => {
992-
return Err(attach_span_if_absent(
993-
DatabaseError::invalid_column("invalid limit expression.".to_owned()),
994-
&offset_ast.value,
995-
))
970+
if let Some(limit_ast) = limit_expr.as_ref() {
971+
limit_value = Some(self.bind_non_negative_limit_value(limit_ast)?);
996972
}
973+
974+
if let Some(offset_ast) = offset_expr.as_ref() {
975+
offset_value = Some(self.bind_non_negative_limit_value(&offset_ast.value)?);
976+
}
977+
}
978+
LimitClause::OffsetCommaLimit {
979+
offset: offset_expr,
980+
limit: limit_expr,
981+
} => {
982+
limit_value = Some(self.bind_non_negative_limit_value(&limit_expr)?);
983+
offset_value = Some(self.bind_non_negative_limit_value(&offset_expr)?);
997984
}
998985
}
999986

1000-
Ok(LimitOperator::build(offset, limit, children))
987+
Ok(LimitOperator::build(offset_value, limit_value, children))
1001988
}
1002989

1003990
pub fn extract_select_join(&mut self, select_items: &mut [ScalarExpression]) {

src/types/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ impl TryFrom<sqlparser::ast::DataType> for LogicalType {
402402
len = cmp::max(len, length);
403403
char_unit = unit;
404404
}
405-
sqlparser::ast::CharacterLength::Max => {}
405+
sqlparser::ast::CharacterLength::Max => {
406+
return Err(DatabaseError::UnsupportedStmt(
407+
"CHAR(MAX) is not supported".to_string(),
408+
));
409+
}
406410
}
407411
}
408412
Ok(LogicalType::Char(
@@ -421,7 +425,11 @@ impl TryFrom<sqlparser::ast::DataType> for LogicalType {
421425
len = Some(length as u32);
422426
char_unit = unit;
423427
}
424-
sqlparser::ast::CharacterLength::Max => {}
428+
sqlparser::ast::CharacterLength::Max => {
429+
return Err(DatabaseError::UnsupportedStmt(
430+
"VARCHAR(MAX) is not supported".to_string(),
431+
));
432+
}
425433
}
426434
}
427435
Ok(LogicalType::Varchar(

0 commit comments

Comments
 (0)