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

Support IN () syntax of SQLite #1005

Closed
wants to merge 7 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,10 @@ pub trait Dialect: Debug + Any {
fn supports_substring_from_for_expr(&self) -> bool {
true
}
/// Returns true if the dialect supports `(NOT) IN ()` expressions
fn supports_in_empty_list(&self) -> bool {
false
}
/// Dialect-specific prefix parser override
fn parse_prefix(&self, _parser: &mut Parser) -> Option<Result<Expr, ParserError>> {
// return None to fall back to the default behavior
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,8 @@ impl Dialect for SQLiteDialect {
None
}
}

fn supports_in_empty_list(&self) -> bool {
true
}
}
137 changes: 124 additions & 13 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,11 @@ impl<'a> Parser<'a> {
} else {
Expr::InList {
expr: Box::new(expr),
list: self.parse_comma_separated(Parser::parse_expr)?,
list: if self.dialect.supports_in_empty_list() {
self.parse_comma_separated0(Parser::parse_expr)?
} else {
self.parse_comma_separated(Parser::parse_expr)?
},
negated,
}
};
Expand Down Expand Up @@ -2463,26 +2467,66 @@ impl<'a> Parser<'a> {
if !self.consume_token(&Token::Comma) {
break;
} else if self.options.trailing_commas {
match self.peek_token().token {
Token::Word(kw)
if keywords::RESERVED_FOR_COLUMN_ALIAS
.iter()
.any(|d| kw.keyword == *d) =>
{
if Self::is_comma_separated_end(&self.peek_token().token) {
break;
}
}
}
Ok(values)
}

/// Parse a comma-separated list of 0+ items accepted by `F`
pub fn parse_comma_separated0<T, F>(&mut self, mut f: F) -> Result<Vec<T>, ParserError>
where
F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
{
let mut values = vec![];
let index = self.index;
match f(self) {
Ok(v) => values.push(v),
Err(e) => {
// FIXME: this is a workaround because f (e.g. Parser::parse_expr)
// might eat tokens even thought it fails.
self.index = index;
let peek_token = &self.peek_token().token;
return if Self::is_comma_separated_end(peek_token)
|| matches!(peek_token, Token::Comma) && self.options.trailing_commas
{
Ok(values)
} else {
Err(e)
};
}
}
loop {
if !self.consume_token(&Token::Comma) {
break;
} else {
if self.options.trailing_commas {
if Self::is_comma_separated_end(&self.peek_token().token) {
break;
}
Token::RParen
| Token::SemiColon
| Token::EOF
| Token::RBracket
| Token::RBrace => break,
_ => continue,
}
values.push(f(self)?);
}
}
Ok(values)
}

fn is_comma_separated_end(token: &Token) -> bool {
match token {
Token::Word(kw)
if keywords::RESERVED_FOR_COLUMN_ALIAS
.iter()
.any(|d| kw.keyword == *d) =>
{
true
}
Token::RParen | Token::SemiColon | Token::EOF | Token::RBracket | Token::RBrace => true,
_ => false,
}
}

/// Run a parser method `f`, reverting back to the current position
/// if unsuccessful.
#[must_use]
Expand Down Expand Up @@ -8374,4 +8418,71 @@ mod tests {
panic!("fail to parse mysql partition selection");
}
}

#[test]
fn test_comma_separated0() {
let sql = "1, 2, 3";
let ast = Parser::new(&GenericDialect)
.try_with_sql(sql)
.unwrap()
.parse_comma_separated0(Parser::parse_expr);
assert_eq!(
ast,
Ok(vec![
Expr::Value(Value::Number("1".to_string(), false)),
Expr::Value(Value::Number("2".to_string(), false)),
Expr::Value(Value::Number("3".to_string(), false)),
])
);

let sql = "";
let ast = Parser::new(&GenericDialect)
.try_with_sql(sql)
.unwrap()
.parse_comma_separated0(Parser::parse_expr);
assert_eq!(ast, Ok(vec![]));

let sql = ",";
let ast = Parser::new(&GenericDialect)
.try_with_sql(sql)
.unwrap()
.parse_comma_separated0(Parser::parse_expr);
assert_eq!(
ast,
Err(ParserError::ParserError(
"Expected an expression:, found: , at Line: 1, Column 1".to_string()
))
);

let sql = ",";
let ast = Parser::new(&GenericDialect)
.with_options(ParserOptions::new().with_trailing_commas(true))
.try_with_sql(sql)
.unwrap()
.parse_comma_separated0(Parser::parse_expr);
assert_eq!(ast, Ok(vec![]));

let sql = "1,";
let ast = Parser::new(&GenericDialect)
.try_with_sql(sql)
.unwrap()
.parse_comma_separated0(Parser::parse_expr);
assert_eq!(
ast,
Err(ParserError::ParserError(
"Expected an expression:, found: EOF".to_string()
))
);

let sql = "1,";
let ast = Parser::new(&GenericDialect)
.with_options(ParserOptions::new().with_trailing_commas(true))
.try_with_sql(sql)
.unwrap()
.parse_comma_separated0(Parser::parse_expr);
assert_eq!(
ast,
Ok(vec![Expr::Value(Value::Number("1".to_string(), false)),])
);
}
}
11 changes: 11 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,17 @@ fn parse_attach_database() {
}
}

#[test]
fn parse_where_in_empty_list() {
let sql = "SELECT * FROM t1 WHERE a IN ()";
let select = sqlite().verified_only_select(sql);
if let Expr::InList { list, .. } = select.selection.as_ref().unwrap() {
assert_eq!(list.len(), 0);
} else {
unreachable!()
}
}

fn sqlite() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(SQLiteDialect {})],
Expand Down