mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-30 02:44:11 +00:00
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:
parent
35f5f0be4d
commit
9750841a66
3 changed files with 58 additions and 55 deletions
|
@ -4353,15 +4353,14 @@ fn test_revoke() {
|
|||
|
||||
#[test]
|
||||
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_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 = "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";
|
||||
match (verified_stmt(sql), verified_stmt(sql_no_into)) {
|
||||
(
|
||||
Statement::Merge {
|
||||
into,
|
||||
table,
|
||||
source,
|
||||
alias,
|
||||
on,
|
||||
clauses,
|
||||
},
|
||||
|
@ -4369,7 +4368,6 @@ fn parse_merge() {
|
|||
into: no_into,
|
||||
table: table_no_into,
|
||||
source: source_no_into,
|
||||
alias: alias_no_into,
|
||||
on: on_no_into,
|
||||
clauses: clauses_no_into,
|
||||
},
|
||||
|
@ -4393,49 +4391,50 @@ fn parse_merge() {
|
|||
|
||||
assert_eq!(
|
||||
source,
|
||||
Box::new(SetExpr::Query(Box::new(Query {
|
||||
with: None,
|
||||
body: SetExpr::Select(Box::new(Select {
|
||||
distinct: false,
|
||||
top: None,
|
||||
projection: vec![SelectItem::Wildcard],
|
||||
into: None,
|
||||
from: vec![TableWithJoins {
|
||||
relation: TableFactor::Table {
|
||||
name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]),
|
||||
alias: None,
|
||||
args: vec![],
|
||||
with_hints: vec![],
|
||||
},
|
||||
joins: vec![]
|
||||
}],
|
||||
lateral_views: vec![],
|
||||
selection: None,
|
||||
group_by: vec![],
|
||||
cluster_by: vec![],
|
||||
distribute_by: vec![],
|
||||
sort_by: vec![],
|
||||
having: None,
|
||||
qualify: None
|
||||
})),
|
||||
order_by: vec![],
|
||||
limit: None,
|
||||
offset: None,
|
||||
fetch: None,
|
||||
lock: None
|
||||
})))
|
||||
TableFactor::Derived {
|
||||
lateral: false,
|
||||
subquery: Box::new(Query {
|
||||
with: None,
|
||||
body: SetExpr::Select(Box::new(Select {
|
||||
distinct: false,
|
||||
top: None,
|
||||
projection: vec![SelectItem::Wildcard],
|
||||
into: None,
|
||||
from: vec![TableWithJoins {
|
||||
relation: TableFactor::Table {
|
||||
name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]),
|
||||
alias: None,
|
||||
args: vec![],
|
||||
with_hints: vec![]
|
||||
},
|
||||
joins: vec![]
|
||||
}],
|
||||
lateral_views: vec![],
|
||||
selection: None,
|
||||
group_by: vec![],
|
||||
cluster_by: vec![],
|
||||
distribute_by: vec![],
|
||||
sort_by: vec![],
|
||||
having: None,
|
||||
qualify: None,
|
||||
})),
|
||||
order_by: vec![],
|
||||
limit: None,
|
||||
offset: None,
|
||||
fetch: None,
|
||||
lock: None
|
||||
}),
|
||||
alias: Some(TableAlias {
|
||||
name: Ident {
|
||||
value: "stg".to_string(),
|
||||
quote_style: None
|
||||
},
|
||||
columns: vec![]
|
||||
})
|
||||
}
|
||||
);
|
||||
assert_eq!(source, source_no_into);
|
||||
|
||||
assert_eq!(
|
||||
alias,
|
||||
Some(TableAlias {
|
||||
name: Ident::new("stg"),
|
||||
columns: vec![]
|
||||
})
|
||||
);
|
||||
assert_eq!(alias, alias_no_into);
|
||||
|
||||
assert_eq!(
|
||||
on,
|
||||
Box::new(Expr::BinaryOp {
|
||||
|
@ -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]
|
||||
fn test_lock() {
|
||||
let sql = "SELECT * FROM student WHERE id = '1' FOR UPDATE";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue