Skip to content

Multiple aggregate on same type #2077

Description

Hello, I'm I would like to define multiple aggregate functions on one of my types: https://github.com/JeanChristopheMorinPerso/data_experiments/blob/c47bca89aab29d96a25a96a475c63a438707a9cd/conda_pgsql_rust_ext/src/lib.rs#L7-L12

I can add at least one aggregate function using the pg_aggregate macro like this:

#[pg_aggregate]
impl Aggregate for CondaVersion {
    type State = CondaVersion;
    type Args = CondaVersion;
    const NAME: &'static str = "min";
    const SORT_OPERATOR: Option<&'static str> = Some("condaversion_lt");

    fn state(
        current: Self::State,
        arg: Self::Args,
        _fcinfo: pg_sys::FunctionCallInfo
    ) -> Self::State {
        // NOTE THAT I HAVE NOT YET TESTED THIS IMPLEMENTATION
        if current <= arg {
            return current;
        }
        return arg;
    }
}

This results in

CREATE  FUNCTION "conda_version_state"(
        "this" CondaVersion, /* conda_pgsql_rust_ext::CondaVersion */
        "arg_one" CondaVersion /* conda_pgsql_rust_ext::CondaVersion */
) RETURNS CondaVersion /* conda_pgsql_rust_ext::CondaVersion */
STRICT
LANGUAGE c /* Rust */
AS 'MODULE_PATHNAME', 'conda_version_state_wrapper';


CREATE AGGREGATE min (
	CondaVersion /* conda_pgsql_rust_ext::CondaVersion */
)
(
	SFUNC = "conda_version_state", /* conda_pgsql_rust_ext::CondaVersion::state */
	STYPE = CondaVersion, /* conda_pgsql_rust_ext::CondaVersion */
	SORTOP = "condaversion_lt" /* conda_pgsql_rust_ext::CondaVersion::SORT_OPERATOR */
);

But I'm kind of failing to see how I could define a second one... Say, a max aggregate.

Is this possible with pgrx? I'm not very good at rust, so forgive me if this is more a rust question than a pgrx question.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions