Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,17 @@ pub enum AlterTableOperation {
/// Name of the constraint to validate.
name: Ident,
},
/// `ALTER CONSTRAINT <name> [ <characteristics> ]` or `ALTER CONSTRAINT <name> [ NO ] INHERIT`
///
/// Note: this is PostgreSQL-specific <https://www.postgresql.org/docs/current/sql-altertable.html>
AlterConstraint {
/// Name of the constraint to alter.
name: Ident,
/// `[ NOT ] DEFERRABLE`, `INITIALLY { DEFERRED | IMMEDIATE }` and `[ NOT ] ENFORCED`.
characteristics: Option<ConstraintCharacteristics>,
/// `INHERIT` (`true`) or `NO INHERIT` (`false`).
inherit: Option<bool>,
},
/// `INHERIT <parent_table>`
///
/// Note: this is PostgreSQL-specific <https://www.postgresql.org/docs/current/sql-altertable.html>
Expand Down Expand Up @@ -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 } => {
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
19 changes: 19 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
87 changes: 87 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
);
}
}
Loading