Skip to content

Commit

Permalink
Parse not in expr
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Sep 26, 2023
1 parent b32d199 commit d85fa32
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
33 changes: 29 additions & 4 deletions src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ fn prefix(i: Input) -> Result<(Input, PrattExpr), String> {
Ok((i, PrattExpr::Expr(expr)))
}
Plus => {
let (i, pratt_expr) = pratt_parse(i.slice(1..), precedence(PrattOp::Plus, AffixKind::Prefix)?)?;
let (i, pratt_expr) =
pratt_parse(i.slice(1..), precedence(PrattOp::Plus, AffixKind::Prefix)?)?;
Ok((
i,
PrattExpr::Expr(Expr::UnaryOp {
Expand All @@ -107,7 +108,8 @@ fn prefix(i: Input) -> Result<(Input, PrattExpr), String> {
))
}
Minus => {
let (i, pratt_expr) = pratt_parse(i.slice(1..), precedence(PrattOp::Minus, AffixKind::Prefix)?)?;
let (i, pratt_expr) =
pratt_parse(i.slice(1..), precedence(PrattOp::Minus, AffixKind::Prefix)?)?;
Ok((
i,
PrattExpr::Expr(Expr::UnaryOp {
Expand Down Expand Up @@ -156,7 +158,8 @@ fn infix(i: Input, pratt_left: PrattExpr) -> Result<(Input, PrattExpr), String>
))
}
Multiply => {
let (i, pratt_right) = pratt_parse(i, precedence(PrattOp::Multiply, AffixKind::Infix)?)?;
let (i, pratt_right) =
pratt_parse(i, precedence(PrattOp::Multiply, AffixKind::Infix)?)?;
Ok((
i,
PrattExpr::Expr(Expr::BinaryOp {
Expand Down Expand Up @@ -276,6 +279,29 @@ fn infix(i: Input, pratt_left: PrattExpr) -> Result<(Input, PrattExpr), String>
}),
))
}
NOT => {
let Some(token) = i.get(0) else {
return Err("No token found".to_string());
};
match token.kind {
IN => {
let i = i.slice(1..);
let (i, pratt_right) =
pratt_parse(i, precedence(PrattOp::In, AffixKind::Infix)?)?;
Ok((
i,
PrattExpr::Expr(Expr::InSubquery {
not: true,
expr: Box::new(pratt_left.into_expr()),
subquery: Box::new(pratt_right.into_query()),
}),
))
}
_ => {
return Err(format!("Not support pratt operator: not {}", token.kind));
}
}
}
_ => {
return Err("The token can't be treated as infix".to_string());
}
Expand Down Expand Up @@ -347,7 +373,6 @@ fn precedence(op: PrattOp, affix: AffixKind) -> Result<u32, String> {
AffixKind::Prefix => match op {
PrattOp::Plus | PrattOp::Minus => Ok(300),
_ => Err(format!("pratt op {:?} can't be treated as prefix", op)),

},
AffixKind::Infix => match op {
PrattOp::In => Ok(7),
Expand Down
5 changes: 4 additions & 1 deletion tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ pub fn test_query() {
r#"select * from x where column_1 in (select column_1 from x);"#,
r#"SELECT * FROM x WHERE column_1 IN (SELECT column_1 FROM x)"#,
),
// (r#"select * from x where column_1 not in (select column_1 from x);"#, r#""#),
(
r#"select * from x where column_1 not in (select column_1 from x);"#,
r#"SELECT * FROM x WHERE column_1 NOT IN (SELECT column_1 FROM x)"#,
),
// (r#"select * from x y where column_1 < (select sum(column_2) from x where x.column_1 = y.column_1);"#, r#""#),
// window function
(
Expand Down

0 comments on commit d85fa32

Please sign in to comment.