Skip to content

Commit

Permalink
Fix function parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
lewiszlw committed Sep 25, 2023
1 parent 2ef129a commit 71cfcf0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nom::{branch::alt, sequence::tuple};
use nom::{Parser, Slice};

use crate::ast::expr::{BinaryOp, Expr, FunctionArg, Literal, UnaryOp, Window, WindowSpec};
use crate::parser::common::{AffixKind, MIN_PRECEDENCE};
use crate::parser::common::{comma_separated_list0, AffixKind, MIN_PRECEDENCE};
use crate::parser::error::PError;
use crate::parser::statement::order_by_expr;
use crate::parser::token::{LParen, RParen, Token, TokenKind, BY, ORDER, OVER, PARTITION};
Expand Down Expand Up @@ -311,7 +311,7 @@ fn function_expr(i: Input) -> IResult<Expr> {
ident,
match_token(TokenKind::LParen),
opt(match_token(TokenKind::DISTINCT)),
comma_separated_list1(function_arg),
comma_separated_list0(function_arg),
match_token(TokenKind::RParen),
opt(window),
))(i)
Expand Down
1 change: 1 addition & 0 deletions src/parser/table_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ fn join_operator(i: Input) -> IResult<JoinOp> {
tuple((match_token(FULL), match_token(JOIN))).map(|(_, _)| JoinOp::FullOuter),
tuple((match_token(FULL), match_token(OUTER), match_token(JOIN)))
.map(|(_, _, _)| JoinOp::FullOuter),
tuple((match_token(CROSS), match_token(JOIN))).map(|(_, _)| JoinOp::CrossJoin),
))(i)
}

Expand Down
4 changes: 4 additions & 0 deletions src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ pub enum TokenKind {
#[token("BY", ignore(ascii_case))]
BY,

#[token("CROSS", ignore(ascii_case))]
CROSS,

#[token("DESC", ignore(ascii_case))]
DESC,

Expand Down Expand Up @@ -310,6 +313,7 @@ impl std::fmt::Display for TokenKind {
AS => write!(f, "AS"),
ASC => write!(f, "ASC"),
BY => write!(f, "BY"),
CROSS => write!(f, "CROSS"),
DESC => write!(f, "DESC"),
DISTINCT => write!(f, "DISTINCT"),
EXISTS => write!(f, "EXISTS"),
Expand Down
6 changes: 5 additions & 1 deletion tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ pub fn test_query() {
r#"SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary;"#,
r#"SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname) FROM empsalary"#,
),
// (r#"SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary DESC) FROM empsalary;"#, r#""#),
(
r#"SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary DESC) FROM empsalary;"#,
r#"SELECT depname, empno, salary, rank() OVER (PARTITION BY depname ORDER BY salary DESC) FROM empsalary"#,
),
// (r#"SELECT depname, empno, salary, avg(salary) OVER(ORDER BY salary ASC ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS avg, min(salary) OVER(ORDER BY empno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS cum_min FROM empsalary ORDER BY empno ASC;"#, r#""#),
(
r#"SELECT sum(salary) OVER w, avg(salary) OVER w FROM empsalary WINDOW w AS (PARTITION BY depname ORDER BY salary DESC);"#,
Expand All @@ -98,6 +101,7 @@ pub fn test_query() {
];
for (input, output) in &cases {
let result = parse_query(input);
println!("{:?}", result);
assert!(result.is_ok());
let result = result.unwrap();
assert_eq!(output, &result.to_string());
Expand Down

0 comments on commit 71cfcf0

Please sign in to comment.