Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix interval parsing logic and precedence #705

Merged
merged 6 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
32 changes: 31 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,36 @@ impl<'a> Parser<'a> {
Ok(expr)
}

pub fn parse_interval_expr(&mut self) -> Result<Expr, ParserError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you investigated following a similar pattern to NOT which adjusts the precedence based on peeking in the token stream?

https://github.com/sqlparser-rs/sqlparser-rs/blob/87b4a168cbcd83439c4e8729dd992d150eebd15c/src/parser.rs#L1648-L1660

I couldn't find any example of a similar partten in the codebase

https://github.com/search?q=repo%3Asqlparser-rs%2Fsqlparser-rs%20parse_infix&type=code

What do you think @AugustoFKL ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alamb I think we may need to refactor the expression parsing...

With the current structure, we end up with no such good expansion of expressions to parse. And there's still the (big) problem with precedence analysis.

But, for now, and considering the scope of this PR, I think that's OK. Doing it for NOT would be better in another PR, right?

let precedence = 0;
let mut expr = self.parse_prefix()?;

loop {
let next_precedence = self.get_next_interval_precedence()?;

if precedence >= next_precedence {
break;
}

expr = self.parse_infix(expr, next_precedence)?;
}

Ok(expr)
}

/// Get the precedence of the next token
/// With AND, OR, and XOR
pub fn get_next_interval_precedence(&self) -> Result<u8, ParserError> {
let token = self.peek_token();

match token {
Token::Word(w) if w.keyword == Keyword::AND => Ok(0),
Token::Word(w) if w.keyword == Keyword::OR => Ok(0),
Token::Word(w) if w.keyword == Keyword::XOR => Ok(0),
_ => self.get_next_precedence(),
}
}

pub fn parse_assert(&mut self) -> Result<Statement, ParserError> {
let condition = self.parse_expr()?;
let message = if self.parse_keyword(Keyword::AS) {
Expand Down Expand Up @@ -1151,7 +1181,7 @@ impl<'a> Parser<'a> {

// The first token in an interval is a string literal which specifies
// the duration of the interval.
let value = self.parse_expr()?;
let value = self.parse_interval_expr()?;

// Following the string literal is a qualifier which indicates the units
// of the duration specified in the string literal.
Expand Down
97 changes: 97 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3120,6 +3120,103 @@ fn parse_interval() {
);
}

#[test]
fn parse_interval_and_or_xor() {
let sql = "SELECT col FROM test \
WHERE d3_date > d1_date + INTERVAL '5 days' \
AND d2_date > d1_date + INTERVAL '3 days'";

let actual_ast = Parser::parse_sql(&GenericDialect {}, sql).unwrap();

let expected_ast = vec![Statement::Query(Box::new(Query {
with: None,
body: Box::new(SetExpr::Select(Box::new(Select {
distinct: false,
top: None,
projection: vec![UnnamedExpr(Expr::Identifier(Ident {
value: "col".to_string(),
quote_style: None,
}))],
into: None,
from: vec![TableWithJoins {
relation: TableFactor::Table {
name: ObjectName(vec![Ident {
value: "test".to_string(),
quote_style: None,
}]),
alias: None,
args: None,
with_hints: vec![],
},
joins: vec![],
}],
lateral_views: vec![],
selection: Some(Expr::BinaryOp {
left: Box::new(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident {
value: "d3_date".to_string(),
quote_style: None,
})),
op: BinaryOperator::Gt,
right: Box::new(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident {
value: "d1_date".to_string(),
quote_style: None,
})),
op: BinaryOperator::Plus,
right: Box::new(Expr::Interval {
value: Box::new(Expr::Value(Value::SingleQuotedString(
"5 days".to_string(),
))),
leading_field: None,
leading_precision: None,
last_field: None,
fractional_seconds_precision: None,
}),
}),
}),
op: BinaryOperator::And,
right: Box::new(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident {
value: "d2_date".to_string(),
quote_style: None,
})),
op: BinaryOperator::Gt,
right: Box::new(Expr::BinaryOp {
left: Box::new(Expr::Identifier(Ident {
value: "d1_date".to_string(),
quote_style: None,
})),
op: BinaryOperator::Plus,
right: Box::new(Expr::Interval {
value: Box::new(Expr::Value(Value::SingleQuotedString(
"3 days".to_string(),
))),
leading_field: None,
leading_precision: None,
last_field: None,
fractional_seconds_precision: None,
}),
}),
}),
}),
group_by: vec![],
cluster_by: vec![],
distribute_by: vec![],
sort_by: vec![],
having: None,
qualify: None,
}))),
order_by: vec![],
limit: None,
offset: None,
fetch: None,
lock: None,
}))];

assert_eq!(actual_ast, expected_ast);
}

#[test]
fn parse_at_timezone() {
let zero = Expr::Value(number("0"));
Expand Down