Skip to content

Commit 3392623

Browse files
authored
add support for with clauses (CTEs) in delete statements (#1764)
1 parent 1e54a34 commit 3392623

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/ast/query.rs

+2
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ pub enum SetExpr {
156156
Values(Values),
157157
Insert(Statement),
158158
Update(Statement),
159+
Delete(Statement),
159160
Table(Box<Table>),
160161
}
161162

@@ -178,6 +179,7 @@ impl fmt::Display for SetExpr {
178179
SetExpr::Values(v) => write!(f, "{v}"),
179180
SetExpr::Insert(v) => write!(f, "{v}"),
180181
SetExpr::Update(v) => write!(f, "{v}"),
182+
SetExpr::Delete(v) => write!(f, "{v}"),
181183
SetExpr::Table(t) => write!(f, "{t}"),
182184
SetExpr::SetOperation {
183185
left,

src/ast/spans.rs

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ impl Spanned for SetExpr {
191191
SetExpr::Insert(statement) => statement.span(),
192192
SetExpr::Table(_) => Span::empty(),
193193
SetExpr::Update(statement) => statement.span(),
194+
SetExpr::Delete(statement) => statement.span(),
194195
}
195196
}
196197
}

src/parser/mod.rs

+22
Original file line numberDiff line numberDiff line change
@@ -10050,6 +10050,13 @@ impl<'a> Parser<'a> {
1005010050
Ok(parent_type(inside_type.into()))
1005110051
}
1005210052

10053+
/// Parse a DELETE statement, returning a `Box`ed SetExpr
10054+
///
10055+
/// This is used to reduce the size of the stack frames in debug builds
10056+
fn parse_delete_setexpr_boxed(&mut self) -> Result<Box<SetExpr>, ParserError> {
10057+
Ok(Box::new(SetExpr::Delete(self.parse_delete()?)))
10058+
}
10059+
1005310060
pub fn parse_delete(&mut self) -> Result<Statement, ParserError> {
1005410061
let (tables, with_from_keyword) = if !self.parse_keyword(Keyword::FROM) {
1005510062
// `FROM` keyword is optional in BigQuery SQL.
@@ -10249,6 +10256,21 @@ impl<'a> Parser<'a> {
1024910256
format_clause: None,
1025010257
}
1025110258
.into())
10259+
} else if self.parse_keyword(Keyword::DELETE) {
10260+
Ok(Query {
10261+
with,
10262+
body: self.parse_delete_setexpr_boxed()?,
10263+
limit: None,
10264+
limit_by: vec![],
10265+
order_by: None,
10266+
offset: None,
10267+
fetch: None,
10268+
locks: vec![],
10269+
for_clause: None,
10270+
settings: None,
10271+
format_clause: None,
10272+
}
10273+
.into())
1025210274
} else {
1025310275
let body = self.parse_query_body(self.dialect.prec_unknown())?;
1025410276

tests/sqlparser_common.rs

+27
Original file line numberDiff line numberDiff line change
@@ -7383,6 +7383,33 @@ fn parse_recursive_cte() {
73837383
assert_eq!(with.cte_tables.first().unwrap(), &expected);
73847384
}
73857385

7386+
#[test]
7387+
fn parse_cte_in_data_modification_statements() {
7388+
match verified_stmt("WITH x AS (SELECT 1) UPDATE t SET bar = (SELECT * FROM x)") {
7389+
Statement::Query(query) => {
7390+
assert_eq!(query.with.unwrap().to_string(), "WITH x AS (SELECT 1)");
7391+
assert!(matches!(*query.body, SetExpr::Update(_)));
7392+
}
7393+
other => panic!("Expected: UPDATE, got: {:?}", other),
7394+
}
7395+
7396+
match verified_stmt("WITH t (x) AS (SELECT 9) DELETE FROM q WHERE id IN (SELECT x FROM t)") {
7397+
Statement::Query(query) => {
7398+
assert_eq!(query.with.unwrap().to_string(), "WITH t (x) AS (SELECT 9)");
7399+
assert!(matches!(*query.body, SetExpr::Delete(_)));
7400+
}
7401+
other => panic!("Expected: DELETE, got: {:?}", other),
7402+
}
7403+
7404+
match verified_stmt("WITH x AS (SELECT 42) INSERT INTO t SELECT foo FROM x") {
7405+
Statement::Query(query) => {
7406+
assert_eq!(query.with.unwrap().to_string(), "WITH x AS (SELECT 42)");
7407+
assert!(matches!(*query.body, SetExpr::Insert(_)));
7408+
}
7409+
other => panic!("Expected: INSERT, got: {:?}", other),
7410+
}
7411+
}
7412+
73867413
#[test]
73877414
fn parse_derived_tables() {
73887415
let sql = "SELECT a.x, b.y FROM (SELECT x FROM foo) AS a CROSS JOIN (SELECT y FROM bar) AS b";

0 commit comments

Comments
 (0)