diff --git a/fuzz/fuzz_seeds/postgres_1987_insert_select_on_conflict.sql b/fuzz/fuzz_seeds/postgres_1987_insert_select_on_conflict.sql new file mode 100644 index 0000000000..736fa2354a --- /dev/null +++ b/fuzz/fuzz_seeds/postgres_1987_insert_select_on_conflict.sql @@ -0,0 +1 @@ +INSERT INTO t SELECT 1 ON CONFLICT DO NOTHING; diff --git a/src/dialect/sqlite.rs b/src/dialect/sqlite.rs index d97fc93197..97db87377a 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -226,6 +226,7 @@ impl Dialect for SQLiteDialect { | Keyword::INTERSECT | Keyword::RETURNING | Keyword::VALUES + | Keyword::ON | Keyword::FROM | Keyword::INTO ) diff --git a/src/keywords.rs b/src/keywords.rs index 420bbb7a39..9b6af033b2 100644 --- a/src/keywords.rs +++ b/src/keywords.rs @@ -1351,6 +1351,7 @@ pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[ Keyword::DISTRIBUTE, Keyword::RETURNING, Keyword::VALUES, + Keyword::ON, // Reserved only as a column alias in the `SELECT` clause Keyword::FROM, Keyword::INTO, diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 541511d949..3cdb52bd16 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -23830,3 +23830,37 @@ fn parse_try_cast_shortcut() { dialects.one_statement_parses_to("SELECT ? ::INT", "SELECT ?::INT"); dialects.verified_stmt("SELECT v:items[0].price?::DECIMAL(10,2)"); } + +#[test] +fn parse_insert_select_on_conflict() { + let dialects = TestedDialects::new(vec![ + Box::new(PostgreSqlDialect {}), + Box::new(GenericDialect {}), + Box::new(SQLiteDialect {}), + ]); + + for sql in [ + "INSERT INTO t SELECT 1 ON CONFLICT DO NOTHING", + "INSERT INTO t SELECT 1 AS \"ON\" ON CONFLICT DO NOTHING", + "INSERT INTO t SELECT 1 ON CONFLICT(a) DO UPDATE SET a = 1", + ] { + dialects.verified_stmt(sql); + } + + let pg_and_generic = TestedDialects::new(vec![ + Box::new(PostgreSqlDialect {}), + Box::new(GenericDialect {}), + ]); + pg_and_generic.verified_stmt("INSERT INTO t SELECT 1 AS ON ON CONFLICT DO NOTHING"); + let mysql_and_generic = + TestedDialects::new(vec![Box::new(MySqlDialect {}), Box::new(GenericDialect {})]); + mysql_and_generic.verified_stmt("INSERT INTO t SELECT 1 ON DUPLICATE KEY UPDATE a = 1"); + + let err = dialects + .parse_sql_statements("INSERT INTO t SELECT 1 ON CONFLICT") + .unwrap_err(); + assert_eq!( + err, + ParserError::ParserError("Expected: DO, found: EOF".to_string()) + ); +}