Skip to content

Commit f5e6209

Browse files
Merge branch 'main' into no-whitespace-parser
2 parents df0b9f2 + 982f766 commit f5e6209

File tree

5 files changed

+132
-8
lines changed

5 files changed

+132
-8
lines changed

src/ast/ddl.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4251,3 +4251,40 @@ impl Spanned for DropOperator {
42514251
Span::empty()
42524252
}
42534253
}
4254+
4255+
/// `DROP OPERATOR FAMILY` statement
4256+
/// See <https://www.postgresql.org/docs/current/sql-dropopfamily.html>
4257+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4258+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4259+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4260+
pub struct DropOperatorFamily {
4261+
/// `IF EXISTS` clause
4262+
pub if_exists: bool,
4263+
/// One or more operator families to drop
4264+
pub names: Vec<ObjectName>,
4265+
/// Index method (btree, hash, gist, gin, etc.)
4266+
pub using: Ident,
4267+
/// `CASCADE or RESTRICT`
4268+
pub drop_behavior: Option<DropBehavior>,
4269+
}
4270+
4271+
impl fmt::Display for DropOperatorFamily {
4272+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4273+
write!(f, "DROP OPERATOR FAMILY")?;
4274+
if self.if_exists {
4275+
write!(f, " IF EXISTS")?;
4276+
}
4277+
write!(f, " {}", display_comma_separated(&self.names))?;
4278+
write!(f, " USING {}", self.using)?;
4279+
if let Some(drop_behavior) = &self.drop_behavior {
4280+
write!(f, " {}", drop_behavior)?;
4281+
}
4282+
Ok(())
4283+
}
4284+
}
4285+
4286+
impl Spanned for DropOperatorFamily {
4287+
fn span(&self) -> Span {
4288+
Span::empty()
4289+
}
4290+
}

src/ast/mod.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,13 @@ pub use self::ddl::{
6767
ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
6868
CreateExtension, CreateFunction, CreateIndex, CreateOperator, CreateOperatorClass,
6969
CreateOperatorFamily, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
70-
DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorSignature, DropTrigger,
71-
GeneratedAs, GeneratedExpressionMode, IdentityParameters, IdentityProperty,
72-
IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder, IndexColumn,
73-
IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption, OperatorArgTypes,
74-
OperatorClassItem, OperatorPurpose, Owner, Partition, ProcedureParam, ReferentialAction,
75-
RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind, Truncate,
76-
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
70+
DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorFamily,
71+
DropOperatorSignature, DropTrigger, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
72+
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
73+
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
74+
OperatorArgTypes, OperatorClassItem, OperatorPurpose, Owner, Partition, ProcedureParam,
75+
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind,
76+
Truncate, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
7777
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
7878
UserDefinedTypeStorage, ViewColumnDef,
7979
};
@@ -3567,6 +3567,12 @@ pub enum Statement {
35673567
/// <https://www.postgresql.org/docs/current/sql-dropoperator.html>
35683568
DropOperator(DropOperator),
35693569
/// ```sql
3570+
/// DROP OPERATOR FAMILY [ IF EXISTS ] name USING index_method [ CASCADE | RESTRICT ]
3571+
/// ```
3572+
/// Note: this is a PostgreSQL-specific statement.
3573+
/// <https://www.postgresql.org/docs/current/sql-dropopfamily.html>
3574+
DropOperatorFamily(DropOperatorFamily),
3575+
/// ```sql
35703576
/// FETCH
35713577
/// ```
35723578
/// Retrieve rows from a query using a cursor
@@ -4794,6 +4800,9 @@ impl fmt::Display for Statement {
47944800
Statement::CreateExtension(create_extension) => write!(f, "{create_extension}"),
47954801
Statement::DropExtension(drop_extension) => write!(f, "{drop_extension}"),
47964802
Statement::DropOperator(drop_operator) => write!(f, "{drop_operator}"),
4803+
Statement::DropOperatorFamily(drop_operator_family) => {
4804+
write!(f, "{drop_operator_family}")
4805+
}
47974806
Statement::CreateRole(create_role) => write!(f, "{create_role}"),
47984807
Statement::CreateSecret {
47994808
or_replace,

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ impl Spanned for Statement {
369369
Statement::CreateExtension(create_extension) => create_extension.span(),
370370
Statement::DropExtension(drop_extension) => drop_extension.span(),
371371
Statement::DropOperator(drop_operator) => drop_operator.span(),
372+
Statement::DropOperatorFamily(drop_operator_family) => drop_operator_family.span(),
372373
Statement::CreateSecret { .. } => Span::empty(),
373374
Statement::CreateServer { .. } => Span::empty(),
374375
Statement::CreateConnector { .. } => Span::empty(),

src/parser/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6709,7 +6709,12 @@ impl<'a> Parser<'a> {
67096709
} else if self.parse_keyword(Keyword::EXTENSION) {
67106710
return self.parse_drop_extension();
67116711
} else if self.parse_keyword(Keyword::OPERATOR) {
6712-
return self.parse_drop_operator();
6712+
// Check if this is DROP OPERATOR FAMILY
6713+
return if self.parse_keyword(Keyword::FAMILY) {
6714+
self.parse_drop_operator_family()
6715+
} else {
6716+
self.parse_drop_operator()
6717+
};
67136718
} else {
67146719
return self.expected(
67156720
"CONNECTOR, DATABASE, EXTENSION, FUNCTION, INDEX, OPERATOR, POLICY, PROCEDURE, ROLE, SCHEMA, SECRET, SEQUENCE, STAGE, TABLE, TRIGGER, TYPE, VIEW, MATERIALIZED VIEW or USER after DROP",
@@ -7508,6 +7513,23 @@ impl<'a> Parser<'a> {
75087513
})
75097514
}
75107515

7516+
/// Parse a [Statement::DropOperatorFamily]
7517+
///
7518+
/// [PostgreSQL Documentation](https://www.postgresql.org/docs/current/sql-dropopfamily.html)
7519+
pub fn parse_drop_operator_family(&mut self) -> Result<Statement, ParserError> {
7520+
let if_exists = self.parse_keywords(&[Keyword::IF, Keyword::EXISTS]);
7521+
let names = self.parse_comma_separated(|p| p.parse_object_name(false))?;
7522+
self.expect_keyword(Keyword::USING)?;
7523+
let using = self.parse_identifier()?;
7524+
let drop_behavior = self.parse_optional_drop_behavior();
7525+
Ok(Statement::DropOperatorFamily(DropOperatorFamily {
7526+
if_exists,
7527+
names,
7528+
using,
7529+
drop_behavior,
7530+
}))
7531+
}
7532+
75117533
//TODO: Implement parsing for Skewed
75127534
pub fn parse_hive_distribution(&mut self) -> Result<HiveDistributionStyle, ParserError> {
75137535
if self.parse_keywords(&[Keyword::PARTITIONED, Keyword::BY]) {

tests/sqlparser_postgres.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6932,6 +6932,61 @@ fn parse_drop_operator() {
69326932
);
69336933
}
69346934

6935+
#[test]
6936+
fn parse_drop_operator_family() {
6937+
for if_exists in [true, false] {
6938+
for drop_behavior in [
6939+
None,
6940+
Some(DropBehavior::Cascade),
6941+
Some(DropBehavior::Restrict),
6942+
] {
6943+
for index_method in &["btree", "hash", "gist", "gin", "spgist", "brin"] {
6944+
for (names_str, names_vec) in [
6945+
(
6946+
"float_ops",
6947+
vec![ObjectName::from(vec![Ident::new("float_ops")])],
6948+
),
6949+
(
6950+
"myschema.custom_ops",
6951+
vec![ObjectName::from(vec![
6952+
Ident::new("myschema"),
6953+
Ident::new("custom_ops"),
6954+
])],
6955+
),
6956+
(
6957+
"ops1, ops2, schema.ops3",
6958+
vec![
6959+
ObjectName::from(vec![Ident::new("ops1")]),
6960+
ObjectName::from(vec![Ident::new("ops2")]),
6961+
ObjectName::from(vec![Ident::new("schema"), Ident::new("ops3")]),
6962+
],
6963+
),
6964+
] {
6965+
let sql = format!(
6966+
"DROP OPERATOR FAMILY{} {} USING {}{}",
6967+
if if_exists { " IF EXISTS" } else { "" },
6968+
names_str,
6969+
index_method,
6970+
match drop_behavior {
6971+
Some(behavior) => format!(" {}", behavior),
6972+
None => String::new(),
6973+
}
6974+
);
6975+
assert_eq!(
6976+
pg_and_generic().verified_stmt(&sql),
6977+
Statement::DropOperatorFamily(DropOperatorFamily {
6978+
if_exists,
6979+
names: names_vec,
6980+
using: Ident::new(*index_method),
6981+
drop_behavior,
6982+
})
6983+
);
6984+
}
6985+
}
6986+
}
6987+
}
6988+
}
6989+
69356990
#[test]
69366991
fn parse_create_operator_family() {
69376992
for index_method in &["btree", "hash", "gist", "gin", "spgist", "brin"] {

0 commit comments

Comments
 (0)