Skip to content

Commit 6a998ee

Browse files
LucaCappelletti94ayman-sigma
authored andcommitted
Added support for ALTER OPERATOR FAMILY syntax (apache#2125)
1 parent 4eee454 commit 6a998ee

File tree

5 files changed

+778
-25
lines changed

5 files changed

+778
-25
lines changed

src/ast/ddl.rs

Lines changed: 192 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4198,25 +4198,25 @@ impl fmt::Display for OperatorArgTypes {
41984198
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
41994199
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
42004200
pub enum OperatorClassItem {
4201-
/// OPERATOR clause
4201+
/// `OPERATOR` clause
42024202
Operator {
4203-
strategy_number: u32,
4203+
strategy_number: u64,
42044204
operator_name: ObjectName,
42054205
/// Optional operator argument types
42064206
op_types: Option<OperatorArgTypes>,
4207-
/// FOR SEARCH or FOR ORDER BY
4207+
/// `FOR SEARCH` or `FOR ORDER BY`
42084208
purpose: Option<OperatorPurpose>,
42094209
},
4210-
/// FUNCTION clause
4210+
/// `FUNCTION` clause
42114211
Function {
4212-
support_number: u32,
4212+
support_number: u64,
42134213
/// Optional function argument types for the operator class
42144214
op_types: Option<Vec<DataType>>,
42154215
function_name: ObjectName,
42164216
/// Function argument types
42174217
argument_types: Vec<DataType>,
42184218
},
4219-
/// STORAGE clause
4219+
/// `STORAGE` clause
42204220
Storage { storage_type: DataType },
42214221
}
42224222

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

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::{
8183
Delete, Insert, Merge, MergeAction, MergeClause, MergeClauseKind, MergeInsertExpr,
@@ -3445,6 +3447,11 @@ pub enum Statement {
34453447
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteroperator.html)
34463448
AlterOperator(AlterOperator),
34473449
/// ```sql
3450+
/// ALTER OPERATOR FAMILY
3451+
/// ```
3452+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteropfamily.html)
3453+
AlterOperatorFamily(AlterOperatorFamily),
3454+
/// ```sql
34483455
/// ALTER ROLE
34493456
/// ```
34503457
AlterRole {
@@ -5006,6 +5013,9 @@ impl fmt::Display for Statement {
50065013
write!(f, "ALTER TYPE {name} {operation}")
50075014
}
50085015
Statement::AlterOperator(alter_operator) => write!(f, "{alter_operator}"),
5016+
Statement::AlterOperatorFamily(alter_operator_family) => {
5017+
write!(f, "{alter_operator_family}")
5018+
}
50095019
Statement::AlterRole { name, operation } => {
50105020
write!(f, "ALTER ROLE {name} {operation}")
50115021
}

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)