You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Closespgcentralfoundation#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:
```rust
#[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.
This creates `sql/exploring_aggregates-0.0.0.sql`:
338
338
339
339
```sql
340
-
/*
340
+
/*
341
341
This file is auto generated by pgrx.
342
342
343
343
The ordering of items is not stable, it is driven by a dependency graph.
@@ -378,6 +378,10 @@ but it should be flexible enough for any use.
378
378
Aggregates in `pgrx` are defined by creating a type (this doesn't necessarily need to be the state type), then using the [`#[pg_aggregate]`][pgrx-pg_aggregate]
379
379
procedural macro on an [`pgrx::Aggregate`][pgrx-aggregate-aggregate] implementation for that type.
380
380
381
+
The aggregate name is specified through a type parameter that implements the `ToAggregateName` trait.
382
+
Or you can use a derive-macro `AggregateName` to automatically implement this trait for some type.
383
+
The default name of the aggregate will be taken as the name of the structure, but you can change this with the `aggregate_name` attribute
384
+
381
385
The [`pgrx::Aggregate`][pgrx-aggregate-aggregate] trait has quite a few items (`fn`s, `const`s, `type`s) that you can implement, but the procedural macro can fill in
382
386
stubs for all non-essential items. The state type (the implementation target by default) must have a [`#[derive(PostgresType)]`][pgrx-postgrestype] declaration,
383
387
or be a type PostgreSQL already knows about.
@@ -390,13 +394,14 @@ use serde::{Serialize, Deserialize};
SELECT DemoUnique(value) FROM UNNEST(ARRAY ['a', 'a', 'b']) as value;
659
-
-- demounique
635
+
-- demounique
660
636
-- ------------
661
637
-- 2
662
638
-- (1 row)
@@ -674,11 +650,11 @@ PostgreSQL also supports what are called [*Ordered-Set Aggregates*][postgresql-o
674
650
Let's create a simple `percentile_disc` reimplementation to get an idea of how to make one with `pgrx`. You'll notice we add [`ORDERED_SET = true`][pgrx::aggregate::Aggregate::ORDERED_SET] and set an (optional) [`OrderedSetArgs`][pgrx::aggregate::Aggregate::OrderedSetArgs], which determines the direct arguments.
0 commit comments