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 a6b4628f0e..db77943c9a 100644 --- a/src/dialect/sqlite.rs +++ b/src/dialect/sqlite.rs @@ -228,6 +228,7 @@ impl Dialect for SQLiteDialect { | Keyword::INTERSECT | Keyword::RETURNING | Keyword::VALUES + | Keyword::ON | Keyword::FROM | Keyword::INTO | Keyword::ESCAPE diff --git a/src/keywords.rs b/src/keywords.rs index f599f4483b..8ee4cedc02 100644 --- a/src/keywords.rs +++ b/src/keywords.rs @@ -1353,6 +1353,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 3075a27f85..5be503ed80 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -23867,3 +23867,37 @@ fn parse_quantified_comparison_with_arbitrary_operator() { ); } } + +#[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()) + ); +}