Skip to content

Commit 2b0dc2d

Browse files
PostgreSQL ALTER FUNCTION / ALTER AGGREGATE (#2248)
1 parent a05834d commit 2b0dc2d

File tree

6 files changed

+756
-10
lines changed

6 files changed

+756
-10
lines changed

src/ast/ddl.rs

Lines changed: 212 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ use crate::ast::{
4747
FunctionDeterminismSpecifier, FunctionParallel, FunctionSecurity, HiveDistributionStyle,
4848
HiveFormat, HiveIOFormat, HiveRowFormat, HiveSetLocation, Ident, InitializeKind,
4949
MySQLColumnPosition, ObjectName, OnCommit, OneOrManyWithParens, OperateFunctionArg,
50-
OrderByExpr, ProjectionSelect, Query, RefreshModeKind, RowAccessPolicy, SequenceOptions,
51-
Spanned, SqlOption, StorageLifecyclePolicy, StorageSerializationPolicy, TableVersion, Tag,
52-
TriggerEvent, TriggerExecBody, TriggerObject, TriggerPeriod, TriggerReferencing, Value,
53-
ValueWithSpan, WrappedCollection,
50+
OrderByExpr, ProjectionSelect, Query, RefreshModeKind, ResetConfig, RowAccessPolicy,
51+
SequenceOptions, Spanned, SqlOption, StorageLifecyclePolicy, StorageSerializationPolicy,
52+
TableVersion, Tag, TriggerEvent, TriggerExecBody, TriggerObject, TriggerPeriod,
53+
TriggerReferencing, Value, ValueWithSpan, WrappedCollection,
5454
};
5555
use crate::display_utils::{DisplayCommaSeparated, Indent, NewLine, SpaceOrNewline};
5656
use crate::keywords::Keyword;
@@ -5222,6 +5222,214 @@ impl Spanned for AlterOperatorClass {
52225222
}
52235223
}
52245224

5225+
/// `ALTER FUNCTION` / `ALTER AGGREGATE` statement.
5226+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5227+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5228+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5229+
pub struct AlterFunction {
5230+
/// Object type being altered.
5231+
pub kind: AlterFunctionKind,
5232+
/// Function or aggregate signature.
5233+
pub function: FunctionDesc,
5234+
/// `ORDER BY` argument list for aggregate signatures.
5235+
///
5236+
/// This is only used for `ALTER AGGREGATE`.
5237+
pub aggregate_order_by: Option<Vec<OperateFunctionArg>>,
5238+
/// Whether the aggregate signature uses `*`.
5239+
///
5240+
/// This is only used for `ALTER AGGREGATE`.
5241+
pub aggregate_star: bool,
5242+
/// Operation applied to the object.
5243+
pub operation: AlterFunctionOperation,
5244+
}
5245+
5246+
/// Function-like object type used by [`AlterFunction`].
5247+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5248+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5249+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5250+
pub enum AlterFunctionKind {
5251+
/// `FUNCTION`
5252+
Function,
5253+
/// `AGGREGATE`
5254+
Aggregate,
5255+
}
5256+
5257+
impl fmt::Display for AlterFunctionKind {
5258+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5259+
match self {
5260+
Self::Function => write!(f, "FUNCTION"),
5261+
Self::Aggregate => write!(f, "AGGREGATE"),
5262+
}
5263+
}
5264+
}
5265+
5266+
/// Operation for `ALTER FUNCTION` / `ALTER AGGREGATE`.
5267+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5268+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5269+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5270+
pub enum AlterFunctionOperation {
5271+
/// `RENAME TO new_name`
5272+
RenameTo {
5273+
/// New unqualified function or aggregate name.
5274+
new_name: Ident,
5275+
},
5276+
/// `OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USER }`
5277+
OwnerTo(Owner),
5278+
/// `SET SCHEMA schema_name`
5279+
SetSchema {
5280+
/// The target schema name.
5281+
schema_name: ObjectName,
5282+
},
5283+
/// `[ NO ] DEPENDS ON EXTENSION extension_name`
5284+
DependsOnExtension {
5285+
/// `true` when `NO DEPENDS ON EXTENSION`.
5286+
no: bool,
5287+
/// Extension name.
5288+
extension_name: ObjectName,
5289+
},
5290+
/// `action [ ... ] [ RESTRICT ]` (function only).
5291+
Actions {
5292+
/// One or more function actions.
5293+
actions: Vec<AlterFunctionAction>,
5294+
/// Whether `RESTRICT` is present.
5295+
restrict: bool,
5296+
},
5297+
}
5298+
5299+
/// Function action in `ALTER FUNCTION ... action [ ... ] [ RESTRICT ]`.
5300+
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
5301+
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
5302+
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
5303+
pub enum AlterFunctionAction {
5304+
/// `CALLED ON NULL INPUT` / `RETURNS NULL ON NULL INPUT` / `STRICT`
5305+
CalledOnNull(FunctionCalledOnNull),
5306+
/// `IMMUTABLE` / `STABLE` / `VOLATILE`
5307+
Behavior(FunctionBehavior),
5308+
/// `[ NOT ] LEAKPROOF`
5309+
Leakproof(bool),
5310+
/// `[ EXTERNAL ] SECURITY { DEFINER | INVOKER }`
5311+
Security {
5312+
/// Whether the optional `EXTERNAL` keyword was present.
5313+
external: bool,
5314+
/// Security mode.
5315+
security: FunctionSecurity,
5316+
},
5317+
/// `PARALLEL { UNSAFE | RESTRICTED | SAFE }`
5318+
Parallel(FunctionParallel),
5319+
/// `COST execution_cost`
5320+
Cost(Expr),
5321+
/// `ROWS result_rows`
5322+
Rows(Expr),
5323+
/// `SUPPORT support_function`
5324+
Support(ObjectName),
5325+
/// `SET configuration_parameter { TO | = } { value | DEFAULT }`
5326+
/// or `SET configuration_parameter FROM CURRENT`
5327+
Set(FunctionDefinitionSetParam),
5328+
/// `RESET configuration_parameter` or `RESET ALL`
5329+
Reset(ResetConfig),
5330+
}
5331+
5332+
impl fmt::Display for AlterFunction {
5333+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5334+
write!(f, "ALTER {} ", self.kind)?;
5335+
match self.kind {
5336+
AlterFunctionKind::Function => {
5337+
write!(f, "{} ", self.function)?;
5338+
}
5339+
AlterFunctionKind::Aggregate => {
5340+
write!(f, "{}(", self.function.name)?;
5341+
if self.aggregate_star {
5342+
write!(f, "*")?;
5343+
} else {
5344+
if let Some(args) = &self.function.args {
5345+
write!(f, "{}", display_comma_separated(args))?;
5346+
}
5347+
if let Some(order_by_args) = &self.aggregate_order_by {
5348+
if self
5349+
.function
5350+
.args
5351+
.as_ref()
5352+
.is_some_and(|args| !args.is_empty())
5353+
{
5354+
write!(f, " ")?;
5355+
}
5356+
write!(f, "ORDER BY {}", display_comma_separated(order_by_args))?;
5357+
}
5358+
}
5359+
write!(f, ") ")?;
5360+
}
5361+
}
5362+
write!(f, "{}", self.operation)
5363+
}
5364+
}
5365+
5366+
impl fmt::Display for AlterFunctionOperation {
5367+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5368+
match self {
5369+
AlterFunctionOperation::RenameTo { new_name } => {
5370+
write!(f, "RENAME TO {new_name}")
5371+
}
5372+
AlterFunctionOperation::OwnerTo(owner) => write!(f, "OWNER TO {owner}"),
5373+
AlterFunctionOperation::SetSchema { schema_name } => {
5374+
write!(f, "SET SCHEMA {schema_name}")
5375+
}
5376+
AlterFunctionOperation::DependsOnExtension { no, extension_name } => {
5377+
if *no {
5378+
write!(f, "NO DEPENDS ON EXTENSION {extension_name}")
5379+
} else {
5380+
write!(f, "DEPENDS ON EXTENSION {extension_name}")
5381+
}
5382+
}
5383+
AlterFunctionOperation::Actions { actions, restrict } => {
5384+
write!(f, "{}", display_separated(actions, " "))?;
5385+
if *restrict {
5386+
write!(f, " RESTRICT")?;
5387+
}
5388+
Ok(())
5389+
}
5390+
}
5391+
}
5392+
}
5393+
5394+
impl fmt::Display for AlterFunctionAction {
5395+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5396+
match self {
5397+
AlterFunctionAction::CalledOnNull(called_on_null) => write!(f, "{called_on_null}"),
5398+
AlterFunctionAction::Behavior(behavior) => write!(f, "{behavior}"),
5399+
AlterFunctionAction::Leakproof(leakproof) => {
5400+
if *leakproof {
5401+
write!(f, "LEAKPROOF")
5402+
} else {
5403+
write!(f, "NOT LEAKPROOF")
5404+
}
5405+
}
5406+
AlterFunctionAction::Security { external, security } => {
5407+
if *external {
5408+
write!(f, "EXTERNAL ")?;
5409+
}
5410+
write!(f, "{security}")
5411+
}
5412+
AlterFunctionAction::Parallel(parallel) => write!(f, "{parallel}"),
5413+
AlterFunctionAction::Cost(execution_cost) => write!(f, "COST {execution_cost}"),
5414+
AlterFunctionAction::Rows(result_rows) => write!(f, "ROWS {result_rows}"),
5415+
AlterFunctionAction::Support(support_function) => {
5416+
write!(f, "SUPPORT {support_function}")
5417+
}
5418+
AlterFunctionAction::Set(set_param) => write!(f, "{set_param}"),
5419+
AlterFunctionAction::Reset(reset_config) => match reset_config {
5420+
ResetConfig::ALL => write!(f, "RESET ALL"),
5421+
ResetConfig::ConfigName(name) => write!(f, "RESET {name}"),
5422+
},
5423+
}
5424+
}
5425+
}
5426+
5427+
impl Spanned for AlterFunction {
5428+
fn span(&self) -> Span {
5429+
Span::empty()
5430+
}
5431+
}
5432+
52255433
/// CREATE POLICY statement.
52265434
///
52275435
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-createpolicy.html)

src/ast/mod.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ pub use self::dcl::{
6060
SetConfigValue, Use,
6161
};
6262
pub use self::ddl::{
63-
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterIndexOperation, AlterOperator,
63+
Alignment, AlterColumnOperation, AlterConnectorOwner, AlterFunction, AlterFunctionAction,
64+
AlterFunctionKind, AlterFunctionOperation, AlterIndexOperation, AlterOperator,
6465
AlterOperatorClass, AlterOperatorClassOperation, AlterOperatorFamily,
6566
AlterOperatorFamilyOperation, AlterOperatorOperation, AlterPolicy, AlterPolicyOperation,
6667
AlterSchema, AlterSchemaOperation, AlterTable, AlterTableAlgorithm, AlterTableLock,
@@ -3751,6 +3752,13 @@ pub enum Statement {
37513752
with_options: Vec<SqlOption>,
37523753
},
37533754
/// ```sql
3755+
/// ALTER FUNCTION
3756+
/// ALTER AGGREGATE
3757+
/// ```
3758+
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-alterfunction.html)
3759+
/// and [PostgreSQL](https://www.postgresql.org/docs/current/sql-alteraggregate.html)
3760+
AlterFunction(AlterFunction),
3761+
/// ```sql
37543762
/// ALTER TYPE
37553763
/// See [PostgreSQL](https://www.postgresql.org/docs/current/sql-altertype.html)
37563764
/// ```
@@ -5495,6 +5503,7 @@ impl fmt::Display for Statement {
54955503
}
54965504
write!(f, " AS {query}")
54975505
}
5506+
Statement::AlterFunction(alter_function) => write!(f, "{alter_function}"),
54985507
Statement::AlterType(AlterType { name, operation }) => {
54995508
write!(f, "ALTER TYPE {name} {operation}")
55005509
}
@@ -9832,6 +9841,8 @@ pub enum ArgMode {
98329841
Out,
98339842
/// `INOUT` mode.
98349843
InOut,
9844+
/// `VARIADIC` mode.
9845+
Variadic,
98359846
}
98369847

98379848
impl fmt::Display for ArgMode {
@@ -9840,6 +9851,7 @@ impl fmt::Display for ArgMode {
98409851
ArgMode::In => write!(f, "IN"),
98419852
ArgMode::Out => write!(f, "OUT"),
98429853
ArgMode::InOut => write!(f, "INOUT"),
9854+
ArgMode::Variadic => write!(f, "VARIADIC"),
98439855
}
98449856
}
98459857
}
@@ -9896,6 +9908,8 @@ impl fmt::Display for FunctionSecurity {
98969908
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98979909
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
98989910
pub enum FunctionSetValue {
9911+
/// SET param = DEFAULT / SET param TO DEFAULT
9912+
Default,
98999913
/// SET param = value1, value2, ...
99009914
Values(Vec<Expr>),
99019915
/// SET param FROM CURRENT
@@ -9910,7 +9924,7 @@ pub enum FunctionSetValue {
99109924
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
99119925
pub struct FunctionDefinitionSetParam {
99129926
/// The name of the configuration parameter.
9913-
pub name: Ident,
9927+
pub name: ObjectName,
99149928
/// The value to set for the parameter.
99159929
pub value: FunctionSetValue,
99169930
}
@@ -9919,6 +9933,7 @@ impl fmt::Display for FunctionDefinitionSetParam {
99199933
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99209934
write!(f, "SET {} ", self.name)?;
99219935
match &self.value {
9936+
FunctionSetValue::Default => write!(f, "= DEFAULT"),
99229937
FunctionSetValue::Values(values) => {
99239938
write!(f, "= {}", display_comma_separated(values))
99249939
}
@@ -12098,6 +12113,12 @@ impl From<AlterSchema> for Statement {
1209812113
}
1209912114
}
1210012115

12116+
impl From<AlterFunction> for Statement {
12117+
fn from(a: AlterFunction) -> Self {
12118+
Self::AlterFunction(a)
12119+
}
12120+
}
12121+
1210112122
impl From<AlterType> for Statement {
1210212123
fn from(a: AlterType) -> Self {
1210312124
Self::AlterType(a)

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
.chain(with_options.iter().map(|i| i.span())),
404404
),
405405
// These statements need to be implemented
406+
Statement::AlterFunction { .. } => Span::empty(),
406407
Statement::AlterType { .. } => Span::empty(),
407408
Statement::AlterOperator { .. } => Span::empty(),
408409
Statement::AlterOperatorFamily { .. } => Span::empty(),

src/keywords.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ define_keywords!(
259259
COPY_OPTIONS,
260260
CORR,
261261
CORRESPONDING,
262+
COST,
262263
COUNT,
263264
COVAR_POP,
264265
COVAR_SAMP,
@@ -324,6 +325,7 @@ define_keywords!(
324325
DELTA,
325326
DENSE_RANK,
326327
DENY,
328+
DEPENDS,
327329
DEREF,
328330
DESC,
329331
DESCRIBE,
@@ -1136,6 +1138,7 @@ define_keywords!(
11361138
VARCHAR2,
11371139
VARIABLE,
11381140
VARIABLES,
1141+
VARIADIC,
11391142
VARYING,
11401143
VAR_POP,
11411144
VAR_SAMP,

0 commit comments

Comments
 (0)