pgrx
                                
                                
                                
                                    pgrx copied to clipboard
                            
                            
                            
                        Returning tuples from Aggs not supported
It would be great to be able to return a tuple like (i32, i32) from an Aggregate like you can with normal functions.
I'm having a go at doing this myself, but just wanted to make an issue to track.
Thanks @jamessewell ! Let me know if I can help at all.
I don't know if this is gonna work.
I eventually got it to compile once I realised I need to break out the #type_finalize type from .tr:
let type_finalize = &type_finalize.expect("no finalize type, but got finalize function").ty;
Then I can use #type_finalize as the return type here:
https://github.com/zombodb/pgx/blob/9f2164fe248d59edd7782b27f9ad6afc28b7c418/pgx-utils/src/sql_entity_graph/aggregate/mod.rs#L317
Then it compiles, but we get:
Re-using existing database gitaggb
psql (14.2)
Type "help" for help.
gitaggb=# create extension gitaggb ;
ERROR:  function demo_sum_finalize(demosum) returns a set
Normal functions seem to get round this by wrapping like so:
fn hello_a() -> impl std::iter::Iterator<Item = (i32, i32)> {
    Some({ (1, 1) }).into_iter()
}
But ...
Error[E0658]: `impl Trait` in type aliases is unstable
  --> src/lib.rs:24:21
   |
24 |     type Finalize = impl std::iter::Iterator<Item = (name!(this, i32), name!(that,i64))>;
   |                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: see issue #63063 <https://github.com/rust-lang/rust/issues/63063> for more information
I need a bag of magic :(
Yeah, impl Iterator in aggregates is.... going to need some design consideration. We'd need to use a concerete type. You should be able to get it to work with just a tuple though?
Hmmm I tried this on https://github.com/zombodb/pgx/commit/2817f84c8d954cbf2722bbe3ad38dd2062a9660b
However when I try to load an example:
ana@autonoma:~/git/zombodb/pgx/pgx-tests$ cargo pgx run pg14 --features pg_test
    Stopping Postgres v14
building extension with features `pg_test pg14`
"cargo" "build" "--features" "pg_test pg14" "--no-default-features" "--message-format=json-render-diagnostics"
    Finished dev [unoptimized + debuginfo] target(s) in 0.08s
installing extension
     Copying control file to /home/ana/.pgx/14.2/pgx-install/share/postgresql/extension/pgx_tests.control
     Copying shared library to /home/ana/.pgx/14.2/pgx-install/lib/postgresql/pgx_tests.so
 Discovering SQL entities
  Discovered 335 SQL entities: 32 schemas (2 unique), 284 functions, 12 types, 1 enums, 2 sqls, 0 ords, 0 hashes, 4 aggregates
     Writing SQL entities to /home/ana/.pgx/14.2/pgx-install/share/postgresql/extension/pgx_tests--1.0.sql
    Finished installing pgx_tests
    Starting Postgres v14 on port 28814
    Re-using existing database pgx_tests
psql (14.2)
Type "help" for help.
pgx_tests=# create extension pgx_tests;
ERROR:  function demo_count_and_sum_finalize(democountandsum) returns a set
                                    
                                    
                                    
                                
I ran across this today. I would like for the finalfunc to be a set returning function like:
     type Finalize = TableIterator<
         '_,
         (
             name!(time, Option<TimestampWithTimeZone>),
             name!(peak, Option<f64>),
         ),
     >;
Any ideas on what needs to be done in order to make this work?
My work around for now is to aggregate to JSONB then call another UDF that parses the JSONB back into a TableIterator. That can get rather expensive.