-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Avoid order by on sub queries in postgresql reflection #8561
Copy link
Copy link
Closed
Labels
bugSomething isn't workingSomething isn't workingpostgresqlreflectionreflection of tables, columns, constraints, defaults, sequences, views, everything elsereflection of tables, columns, constraints, defaults, sequences, views, everything else
Milestone
Description
I'm fairly sure that the order by we apply to subqueries in the postresql reflection, such as
sqlalchemy/lib/sqlalchemy/dialects/postgresql/base.py
Lines 3651 to 3678 in 9ae645d
| select( | |
| con_sq.c.conrelid, | |
| con_sq.c.conname, | |
| con_sq.c.description, | |
| pg_catalog.pg_attribute.c.attname, | |
| ) | |
| .select_from(pg_catalog.pg_attribute) | |
| .join( | |
| con_sq, | |
| sql.and_( | |
| pg_catalog.pg_attribute.c.attnum == con_sq.c.attnum, | |
| pg_catalog.pg_attribute.c.attrelid == con_sq.c.conrelid, | |
| ), | |
| ) | |
| .order_by(con_sq.c.conname, con_sq.c.ord) | |
| .subquery("attr") | |
| ) | |
| return ( | |
| select( | |
| attr_sq.c.conrelid, | |
| sql.func.array_agg(attr_sq.c.attname).label("cols"), | |
| attr_sq.c.conname, | |
| sql.func.min(attr_sq.c.description).label("description"), | |
| ) | |
| .group_by(attr_sq.c.conrelid, attr_sq.c.conname) | |
| .order_by(attr_sq.c.conrelid, attr_sq.c.conname) | |
| ) |
may not correct, since a db can technically ignore it from what I gather.
I don't think this ever happened in our test, but it's probably better to use an alternative or get confirmation that the query is indeed correct. @zzzeek I can try asking to the postgresql mailing list for help if you don't have a better suggestion.
Since the use case is use array_agg and a group by we investigate ordering in the array_agg construct, using aggregate_order_by.
In this case we would need to make aggregate_order_by generate a cache key, since at the moment it's not able to.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingpostgresqlreflectionreflection of tables, columns, constraints, defaults, sequences, views, everything elsereflection of tables, columns, constraints, defaults, sequences, views, everything else