Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PostgreSQL Aggregate ORDER BY with sqlalchemy

I have a query which uses PostgreSQL's syntax for using ORDER BY in an aggregate function, something like this:

SELECT some_agg_func(a ORDER BY b DESC) FROM table;

Does anyone know of anyway to do this with the sqlalchemy expression language?

like image 648
Alex jg Avatar asked Jan 29 '13 14:01

Alex jg


3 Answers

From the SQLAlchemy Docs:

from sqlalchemy.dialects.postgresql import aggregate_order_by

expr = func.array_agg(aggregate_order_by(table.c.a, table.c.b.desc()))
stmt = select([expr])
like image 94
avyk37 Avatar answered Oct 17 '22 22:10

avyk37


I think SqlAlchemy 1.1 has native support for string_agg now: http://docs.sqlalchemy.org/en/latest/dialects/postgresql.html#sqlalchemy.dialects.postgresql.aggregate_order_by (search the page for "func.string_agg")

like image 41
Mar Avatar answered Oct 17 '22 22:10

Mar


You need to use SQLAlchemy's compiler extension to achieve this. Here's an example for Postgres string_agg function:

from sqlalchemy.sql.expression import ColumnElement, _literal_as_column
from sqlalchemy.dialects import postgresql
from sqlalchemy.ext.compiler import compiles

class string_agg(ColumnElement):
    def __init__(self, expr, separator, order_by=None):
        self.type = Text()
        self.expr = _literal_as_column(expr)
        self.separator = literal(separator)
        self.order_by = _literal_as_column(order_by)

    @property
    def _from_objects(self):
        return self.expr._from_objects

@compiles(string_agg, 'postgresql')
def compile_string_agg(element, compiler, **kwargs):
    head = 'string_agg(%s, %s' % (
        compiler.process(element.expr),
        compiler.process(element.separator)
    )
    if element.order_by is not None:
        tail = ' ORDER BY %s)' % compiler.process(element.order_by)
    else:
        tail = ')'
    return head + tail

query = session.query(string_agg(Foo.bar, ', ', order_by=Foo.bar.desc()))
# Print compiled SQL query.
print query.statement.compile(dialect=postgresql.dialect())
# Run the query and print result.
print query.scalar()
like image 1
Audrius Kažukauskas Avatar answered Oct 17 '22 22:10

Audrius Kažukauskas