Skip to content

Commit 31280e0

Browse files
LucaCappelletti94fmguerreiro
authored andcommitted
Added support for ALTER OPERATOR FAMILY syntax (apache#2125)
1 parent f942fae commit 31280e0

File tree

5 files changed

+776
-23
lines changed

5 files changed

+776
-23
lines changed

src/ast/ddl.rs

Lines changed: 192 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4301,25 +4301,25 @@ impl fmt::Display for OperatorArgTypes {
43014301
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
43024302
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
43034303
pub enum OperatorClassItem {
4304-
/// OPERATOR clause
4304+
/// `OPERATOR` clause
43054305
Operator {
4306-
strategy_number: u32,
4306+
strategy_number: u64,
43074307
operator_name: ObjectName,
43084308
/// Optional operator argument types
43094309
op_types: Option<OperatorArgTypes>,
4310-
/// FOR SEARCH or FOR ORDER BY
4310+
/// `FOR SEARCH` or `FOR ORDER BY`
43114311
purpose: Option<OperatorPurpose>,
43124312
},
4313-
/// FUNCTION clause
4313+
/// `FUNCTION` clause
43144314
Function {
4315-
support_number: u32,
4315+
support_number: u64,
43164316
/// Optional function argument types for the operator class
43174317
op_types: Option<Vec<DataType>>,
43184318
function_name: ObjectName,
43194319
/// Function argument types
43204320
argument_types: Vec<DataType>,
43214321
},
4322-
/// STORAGE clause
4322+
/// `STORAGE` clause
43234323
Storage { storage_type: DataType },
43244324
}
43254325

@@ -4516,3 +4516,189 @@ impl Spanned for DropOperatorClass {
45164516
Span::empty()
45174517
}
45184518
}
4519+
4520+
/// An item in an ALTER OPERATOR FAMILY ADD statement
4521+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4522+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4523+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4524+
pub enum OperatorFamilyItem {
4525+
/// `OPERATOR` clause
4526+
Operator {
4527+
strategy_number: u64,
4528+
operator_name: ObjectName,
4529+
/// Operator argument types
4530+
op_types: Vec<DataType>,
4531+
/// `FOR SEARCH` or `FOR ORDER BY`
4532+
purpose: Option<OperatorPurpose>,
4533+
},
4534+
/// `FUNCTION` clause
4535+
Function {
4536+
support_number: u64,
4537+
/// Optional operator argument types for the function
4538+
op_types: Option<Vec<DataType>>,
4539+
function_name: ObjectName,
4540+
/// Function argument types
4541+
argument_types: Vec<DataType>,
4542+
},
4543+
}
4544+
4545+
/// An item in an ALTER OPERATOR FAMILY DROP statement
4546+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4547+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4548+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4549+
pub enum OperatorFamilyDropItem {
4550+
/// `OPERATOR` clause
4551+
Operator {
4552+
strategy_number: u64,
4553+
/// Operator argument types
4554+
op_types: Vec<DataType>,
4555+
},
4556+
/// `FUNCTION` clause
4557+
Function {
4558+
support_number: u64,
4559+
/// Operator argument types for the function
4560+
op_types: Vec<DataType>,
4561+
},
4562+
}
4563+
4564+
impl fmt::Display for OperatorFamilyItem {
4565+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4566+
match self {
4567+
OperatorFamilyItem::Operator {
4568+
strategy_number,
4569+
operator_name,
4570+
op_types,
4571+
purpose,
4572+
} => {
4573+
write!(
4574+
f,
4575+
"OPERATOR {strategy_number} {operator_name} ({})",
4576+
display_comma_separated(op_types)
4577+
)?;
4578+
if let Some(purpose) = purpose {
4579+
write!(f, " {purpose}")?;
4580+
}
4581+
Ok(())
4582+
}
4583+
OperatorFamilyItem::Function {
4584+
support_number,
4585+
op_types,
4586+
function_name,
4587+
argument_types,
4588+
} => {
4589+
write!(f, "FUNCTION {support_number}")?;
4590+
if let Some(types) = op_types {
4591+
write!(f, " ({})", display_comma_separated(types))?;
4592+
}
4593+
write!(f, " {function_name}")?;
4594+
if !argument_types.is_empty() {
4595+
write!(f, "({})", display_comma_separated(argument_types))?;
4596+
}
4597+
Ok(())
4598+
}
4599+
}
4600+
}
4601+
}
4602+
4603+
impl fmt::Display for OperatorFamilyDropItem {
4604+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4605+
match self {
4606+
OperatorFamilyDropItem::Operator {
4607+
strategy_number,
4608+
op_types,
4609+
} => {
4610+
write!(
4611+
f,
4612+
"OPERATOR {strategy_number} ({})",
4613+
display_comma_separated(op_types)
4614+
)
4615+
}
4616+
OperatorFamilyDropItem::Function {
4617+
support_number,
4618+
op_types,
4619+
} => {
4620+
write!(
4621+
f,
4622+
"FUNCTION {support_number} ({})",
4623+
display_comma_separated(op_types)
4624+
)
4625+
}
4626+
}
4627+
}
4628+
}
4629+
4630+
/// `ALTER OPERATOR FAMILY` statement
4631+
/// See <https://www.postgresql.org/docs/current/sql-alteropfamily.html>
4632+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4633+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4634+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4635+
pub struct AlterOperatorFamily {
4636+
/// Operator family name (can be schema-qualified)
4637+
pub name: ObjectName,
4638+
/// Index method (btree, hash, gist, gin, etc.)
4639+
pub using: Ident,
4640+
/// The operation to perform
4641+
pub operation: AlterOperatorFamilyOperation,
4642+
}
4643+
4644+
/// An [AlterOperatorFamily] operation
4645+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
4646+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
4647+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
4648+
pub enum AlterOperatorFamilyOperation {
4649+
/// `ADD { OPERATOR ... | FUNCTION ... } [, ...]`
4650+
Add {
4651+
/// List of operator family items to add
4652+
items: Vec<OperatorFamilyItem>,
4653+
},
4654+
/// `DROP { OPERATOR ... | FUNCTION ... } [, ...]`
4655+
Drop {
4656+
/// List of operator family items to drop
4657+
items: Vec<OperatorFamilyDropItem>,
4658+
},
4659+
/// `RENAME TO new_name`
4660+
RenameTo { new_name: ObjectName },
4661+
/// `OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }`
4662+
OwnerTo(Owner),
4663+
/// `SET SCHEMA new_schema`
4664+
SetSchema { schema_name: ObjectName },
4665+
}
4666+
4667+
impl fmt::Display for AlterOperatorFamily {
4668+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4669+
write!(
4670+
f,
4671+
"ALTER OPERATOR FAMILY {} USING {}",
4672+
self.name, self.using
4673+
)?;
4674+
write!(f, " {}", self.operation)
4675+
}
4676+
}
4677+
4678+
impl fmt::Display for AlterOperatorFamilyOperation {
4679+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
4680+
match self {
4681+
AlterOperatorFamilyOperation::Add { items } => {
4682+
write!(f, "ADD {}", display_comma_separated(items))
4683+
}
4684+
AlterOperatorFamilyOperation::Drop { items } => {
4685+
write!(f, "DROP {}", display_comma_separated(items))
4686+
}
4687+
AlterOperatorFamilyOperation::RenameTo { new_name } => {
4688+
write!(f, "RENAME TO {new_name}")
4689+
}
4690+
AlterOperatorFamilyOperation::OwnerTo(owner) => {
4691+
write!(f, "OWNER TO {owner}")
4692+
}
4693+
AlterOperatorFamilyOperation::SetSchema { schema_name } => {
4694+
write!(f, "SET SCHEMA {schema_name}")
4695+
}
4696+
}
4697+
}
4698+
}
4699+
4700+
impl Spanned for AlterOperatorFamily {
4701+
fn span(&self) -> Span {
4702+
Span::empty()
4703+
}
4704+
}

src/ast/mod.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,22 @@ 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, ForValues, GeneratedAs, GeneratedExpressionMode,
7273
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
7374
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
74-
NullsDistinctOption, OperatorArgTypes, OperatorClassItem, OperatorOption, OperatorPurpose,
75-
Owner, Partition, PartitionBoundValue, ProcedureParam, ReferentialAction, RenameTableNameKind,
76-
ReplicaIdentity, TagsColumnOption, TriggerObjectKind, Truncate,
75+
NullsDistinctOption, OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem,
76+
OperatorFamilyItem, OperatorOption, OperatorPurpose, Owner, Partition, PartitionBoundValue,
77+
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
78+
TriggerObjectKind, Truncate,
7779
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
7880
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
7981
UserDefinedTypeStorage, ViewColumnDef,
@@ -3412,6 +3414,11 @@ pub enum Statement {
34123414
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteroperator.html)
34133415
AlterOperator(AlterOperator),
34143416
/// ```sql
3417+
/// ALTER OPERATOR FAMILY
3418+
/// ```
3419+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteropfamily.html)
3420+
AlterOperatorFamily(AlterOperatorFamily),
3421+
/// ```sql
34153422
/// ALTER ROLE
34163423
/// ```
34173424
AlterRole {
@@ -4973,6 +4980,9 @@ impl fmt::Display for Statement {
49734980
write!(f, "ALTER TYPE {name} {operation}")
49744981
}
49754982
Statement::AlterOperator(alter_operator) => write!(f, "{alter_operator}"),
4983+
Statement::AlterOperatorFamily(alter_operator_family) => {
4984+
write!(f, "{alter_operator_family}")
4985+
}
49764986
Statement::AlterRole { name, operation } => {
49774987
write!(f, "ALTER ROLE {name} {operation}")
49784988
}

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)