@@ -6078,3 +6078,165 @@ impl Spanned for CreateForeignTable {
60786078 )
60796079 }
60806080}
6081+
6082+ /// CREATE AGGREGATE statement.
6083+ /// See <https://www.postgresql.org/docs/current/sql-createaggregate.html>
6084+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
6085+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
6086+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
6087+ pub struct CreateAggregate {
6088+ /// True if `OR REPLACE` was specified.
6089+ pub or_replace : bool ,
6090+ /// The aggregate name (can be schema-qualified).
6091+ pub name : ObjectName ,
6092+ /// The argument list preceding the options list.
6093+ pub args : CreateAggregateArgs ,
6094+ /// The options listed inside the required parentheses after the argument
6095+ /// list (e.g. `SFUNC`, `STYPE`, `FINALFUNC`, `PARALLEL`, …).
6096+ pub options : Vec < CreateAggregateOption > ,
6097+ }
6098+
6099+ impl fmt:: Display for CreateAggregate {
6100+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
6101+ write ! ( f, "CREATE" ) ?;
6102+ if self . or_replace {
6103+ write ! ( f, " OR REPLACE" ) ?;
6104+ }
6105+ write ! ( f, " AGGREGATE {}" , self . name) ?;
6106+ match & self . args {
6107+ CreateAggregateArgs :: Legacy => { }
6108+ CreateAggregateArgs :: Star => write ! ( f, " (*)" ) ?,
6109+ CreateAggregateArgs :: List ( args) => write ! ( f, " ({})" , display_comma_separated( args) ) ?,
6110+ }
6111+ write ! ( f, " ({})" , display_comma_separated( & self . options) )
6112+ }
6113+ }
6114+
6115+ /// The argument list of a [`CreateAggregate`] statement.
6116+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
6117+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
6118+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
6119+ pub enum CreateAggregateArgs {
6120+ /// No argument-list parentheses: the historical form that packs everything,
6121+ /// usually including a `BASETYPE` option, into a single list.
6122+ Legacy ,
6123+ /// The wildcard form `(*)`, used by zero-argument aggregates such as
6124+ /// `count(*)`.
6125+ Star ,
6126+ /// An explicit argument list, possibly empty: `()`, `(NUMERIC)`,
6127+ /// `(input INT, VARIADIC tail TEXT)`.
6128+ List ( Vec < OperateFunctionArg > ) ,
6129+ }
6130+
6131+ impl From < CreateAggregate > for crate :: ast:: Statement {
6132+ fn from ( v : CreateAggregate ) -> Self {
6133+ crate :: ast:: Statement :: CreateAggregate ( v)
6134+ }
6135+ }
6136+
6137+ /// A single option in a `CREATE AGGREGATE` options list.
6138+ ///
6139+ /// See <https://www.postgresql.org/docs/current/sql-createaggregate.html>
6140+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
6141+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
6142+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
6143+ pub enum CreateAggregateOption {
6144+ /// `SFUNC = state_transition_function`
6145+ StateTransitionFunction ( ObjectName ) ,
6146+ /// `STYPE = state_data_type`
6147+ StateDataType ( DataType ) ,
6148+ /// `SSPACE = state_data_size` (in bytes)
6149+ StateDataSize ( u64 ) ,
6150+ /// `FINALFUNC = final_function`
6151+ FinalFunction ( ObjectName ) ,
6152+ /// `FINALFUNC_EXTRA`. Passes extra dummy arguments to the final function.
6153+ FinalFunctionExtra ,
6154+ /// `FINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE }`
6155+ FinalFunctionModify ( AggregateModifyKind ) ,
6156+ /// `COMBINEFUNC = combine_function`
6157+ CombineFunction ( ObjectName ) ,
6158+ /// `SERIALFUNC = serial_function`
6159+ SerialFunction ( ObjectName ) ,
6160+ /// `DESERIALFUNC = deserial_function`
6161+ DeserialFunction ( ObjectName ) ,
6162+ /// `INITCOND = initial_condition` (a string literal)
6163+ InitialCondition ( ValueWithSpan ) ,
6164+ /// `MSFUNC = moving_state_transition_function`
6165+ MovingStateTransitionFunction ( ObjectName ) ,
6166+ /// `MINVFUNC = moving_inverse_transition_function`
6167+ MovingInverseTransitionFunction ( ObjectName ) ,
6168+ /// `MSTYPE = moving_state_data_type`
6169+ MovingStateDataType ( DataType ) ,
6170+ /// `MSSPACE = moving_state_data_size` (in bytes)
6171+ MovingStateDataSize ( u64 ) ,
6172+ /// `MFINALFUNC = moving_final_function`
6173+ MovingFinalFunction ( ObjectName ) ,
6174+ /// `MFINALFUNC_EXTRA`
6175+ MovingFinalFunctionExtra ,
6176+ /// `MFINALFUNC_MODIFY = { READ_ONLY | SHAREABLE | READ_WRITE }`
6177+ MovingFinalFunctionModify ( AggregateModifyKind ) ,
6178+ /// `MINITCOND = moving_initial_condition` (a string literal)
6179+ MovingInitialCondition ( ValueWithSpan ) ,
6180+ /// `SORTOP = sort_operator`
6181+ SortOperator ( ObjectName ) ,
6182+ /// `PARALLEL = { SAFE | RESTRICTED | UNSAFE }`
6183+ Parallel ( FunctionParallel ) ,
6184+ /// `HYPOTHETICAL`. Marks the aggregate as hypothetical-set.
6185+ Hypothetical ,
6186+ /// `BASETYPE = base_type` (old aggregate syntax).
6187+ BaseType ( DataType ) ,
6188+ }
6189+
6190+ impl fmt:: Display for CreateAggregateOption {
6191+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
6192+ match self {
6193+ Self :: StateTransitionFunction ( name) => write ! ( f, "SFUNC = {name}" ) ,
6194+ Self :: StateDataType ( data_type) => write ! ( f, "STYPE = {data_type}" ) ,
6195+ Self :: StateDataSize ( size) => write ! ( f, "SSPACE = {size}" ) ,
6196+ Self :: FinalFunction ( name) => write ! ( f, "FINALFUNC = {name}" ) ,
6197+ Self :: FinalFunctionExtra => write ! ( f, "FINALFUNC_EXTRA" ) ,
6198+ Self :: FinalFunctionModify ( kind) => write ! ( f, "FINALFUNC_MODIFY = {kind}" ) ,
6199+ Self :: CombineFunction ( name) => write ! ( f, "COMBINEFUNC = {name}" ) ,
6200+ Self :: SerialFunction ( name) => write ! ( f, "SERIALFUNC = {name}" ) ,
6201+ Self :: DeserialFunction ( name) => write ! ( f, "DESERIALFUNC = {name}" ) ,
6202+ Self :: InitialCondition ( cond) => write ! ( f, "INITCOND = {cond}" ) ,
6203+ Self :: MovingStateTransitionFunction ( name) => write ! ( f, "MSFUNC = {name}" ) ,
6204+ Self :: MovingInverseTransitionFunction ( name) => write ! ( f, "MINVFUNC = {name}" ) ,
6205+ Self :: MovingStateDataType ( data_type) => write ! ( f, "MSTYPE = {data_type}" ) ,
6206+ Self :: MovingStateDataSize ( size) => write ! ( f, "MSSPACE = {size}" ) ,
6207+ Self :: MovingFinalFunction ( name) => write ! ( f, "MFINALFUNC = {name}" ) ,
6208+ Self :: MovingFinalFunctionExtra => write ! ( f, "MFINALFUNC_EXTRA" ) ,
6209+ Self :: MovingFinalFunctionModify ( kind) => write ! ( f, "MFINALFUNC_MODIFY = {kind}" ) ,
6210+ Self :: MovingInitialCondition ( cond) => write ! ( f, "MINITCOND = {cond}" ) ,
6211+ Self :: SortOperator ( name) => write ! ( f, "SORTOP = {name}" ) ,
6212+ Self :: Parallel ( parallel) => write ! ( f, "PARALLEL = {}" , parallel. as_str( ) ) ,
6213+ Self :: Hypothetical => write ! ( f, "HYPOTHETICAL" ) ,
6214+ Self :: BaseType ( data_type) => write ! ( f, "BASETYPE = {data_type}" ) ,
6215+ }
6216+ }
6217+ }
6218+
6219+ /// Modifier kind for `FINALFUNC_MODIFY` / `MFINALFUNC_MODIFY` in `CREATE AGGREGATE`.
6220+ ///
6221+ /// See <https://www.postgresql.org/docs/current/sql-createaggregate.html>
6222+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
6223+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
6224+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
6225+ pub enum AggregateModifyKind {
6226+ /// The final function does not modify the transition state.
6227+ ReadOnly ,
6228+ /// The transition state may be shared between aggregate calls.
6229+ Shareable ,
6230+ /// The final function may modify the transition state.
6231+ ReadWrite ,
6232+ }
6233+
6234+ impl fmt:: Display for AggregateModifyKind {
6235+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
6236+ match self {
6237+ Self :: ReadOnly => write ! ( f, "READ_ONLY" ) ,
6238+ Self :: Shareable => write ! ( f, "SHAREABLE" ) ,
6239+ Self :: ReadWrite => write ! ( f, "READ_WRITE" ) ,
6240+ }
6241+ }
6242+ }
0 commit comments