Allows to create multiple aggregates for the same Rust type
Closes #2077
Unfortunately, the rust doesn't allow using &'static str in const generic yet. This would allow simply moving an associative constant to a generic trait. Therefore an auxiliary trait ToAggregateName with associative constant NAME was added. This allows Aggregate to be implemented multiple times for the same type with different names.
This change introduces a bit of boilerplate. In order to simplify the use of this trait, a derive macro AggregateName has been developed, which automatically implements this trait. In this case, NAME becomes equal to the name of the structure for which this macro is used. To change the name of the name you should use attribute #[aggregate_name = “custom_name”].
Example:
#[derive(Copy, Clone, Default, Debug, PostgresType, Serialize, Deserialize)]
pub struct DemoOps {
count: i32,
}
struct DemoSumName;
// Using derive macro
#[derive(AggregateName)]
#[aggregate_name = "demo_sub"]
struct DemoSubName;
// Explicit trait implementation
impl ToAggregateName for DemoSumName {
const NAME: &'static str = "demo_sum";
}
// The first aggregate:
#[pg_aggregate]
impl Aggregate<DemoSumName> for DemoOps {
/* implementation */
}
// The second aggregate:
#[pg_aggregate]
impl Aggregate<DemoSubName> for DemoOps {
/* implementation */
}
BREAKING CHANGES: Now for the Aggregate trait you need to specify a generic parameter that implements the ToAggregateName trait. I tried to leave backwards compatibility, but decided to break it after all so that developers using pgrx would see that the semantics of the Aggregate trait had changed.
@eeeebbbbrrrr let's see if CI passes now? Seems @if0ne did some work for it.