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
Conversation
Pull Request Test Coverage Report for Build 3491640784
💛 - Coveralls |
@@ -363,6 +363,36 @@ impl<'a> Parser<'a> { | |||
Ok(expr) | |||
} | |||
|
|||
pub fn parse_interval_expr(&mut self) -> Result<Expr, ParserError> { |
There was a problem hiding this comment.
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?
Lines 1648 to 1660 in 87b4a16
Token::Word(w) if w.keyword == Keyword::NOT => match self.peek_nth_token(1) { | |
// The precedence of NOT varies depending on keyword that | |
// follows it. If it is followed by IN, BETWEEN, or LIKE, | |
// it takes on the precedence of those tokens. Otherwise it | |
// is not an infix operator, and therefore has zero | |
// precedence. | |
Token::Word(w) if w.keyword == Keyword::IN => Ok(Self::BETWEEN_PREC), | |
Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(Self::BETWEEN_PREC), | |
Token::Word(w) if w.keyword == Keyword::LIKE => Ok(Self::LIKE_PREC), | |
Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(Self::LIKE_PREC), | |
Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(Self::LIKE_PREC), | |
_ => Ok(0), | |
}, |
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 ?
There was a problem hiding this comment.
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?
Thank you for the contribution @sarahyurick |
Thanks @alamb @AugustoFKL ! Let me know what you think. Should I update anything with the precedence logic, or just keep as is for now? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
@sarahyurick IMHO that's ok
Thanks again @sarahyurick and @AugustoFKL |
I originally opened an issue for this on the DataFusion side: apache/datafusion#3944, but found the error to lie in how the queries were being parsed.
These changes allow something like
"SELECT col FROM test WHERE d3_date > d1_date + INTERVAL '5 days' AND d2_date > d1_date + INTERVAL '3 days'"
to be parsed correctly. Previously, the '5 days' was getting separated from theINTERVAL
keyword and lumped into theAND
operator.