Parse merge source as table factor (#483)

* Parse source of MERGE as table_factor

Some MERGE queries need a table as a soruce,
added proper test showing it

* Clippy fix

Co-authored-by: Maciej Skrzypkowski <maciej.skrzypkowski@satoricyber.com>
This commit is contained in:
Maciej Skrzypkowski 2022-05-10 12:34:45 +02:00 committed by GitHub
parent 35f5f0be4d
commit 9750841a66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 58 additions and 55 deletions

View file

@ -1058,9 +1058,7 @@ pub enum Statement {
// Specifies the table to merge // Specifies the table to merge
table: TableFactor, table: TableFactor,
// Specifies the table or subquery to join with the target table // Specifies the table or subquery to join with the target table
source: Box<SetExpr>, source: TableFactor,
// Specifies alias to the table that is joined with target table
alias: Option<TableAlias>,
// Specifies the expression on which to join the target table and source // Specifies the expression on which to join the target table and source
on: Box<Expr>, on: Box<Expr>,
// Specifies the actions to perform when values match or do not match. // Specifies the actions to perform when values match or do not match.
@ -1772,7 +1770,6 @@ impl fmt::Display for Statement {
into, into,
table, table,
source, source,
alias,
on, on,
clauses, clauses,
} => { } => {
@ -1781,9 +1778,6 @@ impl fmt::Display for Statement {
"MERGE{int} {table} USING {source} ", "MERGE{int} {table} USING {source} ",
int = if *into { " INTO" } else { "" } int = if *into { " INTO" } else { "" }
)?; )?;
if let Some(a) = alias {
write!(f, "as {} ", a)?;
};
write!(f, "ON {} ", on)?; write!(f, "ON {} ", on)?;
write!(f, "{}", display_separated(clauses, " ")) write!(f, "{}", display_separated(clauses, " "))
} }

View file

@ -4288,8 +4288,7 @@ impl<'a> Parser<'a> {
let table = self.parse_table_factor()?; let table = self.parse_table_factor()?;
self.expect_keyword(Keyword::USING)?; self.expect_keyword(Keyword::USING)?;
let source = self.parse_query_body(0)?; let source = self.parse_table_factor()?;
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
self.expect_keyword(Keyword::ON)?; self.expect_keyword(Keyword::ON)?;
let on = self.parse_expr()?; let on = self.parse_expr()?;
let clauses = self.parse_merge_clauses()?; let clauses = self.parse_merge_clauses()?;
@ -4297,8 +4296,7 @@ impl<'a> Parser<'a> {
Ok(Statement::Merge { Ok(Statement::Merge {
into, into,
table, table,
source: Box::new(source), source,
alias,
on: Box::new(on), on: Box::new(on),
clauses, clauses,
}) })

View file

@ -4353,15 +4353,14 @@ fn test_revoke() {
#[test] #[test]
fn parse_merge() { fn parse_merge() {
let sql = "MERGE INTO s.bar AS dest USING (SELECT * FROM s.foo) as stg ON dest.D = stg.D AND dest.E = stg.E WHEN NOT MATCHED THEN INSERT (A, B, C) VALUES (stg.A, stg.B, stg.C) WHEN MATCHED AND dest.A = 'a' THEN UPDATE SET dest.F = stg.F, dest.G = stg.G WHEN MATCHED THEN DELETE"; let sql = "MERGE INTO s.bar AS dest USING (SELECT * FROM s.foo) AS stg ON dest.D = stg.D AND dest.E = stg.E WHEN NOT MATCHED THEN INSERT (A, B, C) VALUES (stg.A, stg.B, stg.C) WHEN MATCHED AND dest.A = 'a' THEN UPDATE SET dest.F = stg.F, dest.G = stg.G WHEN MATCHED THEN DELETE";
let sql_no_into = "MERGE s.bar AS dest USING (SELECT * FROM s.foo) as stg ON dest.D = stg.D AND dest.E = stg.E WHEN NOT MATCHED THEN INSERT (A, B, C) VALUES (stg.A, stg.B, stg.C) WHEN MATCHED AND dest.A = 'a' THEN UPDATE SET dest.F = stg.F, dest.G = stg.G WHEN MATCHED THEN DELETE"; let sql_no_into = "MERGE s.bar AS dest USING (SELECT * FROM s.foo) AS stg ON dest.D = stg.D AND dest.E = stg.E WHEN NOT MATCHED THEN INSERT (A, B, C) VALUES (stg.A, stg.B, stg.C) WHEN MATCHED AND dest.A = 'a' THEN UPDATE SET dest.F = stg.F, dest.G = stg.G WHEN MATCHED THEN DELETE";
match (verified_stmt(sql), verified_stmt(sql_no_into)) { match (verified_stmt(sql), verified_stmt(sql_no_into)) {
( (
Statement::Merge { Statement::Merge {
into, into,
table, table,
source, source,
alias,
on, on,
clauses, clauses,
}, },
@ -4369,7 +4368,6 @@ fn parse_merge() {
into: no_into, into: no_into,
table: table_no_into, table: table_no_into,
source: source_no_into, source: source_no_into,
alias: alias_no_into,
on: on_no_into, on: on_no_into,
clauses: clauses_no_into, clauses: clauses_no_into,
}, },
@ -4393,7 +4391,9 @@ fn parse_merge() {
assert_eq!( assert_eq!(
source, source,
Box::new(SetExpr::Query(Box::new(Query { TableFactor::Derived {
lateral: false,
subquery: Box::new(Query {
with: None, with: None,
body: SetExpr::Select(Box::new(Select { body: SetExpr::Select(Box::new(Select {
distinct: false, distinct: false,
@ -4405,7 +4405,7 @@ fn parse_merge() {
name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]), name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]),
alias: None, alias: None,
args: vec![], args: vec![],
with_hints: vec![], with_hints: vec![]
}, },
joins: vec![] joins: vec![]
}], }],
@ -4416,25 +4416,24 @@ fn parse_merge() {
distribute_by: vec![], distribute_by: vec![],
sort_by: vec![], sort_by: vec![],
having: None, having: None,
qualify: None qualify: None,
})), })),
order_by: vec![], order_by: vec![],
limit: None, limit: None,
offset: None, offset: None,
fetch: None, fetch: None,
lock: None lock: None
}))) }),
); alias: Some(TableAlias {
assert_eq!(source, source_no_into); name: Ident {
value: "stg".to_string(),
assert_eq!( quote_style: None
alias, },
Some(TableAlias {
name: Ident::new("stg"),
columns: vec![] columns: vec![]
}) })
}
); );
assert_eq!(alias, alias_no_into); assert_eq!(source, source_no_into);
assert_eq!( assert_eq!(
on, on,
@ -4515,6 +4514,18 @@ fn parse_merge() {
} }
} }
#[test]
fn test_merge_into_using_table() {
let sql = "MERGE INTO target_table USING source_table \
ON target_table.id = source_table.oooid \
WHEN MATCHED THEN \
UPDATE SET target_table.description = source_table.description \
WHEN NOT MATCHED THEN \
INSERT (ID, description) VALUES (source_table.id, source_table.description)";
verified_stmt(sql);
}
#[test] #[test]
fn test_lock() { fn test_lock() {
let sql = "SELECT * FROM student WHERE id = '1' FOR UPDATE"; let sql = "SELECT * FROM student WHERE id = '1' FOR UPDATE";