diff --git a/src/ast/ddl.rs b/src/ast/ddl.rs index a4a21bff05..1e39944cbc 100644 --- a/src/ast/ddl.rs +++ b/src/ast/ddl.rs @@ -558,6 +558,17 @@ pub enum AlterTableOperation { /// Name of the constraint to validate. name: Ident, }, + /// `ALTER CONSTRAINT [ ]` or `ALTER CONSTRAINT [ NO ] INHERIT` + /// + /// Note: this is PostgreSQL-specific + AlterConstraint { + /// Name of the constraint to alter. + name: Ident, + /// `[ NOT ] DEFERRABLE`, `INITIALLY { DEFERRED | IMMEDIATE }` and `[ NOT ] ENFORCED`. + characteristics: Option, + /// `INHERIT` (`true`) or `NO INHERIT` (`false`). + inherit: Option, + }, /// `INHERIT ` /// /// Note: this is PostgreSQL-specific @@ -1167,6 +1178,21 @@ impl fmt::Display for AlterTableOperation { AlterTableOperation::ValidateConstraint { name } => { write!(f, "VALIDATE CONSTRAINT {name}") } + AlterTableOperation::AlterConstraint { + name, + characteristics, + inherit, + } => { + write!(f, "ALTER CONSTRAINT {name}")?; + if let Some(characteristics) = characteristics { + write!(f, " {characteristics}")?; + } + match inherit { + Some(true) => write!(f, " INHERIT"), + Some(false) => write!(f, " NO INHERIT"), + None => Ok(()), + } + } AlterTableOperation::Inherit { parent } => write!(f, "INHERIT {parent}"), AlterTableOperation::NoInherit { parent } => write!(f, "NO INHERIT {parent}"), AlterTableOperation::SetOptionsParens { options } => { diff --git a/src/ast/spans.rs b/src/ast/spans.rs index 2ce471e95b..220a821ae8 100644 --- a/src/ast/spans.rs +++ b/src/ast/spans.rs @@ -1385,6 +1385,7 @@ impl Spanned for AlterTableOperation { AlterTableOperation::Lock { .. } => Span::empty(), AlterTableOperation::ReplicaIdentity { .. } => Span::empty(), AlterTableOperation::ValidateConstraint { name } => name.span, + AlterTableOperation::AlterConstraint { name, .. } => name.span, AlterTableOperation::Inherit { parent } | AlterTableOperation::NoInherit { parent } => { parent.span() } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index a4580954a1..43cb8f6e89 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -11806,6 +11806,25 @@ impl<'a> Parser<'a> { options, column_position, } + } else if self.parse_keywords(&[Keyword::ALTER, Keyword::CONSTRAINT]) { + let name = self.parse_identifier()?; + let inherit = if self.parse_keyword(Keyword::INHERIT) { + Some(true) + } else if self.parse_keywords(&[Keyword::NO, Keyword::INHERIT]) { + Some(false) + } else { + None + }; + let characteristics = if inherit.is_none() { + self.parse_constraint_characteristics()? + } else { + None + }; + AlterTableOperation::AlterConstraint { + name, + characteristics, + inherit, + } } else if self.parse_keyword(Keyword::ALTER) { if self.peek_keyword(Keyword::SORTKEY) { self.prev_token(); diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index d00aac9a9f..4b88b65310 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -14321,3 +14321,90 @@ fn display_create_index_on_only_propagates_formatter_errors() { assert_eq!(write!(writer, "{stmt}"), Err(fmt::Error), "{rejected:?}"); } } + +#[test] +fn parse_alter_table_alter_constraint() { + let alter_constraint = + |sql| alter_table_op(pg_and_generic().verified_stmt(&format!("ALTER TABLE tab {sql}"))); + let characteristics = |deferrable, initially, enforced| ConstraintCharacteristics { + deferrable, + initially, + enforced, + }; + + for (sql, expected_characteristics, expected_inherit) in [ + ("ALTER CONSTRAINT f", None, None), + ( + "ALTER CONSTRAINT f DEFERRABLE INITIALLY DEFERRED", + Some(characteristics( + Some(true), + Some(DeferrableInitial::Deferred), + None, + )), + None, + ), + ( + "ALTER CONSTRAINT f NOT DEFERRABLE INITIALLY IMMEDIATE ENFORCED", + Some(characteristics( + Some(false), + Some(DeferrableInitial::Immediate), + Some(true), + )), + None, + ), + ( + "ALTER CONSTRAINT f NOT ENFORCED", + Some(characteristics(None, None, Some(false))), + None, + ), + ("ALTER CONSTRAINT f INHERIT", None, Some(true)), + ("ALTER CONSTRAINT f NO INHERIT", None, Some(false)), + ] { + assert_eq!( + alter_constraint(sql), + AlterTableOperation::AlterConstraint { + name: "f".into(), + characteristics: expected_characteristics, + inherit: expected_inherit, + }, + "{sql}" + ); + } + + pg_and_generic().verified_stmt( + "ALTER TABLE tab ALTER CONSTRAINT f DEFERRABLE, ALTER CONSTRAINT g NO INHERIT, ALTER COLUMN c SET NOT NULL", + ); + pg_and_generic().one_statement_parses_to( + "ALTER TABLE tab ALTER CONSTRAINT f NOT ENFORCED INITIALLY DEFERRED DEFERRABLE", + "ALTER TABLE tab ALTER CONSTRAINT f DEFERRABLE INITIALLY DEFERRED NOT ENFORCED", + ); + + for (sql, expected) in [ + ( + "ALTER TABLE tab ALTER CONSTRAINT f INHERIT DEFERRABLE", + "Expected: end of statement, found: DEFERRABLE", + ), + ( + "ALTER TABLE tab ALTER CONSTRAINT f NO INHERIT DEFERRABLE", + "Expected: end of statement, found: DEFERRABLE", + ), + ( + "ALTER TABLE tab ALTER CONSTRAINT f DEFERRABLE NO INHERIT", + "Expected: end of statement, found: NO", + ), + ( + "ALTER TABLE tab ALTER CONSTRAINT f NOT VALID", + "Expected: end of statement, found: NOT", + ), + ( + "ALTER TABLE tab ALTER CONSTRAINT", + "Expected: identifier, found: EOF", + ), + ] { + assert_eq!( + pg_and_generic().parse_sql_statements(sql).unwrap_err(), + ParserError::ParserError(expected.to_string()), + "{sql}" + ); + } +}