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

Array aggregate with ORDER BY clause #2094

Closed
iksteen opened this issue Jan 24, 2020 · 3 comments
Closed

Array aggregate with ORDER BY clause #2094

iksteen opened this issue Jan 24, 2020 · 3 comments

Comments

@iksteen
Copy link

iksteen commented Jan 24, 2020

Postgresql supports ordered array aggregation in the form of:
ARRAY_AGG(expression [ORDER BY [sort_expression {ASC | DESC}], [...])

The way I currently implement it:

fn.array_agg(NodeList([
    subscriptions_q.c.topic_id,
    SQL("ORDER BY"),
    popularity_q.c.popularity.asc(),
]))

Do you happen to know a nicer way of implementing array_agg's order by clause using peewee? If not, would you be willing to accept a PR that adds a function that adds this functionality to f.e. playhouse.postgres_ext?

@coleifer
Copy link
Owner

The method you're describing is an acceptable way of handling this. I'm going to pass on implementing a fix right now, but may reconsider it later.

coleifer added a commit that referenced this issue Jan 25, 2020
I've decided to fix, but opted for a slightly hacky way of doing this.
Instead of copying the NodeList() hack to avoid double-wrapping
expressions in parentheses, I've chosen to modify the last argument
in-place to extend it with ordering information.

Fixes #2094
@coleifer
Copy link
Owner

Actual fix is now in: fe84246

@iksteen
Copy link
Author

iksteen commented Jan 25, 2020

That's awesome! Thanks for the quick response (and implementing the feature!).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants