Read ADD, SET and DROP before a string literal as option names
diff --git a/fuzz/fuzz_seeds/postgres_2745_options_keyword_names.sql b/fuzz/fuzz_seeds/postgres_2745_options_keyword_names.sql new file mode 100644 index 0000000..e36ee1a --- /dev/null +++ b/fuzz/fuzz_seeds/postgres_2745_options_keyword_names.sql
@@ -0,0 +1 @@ +ALTER FOREIGN TABLE ft OPTIONS (add 'x', SET set 'y', DROP drop)
diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 2429ece..4a8c591 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs
@@ -21580,16 +21580,18 @@ fn parse_alter_generic_options(&mut self) -> Result<Vec<AlterGenericOption>, ParserError> { self.expect_token(&Token::LParen)?; let options = self.parse_comma_separated(|p| { - if p.parse_keyword(Keyword::SET) { - Ok(AlterGenericOption::Set(p.parse_pg_option()?)) - } else if p.parse_keyword(Keyword::DROP) { - Ok(AlterGenericOption::Drop(p.parse_identifier()?)) - } else { - let add_keyword = p.parse_keyword(Keyword::ADD); - Ok(AlterGenericOption::Add { + // Before a string literal, `ADD`, `SET` and `DROP` name an option. + let action = match p.peek_nth_token_ref(1).token { + Token::SingleQuotedString(_) => None, + _ => p.parse_one_of_keywords(&[Keyword::ADD, Keyword::SET, Keyword::DROP]), + }; + match action { + Some(Keyword::SET) => Ok(AlterGenericOption::Set(p.parse_pg_option()?)), + Some(Keyword::DROP) => Ok(AlterGenericOption::Drop(p.parse_identifier()?)), + action => Ok(AlterGenericOption::Add { option: p.parse_pg_option()?, - add_keyword, - }) + add_keyword: action.is_some(), + }), } })?; self.expect_token(&Token::RParen)?;
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index f9f5332..73f6671 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs
@@ -12422,6 +12422,44 @@ "ALTER FOREIGN TABLE ft ALTER COLUMN id OPTIONS (SET column_name 'remote_id', DROP other)", ); + // `ADD`, `SET` and `DROP` before a string literal name a plain option. + let Statement::AlterTable(alter) = pg_and_generic() + .verified_stmt("ALTER FOREIGN TABLE ft OPTIONS (add 'x', set 'y', drop 'z')") + else { + unreachable!() + }; + assert_eq!( + alter.operations, + vec![AlterTableOperation::GenericOptions { + options: ["add", "set", "drop"] + .into_iter() + .zip(["x", "y", "z"]) + .map(|(key, value)| AlterGenericOption::Add { + option: option(key, value), + add_keyword: false, + }) + .collect(), + }] + ); + let Statement::AlterTable(alter) = pg_and_generic() + .verified_stmt("ALTER FOREIGN TABLE ft OPTIONS (ADD add 'x', SET set 'y', DROP drop)") + else { + unreachable!() + }; + assert_eq!( + alter.operations, + vec![AlterTableOperation::GenericOptions { + options: vec![ + AlterGenericOption::Add { + option: option("add", "x"), + add_keyword: true, + }, + AlterGenericOption::Set(option("set", "y")), + AlterGenericOption::Drop(Ident::new("drop")), + ], + }] + ); + for (sql, expected) in [ ( "ALTER FOREIGN TABLE ft OPTIONS (DROP fetch_size '100')",