Skip to content

Commit c7ae83b

Browse files
committed
fix parsing of INSERT INTO ... SELECT ... RETURNING
fixes #1660
1 parent b4b5576 commit c7ae83b

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

src/keywords.rs

+1
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ pub const RESERVED_FOR_TABLE_ALIAS: &[Keyword] = &[
946946
Keyword::GLOBAL,
947947
Keyword::ANTI,
948948
Keyword::SEMI,
949+
Keyword::RETURNING,
949950
// for MSSQL-specific OUTER APPLY (seems reserved in most dialects)
950951
Keyword::OUTER,
951952
Keyword::SET,

tests/sqlparser_common.rs

+21
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,27 @@ fn parse_insert_select_returning() {
265265
}
266266
}
267267

268+
#[test]
269+
fn parse_insert_select_from_returning() {
270+
let sql = "INSERT INTO table1 SELECT * FROM table2 RETURNING id";
271+
match verified_stmt(sql) {
272+
Statement::Insert(Insert {
273+
table: TableObject::TableName(table_name),
274+
source: Some(source),
275+
returning: Some(returning),
276+
..
277+
}) => {
278+
assert_eq!("table1", table_name.to_string());
279+
assert!(matches!(*source.body, SetExpr::Select(_)));
280+
assert_eq!(
281+
returning,
282+
vec![SelectItem::UnnamedExpr(Expr::Identifier(Ident::new("id"))),]
283+
);
284+
}
285+
bas_statement => unreachable!("Expected insert with select from returning, got {:?}", bas_statement),
286+
}
287+
}
288+
268289
#[test]
269290
fn parse_returning_as_column_alias() {
270291
verified_stmt("SELECT 1 AS RETURNING");

0 commit comments

Comments
 (0)