Skip to content

Support multiple tables in UPDATE FROM clause #1681

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
Jan 28, 2025
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: 2 additions & 2 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3872,13 +3872,13 @@ impl fmt::Display for Statement {
}
write!(f, "{table}")?;
if let Some(UpdateTableFromKind::BeforeSet(from)) = from {
write!(f, " FROM {from}")?;
write!(f, " FROM {}", display_comma_separated(from))?;
}
if !assignments.is_empty() {
write!(f, " SET {}", display_comma_separated(assignments))?;
}
if let Some(UpdateTableFromKind::AfterSet(from)) = from {
write!(f, " FROM {from}")?;
write!(f, " FROM {}", display_comma_separated(from))?;
}
if let Some(selection) = selection {
write!(f, " WHERE {selection}")?;
Expand Down
4 changes: 2 additions & 2 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2829,8 +2829,8 @@ impl fmt::Display for ValueTableMode {
pub enum UpdateTableFromKind {
/// Update Statement where the 'FROM' clause is before the 'SET' keyword (Supported by Snowflake)
/// For Example: `UPDATE FROM t1 SET t1.name='aaa'`
BeforeSet(TableWithJoins),
BeforeSet(Vec<TableWithJoins>),
/// Update Statement where the 'FROM' clause is after the 'SET' keyword (Which is the standard way)
/// For Example: `UPDATE SET t1.name='aaa' FROM t1`
AfterSet(TableWithJoins),
AfterSet(Vec<TableWithJoins>),
}
9 changes: 5 additions & 4 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2138,10 +2138,11 @@ impl Spanned for SelectInto {

impl Spanned for UpdateTableFromKind {
fn span(&self) -> Span {
match self {
UpdateTableFromKind::BeforeSet(from) => from.span(),
UpdateTableFromKind::AfterSet(from) => from.span(),
}
let from = match self {
UpdateTableFromKind::BeforeSet(from) => from,
UpdateTableFromKind::AfterSet(from) => from,
};
union_spans(from.iter().map(|t| t.span()))
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12617,15 +12617,17 @@ impl<'a> Parser<'a> {
let table = self.parse_table_and_joins()?;
let from_before_set = if self.parse_keyword(Keyword::FROM) {
Some(UpdateTableFromKind::BeforeSet(
self.parse_table_and_joins()?,
self.parse_table_with_joins()?,
))
} else {
None
};
self.expect_keyword(Keyword::SET)?;
let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
let from = if from_before_set.is_none() && self.parse_keyword(Keyword::FROM) {
Some(UpdateTableFromKind::AfterSet(self.parse_table_and_joins()?))
Some(UpdateTableFromKind::AfterSet(
self.parse_table_with_joins()?,
))
} else {
from_before_set
};
Expand Down
11 changes: 7 additions & 4 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ fn parse_update_set_from() {
target: AssignmentTarget::ColumnName(ObjectName::from(vec![Ident::new("name")])),
value: Expr::CompoundIdentifier(vec![Ident::new("t2"), Ident::new("name")])
}],
from: Some(UpdateTableFromKind::AfterSet(TableWithJoins {
from: Some(UpdateTableFromKind::AfterSet(vec![TableWithJoins {
relation: TableFactor::Derived {
lateral: false,
subquery: Box::new(Query {
Expand Down Expand Up @@ -455,7 +455,7 @@ fn parse_update_set_from() {
})
},
joins: vec![]
})),
}])),
selection: Some(Expr::BinaryOp {
left: Box::new(Expr::CompoundIdentifier(vec![
Ident::new("t1"),
Expand All @@ -471,6 +471,9 @@ fn parse_update_set_from() {
or: None,
}
);

let sql = "UPDATE T SET a = b FROM U, (SELECT foo FROM V) AS W WHERE 1 = 1";
dialects.verified_stmt(sql);
}

#[test]
Expand Down Expand Up @@ -13051,8 +13054,8 @@ fn parse_select_without_projection() {

#[test]
fn parse_update_from_before_select() {
all_dialects()
.verified_stmt("UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name WHERE t1.id = t2.id");
verified_stmt("UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name WHERE t1.id = t2.id");
verified_stmt("UPDATE t1 FROM U, (SELECT id FROM V) AS W SET a = b WHERE 1 = 1");

let query =
"UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name FROM (SELECT name from t2) AS t2";
Expand Down
Loading