Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
131 changes: 102 additions & 29 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,103 @@ impl fmt::Display for AlterTypeOperation {
}
}

/// `ALTER OPERATOR` statement
/// See <https://www.postgresql.org/docs/current/sql-alteroperator.html>
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct AlterOperator {
/// Operator name (can be schema-qualified)
pub name: ObjectName,
/// Left operand type (`None` if no left operand)
pub left_type: Option<DataType>,
/// Right operand type
pub right_type: DataType,
/// The operation to perform
pub operation: AlterOperatorOperation,
}

/// An [AlterOperator] operation
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum AlterOperatorOperation {
/// `OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }`
OwnerTo(Owner),
/// `SET SCHEMA new_schema`
SetSchema { schema_name: ObjectName },
/// `SET ( options )`
Set {
/// List of operator options to set
options: Vec<OperatorOption>,
},
}

/// Option for `ALTER OPERATOR SET` operation
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum OperatorOption {
/// `RESTRICT = { res_proc | NONE }`
Restrict(Option<ObjectName>),
/// `JOIN = { join_proc | NONE }`
Join(Option<ObjectName>),
/// `COMMUTATOR = com_op`
Commutator(ObjectName),
/// `NEGATOR = neg_op`
Negator(ObjectName),
/// `HASHES`
Hashes,
/// `MERGES`
Merges,
}

impl fmt::Display for AlterOperator {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ALTER OPERATOR {} (", self.name)?;
if let Some(left_type) = &self.left_type {
write!(f, "{}", left_type)?;
} else {
write!(f, "NONE")?;
}
write!(f, ", {}) {}", self.right_type, self.operation)
}
}

impl fmt::Display for AlterOperatorOperation {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::OwnerTo(owner) => write!(f, "OWNER TO {}", owner),
Self::SetSchema { schema_name } => write!(f, "SET SCHEMA {}", schema_name),
Self::Set { options } => {
write!(f, "SET (")?;
for (i, option) in options.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{}", option)?;
}
write!(f, ")")
}
}
}
}

impl fmt::Display for OperatorOption {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Restrict(Some(proc_name)) => write!(f, "RESTRICT = {}", proc_name),
Self::Restrict(None) => write!(f, "RESTRICT = NONE"),
Self::Join(Some(proc_name)) => write!(f, "JOIN = {}", proc_name),
Self::Join(None) => write!(f, "JOIN = NONE"),
Self::Commutator(op_name) => write!(f, "COMMUTATOR = {}", op_name),
Self::Negator(op_name) => write!(f, "NEGATOR = {}", op_name),
Self::Hashes => write!(f, "HASHES"),
Self::Merges => write!(f, "MERGES"),
}
}
}

/// An `ALTER COLUMN` (`Statement::AlterTable`) operation
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down Expand Up @@ -3979,18 +4076,8 @@ pub struct CreateOperator {
pub left_arg: Option<DataType>,
/// RIGHTARG parameter (right operand type)
pub right_arg: Option<DataType>,
/// COMMUTATOR parameter (commutator operator)
pub commutator: Option<ObjectName>,
/// NEGATOR parameter (negator operator)
pub negator: Option<ObjectName>,
/// RESTRICT parameter (restriction selectivity function)
pub restrict: Option<ObjectName>,
/// JOIN parameter (join selectivity function)
pub join: Option<ObjectName>,
/// HASHES flag
pub hashes: bool,
/// MERGES flag
pub merges: bool,
/// Operator options (COMMUTATOR, NEGATOR, RESTRICT, JOIN, HASHES, MERGES)
pub options: Vec<OperatorOption>,
}

/// CREATE OPERATOR FAMILY statement
Expand Down Expand Up @@ -4042,23 +4129,9 @@ impl fmt::Display for CreateOperator {
if let Some(right_arg) = &self.right_arg {
params.push(format!("RIGHTARG = {}", right_arg));
}
if let Some(commutator) = &self.commutator {
params.push(format!("COMMUTATOR = {}", commutator));
}
if let Some(negator) = &self.negator {
params.push(format!("NEGATOR = {}", negator));
}
if let Some(restrict) = &self.restrict {
params.push(format!("RESTRICT = {}", restrict));
}
if let Some(join) = &self.join {
params.push(format!("JOIN = {}", join));
}
if self.hashes {
params.push("HASHES".to_string());
}
if self.merges {
params.push("MERGES".to_string());

for option in &self.options {
params.push(option.to_string());
}

write!(f, "{}", params.join(", "))?;
Expand Down
28 changes: 17 additions & 11 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,23 @@ pub use self::dcl::{
AlterRoleOperation, CreateRole, ResetConfig, RoleOption, SecondaryRoles, SetConfigValue, Use,
};
pub use self::ddl::{
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation,
AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm,
AlterTableLock, AlterTableOperation, AlterTableType, AlterType, AlterTypeAddValue,
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterOperator,
AlterOperatorOperation, AlterPolicyOperation, AlterSchema, AlterSchemaOperation, AlterTable,
AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterTableType, AlterType,
AlterTypeAddValue, AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename,
AlterTypeRenameValue, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions,
ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics, CreateConnector, CreateDomain,
CreateExtension, CreateFunction, CreateIndex, CreateOperator, CreateOperatorClass,
CreateOperatorFamily, CreateTable, CreateTrigger, CreateView, Deduplicate, DeferrableInitial,
DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily,
DropOperatorSignature, DropTrigger, GeneratedAs, GeneratedExpressionMode, IdentityParameters,
IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder,
IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption,
OperatorArgTypes, OperatorClassItem, OperatorPurpose, Owner, Partition, ProcedureParam,
ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption, TriggerObjectKind,
Truncate, UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
UserDefinedTypeStorage, ViewColumnDef,
OperatorArgTypes, OperatorClassItem, OperatorOption, OperatorPurpose, Owner, Partition,
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, TagsColumnOption,
TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, ViewColumnDef,
};
pub use self::dml::{Delete, Insert, Update};
pub use self::operator::{BinaryOperator, UnaryOperator};
Expand Down Expand Up @@ -3396,6 +3396,11 @@ pub enum Statement {
/// ```
AlterType(AlterType),
/// ```sql
/// ALTER OPERATOR
/// ```
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteroperator.html)
AlterOperator(AlterOperator),
/// ```sql
/// ALTER ROLE
/// ```
AlterRole {
Expand Down Expand Up @@ -4971,6 +4976,7 @@ impl fmt::Display for Statement {
Statement::AlterType(AlterType { name, operation }) => {
write!(f, "ALTER TYPE {name} {operation}")
}
Statement::AlterOperator(alter_operator) => write!(f, "{alter_operator}"),
Statement::AlterRole { name, operation } => {
write!(f, "ALTER ROLE {name} {operation}")
}
Expand Down
2 changes: 2 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ impl Spanned for Values {
/// - [Statement::CreateSecret]
/// - [Statement::CreateRole]
/// - [Statement::AlterType]
/// - [Statement::AlterOperator]
/// - [Statement::AlterRole]
/// - [Statement::AttachDatabase]
/// - [Statement::AttachDuckDBDatabase]
Expand Down Expand Up @@ -401,6 +402,7 @@ impl Spanned for Statement {
),
// These statements need to be implemented
Statement::AlterType { .. } => Span::empty(),
Statement::AlterOperator { .. } => Span::empty(),
Statement::AlterRole { .. } => Span::empty(),
Statement::AlterSession { .. } => Span::empty(),
Statement::AttachDatabase { .. } => Span::empty(),
Expand Down
Loading