Please add array_agg function (add a series of PG ARRAY functions)

Issue #3132 resolved
Former user created an issue

PostgreSQL has this nice array_agg function, and it'd be awesome if sqlalchemy would know that it's return type is of ARRAY(type of thing passed in).

BTW, Thanks for making SQLAlchemy!

Comments (14)

  1. Mike Bayer repo owner

    oh you need to pass it through, then this one:

    from sqlalchemy.sql.functions import ReturnTypeFromArgs
    class array_agg(ReturnTypeFromArgs):
        pass
    

    feel free to test the feature there and let me know if _type_from_args() works in the case of ARRAY(something).

  2. Mike Bayer repo owner
    • Added support for the SQL-standard function :class:.array_agg, which automatically returns an :class:.Array of the correct type and supports index / slice operations. As arrays are only supported on Postgresql at the moment, only actually works on Postgresql. fixes #3132

    → <<cset cfae9c2eaf00>>

  3. Sebastian Bank

    Nice. Is it desired/possible to also support the ORDER BY clause of aggregates?

    So far, I have been using variations of this recipe for array_agg and also string_agg.

  4. Sebastian Bank

    Thanks! (with string_agg the call looks a little suprising, right?)

    Maybe the docs can give a hint to wrap array_agg in type_coerce(..., postgresql.ARRAY) if one wants to use contains or other postgres-specific methods.

  5. jmagnusson

    @zzzeek Does aggregate_order_by somehow support multiple ORDER BY arguments in it's current form?

  6. Mike Bayer repo owner

    we can make that easier but for the moment you can send a list of items as a sqlalchemy.sql.expression.ClauseList. Send a PR to make this more part of the API (see how Selectable.order_by does it).

  7. Log in to comment