mirror of
https://github.com/apache/datafusion-sqlparser-rs.git
synced 2025-08-31 03:07:20 +00:00
Support row_alias
and col_aliases
in INSERT
statement for mysql and generic dialects (#1136)
This commit is contained in:
parent
5da66adda9
commit
11899fd0cb
4 changed files with 148 additions and 4 deletions
|
@ -19,7 +19,7 @@ use matches::assert_matches;
|
|||
use sqlparser::ast::MysqlInsertPriority::{Delayed, HighPriority, LowPriority};
|
||||
use sqlparser::ast::*;
|
||||
use sqlparser::dialect::{GenericDialect, MySqlDialect};
|
||||
use sqlparser::parser::ParserOptions;
|
||||
use sqlparser::parser::{ParserError, ParserOptions};
|
||||
use sqlparser::tokenizer::Token;
|
||||
use test_utils::*;
|
||||
|
||||
|
@ -1330,6 +1330,112 @@ fn parse_priority_insert() {
|
|||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_insert_as() {
|
||||
let sql = r"INSERT INTO `table` (`date`) VALUES ('2024-01-01') AS `alias`";
|
||||
match mysql_and_generic().verified_stmt(sql) {
|
||||
Statement::Insert {
|
||||
table_name,
|
||||
columns,
|
||||
source,
|
||||
insert_alias,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(
|
||||
ObjectName(vec![Ident::with_quote('`', "table")]),
|
||||
table_name
|
||||
);
|
||||
assert_eq!(vec![Ident::with_quote('`', "date")], columns);
|
||||
let insert_alias = insert_alias.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
ObjectName(vec![Ident::with_quote('`', "alias")]),
|
||||
insert_alias.row_alias
|
||||
);
|
||||
assert_eq!(Some(vec![]), insert_alias.col_aliases);
|
||||
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!(),
|
||||
}
|
||||
|
||||
let sql = r"INSERT INTO `table` (`date`) VALUES ('2024-01-01') AS `alias` ()";
|
||||
assert!(matches!(
|
||||
mysql_and_generic().parse_sql_statements(sql),
|
||||
Err(ParserError::ParserError(_))
|
||||
));
|
||||
|
||||
let sql = r"INSERT INTO `table` (`id`, `date`) VALUES (1, '2024-01-01') AS `alias` (`mek_id`, `mek_date`)";
|
||||
match mysql_and_generic().verified_stmt(sql) {
|
||||
Statement::Insert {
|
||||
table_name,
|
||||
columns,
|
||||
source,
|
||||
insert_alias,
|
||||
..
|
||||
} => {
|
||||
assert_eq!(
|
||||
ObjectName(vec![Ident::with_quote('`', "table")]),
|
||||
table_name
|
||||
);
|
||||
assert_eq!(
|
||||
vec![Ident::with_quote('`', "id"), Ident::with_quote('`', "date")],
|
||||
columns
|
||||
);
|
||||
let insert_alias = insert_alias.unwrap();
|
||||
assert_eq!(
|
||||
ObjectName(vec![Ident::with_quote('`', "alias")]),
|
||||
insert_alias.row_alias
|
||||
);
|
||||
assert_eq!(
|
||||
Some(vec![
|
||||
Ident::with_quote('`', "mek_id"),
|
||||
Ident::with_quote('`', "mek_date")
|
||||
]),
|
||||
insert_alias.col_aliases
|
||||
);
|
||||
assert_eq!(
|
||||
Some(Box::new(Query {
|
||||
with: None,
|
||||
body: Box::new(SetExpr::Values(Values {
|
||||
explicit_row: false,
|
||||
rows: vec![vec![
|
||||
Expr::Value(number("1")),
|
||||
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)";
|
||||
|
|
|
@ -3764,7 +3764,8 @@ fn test_simple_postgres_insert_with_alias() {
|
|||
on: None,
|
||||
returning: None,
|
||||
replace_into: false,
|
||||
priority: None
|
||||
priority: None,
|
||||
insert_alias: None
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -3830,7 +3831,8 @@ fn test_simple_postgres_insert_with_alias() {
|
|||
on: None,
|
||||
returning: None,
|
||||
replace_into: false,
|
||||
priority: None
|
||||
priority: None,
|
||||
insert_alias: None
|
||||
}
|
||||
)
|
||||
}
|
||||
|
@ -3892,7 +3894,8 @@ fn test_simple_insert_with_quoted_alias() {
|
|||
on: None,
|
||||
returning: None,
|
||||
replace_into: false,
|
||||
priority: None
|
||||
priority: None,
|
||||
insert_alias: None,
|
||||
}
|
||||
)
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue