PostgreSQL: Add support for `ALTER SYSTEM`
diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index d6394da..cff93bb 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs
@@ -6357,6 +6357,31 @@ } } +/// `ALTER SYSTEM` statement. +/// +/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-altersystem.html) +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] +pub enum AlterSystem { + /// `ALTER SYSTEM SET configuration_parameter { TO | = } { value [, ...] | DEFAULT }` + Set(FunctionDefinitionSetParam), + /// `ALTER SYSTEM RESET { configuration_parameter | ALL }` + Reset(ResetConfig), +} + +impl fmt::Display for AlterSystem { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + AlterSystem::Set(set_param) => write!(f, "ALTER SYSTEM {set_param}"), + AlterSystem::Reset(ResetConfig::ALL) => write!(f, "ALTER SYSTEM RESET ALL"), + AlterSystem::Reset(ResetConfig::ConfigName(name)) => { + write!(f, "ALTER SYSTEM RESET {name}") + } + } + } +} + /// Text search object kind. /// /// See [PostgreSQL](https://www.postgresql.org/docs/current/textsearch-intro.html).
diff --git a/src/ast/mod.rs b/src/ast/mod.rs index b64cb6a..c899cf4 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs
@@ -70,8 +70,8 @@ AlterFunctionOperation, AlterFunctionSignature, AlterGenericOption, AlterIndexOperation, AlterOperator, AlterOperatorClass, AlterOperatorClassOperation, AlterOperatorFamily, AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy, AlterPolicyOperation, - AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock, - AlterTableOperation, AlterTableType, AlterTextSearch, AlterTextSearchOperation, + AlterSchema, AlterSchemaOperation, AlterSystem, AlterTable, AlterTableAlgorithm, + AlterTableLock, AlterTableOperation, AlterTableType, AlterTextSearch, AlterTextSearchOperation, AlterTextSearchOption, AlterTrigger, AlterTriggerOperation, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, AlterView, AlterViewOperation, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, @@ -3977,6 +3977,11 @@ operation: AlterRoleOperation, }, /// ```sql + /// ALTER SYSTEM + /// ``` + /// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-altersystem.html) + AlterSystem(AlterSystem), + /// ```sql /// ALTER POLICY <NAME> ON <TABLE NAME> [<OPERATION>] /// ``` /// (Postgresql-specific) @@ -5810,6 +5815,7 @@ Statement::AlterRole { name, operation } => { write!(f, "ALTER ROLE {name} {operation}") } + Statement::AlterSystem(alter_system) => write!(f, "{alter_system}"), Statement::AlterPolicy(alter_policy) => write!(f, "{alter_policy}"), Statement::AlterTrigger(alter_trigger) => write!(f, "{alter_trigger}"), Statement::AlterConnector { @@ -12796,6 +12802,12 @@ } } +impl From<AlterSystem> for Statement { + fn from(a: AlterSystem) -> Self { + Self::AlterSystem(a) + } +} + impl From<AlterView> for Statement { fn from(a: AlterView) -> Self { Self::AlterView(a)
diff --git a/src/ast/spans.rs b/src/ast/spans.rs index c76053c..866a153 100644 --- a/src/ast/spans.rs +++ b/src/ast/spans.rs
@@ -265,6 +265,7 @@ /// - [Statement::AlterType] /// - [Statement::AlterOperator] /// - [Statement::AlterRole] +/// - [Statement::AlterSystem] /// - [Statement::AttachDatabase] /// - [Statement::AttachDuckDBDatabase] /// - [Statement::DetachDuckDBDatabase] @@ -423,6 +424,7 @@ Statement::AlterOperatorClass { .. } => Span::empty(), Statement::AlterTextSearch { .. } => Span::empty(), Statement::AlterRole { .. } => Span::empty(), + Statement::AlterSystem(_) => Span::empty(), Statement::AlterSession { .. } => Span::empty(), Statement::AttachDatabase { .. } => Span::empty(), Statement::AttachDuckDBDatabase { .. } => Span::empty(),
diff --git a/src/parser/mod.rs b/src/parser/mod.rs index c36da23..972e93d 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs
@@ -12068,6 +12068,7 @@ } } Keyword::ROLE => self.parse_alter_role(), + Keyword::SYSTEM => self.parse_alter_system().map(Into::into), Keyword::POLICY => self.parse_alter_policy().map(Into::into), Keyword::TRIGGER => self.parse_alter_trigger().map(Into::into), Keyword::CONNECTOR => self.parse_alter_connector(), @@ -12078,7 +12079,7 @@ _ => { self.index = index; self.expected_ref( - "one of VIEW or MATERIALIZED VIEW or TYPE or COLLATION or TABLE or INDEX or FUNCTION or AGGREGATE or ROLE or POLICY or TRIGGER or CONNECTOR or ICEBERG or FOREIGN TABLE or SCHEMA or USER or OPERATOR or EXTENSION", + "one of VIEW or MATERIALIZED VIEW or TYPE or COLLATION or TABLE or INDEX or FUNCTION or AGGREGATE or ROLE or SYSTEM or POLICY or TRIGGER or CONNECTOR or ICEBERG or FOREIGN TABLE or SCHEMA or USER or OPERATOR or EXTENSION", self.peek_token_ref(), ) } @@ -12192,26 +12193,14 @@ let value = if self.parse_keywords(&[Keyword::FROM, Keyword::CURRENT]) { FunctionSetValue::FromCurrent } else { - if !self.consume_token(&Token::Eq) && !self.parse_keyword(Keyword::TO) { - return self.expected_ref("= or TO", self.peek_token_ref()); - } - if self.parse_keyword(Keyword::DEFAULT) { - FunctionSetValue::Default - } else { - FunctionSetValue::Values(self.parse_comma_separated(Parser::parse_expr)?) - } + self.parse_config_assignment_value()? }; Some(AlterFunctionAction::Set(FunctionDefinitionSetParam { name, value, })) } else if self.parse_keyword(Keyword::RESET) { - let reset_config = if self.parse_keyword(Keyword::ALL) { - ResetConfig::ALL - } else { - ResetConfig::ConfigName(self.parse_object_name(false)?) - }; - Some(AlterFunctionAction::Reset(reset_config)) + Some(AlterFunctionAction::Reset(self.parse_reset_config()?)) } else { None }; @@ -12219,6 +12208,44 @@ Ok(action) } + /// Parse `{ = | TO } { DEFAULT | value [, ...] }` after a configuration parameter name. + fn parse_config_assignment_value(&mut self) -> Result<FunctionSetValue, ParserError> { + if !self.consume_token(&Token::Eq) && !self.parse_keyword(Keyword::TO) { + return self.expected_ref("= or TO", self.peek_token_ref()); + } + if self.parse_keyword(Keyword::DEFAULT) { + Ok(FunctionSetValue::Default) + } else { + Ok(FunctionSetValue::Values( + self.parse_comma_separated(Parser::parse_expr)?, + )) + } + } + + /// Parse `{ ALL | configuration_parameter }` after `RESET`. + fn parse_reset_config(&mut self) -> Result<ResetConfig, ParserError> { + if self.parse_keyword(Keyword::ALL) { + Ok(ResetConfig::ALL) + } else { + Ok(ResetConfig::ConfigName(self.parse_object_name(false)?)) + } + } + + /// Parse an `ALTER SYSTEM` statement after the `ALTER SYSTEM` keywords. + /// + /// [PostgreSQL](https://www.postgresql.org/docs/current/sql-altersystem.html) + pub fn parse_alter_system(&mut self) -> Result<AlterSystem, ParserError> { + if self.parse_keyword(Keyword::SET) { + let name = self.parse_object_name(false)?; + let value = self.parse_config_assignment_value()?; + Ok(AlterSystem::Set(FunctionDefinitionSetParam { name, value })) + } else if self.parse_keyword(Keyword::RESET) { + Ok(AlterSystem::Reset(self.parse_reset_config()?)) + } else { + self.expected_ref("SET or RESET", self.peek_token_ref()) + } + } + fn parse_alter_function_actions( &mut self, ) -> Result<(Vec<AlterFunctionAction>, bool), ParserError> {
diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 2f6df7d..fbb1cf2 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs
@@ -16196,7 +16196,7 @@ #[test] fn parse_alter_rejects_missing_or_unknown_object_type() { - let objects = "one of VIEW or MATERIALIZED VIEW or TYPE or COLLATION or TABLE or INDEX or FUNCTION or AGGREGATE or ROLE or POLICY or TRIGGER or CONNECTOR or ICEBERG or FOREIGN TABLE or SCHEMA or USER or OPERATOR or EXTENSION"; + let objects = "one of VIEW or MATERIALIZED VIEW or TYPE or COLLATION or TABLE or INDEX or FUNCTION or AGGREGATE or ROLE or SYSTEM or POLICY or TRIGGER or CONNECTOR or ICEBERG or FOREIGN TABLE or SCHEMA or USER or OPERATOR or EXTENSION"; for (sql, found) in [ ("ALTER", "EOF"), ("ALTER 1", "1"),
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 1367c8f..bda502f 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs
@@ -14140,3 +14140,69 @@ ); } } + +#[test] +fn parse_alter_system() { + for sql in [ + "ALTER SYSTEM SET log_min_duration_statement = '1000'", + "ALTER SYSTEM SET log_min_duration_statement = 1000", + "ALTER SYSTEM SET wal_level = logical", + "ALTER SYSTEM SET my_extension.setting = 'value'", + "ALTER SYSTEM RESET log_min_duration_statement", + ] { + pg_and_generic().verified_stmt(sql); + } + pg_and_generic().one_statement_parses_to( + "ALTER SYSTEM SET work_mem TO '64MB'", + "ALTER SYSTEM SET work_mem = '64MB'", + ); + pg_and_generic().one_statement_parses_to( + "ALTER SYSTEM SET work_mem TO DEFAULT", + "ALTER SYSTEM SET work_mem = DEFAULT", + ); + + assert_eq!( + pg_and_generic().verified_stmt("ALTER SYSTEM SET search_path = pg_catalog, public"), + Statement::AlterSystem(AlterSystem::Set(FunctionDefinitionSetParam { + name: ObjectName::from(vec![Ident::new("search_path")]), + value: FunctionSetValue::Values(vec![ + Expr::Identifier(Ident::new("pg_catalog")), + Expr::Identifier(Ident::new("public")), + ]), + })) + ); + assert_eq!( + pg_and_generic().verified_stmt("ALTER SYSTEM SET work_mem = DEFAULT"), + Statement::AlterSystem(AlterSystem::Set(FunctionDefinitionSetParam { + name: ObjectName::from(vec![Ident::new("work_mem")]), + value: FunctionSetValue::Default, + })) + ); + assert_eq!( + pg_and_generic().verified_stmt("ALTER SYSTEM RESET ALL"), + Statement::AlterSystem(AlterSystem::Reset(ResetConfig::ALL)) + ); +} + +#[test] +fn parse_alter_system_rejects_malformed_statements() { + for (sql, expected) in [ + ("ALTER SYSTEM", "Expected: SET or RESET, found: EOF"), + ("ALTER SYSTEM SET work_mem", "Expected: = or TO, found: EOF"), + ( + "ALTER SYSTEM SET work_mem FROM CURRENT", + "Expected: = or TO, found: FROM", + ), + ( + "ALTER SYSTEM SET work_mem =", + "Expected: an expression, found: EOF", + ), + ("ALTER SYSTEM RESET", "Expected: identifier, found: EOF"), + ] { + assert_eq!( + pg_and_generic().parse_sql_statements(sql).unwrap_err(), + ParserError::ParserError(expected.to_string()), + "{sql}" + ); + } +}