Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Returning tuples from Aggs not supported #477

Open
jamessewell opened this issue Mar 10, 2022 · 5 comments
Open

Returning tuples from Aggs not supported #477

jamessewell opened this issue Mar 10, 2022 · 5 comments
Assignees
Labels
enhancement New feature or request rust+sql Interop between Rust and SQL

Comments

@jamessewell
Copy link
Contributor

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.

@Hoverbear
Copy link
Contributor

Thanks @jamessewell ! Let me know if I can help at all.

@jamessewell
Copy link
Contributor Author

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 :(

@Hoverbear
Copy link
Contributor

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?

@Hoverbear
Copy link
Contributor

Hmmm I tried this on 2817f84

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

@workingjubilee workingjubilee added the enhancement New feature or request label Aug 12, 2022
@workingjubilee workingjubilee added the rust+sql Interop between Rust and SQL label Aug 20, 2022
@trueb2
Copy link

trueb2 commented Dec 20, 2022

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request rust+sql Interop between Rust and SQL
Projects
None yet
Development

No branches or pull requests

4 participants