sabir-akhadov-localstack commented on code in PR #2417:
URL:
https://github.com/apache/datafusion-sqlparser-rs/pull/2417#discussion_r4315364761
##########
src/parser/mod.rs:
##########
@@ -18677,6 +18674,102 @@ impl<'a> Parser<'a> {
})
}
+ /// Parse `ALTER DEFAULT PRIVILEGES`, whose keywords are already consumed.
+ ///
+ /// See [PostgreSQL
docs](https://www.postgresql.org/docs/current/sql-alterdefaultprivileges.html).
+ pub fn parse_alter_default_privileges(
+ &mut self,
+ ) -> Result<AlterDefaultPrivileges, ParserError> {
+ // `FOR USER` is a synonym of `FOR ROLE` and is normalised to it.
+ let for_roles = if self.parse_keyword(Keyword::FOR) {
+ self.expect_one_of_keywords(&[Keyword::ROLE, Keyword::USER])?;
+ self.parse_comma_separated(|p| p.parse_identifier())?
+ } else {
+ vec![]
+ };
+
+ let in_schemas = if self.parse_keywords(&[Keyword::IN,
Keyword::SCHEMA]) {
+ self.parse_comma_separated(|p| p.parse_object_name(false))?
+ } else {
+ vec![]
+ };
+
+ let is_grant = if self.parse_keyword(Keyword::GRANT) {
+ true
+ } else if self.parse_keyword(Keyword::REVOKE) {
+ false
+ } else {
+ return self.expected_ref("GRANT or REVOKE", self.peek_token_ref());
+ };
+
+ let grant_option_for =
+ !is_grant && self.parse_keywords(&[Keyword::GRANT,
Keyword::OPTION, Keyword::FOR]);
+ let privileges = self.parse_privileges()?;
+ self.expect_keyword_is(Keyword::ON)?;
+ let object_type = self.parse_default_privileges_object_type()?;
+ self.expect_keyword_is(if is_grant { Keyword::TO } else {
Keyword::FROM })?;
+ let grantees = self.parse_grantees()?;
+
+ let operation = if is_grant {
+ AlterDefaultPrivilegesOperation::Grant {
+ privileges,
+ object_type,
+ grantees,
+ with_grant_option: self.parse_keywords(&[
+ Keyword::WITH,
+ Keyword::GRANT,
+ Keyword::OPTION,
+ ]),
+ }
+ } else {
+ AlterDefaultPrivilegesOperation::Revoke {
+ grant_option_for,
+ privileges,
+ object_type,
+ grantees,
+ cascade: self.parse_cascade_option(),
+ }
+ };
+
+ Ok(AlterDefaultPrivileges {
+ for_roles,
+ in_schemas,
+ operation,
+ })
+ }
+
+ /// Parse `ALL [PRIVILEGES]` or a comma separated privilege list.
+ fn parse_privileges(&mut self) -> Result<Privileges, ParserError> {
+ if self.parse_keyword(Keyword::ALL) {
+ Ok(Privileges::All {
+ with_privileges_keyword:
self.parse_keyword(Keyword::PRIVILEGES),
+ })
+ } else {
+ Ok(Privileges::Actions(self.parse_actions_list()?))
+ }
+ }
+
+ fn parse_default_privileges_object_type(
+ &mut self,
+ ) -> Result<DefaultPrivilegesObjectType, ParserError> {
+ for (keyword, object_type) in [
+ (Keyword::TABLES, DefaultPrivilegesObjectType::Tables),
+ (Keyword::SEQUENCES, DefaultPrivilegesObjectType::Sequences),
+ (Keyword::FUNCTIONS, DefaultPrivilegesObjectType::Functions),
+ (Keyword::ROUTINES, DefaultPrivilegesObjectType::Routines),
+ (Keyword::TYPES, DefaultPrivilegesObjectType::Types),
+ (Keyword::SCHEMAS, DefaultPrivilegesObjectType::Schemas),
Review Comment:
The current docs also list `ON LARGE OBJECTS` (PG 17+), so `ALTER DEFAULT
PRIVILEGES GRANT SELECT, UPDATE ON LARGE OBJECTS TO app` fails here; a
`LargeObjects` variant matched via `parse_keywords(&[LARGE, OBJECTS])` would
cover it.
##########
src/parser/mod.rs:
##########
@@ -18677,6 +18674,102 @@ impl<'a> Parser<'a> {
})
}
+ /// Parse `ALTER DEFAULT PRIVILEGES`, whose keywords are already consumed.
+ ///
+ /// See [PostgreSQL
docs](https://www.postgresql.org/docs/current/sql-alterdefaultprivileges.html).
+ pub fn parse_alter_default_privileges(
+ &mut self,
+ ) -> Result<AlterDefaultPrivileges, ParserError> {
+ // `FOR USER` is a synonym of `FOR ROLE` and is normalised to it.
+ let for_roles = if self.parse_keyword(Keyword::FOR) {
+ self.expect_one_of_keywords(&[Keyword::ROLE, Keyword::USER])?;
+ self.parse_comma_separated(|p| p.parse_identifier())?
+ } else {
+ vec![]
+ };
+
+ let in_schemas = if self.parse_keywords(&[Keyword::IN,
Keyword::SCHEMA]) {
Review Comment:
`DefACLOptionList` in gram.y is an unordered list, so `ALTER DEFAULT
PRIVILEGES IN SCHEMA s FOR ROLE r GRANT SELECT ON TABLES TO app` is valid PG
but fails here with `Expected: GRANT or REVOKE`; looping over the two options
until neither matches would accept it. pg_dump always emits `FOR ROLE` first,
so non-blocking.
--
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]