LucaCappelletti94 commented on code in PR #3079:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/3079#discussion_r4754779347
##########
tests/sqlparser_common.rs:
##########
@@ -23830,3 +23830,36 @@ 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 generic = TestedDialects::new(vec![Box::new(GenericDialect {})]);
+ generic.one_statement_parses_to("SELECT 1 ON", "SELECT 1 AS ON");
Review Comment:
You could replace the `SELECT 1 ON` pin with the MySQL statement. The pin
asserts an alias that all three engines reject, and the MySQL statement fails
without the reservation.
```suggestion
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");
```
##########
src/parser/mod.rs:
##########
@@ -14719,6 +14719,9 @@ impl<'a> Parser<'a> {
/// Optionally parses an alias for a select list item
fn maybe_parse_select_item_alias(&mut self) -> Result<Option<Ident>,
ParserError> {
fn validator(explicit: bool, kw: &Keyword, parser: &mut Parser) ->
bool {
+ if !explicit && kw == &Keyword::ON &&
parser.peek_keyword(Keyword::CONFLICT) {
+ return false;
+ }
Review Comment:
You could drop this lookahead and reserve `ON` as an implicit column alias
instead. Special-casing `ON CONFLICT` leaves the same failure for MySQL, where
`INSERT INTO t SELECT 1 ON DUPLICATE KEY UPDATE a = 1` still fails on this
branch with `Expected: end of statement, found: DUPLICATE` although MySQL 8.4
runs it. PostgreSQL 17, SQLite 3.51 and MySQL 8.4 all reject `SELECT 1 ON`, and
with the reservation every existing test still passes.
```suggestion
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]