Skip to content

Commit 778a8d3

Browse files
Added support for ALTER OPERATOR FAMILY syntax
1 parent 326f111 commit 778a8d3

File tree

5 files changed

+779
-25
lines changed

5 files changed

+779
-25
lines changed

src/ast/ddl.rs

Lines changed: 192 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4185,25 +4185,25 @@ impl fmt::Display for OperatorArgTypes {
41854185
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
41864186
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
41874187
pub enum OperatorClassItem {
4188-
/// OPERATOR clause
4188+
/// `OPERATOR` clause
41894189
Operator {
4190-
strategy_number: u32,
4190+
strategy_number: u64,
41914191
operator_name: ObjectName,
41924192
/// Optional operator argument types
41934193
op_types: Option<OperatorArgTypes>,
4194-
/// FOR SEARCH or FOR ORDER BY
4194+
/// `FOR SEARCH` or `FOR ORDER BY`
41954195
purpose: Option<OperatorPurpose>,
41964196
},
4197-
/// FUNCTION clause
4197+
/// `FUNCTION` clause
41984198
Function {
4199-
support_number: u32,
4199+
support_number: u64,
42004200
/// Optional function argument types for the operator class
42014201
op_types: Option<Vec<DataType>>,
42024202
function_name: ObjectName,
42034203
/// Function argument types
42044204
argument_types: Vec<DataType>,
42054205
},
4206-
/// STORAGE clause
4206+
/// `STORAGE` clause
42074207
Storage { storage_type: DataType },
42084208
}
42094209

@@ -4400,3 +4400,189 @@ impl Spanned for DropOperatorClass {
44004400
Span::empty()
44014401
}
44024402
}
4403+
4404+
/// An item in an ALTER OPERATOR FAMILY ADD statement
4405+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4406+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4407+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4408+
pub enum OperatorFamilyItem {
4409+
/// `OPERATOR` clause
4410+
Operator {
4411+
strategy_number: u64,
4412+
operator_name: ObjectName,
4413+
/// Operator argument types
4414+
op_types: Vec<DataType>,
4415+
/// `FOR SEARCH` or `FOR ORDER BY`
4416+
purpose: Option<OperatorPurpose>,
4417+
},
4418+
/// `FUNCTION` clause
4419+
Function {
4420+
support_number: u64,
4421+
/// Optional operator argument types for the function
4422+
op_types: Option<Vec<DataType>>,
4423+
function_name: ObjectName,
4424+
/// Function argument types
4425+
argument_types: Vec<DataType>,
4426+
},
4427+
}
4428+
4429+
/// An item in an ALTER OPERATOR FAMILY DROP statement
4430+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4431+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4432+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4433+
pub enum OperatorFamilyDropItem {
4434+
/// `OPERATOR` clause
4435+
Operator {
4436+
strategy_number: u64,
4437+
/// Operator argument types
4438+
op_types: Vec<DataType>,
4439+
},
4440+
/// `FUNCTION` clause
4441+
Function {
4442+
support_number: u64,
4443+
/// Operator argument types for the function
4444+
op_types: Vec<DataType>,
4445+
},
4446+
}
4447+
4448+
impl fmt::Display for OperatorFamilyItem {
4449+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4450+
match self {
4451+
OperatorFamilyItem::Operator {
4452+
strategy_number,
4453+
operator_name,
4454+
op_types,
4455+
purpose,
4456+
} => {
4457+
write!(
4458+
f,
4459+
"OPERATOR {strategy_number} {operator_name} ({})",
4460+
display_comma_separated(op_types)
4461+
)?;
4462+
if let Some(purpose) = purpose {
4463+
write!(f, " {purpose}")?;
4464+
}
4465+
Ok(())
4466+
}
4467+
OperatorFamilyItem::Function {
4468+
support_number,
4469+
op_types,
4470+
function_name,
4471+
argument_types,
4472+
} => {
4473+
write!(f, "FUNCTION {support_number}")?;
4474+
if let Some(types) = op_types {
4475+
write!(f, " ({})", display_comma_separated(types))?;
4476+
}
4477+
write!(f, " {function_name}")?;
4478+
if !argument_types.is_empty() {
4479+
write!(f, "({})", display_comma_separated(argument_types))?;
4480+
}
4481+
Ok(())
4482+
}
4483+
}
4484+
}
4485+
}
4486+
4487+
impl fmt::Display for OperatorFamilyDropItem {
4488+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4489+
match self {
4490+
OperatorFamilyDropItem::Operator {
4491+
strategy_number,
4492+
op_types,
4493+
} => {
4494+
write!(
4495+
f,
4496+
"OPERATOR {strategy_number} ({})",
4497+
display_comma_separated(op_types)
4498+
)
4499+
}
4500+
OperatorFamilyDropItem::Function {
4501+
support_number,
4502+
op_types,
4503+
} => {
4504+
write!(
4505+
f,
4506+
"FUNCTION {support_number} ({})",
4507+
display_comma_separated(op_types)
4508+
)
4509+
}
4510+
}
4511+
}
4512+
}
4513+
4514+
/// `ALTER OPERATOR FAMILY` statement
4515+
/// See <https://www.postgresql.org/docs/current/sql-alteropfamily.html>
4516+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4517+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4518+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4519+
pub struct AlterOperatorFamily {
4520+
/// Operator family name (can be schema-qualified)
4521+
pub name: ObjectName,
4522+
/// Index method (btree, hash, gist, gin, etc.)
4523+
pub using: Ident,
4524+
/// The operation to perform
4525+
pub operation: AlterOperatorFamilyOperation,
4526+
}
4527+
4528+
/// An [AlterOperatorFamily] operation
4529+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4530+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4531+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4532+
pub enum AlterOperatorFamilyOperation {
4533+
/// `ADD { OPERATOR ... | FUNCTION ... } [, ...]`
4534+
Add {
4535+
/// List of operator family items to add
4536+
items: Vec<OperatorFamilyItem>,
4537+
},
4538+
/// `DROP { OPERATOR ... | FUNCTION ... } [, ...]`
4539+
Drop {
4540+
/// List of operator family items to drop
4541+
items: Vec<OperatorFamilyDropItem>,
4542+
},
4543+
/// `RENAME TO new_name`
4544+
RenameTo { new_name: ObjectName },
4545+
/// `OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }`
4546+
OwnerTo(Owner),
4547+
/// `SET SCHEMA new_schema`
4548+
SetSchema { schema_name: ObjectName },
4549+
}
4550+
4551+
impl fmt::Display for AlterOperatorFamily {
4552+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4553+
write!(
4554+
f,
4555+
"ALTER OPERATOR FAMILY {} USING {}",
4556+
self.name, self.using
4557+
)?;
4558+
write!(f, " {}", self.operation)
4559+
}
4560+
}
4561+
4562+
impl fmt::Display for AlterOperatorFamilyOperation {
4563+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4564+
match self {
4565+
AlterOperatorFamilyOperation::Add { items } => {
4566+
write!(f, "ADD {}", display_comma_separated(items))
4567+
}
4568+
AlterOperatorFamilyOperation::Drop { items } => {
4569+
write!(f, "DROP {}", display_comma_separated(items))
4570+
}
4571+
AlterOperatorFamilyOperation::RenameTo { new_name } => {
4572+
write!(f, "RENAME TO {new_name}")
4573+
}
4574+
AlterOperatorFamilyOperation::OwnerTo(owner) => {
4575+
write!(f, "OWNER TO {owner}")
4576+
}
4577+
AlterOperatorFamilyOperation::SetSchema { schema_name } => {
4578+
write!(f, "SET SCHEMA {schema_name}")
4579+
}
4580+
}
4581+
}
4582+
}
4583+
4584+
impl Spanned for AlterOperatorFamily {
4585+
fn span(&self) -> Span {
4586+
Span::empty()
4587+
}
4588+
}

src/ast/mod.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,24 @@ pub use self::dcl::{
6060
};
6161
pub use self::ddl::{
6262
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterOperator,
63-
AlterOperatorOperation, AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable,
64-
AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterTableType, AlterType,
65-
AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename,
66-
AlterTypeRenameValue, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions,
67-
ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
63+
AlterOperatorFamily, AlterOperatorFamilyOperation, AlterOperatorOperation,
64+
AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm,
65+
AlterTableLock, AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue,
66+
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
67+
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
68+
ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
6869
CreateExtension, CreateFunction, CreateIndex, CreateOperator, CreateOperatorClass,
6970
CreateOperatorFamily, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
7071
DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily,
7172
DropOperatorSignature, DropTrigger, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
7273
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
7374
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
74-
OperatorArgTypes, OperatorClassItem, OperatorOption, OperatorPurpose, Owner, Partition,
75-
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
76-
TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
77-
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
78-
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
75+
OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem,
76+
OperatorOption, OperatorPurpose, Owner, Partition, ProcedureParam, ReferentialAction,
77+
RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind, Truncate,
78+
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
79+
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
80+
UserDefinedTypeStorage, ViewColumnDef,
7981
};
8082
pub use self::dml::{Delete, Insert, Update};
8183
pub use self::operator::{BinaryOperator, UnaryOperator};
@@ -3401,6 +3403,11 @@ pub enum Statement {
34013403
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteroperator.html)
34023404
AlterOperator(AlterOperator),
34033405
/// ```sql
3406+
/// ALTER OPERATOR FAMILY
3407+
/// ```
3408+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteropfamily.html)
3409+
AlterOperatorFamily(AlterOperatorFamily),
3410+
/// ```sql
34043411
/// ALTER ROLE
34053412
/// ```
34063413
AlterRole {
@@ -4977,6 +4984,9 @@ impl fmt::Display for Statement {
49774984
write!(f, "ALTER TYPE {name} {operation}")
49784985
}
49794986
Statement::AlterOperator(alter_operator) => write!(f, "{alter_operator}"),
4987+
Statement::AlterOperatorFamily(alter_operator_family) => {
4988+
write!(f, "{alter_operator_family}")
4989+
}
49804990
Statement::AlterRole { name, operation } => {
49814991
write!(f, "ALTER ROLE {name} {operation}")
49824992
}

src/ast/spans.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ impl Spanned for Statement {
403403
// These statements need to be implemented
404404
Statement::AlterType { .. } => Span::empty(),
405405
Statement::AlterOperator { .. } => Span::empty(),
406+
Statement::AlterOperatorFamily { .. } => Span::empty(),
406407
Statement::AlterRole { .. } => Span::empty(),
407408
Statement::AlterSession { .. } => Span::empty(),
408409
Statement::AttachDatabase { .. } => Span::empty(),

0 commit comments

Comments
 (0)