feat: add countdistinct() aggregate function #4957
Conversation
added count distinct as countdistinct agg. tested to work, that is is gated if aggs are off. tested against collisions with tables and columns named countdistincts. Returns based on postgres rules name count().
|
Motivation on #915 (comment). Please check if you've followed https://github.com/PostgREST/postgrest/blob/main/CONTRIBUTING.md#ai-policy. |
Added count(distinct) as
Added count(distinct) as countdistinct aggregate. Tested to work, and that it is gated when aggregates are off. Tested against collisions with tables and columns named countdistinct. Returns type based on Postgres rules for count(). Extended test coverage to paths including spread path.
Added count(distinct) as countdistinct aggregate. Tested to work, and that it is gated when aggregates are off. Tested against collisions with tables and columns named countdistinct. Returns type based on Postgres rules for count().
233a4f5 to
3142d4c
Compare
So, the reason we are limited to the few aggregates as avg, min, max, sum and count right now is, I believe, not that we'd never want to allow others - but more that we haven't found a good way to solve some of the challenges around these, yet. In the general case, the I'm not sure whether it would work to implement this as a "modifier" as |
|
Hi Wolfgang Thank you for your feedback. I'm really open what you think is usefull to rewrite the code. That is fast done. Just tell me what fits better in the general architecture. Just a gut fealing about: count.distinct or count().distinct => they would be still different with the sql count(distinct ). For the first one would it be: count.distinct() ? For the second: count().distinct => just personal preference: we would then have 2 cases: one with modifier and one without - not impossible but would opt probably for count.distinct(). Just tell me what the prefered way is and I update my PR. Regards |
We already have a syntax for operators modifiers which unfortunately does not seem usable for aggregates modifiers. IMO Another idea could be using |
I'm opposed to that. A tilde is really just something, but imho not semantically connected to the distinct operation. It will be hard to understand - and additionally it's limiting, in case we'll want to support other modifiers.
I think a better analogy for the distinct operation is actually What But could we make |
Agree with
The idea was to replace |
|
Marking as draft until the feedback has been addressed and/or it has been confirmed that the |
Use Case: If I want to know the customers from the order table I need to do a count distinct
Solution: adding new count(distinct field) as countdistinct() and not as count(distinct). I took decision not to need to import params in the aggregates. But if you think count(distinct) would have been better: it would be about 10-15 lines more code and possible. But as I'm not familiar with the code the change looked more risky with count(distinct) as I couldn't foresee if I could potentially create some performance or memory issue.
Tested to work, that is is gated if aggs are off. tested against collisions with tables and columns named countdistincts.
Returns based on postgres rules name count(). (i.e. not countdistinct)
Added tests for the different cases if the amounts are correct.
Docs Updated.
This is my first PR so I'm open for feedback.