Skip to content

Support EXISTS subqueries #90

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

Merged
merged 1 commit into from
Jun 3, 2019
Merged
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/sqlast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ pub enum ASTNode {
results: Vec<ASTNode>,
else_result: Option<Box<ASTNode>>,
},
/// An exists expression `EXISTS(SELECT ...)`, used in expressions like
/// `WHERE EXISTS (SELECT ...)`.
SQLExists(Box<SQLQuery>),
/// A parenthesized subquery `(SELECT ...)`, used in expression like
/// `SELECT (subquery) AS x` or `WHERE (subquery) = x`
SQLSubquery(Box<SQLQuery>),
Expand Down Expand Up @@ -230,6 +233,7 @@ impl ToString for ASTNode {
}
s + " END"
}
ASTNode::SQLExists(s) => format!("EXISTS ({})", s.to_string()),
ASTNode::SQLSubquery(s) => format!("({})", s.to_string()),
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/sqlparser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ impl Parser {
}
"CASE" => self.parse_case_expression(),
"CAST" => self.parse_cast_expression(),
"EXISTS" => self.parse_exists_expression(),
"NOT" => Ok(ASTNode::SQLUnary {
operator: SQLOperator::Not,
expr: Box::new(self.parse_subexpr(Self::UNARY_NOT_PREC)?),
Expand Down Expand Up @@ -404,6 +405,14 @@ impl Parser {
})
}

/// Parse a SQL EXISTS expression e.g. `WHERE EXISTS(SELECT ...)`.
pub fn parse_exists_expression(&mut self) -> Result<ASTNode, ParserError> {
self.expect_token(&Token::LParen)?;
let exists_node = ASTNode::SQLExists(Box::new(self.parse_query()?));
self.expect_token(&Token::RParen)?;
Ok(exists_node)
}

/// Parse an operator following an expression
pub fn parse_infix(&mut self, expr: ASTNode, precedence: u8) -> Result<ASTNode, ParserError> {
debug!("parsing infix");
Expand Down
40 changes: 40 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1385,6 +1385,46 @@ fn parse_scalar_subqueries() {
});
}

#[test]
fn parse_exists_subquery() {
let expected_inner = verified_query("SELECT 1");
let sql = "SELECT * FROM t WHERE EXISTS (SELECT 1)";
let select = verified_only_select(sql);
assert_eq!(
ASTNode::SQLExists(Box::new(expected_inner.clone())),
select.selection.unwrap(),
);

let sql = "SELECT * FROM t WHERE NOT EXISTS (SELECT 1)";
let select = verified_only_select(sql);
assert_eq!(
ASTNode::SQLUnary {
operator: SQLOperator::Not,
expr: Box::new(ASTNode::SQLExists(Box::new(expected_inner))),
},
select.selection.unwrap(),
);

verified_stmt("SELECT * FROM t WHERE EXISTS (WITH u AS (SELECT 1) SELECT * FROM u)");
verified_stmt("SELECT EXISTS (SELECT 1)");

let res = parse_sql_statements("SELECT EXISTS (");
assert_eq!(
ParserError::ParserError(
"Expected SELECT or a subquery in the query body, found: EOF".to_string()
),
res.unwrap_err(),
);

let res = parse_sql_statements("SELECT EXISTS (NULL)");
assert_eq!(
ParserError::ParserError(
"Expected SELECT or a subquery in the query body, found: NULL".to_string()
),
res.unwrap_err(),
);
}

#[test]
fn parse_create_view() {
let sql = "CREATE VIEW myschema.myview AS SELECT foo FROM bar";
Expand Down