Skip to content

Commit f753ea0

Browse files
committed
cr
1 parent 8df89f4 commit f753ea0

File tree

5 files changed

+38
-15
lines changed

5 files changed

+38
-15
lines changed

src/ast/mod.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ pub use self::query::{
7272
TableAlias, TableAliasColumnDef, TableFactor, TableFunctionArgs, TableSample,
7373
TableSampleBucket, TableSampleKind, TableSampleMethod, TableSampleModifier,
7474
TableSampleQuantity, TableSampleSeed, TableSampleSeedModifier, TableSampleUnit, TableVersion,
75-
TableWithJoins, Top, TopQuantity, ValueTableMode, Values, WildcardAdditionalOptions, With,
76-
WithFill,
75+
TableWithJoins, Top, TopQuantity, UpdateTableFromKind, ValueTableMode, Values,
76+
WildcardAdditionalOptions, With, WithFill,
7777
};
7878

7979
pub use self::trigger::{
@@ -2479,7 +2479,7 @@ pub enum Statement {
24792479
/// Column assignments
24802480
assignments: Vec<Assignment>,
24812481
/// Table which provide value to be set
2482-
from: Option<TableWithJoins>,
2482+
from: Option<UpdateTableFromKind>,
24832483
/// WHERE
24842484
selection: Option<Expr>,
24852485
/// RETURNING
@@ -3751,10 +3751,13 @@ impl fmt::Display for Statement {
37513751
write!(f, "{or} ")?;
37523752
}
37533753
write!(f, "{table}")?;
3754+
if let Some(UpdateTableFromKind::BeforeSet(from)) = from {
3755+
write!(f, " FROM {from}")?;
3756+
}
37543757
if !assignments.is_empty() {
37553758
write!(f, " SET {}", display_comma_separated(assignments))?;
37563759
}
3757-
if let Some(from) = from {
3760+
if let Some(UpdateTableFromKind::AfterSet(from)) = from {
37583761
write!(f, " FROM {from}")?;
37593762
}
37603763
if let Some(selection) = selection {

src/ast/query.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2788,3 +2788,14 @@ impl fmt::Display for ValueTableMode {
27882788
}
27892789
}
27902790
}
2791+
2792+
/// The update table from options
2793+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
2794+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
2795+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
2796+
pub enum UpdateTableFromKind {
2797+
/// Table sample located before the table alias option
2798+
BeforeSet(TableWithJoins),
2799+
/// Table sample located after the table alias option
2800+
AfterSet(TableWithJoins),
2801+
}

src/ast/spans.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ use super::{
3232
PivotValueSource, ProjectionSelect, Query, ReferentialAction, RenameSelectItem,
3333
ReplaceSelectElement, ReplaceSelectItem, Select, SelectInto, SelectItem, SetExpr, SqlOption,
3434
Statement, Subscript, SymbolDefinition, TableAlias, TableAliasColumnDef, TableConstraint,
35-
TableFactor, TableOptionsClustered, TableWithJoins, Use, Value, Values, ViewColumnDef,
36-
WildcardAdditionalOptions, With, WithFill,
35+
TableFactor, TableOptionsClustered, TableWithJoins, UpdateTableFromKind, Use, Value, Values,
36+
ViewColumnDef, WildcardAdditionalOptions, With, WithFill,
3737
};
3838

3939
/// Given an iterator of spans, return the [Span::union] of all spans.
@@ -2098,6 +2098,15 @@ impl Spanned for SelectInto {
20982098
}
20992099
}
21002100

2101+
impl Spanned for UpdateTableFromKind {
2102+
fn span(&self) -> Span {
2103+
match self {
2104+
UpdateTableFromKind::BeforeSet(vec) => vec.span(),
2105+
UpdateTableFromKind::AfterSet(vec) => vec.span(),
2106+
}
2107+
}
2108+
}
2109+
21012110
#[cfg(test)]
21022111
pub mod tests {
21032112
use crate::dialect::{Dialect, GenericDialect, SnowflakeDialect};

src/parser/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11699,14 +11699,16 @@ impl<'a> Parser<'a> {
1169911699
let or = self.parse_conflict_clause();
1170011700
let table = self.parse_table_and_joins()?;
1170111701
let from_before_set = if self.parse_keyword(Keyword::FROM) {
11702-
Some(self.parse_table_and_joins()?)
11702+
Some(UpdateTableFromKind::BeforeSet(
11703+
self.parse_table_and_joins()?,
11704+
))
1170311705
} else {
1170411706
None
1170511707
};
1170611708
self.expect_keyword(Keyword::SET)?;
1170711709
let assignments = self.parse_comma_separated(Parser::parse_assignment)?;
1170811710
let from = if from_before_set.is_none() && self.parse_keyword(Keyword::FROM) {
11709-
Some(self.parse_table_and_joins()?)
11711+
Some(UpdateTableFromKind::AfterSet(self.parse_table_and_joins()?))
1171011712
} else {
1171111713
from_before_set
1171211714
};

tests/sqlparser_common.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ fn parse_update_set_from() {
366366
target: AssignmentTarget::ColumnName(ObjectName(vec![Ident::new("name")])),
367367
value: Expr::CompoundIdentifier(vec![Ident::new("t2"), Ident::new("name")])
368368
}],
369-
from: Some(TableWithJoins {
369+
from: Some(UpdateTableFromKind::AfterSet(TableWithJoins {
370370
relation: TableFactor::Derived {
371371
lateral: false,
372372
subquery: Box::new(Query {
@@ -417,8 +417,8 @@ fn parse_update_set_from() {
417417
columns: vec![],
418418
})
419419
},
420-
joins: vec![],
421-
}),
420+
joins: vec![]
421+
})),
422422
selection: Some(Expr::BinaryOp {
423423
left: Box::new(Expr::CompoundIdentifier(vec![
424424
Ident::new("t1"),
@@ -12449,10 +12449,8 @@ fn overflow() {
1244912449

1245012450
#[test]
1245112451
fn parse_update_from_before_select() {
12452-
let res = all_dialects()
12453-
.parse_sql_statements("UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name WHERE t1.id = t2.id");
12454-
12455-
assert!(res.is_ok(), "{res:?}");
12452+
all_dialects()
12453+
.verified_stmt("UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name WHERE t1.id = t2.id");
1245612454

1245712455
let res = all_dialects().parse_sql_statements(
1245812456
"UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name FROM (SELECT name from t2) AS t2",

0 commit comments

Comments
 (0)