Skip to content

Commit

Permalink
Support "UPDATE" statement in "WITH" subquery (#842)
Browse files Browse the repository at this point in the history
* fix(WITH): allow "UPDATE" statement in "WITH" subquery"

* add test case

* update test to validate round-trip
  • Loading branch information
nicksrandall committed Apr 9, 2023
1 parent 29dea5d commit 784a191
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub enum SetExpr {
},
Values(Values),
Insert(Statement),
Update(Statement),
Table(Box<Table>),
}

Expand All @@ -99,6 +100,7 @@ impl fmt::Display for SetExpr {
SetExpr::Query(q) => write!(f, "({q})"),
SetExpr::Values(v) => write!(f, "{v}"),
SetExpr::Insert(v) => write!(f, "{v}"),
SetExpr::Update(v) => write!(f, "{v}"),
SetExpr::Table(t) => write!(f, "{t}"),
SetExpr::SetOperation {
left,
Expand Down
44 changes: 31 additions & 13 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4910,7 +4910,30 @@ impl<'a> Parser<'a> {
None
};

if !self.parse_keyword(Keyword::INSERT) {
if self.parse_keyword(Keyword::INSERT) {
let insert = self.parse_insert()?;

Ok(Query {
with,
body: Box::new(SetExpr::Insert(insert)),
limit: None,
order_by: vec![],
offset: None,
fetch: None,
locks: vec![],
})
} else if self.parse_keyword(Keyword::UPDATE) {
let update = self.parse_update()?;
Ok(Query {
with,
body: Box::new(SetExpr::Update(update)),
limit: None,
order_by: vec![],
offset: None,
fetch: None,
locks: vec![],
})
} else {
let body = Box::new(self.parse_query_body(0)?);

let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
Expand Down Expand Up @@ -4966,18 +4989,6 @@ impl<'a> Parser<'a> {
fetch,
locks,
})
} else {
let insert = self.parse_insert()?;

Ok(Query {
with,
body: Box::new(SetExpr::Insert(insert)),
limit: None,
order_by: vec![],
offset: None,
fetch: None,
locks: vec![],
})
}
}

Expand Down Expand Up @@ -7447,4 +7458,11 @@ mod tests {
))
);
}

#[test]
fn test_update_in_with_subquery() {
let sql = r#"WITH "result" AS (UPDATE "Hero" SET "name" = 'Captain America', "number_of_movies" = "number_of_movies" + 1 WHERE "secret_identity" = 'Sam Wilson' RETURNING "id", "name", "secret_identity", "number_of_movies") SELECT * FROM "result""#;
let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
assert_eq!(ast[0].to_string(), sql);
}
}

0 comments on commit 784a191

Please sign in to comment.