Skip to content

Added support for DROP DOMAIN #1828

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 2, 2025
Merged
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
36 changes: 36 additions & 0 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3319,6 +3319,14 @@ pub enum Statement {
drop_behavior: Option<DropBehavior>,
},
/// ```sql
/// DROP DOMAIN
/// ```
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-dropdomain.html)
///
/// DROP DOMAIN [ IF EXISTS ] name [, ...] [ CASCADE | RESTRICT ]
///
DropDomain(DropDomain),
/// ```sql
/// DROP PROCEDURE
/// ```
DropProcedure {
Expand Down Expand Up @@ -5092,6 +5100,21 @@ impl fmt::Display for Statement {
}
Ok(())
}
Statement::DropDomain(DropDomain {
if_exists,
name,
drop_behavior,
}) => {
write!(
f,
"DROP DOMAIN{} {name}",
if *if_exists { " IF EXISTS" } else { "" },
)?;
if let Some(op) = drop_behavior {
write!(f, " {op}")?;
}
Ok(())
}
Statement::DropProcedure {
if_exists,
proc_desc,
Expand Down Expand Up @@ -6827,6 +6850,19 @@ impl fmt::Display for CloseCursor {
}
}

/// A Drop Domain statement
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct DropDomain {
/// Whether to drop the domain if it exists
pub if_exists: bool,
/// The name of the domain to drop
pub name: ObjectName,
/// The behavior to apply when dropping the domain
pub drop_behavior: Option<DropBehavior>,
}

/// A function call
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down
1 change: 1 addition & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,7 @@ impl Spanned for Statement {
Statement::DetachDuckDBDatabase { .. } => Span::empty(),
Statement::Drop { .. } => Span::empty(),
Statement::DropFunction { .. } => Span::empty(),
Statement::DropDomain { .. } => Span::empty(),
Statement::DropProcedure { .. } => Span::empty(),
Statement::DropSecret { .. } => Span::empty(),
Statement::Declare { .. } => Span::empty(),
Expand Down
1 change: 1 addition & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ define_keywords!(
DISTRIBUTE,
DIV,
DO,
DOMAIN,
DOUBLE,
DOW,
DOY,
Expand Down
16 changes: 16 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6051,6 +6051,8 @@ impl<'a> Parser<'a> {
return self.parse_drop_policy();
} else if self.parse_keyword(Keyword::CONNECTOR) {
return self.parse_drop_connector();
} else if self.parse_keyword(Keyword::DOMAIN) {
return self.parse_drop_domain();
} else if self.parse_keyword(Keyword::PROCEDURE) {
return self.parse_drop_procedure();
} else if self.parse_keyword(Keyword::SECRET) {
Expand Down Expand Up @@ -6146,6 +6148,20 @@ impl<'a> Parser<'a> {
Ok(Statement::DropConnector { if_exists, name })
}

/// ```sql
/// DROP DOMAIN [ IF EXISTS ] name [ CASCADE | RESTRICT ]
/// ```
fn parse_drop_domain(&mut self) -> Result<Statement, ParserError> {
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
let name = self.parse_object_name(false)?;
let drop_behavior = self.parse_optional_drop_behavior();
Ok(Statement::DropDomain(DropDomain {
if_exists,
name,
drop_behavior,
}))
}

/// ```sql
/// DROP PROCEDURE [ IF EXISTS ] name [ ( [ [ argmode ] [ argname ] argtype [, ...] ] ) ] [, ...]
/// [ CASCADE | RESTRICT ]
Expand Down
60 changes: 60 additions & 0 deletions tests/sqlparser_postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4239,6 +4239,66 @@ fn parse_drop_function() {
);
}

#[test]
fn parse_drop_domain() {
let sql = "DROP DOMAIN IF EXISTS jpeg_domain";
assert_eq!(
pg().verified_stmt(sql),
Statement::DropDomain(DropDomain {
if_exists: true,
name: ObjectName::from(vec![Ident {
value: "jpeg_domain".to_string(),
quote_style: None,
span: Span::empty(),
}]),
drop_behavior: None
})
);

let sql = "DROP DOMAIN jpeg_domain";
assert_eq!(
pg().verified_stmt(sql),
Statement::DropDomain(DropDomain {
if_exists: false,
name: ObjectName::from(vec![Ident {
value: "jpeg_domain".to_string(),
quote_style: None,
span: Span::empty(),
}]),
drop_behavior: None
})
);

let sql = "DROP DOMAIN IF EXISTS jpeg_domain CASCADE";
assert_eq!(
pg().verified_stmt(sql),
Statement::DropDomain(DropDomain {
if_exists: true,
name: ObjectName::from(vec![Ident {
value: "jpeg_domain".to_string(),
quote_style: None,
span: Span::empty(),
}]),
drop_behavior: Some(DropBehavior::Cascade)
})
);

let sql = "DROP DOMAIN IF EXISTS jpeg_domain RESTRICT";

assert_eq!(
pg().verified_stmt(sql),
Statement::DropDomain(DropDomain {
if_exists: true,
name: ObjectName::from(vec![Ident {
value: "jpeg_domain".to_string(),
quote_style: None,
span: Span::empty(),
}]),
drop_behavior: Some(DropBehavior::Restrict)
})
);
}

#[test]
fn parse_drop_procedure() {
let sql = "DROP PROCEDURE IF EXISTS test_proc";
Expand Down