Skip to content
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

Support row_alias and col_aliases in INSERT statement for mysql and generic dialects #1136

Merged
merged 6 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 14 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,10 @@ pub enum Statement {
replace_into: bool,
/// Only for mysql
priority: Option<MysqlInsertPriority>,
/// Only for mysql
emin100 marked this conversation as resolved.
Show resolved Hide resolved
as_table: Option<ObjectName>,
emin100 marked this conversation as resolved.
Show resolved Hide resolved
/// Only for mysql
as_table_after_columns: Option<Vec<Ident>>,
},
/// ```sql
/// INSTALL
Expand Down Expand Up @@ -2592,6 +2596,8 @@ impl fmt::Display for Statement {
returning,
replace_into,
priority,
as_table,
as_table_after_columns,
} => {
let table_name = if let Some(alias) = table_alias {
format!("{table_name} AS {alias}")
Expand Down Expand Up @@ -2641,6 +2647,14 @@ impl fmt::Display for Statement {
write!(f, "DEFAULT VALUES")?;
}

if let Some(as_table) = as_table {
write!(f, " AS {as_table}")?;

if let Some(as_table_after_columns) = as_table_after_columns {
write!(f, " ({})", display_comma_separated(as_table_after_columns))?;
}
}

if let Some(on) = on {
write!(f, "{on}")?;
}
Expand Down
13 changes: 13 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7990,6 +7990,17 @@ impl<'a> Parser<'a> {
(columns, partitioned, after_columns, source)
};

let (as_table, as_table_after_columns) = if dialect_of!(self is MySqlDialect | GenericDialect)
&& self.parse_keyword(Keyword::AS)
{
let as_table = Some(self.parse_object_name(false)?);
let as_table_after_columns =
self.parse_parenthesized_column_list(Optional, false)?;
(as_table, Some(as_table_after_columns))
} else {
(None, None)
};

let on = if self.parse_keyword(Keyword::ON) {
if self.parse_keyword(Keyword::CONFLICT) {
let conflict_target =
Expand Down Expand Up @@ -8055,6 +8066,8 @@ impl<'a> Parser<'a> {
after_columns,
source,
table,
as_table,
as_table_after_columns,
on,
returning,
replace_into,
Expand Down
49 changes: 49 additions & 0 deletions tests/sqlparser_mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,6 +1327,55 @@ fn parse_priority_insert() {
}
}

#[test]
fn parse_insert_as() {
let sql = r"INSERT INTO `table` (`date`) VALUES ('2024-01-01') AS `alias` (`mek`)";
emin100 marked this conversation as resolved.
Show resolved Hide resolved
match mysql_and_generic().verified_stmt(sql) {
Statement::Insert {
table_name,
columns,
source,
as_table,
as_table_after_columns,
..
} => {
assert_eq!(
ObjectName(vec![Ident::with_quote('`', "table")]),
table_name
);
assert_eq!(vec![Ident::with_quote('`', "date")], columns);
assert_eq!(
ObjectName(vec![Ident::with_quote('`', "alias")]),
as_table.unwrap()
);
assert_eq!(
Some(vec![Ident::with_quote('`', "mek")]),
as_table_after_columns
);
assert_eq!(
Some(Box::new(Query {
with: None,
body: Box::new(SetExpr::Values(Values {
explicit_row: false,
rows: vec![vec![Expr::Value(Value::SingleQuotedString(
"2024-01-01".to_string()
))]]
})),
order_by: vec![],
limit: None,
limit_by: vec![],
offset: None,
fetch: None,
locks: vec![],
for_clause: None,
})),
source
);
}
_ => unreachable!(),
}
}

#[test]
fn parse_replace_insert() {
let sql = r"REPLACE DELAYED INTO tasks (title, priority) VALUES ('Test Some Inserts', 1)";
Expand Down
12 changes: 9 additions & 3 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3683,7 +3683,9 @@ fn test_simple_postgres_insert_with_alias() {
on: None,
returning: None,
replace_into: false,
priority: None
priority: None,
as_table: None,
as_table_after_columns: None
}
)
}
Expand Down Expand Up @@ -3749,7 +3751,9 @@ fn test_simple_postgres_insert_with_alias() {
on: None,
returning: None,
replace_into: false,
priority: None
priority: None,
as_table: None,
as_table_after_columns: None
}
)
}
Expand Down Expand Up @@ -3811,7 +3815,9 @@ fn test_simple_insert_with_quoted_alias() {
on: None,
returning: None,
replace_into: false,
priority: None
priority: None,
as_table: None,
as_table_after_columns: None,
}
)
}
Expand Down
Loading