Skip to content

Parse merge source as table factor #483

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 3 commits into from
May 10, 2022
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
8 changes: 1 addition & 7 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,9 +1058,7 @@ pub enum Statement {
// Specifies the table to merge
table: TableFactor,
// Specifies the table or subquery to join with the target table
source: Box<SetExpr>,
// Specifies alias to the table that is joined with target table
alias: Option<TableAlias>,
source: TableFactor,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mobuchowski What do you think about this change?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me 👍

// Specifies the expression on which to join the target table and source
on: Box<Expr>,
// Specifies the actions to perform when values match or do not match.
Expand Down Expand Up @@ -1772,7 +1770,6 @@ impl fmt::Display for Statement {
into,
table,
source,
alias,
on,
clauses,
} => {
Expand All @@ -1781,9 +1778,6 @@ impl fmt::Display for Statement {
"MERGE{int} {table} USING {source} ",
int = if *into { " INTO" } else { "" }
)?;
if let Some(a) = alias {
write!(f, "as {} ", a)?;
};
write!(f, "ON {} ", on)?;
write!(f, "{}", display_separated(clauses, " "))
}
Expand Down
6 changes: 2 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4288,17 +4288,15 @@ impl<'a> Parser<'a> {
let table = self.parse_table_factor()?;

self.expect_keyword(Keyword::USING)?;
let source = self.parse_query_body(0)?;
let alias = self.parse_optional_table_alias(keywords::RESERVED_FOR_TABLE_ALIAS)?;
let source = self.parse_table_factor()?;
self.expect_keyword(Keyword::ON)?;
let on = self.parse_expr()?;
let clauses = self.parse_merge_clauses()?;

Ok(Statement::Merge {
into,
table,
source: Box::new(source),
alias,
source,
on: Box::new(on),
clauses,
})
Expand Down
99 changes: 55 additions & 44 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4353,23 +4353,21 @@ 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,
},
Statement::Merge {
into: no_into,
table: table_no_into,
source: source_no_into,
alias: alias_no_into,
on: on_no_into,
clauses: clauses_no_into,
},
Expand All @@ -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 {
Expand Down Expand Up @@ -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";
Expand Down