mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-20 06:00:15 +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
|
@ -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, " "))
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
})
|
})
|
||||||
|
|
|
@ -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,49 +4391,50 @@ fn parse_merge() {
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
source,
|
source,
|
||||||
Box::new(SetExpr::Query(Box::new(Query {
|
TableFactor::Derived {
|
||||||
with: None,
|
lateral: false,
|
||||||
body: SetExpr::Select(Box::new(Select {
|
subquery: Box::new(Query {
|
||||||
distinct: false,
|
with: None,
|
||||||
top: None,
|
body: SetExpr::Select(Box::new(Select {
|
||||||
projection: vec![SelectItem::Wildcard],
|
distinct: false,
|
||||||
into: None,
|
top: None,
|
||||||
from: vec![TableWithJoins {
|
projection: vec![SelectItem::Wildcard],
|
||||||
relation: TableFactor::Table {
|
into: None,
|
||||||
name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]),
|
from: vec![TableWithJoins {
|
||||||
alias: None,
|
relation: TableFactor::Table {
|
||||||
args: vec![],
|
name: ObjectName(vec![Ident::new("s"), Ident::new("foo")]),
|
||||||
with_hints: vec![],
|
alias: None,
|
||||||
},
|
args: vec![],
|
||||||
joins: vec![]
|
with_hints: vec![]
|
||||||
}],
|
},
|
||||||
lateral_views: vec![],
|
joins: vec![]
|
||||||
selection: None,
|
}],
|
||||||
group_by: vec![],
|
lateral_views: vec![],
|
||||||
cluster_by: vec![],
|
selection: None,
|
||||||
distribute_by: vec![],
|
group_by: vec![],
|
||||||
sort_by: vec![],
|
cluster_by: vec![],
|
||||||
having: None,
|
distribute_by: vec![],
|
||||||
qualify: None
|
sort_by: vec![],
|
||||||
})),
|
having: None,
|
||||||
order_by: vec![],
|
qualify: None,
|
||||||
limit: None,
|
})),
|
||||||
offset: None,
|
order_by: vec![],
|
||||||
fetch: None,
|
limit: None,
|
||||||
lock: 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!(source, source_no_into);
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
alias,
|
|
||||||
Some(TableAlias {
|
|
||||||
name: Ident::new("stg"),
|
|
||||||
columns: vec![]
|
|
||||||
})
|
|
||||||
);
|
|
||||||
assert_eq!(alias, alias_no_into);
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
on,
|
on,
|
||||||
Box::new(Expr::BinaryOp {
|
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]
|
#[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";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue