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 all 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
}
}
162 changes: 146 additions & 16 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 @@ -2460,29 +2464,69 @@ impl<'a> Parser<'a> {
let mut values = vec![];
loop {
values.push(f(self)?);
if !self.consume_token(&Token::Comma)
|| self.options.trailing_commas
&& 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) {
Ok(values)
} else if matches!(peek_token, Token::Comma) && self.options.trailing_commas {
let _ = self.consume_token(&Token::Comma);
Ok(values)
} else {
Err(e)
};
}
}
loop {
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) =>
{
break;
}
Token::RParen
| Token::SemiColon
| Token::EOF
| Token::RBracket
| Token::RBrace => break,
_ => continue,
} else {
if self.options.trailing_commas
&& Self::is_comma_separated_end(&self.peek_token().token)
{
break;
}
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,90 @@ 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);
#[cfg(feature = "bigdecimal")]
assert_eq!(
ast,
Ok(vec![
Expr::Value(Value::Number(bigdecimal::BigDecimal::from(1), false)),
Expr::Value(Value::Number(bigdecimal::BigDecimal::from(2), false)),
Expr::Value(Value::Number(bigdecimal::BigDecimal::from(3), false)),
])
);
#[cfg(not(feature = "bigdecimal"))]
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);
#[cfg(feature = "bigdecimal")]
assert_eq!(
ast,
Ok(vec![Expr::Value(Value::Number(
bigdecimal::BigDecimal::from(1),
false
)),])
);
#[cfg(not(feature = "bigdecimal"))]
assert_eq!(
ast,
Ok(vec![Expr::Value(Value::Number("1".to_string(), false)),])
);
}
}
24 changes: 24 additions & 0 deletions tests/sqlparser_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use test_utils::*;
use sqlparser::ast::SelectItem::UnnamedExpr;
use sqlparser::ast::*;
use sqlparser::dialect::{GenericDialect, SQLiteDialect};
use sqlparser::parser::ParserOptions;
use sqlparser::tokenizer::Token;

#[test]
Expand Down Expand Up @@ -386,13 +387,36 @@ 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!()
}

sqlite_with_options(ParserOptions::new().with_trailing_commas(true)).one_statement_parses_to(
"SELECT * FROM t1 WHERE a IN (,)",
Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW this doesn't appear to be valid SQLite syntax:

sqlite> create table t as values (1);
sqlite> select * from t where column1 IN (,);
Parse error: near ",": syntax error
  select * from t where column1 IN (,);
                      error here ---^

"SELECT * FROM t1 WHERE a IN ()",
);
}

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

fn sqlite_with_options(options: ParserOptions) -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(SQLiteDialect {})],
options: Some(options),
}
}

fn sqlite_and_generic() -> TestedDialects {
TestedDialects {
// we don't have a separate SQLite dialect, so test only the generic dialect for now
Expand Down
Loading