Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
106 changes: 57 additions & 49 deletions src/ast/ddl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6087,43 +6087,79 @@ impl Spanned for AlterOperatorClass {
}
}

/// An aggregate's name and argument signature.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct AggregateSignature {
/// Aggregate name.
pub name: ObjectName,
/// Aggregate arguments.
pub args: AggregateArgs,
}

/// Arguments identifying an aggregate.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum AggregateArgs {
/// The `*` signature.
Star,
/// Direct arguments with an optional ordered-set argument list.
List {
/// Direct arguments.
args: Vec<OperateFunctionArg>,
/// Arguments following `ORDER BY`.
order_by: Option<Vec<OperateFunctionArg>>,
},
}

impl fmt::Display for AggregateSignature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}(", self.name)?;
match &self.args {
AggregateArgs::Star => write!(f, "*")?,
AggregateArgs::List { args, order_by } => {
write!(f, "{}", display_comma_separated(args))?;
if let Some(order_by) = order_by {
if !args.is_empty() {
write!(f, " ")?;
}
write!(f, "ORDER BY {}", display_comma_separated(order_by))?;
}
}
}
write!(f, ")")
}
}

/// `ALTER FUNCTION` / `ALTER AGGREGATE` statement.
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub struct AlterFunction {
/// Object type being altered.
pub kind: AlterFunctionKind,
/// Function or aggregate signature.
pub function: FunctionDesc,
/// `ORDER BY` argument list for aggregate signatures.
///
/// This is only used for `ALTER AGGREGATE`.
pub aggregate_order_by: Option<Vec<OperateFunctionArg>>,
/// Whether the aggregate signature uses `*`.
///
/// This is only used for `ALTER AGGREGATE`.
pub aggregate_star: bool,
pub signature: AlterFunctionSignature,
/// Operation applied to the object.
pub operation: AlterFunctionOperation,
}

/// Function-like object type used by [`AlterFunction`].
/// The function or aggregate identified by [`AlterFunction`].
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum AlterFunctionKind {
/// `FUNCTION`
Function,
/// `AGGREGATE`
Aggregate,
pub enum AlterFunctionSignature {
/// A function signature.
Function(FunctionDesc),
/// An aggregate signature.
Aggregate(AggregateSignature),
}

impl fmt::Display for AlterFunctionKind {
impl fmt::Display for AlterFunctionSignature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Self::Function => write!(f, "FUNCTION"),
Self::Aggregate => write!(f, "AGGREGATE"),
Self::Function(function) => write!(f, "FUNCTION {function}"),
Self::Aggregate(signature) => write!(f, "AGGREGATE {signature}"),
}
}
}
Expand Down Expand Up @@ -6196,35 +6232,7 @@ pub enum AlterFunctionAction {

impl fmt::Display for AlterFunction {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "ALTER {} ", self.kind)?;
match self.kind {
AlterFunctionKind::Function => {
write!(f, "{} ", self.function)?;
}
AlterFunctionKind::Aggregate => {
write!(f, "{}(", self.function.name)?;
if self.aggregate_star {
write!(f, "*")?;
} else {
if let Some(args) = &self.function.args {
write!(f, "{}", display_comma_separated(args))?;
}
if let Some(order_by_args) = &self.aggregate_order_by {
if self
.function
.args
.as_ref()
.is_some_and(|args| !args.is_empty())
{
write!(f, " ")?;
}
write!(f, "ORDER BY {}", display_comma_separated(order_by_args))?;
}
}
write!(f, ") ")?;
}
}
write!(f, "{}", self.operation)
write!(f, "ALTER {} {}", self.signature, self.operation)
}
}

Expand Down
60 changes: 30 additions & 30 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,36 +64,36 @@ pub use self::dcl::{
SetConfigValue, Use,
};
pub use self::ddl::{
AggregateModifyKind, Alignment, AlterCollation, AlterCollationOperation, AlterColumnOperation,
AlterConnectorOwner, AlterExtension, AlterExtensionMember, AlterExtensionOperation,
AlterFunction, AlterFunctionAction, AlterFunctionKind, AlterFunctionOperation,
AlterGenericOption, AlterIndexOperation, AlterOperator, AlterOperatorClass,
AlterOperatorClassOperation, AlterOperatorFamily, AlterOperatorFamilyOperation,
AlterOperatorOperation, AlterPolicy, AlterPolicyOperation, AlterSchema, AlterSchemaOperation,
AlterTable, AlterTableAlgorithm, AlterTableLock, AlterTableOperation, AlterTableType,
AlterTextSearch, AlterTextSearchOperation, AlterTextSearchOption, AlterTrigger,
AlterTriggerOperation, AlterType, AlterTypeAddValue, AlterTypeAddValuePosition,
AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue, AlterView, AlterViewOperation,
ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef, ColumnOptions, ColumnPolicy,
ColumnPolicyProperty, ConstraintCharacteristics, CreateAggregate, CreateAggregateArgs,
CreateAggregateOption, CreateCollation, CreateCollationDefinition, CreateConnector,
CreateDomain, CreateExtension, CreateForeignDataWrapper, CreateForeignTable, CreateFunction,
CreateIndex, CreateOperator, CreateOperatorClass, CreateOperatorFamily, CreatePolicy,
CreatePolicyCommand, CreatePolicyType, CreateRule, CreateRuleAction, CreateRuleDoKind,
CreateRuleEvent, CreateTable, CreateTextSearch, CreateTrigger, CreateUserMapping, CreateView,
Deduplicate, DeferrableInitial, DetachPartitionMode, DistStyle, DropBehavior, DropExtension,
DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily, DropOperatorSignature,
DropPolicy, DropRule, DropTrigger, ForValues, ForeignDataWrapperRoutineClause,
FunctionReturnType, GeneratedAs, GeneratedExpressionMode, IdentityParameters, IdentityProperty,
IdentityPropertyFormatKind, IdentityPropertyKind, IdentityPropertyOrder, IndexColumn,
IndexOption, IndexType, KeyOrIndexDisplay, Msck, NullsDistinctOption, OperatorArgTypes,
OperatorClassItem, OperatorFamilyDropItem, OperatorFamilyItem, OperatorOption, OperatorPurpose,
Owner, Partition, PartitionBoundValue, ProcedureParam, ReferentialAction, RenameTableNameKind,
ReplicaIdentity, SqliteTableOption, SqliteTableOptions, TagsColumnOption, TextSearchObjectType,
TriggerObjectKind, Truncate, UserDefinedTypeCompositeAttributeDef,
UserDefinedTypeInternalLength, UserDefinedTypeRangeOption, UserDefinedTypeRepresentation,
UserDefinedTypeSqlDefinitionOption, UserDefinedTypeStorage, UserMappingUser, ViewColumnDef,
WithData,
AggregateArgs, AggregateModifyKind, AggregateSignature, Alignment, AlterCollation,
AlterCollationOperation, AlterColumnOperation, AlterConnectorOwner, AlterExtension,
AlterExtensionMember, AlterExtensionOperation, AlterFunction, AlterFunctionAction,
AlterFunctionOperation, AlterFunctionSignature, AlterGenericOption, AlterIndexOperation,
AlterOperator, AlterOperatorClass, AlterOperatorClassOperation, AlterOperatorFamily,
AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy, AlterPolicyOperation,
AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock,
AlterTableOperation, AlterTableType, AlterTextSearch, AlterTextSearchOperation,
AlterTextSearchOption, AlterTrigger, AlterTriggerOperation, AlterType, AlterTypeAddValue,
AlterTypeAddValuePosition, AlterTypeOperation, AlterTypeRename, AlterTypeRenameValue,
AlterView, AlterViewOperation, ClusteredBy, ColumnDef, ColumnOption, ColumnOptionDef,
ColumnOptions, ColumnPolicy, ColumnPolicyProperty, ConstraintCharacteristics, CreateAggregate,
CreateAggregateArgs, CreateAggregateOption, CreateCollation, CreateCollationDefinition,
CreateConnector, CreateDomain, CreateExtension, CreateForeignDataWrapper, CreateForeignTable,
CreateFunction, CreateIndex, CreateOperator, CreateOperatorClass, CreateOperatorFamily,
CreatePolicy, CreatePolicyCommand, CreatePolicyType, CreateRule, CreateRuleAction,
CreateRuleDoKind, CreateRuleEvent, CreateTable, CreateTextSearch, CreateTrigger,
CreateUserMapping, CreateView, Deduplicate, DeferrableInitial, DetachPartitionMode, DistStyle,
DropBehavior, DropExtension, DropFunction, DropOperator, DropOperatorClass, DropOperatorFamily,
DropOperatorSignature, DropPolicy, DropRule, DropTrigger, ForValues,
ForeignDataWrapperRoutineClause, FunctionReturnType, GeneratedAs, GeneratedExpressionMode,
IdentityParameters, IdentityProperty, IdentityPropertyFormatKind, IdentityPropertyKind,
IdentityPropertyOrder, IndexColumn, IndexOption, IndexType, KeyOrIndexDisplay, Msck,
NullsDistinctOption, OperatorArgTypes, OperatorClassItem, OperatorFamilyDropItem,
OperatorFamilyItem, OperatorOption, OperatorPurpose, Owner, Partition, PartitionBoundValue,
ProcedureParam, ReferentialAction, RenameTableNameKind, ReplicaIdentity, SqliteTableOption,
SqliteTableOptions, TagsColumnOption, TextSearchObjectType, TriggerObjectKind, Truncate,
UserDefinedTypeCompositeAttributeDef, UserDefinedTypeInternalLength,
UserDefinedTypeRangeOption, UserDefinedTypeRepresentation, UserDefinedTypeSqlDefinitionOption,
UserDefinedTypeStorage, UserMappingUser, ViewColumnDef, WithData,
};
pub use self::dml::{
Delete, Insert, InsertOverriding, Merge, MergeAction, MergeClause, MergeClauseKind,
Expand Down
63 changes: 27 additions & 36 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12049,8 +12049,15 @@ impl<'a> Parser<'a> {
operation,
})
}
Keyword::FUNCTION => self.parse_alter_function(AlterFunctionKind::Function),
Keyword::AGGREGATE => self.parse_alter_function(AlterFunctionKind::Aggregate),
Keyword::FUNCTION => {
let signature = AlterFunctionSignature::Function(self.parse_function_desc()?);
self.parse_alter_function(signature)
}
Keyword::AGGREGATE => {
let signature =
AlterFunctionSignature::Aggregate(self.parse_alter_aggregate_signature()?);
self.parse_alter_function(signature)
}
Keyword::OPERATOR => {
if self.parse_keyword(Keyword::FAMILY) {
self.parse_alter_operator_family().map(Into::into)
Expand Down Expand Up @@ -12078,22 +12085,16 @@ impl<'a> Parser<'a> {
}
}

fn parse_alter_aggregate_signature(
&mut self,
) -> Result<(FunctionDesc, bool, Option<Vec<OperateFunctionArg>>), ParserError> {
fn parse_alter_aggregate_signature(&mut self) -> Result<AggregateSignature, ParserError> {
let name = self.parse_object_name(false)?;
self.expect_token(&Token::LParen)?;

if self.consume_token(&Token::Mul) {
self.expect_token(&Token::RParen)?;
return Ok((
FunctionDesc {
name,
args: Some(vec![]),
},
true,
None,
));
return Ok(AggregateSignature {
name,
args: AggregateArgs::Star,
});
}

let args =
Expand All @@ -12103,21 +12104,17 @@ impl<'a> Parser<'a> {
self.parse_comma_separated(Parser::parse_aggregate_function_arg)?
};

let aggregate_order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
let order_by = if self.parse_keywords(&[Keyword::ORDER, Keyword::BY]) {
Some(self.parse_comma_separated(Parser::parse_aggregate_function_arg)?)
} else {
None
};

self.expect_token(&Token::RParen)?;
Ok((
FunctionDesc {
name,
args: Some(args),
},
false,
aggregate_order_by,
))
Ok(AggregateSignature {
name,
args: AggregateArgs::List { args, order_by },
})
}

fn parse_alter_function_action(&mut self) -> Result<Option<AlterFunctionAction>, ParserError> {
Expand Down Expand Up @@ -12236,16 +12233,11 @@ impl<'a> Parser<'a> {
Ok((actions, restrict))
}

/// Parse an `ALTER FUNCTION` or `ALTER AGGREGATE` statement.
/// Parse an `ALTER FUNCTION` or `ALTER AGGREGATE` operation after its signature.
pub fn parse_alter_function(
&mut self,
kind: AlterFunctionKind,
signature: AlterFunctionSignature,
) -> Result<Statement, ParserError> {
let (function, aggregate_star, aggregate_order_by) = match kind {
AlterFunctionKind::Function => (self.parse_function_desc()?, false, None),
AlterFunctionKind::Aggregate => self.parse_alter_aggregate_signature()?,
};

let operation = if self.parse_keywords(&[Keyword::RENAME, Keyword::TO]) {
let new_name = self.parse_identifier()?;
AlterFunctionOperation::RenameTo { new_name }
Expand All @@ -12255,7 +12247,9 @@ impl<'a> Parser<'a> {
AlterFunctionOperation::SetSchema {
schema_name: self.parse_object_name(false)?,
}
} else if matches!(kind, AlterFunctionKind::Function) && self.parse_keyword(Keyword::NO) {
} else if matches!(signature, AlterFunctionSignature::Function(_))
&& self.parse_keyword(Keyword::NO)
{
if !self.parse_keyword(Keyword::DEPENDS) {
return self.expected_ref("DEPENDS after NO", self.peek_token_ref());
}
Expand All @@ -12264,15 +12258,15 @@ impl<'a> Parser<'a> {
no: true,
extension_name: self.parse_object_name(false)?,
}
} else if matches!(kind, AlterFunctionKind::Function)
} else if matches!(signature, AlterFunctionSignature::Function(_))
&& self.parse_keyword(Keyword::DEPENDS)
{
self.expect_keywords(&[Keyword::ON, Keyword::EXTENSION])?;
AlterFunctionOperation::DependsOnExtension {
no: false,
extension_name: self.parse_object_name(false)?,
}
} else if matches!(kind, AlterFunctionKind::Function) {
} else if matches!(signature, AlterFunctionSignature::Function(_)) {
let (actions, restrict) = self.parse_alter_function_actions()?;
AlterFunctionOperation::Actions { actions, restrict }
} else {
Expand All @@ -12283,10 +12277,7 @@ impl<'a> Parser<'a> {
};

Ok(Statement::AlterFunction(AlterFunction {
kind,
function,
aggregate_order_by,
aggregate_star,
signature,
operation,
}))
}
Expand Down
Loading
Loading