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 "UPDATE" statement in "WITH" subquery #842

Merged
merged 3 commits into from
Apr 9, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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""#;
Copy link
Contributor

Choose a reason for hiding this comment

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

Generally it's best to test across dialects, using the testing helpers (e.g. see test_parse_limit earlier in the file). In this case, the change itself has little to do with the dialect itself, so I'm not particularly concerned by it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, I agree the tests are a little out of place here -- in general it seems like we have some tests in parser.rs that really belong in src/tests/...

I'll move them around in a follow on PR

Copy link
Collaborator

Choose a reason for hiding this comment

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

let ast = Parser::parse_sql(&GenericDialect, sql).unwrap();
assert_eq!(ast[0].to_string(), sql);
}
}